Skip to content

Commit c0461bd

Browse files
deeglazebp3tk0v
authored andcommitted
x86/efi: Safely enable unaccepted memory in UEFI
The UEFI v2.9 specification includes a new memory type to be used in environments where the OS must accept memory that is provided from its host. Before the introduction of this memory type, all memory was accepted eagerly in the firmware. In order for the firmware to safely stop accepting memory on the OS's behalf, the OS must affirmatively indicate support to the firmware. This is only a problem for AMD SEV-SNP, since Linux has had support for it since 5.19. The other technology that can make use of unaccepted memory, Intel TDX, does not yet have Linux support, so it can strictly require unaccepted memory support as a dependency of CONFIG_TDX and not require communication with the firmware. Enabling unaccepted memory requires calling a 0-argument enablement protocol before ExitBootServices. This call is only made if the kernel is compiled with UNACCEPTED_MEMORY=y This protocol will be removed after the end of life of the first LTS that includes it, in order to give firmware implementations an expiration date for it. When the protocol is removed, firmware will strictly infer that a SEV-SNP VM is running an OS that supports the unaccepted memory type. At the earliest convenience, when unaccepted memory support is added to Linux, SEV-SNP may take strict dependence in it. After the firmware removes support for the protocol, this should be reverted. [tl: address some checkscript warnings] Signed-off-by: Dionna Glaze <[email protected]> Signed-off-by: Tom Lendacky <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/0d5f3d9a20b5cf361945b7ab1263c36586a78a42.1686063086.git.thomas.lendacky@amd.com
1 parent 6c32117 commit c0461bd

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

drivers/firmware/efi/libstub/x86-stub.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ const efi_dxe_services_table_t *efi_dxe_table;
2626
u32 image_offset __section(".data");
2727
static efi_loaded_image_t *image = NULL;
2828

29+
typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
30+
union sev_memory_acceptance_protocol {
31+
struct {
32+
efi_status_t (__efiapi * allow_unaccepted_memory)(
33+
sev_memory_acceptance_protocol_t *);
34+
};
35+
struct {
36+
u32 allow_unaccepted_memory;
37+
} mixed_mode;
38+
};
39+
2940
static efi_status_t
3041
preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
3142
{
@@ -310,6 +321,29 @@ setup_memory_protection(unsigned long image_base, unsigned long image_size)
310321
#endif
311322
}
312323

324+
static void setup_unaccepted_memory(void)
325+
{
326+
efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
327+
sev_memory_acceptance_protocol_t *proto;
328+
efi_status_t status;
329+
330+
if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
331+
return;
332+
333+
/*
334+
* Enable unaccepted memory before calling exit boot services in order
335+
* for the UEFI to not accept all memory on EBS.
336+
*/
337+
status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
338+
(void **)&proto);
339+
if (status != EFI_SUCCESS)
340+
return;
341+
342+
status = efi_call_proto(proto, allow_unaccepted_memory);
343+
if (status != EFI_SUCCESS)
344+
efi_err("Memory acceptance protocol failed\n");
345+
}
346+
313347
static const efi_char16_t apple[] = L"Apple";
314348

315349
static void setup_quirks(struct boot_params *boot_params,
@@ -908,6 +942,8 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
908942

909943
setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start);
910944

945+
setup_unaccepted_memory();
946+
911947
status = exit_boot(boot_params, handle);
912948
if (status != EFI_SUCCESS) {
913949
efi_err("exit_boot() failed!\n");

include/linux/efi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ void efi_native_runtime_setup(void);
437437
#define DELLEMC_EFI_RCI2_TABLE_GUID EFI_GUID(0x2d9f28a2, 0xa886, 0x456a, 0x97, 0xa8, 0xf1, 0x1e, 0xf2, 0x4f, 0xf4, 0x55)
438438
#define AMD_SEV_MEM_ENCRYPT_GUID EFI_GUID(0x0cf29b71, 0x9e51, 0x433a, 0xa3, 0xb7, 0x81, 0xf3, 0xab, 0x16, 0xb8, 0x75)
439439

440+
/* OVMF protocol GUIDs */
441+
#define OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID EFI_GUID(0xc5a010fe, 0x38a7, 0x4531, 0x8a, 0x4a, 0x05, 0x00, 0xd2, 0xfd, 0x16, 0x49)
442+
440443
typedef struct {
441444
efi_guid_t guid;
442445
u64 table;

0 commit comments

Comments
 (0)