From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (NAM11-BN8-obe.outbound.protection.outlook.com [40.107.236.49]) by mx.groups.io with SMTP id smtpd.web11.90.1649179916631465599 for ; Tue, 05 Apr 2022 10:31:57 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=z2vJMhfT; 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.236.49, mailfrom: ashish.kalra@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G0Vqzmx34Sxkx5byJfTWgp+Drf1WD6fl1R+0NNyc97LT5Tt5UUUz1QeCQwZUSSa09eFaUED1ZgOvgNj7+G4OI56NS+Lt25R+LK7H9JpbQwVJ2zRZSU7n6p6a29StcpAemyrI2rrpjFQnTY0aaHAIcs7wtWVlPbfxs9AstkE+ng+iIZHgPezeJQC5DMyxmeZc3WSsS+YLINwmsESML71tKr13tYd6XeyI998oLlO3GuobEmsmQcvs+2m6nos3zm8/nfHQmiBAvAGSUM9FFsqNuiynz2OehWlC6hkadSvSOMwRWS3TCuQ/br/COpJgKvWWs+8TxkLPJfrzH7/bVt08nA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5+2FSP+GNskLnDp4nr/+3dIiXYiPBey73eIOjyKGMQU=; b=VzkDmtEqlObtjuNyDJDM/se3/aLmhe/hG9fM3BgWwnbtKh0qR6DrVnnbnJusaa1qGOTHwO+hpKyNYlTcUWf3M7afdx2xytIIafrNSho953VQ4cB7oJmFjYaG3vIKt2XZm82KphAGwu26MoToMXPP7BAS2AEOq/JD6mRnzdjrp8nNv+Yc6w+/xyi3ESlcKyw6oz1FtlT40EmwgiB0odRB+8bQuP1oYjheyB8zgiHFaDJ7PZ1ACUcvOv+bTEWNZDBjxizawvhxg8Dx+Te28nz50BoGTwTbEbwCWT6+UbxejK/IuH9h19GBTKa1sXI4cMRs2z6ntdlsxFoKck2r4MD6Rw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); 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=5+2FSP+GNskLnDp4nr/+3dIiXYiPBey73eIOjyKGMQU=; b=z2vJMhfTFoeH2mNIIPNB3kYjoSxKR6Tr/ICN3Upqfncslpkux9HtRGVfZN5LSaBeY23ghvNoBouD4zSXQmGjMhylCrBbs1eUGmOhsoHw9k+YY+dROufyKe9m4JiZ2TZER1I82otHn0a8dkpGZseQDi0XXS5qnkWvbjuuTLhIFJM= Received: from BN9PR03CA0533.namprd03.prod.outlook.com (2603:10b6:408:131::28) by CY4PR1201MB0184.namprd12.prod.outlook.com (2603:10b6:910:1d::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31; Tue, 5 Apr 2022 17:31:53 +0000 Received: from BN8NAM11FT035.eop-nam11.prod.protection.outlook.com (2603:10b6:408:131:cafe::f4) by BN9PR03CA0533.outlook.office365.com (2603:10b6:408:131::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31 via Frontend Transport; Tue, 5 Apr 2022 17:31:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT035.mail.protection.outlook.com (10.13.177.116) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5123.19 via Frontend Transport; Tue, 5 Apr 2022 17:31:53 +0000 Received: from ashkalraubuntuserver.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 5 Apr 2022 12:31:52 -0500 From: "Ashish Kalra" To: CC: , , , , , , , , Subject: [PATCH v8 2/6] OvmfPkg/BaseMemEncryptLib: Hypercall API for page encryption state change Date: Tue, 5 Apr 2022 17:31:36 +0000 Message-ID: <15a91fa87f2dde8ce557000159b9183979b123bc.1649178155.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Return-Path: Ashish.Kalra@amd.com X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7895d152-bb3f-4dc3-4ee0-08da172a2cbc X-MS-TrafficTypeDiagnostic: CY4PR1201MB0184:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fDDN7LE7FilPwoqcTljG19klv9ybS9XDSlhOkfEs+C5kjPeAOg0XU8AEDVkMi7vR1/NyXY4ycVdUJ9DwDl/E3eJXq+sHBZvtfJ6mnkofzHJbFMM7zmt69/ZVmu1XwUaCoNls1XYylYLBH4WDHPh70OWANzP/mCi+bXiATSNDSQ0t73e8JPj7e2If7pRX5AWHzTE+voJqy4A4gaczxSey5TaPz7yICQlt4V3lcoqbJRg6yoMr+rPuy1W3hRpHefWOUUX3jwhkqkpJZfJXlmf4xQYQWRoHUb3XOv5V2lk/OzG6kSt7nehJc4YF699sTCozpzqtyFedKxDrJhvWmp3KmahdUxIMyYreXtUpAnFpc9tW1ZRfQmkHTKsQinyMX6L9gV/DiUPr+eurLOXSHxc5DKsA2oqK3mPKFrq5J/HOGSUKkjkarsCLvvrIScG3GUM4LsUFMzEJQY2t+eDWPYeWoXG0xbZJ+QcGvZNwIptgPBa6n4rDbF1oXYMbTVssQ9jHRrSZQn+AmG02rtDs5ufWM6Du8HD1ktYHCq9Sr7ITitOoVPYI9p2G77kHsN4NAQKl0bsJGfXBsooDLYVgxwlsvK5KP+5og9h5zupPYLFBHx5cAV42AsFoWAd2RrmnoWhfWcpJex8Ai3PuAx2LZUQvk4jzjelPv4hAhzTp3kvCOvwJ8TM87OwGMqX7mqsA4+qi0gn1ypjjsNYX6FY1TFNV1Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(40470700004)(36840700001)(2616005)(19627235002)(5660300002)(336012)(186003)(26005)(426003)(316002)(40460700003)(47076005)(2906002)(54906003)(6916009)(81166007)(36860700001)(356005)(16526019)(36756003)(7696005)(6666004)(82310400005)(508600001)(70586007)(70206006)(8936002)(8676002)(86362001)(83380400001)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Apr 2022 17:31:53.2169 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7895d152-bb3f-4dc3-4ee0-08da172a2cbc X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT035.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB0184 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain From: Ashish Kalra Add API to issue hypercall on page encryption state change. By default all the SEV guest memory regions are considered encrypted, if a guest changes the encryption attribute of the page (e.g mark a page as decrypted) then notify hypervisor. Hypervisor will need to track the unencrypted pages. The information will be used during guest live migration, guest page migration and guest debugging. This hypercall is used to notify hypervisor when the page's encryption state changes. Cc: Jordan Justen Cc: Ard Biesheuvel Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- OvmfPkg/Include/Library/MemEncryptSevLib.h | 52 +++++++++++++++ .../DxeMemEncryptSevLib.inf | 1 + .../Ia32/MemEncryptSevLib.c | 27 ++++++++ .../PeiMemEncryptSevLib.inf | 1 + .../SecMemEncryptSevLibInternal.c | 20 ++++++ .../X64/AsmHelperStub.nasm | 33 ++++++++++ .../X64/MemEncryptSevLib.c | 66 +++++++++++++++++++ 7 files changed, 200 insertions(+) create mode 100644 OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.= nasm diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/L= ibrary/MemEncryptSevLib.h index babec60df4..b60496c26f 100644 --- a/OvmfPkg/Include/Library/MemEncryptSevLib.h +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -240,4 +240,56 @@ MemEncryptSevSnpPreValidateSystemRam ( IN UINTN NumPages=0D );=0D =0D +/**=0D + This hypercall is used to notify hypervisor when the page's encryption=0D + state changes.=0D +=0D + @param[in] PhysicalAddress The physical address that is the start= address=0D + of a memory region.=0D + @param[in] Pages Number of pages in memory region.=0D + @param[in] IsEncrypted Encrypted or Decrypted.=0D +=0D + @retval RETURN_SUCCESS Hypercall returned success.=0D + @retval RETURN_UNSUPPORTED Hypercall not supported.=0D + @retval RETURN_NO_MAPPING Hypercall returned error.=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SetMemoryEncDecHypercall3 (=0D + IN UINTN PhysicalAddress,=0D + IN UINTN Pages,=0D + IN BOOLEAN IsEncrypted=0D + );=0D +=0D +#define KVM_HC_MAP_GPA_RANGE 12=0D +#define KVM_MAP_GPA_RANGE_PAGE_SZ_4K 0=0D +#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M BIT0=0D +#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G BIT1=0D +#define KVM_MAP_GPA_RANGE_ENC_STATE(n) ((n) << 4)=0D +#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STATE(1)=0D +#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STATE(0)=0D +=0D +/**=0D + Interface exposed by the ASM implementation of the core hypercall=0D +=0D + @param[in] HypercallNum KVM_HC_MAP_GPA_RANGE hypercall.=0D + @param[in] PhysicalAddress The physical address that is the start= address=0D + of a memory region.=0D + @param[in] Pages Number of pages in memory region.=0D + @param[in] Attributes Bits 3:0 - preferred page size encodin= g,=0D + 0 =3D 4kb, 1 =3D 2mb, 2 =3D 1gb, etc..= .=0D + Bit 4 - plaintext =3D 0, encrypted = =3D 1=0D + Bits 63:5 - reserved (must be zero)=0D +=0D + @retval Hypercall returned status.=0D +**/=0D +UINTN=0D +EFIAPI=0D +SetMemoryEncDecHypercall3AsmStub (=0D + IN UINTN HypercallNum,=0D + IN UINTN PhysicalAddress,=0D + IN UINTN Pages,=0D + IN UINTN Attributes=0D + );=0D +=0D #endif // _MEM_ENCRYPT_SEV_LIB_H_=0D diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b= /OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf index 35b7d519d9..af7d2059a2 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf @@ -40,6 +40,7 @@ X64/SnpPageStateChangeInternal.c=0D X64/VirtualMemory.c=0D X64/VirtualMemory.h=0D + X64/AsmHelperStub.nasm=0D =0D [Sources.IA32]=0D Ia32/MemEncryptSevLib.c=0D diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c b= /OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c index f92299fc77..c1c10a61c4 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c @@ -153,3 +153,30 @@ MemEncryptSevSnpPreValidateSystemRam ( {=0D ASSERT (FALSE);=0D }=0D +=0D +/**=0D + This hyercall is used to notify hypervisor when the page's encryption=0D + state changes.=0D +=0D + @param[in] PhysicalAddress The physical address that is the start= address=0D + of a memory region.=0D + @param[in] Pages Number of Pages in the memory region.= =0D + @param[in] IsEncrypted Encrypted or Decrypted.=0D +=0D + @retval RETURN_SUCCESS Hypercall returned success.=0D + @retval RETURN_UNSUPPORTED Hypercall not supported.=0D + @retval RETURN_NO_MAPPING Hypercall returned error.=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SetMemoryEncDecHypercall3 (=0D + IN UINTN PhysicalAddress,=0D + IN UINTN Pages,=0D + IN BOOLEAN IsEncrypted=0D + )=0D +{=0D + //=0D + // Memory encryption bit is not accessible in 32-bit mode=0D + //=0D + return RETURN_UNSUPPORTED;=0D +}=0D diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b= /OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf index 714da33237..e28008d26b 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf @@ -40,6 +40,7 @@ X64/SnpPageStateChangeInternal.c=0D X64/VirtualMemory.c=0D X64/VirtualMemory.h=0D + X64/AsmHelperStub.nasm=0D =0D [Sources.IA32]=0D Ia32/MemEncryptSevLib.c=0D diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibIntern= al.c b/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c index b05dbec02e..d7504ea9e7 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c @@ -139,6 +139,26 @@ MemEncryptSevLiveMigrationIsEnabled ( return FALSE;=0D }=0D =0D +/**=0D + Interface exposed by the ASM implementation of the core hypercall=0D +=0D + @retval Hypercall returned status.=0D +**/=0D +UINTN=0D +EFIAPI=0D +SetMemoryEncDecHypercall3AsmStub (=0D + IN UINTN HypercallNum,=0D + IN UINTN PhysicalAddress,=0D + IN UINTN Pages,=0D + IN UINTN Attributes=0D + )=0D +{=0D + //=0D + // Not used in SEC phase.=0D + //=0D + return RETURN_UNSUPPORTED;=0D +}=0D +=0D /**=0D Returns the SEV encryption mask.=0D =0D diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm b/= OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm new file mode 100644 index 0000000000..0ec35dd9b6 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm @@ -0,0 +1,33 @@ +/** @file=0D +=0D + ASM helper stub to invoke hypercall=0D +=0D + Copyright (c) 2021, AMD Incorporated. All rights reserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +DEFAULT REL=0D +SECTION .text=0D +=0D +; UINTN=0D +; EFIAPI=0D +; SetMemoryEncDecHypercall3AsmStub (=0D +; IN UINTN HypercallNum,=0D +; IN UINTN Arg1,=0D +; IN UINTN Arg2,=0D +; IN UINTN Arg3=0D +; );=0D +global ASM_PFX(SetMemoryEncDecHypercall3AsmStub)=0D +ASM_PFX(SetMemoryEncDecHypercall3AsmStub):=0D + ; UEFI calling conventions require RBX to=0D + ; be nonvolatile/callee-saved.=0D + push rbx=0D + mov rax, rcx ; Copy HypercallNumber to rax=0D + mov rbx, rdx ; Copy Arg1 to the register expected by KVM=0D + mov rcx, r8 ; Copy Arg2 to register expected by KVM=0D + mov rdx, r9 ; Copy Arg3 to register expected by KVM=0D + vmmcall ; Call VMMCALL=0D + pop rbx=0D + ret=0D diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c b/= OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c index e7c703bb9a..a64ff2a5e6 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c @@ -142,3 +142,69 @@ MemEncryptSevClearMmioPageEncMask ( EFI_PAGES_TO_SIZE (NumPages)=0D );=0D }=0D +=0D +/**=0D + This hyercall is used to notify hypervisor when the page's encryption=0D + state changes.=0D +=0D + @param[in] PhysicalAddress The physical address that is the start= address=0D + of a memory region.=0D + @param[in] Pages Number of Pages in the memory region.= =0D + @param[in] IsEncrypted Encrypted or Decrypted.=0D +=0D + @retval RETURN_SUCCESS Hypercall returned success.=0D + @retval RETURN_UNSUPPORTED Hypercall not supported.=0D + @retval RETURN_NO_MAPPING Hypercall returned error.=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SetMemoryEncDecHypercall3 (=0D + IN UINTN PhysicalAddress,=0D + IN UINTN Pages,=0D + IN BOOLEAN IsEncrypted=0D + )=0D +{=0D + RETURN_STATUS Ret;=0D + UINTN Error;=0D + UINTN EncryptState;=0D +=0D + Ret =3D RETURN_UNSUPPORTED;=0D +=0D + if (MemEncryptSevLiveMigrationIsEnabled ()) {=0D + Ret =3D RETURN_SUCCESS;=0D + //=0D + // The encryption bit is set/clear on the smallest page size, hence=0D + // use the 4k page size in MAP_GPA_RANGE hypercall below.=0D + //=0D + // Also, when the GCD map is being walked and the c-bit being cleared= =0D + // from MMIO and NonExistent memory spaces, the physical address=0D + // range being passed may not be page-aligned and adding an assert=0D + // here prevents booting. Hence, rounding it down when calling=0D + // SetMemoryEncDecHypercall3AsmStub below.=0D + //=0D +=0D + EncryptState =3D IsEncrypted ? KVM_MAP_GPA_RANGE_ENCRYPTED :=0D + KVM_MAP_GPA_RANGE_DECRYPTED;=0D +=0D + Error =3D SetMemoryEncDecHypercall3AsmStub (=0D + KVM_HC_MAP_GPA_RANGE,=0D + PhysicalAddress & ~EFI_PAGE_MASK,=0D + Pages,=0D + KVM_MAP_GPA_RANGE_PAGE_SZ_4K | EncryptState=0D + );=0D +=0D + if (Error !=3D 0) {=0D + DEBUG ((=0D + DEBUG_ERROR,=0D + "SetMemoryEncDecHypercall3 failed, Phys =3D %x, Pages =3D %d, Err = =3D %Ld\n",=0D + PhysicalAddress,=0D + Pages,=0D + (INT64)Error=0D + ));=0D +=0D + Ret =3D RETURN_NO_MAPPING;=0D + }=0D + }=0D +=0D + return Ret;=0D +}=0D --=20 2.25.1