From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web08.6093.1620863210224608460 for ; Wed, 12 May 2021 16:46:51 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=2Vd/9zyK; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: , mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iSZuvDs6rEoScXctrQVisWt7PJpWoILkZQintj70nDJhbM6k4ohkwZDyzegnsnMimG6X1a0sllXqonYuHjUS3BFG7F4unbcNVVjC/4UQSNEgKKUQdJgPLBBwZPbOoWeFOfu7W5gZuHO7ozRgj8y1WmhqlYeb2ODksi/m+3ZIz5aaKjwA7fUJ7KDKByGPpDcNltQnfEDLkfWT97h17K1+icIToy+iCNaYwmnEGOLUMsMWA7HKHtOpQe4L9zF+BbRJb9qrgjdynr9woqUu6sZd9tJI8Y2AtMeBRigbIhiEQxm9az2jJqRrbtzWxCT+SXyip8YNYykGgrD/hczhjWFihQ== 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=LI1uichFS8SJjgCXla5b56aAT0mDIzIYmz5Pa8o8PZM=; b=m/OOf3wgfCtxiywnduKwsdvz5PSRps3dFj3R1N56JArasYh1KY1x4j9mSnSkdKYEkSQnNJl7PBPpW03BdLooxFW/PvH5nC+sCjUvCtHndniBBtJ1umZxCaVwG5H6THdzcT9HlGNoMkIiYcf8l03CJlblf2/4PX0BQa7Civma0NGcurOnrEsCTtY425IN8+baeoBp1WiYCfSyL5c85vKSjqoCBMpbgrrpiRzQLbGUDnH3+MtJ3gfozZpPdReNiXlECRalHPxxqtorDtKEBYOoVNSDCrw2yxjhgD9oFmqXzxqOZ8FEcc3Y5chTShyFKHH9U3XPp/YmVPrCowSR7xzArw== 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=LI1uichFS8SJjgCXla5b56aAT0mDIzIYmz5Pa8o8PZM=; b=2Vd/9zyK+47rH4mzOuoM5L/hOjBLKb6sDyMIrcKlMHJpZ5HDUEqf+4NTXNuyKHbMIj5vMpx7CDfTt40wGPiZjFA0N6Wf22QNixsNIKIVFUYGoruR20cuKDSzcmzG+skwCO4HDa+ZOQ2oDRRD1Ro0TlbzNe7DugAT5sQ0xlnELtA= 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 SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2445.namprd12.prod.outlook.com (2603:10b6:802:31::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4108.26; Wed, 12 May 2021 23:46:45 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4129.025; Wed, 12 May 2021 23:46:45 +0000 From: "Brijesh Singh" To: devel@edk2.groups.io CC: Brijesh Singh , James Bottomley , Min Xu , Jiewen Yao , Tom Lendacky , Jordan Justen , Ard Biesheuvel , Laszlo Ersek , Erdem Aktas , Michael D Kinney , Liming Gao , Zhiguang Liu Subject: [PATCH v2 07/13] MdePkg/BaseLib: add support for PVALIDATE instruction Date: Wed, 12 May 2021 18:46:09 -0500 Message-ID: <20210512234615.1726-8-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210512234615.1726-1-brijesh.singh@amd.com> References: <20210512234615.1726-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR13CA0003.namprd13.prod.outlook.com (2603:10b6:806:130::8) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) 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 SA0PR13CA0003.namprd13.prod.outlook.com (2603:10b6:806:130::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4150.11 via Frontend Transport; Wed, 12 May 2021 23:46:43 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f9173be1-a568-4de2-0152-08d915a032b6 X-MS-TrafficTypeDiagnostic: SN1PR12MB2445: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n0+LhhvvlEP1A6zQfNSfb/TW/SbY+U+laYe5CCwV+GZFsGngcy2Hj501BQqs3j9zcurXlryrIo9rzlqd4NaxFw32yxznne84TnIRcd2ZrQ5uUVwDOaUCgWrCNmoM7hIBXy2ijIEXQoR+H4dzjKF/eAdSMWVfSv95Q/HNYI9buIsIakj0VG6l/Fg5DA5X7QOI0Kl/v8uS3PL5lw5KeuKRN7B1yLa95wgRVVnYOn3A3RIG88IxmXSYGhRGayVVjHbnTRBK1XjgRTLvkligVlVgESkvto2sJNovKkTNAhPkzTPDpyWR/UL1cRHRmyymXxegJZqUElX67frDnzvIINKOon9Ys/QV4Qk4sZzQEVTRoZgPJWdYCWgjADLfdAKOQEMDJZGM0fU3dUfqe3L0MDm3yUWcv9XipVCuV9B9EDKpaoFfLNi3Ig4aDuIlzegOF1Npt+zvA4Rdxh64TBAOHKkyGswVq6K/6Pxb+WgkdmXmdirJcjL8QBVGtr9AzGDPUka97ELCzQ4Y7a1h/cQ/G4fHb0YyzBGxehPqwZsQYkZaJ5zuNEeKHqPa1VvRYNER70bY8+kPiOJ0hu6JS7Sn3IutPlvbm5bZQAb31nwLpiN0d1XoBm874QT6iTjH7em9gOuEGJ3uib/viEgBQwSbWKgrS1oTsSfFOxub5yYlb+biMQMy7S0X8AQpgPOCyPCSrc7KM+91pGoboalpjD46a9GMaYWv0PoS8JXZ7VhD3jx8Vos= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(39850400004)(396003)(346002)(366004)(6486002)(54906003)(8936002)(2616005)(7696005)(478600001)(2906002)(966005)(8676002)(19627235002)(956004)(5660300002)(1076003)(44832011)(83380400001)(52116002)(186003)(66476007)(6666004)(66946007)(7416002)(36756003)(26005)(16526019)(86362001)(38350700002)(38100700002)(66556008)(316002)(6916009)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?8Ek/ubuFMziMbfs53uBadfjC9aq8FAKrwkFnsnAD9CYt9Ju04LNlIAQjSzaF?= =?us-ascii?Q?NeTXSdrbb1+gZcow9uZ2vp2AKZ5RgSS4jSkaWB6a5q/eVGjWmPEdHVlKsoCG?= =?us-ascii?Q?S4UjTMNd8Xk3agPWewxCssJhrYYHGH3gj7iWIDwqrLStSJ19KUFCfInRIJSf?= =?us-ascii?Q?WJ8N35hg1mm7hAF4aIHRPbJkhI6wtCn9uvQJCbAIIbW0UajJVX+4PFMk1Q+K?= =?us-ascii?Q?O5a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wxm16OnJFYayA?= =?us-ascii?Q?fLSNJII8waiMExemvQv+A6eE1W7jAm52tc+FJAL65U+IbzYrtRbqQLPWiZ9D?= =?us-ascii?Q?woaqo6w0CpZg8ExqWtwOgKXEfrOcxLXhSTp9E7VSW7JYWN2bzblqfK5M1Sve?= =?us-ascii?Q?YWEcgIsIC0UROEnRI668bUfwLiF+qSK4bS0a60VuuTz7HtwEZhdItb6XjfFr?= =?us-ascii?Q?Hiz3MopFUYThiADX6AhSKWT6?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f9173be1-a568-4de2-0152-08d915a032b6 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 May 2021 23:46:44.0884 (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: vVOM1i8UXPfJaCIjS9n8GB24vLFNkycV5GHMmeLDCOq1FqXYfyJwrLjnd60ytznLT+NN8I7ge0Fgf5+nJ+GbOw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2445 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3275 The PVALIDATE instruction validates or rescinds validation of a guest page RMP entry. Upon completion, a return code is stored in EAX, rFLAGS bits OF, ZF, AF, PF and SF are set based on this return code. If the instruction completed succesfully, the rFLAGS bit CF indicates if the contents of the RMP entry were changed or not. For more information about the instruction see AMD APM volume 3. Cc: James Bottomley Cc: Min Xu Cc: Jiewen Yao Cc: Tom Lendacky Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Laszlo Ersek Cc: Erdem Aktas Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Reviewed-by: Liming Gao Signed-off-by: Brijesh Singh --- MdePkg/Library/BaseLib/BaseLib.inf | 1 + MdePkg/Include/Library/BaseLib.h | 50 +++++++++++++++++++++++ MdePkg/Include/X64/Nasm.inc | 8 ++++ MdePkg/Library/BaseLib/X64/Pvalidate.nasm | 42 +++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 MdePkg/Library/BaseLib/X64/Pvalidate.nasm diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/Ba= seLib.inf index b76f3af380ea..89a52f72c08a 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -317,6 +317,7 @@ [Sources.X64] X64/GccInlinePriv.c | GCC X64/EnableDisableInterrupts.nasm X64/DisablePaging64.nasm + X64/Pvalidate.nasm X64/RdRand.nasm X64/XGetBv.nasm X64/XSetBv.nasm diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/Base= Lib.h index 7253997a6f8c..a2cd134bea9a 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -4813,6 +4813,56 @@ SpeculationBarrier ( VOID ); =20 +#if defined (MDE_CPU_X64) +// +// The page size for the PVALIDATE instruction +// +typedef enum { + PvalidatePageSize4K =3D 0, + PvalidatePageSize2MB, +} PVALIDATE_PAGE_SIZE; + +// +// PVALIDATE Return Code. +// +#define PVALIDATE_RET_SUCCESS 0 +#define PVALIDATE_RET_FAIL_INPUT 1 +#define PVALIDATE_RET_SIZE_MISMATCH 6 + +// +// The PVALIDATE instruction did not make any changes to the RMP entry. +// +#define PVALIDATE_RET_NO_RMPUPDATE 255 + +/** + Execute a PVALIDATE instruction to validate or to rescinds validation of = a guest + page's RMP entry. + + The instruction is available only when CPUID Fn8000_001F_EAX[SNP]=3D1. + + The function is available on X64. + + @param[in] PageSize The page size to use. + @param[in] Validate If TRUE, validate the guest virtual addres= s + otherwise invalidate the guest virtual add= ress. + @param[in] Address The guest virtual address. + + @retval PVALIDATE_RET_SUCCESS The PVALIDATE instruction succeeded,= and + updated the RMP entry. + @retval PVALIDATE_RET_NO_RMPUPDATE The PVALIDATE instruction succeeded,= but + did not update the RMP entry. + @return Failure code from the PVALIDATE + instruction. +**/ +UINT32 +EFIAPI +AsmPvalidate ( + IN PVALIDATE_PAGE_SIZE PageSize, + IN BOOLEAN Validate, + IN PHYSICAL_ADDRESS Address + ); +#endif + =20 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) /// diff --git a/MdePkg/Include/X64/Nasm.inc b/MdePkg/Include/X64/Nasm.inc index 527f71e9eb4d..528bb3385609 100644 --- a/MdePkg/Include/X64/Nasm.inc +++ b/MdePkg/Include/X64/Nasm.inc @@ -33,6 +33,14 @@ DB 0xF3, 0x48, 0x0F, 0xAE, 0xE8 %endmacro =20 +; +; Macro for the PVALIDATE instruction, defined in AMD APM volume 3. +; NASM feature request URL: https://bugzilla.nasm.us/show_bug.cgi?id=3D339= 2753 +; +%macro PVALIDATE 0 + DB 0xF2, 0x0F, 0x01, 0xFF +%endmacro + ; NASM provides built-in macros STRUC and ENDSTRUC for structure definitio= n. ; For example, to define a structure called mytype containing a longword, ; a word, a byte and a string of bytes, you might code diff --git a/MdePkg/Library/BaseLib/X64/Pvalidate.nasm b/MdePkg/Library/Bas= eLib/X64/Pvalidate.nasm new file mode 100644 index 000000000000..a7d177913405 --- /dev/null +++ b/MdePkg/Library/BaseLib/X64/Pvalidate.nasm @@ -0,0 +1,42 @@ +;-------------------------------------------------------------------------= ---- +; +; Copyright (c) 2021, AMD. All rights reserved.
+; SPDX-License-Identifier: BSD-2-Clause-Patent +; +;-------------------------------------------------------------------------= ---- + +%include "Nasm.inc" + + SECTION .text + +;-------------------------------------------------------------------------= ---- +; UINT32 +; EFIAPI +; AsmPvalidate ( +; IN UINT32 PageSize +; IN UINT32 Validate, +; IN UINT64 Address +; ) +;-------------------------------------------------------------------------= ---- +global ASM_PFX(AsmPvalidate) +ASM_PFX(AsmPvalidate): + mov rax, r8 + + PVALIDATE + + ; Save the carry flag. + setc dl + + ; The PVALIDATE instruction returns the status in rax register. + cmp rax, 0 + jne PvalidateExit + + ; Check the carry flag to determine if RMP entry was updated. + cmp dl, 0 + je PvalidateExit + + ; Return the PVALIDATE_RET_NO_RMPUPDATE. + mov rax, 255 + +PvalidateExit: + ret --=20 2.17.1