Skip to contentSkip to conversation

arm64: DRTM, boot portion

PATCH In review Jason Gunthorpe 3 participants 20 messages Nov 20 – Dec 2

Subsystems (5)
Documentation RISC-V arm64 EFI +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover arm64: DRTM, boot portion

This is the first series for an implementation of ARM's DEN 0113 v1.4b Dynamic Root of Trust for Measurement (DRTM) launch protocol. It is built on some generic changes to the EFI stubs that provide for a simple, direct DRTM launch directly into vmlinux.

DRTM is a boot security technique that jumps into some platform trusted environment prior to launching the OS. The trusted environment will sanitize the CPU, block DMA, measure (SHA hash) the OS to a TPM, and then jump into the OS. The security goal is to prevent tampering across the
launch: if the measurement says X, it should be impossible to tamper with the boot such that the CPU is actually running Y. DRTM is a reaction to the historically bad boot security in the UEFI world. It aims to make bugs in UEFI/etc unimportant for OS security.

When I spoke to Catalin about this, he was interested in making the flow cross arch. After looking at what AMD and Intel do, and how different the kernel EFI flow is on x86, I have opted to do that from the perspective of building some generic DRTM infrastructure for the arches using EFI_ZBOOT and FDT. I've sketched out applying the same general ideas to x86 and it works there too, but x86 is so different that not much is directly re-used. I would argue the security/hardening work listed below is a more important place to get commonality.

Several people have asked me about Trenchboot, which is the x86 out-of-tree DRTM implementation. In my view, ARM and Trenchboot have converged on the same basic design. Both offer high-level functions to the OS that don't leak out any platform-specific details. Trenchboot offers these through an EFI mechanism from Grub, while ARM offers it through a standardized FW based SMC API. Effectively Trenchboot is a reaction to x86 having primitives that are far too low level and far too complicated to implement inside the EFI stub.

ARM does not suffer the same issues as x86. The EFI stub implementation for ARM DRTM is only 400 lines in patch 10. This is because the standard already offers a sufficient abstraction to the OS. There is no technical reason to bounce back to Grub to hide complexity that doesn't even exist.

Further, due to how ARM has abstracted DRTM, we cannot hide the ARM details from the post-launch Linux. The kernel must understand it is inside an ARM-specific DLME, it must do the ARM specific ations, and it must process the ARM-specific post-launch data structures that the FW writes. All of the work in this series, and its followups would still be needed if Grub was involved. There is also a sneaky ABI here, the choices the stub makes during launch have to be matched by the vmlinux accomodating them. Keeping this all in one source tree and one link with no ABI is the right answer.

My hope is that this series will encourage other platforms, like RISCV, to follow ARM's approach and not x86's. I outlined a suggestion on how to progress x86 DRTM upstream along these lines here:
https://lore.kernel.org/all/[email protected]/

Broadly, ARM's abstraction does several key things for Linux:
1) No TPM is required until after some ACPI is parsed. ARM has many different kinds of TPMs, so TPM without ACPI is hard.

2) The launch uses a contiguous chunk of memory that can be perfectly overlaid on vmlinux, with space for the headers, measured section, unmeasured data, bss and trusted post-launch information. It is trivial to insert Linux into this scheme.

3) The launch protocol is trivial. The launch directly measures a portion of vmlinux. About 50% of the stub code is just inspecting features and doing FW call compatibility work, not tricky DRTM things. Zero SOC-specific code.

4) The post-launch world isn't special. We don't need unique kernel patches to undo things that DRTM did. The normal drivers work in the normal way just fine.

This series is enough to perform the launch and get a working kernel inside a Dynamic Launch Measured Environment (DLME). There are several following security-focused topics/series that aim to further prevent attackers in the unmeasured world from compromising the measured kernel. Much of this list comes from internal security research at NVIDIA on securing Linux post-DRTM.

- The kernel command line needs to be measured, without any "early tpm" for Linux.

- ARM's DRTM protocol for verifying the address map needs to be implemented

- ARM's protocol for checking the "TCB" ACPI needs to be implemented

- Non-TCB ACPI needs to be deferred until the TPM is discovered and then the tables are measured into the TPM

- Initrd/Initramfs measurement by Linux before using it

- Exposing the DRTM launches TCG audit log to userspace

- TPM locality handling

- Kexec, via Ard's EFI entry approach:

    https://lore.kernel.org/all/[email protected]/

- FDT security

- KASLR RNG security

These topics all largely require different maintainers and different cross-arch solutions. Post-launch kernel defense against an adversarial boot is a complicated topic with several different threat models people are interested in. It has alot of overlap with the Confidential Computing need to deal with an adversarial host.

This series is arranged to try to build re-usable things that any arch can use to implement DRTM:
- New general linker sections/etc to move mutable data out of the measured range
- Cross-arch symbols __drtm_measured_start/end that allow a user tool to compute the measurement hash from the vmlinux itself. Because of the mutable values this isn't just all of vmlinux.
- CONFIG_EFI_DRTM and general EFI stub arch hooks to plug into for EFI ZBOOT and FDT arches
- Shared drtm= kernel command-line processing

For x86, it does set some user-visible expectations like how to compute a hash for vmlinux and the drtm= parameter. IMHO, these are key areas to get cross-arch alignment on so the user tooling, like a systemd UKI, can use DRTM easily.

I have a slop'd up qemu that implements the SMC protocol which is very useful for testing:
https://github.com/jgunthorpe/qemu/commits/arm64_drtm

NVIDIA has several real HW platforms this works with, I've been able to see that one of them works now, and someone else tested it on a different simulator. I'll get more HW testing as the review progresses.

This is on github: https://github.com/jgunthorpe/linux/commits/arm64_drtm

I've fed it to a local Sashiko and am satisfied the remaining remarks are not applicable, and fixed several of the previously existing bugs it found.

v3:
- Rebase on v7.4-rc3 + Arnd's series:

     https://lore.kernel.org/all/[email protected]

- Use the SMCC 1.0 calling convention when discovering if v1.1 is supported
- Tidy comments
- Move kernel drtm code to drivers/firmware/drtm/arm64.c
v2: https://patch.msgid.link/r/[email protected]
- Swap ftd_size_allocated to be inited by efi_load_dtb
- Move the __free
- Use a 64 bit for efi_image_info_offset because 0-day found a randconfig that triggered the linker assertions for 32 bit. Clarify some of the var names around this flow so they are less similar
- Fixup some issue with linker script compatability on binutils, I was using lld
- Drop the le64, everything is u64
- Adjust whitespace
- Fix efibss placement in zboot.lds, binutils pointed out it is made allocated and should not be in bss
- Significantly revise the header file with the DEN0113 definitions
- Support v1.0 implementations and issue a revision 1 TPM parameters unless revision 2 is really required
- Review the upstream sashiko notes and reject all of them talking about not having enough hardening, the patch plan for security progression is explained in the cover leter
v1: https://patch.msgid.link/r/[email protected]

Jason Gunthorpe (11):
efi/libstub: Add generic arch callbacks for DRTM efi/libstub: Use free_addr/size as the arg to handle_kernel_image efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size
efi: Add a __efi_data_handoff section annotation efi/libstub: Put the stub's writable data in unique sections for DRTM
arm64: drtm: Add macro definitions for DEN0113
arm64: drtm: Update the linker script for EFI_STUB_DRTM
arm64: drtm: Add drtm_entry point to head.S
arm64: drtm: Call UNPROTECT_MEMORY
efi/arm64: Add a way to detect if SMCC 1.1 can be used efi/arm64: Implement ARM64 DRTM in the stub

 .../admin-guide/kernel-parameters.txt         |  12 +
 arch/arm64/Kconfig                            |  16 +
 arch/arm64/include/asm/drtm.h                 | 325 +++++++++++++++
 arch/arm64/kernel/head.S                      |  83 ++++
 arch/arm64/kernel/vmlinux.lds.S               |  65 ++-
 drivers/firmware/Makefile                     |   1 +
 drivers/firmware/drtm/Makefile                |   2 +
 drivers/firmware/drtm/arm64.c                 |  49 +++
 drivers/firmware/efi/Kconfig                  |  30 ++
 drivers/firmware/efi/efi-init.c               |   2 +-
 drivers/firmware/efi/libstub/Makefile         |  11 +
 drivers/firmware/efi/libstub/arm32-stub.c     |   8 +-
 drivers/firmware/efi/libstub/arm64-drtm.c     | 383 ++++++++++++++++++
 drivers/firmware/efi/libstub/arm64-smcc.c     | 212 ++++++++++
 drivers/firmware/efi/libstub/arm64-stub.c     |   6 +-
 drivers/firmware/efi/libstub/arm64-stub.h     |   3 +
 drivers/firmware/efi/libstub/efi-stub-entry.c |  12 +-
 .../firmware/efi/libstub/efi-stub-helper.c    |  34 ++
 drivers/firmware/efi/libstub/efistub.h        |  81 +++-
 drivers/firmware/efi/libstub/fdt.c            |   8 +-
 drivers/firmware/efi/libstub/image_info.S     |   7 +
 drivers/firmware/efi/libstub/kaslr.c          | 131 ++++--
 drivers/firmware/efi/libstub/loongarch-stub.c |  10 +-
 drivers/firmware/efi/libstub/riscv-stub.c     |   6 +-
 drivers/firmware/efi/libstub/zboot.c          |  15 +-
 drivers/firmware/efi/libstub/zboot.lds        |   2 +-
 include/linux/efi.h                           |  12 +
 27 files changed, 1457 insertions(+), 69 deletions(-)
 create mode 100644 arch/arm64/include/asm/drtm.h
 create mode 100644 drivers/firmware/drtm/Makefile
 create mode 100644 drivers/firmware/drtm/arm64.c
 create mode 100644 drivers/firmware/efi/libstub/arm64-drtm.c
 create mode 100644 drivers/firmware/efi/libstub/arm64-smcc.c
 create mode 100644 drivers/firmware/efi/libstub/arm64-stub.h

base-commit: 1e7814d34a25521a2ee48b3be8c1bc54f04821f8

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/11 efi/libstub: Add generic arch callbacks for DRTM

If the architecture supports an EFI stub launched version of DRTM, it can select the kconfig and provide the hooks under its architecture build.

Have the common code parse a drtm=enforce|auto|off kernel command line parameter to set the boot behavior. Support commas in the args list since there will be more options here down the road.

efi_drtm_prepare() is called before doing any decompression or image relocation. It should figure out if DRTM is supported, policy-permitted, and how much memory efi_drtm_get_extra_size() should return.

The image handling is revised to allocate extra_size at the end of the normal image. The architecture can use this to store any information needed for the DRTM flow. It is always contiguous with Image, which is a requirement of ARM's specification.

efi_drtm_prepare_launch() is called while still in EFI boot services after Image is fully prepared in its final location.

efi_drtm_launch() is called after exiting boot services and must jump into the kernel through the DRTM flow. If it returns, then the normal launch is tried (auto mode).

The extra_size is allocated directly after the Image. Handle the trivial zboot flow now, and vmlinux in the following patch.

---
 .../admin-guide/kernel-parameters.txt         | 12 ++++++
 drivers/firmware/efi/Kconfig                  | 30 ++++++++++++++
 drivers/firmware/efi/libstub/efi-stub-entry.c |  4 ++
 .../firmware/efi/libstub/efi-stub-helper.c    | 34 ++++++++++++++++
 drivers/firmware/efi/libstub/efistub.h        | 39 +++++++++++++++++++
 drivers/firmware/efi/libstub/fdt.c            |  8 +++-
 drivers/firmware/efi/libstub/zboot.c          | 15 ++++---
 7 files changed, 136 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +12 -0
index 0b3c140babd944..4c95e36dd8c94b 100644
@@ -1377,6 +1377,18 @@ Kernel parameters
 			data set with no connector name will be used for
 			any connectors not explicitly specified.
 
