From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by mx.groups.io with SMTP id smtpd.web11.2826.1667942169449201697 for ; Tue, 08 Nov 2022 13:16:09 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@google.com header.s=20210112 header.b=tgu8sZCu; spf=pass (domain: flex--dionnaglaze.bounces.google.com, ip: 209.85.216.73, mailfrom: 3gmdqywskbxmuzfeerxcrqvxffxcv.tfduvmvcvubt.xiflgj.zf@flex--dionnaglaze.bounces.google.com) Received: by mail-pj1-f73.google.com with SMTP id nl16-20020a17090b385000b002138288fd51so1623pjb.6 for ; Tue, 08 Nov 2022 13:16:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Yb0Id9tFbO9mT6dJFR88F1Z7x48wPJVaUrwt3S8WsD4=; b=tgu8sZCuNpIWEGrHgOegEVyRgjm2B+9laAqCVqOpTrVxbCRtAwp15F+JWMlUxjYX+X wPOlxIC6g/Dmpjg7XCwxkHZJv4YuU8HXq4SKqCZCPkOSXB5ilcX1dk2+HVri0lL4cKN4 3S6bK25wQffqZfkgM6tj6hCt497TTnHwZbpKwWka1gA4hpycXkKX7ul9ds+3cjViXiSn k9O1OHP5Kc/Gznn/Mm89IegiG/o3Dz3Juf9bjnW5pyy0HyQ+R1LtYoekc7QikgYwa+T0 V0lEBsotlqWsN6v4Sh1PCIDtagR5UNhx87zcUjNA51r+P2OS2jEy3X5f66CkAiMLFE52 bICA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Yb0Id9tFbO9mT6dJFR88F1Z7x48wPJVaUrwt3S8WsD4=; b=cs05V+7IOKQKx0CjqRSoRcXkxWlnoBlpAlPypO6Fjsg+V4JGidys5VxL6TZ0xzAZiC UdC8p+yAQY6p4jrbeWVINsfsqP6HnyOaCUSr7o5Z6sBcXYcBVHJ0IJGcQqvjMmYSBDD+ OJyOpLHAmYbrf4OWpYyFjNFUlKyAgKyrdEOAB8h6hSPLcFqWcb92+D45bgx0LD9UnPuc ADeNrX5WQ4xWFHWE8/6aB0miQo1oNF1n/GtrqyelyK4KnrbA3dHuV3AwLCt0TJUlOjvH OEJF784boR6JwrbLYLLgYWG55VRFA7Wi1qu/D6d9Rq1qQOoyaMPDhisTYCQJWG0q7F2f /n7Q== X-Gm-Message-State: ACrzQf1d4+5Wzmg68updkNZzeklXNBRAMaR/I5NMKY9asiFTSHwiMDvh HQD0OXZ6CYZIL9RO3zVwOPng+Zm1CDn6bW9cTFTUvjAK8Bb8UNlMCmai1ib5eKhXAYbEcT+xyro 2EAYqC/SyDBM7Q3Se2MNIaUUpsr9ac18lS7PH8bFhzX7DEVo4fT+vOL9mWw9SlRONUqdO0zrM X-Google-Smtp-Source: AMsMyM62dBB++5fnH/QKQCrzpOwHdF4TdfXW4G09c/V4GoV6ZL+iUo9bVOV2zRzRarli7uixYS0ECmNtLvdwREm04w== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:aa7:82d8:0:b0:561:5d08:761 with SMTP id f24-20020aa782d8000000b005615d080761mr1068266pfn.14.1667942168358; Tue, 08 Nov 2022 13:16:08 -0800 (PST) Date: Tue, 8 Nov 2022 21:15:54 +0000 In-Reply-To: <20221108211557.3306520-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221108211557.3306520-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Message-ID: <20221108211557.3306520-2-dionnaglaze@google.com> Subject: [PATCH v2 1/4] OvmfPkg: Realize EfiMemoryAcceptProtocol in AmdSevDxe From: "Dionna Glaze" To: devel@edk2.groups.io Cc: Dionna Glaze , Gerd Hoffmann , James Bottomley , Jiewen Yao , Tom Lendacky Content-Type: text/plain; charset="UTF-8" From: Sophia Wolf When a guest OS does not support unaccepted memory, the unaccepted memory must be accepted before returning a memory map to the caller. EfiMemoryAcceptProtocol is defined in MdePkg and is implemented / Installed in AmdSevDxe for AMD SEV-SNP memory acceptance. Cc: Gerd Hoffmann Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Reviewed-by: Tom Lendacky Signed-off-by: Dionna Glaze --- OvmfPkg/AmdSevDxe/AmdSevDxe.c | 55 ++++++++++++++++++-- OvmfPkg/AmdSevDxe/AmdSevDxe.inf | 3 ++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c | 24 +++++++-- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/AmdSevDxe/AmdSevDxe.c b/OvmfPkg/AmdSevDxe/AmdSevDxe.c index 662d3c4ccb..f7600c3c81 100644 --- a/OvmfPkg/AmdSevDxe/AmdSevDxe.c +++ b/OvmfPkg/AmdSevDxe/AmdSevDxe.c @@ -20,6 +20,7 @@ #include #include #include +#include STATIC CONFIDENTIAL_COMPUTING_SNP_BLOB_LOCATION mSnpBootDxeTable = { SIGNATURE_32 ('A', 'M', 'D', 'E'), @@ -31,6 +32,40 @@ STATIC CONFIDENTIAL_COMPUTING_SNP_BLOB_LOCATION mSnpBootDxeTable = { FixedPcdGet32 (PcdOvmfCpuidSize), }; +STATIC EFI_HANDLE mAmdSevDxeHandle = NULL; + +#define IS_ALIGNED(x, y) ((((x) & ((y) - 1)) == 0)) + +STATIC +EFI_STATUS +EFIAPI +AmdSevMemoryAccept ( + IN EDKII_MEMORY_ACCEPT_PROTOCOL *This, + IN EFI_PHYSICAL_ADDRESS StartAddress, + IN UINTN Size + ) +{ + // + // The StartAddress must be page-aligned, and the Size must be a positive + // multiple of SIZE_4KB. Use an assert instead of returning an erros since + // this is an EDK2-internal protocol. + // + ASSERT (IS_ALIGNED (StartAddress, SIZE_4KB)); + ASSERT (IS_ALIGNED (Size, SIZE_4KB)); + ASSERT (Size != 0); + + MemEncryptSevSnpPreValidateSystemRam ( + StartAddress, + EFI_SIZE_TO_PAGES (Size) + ); + + return EFI_SUCCESS; +} + +STATIC EDKII_MEMORY_ACCEPT_PROTOCOL mMemoryAcceptProtocol = { + AmdSevMemoryAccept +}; + EFI_STATUS EFIAPI AmdSevDxeEntryPoint ( @@ -147,11 +182,23 @@ AmdSevDxeEntryPoint ( } } - // - // If its SEV-SNP active guest then install the CONFIDENTIAL_COMPUTING_SEV_SNP_BLOB. - // It contains the location for both the Secrets and CPUID page. - // if (MemEncryptSevSnpIsEnabled ()) { + // + // Memory acceptance began being required in SEV-SNP, so install the + // memory accept protocol implementation for a SEV-SNP active guest. + // + Status = gBS->InstallProtocolInterface ( + &mAmdSevDxeHandle, + &gEdkiiMemoryAcceptProtocolGuid, + EFI_NATIVE_INTERFACE, + &mMemoryAcceptProtocol + ); + ASSERT_EFI_ERROR (Status); + + // + // If its SEV-SNP active guest then install the CONFIDENTIAL_COMPUTING_SEV_SNP_BLOB. + // It contains the location for both the Secrets and CPUID page. + // return gBS->InstallConfigurationTable ( &gConfidentialComputingSevSnpBlobGuid, &mSnpBootDxeTable diff --git a/OvmfPkg/AmdSevDxe/AmdSevDxe.inf b/OvmfPkg/AmdSevDxe/AmdSevDxe.inf index 9acf860cf2..cd1b686c53 100644 --- a/OvmfPkg/AmdSevDxe/AmdSevDxe.inf +++ b/OvmfPkg/AmdSevDxe/AmdSevDxe.inf @@ -47,6 +47,9 @@ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsSize +[Protocols] + gEdkiiMemoryAcceptProtocolGuid + [Guids] gConfidentialComputingSevSnpBlobGuid diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c index d3a95e4913..cbcdd46f52 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c @@ -14,6 +14,7 @@ #include #include "SnpPageStateChange.h" +#include "VirtualMemory.h" /** Pre-validate the system RAM when SEV-SNP is enabled in the guest VM. @@ -29,12 +30,27 @@ MemEncryptSevSnpPreValidateSystemRam ( IN UINTN NumPages ) { + EFI_STATUS Status; + if (!MemEncryptSevSnpIsEnabled ()) { return; } - // - // All the pre-validation must be completed in the PEI phase. - // - ASSERT (FALSE); + // DXE pre-validation may happen with the memory accept protocol. + // The protocol should only be called outside the prevalidated ranges + // that the PEI stage code explicitly skips. Specifically, only memory + // ranges that are classified as unaccepted. + if (BaseAddress >= SIZE_4GB) { + Status = InternalMemEncryptSevCreateIdentityMap1G ( + 0, + BaseAddress, + EFI_PAGES_TO_SIZE (NumPages) + ); + if (EFI_ERROR (Status)) { + ASSERT (FALSE); + CpuDeadLoop (); + } + } + + InternalSetPageState (BaseAddress, NumPages, SevSnpPagePrivate, TRUE); } -- 2.38.1.431.g37b22c650d-goog