From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by mx.groups.io with SMTP id smtpd.web11.1504.1664823241651350967 for ; Mon, 03 Oct 2022 11:54:01 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@google.com header.s=20210112 header.b=bcXSd5e7; spf=pass (domain: flex--dionnaglaze.bounces.google.com, ip: 209.85.214.202, mailfrom: 3ys87ywskbtyvagffsydsrwyggydw.ugevwnwdwvcu.yjgmhk.ag@flex--dionnaglaze.bounces.google.com) Received: by mail-pl1-f202.google.com with SMTP id l2-20020a170902f68200b00177ee7e673eso8683629plg.2 for ; Mon, 03 Oct 2022 11:54:01 -0700 (PDT) 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; bh=CkNu9cmPYBidF5ofRWdBPZWtosx/ag/LC5wJ+9Gd5rU=; b=bcXSd5e7J7xglmE9AHfrE34jmCekfR0EyiEaUh34YNr33uNiiSvG6YIib6+0DM/NhW Pa2QOG9CvVo9Rmkx0WB4uf4sDDZdSt9BxcfhdXz/iGNLIYi9mxgC2v/633IneCKwkVsQ 2gsBuIBFnWnhim7UaDdfT0kCL9fjErf5e7sO0kSJ46h19YRtYM2Y561uJhC3W1URKaDi GHnixaXsgSXV4GXupGuezT8KkTJooUNLt3tYh0Iozh3PTgeq8sPkh89fQpU43PTwdCXS 6BQdsFON6R/xCHBgazmJpP+cf69/O3VkQqruItwREPnSzLwvhPJLiKtQmC5LWutYWtVP /4Dg== 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; bh=CkNu9cmPYBidF5ofRWdBPZWtosx/ag/LC5wJ+9Gd5rU=; b=Zd+wJ3CuhDSkYwLixbYCtNllmWHgUX1K5p1ivjhvuZWTbvPzRqi9PbaqRWecLg1ndL vYfYC5SOs3uDCGgXa4KXsX7CPCwEwbKwa9gin5DH0ZvhC3IC0ho8GlNmhkBj2mBCc+aT nx1fHKB3HAxGKlAE7Cchqd/6FcLaCTm9/JPbfEM8TD20NMOv7ZEoALU96g/ahYzj/rap 7vlDNx7v2cRyYmIiGmiUbPWm/jMLzav9wNEp8yCs4gmPtx6VxA28qkM/ec9ut5ZD1EJs k0jTMGo6S1/xWGzkt8BD6hZ2BgzvgaSvjZF7qnDaNmEuczEU8yca2FBDM74tCAWq3nlk dtsQ== X-Gm-Message-State: ACrzQf2XKln7WSNY8CnVphP+YpWn8Age9/hs3y+bmG/6R4pbFsI0t4pl iyvDKGIw4voFAsp4sZe1h/7CyIp7Mr1k+so7iWg8Gk/MmvO6DsS0ybXZmdP5altQxC0VZ6YsxRY M8OA1y/9peBKeMT8OeFg6ro8WcYTAwASOIjZ11PGuQXE6bSp0/bixeiqax4iDXU15R69fOg/s X-Google-Smtp-Source: AMsMyM4w2jej9lsQV+InotlcsRncatxAaXoXwOMxuiU/MMWUApjYkFIArc83PLOwgZdkl3LLj9F6FV/cO7gYkO0nhg== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a17:90b:3ec2:b0:202:b123:29cc with SMTP id rm2-20020a17090b3ec200b00202b12329ccmr13739344pjb.167.1664823241120; Mon, 03 Oct 2022 11:54:01 -0700 (PDT) Date: Mon, 3 Oct 2022 18:53:46 +0000 In-Reply-To: <20221003185352.3969999-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20221003185352.3969999-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221003185352.3969999-2-dionnaglaze@google.com> Subject: [PATCH v6 1/7] 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 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..13fb58cc02 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 EFI_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 EFI_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, + &gEfiMemoryAcceptProtocolGuid, + 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..5ddddabc32 100644 --- a/OvmfPkg/AmdSevDxe/AmdSevDxe.inf +++ b/OvmfPkg/AmdSevDxe/AmdSevDxe.inf @@ -47,6 +47,9 @@ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsSize +[Protocols] + gEfiMemoryAcceptProtocolGuid + [Guids] gConfidentialComputingSevSnpBlobGuid diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/DxeSnpSystemRamValidate.c index d3a95e4913..ee3710f7b3 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.0.rc1.362.ged0d419d3c-goog