+	drtm=		[EFI,EARLY]
+			Format: { "off" | "auto" | "enforce" }
+			Control the EFI stub Dynamic Root of Trust for
+			Measurement (DRTM) launch policy.
+			off: do not attempt a DRTM launch.
+			auto: attempt a DRTM launch when supported, but fall
+			back to a normal boot if preparation or launch fails.
+			enforce: require a DRTM launch and refuse to fall back
+			to a normal boot if preparation or launch fails.
+			The default is selected by the EFI_STUB_DRTM_DEFAULT_*
+			configuration options.
+
 	dscc4.setup=	[NET]
 
 	dt_cpu_ftrs=	[PPC,EARLY]
drivers/firmware/efi/Kconfig +30 -0
index 29e0729299f5bd..6806bcd6a52420 100644
@@ -72,6 +72,36 @@ config EFI_RUNTIME_WRAPPERS
 config EFI_GENERIC_STUB
 	bool
 
+config EFI_STUB_DRTM
+	bool
+
+choice
+	prompt "Default DRTM launch policy"
+	depends on EFI_STUB_DRTM
+	default EFI_STUB_DRTM_DEFAULT_OFF
+	help
+	  Select the DRTM policy used when no drtm= option is present on the
+	  kernel command line. The command-line option overrides this default.
+
+config EFI_STUB_DRTM_DEFAULT_OFF
+	bool "Off"
+	help
+	  Do not attempt a DRTM launch unless it is requested explicitly.
+
+config EFI_STUB_DRTM_DEFAULT_AUTO
+	bool "Automatic with fallback"
+	help
+	  Attempt a DRTM launch when the firmware supports it, but fall back to
+	  normal boot if preparation or a returnable launch attempt fails.
+
+config EFI_STUB_DRTM_DEFAULT_ENFORCE
+	bool "Enforce"
+	help
+	  Require a DRTM launch and refuse normal-boot fallback if preparation
+	  or a returnable launch attempt fails.
+
+endchoice
+
 config EFI_ZBOOT
 	bool "Enable the generic EFI decompressor"
 	depends on EFI_GENERIC_STUB && !ARM
drivers/firmware/efi/libstub/efi-stub-entry.c +4 -0
index dcf4442a16b078..fd272d07737371 100644
@@ -47,6 +47,10 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	if (status != EFI_SUCCESS)
 		return status;
 
