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.web08.9577.1664379226005463691 for ; Wed, 28 Sep 2022 08:33:46 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@google.com header.s=20210112 header.b=UYJA74H5; spf=pass (domain: flex--dionnaglaze.bounces.google.com, ip: 209.85.216.73, mailfrom: 3wwk0ywskbr47cihh4af4t8aiiaf8.6ig78p8f87ew.aliojm.ci@flex--dionnaglaze.bounces.google.com) Received: by mail-pj1-f73.google.com with SMTP id t18-20020a17090ad51200b00200aaa66422so5264298pju.0 for ; Wed, 28 Sep 2022 08:33:45 -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=aOY44P70TcHv+VycGA82ZG6SmDvPP7S2SDwkMUxiBZE=; b=UYJA74H5KICmRZKDtbGi8TrDlylzOokMFHqj1ghqUCD3QC6PzXqg+oSJ+p8P4L7FXC RNeqigEAvhh/yiE0ievYw1Pw4hCwGpMwbd9yUyPsBGiBTypI6plju55zx6zIgeI4kjhm ItCDKo/tyWmKYsP7NTdyDrUVnWKRGivj5mm/W6+n6+tXLKNohbdmaVsWUjF4Ese6a6c4 GueNj0iF6VD20jLk4/Df4dNvXp2w7fQSvBW84tRSmQWJAw9VejA4pQfPGnWlGQa/1DXu f6ADfzWsDSnAqTjxUaqNZhBFo10NKGcXfjr1qc5PCP8t0DgUFouT8Rmh8GM7RCXugnZq T/Wg== 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=aOY44P70TcHv+VycGA82ZG6SmDvPP7S2SDwkMUxiBZE=; b=xcgYmSLIHNNtQFKsjs6ssQokWi9TlRyZDyfDYoMZCy/p3L7Kjqy4QOpoj8glKVmCge lPAHGhUeDA+22sW+TQnGrTCZ2XxpnX9CRvUZ6tFpLg3vLxMdipCvU/xfdXvnAVaVwclt OvFsJLZXV8yCAuUQzZV70SKyMkJsxRfDObz3xr6+oLfTFFPFOKlgqmqT8aDNV1Mrjo7x INuxfKxctYtV8Rz09eCfSioWKtqGB/dqN1R/Cbyc82tDo/JVHmlPvZdH3ndLM8tpa+8Z JF+G9JyDcPr1lYcahz9boGn5k6Hzc07wfxVaer6XtNDQlk5ZjuADYHfbO4ntD1tUlt48 XA8w== X-Gm-Message-State: ACrzQf1V3f3laZ/V+xpjGY/FfXs1LSoy9Vz+M3Bfa446HlQc/ntLiX54 etyKDhSLlzL/fp11wCWIU4P801wbywAzgMGjJ4h6ZqLskDSQpwsPqr3r30OzRswCV9aVzqfM7sH oUB7VZSamxnywgIl9Knv/NkvNhcHXFN/3oqTeCv6dHwUs0YXcgyICtoolt2TbGSFacQIv+Ap6 X-Google-Smtp-Source: AMsMyM7ejFyhGe8adXRgoxEjgUZ7aVLExzZ2E/TtAUsuB993UWfx9N3qn7rVr7uyE/UF118IsM6/Ckkvd8i3REcNuQ== X-Received: from dionnaglaze.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2ee6]) (user=dionnaglaze job=sendgmr) by 2002:a05:6a00:1ad3:b0:548:962b:4c53 with SMTP id f19-20020a056a001ad300b00548962b4c53mr34909847pfv.59.1664379225361; Wed, 28 Sep 2022 08:33:45 -0700 (PDT) Date: Wed, 28 Sep 2022 15:33:21 +0000 In-Reply-To: <20220928153323.2583389-1-dionnaglaze@google.com> Mime-Version: 1.0 References: <20220928153323.2583389-1-dionnaglaze@google.com> X-Mailer: git-send-email 2.37.3.998.g577e59143f-goog Message-ID: <20220928153323.2583389-5-dionnaglaze@google.com> Subject: [PATCH v4 4/6] MdeModulePkg: DxeMain accepts all memory at EBS if needed From: "Dionna Glaze" To: devel@edk2.groups.io Cc: Dionna Glaze , Gerd Hoffmann , James Bottomley , Jiewen Yao , Tom Lendacky , Ard Biesheuvel Content-Type: text/plain; charset="UTF-8" With the addition of the EfiUnacceptedMemory memory type, it is possible the EFI-enlightened guests do not themselves support the new memory type. This commit uses the new PcdEnableUnacceptedMemory to enable unaccepted memory support before ExitBootServices is called by not accepting all unaccepted memory at EBS. The expected usage is to set the new Pcd with a protocol that is usable by bootloaders and directly-booted OSes when they can determine that the OS does indeed support unaccepted memory. Cc: Gerd Hoffmann Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Cc: Ard Biesheuvel Signed-off-by: Dionna Glaze --- MdeModulePkg/Core/Dxe/DxeMain.h | 10 +++ MdeModulePkg/Core/Dxe/DxeMain.inf | 2 + MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c | 14 +++- MdeModulePkg/Core/Dxe/Mem/Page.c | 87 ++++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMain.h index 815a6b4bd8..ac943c87a3 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -2698,6 +2698,16 @@ CoreInitializeMemoryProtection ( VOID ); +/** + Accept and convert unaccepted memory to conventional memory if unaccepted + memory is not enabled and there is an implementation of MemoryAcceptProtocol + installed. + **/ +EFI_STATUS +CoreResolveUnacceptedMemory ( + VOID + ); + /** Install MemoryAttributesTable on memory allocation. diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeMain.inf index e4bca89577..deb8bb2ba8 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -153,6 +153,7 @@ gEfiHiiPackageListProtocolGuid ## SOMETIMES_PRODUCES gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES gEdkiiPeCoffImageEmulatorProtocolGuid ## SOMETIMES_CONSUMES + gEfiMemoryAcceptProtocolGuid ## SOMETIMES_CONSUMES # Arch Protocols gEfiBdsArchProtocolGuid ## CONSUMES @@ -186,6 +187,7 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ## CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdFwVolDxeMaxEncapsulationDepth ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdEnableUnacceptedMemory ## CONSUMES # [Hob] # RESOURCE_DESCRIPTOR ## CONSUMES diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c index 5733f0c8ec..8d1de32fe7 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -768,13 +768,25 @@ CoreExitBootServices ( // gTimer->SetTimerPeriod (gTimer, 0); + // + // Accept all memory if unaccepted memory isn't enabled. + // + Status = CoreResolveUnacceptedMemory(); + if (EFI_ERROR (Status)) { + // + // Notify other drivers that ExitBootServices failed + // + CoreNotifySignalList (&gEventExitBootServicesFailedGuid); + return Status; + } + // // Terminate memory services if the MapKey matches // Status = CoreTerminateMemoryMap (MapKey); if (EFI_ERROR (Status)) { // - // Notify other drivers that ExitBootServices fail + // Notify other drivers that ExitBootServices failed // CoreNotifySignalList (&gEventExitBootServicesFailedGuid); return Status; diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem/Page.c index ffe79dcca9..cbebe62a28 100644 --- a/MdeModulePkg/Core/Dxe/Mem/Page.c +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c @@ -9,6 +9,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "DxeMain.h" #include "Imem.h" #include "HeapGuard.h" +#include +#include // // Entry for tracking the memory regions for each memory type to coalesce similar memory types @@ -2118,6 +2120,91 @@ CoreFreePoolPages ( CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory); } +EFI_EVENT gExitBootServiceEvent = NULL; + +STATIC +EFI_STATUS +AcceptAllUnacceptedMemory ( + IN EFI_MEMORY_ACCEPT_PROTOCOL *AcceptMemory + ) +{ + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap; + UINTN NumEntries; + UINTN Index; + EFI_STATUS Status; + + /* + * Get a copy of the memory space map to iterate over while + * changing the map. + */ + Status = CoreGetMemorySpaceMap (&NumEntries, &AllDescMap); + if (EFI_ERROR (Status)) { + return Status; + } + for (Index = 0; Index < NumEntries; Index++) { + CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc; + + Desc = &AllDescMap[Index]; + if (Desc->GcdMemoryType != EfiGcdMemoryTypeUnaccepted) { + continue; + } + + Status = AcceptMemory->AcceptMemory ( + AcceptMemory, + Desc->BaseAddress, + Desc->Length + ); + if (EFI_ERROR(Status)) { + goto done; + } + + Status = CoreRemoveMemorySpace(Desc->BaseAddress, Desc->Length); + if (EFI_ERROR(Status)) { + goto done; + } + + Status = CoreAddMemorySpace ( + EfiGcdMemoryTypeSystemMemory, + Desc->BaseAddress, + Desc->Length, + EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_XP | EFI_MEMORY_RO | EFI_MEMORY_RP + ); + if (EFI_ERROR(Status)) { + goto done; + } + } + +done: + FreePool (AllDescMap); + return Status; +} + +EFI_STATUS +CoreResolveUnacceptedMemory ( + VOID + ) +{ + EFI_MEMORY_ACCEPT_PROTOCOL *AcceptMemory; + EFI_STATUS Status; + + // No need to accept anything. Unaccepted memory is enabled. + if (PcdGetBool(PcdEnableUnacceptedMemory)) { + return EFI_SUCCESS; + } + + Status = gBS->LocateProtocol (&gEfiMemoryAcceptProtocolGuid, NULL, + (VOID **)&AcceptMemory); + if (Status == EFI_NOT_FOUND) { + return EFI_SUCCESS; + } + if (Status != EFI_SUCCESS) { + DEBUG ((DEBUG_ERROR, "Error locating MemoryAcceptProtocol: %d\n", Status)); + return Status; + } + + return AcceptAllUnacceptedMemory(AcceptMemory); +} + /** Make sure the memory map is following all the construction rules, it is the last time to check memory map error before exit boot services. -- 2.37.3.998.g577e59143f-goog