From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.66]) by mx.groups.io with SMTP id smtpd.web11.5553.1625057655099338516 for ; Wed, 30 Jun 2021 05:54:15 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=JQGN7Eqt; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.223.66, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jPPGddHw9Gr5UbkD/VIW7Tsw7MjsdRRK60VFq1UGCH17PXAGSDMP6KYmEuUqxhfaypf5ZD1vlKh8+P+qK8sbZS8WaGYi2RB6fJPI4IVO4Q5+lXXayqjHA1PnCGzHu8RvTAM6/FmJyCF8YtEsFTRgL/fGP9+m1Fz0PLoD07Mx++kpWk8SWc7C+Vd6VslWPOJT5vGXBa+kJIS4lab1Kpuum+VSOKE4fIa5NaYOm80BPLCAxrYcvbBZj6Ow9wN2/joR3GMBs7rd3VWFjGmdrS8RHW0kn1X39pOqE9HiPyqtXczSWLYYMiYMV7Vyc7lfDKTA6ihAhUHusBUxdoM3/KvhzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BpJywUdqMzV59Aau7s5e6D1tCb/jKYn4wlqWyBOk3z8=; b=bz2H9xxgxdvghtjVgSThZgZrK8B3rkpTEf5PWlFdettcwXxKs+mIUfg8c03f61sm2DLJU9aOg8FA0f7H+Begq1wk6L+P5gAsf8k6Awve+62Hzb+fcK3biGV9NZPsUKrQLGxx3h2SmtJtDIjCX3rNZLwOQWuzMjY+NWY19q7crESaH9a87MNzZhOyhnl0B//0TtNwzz74/DkYL51k18i7gMJBnelRVE8N3QAsmsSFWthzy9HdmBPQGPlzdGG80BPXPRrFjbCA/2IQSWyFNJoUMfLwTxi/2q4y4Q3+vye+C4t+yDSRXK9Z5AY4ejaqauZdEwRm+Jc6VI6JnPjyuvGwbg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BpJywUdqMzV59Aau7s5e6D1tCb/jKYn4wlqWyBOk3z8=; b=JQGN7Eqt8jA9bwqOmLCgXfpKf4uPP3gV+YQL47CUqRzx8T/gBCdZOtYq19xhmn3KwmkfMVlHvAlhNdO2Ty1VfbLZmdzK9wgRORAIidJUr0ahtHpw9Jjq+feWwstbudzV88ZV0HJUzHgsU/7Dm6mZK12dVFELoeaN2b8XLIQAJaM= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from DM6PR12MB2714.namprd12.prod.outlook.com (2603:10b6:5:42::18) by DM5PR1201MB0076.namprd12.prod.outlook.com (2603:10b6:4:55::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4264.20; Wed, 30 Jun 2021 12:54:13 +0000 Received: from DM6PR12MB2714.namprd12.prod.outlook.com ([fe80::7df8:b0cd:fe1b:ae7b]) by DM6PR12MB2714.namprd12.prod.outlook.com ([fe80::7df8:b0cd:fe1b:ae7b%5]) with mapi id 15.20.4264.026; Wed, 30 Jun 2021 12:54:13 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io CC: James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Laszlo Ersek , Erdem Aktas , Eric Dong , Ray Ni , Rahul Kumar , Michael D Kinney , Liming Gao , Zhiguang Liu , Michael Roth , Brijesh Singh Subject: [RFC PATCH v5 09/28] OvmfPkg/ResetVector: check the vmpl level Date: Wed, 30 Jun 2021 07:53:02 -0500 Message-ID: <20210630125321.30278-10-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210630125321.30278-1-brijesh.singh@amd.com> References: <20210630125321.30278-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA9PR13CA0018.namprd13.prod.outlook.com (2603:10b6:806:21::23) To DM6PR12MB2714.namprd12.prod.outlook.com (2603:10b6:5:42::18) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA9PR13CA0018.namprd13.prod.outlook.com (2603:10b6:806:21::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.14 via Frontend Transport; Wed, 30 Jun 2021 12:54:11 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c5d05df9-9e0d-4d61-f660-08d93bc6291d X-MS-TrafficTypeDiagnostic: DM5PR1201MB0076: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n9q01PXYRZ1Z7RSbJKaRRauFrQ4D3r/v4lCJQ3DmOuOAk6XAMT99aqdA9cC3LkqlqnzrOOcXaumwxZhYEtb3EWPPyXJaWldGQxyYcE0W88y4Y7KcVHqooNoBEZN8OSFmvCg0uVsEyvTRsT52a4yBniC0QuyKTufrWNbr3q0DK/q/DxWDdaeysIhZgiu/z/82PhUhMOy/DVwaAlJ2yCuqTV8fQFnBo+RRW1WqsRURofrDpoy07tJ6dXECGLrizdXai01L0WPo90ucfFN3f+7JRUdTe5EFl5Ej/J5kQgAvFgMe+jb630pvyXNuZ7xefA3eV/HpPbJxN4SNSs0NJsr5hVjwq0tdrGEcdhYnnTg3bO2ue3XEeGodwbFuwxD3Fl+f532P73VOHVLF/k+gFqUxEbHm7uJwWpFAWMWrUSSQXyN/A+taJNSjkOnRXegUkYqkd9jkGwNrClCO2dXc8CczZy9p+rl55QmUkbUoAyXNCIa/NV9aK4Zfdoa2qMRBk5qWLd8UOOMph2bwK0W5UlXq5SEP9TDBlDu0eLFeZy1nJzhX10KG18IbvEwSGC/1t1rHdBREAZkiB7l409UkLcKPL/fT8UQHsiYWHlBE0mc771f1Zc1a63TsNbWPF98T954NFaKIEtWTCTIRhFJEKm2N9RHduKiw7E0vwRSKj7ku+Uo5WQPANTKDoOFateXHAeuKDRf4XFYPdxZmfsIcnfOg5r2kKhWR5VKnxSJz+iqkKcEr4RwZ2ndQl3CgogumlPzKW0kqFOjxsThb9Xx0O+0mABkKh+pmHDbxOPiXUoHzGwBB3QAlW+UCoEQyBK+/UuwbqIxL7aIv86py7sET/PKSYQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB2714.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(366004)(346002)(39860400002)(396003)(7696005)(1076003)(8676002)(16526019)(8936002)(6666004)(2616005)(4326008)(956004)(38350700002)(38100700002)(478600001)(186003)(6486002)(19627235002)(66556008)(66476007)(26005)(7416002)(316002)(966005)(86362001)(66946007)(52116002)(36756003)(54906003)(5660300002)(2906002)(6916009)(44832011)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?M0dL3tsd+9r7+Zcarbjp0J5H22gybVAWZPelOzyY7gzhgWYUe4IeNlPg627J?= =?us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UCgq2JSl3QgjT5XHrtWWw1KryXgu2?= =?us-ascii?Q?Motp4XYvIk4t85MAcjRFZCrtoTtqD81yQHpZl0OhOMvkZYl+Lr79qLaCkzFr?= =?us-ascii?Q?wLqOGDDSeELlZ0A/9daJW77SF4r22UdZruNctQbDy97Du+D0AMSrY6DbbZoq?= =?us-ascii?Q?GlXL0CTHmOPMcmVRrTlvhiR9q6Wo4rmTrukwAWobdGmdCEFgy5XW25a4Py2v?= =?us-ascii?Q?1oONWBjI6nh0aeuCZI2PtUVKFfUwyCHJQHzkJzHy7vBjkeU1gTcdekE0fhkS?= =?us-ascii?Q?ZgEYYnq6LEPqDpNDlr3mOaZzV/2VMCFlBpQQo6cPsyvTO7sWSoSFqAa5bZ9G?= =?us-ascii?Q?oWEpcip3B6ecm2PYGybaIKDFAp7pJFrmHQw/txjq1U+oNdcjVEJmdq2Hvnat?= =?us-ascii?Q?6+ZevGDDHYBaygwRUtb7c8Mr?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c5d05df9-9e0d-4d61-f660-08d93bc6291d X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB2714.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jun 2021 12:54:13.0967 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: D2lfMYVBWtPfXMCnquY74iJX6JWtUjkCOTUQMeLoKnCxLysOdm2AYrnXJmtTYDWaFX8H+GemtLNURWYLq4Z5+g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0076 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 Virtual Machine Privilege Level (VMPL) is an optional feature in the SEV-SNP architecture, which allows a guest VM to divide its address space into four levels. The level can be used to provide the hardware isolated abstraction layers with a VM. The VMPL0 is the highest privilege, and VMPL3 is the least privilege. Certain operations must be done by the VMPL0 software, such as: * Validate or invalidate memory range (PVALIDATE instruction) * Allocate VMSA page (RMPADJUST instruction when VMSA=3D1) The initial SEV-SNP support assumes that it's running on VMPL0. Let's add a check to make sure that we are running at VMPL0 before continuing the boot. There is no easy method to query the current VMPL level. One simple approach is to call PVALIDATE instruction and if the instruction causes a #GP then its SEV-SNP guest is not booted under VMPL0. See the AMD APL volume 3 (PVALIDATE) for additional information on the PVALIDATE. Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Laszlo Ersek Cc: Erdem Aktas Signed-off-by: Brijesh Singh --- OvmfPkg/ResetVector/Ia32/AmdSev.asm | 89 ++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/ResetVector/Ia32/AmdSev.asm b/OvmfPkg/ResetVector/Ia32= /AmdSev.asm index b6f33d049a43..a9101ca8b8b2 100644 --- a/OvmfPkg/ResetVector/Ia32/AmdSev.asm +++ b/OvmfPkg/ResetVector/Ia32/AmdSev.asm @@ -53,6 +53,12 @@ BITS 32 ; Hypervisor does not support SEV-SNP feature %define TERM_HV_UNSUPPORTED_FEATURE 4 =20 +; SEV-SNP guest is not launched at VMPL-0 +%define TERM_SNP_NOT_VMPL0 5 + +; The #VC is not for PVALIDATE +%define TERM_VC_NOT_PVALIDATE 6 + ; GHCB SEV Information MSR protocol %define GHCB_SEV_INFORMATION_REQUEST 2 %define GHCB_SEV_INFORMATION_RESPONSE 1 @@ -139,6 +145,25 @@ BITS 32 SevEsUnexpectedRespTerminate: TerminateVmgExit TERM_UNEXPECTED_RESP_CODE =20 +; Check whether we're booted under the VMPL-0. +; +; There is no straightforward way to query the current VMPL level. The sim= plest +; method is to use the PVALIDATE instruction to change the page state. If = its +; not a VMPL-0 guest then PVALIDATE will cause #GP. +; +CheckSnpVmpl0: + ; This routine is part of the ROM, and should have been validated by t= he SNP + ; guest launch sequence. So its safe to re-validate the page containin= g + ; this routine. + mov eax, ADDR_OF(CheckSnpVmpl0) + mov ecx, 0 + mov edx, 1 + PVALIDATE + + ; We will reach here only if we are running at VMPL-0. + + OneTimeCallRet CheckSnpVmpl0 + ; Check if Secure Encrypted Virtualization (SEV) features are enabled. ; ; Register usage is tight in this routine, so multiple calls for the @@ -193,6 +218,17 @@ CheckSevFeatures: bt eax, 0 jnc NoSev =20 + ; Check if we're SEV-SNP guest and booted under VMPL-0. + ; + ; This check should happen here because the PVALIDATE instruction + ; used in the check will cause an exception. The IDT is active + ; during the CheckSevFeatures only. + ; + bt eax, 2 + jnc SkipCheckSnpVmpl0 + OneTimeCall CheckSnpVmpl0 + +SkipCheckSnpVmpl0: ; Check for SEV-ES memory encryption feature: ; CPUID Fn8000_001F[EAX] - Bit 3 ; CPUID raises a #VC exception if running as an SEV-ES guest @@ -393,6 +429,36 @@ IsSevEsEnabled: SevEsDisabled: OneTimeCallRet IsSevEsEnabled =20 +; Start handling of #GP exception handling routines +; +SevEsIdtNotPvalidate: + TerminateVmgExit TERM_VC_NOT_PVALIDATE + iret + +SevSnpGpException: + ; + ; If we're here, then we are an SEV-SNP guest and this + ; was triggered by a PVALIDATE instruction. + ; + ; Verify that its an PVALIDATE instruction + ; The exception stack looks like this: + ; +---------+ + ; | .... | + ; | eip | + ; | err code| + ; +---------+ + pop ebx + pop ebx + mov ecx, [ebx] + cmp ecx, 0xff010ff2 ; Compare EIP with PVALIDATE menomics + jne SevEsIdtNotPvalidate + + ; The #GP was triggered by the PVALIDATE instruction, this will happen + ; only when we're not running at VMPL-0 + ; + TerminateVmgExit TERM_SNP_NOT_VMPL0 + iret + ; Start of #VC exception handling routines ; =20 @@ -522,15 +588,34 @@ ALIGN 16 ; IDT_BASE: ; -; Vectors 0 - 28 (No handlers) +; Vectors 0 - 12 (No handlers) ; -%rep 29 +%rep 13 dw 0 ; Offset low bits 15..0 dw 0x10 ; Selector db 0 ; Reserved db 0x8E ; Gate Type (IA32_IDT_GAT= E_TYPE_INTERRUPT_32) dw 0 ; Offset high bits 31..16 %endrep +; +; Vector 13 (GP Exception) +; + dw (ADDR_OF(SevSnpGpException) & 0xffff) ; Offset low bits 15..0 + dw 0x10 ; Selector + db 0 ; Reserved + db 0x8E ; Gate Type (IA32_IDT_GAT= E_TYPE_INTERRUPT_32) + dw (ADDR_OF(SevSnpGpException) >> 16) ; Offset high bits 31..16 +; +; Vectors 14 - 28 (No handlers) +; +%rep 15 + dw 0 ; Offset low bits 15..0 + dw 0x10 ; Selector + db 0 ; Reserved + db 0x8E ; Gate Type (IA32_IDT_GAT= E_TYPE_INTERRUPT_32) + dw 0 ; Offset high bits 31..16 +%endrep + ; ; Vector 29 (VMM Communication Exception) ; --=20 2.17.1