+	status = efi_drtm_prepare();
+	if (status != EFI_SUCCESS)
+		return status;
+
 	efi_info("Booting Linux Kernel...\n");
 
 	status = handle_kernel_image(&image_addr, &image_size,
drivers/firmware/efi/libstub/efi-stub-helper.c +34 -0
index e50deec9968658..2840b589653fe8 100644
@@ -28,6 +28,37 @@ static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
 
 int efi_mem_encrypt;
 
+#ifdef CONFIG_EFI_STUB_DRTM
+enum efi_drtm_policy efi_drtm_policy =
+	IS_ENABLED(CONFIG_EFI_STUB_DRTM_DEFAULT_ENFORCE) ? EFI_DRTM_ENFORCE :
+	IS_ENABLED(CONFIG_EFI_STUB_DRTM_DEFAULT_AUTO) ? EFI_DRTM_AUTO :
+	EFI_DRTM_OFF;
+
+static void efi_drtm_parse_options(char *options)
+{
+	char *keyword;
+
+	while (options) {
+		keyword = options;
+		while (*options && *options != ',')
+			options++;
+		if (*options)
+			*options++ = '\0';
+		else
+			options = NULL;
+
+		if (!strcmp(keyword, "off"))
+			efi_drtm_policy = EFI_DRTM_OFF;
+		else if (!strcmp(keyword, "auto"))
+			efi_drtm_policy = EFI_DRTM_AUTO;
+		else if (!strcmp(keyword, "enforce"))
+			efi_drtm_policy = EFI_DRTM_ENFORCE;
+	}
+}
+#else
+static void efi_drtm_parse_options(char *options) {}
+#endif
+
 bool __pure __efi_soft_reserve_enabled(void)
 {
 	return !efi_nosoftreserve;
@@ -90,6 +121,9 @@ efi_status_t efi_parse_options(char const *cmdline)
 				efi_mem_encrypt = 1;
 			else if (parse_option_str(val, "off"))
 				efi_mem_encrypt = -1;
+		} else if (IS_ENABLED(CONFIG_EFI_STUB_DRTM) &&
+			   !strcmp(param, "drtm") && val) {
+			efi_drtm_parse_options(val);
 		} else if (!strcmp(param, "efi") && val) {
 			efi_nochunk = parse_option_str(val, "nochunk");
 			efi_novamap |= parse_option_str(val, "novamap");
drivers/firmware/efi/libstub/efistub.h +39 -0
index df3f88f4786b82..80b9558f9f689a 100644
@@ -1077,6 +1077,45 @@ efi_status_t check_platform_features(void);
 
 void *get_efi_config_table(efi_guid_t guid);
 
+struct efi_image_info;
+
+enum efi_drtm_policy {
+	EFI_DRTM_OFF,
+	EFI_DRTM_AUTO,
+	EFI_DRTM_ENFORCE,
+};
+
+#ifdef CONFIG_EFI_STUB_DRTM
+extern enum efi_drtm_policy efi_drtm_policy;
+efi_status_t efi_drtm_prepare(void);
+unsigned long efi_drtm_get_extra_size(void);
+efi_status_t efi_drtm_prepare_launch(const struct efi_image_info *image_info,
+				     unsigned long fdt_addr);
+void efi_drtm_launch(void);
+#else
+enum {efi_drtm_policy = EFI_DRTM_OFF};
+static inline efi_status_t efi_drtm_prepare(void)
+{
+	return EFI_SUCCESS;
+}
+
+static inline unsigned long efi_drtm_get_extra_size(void)
+{
+	return 0;
+}
+
+static inline efi_status_t
+efi_drtm_prepare_launch(const struct efi_image_info *image_info,
+			unsigned long fdt_addr)
+{
+	return EFI_SUCCESS;
+}
+
+static inline void efi_drtm_launch(void)
+{
+}
+#endif
+
 void efi_char16_puts(efi_char16_t *);
 
 int efi_vsnprintf(efi_char16_t *buf, size_t size, const char *fmt, va_list ap,
drivers/firmware/efi/libstub/fdt.c +7 -1
index 09e9e5bc831e8b..d39455d38a911a 100644
@@ -223,6 +223,7 @@ static
 efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 					    efi_loaded_image_t *image,
 					    unsigned long *new_fdt_addr,
+					    const struct efi_image_info *image_info,
 					    char *cmdline_ptr)
 {
 	unsigned long desc_size;
@@ -289,6 +290,10 @@ efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 		goto fail_free_new_fdt;
 	}
 
+	status = efi_drtm_prepare_launch(image_info, *new_fdt_addr);
+	if (status != EFI_SUCCESS)
+		goto fail_free_new_fdt;
+
 	priv.new_fdt_addr = (void *)*new_fdt_addr;
 
 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
@@ -351,7 +356,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	efi_status_t status;
 
 	status = allocate_new_fdt_and_exit_boot(handle, image, &fdt_addr,
-						cmdline_ptr);
+						image_info, cmdline_ptr);
 	if (status != EFI_SUCCESS) {
 		efi_err("Failed to update FDT and exit boot services\n");
 		return status;
@@ -360,6 +365,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	if (IS_ENABLED(CONFIG_ARM))
 		efi_handle_post_ebs_state();
 
+	efi_drtm_launch();
 	efi_enter_kernel(image_info, fdt_addr, fdt_totalsize((void *)fdt_addr));
 	/* not reached */
 }
drivers/firmware/efi/libstub/zboot.c +10 -5
index 4089a29068caae..69962a8f094caf 100644
@@ -50,7 +50,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 {
 	static efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID;
 	char *cmdline_ptr __free(efi_pool) = NULL;
-	unsigned long image_base, alloc_size;
+	unsigned long image_base, image_size, alloc_size;
 	efi_loaded_image_t *image;
 	efi_status_t status;
 
@@ -67,12 +67,17 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	if (status != EFI_SUCCESS)
 		return status;
 
-	efi_info("Decompressing Linux Kernel...\n");
-
-	status = efi_zboot_decompress_init(&alloc_size);
+	status = efi_drtm_prepare();
 	if (status != EFI_SUCCESS)
 		return status;
 
+	efi_info("Decompressing Linux Kernel...\n");
+
+	status = efi_zboot_decompress_init(&image_size);
+	if (status != EFI_SUCCESS)
+		return status;
+	alloc_size = image_size + efi_drtm_get_extra_size();
+
 	 // If the architecture has a preferred address for the image,
 	 // try that first.
 	image_base = alloc_preferred_address(alloc_size);
@@ -107,7 +112,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	}
 
 	// Decompress the payload into the newly allocated buffer
-	status = efi_zboot_decompress((void *)image_base, alloc_size);
+	status = efi_zboot_decompress((void *)image_base, image_size);
 	if (status != EFI_SUCCESS)
 		goto error;
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/11 efi/libstub: Use free_addr/size as the arg to handle_kernel_image

In reply to Jason Gunthorpe

The only use for these values is to trigger a memory free if the boot is aborted by efi_pe_entry(), make it clear through the call chain.

This cleanly allows them to be used to describe something that is not the image in the next patch.

Reorganize efi_kaslr_relocate_kernel() with a efi_kaslr_copy_kernel() helper as clang isn't able to grok the original control flow once image_addr is only set by allocation.

---
 drivers/firmware/efi/libstub/arm32-stub.c     |  8 +-
 drivers/firmware/efi/libstub/arm64-stub.c     |  6 +-
 drivers/firmware/efi/libstub/efi-stub-entry.c |  8 +-
 drivers/firmware/efi/libstub/efistub.h        |  8 +-
 drivers/firmware/efi/libstub/kaslr.c          | 88 +++++++++++--------
 drivers/firmware/efi/libstub/loongarch-stub.c | 10 +--
 drivers/firmware/efi/libstub/riscv-stub.c     |  6 +-
 7 files changed, 73 insertions(+), 61 deletions(-)
drivers/firmware/efi/libstub/arm32-stub.c +4 -4
index 89e48ac0b7cbbe..b4c843e7f6b8fd 100644
@@ -125,8 +125,8 @@ extern u32 zimage_magic_table[] asm("_table_start");
 
 struct efi_image_info efistub_image_info = {};
 
-efi_status_t handle_kernel_image(unsigned long *image_addr,
-				 unsigned long *image_size,
+efi_status_t handle_kernel_image(unsigned long *free_addr,
+				 unsigned long *free_size,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
@@ -173,8 +173,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	efi_free(kernel_base - alloc_base, alloc_base);	/* no-op if 0 */
 	efi_free(alloc_base + margin - kernel_base, kernel_base + alloc_size);
 
-	*image_addr = kernel_base;
-	*image_size = alloc_size;
+	*free_addr = kernel_base;
+	*free_size = alloc_size;
 
 	return EFI_SUCCESS;
 }
drivers/firmware/efi/libstub/arm64-stub.c +3 -3
index 351fcb4493e19b..e0adc4de0beac3 100644
@@ -13,8 +13,8 @@
 
 #include "efistub.h"
 
-efi_status_t handle_kernel_image(unsigned long *image_addr,
-				 unsigned long *image_size,
+efi_status_t handle_kernel_image(unsigned long *free_addr,
+				 unsigned long *free_size,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
@@ -30,7 +30,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
 			SEGMENT_ALIGN >> 10);
 
-	return efi_kaslr_relocate_kernel(image_addr, image_size, image_info,
+	return efi_kaslr_relocate_kernel(free_addr, free_size, image_info,
 					 efi_kaslr_get_phys_seed(image_handle));
 }
 
drivers/firmware/efi/libstub/efi-stub-entry.c +4 -4
index fd272d07737371..7febed273c8c08 100644
@@ -21,8 +21,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	const struct efi_image_info *image_info = &efistub_image_info;
 	efi_loaded_image_t *image;
 	efi_status_t status;
-	unsigned long image_addr;
-	unsigned long image_size = 0;
+	unsigned long free_addr;
+	unsigned long free_size = 0;
 
 	WRITE_ONCE(efi_system_table, systab);
 
@@ -53,7 +53,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 
 	efi_info("Booting Linux Kernel...\n");
 
-	status = handle_kernel_image(&image_addr, &image_size,
+	status = handle_kernel_image(&free_addr, &free_size,
 				     image, handle, &image_info);
 	if (status != EFI_SUCCESS) {
 		efi_err("Failed to relocate kernel\n");
@@ -62,7 +62,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 
 	status = efi_stub_common(handle, image, image_info, cmdline_ptr);
 
-	efi_free(image_size, image_addr);
+	efi_free(free_size, free_addr);
 
 	return status;
 }
drivers/firmware/efi/libstub/efistub.h +4 -4
index 80b9558f9f689a..e404f29693c51c 100644
@@ -1187,8 +1187,8 @@ struct efi_image_info {
  * must be reserved. On failure it is required to free all
  * all allocations it has made.
  */
-efi_status_t handle_kernel_image(unsigned long *image_addr,
-				 unsigned long *image_size,
+efi_status_t handle_kernel_image(unsigned long *free_addr,
+				 unsigned long *free_size,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info);
@@ -1331,8 +1331,8 @@ const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,
 
 efi_status_t efi_remap_image(unsigned long image_base, unsigned alloc_size,
 			     unsigned long code_size);
-efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
-				       unsigned long *alloc_size,
+efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
+				       unsigned long *free_size,
 				       const struct efi_image_info **image_info,
 				       u32 phys_seed);
 u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle);
drivers/firmware/efi/libstub/kaslr.c +50 -38

119 lines not on this page · raw message

drivers/firmware/efi/libstub/loongarch-stub.c +5 -5
index df6749643bb8bd..d8886b9c74099d 100644
@@ -89,8 +89,8 @@ efi_status_t efi_relocate_kernel(unsigned long *image_addr,
 	return status;
 }
 
-efi_status_t handle_kernel_image(unsigned long *image_addr,
-				 unsigned long *image_size,
+efi_status_t handle_kernel_image(unsigned long *free_addr,
+				 unsigned long *free_size,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
@@ -105,9 +105,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
 		     EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
 
-	*image_addr = kernel_addr;
-	*image_size = kernel_asize;
+	*free_addr = kernel_addr;
+	*free_size = kernel_asize;
 
-	*(unsigned long *)image_info += *image_addr - (unsigned long)image->image_base;
+	*(unsigned long *)image_info += kernel_addr - (unsigned long)image->image_base;
 	return status;
 }
drivers/firmware/efi/libstub/riscv-stub.c +3 -3
index 8cd0cd9b00486c..45a345c1701ffa 100644
@@ -10,15 +10,15 @@
 
 #include "efistub.h"
 
-efi_status_t handle_kernel_image(unsigned long *image_addr,
-				 unsigned long *image_size,
+efi_status_t handle_kernel_image(unsigned long *free_addr,
+				 unsigned long *free_size,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
 {
 	efi_status_t status;
 
-	status = efi_kaslr_relocate_kernel(image_addr, image_size, image_info,
+	status = efi_kaslr_relocate_kernel(free_addr, free_size, image_info,
 					   efi_kaslr_get_phys_seed(image_handle));
 	if (status != EFI_SUCCESS)
 		efi_err("Failed to relocate kernel\n");
Signature · 1 line
2.43.0
Reviewed-by on v3 2/11
#3 lore raw

Re: [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image

Quoted 11 lines · On Fri, 20 Nov 2026 13:51:55 -0400 Jason Gunthorpe <[email protected]> wrote:

The only use for these values is to trigger a memory free if the boot is aborted by efi_pe_entry(), make it clear through the call chain.

This cleanly allows them to be used to describe something that is not the image in the next patch.

Reorganize efi_kaslr_relocate_kernel() with a efi_kaslr_copy_kernel() helper as clang isn't able to grok the original control flow once image_addr is only set by allocation.

One small thing inline. I think the code flow is a little missleading with the update of *free_addr and *free_size inside a function that wasn't responsible for the allocation.

With that changed.

Quoted 11 lines
drivers/firmware/efi/libstub/kaslr.c +3 -0
index 7cf68a6123946a..2f1f3e19583def 100644
@@ -80,10 +80,30 @@ static bool check_image_region(u64 base, u64 size)
 	return false;
 }
 
+static void efi_kaslr_copy_kernel(unsigned long image_addr,
+				  unsigned long *free_addr,
+				  unsigned long *free_size,

drivers/firmware/efi/libstub/kaslr.c static bool check_image_region(u64 base, u64 size) · line 95

Quoted 10 lines
drivers/firmware/efi/libstub/kaslr.c +10 -0
+				  const struct efi_image_info **image_info)
+{
+	unsigned long kernel_base	= efistub_kernel_base(*image_info);
+	unsigned long kernel_size	= efistub_kernel_size(*image_info);
+	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
+	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
+
+	memcpy((void *)image_addr, (void *)kernel_base, kernel_size);
+	*free_addr = image_addr;
+	*free_size = kernel_memsize;

This feels like it's in slightly the wrong place. The allocation was what made these necessary to free not the copy.

Could we set those two just before calling this copy function? Means duplicating these two lines which isn't ideal.

Or does that not avoid the llvm issue?

Quoted 49 lines
drivers/firmware/efi/libstub/kaslr.c +13 -13
+
+	efi_icache_sync(image_addr, image_addr + kernel_codesize);
+	efi_remap_image(image_addr, kernel_memsize, kernel_codesize);
+
+	*(unsigned long *)image_info += image_addr - kernel_base;
+}
+
 /**
  * efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled)
- * @image_addr:		Pointer to variable carrying the image base address
- * @alloc_size:		Pointer to variable carrying the allocated size
+ * @free_addr:		Pointer to variable carrying the address to free
+ * @free_size:		Pointer to variable carrying the size to free
  * @image_info:		Pointer to struct efi_image_info pointer
  * @phys_seed:		Random seed used for the relocation
  *
@@ -93,16 +113,15 @@ static bool check_image_region(u64 base, u64 size)
  *
  * Return:	status code, EFI_SUCCESS if relocation is successful
  */
-efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
-				       unsigned long *alloc_size,
+efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
+				       unsigned long *free_size,
 				       const struct efi_image_info **image_info,
 				       u32 phys_seed)
 {
 	unsigned long kernel_base	= efistub_kernel_base(*image_info);
-	unsigned long kernel_size	= efistub_kernel_size(*image_info);
-	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
 	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
 	u64 min_kimg_align		= efi_get_kimg_min_align();
+	unsigned long image_addr;
 	efi_status_t status;
 
 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
@@ -111,43 +130,36 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
 		 * locate the kernel at a randomized offset in physical memory.
 		 */
 		status = efi_random_alloc(kernel_memsize, min_kimg_align,
-					  image_addr, phys_seed,
+					  &image_addr, phys_seed,
 					  EFI_LOADER_CODE, 0, EFI_ALLOC_LIMIT);
-		if (status != EFI_SUCCESS)
-			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
-	} else {
-		status = EFI_OUT_OF_RESOURCES;
-	}
-

drivers/firmware/efi/libstub/kaslr.c efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, · line 135

Quoted 10 lines
drivers/firmware/efi/libstub/kaslr.c +1 -9
-	if (status != EFI_SUCCESS) {
-		if (!check_image_region(kernel_base, kernel_memsize)) {
-			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
-		} else if (IS_ALIGNED(kernel_base, min_kimg_align) &&
-			   (kernel_base + kernel_memsize) < EFI_ALLOC_LIMIT) {
-			/*
-			 * Just execute from wherever we were loaded by the
-			 * UEFI PE/COFF loader if the placement is suitable.
-			 */
+		if (status == EFI_SUCCESS) {

That means setting free_addr and free_size here...

Quoted 33 lines
drivers/firmware/efi/libstub/kaslr.c +15 -13
+			efi_kaslr_copy_kernel(image_addr, free_addr, free_size,
+					      image_info);
 			return EFI_SUCCESS;
 		}
-
-		status = efi_allocate_pages_aligned(kernel_memsize, image_addr,
-						    ULONG_MAX, min_kimg_align,
-						    EFI_LOADER_CODE);
-
-		if (status != EFI_SUCCESS) {
-			efi_err("Failed to relocate kernel\n");
-			return status;
-		}
+		efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
 	}
 
-	memcpy((void *)*image_addr, (void *)kernel_base, kernel_size);
-	*alloc_size = kernel_memsize;
+	/* Try to use the image in place */
+	if (!check_image_region(kernel_base, kernel_memsize)) {
+		efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
+	} else if (IS_ALIGNED(kernel_base, min_kimg_align) &&
+		   (kernel_base + kernel_memsize) < EFI_ALLOC_LIMIT) {
+		/*
+		 * Just execute from wherever we were loaded by the UEFI PE/COFF
+		 * loader if the placement is suitable.
+		 */
+		return EFI_SUCCESS;
+	}
 
-	efi_icache_sync(*image_addr, *image_addr + kernel_codesize);
-	efi_remap_image(*image_addr, kernel_memsize, kernel_codesize);
+	status = efi_allocate_pages_aligned(kernel_memsize, &image_addr,

drivers/firmware/efi/libstub/kaslr.c efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, · line 152

Quoted 10 lines
drivers/firmware/efi/libstub/kaslr.c +6 -3
+					    ULONG_MAX, min_kimg_align,
+					    EFI_LOADER_CODE);
+	if (status != EFI_SUCCESS) {
+		efi_err("Failed to relocate kernel\n");
+		return status;
+	}
 
-	*(unsigned long *)image_info += *image_addr - kernel_base;
-
-	return status;

and here...

Quoted 3 lines
drivers/firmware/efi/libstub/kaslr.c +2 -0
+	efi_kaslr_copy_kernel(image_addr, free_addr, free_size, image_info);
+	return EFI_SUCCESS;
 }
#4 lore raw

PATCH v3 3/11 efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size

In reply to Jason Gunthorpe

extra_size is additional memory required that must be immediately after the image.

There are three possibilities here, each a little different:
- relocating for kaslr, just request extra_size like the zboot flow does when allocating the random placement from EFI
- in place, attempt to allocate from EFI precisely after Image to confirm nothing weird is there, failing that
- like any other error with the in place EFI location allocate non-randomly with extra_size included

Adjust things to use a kernel_allocsize which is >= the memsize computed from the image_info.

---
 drivers/firmware/efi/libstub/kaslr.c | 77 ++++++++++++++++++++++------
 1 file changed, 61 insertions(+), 16 deletions(-)
drivers/firmware/efi/libstub/kaslr.c +61 -16
index 2f1f3e19583def..f50c36a2fd635e 100644
@@ -80,7 +80,44 @@ static bool check_image_region(u64 base, u64 size)
 	return false;
 }
 
+/*
+ * The PE loader owns only kernel_memsize of the image.  Try to own the
+ * requested tail separately at the exact adjacent address instead of
+ * relocating the complete region.
+ */
+static efi_status_t allocate_image_tail(unsigned long image_addr,
+					unsigned long kernel_memsize,
+					unsigned long extra_size,
+					unsigned long *free_addr,
+					unsigned long *free_size)
+{
+	efi_physical_addr_t tail_addr;
+	efi_status_t status;
+
+	/*
+	 * Since we intend to use efi_free() for reserve_addr it should be
+	 * aligned to the higher alignment since efi_free() includes rounding.
+	 * For ARM64 the kernel image is already aligned up to EFI_ALLOC_ALIGN
+	 * by the linker.
+	 */
+	tail_addr = image_addr + kernel_memsize;
+	if (!IS_ALIGNED(tail_addr, EFI_ALLOC_ALIGN))
+		return EFI_OUT_OF_RESOURCES;
+
+	extra_size = round_up(extra_size, EFI_ALLOC_ALIGN);
+	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
+			     EFI_LOADER_CODE, extra_size / EFI_PAGE_SIZE,
+			     &tail_addr);
+	if (status != EFI_SUCCESS)
+		return status;
+
+	*free_addr = tail_addr;
+	*free_size = extra_size;
+	return EFI_SUCCESS;
+}
+
 static void efi_kaslr_copy_kernel(unsigned long image_addr,
+				  unsigned long kernel_allocsize,
 				  unsigned long *free_addr,
 				  unsigned long *free_size,
 				  const struct efi_image_info **image_info)
@@ -88,14 +125,13 @@ static void efi_kaslr_copy_kernel(unsigned long image_addr,
 	unsigned long kernel_base	= efistub_kernel_base(*image_info);
 	unsigned long kernel_size	= efistub_kernel_size(*image_info);
 	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
-	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
 
 	memcpy((void *)image_addr, (void *)kernel_base, kernel_size);
 	*free_addr = image_addr;
-	*free_size = kernel_memsize;
+	*free_size = kernel_allocsize;
 
 	efi_icache_sync(image_addr, image_addr + kernel_codesize);
-	efi_remap_image(image_addr, kernel_memsize, kernel_codesize);
+	efi_remap_image(image_addr, kernel_allocsize, kernel_codesize);
 
 	*(unsigned long *)image_info += image_addr - kernel_base;
 }
@@ -119,7 +155,9 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
 				       u32 phys_seed)
 {
 	unsigned long kernel_base	= efistub_kernel_base(*image_info);
-	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
+	unsigned long extra_size	= efi_drtm_get_extra_size();
+	unsigned long kernel_allocsize	= efistub_kernel_memsize(*image_info) +
+					  extra_size;
 	u64 min_kimg_align		= efi_get_kimg_min_align();
 	unsigned long image_addr;
 	efi_status_t status;
@@ -129,30 +167,36 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
 		 * If KASLR is enabled, and we have some randomness available,
 		 * locate the kernel at a randomized offset in physical memory.
 		 */
-		status = efi_random_alloc(kernel_memsize, min_kimg_align,
+		status = efi_random_alloc(kernel_allocsize, min_kimg_align,
 					  &image_addr, phys_seed,
 					  EFI_LOADER_CODE, 0, EFI_ALLOC_LIMIT);
 		if (status == EFI_SUCCESS) {
-			efi_kaslr_copy_kernel(image_addr, free_addr, free_size,
-					      image_info);
+			efi_kaslr_copy_kernel(image_addr, kernel_allocsize,
+					      free_addr, free_size, image_info);
 			return EFI_SUCCESS;
 		}
 		efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
 	}
 
 	/* Try to use the image in place */
-	if (!check_image_region(kernel_base, kernel_memsize)) {
+	if (!check_image_region(kernel_base, kernel_allocsize)) {
 		efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
 	} else if (IS_ALIGNED(kernel_base, min_kimg_align) &&
-		   (kernel_base + kernel_memsize) < EFI_ALLOC_LIMIT) {
-		/*
-		 * Just execute from wherever we were loaded by the UEFI PE/COFF
-		 * loader if the placement is suitable.
-		 */
-		return EFI_SUCCESS;
+		   (kernel_base + kernel_allocsize) < EFI_ALLOC_LIMIT) {
+		if (!extra_size) {
+			/*
+			 * Just execute from wherever we were loaded by the UEFI
+			 * PE/COFF loader if the placement is suitable.
+			 */
+			return EFI_SUCCESS;
+		}
+		status = allocate_image_tail(kernel_base, kernel_allocsize,
+					     extra_size, free_addr, free_size);
+		if (status == EFI_SUCCESS)
+			return EFI_SUCCESS;
 	}
 
-	status = efi_allocate_pages_aligned(kernel_memsize, &image_addr,
+	status = efi_allocate_pages_aligned(kernel_allocsize, &image_addr,
 					    ULONG_MAX, min_kimg_align,
 					    EFI_LOADER_CODE);
 	if (status != EFI_SUCCESS) {
@@ -160,6 +204,7 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
 		return status;
 	}
 
-	efi_kaslr_copy_kernel(image_addr, free_addr, free_size, image_info);
+	efi_kaslr_copy_kernel(image_addr, kernel_allocsize, free_addr,
+			      free_size, image_info);
 	return EFI_SUCCESS;
 }
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size

Hi Jason,

Quoted 3 lines · Jason Gunthorpe

extra_size is additional memory required that must be immediately after the image.

Given how extra BSS memory is essentially free at boot, is there no way we can establish a worst case upper bound for this memory, and just add it to the image unconditionally for DRTM capable kernels?

#6 lore raw

Re: [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size

Quoted 10 lines · Ard Biesheuvel

Hi Jason,

Quoted 3 lines · Jason Gunthorpe

extra_size is additional memory required that must be immediately after the image.

Given how extra BSS memory is essentially free at boot, is there no way we can establish a worst case upper bound for this memory, and just add it to the image unconditionally for DRTM capable kernels?

It doesn't look like it, at least not without reducing support. The FW can request some arbitrary amount for its own private usage during the launch.

Jason

Reviewed-by on v3 3/11
#7 lore raw

Re: [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size

In reply to Jason Gunthorpe
Quoted 11 lines · On Fri, 20 Nov 2026 13:51:56 -0400 Jason Gunthorpe <[email protected]> wrote:

extra_size is additional memory required that must be immediately after the image.

There are three possibilities here, each a little different:
- relocating for kaslr, just request extra_size like the zboot flow does when allocating the random placement from EFI
- in place, attempt to allocate from EFI precisely after Image to confirm nothing weird is there, failing that
- like any other error with the in place EFI location allocate non-randomly with extra_size included

Quoted 4 lines

Adjust things to use a kernel_allocsize which is >= the memsize computed from the image_info.

Comment on patch 2 follows through. Assuming you'll either resolve that or convince me otherwise,

#8 lore raw

PATCH v3 4/11 efi: Add a __efi_data_handoff section annotation

In reply to Jason Gunthorpe

For a clean DRTM measurement, data written by the stub in the image must not fall within the measured range. __efi_data_handoff can be used to mark this data so it is placed outside the measured .data or .init.data section, and also ensure it is force allocated and not part of .bss.

For DRTM data marked this way will need eventual hardening when the kernel consumes it. For example sysfb_primary_display eventually leads to a blind ioremap. Later series will look at hardening these flows.

The arch-agnostic sysfb_primary_display is the first user. The embedded stub writes it.

---
 drivers/firmware/efi/efi-init.c |  2 +-
 include/linux/efi.h             | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/firmware/efi/efi-init.c +1 -1
index 25a5497ebb2ea1..a6ce8b7897967e 100644
@@ -59,7 +59,7 @@ extern __weak const efi_config_table_type_t efi_arch_tables[];
  * it even without EFI, everything else can get them from here.
  */
 #if !defined(CONFIG_X86) && (defined(CONFIG_SYSFB) || defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_FIRMWARE_EDID))
-struct sysfb_display_info sysfb_primary_display __section(".data");
+struct sysfb_display_info sysfb_primary_display __efi_data_handoff;
 EXPORT_SYMBOL_GPL(sysfb_primary_display);
 #endif
 
include/linux/efi.h +12 -0
index 4a809c6ae7241d..f79c5669a356cb 100644
@@ -29,6 +29,18 @@
 
 struct screen_info;
 
+/*
+ * Data the stub wants to pass to the kernel must not land in .bss so it doesn't
+ * get zero'd during early boot, and when DRTM is enabled must not land in the
+ * measured sections. The offset of such data can be passed to both EFI stubs
+ * using a mechanism like struct efi_image_info.
+ */
+#ifdef CONFIG_EFI_STUB_DRTM
+#define __efi_data_handoff	__section(".unmeasured.data")
+#else
+#define __efi_data_handoff	__section(".data")
+#endif
+
 #define EFI_SUCCESS		0
 #define EFI_LOAD_ERROR		( 1 | (1UL << (BITS_PER_LONG-1)))
 #define EFI_INVALID_PARAMETER	( 2 | (1UL << (BITS_PER_LONG-1)))
Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 5/11 efi/libstub: Put the stub's writable data in unique sections for DRTM

In reply to Jason Gunthorpe

Currently the EFI stub's .bss and .data are both placed in .init.data, which allows them to be discarded after boot, but places them within the region of memory we need to measure as part of a DRTM launch.

If the EFI stub modifies the measured data before the measurement, then we can no longer get a predictable measurement.

Give them unique section names; the arch's linker script has to put these sections outside the measured range.

---
 drivers/firmware/efi/libstub/Makefile  | 10 ++++++++++
 drivers/firmware/efi/libstub/zboot.lds |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/Makefile +10 -0
index 0c5fbc21d2a297..207c8f99c5e299 100644
@@ -145,6 +145,16 @@ STUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
 				   --prefix-symbols=__efistub_
 STUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
 
+# Give writable stub state a dedicated section namespace.  objcopy applies
+# section renames before section prefixes, irrespective of their command line
+# ordering, so these become .init.efidata and .init.efibss.
+#
+# The names must stay outside the .init.data.* namespace.  INIT_DATA collects
+# *(.init.data .init.data.*) into a measured kernel output section that the
+# arm64 linker script emits first.
+STUBCOPY_FLAGS-$(CONFIG_EFI_STUB_DRTM) += --rename-section .data=.efidata \
+					   --rename-section .bss=.efibss,load,alloc
+
 # For RISC-V, we don't need anything special other than arm64. Keep all the
 # symbols in .init section and make sure that no absolute symbols references
 # exist.
drivers/firmware/efi/libstub/zboot.lds +1 -1
index e0f0bf00585487..34dfe3c2e77d03 100644
@@ -36,7 +36,7 @@ SECTIONS
 
 	.data : ALIGN(4096) {
 		_data = .;
-		*(.data* .init.data*)
+		*(.data* .init.data* .init.efidata* .init.efibss*)
 		_edata = ALIGN(512);
 		. = _edata;
 	}
Signature · 1 line
2.43.0
#10 lore raw

PATCH v3 6/11 arm64: drtm: Add macro definitions for DEN0113

In reply to Jason Gunthorpe

Taken from rev 1.4b of the spec, following the SMCC register layout and constant names.

---
 arch/arm64/include/asm/drtm.h | 305 ++++++++++++++++++++++++++++++++++
 1 file changed, 305 insertions(+)
 create mode 100644 arch/arm64/include/asm/drtm.h
arch/arm64/include/asm/drtm.h new file +305 -0
new file mode 100644
index 00000000000000..34201bc092bcc6
@@ -0,0 +1,305 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES
+ *
+ * Definitions from Arm DEN0113 "DRTM Architecture for Arm".
+ */
+#ifndef __ASM_DRTM_H
+#define __ASM_DRTM_H
+
+#include <linux/arm-smccc.h>
+#include <linux/bits.h>
+#include <linux/sizes.h>
+
+/*
+ * v1.4B section 3.1 "Introduction to interface functions and data
+ * structures". Function IDs are 0x110 - 0x12F, called with SMC64.
+ * Offset 0x02 is reserved.
+ */
+#define ARM_DRTM_SMC_FN_BASE                                      \
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64, \
+			   ARM_SMCCC_OWNER_STANDARD, 0x110)
+
+/*
+ * The spec uses 4KB pages for sizes and alignment throughout, see v1.4B
+ * sections 3.3, 3.13, 3.14 and 3.15.
+ */
+#define ARM_DRTM_PAGE_SIZE			SZ_4K
+
+/* v1.4B section 3.2 "DRTM_VERSION". */
+#define ARM_DRTM_SMC_VERSION			(ARM_DRTM_SMC_FN_BASE + 0x00)
+#define ARM_DRTM_VERSION_MAJOR_MASK		GENMASK_U32(30, 16)
+#define ARM_DRTM_VERSION_MINOR_MASK		GENMASK_U32(15, 0)
+#define ARM_DRTM_VERSION_MAJOR			1
+
+/* v1.4B section 3.3 "DRTM_FEATURES" */
+#define ARM_DRTM_SMC_FEATURES			(ARM_DRTM_SMC_FN_BASE + 0x01)
+/* X1 input parameter */
+#define ARM_DRTM_FEATURE_SELECTOR		BIT_U64(63)
+#define ARM_DRTM_FEATURE_ID_MASK		GENMASK_U64(7, 0)
+#define ARM_DRTM_FEATURE_FUNCID_MASK		GENMASK_U64(31, 0)
+
+/*
+ * v1.4B section 3.3, Table 6 "Return values for DRTM features". Use with
+ * arm_drtm_query_feature() below.
+ */
+
+/* v1.4B Table 6 "TPM features" */
+#define ARM_DRTM_FEATURE_TPM			0x01
+/* Algorithm IDs are defined by TCG, in the kernel they are TPM_ALG_* */
+#define ARM_DRTM_TPM_ALG_MASK			GENMASK_U64(15, 0)
+#define ARM_DRTM_TPM_HASHING			BIT_U64(32)
+#define ARM_DRTM_PCR_SCHEMA_MASK		GENMASK_U64(36, 33)
+
+/*
+ * PCR schema numbers. ARM_DRTM_PCR_SCHEMA_MASK is a bitmap indexed by these,
+ * and the Launch Features ARM_DRTM_LAUNCH_PCR_SCHEMA_MASK field holds one.
+ */
+#define ARM_DRTM_PCR_SCHEMA_DEFAULT		0
+#define ARM_DRTM_PCR_SCHEMA_AUTHORITIES		1
+
+/* v1.4B Table 6 "Minimum memory requirement", in ARM_DRTM_PAGE_SIZE units */
+#define ARM_DRTM_FEATURE_MIN_MEMORY		0x02
+#define ARM_DRTM_DLME_DATA_PAGES_MASK		GENMASK_U64(31, 0)
+#define ARM_DRTM_NW_DCE_PAGES_MASK		GENMASK_U64(63, 32)
+
+/* v1.4B Table 6 "DMA protection features" */
+#define ARM_DRTM_FEATURE_DMA_PROTECTION		0x03
+#define ARM_DRTM_DMA_PROTECTION_MASK		GENMASK_U64(7, 0)
+/* Only valid if ARM_DRTM_DMA_PROTECTION_REGION is supported */
+#define ARM_DRTM_MAX_REGIONS_MASK		GENMASK_U64(23, 8)
+
+/*
+ * DMA protection types. ARM_DRTM_DMA_PROTECTION_MASK is a bitmap indexed by
+ * these, and the Launch Features ARM_DRTM_LAUNCH_DMA_PROTECTION_MASK field
+ * holds one.
+ */
+#define ARM_DRTM_DMA_PROTECTION_COMPLETE	0
+#define ARM_DRTM_DMA_PROTECTION_REGION		1
+
+/*
+ * v1.4B Table 6 "Boot PE ID", the value is the PSCI CPU_ON target_cpu
+ * encoding of the boot PE
+ */
+#define ARM_DRTM_FEATURE_BOOT_PE		0x04
+
+/* v1.4B Table 6 "TCB hash features", max hashes for DRTM_SET_TCB_HASH */
+#define ARM_DRTM_FEATURE_TCB_HASH		0x05
+#define ARM_DRTM_TCB_HASH_COUNT_MASK		GENMASK_U64(7, 0)
+
+/* v1.4B Table 6 "DLME image authentication features" */
+#define ARM_DRTM_FEATURE_IMAGE_AUTH		0x06
+#define ARM_DRTM_IMAGE_AUTH_SUPPORTED		BIT_U64(0)
+
+/* v1.4B section 3.4 "DRTM_DYNAMIC_LAUNCH" */
+#define ARM_DRTM_SMC_DYNAMIC_LAUNCH		(ARM_DRTM_SMC_FN_BASE + 0x04)
+
+/* v1.4B section 3.5 "DRTM_UNPROTECT_MEMORY" */
+#define ARM_DRTM_SMC_UNPROTECT_MEMORY		(ARM_DRTM_SMC_FN_BASE + 0x03)
+
+/* v1.4B section 3.6 "DRTM_CLOSE_LOCALITY" */
+#define ARM_DRTM_SMC_CLOSE_LOCALITY		(ARM_DRTM_SMC_FN_BASE + 0x05)
+
+/* v1.4B section 3.7 "DRTM_GET_ERROR" */
+#define ARM_DRTM_SMC_GET_ERROR			(ARM_DRTM_SMC_FN_BASE + 0x06)
+
+/* v1.4B section 3.8 "DRTM_SET_ERROR" */
+#define ARM_DRTM_SMC_SET_ERROR			(ARM_DRTM_SMC_FN_BASE + 0x07)
+
+/*
+ * v1.4B section 3.12 "DRTM error code encoding", Table 7. Value passed as
+ * the Error Code parameter to DRTM_SET_ERROR and returned by
+ * DRTM_GET_ERROR.
+ */
+#define ARM_DRTM_ERROR_PHASE_MASK		GENMASK_U64(2, 0)
+#define ARM_DRTM_ERROR_ID_MASK			GENMASK_U64(10, 3)
+#define ARM_DRTM_ERROR_DATA_MASK		GENMASK_U64(63, 11)
+
+/* v1.4B Table 7 "DRTM phase when error occurred" */
+#define ARM_DRTM_ERROR_PHASE_NONE		0
+#define ARM_DRTM_ERROR_PHASE_DCRTM_FW		1
+#define ARM_DRTM_ERROR_PHASE_DCRTM_COPROCESSOR	2
+#define ARM_DRTM_ERROR_PHASE_DCE		3
+#define ARM_DRTM_ERROR_PHASE_NW_DCE		4
+#define ARM_DRTM_ERROR_PHASE_DLME		5
+
+/* v1.4B Table 8 "Definition for error IDs (Bits[10:3])" */
+#define ARM_DRTM_ERROR_ID_NONE			0x0
+#define ARM_DRTM_ERROR_ID_PARAMETERS		0x2
+#define ARM_DRTM_ERROR_ID_SECURE_EXCEPTION	0x3
+#define ARM_DRTM_ERROR_ID_TCB_HASH_NOT_LOCKED	0x4
+#define ARM_DRTM_ERROR_ID_TPM			0x5
+#define ARM_DRTM_ERROR_ID_SIGNATURE		0x6
+#define ARM_DRTM_ERROR_ID_IMAGE_FORMAT		0x7
+#define ARM_DRTM_ERROR_ID_VENDOR		0xFF
+
+/*
+ * Construct the Table 7 error code. Plain shifts so this is usable from
+ * assembly as well as C.
+ */
+#define ARM_DRTM_MAKE_ERROR(phase, id, data) \
+	(((data) << 11) | ((id) << 3) | (phase))
+
+/* v1.4B section 3.9 "DRTM_SET_TCB_HASH" */
+#define ARM_DRTM_SMC_SET_TCB_HASH		(ARM_DRTM_SMC_FN_BASE + 0x08)
+
+/* v1.4B section 3.10 "DRTM_LOCK_TCB_HASHES" */
+#define ARM_DRTM_SMC_LOCK_TCB_HASHES		(ARM_DRTM_SMC_FN_BASE + 0x09)
+
+/* v1.4B section 3.11 "DRTM_ENABLE_SECURE_INTERRUPTS" */
+#define ARM_DRTM_SMC_ENABLE_SECURE_INTERRUPTS	(ARM_DRTM_SMC_FN_BASE + 0x0a)
+
+/*
+ * v1.4B Table 9 "Launch Features" field. The 0 constants are for
+ * self-documentation of the selected behavior.
+ */
+#define ARM_DRTM_LAUNCH_HASH_FIRMWARE		0
+#define ARM_DRTM_LAUNCH_HASH_TPM		BIT_U32(0)
+#define ARM_DRTM_LAUNCH_PCR_SCHEMA_MASK		GENMASK_U32(2, 1)
+#define ARM_DRTM_LAUNCH_DMA_PROTECTION_MASK	GENMASK_U32(5, 3)
+#define ARM_DRTM_LAUNCH_NO_AUTH			0
+#define ARM_DRTM_LAUNCH_AUTH			BIT_U32(6)
+#define ARM_DRTM_LAUNCH_KEEP_SECURE_IRQS	0
+#define ARM_DRTM_LAUNCH_DISABLE_SECURE_IRQS	BIT_U32(7)
+
+/* v1.4B section 3.18 "Return codes", Table 20 "Return codes and values" */
+#define ARM_DRTM_SUCCESS			0
+#define ARM_DRTM_NOT_SUPPORTED			-1
+#define ARM_DRTM_INVALID_PARAMETERS		-2
+#define ARM_DRTM_DENIED				-3
+#define ARM_DRTM_NOT_FOUND			-4
+#define ARM_DRTM_INTERNAL_ERROR			-5
+#define ARM_DRTM_MEM_PROTECT_INVALID		-6
+#define ARM_DRTM_COPROCESSOR_ERROR		-7
+#define ARM_DRTM_OUT_OF_RESOURCES		-8
+#define ARM_DRTM_INVALID_DATA			-9
+#define ARM_DRTM_SECONDARY_PE_NOT_OFF		-10
+#define ARM_DRTM_ALREADY_CLOSED			-11
+#define ARM_DRTM_TPM_ERROR			-12
+
+#ifndef __ASSEMBLY__
+
+#include <linux/bitfield.h>
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+#include <linux/types.h>
+
+/* v1.4B section 3.13, Table 9 "DRTM_PARAMETERS definition" */
+struct arm_drtm_parameters {
+	__le16 revision;
+	__le16 reserved;
+	__le32 launch_features;
+	__le64 dlme_region_address;
+	__le64 dlme_region_size;
+	__le64 dlme_image_start;
+	__le64 dlme_entry_point_offset;
+	__le64 dlme_image_size;
+	__le64 dlme_data_offset;
+	__le64 nw_dce_region_address;
+	__le64 nw_dce_region_size;
+	__le64 protection_table_address;
+	__le64 protection_table_size;
+};
+static_assert(sizeof(struct arm_drtm_parameters) == 88);
+
+static inline s32 arm_drtm_version(u16 *major, u16 *minor)
+{
+	struct arm_smccc_res res;
+	u32 version;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
+	version = res.a0;
+	/*
+	 * For this command only the output is specified as w0 a 32 bit value.
+	 * All others use x0 a 64 bit value. The -ve error and version are
+	 * overlayed together with the 32 bit sign bit telling them apart.
+	 */
+	if (version & BIT(31))
+		return version;
+
+	*major = FIELD_GET(ARM_DRTM_VERSION_MAJOR_MASK, version);
+	*minor = FIELD_GET(ARM_DRTM_VERSION_MINOR_MASK, version);
+	return ARM_DRTM_SUCCESS;
+}
+
+static inline s64 arm_drtm_query_function(u64 function_id)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_FEATURES, function_id, 0, 0, 0, 0, 0, 0,
+			  &res);
+	return res.a0;
+}
+
+/*
+ * v1.4b Section 3.3.1 says:
+ *   A return value of greater than 0 means the feature ID is implemented, and
+ *   there are other feature-specific feature or capability bits available in
+ *   other return value registers.
+ * And for FEATURE_SELECTOR all defined features return data in other
+ * registers, so 0 is not an allowed return.
+ */
+static inline s64 arm_drtm_query_feature(u64 feature, u64 *value)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_FEATURES,
+			  ARM_DRTM_FEATURE_SELECTOR |
+				  FIELD_PREP(ARM_DRTM_FEATURE_ID_MASK, feature),
+			  0, 0, 0, 0, 0, 0, &res);
+	*value = res.a1;
+	return res.a0;
+}
+
+static inline s64 arm_drtm_unprotect_memory(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_UNPROTECT_MEMORY, 0, 0, 0, 0, 0, 0, 0,
+			  &res);
+	return res.a0;
+}
+
+static inline s64
+arm_drtm_dynamic_launch(struct arm_drtm_parameters *params_addr)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_DYNAMIC_LAUNCH,
+			  (unsigned long)params_addr, 0, 0, 0, 0, 0, 0, &res);
+	return res.a0;
+}
+
+static inline s64 arm_drtm_close_locality(u32 locality)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_CLOSE_LOCALITY, locality, 0, 0, 0, 0, 0,
+			  0, &res);
+	return res.a0;
+}
+
+static inline s64 arm_drtm_get_error(s64 *error_code)
+{
+	struct arm_smccc_res res;
+	s64 status;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_GET_ERROR, 0, 0, 0, 0, 0, 0, 0, &res);
+	status = res.a0;
+	if (status != ARM_DRTM_SUCCESS)
+		return status;
+
+	*error_code = res.a1;
+	return ARM_DRTM_SUCCESS;
+}
+
+static inline s64 arm_drtm_enable_secure_interrupts(void)
+{
+	struct arm_smccc_res res;
+
+	arm_smccc_1_1_smc(ARM_DRTM_SMC_ENABLE_SECURE_INTERRUPTS, 0, 0, 0, 0, 0,
+			  0, 0, &res);
+	return res.a0;
+}
+
+#endif /* !__ASSEMBLY__ */
+#endif /* __ASM_DRTM_H */
Signature · 1 line
2.43.0
#11 lore raw

