From patchwork Sat Feb 3 00:09:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196108 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp770435dyc; Fri, 2 Feb 2024 16:13:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFMwvX58e2sqy5462V8hlSmuEQVEs3HPuuWg9zGTw+NZbCCldvnCO3RXlzFn7/2Y3xHz6oa X-Received: by 2002:a05:6402:5248:b0:560:1372:d950 with SMTP id t8-20020a056402524800b005601372d950mr561293edd.22.1706919209345; Fri, 02 Feb 2024 16:13:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919209; cv=pass; d=google.com; s=arc-20160816; b=GyopnejWMM+Mo09Tl6AT8eV8DezM1wh0QwZnutMGooXMDZgaH14XujPghvTCMombHB zvZazzGTRxB0hikIVOu4gUrnY7wt+QCkjTn36kFMSY7CeeuF8FJbt27rLGJIRcFrPDqM 9kH6TicGksMTyey/mJbCkDjru2jcPLhnPI+BQmOfmKh3EiqxHpZ/BaWNpHbXGIn2qx+c PcYlYyNjAZpeaMZLjesOyKI6Ajzx+7rlHG5LhDf3HT0E/bN2UmAqNJlvQ9EXzHXTC79R INR3Vf7oaPvD1MeR0oIx/dPrGcw0qAy1o7e0MMXXpLw8f2eSQrcAF5jBfpe/wc+GoZfA n6nA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=VzhuhwHBOVTPe3ek7qdXtFf4wVMa0yBW9i03fvIkD6g=; fh=Dd1IfGRW1muX1NS2R3mh8Dtohf9olMCyDSM5MxIBL80=; b=MfX4yb40KsDefV+FAPMctQhupCVmcFabwS7uz3y7+8Og+erzQgKxeGRXjgUPm9LjfC UJvc4ftnJ9/5L5V8VrGvuLR3g36Vb6skz7u2dd3IttwJ1vxOzCsHQKJNoG9ekqNzlhzJ QEeCZlisNEd9nVNDTz7z2IdENWfK5Y/F/TDtdmqkuVXtMwsaZoZB91ANLjBnCtUBqMvz RWF6NxfsbbTH3Bqxh/0ZM9rxChaIkhXyRBa5jNLKuhvQ/cEpLGFENdM9B+SWGSuELOb8 RFFHR0gmi/dua1BF4tC0yOFBCZNK0VF+xjVTMe1M9adY81UxJ2iPKoAChGOOgSyng4SM Arbg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bnt6gnw2; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCUBXpknROpD5pYzidKFNlqyhFC4s7kK0dpeSIHVlu0jtGWnCO1oMP9SbltztZaNjouilKoKdMIvr7gyly2O2EUPPiZENg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id b18-20020a50b412000000b0055a61376aeesi1259044edh.240.2024.02.02.16.13.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:13:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bnt6gnw2; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B38191F25FBA for ; Sat, 3 Feb 2024 00:13:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 262C11B96B; Sat, 3 Feb 2024 00:09:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bnt6gnw2" Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7156013FF1 for ; Sat, 3 Feb 2024 00:09:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918983; cv=none; b=Hrux55hNm+rWgllrA0pipFv2yz2hVOkokknLQcX7016530P1P8oKOHWFaewZR9tsxhRyfXYyVnmb6K+QYVgBy+pqLnsdTjR/ok7TdEwPREAb84VfeCQqRzUs9DDKLbgYrlB5TYFeALCCnQaZd5Kmq+pxvIVo56ZnWws2nMdnMHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918983; c=relaxed/simple; bh=dxFDbsAQMhSueKOp2cttxeSOtYuXNHRkrePZ7bjyZ1U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mxaN/tHkl3hRQO3jN2BT0uUwq8YE1c1gHWH6yRVIjxY1lQTftqP5lnpTLSR/Z22sJxALyo1/xBxPBFpJzYZoZ+7pAwBwpGBXyx4f0oTps0WZwf/Nk+ZQrg58gEyYgcohHQ8HSo8c8QhQpF/l3vtaw+tw/gQYnY9zxlSmV0yCBbI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bnt6gnw2; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1d968aebbd1so17556115ad.1 for ; Fri, 02 Feb 2024 16:09:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918981; x=1707523781; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=VzhuhwHBOVTPe3ek7qdXtFf4wVMa0yBW9i03fvIkD6g=; b=bnt6gnw2arROnor157+UAWxYa02ZsadT4zbEncIYG1BpJmmwPx3eKR0fMf6BoTeaa+ im0SIt34/wbwKyGJQFpoZ58msCstKa5ohg9dE2iFJYZjwWMIeJv9eVoyzIR5EFo1kPnq o1agCJhmWI1yDdbOnYZ7riV5WVHY3zelYYKBaBsPoaiV5vgfyv9qz0HRw19ETK+/Bcj7 F07UAet363tPsJK0hPUeTy43teKgkl13TL5viArqNF91cD/oav62QkDvzr5VsJ7+Sq8F KcyHc7o59UmfWLJNQK3HOoa0tNgMhqifnBx6Z31ThKC2tzPm8d4XspGqgJt+OaCpsK4W 6mUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918981; x=1707523781; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VzhuhwHBOVTPe3ek7qdXtFf4wVMa0yBW9i03fvIkD6g=; b=FuXjpUd+tToOagUByXg2eYLWbpHydWCgDHpFAvuIJ8zfwYxGTIwmqiHmQjj9RFa/we 7qoGmtwaaIRQR1z9rNw18LZ4XYw89OEiWCJmT9xh8myFPZV4+lULKsh/3HHagXItjieL VsFKiRTbc0P5MM748lORvK4Fxj0PkjrM3cAe76TMx6olhmVg8jBhrthCvSS9pkql/yne 4PsvZ64EDUrllO5Pv73gXYwMhbx1Xb6HbvUU5XbuhqgbpPop2BweyoulbZaRDYgieZqp Xw8fnR5oyn75dbx0bAbi6mSwB0Fxg9sPlWV5kYHIHlo51zf9xMqaJaTvnf6GCR2Pewqu +I5A== X-Gm-Message-State: AOJu0YyEYkebPSjwwqLQjyaQ2AOWmvrEHRmrQ3UVBhNjiOrjSEFt8d1u /obCzwm70tk6qgNdL5KEeXQsNUhqLviLzIcBtke8rYBToqZn5ZOj7pm1PW5fc4ztGq95iI0zVe5 Pxw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e741:b0:1d9:1ca3:1883 with SMTP id p1-20020a170902e74100b001d91ca31883mr51751plf.5.1706918980700; Fri, 02 Feb 2024 16:09:40 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:16 -0800 In-Reply-To: <20240203000917.376631-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-11-seanjc@google.com> Subject: [PATCH v8 10/10] KVM: selftests: Add a basic SEV smoke test From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834516912618550 X-GMAIL-MSGID: 1789834516912618550 From: Peter Gonda Add a basic smoke test for SEV guests to verify that KVM can launch an SEV guest and run a few instructions without exploding. To verify that SEV is indeed enabled, assert that SEV is reported as enabled in MSR_AMD64_SEV, a.k.a. SEV_STATUS, which cannot be intercepted by KVM (architecturally enforced). Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Suggested-by: Michael Roth Signed-off-by: Peter Gonda [sean: rename to "sev_smoke_test"] Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/sev_smoke_test.c | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_smoke_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 169b6ee8f733..da20e6bb43ed 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -120,6 +120,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests +TEST_GEN_PROGS_x86_64 += x86_64/sev_smoke_test TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c new file mode 100644 index 000000000000..c1534efab2be --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "linux/psp-sev.h" +#include "sev.h" + +static void guest_sev_code(void) +{ + GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV)); + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED); + + GUEST_DONE(); +} + +static void test_sev(void *guest_code, uint64_t policy) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + vm = vm_sev_create_with_one_vcpu(policy, guest_code, &vcpu); + + for (;;) { + vcpu_run(vcpu); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + continue; + case UCALL_DONE: + return; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + default: + TEST_FAIL("Unexpected exit: %s", + exit_reason_str(vcpu->run->exit_reason)); + } + } + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(is_kvm_sev_supported()); + + test_sev(guest_sev_code, SEV_POLICY_NO_DBG); + test_sev(guest_sev_code, 0); + + return 0; +}