PATCH v3 7/11 arm64: drtm: Update the linker script for EFI_STUB_DRTM

In reply to Jason Gunthorpe

Implement the link layout that CONFIG_EFI_STUB_DRTM requires. This maps vmlinux into the Dynamic Launch Measured Environment (DLME) defined by the spec.

Broadly, Image fits into the spec defined DLME region memory layout in this order:
- Program headers and padding up to _stext. EFI can write to these while loading the stub so they do not have a stable measurement.
- All the boot-time immutable data, including .text, .data, .init and so on (measured)
- Data written by the EFI stub
- .bss, padding and other 0'd data up to _end
- The "DLME Data" written by the launch process. This is a datastructure the post launch kernel will parse to get trusted information about th launch.

New linker symbols are added to mark these areas and their offsets are placed into the efi_image_info so both stubs can get them.

The spec's design of the DLME was intended to fit a typical OS image like this, with two areas that are unmeasured and an inner measured region.

Linux zeroes the unmeasured area of .bss/etc after the DRTM launch so it also reaches a known value. The DLME data is written by the launch and is trusted. The EFI stub data is either ignored or will have to be sanitized.

Add a section for the unmeasured data before the PECOFF padding. This is an allocated section so it is zero'd in the image and is only 64 bytes in my builds. There is a high chance it gets absorbed into the padding region.

The post-launch kernel will have to reserve the "DLME Data" before starting the allocator since it falls outside the linker map. This will happen in the first series to consume this data.

---
 arch/arm64/kernel/vmlinux.lds.S           | 59 ++++++++++++++++++++++-
 drivers/firmware/efi/libstub/efistub.h    | 20 ++++++++
 drivers/firmware/efi/libstub/image_info.S |  5 ++
 3 files changed, 83 insertions(+), 1 deletion(-)
arch/arm64/kernel/vmlinux.lds.S +58 -1
index 1869b44ae19095..f4e9fd90cd1b12 100644
@@ -190,10 +190,24 @@ SECTIONS
 
 	.head.text : {
 		_text = .;
+#ifdef CONFIG_ARM64_DRTM
+		/* DEN0113 R314010: The DLME region must start at a 4KB aligned address. */
+		ASSERT((_text & (SZ_4K - 1)) == 0,
+		       "DLME region is not 4 KiB aligned");
+#endif
 		HEAD_TEXT
 	}
 	.text : ALIGN(SEGMENT_ALIGN) {	/* Real text segment		*/
 		_stext = .;		/* Text and read-only data	*/
+#ifdef CONFIG_ARM64_DRTM
+		__drtm_measured_start = _stext;
+		/*
+		 * DEN0113 R314020: The DLME image must start at a 4KB aligned
+		 * address. This happens always because SEGMENT_ALIGN is big.
+		 */
+		ASSERT((__drtm_measured_start & (SZ_4K - 1)) == 0,
+		       "DLME measured start is not 4 KiB aligned");
+#endif
 			IRQENTRY_TEXT
 			SOFTIRQENTRY_TEXT
 			ENTRY_TEXT
@@ -352,6 +366,44 @@ SECTIONS
 		__mmuoff_data_end = .;
 	}
 
+	/*
+	 * DRTM layout for DEN0113 as it relates to the link layout, refer
+	 * to Figure 8 in 1.4b. In that language:
+	 *  "Free Space 1" is the PE header and padding between _text and
+	 *        __drtm_measured_start (_stext)
+	 *  "DLME Image" is from __drtm_measured_start to __drtm_measured_end
+	 *  "Free Space 2" is kernel efi data/bss/etc till __drtm_dlme_start
+	 *  "DLME Data" is a memory sized "Minimum Size of DLME data"
+	 *        and is retained after boot for use by the kernel
+	 *
+	 * Thus, the DRTM_PARAMETERS follow from that:
+	 *   DLME Region Address = _text's PA ie the start of Image
+	 *   DLME Region Size = DLME Data Offset + "Minimum Size of DLME data"
+	 *   DLME Image Start Offset = __drtm_measured_start - _text
+	 *   DLME Image Size = __drtm_measured_end - __drtm_measured_start
+	 *   DLME Data Offset = __drtm_dlme_start - _text
+	 *   Normal World DCE region address = PA of __drtm_dlme_start +
+	 *                                  "Minimum Size of DLME data"
+	 *
+	 * The EFI stub will place any optional "Normal World DCE" region
+	 * immediately after the "DLME data". It is only used internally by the
+	 * FW during the launch and has no effect on the link layout.
+	 */
+#ifdef CONFIG_ARM64_DRTM
+	__drtm_measured_end = .;
+
+	/*
+	 * Writable EFI stub state. When DRTM is being used the stub's mutable
+	 * data cannot reside in the normal .init.data because that section
+	 * will be measured.
+	 */
+	.unmeasured.data : {
+		*(.unmeasured.data)
+		*(.init.efidata .init.efidata.*)
+		*(.init.efibss .init.efibss.*)
+	}
+#endif
+
 	PECOFF_EDATA_PADDING
 	__pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin);
 	_edata = .;
@@ -374,10 +426,15 @@ SECTIONS
 
 	. += SZ_4K;		/* stack for the early C runtime */
 	early_init_stack = .;
-
 	. = ALIGN(SEGMENT_ALIGN);
 	__pecoff_data_size = ABSOLUTE(. - __initdata_begin);
 	_end = .;
+#ifdef CONFIG_ARM64_DRTM
+	/* DEN0113 R314030: The DLME data must start at a 4KB aligned address. */
+	__drtm_dlme_start = .;
+	ASSERT((__drtm_dlme_start & (SZ_4K - 1)) == 0,
+	       "DLME data is not 4 KiB aligned")
+#endif
 	__pi__end = .;
 
 	STABS_DEBUG
drivers/firmware/efi/libstub/efistub.h +20 -0
index e404f29693c51c..154d2aefe6505f 100644
@@ -1179,6 +1179,10 @@ struct efi_image_info {
 	s32	_end_offset;
 	s32	entry_offset;
 	s32	sysfb_primary_display_offset;
+#ifdef CONFIG_ARM64_DRTM
+	s32	__drtm_measured_start_offset;
+	s32	__drtm_measured_end_offset;
+#endif
 };
 
 /*
@@ -1223,6 +1227,22 @@ void *efistub_kernel_entry(const struct efi_image_info *image_info)
 	return offset_to_ptr(&image_info->entry_offset);
 }
 
+#ifdef CONFIG_EFI_STUB_DRTM
+static inline unsigned long
+efistub_drtm_measured_offset(const struct efi_image_info *image_info)
+{
+	return image_info->__drtm_measured_start_offset -
+	       image_info->_text_offset;
+}
+
+static inline unsigned long
+efistub_drtm_measured_size(const struct efi_image_info *image_info)
+{
+	return image_info->__drtm_measured_end_offset -
+	       image_info->__drtm_measured_start_offset;
+}
+#endif
+
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 			     const struct efi_image_info *image_info,
 			     char *cmdline_ptr);
drivers/firmware/efi/libstub/image_info.S +5 -0
index 587a3eaff1c363..065bbfdf8fabad 100644
@@ -17,3 +17,8 @@ __efistub_efistub_image_info:
 #else
 	.long	0
 #endif
+
+#ifdef CONFIG_EFI_STUB_DRTM
+	.long	__drtm_measured_start - 0b
+	.long	__drtm_measured_end - 0b
+#endif
Signature · 1 line
2.43.0
#12 lore raw

PATCH v3 8/11 arm64: drtm: Add drtm_entry point to head.S

In reply to Jason Gunthorpe

The DRTM launch defined by DEN0113 cannot use primary_entry because the spec defines a different launch protocol. drtm_entry is a small trampoline that validates the DRTM provided data matches the location expected from the link layout, acquires the DTB through an EFI stub writable handoff struct and then jumps to primary_entry.

Add drivers/firmware/drtm/arm64.c, as later patches and series will need more kernel side functions.

Passing the fdt address like this will require security hardening in future series.

---
 arch/arm64/include/asm/drtm.h             | 20 ++++++
 arch/arm64/kernel/head.S                  | 83 +++++++++++++++++++++++
 arch/arm64/kernel/vmlinux.lds.S           |  6 ++
 drivers/firmware/Makefile                 |  1 +
 drivers/firmware/drtm/Makefile            |  2 +
 drivers/firmware/drtm/arm64.c             | 10 +++
 drivers/firmware/efi/libstub/efistub.h    | 14 ++++
 drivers/firmware/efi/libstub/image_info.S |  2 +
 8 files changed, 138 insertions(+)
 create mode 100644 drivers/firmware/drtm/Makefile
 create mode 100644 drivers/firmware/drtm/arm64.c
arch/arm64/include/asm/drtm.h +20 -0
index 34201bc092bcc6..e74328808466c0 100644
@@ -176,6 +176,10 @@
 #define ARM_DRTM_ALREADY_CLOSED			-11
 #define ARM_DRTM_TPM_ERROR			-12
 
+/* Offsets in struct arm64_drtm_handoff */
+#define ARM64_DRTM_HANDOFF_FDT_ADDR_OFFSET	0
+#define ARM64_DRTM_HANDOFF_ENABLED_OFFSET	8
+
 #ifndef __ASSEMBLY__
 
 #include <linux/bitfield.h>
@@ -301,5 +305,21 @@ static inline s64 arm_drtm_enable_secure_interrupts(void)
 	return res.a0;
 }
 
+/*
+ * Since we cannot pass a parameter through the launch to drtm_entry any
+ * additional data is written by the stub here.
+ */
+struct arm64_drtm_handoff {
+	__le64 fdt_addr;
+	u8 drtm_enabled;
+};
+
+static_assert(offsetof(struct arm64_drtm_handoff, fdt_addr) ==
+	      ARM64_DRTM_HANDOFF_FDT_ADDR_OFFSET);
+static_assert(offsetof(struct arm64_drtm_handoff, drtm_enabled) ==
+	      ARM64_DRTM_HANDOFF_ENABLED_OFFSET);
+
+extern struct arm64_drtm_handoff drtm_handoff;
+
 #endif /* !__ASSEMBLY__ */
 #endif /* __ASM_DRTM_H */
arch/arm64/kernel/head.S +83 -0

101 lines not on this page · raw message

arch/arm64/kernel/vmlinux.lds.S +6 -0
index f4e9fd90cd1b12..a68881189f2313 100644
@@ -380,6 +380,8 @@ SECTIONS
 	 *   DLME Region Address = _text's PA ie the start of Image
 	 *   DLME Region Size = DLME Data Offset + "Minimum Size of DLME data"
 	 *   DLME Image Start Offset = __drtm_measured_start - _text
+	 *   DLME Entry Point Offset = drtm_entry - __drtm_measured_start
+	 *                             (in .init.text)
 	 *   DLME Image Size = __drtm_measured_end - __drtm_measured_start
 	 *   DLME Data Offset = __drtm_dlme_start - _text
 	 *   Normal World DCE region address = PA of __drtm_dlme_start +
@@ -466,6 +468,10 @@ ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE,
 	"HYP init code too big")
 ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
 	"ID map text too big or misaligned")
+#ifdef CONFIG_ARM64_DRTM
+ASSERT(drtm_entry >= __drtm_measured_start && drtm_entry < __drtm_measured_end,
+	"DRTM entry is outside the measured image")
+#endif
 #ifdef CONFIG_HIBERNATION
 ASSERT(__hibernate_exit_text_end - __hibernate_exit_text_start <= SZ_4K,
        "Hibernate exit text is bigger than 4 KiB")
drivers/firmware/Makefile +1 -0
index 50b3383131522d..7f74fd1bf75bda 100644
@@ -30,6 +30,7 @@ obj-y				+= arm_rmm/
 obj-y				+= arm_scmi/
 obj-y				+= broadcom/
 obj-y				+= cirrus/
+obj-y				+= drtm/
 obj-y				+= meson/
 obj-y				+= microchip/
 obj-$(CONFIG_COREBOOT_FIRMWARE)	+= coreboot/
drivers/firmware/drtm/Makefile new file +2 -0
new file mode 100644
index 00000000000000..658798bfd17801
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_ARM64_DRTM) += arm64.o
drivers/firmware/drtm/arm64.c new file +10 -0
new file mode 100644
index 00000000000000..bf736ceb7f0368
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES
+ *
+ * Support functions for ARM DEN 0113 "DRTM Architecture for Arm"
+ */
+#include <linux/efi.h>
+
+#include <asm/drtm.h>
+
+struct arm64_drtm_handoff drtm_handoff __efi_data_handoff;
drivers/firmware/efi/libstub/efistub.h +14 -0
index 154d2aefe6505f..386d65a3943af7 100644
@@ -1182,6 +1182,8 @@ struct efi_image_info {
 #ifdef CONFIG_ARM64_DRTM
 	s32	__drtm_measured_start_offset;
 	s32	__drtm_measured_end_offset;
+	s32	drtm_entry_offset;
+	s32	drtm_handoff_offset;
 #endif
 };
 
@@ -1241,6 +1243,18 @@ efistub_drtm_measured_size(const struct efi_image_info *image_info)
 	return image_info->__drtm_measured_end_offset -
 	       image_info->__drtm_measured_start_offset;
 }
+
+static inline unsigned long
+efistub_drtm_entry_offset(const struct efi_image_info *image_info)
+{
+	return image_info->drtm_entry_offset - image_info->_text_offset;
+}
+
+static inline void *
+efistub_drtm_handoff(const struct efi_image_info *image_info)
+{
+	return offset_to_ptr(&image_info->drtm_handoff_offset);
+}
 #endif
 
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
drivers/firmware/efi/libstub/image_info.S +2 -0
index 065bbfdf8fabad..adf82ad083b354 100644
@@ -21,4 +21,6 @@ __efistub_efistub_image_info:
 #ifdef CONFIG_EFI_STUB_DRTM
 	.long	__drtm_measured_start - 0b
 	.long	__drtm_measured_end - 0b
+	.long	drtm_entry - 0b
+	.long	drtm_handoff - .
 #endif
Signature · 1 line
2.43.0
#13 lore raw

PATCH v3 9/11 arm64: drtm: Call UNPROTECT_MEMORY

In reply to Jason Gunthorpe

The stub always requests ARM_DRTM_LAUNCH_DMA_COMPLETE. On a system with an SMMU the COMPLETE option is sanely implemented using the SMMU global abort register. DMA_COMPLETE should be mostly a no-op. It seems to be designed for the region protection mode that Linux does not use.

However, the FW can still do something interesting under it and we should call it. Choose late_initcall as we know the SMMU drivers are loaded by then. If some platform does actually block DMA beyond the SMMU, then this would result in a noisy failure of built-in driver DMA, not a silent security gap.

The platforms I'm aware of are OK with this placement, and it is a reasonable place to start.

---
 drivers/firmware/drtm/arm64.c | 39 +++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/firmware/drtm/arm64.c +39 -0
index bf736ceb7f0368..07783d55275fd0 100644
@@ -3,8 +3,47 @@
  *
  * Support functions for ARM DEN 0113 "DRTM Architecture for Arm"
  */
+#include <linux/bug.h>
 #include <linux/efi.h>
+#include <linux/init.h>
+#include <linux/printk.h>
 
 #include <asm/drtm.h>
 
 struct arm64_drtm_handoff drtm_handoff __efi_data_handoff;
+
+static int __init arm64_drtm_unprotect_memory(void)
+{
+	s64 status;
+
+	if (!drtm_handoff.drtm_enabled)
+		return 0;
+
+	/*
+	 * Currently Linux can only fully support a DRTM implementation that
+	 * relies only on the SMMU. This should be called directly after
+	 * attaching a driver to every SMMU but before binding any drivers that
+	 * want to use devices attached to the SMMU. Our boot flow does not have
+	 * a way to do that, so for now call it here.
+	 *
+	 * For SMMU based implementations their UNPROTECT_MEMORY is probably a
+	 * NOP since touching the SMMU here is forbidden by spec, but they may
+	 * still do something interesting so be sure to still call it.
+	 *
+	 * The stub excludes the most likely case of non-SMMU by only using
+	 * ARM_DRTM_DMA_PROTECTION_COMPLETE, which is defined as:
+	 *    Complete DMA protection is hardware-based enforcement at the SMMU
+	 *    that blocks all DMA from Non- secure devices."
+	 *
+	 * For now if people have such systems they cannot include built-in
+	 * drivers for any DMA devices, those have to be modules to be ordered
+	 * after this.
+	 */
+	status = arm_drtm_unprotect_memory();
+	WARN(status != ARM_DRTM_SUCCESS,
+	     "DRTM: failed to unprotect memory (x0=%lld)", status);
+	if (status == ARM_DRTM_SUCCESS)
+		pr_info("DRTM: launch completed\n");
+	return 0;
+}
+late_initcall_sync(arm64_drtm_unprotect_memory);
Signature · 1 line
2.43.0
Reviewed-by on v3 9/11
#14 lore raw

Re: [PATCH v3 09/11] arm64: drtm: Call UNPROTECT_MEMORY

Quoted 12 lines · On Fri, 20 Nov 2026 13:52:02 -0400 Jason Gunthorpe <[email protected]> wrote:

The stub always requests ARM_DRTM_LAUNCH_DMA_COMPLETE. On a system with an SMMU the COMPLETE option is sanely implemented using the SMMU global abort register. DMA_COMPLETE should be mostly a no-op. It seems to be designed for the region protection mode that Linux does not use.

However, the FW can still do something interesting under it and we should call it. Choose late_initcall as we know the SMMU drivers are loaded by then. If some platform does actually block DMA beyond the SMMU, then this would result in a noisy failure of built-in driver DMA, not a silent security gap.

Quoted 4 lines

The platforms I'm aware of are OK with this placement, and it is a reasonable place to start.

I didn't comment on 4-8. Whilst they look fine to me, they sit out of my normal playground so I don't feel tagging would be appropriate.

Given the essay below on when this works and when it doesn't this seems as good as it can be for now.

Quoted 56 lines
---
 drivers/firmware/drtm/arm64.c | 39 +++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
drivers/firmware/drtm/arm64.c +39 -0
index bf736ceb7f0368..07783d55275fd0 100644
@@ -3,8 +3,47 @@
  *
  * Support functions for ARM DEN 0113 "DRTM Architecture for Arm"
  */
+#include <linux/bug.h>
 #include <linux/efi.h>
+#include <linux/init.h>
+#include <linux/printk.h>
 
 #include <asm/drtm.h>
 
 struct arm64_drtm_handoff drtm_handoff __efi_data_handoff;
+
+static int __init arm64_drtm_unprotect_memory(void)
+{
+	s64 status;
+
+	if (!drtm_handoff.drtm_enabled)
+		return 0;
+
+	/*
+	 * Currently Linux can only fully support a DRTM implementation that
+	 * relies only on the SMMU. This should be called directly after
+	 * attaching a driver to every SMMU but before binding any drivers that
+	 * want to use devices attached to the SMMU. Our boot flow does not have
+	 * a way to do that, so for now call it here.
+	 *
+	 * For SMMU based implementations their UNPROTECT_MEMORY is probably a
+	 * NOP since touching the SMMU here is forbidden by spec, but they may
+	 * still do something interesting so be sure to still call it.
+	 *
+	 * The stub excludes the most likely case of non-SMMU by only using
+	 * ARM_DRTM_DMA_PROTECTION_COMPLETE, which is defined as:
+	 *    Complete DMA protection is hardware-based enforcement at the SMMU
+	 *    that blocks all DMA from Non- secure devices."
+	 *
+	 * For now if people have such systems they cannot include built-in
+	 * drivers for any DMA devices, those have to be modules to be ordered
+	 * after this.
+	 */
+	status = arm_drtm_unprotect_memory();
+	WARN(status != ARM_DRTM_SUCCESS,
+	     "DRTM: failed to unprotect memory (x0=%lld)", status);
+	if (status == ARM_DRTM_SUCCESS)
+		pr_info("DRTM: launch completed\n");
+	return 0;
+}
+late_initcall_sync(arm64_drtm_unprotect_memory);
#15 lore raw

PATCH v3 10/11 efi/arm64: Add a way to detect if SMCC 1.1 can be used

In reply to Jason Gunthorpe

DRTM is defined over SMCs, but if the stub blindly issues an SMC the machine might crash. To do this safely and automatically, the stub has to extract the ACPI_FADT_PSCI_COMPLIANT (without HVC) from the ACPI table, then check with PSCI if SMCC 1.1 is supported

Implement a really basic ACPI walk down to the FACP and pull it out. Be strict about parsing the ACPI. Put it in the drtm file for now as it is the only expected user.

Follow the overall design in the kernel and finish up with trying to call ARM_SMCCC_VERSION_FUNC_ID like the kernel does.

---
 drivers/firmware/efi/libstub/Makefile     |   1 +
 drivers/firmware/efi/libstub/arm64-smcc.c | 212 ++++++++++++++++++++++
 drivers/firmware/efi/libstub/arm64-stub.h |   3 +
 3 files changed, 216 insertions(+)
 create mode 100644 drivers/firmware/efi/libstub/arm64-smcc.c
 create mode 100644 drivers/firmware/efi/libstub/arm64-stub.h
drivers/firmware/efi/libstub/Makefile +1 -0
index 207c8f99c5e299..2ae86429315e0e 100644
@@ -85,6 +85,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o \
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= kaslr.o arm64.o arm64-stub.o smbios.o
+lib-$(CONFIG_ARM64_DRTM)	+= arm64-smcc.o
 lib-$(CONFIG_X86)		+= x86-stub.o smbios.o
 lib-$(CONFIG_X86_64)		+= x86-5lvl.o
 lib-$(CONFIG_RISCV)		+= kaslr.o riscv.o riscv-stub.o
drivers/firmware/efi/libstub/arm64-smcc.c new file +212 -0

212 lines not on this page · raw message

drivers/firmware/efi/libstub/arm64-stub.h new file +3 -0
new file mode 100644
index 00000000000000..f55e56ecd305bd
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+bool efi_arm64_smccc_1_1(void);
Signature · 1 line
2.43.0
Reviewed-by on v3 10/11
#16 lore raw

Re: [PATCH v3 10/11] efi/arm64: Add a way to detect if SMCC 1.1 can be used

Quoted 9 lines · On Fri, 20 Nov 2026 13:52:03 -0400 Jason Gunthorpe <[email protected]> wrote:

DRTM is defined over SMCs, but if the stub blindly issues an SMC the machine might crash. To do this safely and automatically, the stub has to extract the ACPI_FADT_PSCI_COMPLIANT (without HVC) from the ACPI table, then check with PSCI if SMCC 1.1 is supported

Implement a really basic ACPI walk down to the FACP and pull it out. Be strict about parsing the ACPI. Put it in the drtm file for now as it is the only expected user.

Quoted 5 lines

Follow the overall design in the kernel and finish up with trying to call ARM_SMCCC_VERSION_FUNC_ID like the kernel does.

---

Few trivial things.

Quoted 37 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +31 -0
new file mode 100644
index 00000000000000..a722522e1af85e
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES
+ *
+ * Determine if ARM64 can use SMCC 1.1 calls.
+ */
+#include <linux/acpi.h>
+
+#include <uapi/linux/psci.h>
+
+#include "efistub.h"
+#include "arm64-stub.h"
+
+static bool efi_acpi_checksum_valid(const void *table, u32 length)
+{
+	const u8 *p = table;
+	u8 checksum = 0;
+
+	/*
+	 * Nothing this code walks is remotely close to this large.  The length
+	 * fields are firmware supplied and are used to bound checksum loops, so
+	 * cap them to avoid checksumming forever if one is corrupt.
+	 */
+	if (length > SZ_1M)
+		return false;
+
+	while (length--)
+		checksum += *p++;
+
+	return checksum == 0;
+}
+

drivers/firmware/efi/libstub/arm64-smcc.c line 41

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +10 -0
+static const struct acpi_table_fadt *
+efi_acpi_find_fadt(const struct acpi_table_xsdt *xsdt)
+{
+	u32 nr_entries;
+	u32 i;
+
+	nr_entries = (xsdt->header.length -
+		      offsetof(struct acpi_table_xsdt, table_offset_entry)) /
+		     sizeof(xsdt->table_offset_entry[0]);
+	for (i = 0; i < nr_entries; i++) {

Might as well do

	for (u32 i = 0; 
Quoted 17 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +16 -0
+		u64 address = xsdt->table_offset_entry[i];
+		const struct acpi_table_header *table;
+
+		if (!address)
+			continue;
+
+		table = (const void *)address;
+		if (!memcmp(table->signature, ACPI_SIG_FADT,
+			    sizeof(table->signature)))
+			return (const void *)table;
+	}
+
+	return NULL;
+}
 
+static bool efi_arm64_smc_conduit_available(void)
+{

drivers/firmware/efi/libstub/arm64-smcc.c line 79

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +10 -0
+	const struct acpi_table_fadt *fadt;
+	const struct acpi_table_xsdt *xsdt;
+	const struct acpi_table_rsdp *rsdp;
+	u32 length;
+	u16 flags;
+
+	rsdp = get_efi_config_table(ACPI_20_TABLE_GUID);
+	if (!rsdp) {
+		efi_debug(
+			"DRTM: ACPI 2.0 RSDP is unavailable; SMC conduit is unknown\n");

It is long anyway, so definitely go longer and avoid that ugliness!

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +10 -0
+		return false;
+	}
+
+	if (memcmp(rsdp->signature, ACPI_SIG_RSDP, sizeof(rsdp->signature))) {
+		efi_err("DRTM: ACPI RSDP signature is invalid\n");
+		return false;
+	}
+
+	if (rsdp->revision < 2) {
+		efi_err("DRTM: ACPI RSDP revision does not provide an XSDT\n");

drivers/firmware/efi/libstub/arm64-smcc.c line 99

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +10 -0
+		return false;
+	}
+
+	length = rsdp->length;
+	if (length < sizeof(*rsdp)) {
+		efi_err("DRTM: ACPI RSDP length is invalid\n");
+		return false;
+	}
+	if (!efi_acpi_checksum_valid(rsdp, offsetof(struct acpi_table_rsdp,
+						    length))) {

Could use acpi_rsdp_common instead of offset in the combined structure. Not sure it is much clearer though.

Quoted 3 lines
drivers/firmware/efi/libstub/arm64-smcc.c new file +3 -0
+		efi_err("DRTM: ACPI RSDP legacy checksum is invalid\n");
+		return false;
+	}
#17 lore raw

PATCH v3 11/11 efi/arm64: Implement ARM64 DRTM in the stub

In reply to Jason Gunthorpe

Provide an implementation of the CONFIG_EFI_STUB_DRTM protocol for ARM64 DEN0113 >= v1.1.

First, it queries the FW for support and collects all the information. This is needed to compute the extra_size, which comes from FW reporting how much memory it needs during the launch for the DLME Data and DCE-owned data. The generic stub ensures there is trailing memory after Image for this.

Then the DRTM_PARAMETERS launch structure is computed using the offsets in the efi_info.

Finally, the stub does ExitBootServices and calls the actual launch.

The feature discovery process is deliberately fairly verbose to help debug any FW weirdness in the field. Enable it with efi=debug

It looks something like:

EFI stub: DRTM: interface version 1.4
EFI stub: DEBUG: DRTM: TPM algorithm 0xc, TPM hashing unavailable, PCR schemas 0x1 EFI stub: DEBUG: DRTM: minimum DLME data 73728 bytes, Normal-world DCE 0 bytes EFI stub: Decompressing Linux Kernel...
EFI stub: Generating empty DTB
EFI stub: Exiting boot services...
EFI stub: DEBUG: DRTM: will launch, selected launch features 0x0

 [    0.000000] Booting Linux on physical CPU 0x0000000000 [0x000f0510]

Eventually the launch'd kernel is going to require built in crypto libraries that match what the FW is using so it can validate some of the information left behind during early boot. Refuse to launch if these are not built in and build in the most common ones from the ARM64_DRTM kconfig.

---
 arch/arm64/Kconfig                        |  16 +
 drivers/firmware/efi/libstub/Makefile     |   2 +-
 drivers/firmware/efi/libstub/arm64-drtm.c | 383 ++++++++++++++++++++++
 3 files changed, 400 insertions(+), 1 deletion(-)
 create mode 100644 drivers/firmware/efi/libstub/arm64-drtm.c
arch/arm64/Kconfig +16 -0
index 9b08b2457a555f..412fc29f4e1cf0 100644
@@ -2622,6 +2622,22 @@ config EFI
 	  allow the kernel to be booted as an EFI application. This
 	  is only useful on systems that have UEFI firmware.
 
+config ARM64_DRTM
+	bool "Arm Dynamic Root of Trust for Measurement support"
+	depends on EFI
+	select CRYPTO
+	select CRYPTO_SHA256
+	select CRYPTO_SHA512
+	select EFI_STUB_DRTM
+	help
+	  Support launching Linux as a Dynamically Launched Measured
+	  Environment (DLME) using Arm's DEN0113 DRTM specification. Requires
+	  booting through the EFI stub. The drtm= kernel command line option
+	  enables or disables the launch at boot.
+
+	  If unsure, say N. This option is EXPERIMENTAL and does not yet provide
+	  security.
+
 config COMPRESSED_INSTALL
 	bool "Install compressed image by default"
 	help
drivers/firmware/efi/libstub/Makefile +1 -1
index 2ae86429315e0e..125159d8136f33 100644
@@ -85,7 +85,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o \
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= kaslr.o arm64.o arm64-stub.o smbios.o
-lib-$(CONFIG_ARM64_DRTM)	+= arm64-smcc.o
+lib-$(CONFIG_ARM64_DRTM)	+= arm64-smcc.o arm64-drtm.o
 lib-$(CONFIG_X86)		+= x86-stub.o smbios.o
 lib-$(CONFIG_X86_64)		+= x86-5lvl.o
 lib-$(CONFIG_RISCV)		+= kaslr.o riscv.o riscv-stub.o
drivers/firmware/efi/libstub/arm64-drtm.c new file +383 -0

383 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v3 11/11
#18 lore raw

Re: [PATCH v3 11/11] efi/arm64: Implement ARM64 DRTM in the stub

Quoted 28 lines · On Fri, 20 Nov 2026 13:52:04 -0400 Jason Gunthorpe <[email protected]> wrote:

Provide an implementation of the CONFIG_EFI_STUB_DRTM protocol for ARM64 DEN0113 >= v1.1.

First, it queries the FW for support and collects all the information. This is needed to compute the extra_size, which comes from FW reporting how much memory it needs during the launch for the DLME Data and DCE-owned data. The generic stub ensures there is trailing memory after Image for this.

Then the DRTM_PARAMETERS launch structure is computed using the offsets in the efi_info.

Finally, the stub does ExitBootServices and calls the actual launch.

The feature discovery process is deliberately fairly verbose to help debug any FW weirdness in the field. Enable it with efi=debug

It looks something like:

EFI stub: DRTM: interface version 1.4
EFI stub: DEBUG: DRTM: TPM algorithm 0xc, TPM hashing unavailable, PCR schemas 0x1 EFI stub: DEBUG: DRTM: minimum DLME data 73728 bytes, Normal-world DCE 0 bytes EFI stub: Decompressing Linux Kernel...
EFI stub: Generating empty DTB
EFI stub: Exiting boot services...
EFI stub: DEBUG: DRTM: will launch, selected launch features 0x0

 [    0.000000] Booting Linux on physical CPU 0x0000000000 [0x000f0510]
Quoted 7 lines

Eventually the launch'd kernel is going to require built in crypto libraries that match what the FW is using so it can validate some of the information left behind during early boot. Refuse to launch if these are not built in and build in the most common ones from the ARM64_DRTM kconfig.

Naturally I focused on the important stuff - see below.

drivers/firmware/efi/libstub/arm64-drtm.c line 271

Quoted 6 lines
drivers/firmware/efi/libstub/arm64-drtm.c new file +6 -0
+
+efi_status_t efi_drtm_prepare_launch(const struct efi_image_info *image_info,
+				     unsigned long fdt_addr)
+{
+	struct arm64_drtm_handoff *handoff =
+		efistub_drtm_handoff(image_info);

That is under 80 chars on one line.

Quoted 2 lines
drivers/firmware/efi/libstub/arm64-drtm.c new file +2 -0
+	struct arm_drtm_parameters *params;
+	unsigned long measured_offset;
#19 lore raw
In reply to Jason Gunthorpe
Quoted 4 lines · Jason Gunthorpe

This is the first series for an implementation of ARM's DEN 0113 v1.4b Dynamic Root of Trust for Measurement (DRTM) launch protocol. It is built on some generic changes to the EFI stubs that provide for a simple, direct DRTM launch directly into vmlinux.

Ard, I saw you picked up the efi_info series to your tree, thanks! Are you OK with the remaining EFI parts in this?

Quoted 4 lines

Jason Gunthorpe (11):
efi/libstub: Add generic arch callbacks for DRTM efi/libstub: Use free_addr/size as the arg to handle_kernel_image efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size

Quoted 8 lines

efi: Add a __efi_data_handoff section annotation efi/libstub: Put the stub's writable data in unique sections for DRTM
arm64: drtm: Add macro definitions for DEN0113
arm64: drtm: Update the linker script for EFI_STUB_DRTM
arm64: drtm: Add drtm_entry point to head.S
arm64: drtm: Call UNPROTECT_MEMORY
efi/arm64: Add a way to detect if SMCC 1.1 can be used efi/arm64: Implement ARM64 DRTM in the stub

?

Thanks,
Jason