Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/11] arm64: DRTM, boot portion
@ 2026-11-20 17:51 Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 01/11] efi/libstub: Add generic arch callbacks for DRTM Jason Gunthorpe
                   ` (10 more replies)
  0 siblings, 11 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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/20261016165135.GA494690@nvidia.com/

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/20261016144121.GA439341@nvidia.com/

 - 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/20261118182105.1633603-21-ardb+git@google.com
 - 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/0-v2-989a18390eb1+486-arm64_drtm_jgg@nvidia.com
 - 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/0-v1-27d06b313981+8b-arm64_drtm_jgg@nvidia.com

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
-- 
2.43.0



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3 01/11] efi/libstub: Add generic arch callbacks for DRTM
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
@ 2026-11-20 17:51 ` Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image Jason Gunthorpe
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 .../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(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd944..4c95e36dd8c94b 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -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]
diff --git a/drivers/firmware/efi/Kconfig b/drivers/firmware/efi/Kconfig
index 29e0729299f5bd..6806bcd6a52420 100644
--- a/drivers/firmware/efi/Kconfig
+++ b/drivers/firmware/efi/Kconfig
@@ -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
diff --git a/drivers/firmware/efi/libstub/efi-stub-entry.c b/drivers/firmware/efi/libstub/efi-stub-entry.c
index dcf4442a16b078..fd272d07737371 100644
--- a/drivers/firmware/efi/libstub/efi-stub-entry.c
+++ b/drivers/firmware/efi/libstub/efi-stub-entry.c
@@ -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,
diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c
index e50deec9968658..2840b589653fe8 100644
--- a/drivers/firmware/efi/libstub/efi-stub-helper.c
+++ b/drivers/firmware/efi/libstub/efi-stub-helper.c
@@ -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");
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index df3f88f4786b82..80b9558f9f689a 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -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,
diff --git a/drivers/firmware/efi/libstub/fdt.c b/drivers/firmware/efi/libstub/fdt.c
index 09e9e5bc831e8b..d39455d38a911a 100644
--- a/drivers/firmware/efi/libstub/fdt.c
+++ b/drivers/firmware/efi/libstub/fdt.c
@@ -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 */
 }
diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c
index 4089a29068caae..69962a8f094caf 100644
--- a/drivers/firmware/efi/libstub/zboot.c
+++ b/drivers/firmware/efi/libstub/zboot.c
@@ -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;
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 01/11] efi/libstub: Add generic arch callbacks for DRTM Jason Gunthorpe
@ 2026-11-20 17:51 ` Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size Jason Gunthorpe
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 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(-)

diff --git a/drivers/firmware/efi/libstub/arm32-stub.c b/drivers/firmware/efi/libstub/arm32-stub.c
index 89e48ac0b7cbbe..b4c843e7f6b8fd 100644
--- a/drivers/firmware/efi/libstub/arm32-stub.c
+++ b/drivers/firmware/efi/libstub/arm32-stub.c
@@ -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;
 }
diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c
index 351fcb4493e19b..e0adc4de0beac3 100644
--- a/drivers/firmware/efi/libstub/arm64-stub.c
+++ b/drivers/firmware/efi/libstub/arm64-stub.c
@@ -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));
 }
 
diff --git a/drivers/firmware/efi/libstub/efi-stub-entry.c b/drivers/firmware/efi/libstub/efi-stub-entry.c
index fd272d07737371..7febed273c8c08 100644
--- a/drivers/firmware/efi/libstub/efi-stub-entry.c
+++ b/drivers/firmware/efi/libstub/efi-stub-entry.c
@@ -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;
 }
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 80b9558f9f689a..e404f29693c51c 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -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);
diff --git a/drivers/firmware/efi/libstub/kaslr.c b/drivers/firmware/efi/libstub/kaslr.c
index 7cf68a6123946a..2f1f3e19583def 100644
--- a/drivers/firmware/efi/libstub/kaslr.c
+++ b/drivers/firmware/efi/libstub/kaslr.c
@@ -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,
+				  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;
+
+	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;
-	}
-
-	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) {
+			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,
+					    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;
+	efi_kaslr_copy_kernel(image_addr, free_addr, free_size, image_info);
+	return EFI_SUCCESS;
 }
diff --git a/drivers/firmware/efi/libstub/loongarch-stub.c b/drivers/firmware/efi/libstub/loongarch-stub.c
index df6749643bb8bd..d8886b9c74099d 100644
--- a/drivers/firmware/efi/libstub/loongarch-stub.c
+++ b/drivers/firmware/efi/libstub/loongarch-stub.c
@@ -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;
 }
diff --git a/drivers/firmware/efi/libstub/riscv-stub.c b/drivers/firmware/efi/libstub/riscv-stub.c
index 8cd0cd9b00486c..45a345c1701ffa 100644
--- a/drivers/firmware/efi/libstub/riscv-stub.c
+++ b/drivers/firmware/efi/libstub/riscv-stub.c
@@ -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");
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 01/11] efi/libstub: Add generic arch callbacks for DRTM Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image Jason Gunthorpe
@ 2026-11-20 17:51 ` Jason Gunthorpe
  2026-11-20 23:33   ` Ard Biesheuvel
  2026-11-20 17:51 ` [PATCH v3 04/11] efi: Add a __efi_data_handoff section annotation Jason Gunthorpe
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/firmware/efi/libstub/kaslr.c | 77 ++++++++++++++++++++++------
 1 file changed, 61 insertions(+), 16 deletions(-)

diff --git a/drivers/firmware/efi/libstub/kaslr.c b/drivers/firmware/efi/libstub/kaslr.c
index 2f1f3e19583def..f50c36a2fd635e 100644
--- a/drivers/firmware/efi/libstub/kaslr.c
+++ b/drivers/firmware/efi/libstub/kaslr.c
@@ -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;
 }
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 04/11] efi: Add a __efi_data_handoff section annotation
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (2 preceding siblings ...)
  2026-11-20 17:51 ` [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size Jason Gunthorpe
@ 2026-11-20 17:51 ` Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 05/11] efi/libstub: Put the stub's writable data in unique sections for DRTM Jason Gunthorpe
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/firmware/efi/efi-init.c |  2 +-
 include/linux/efi.h             | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/efi-init.c b/drivers/firmware/efi/efi-init.c
index 25a5497ebb2ea1..a6ce8b7897967e 100644
--- a/drivers/firmware/efi/efi-init.c
+++ b/drivers/firmware/efi/efi-init.c
@@ -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
 
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 4a809c6ae7241d..f79c5669a356cb 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -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)))
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 05/11] efi/libstub: Put the stub's writable data in unique sections for DRTM
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (3 preceding siblings ...)
  2026-11-20 17:51 ` [PATCH v3 04/11] efi: Add a __efi_data_handoff section annotation Jason Gunthorpe
@ 2026-11-20 17:51 ` Jason Gunthorpe
  2026-11-20 17:51 ` [PATCH v3 06/11] arm64: drtm: Add macro definitions for DEN0113 Jason Gunthorpe
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/firmware/efi/libstub/Makefile  | 10 ++++++++++
 drivers/firmware/efi/libstub/zboot.lds |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index 0c5fbc21d2a297..207c8f99c5e299 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -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.
diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
index e0f0bf00585487..34dfe3c2e77d03 100644
--- a/drivers/firmware/efi/libstub/zboot.lds
+++ b/drivers/firmware/efi/libstub/zboot.lds
@@ -36,7 +36,7 @@ SECTIONS
 
 	.data : ALIGN(4096) {
 		_data = .;
-		*(.data* .init.data*)
+		*(.data* .init.data* .init.efidata* .init.efibss*)
 		_edata = ALIGN(512);
 		. = _edata;
 	}
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 06/11] arm64: drtm: Add macro definitions for DEN0113
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (4 preceding siblings ...)
  2026-11-20 17:51 ` [PATCH v3 05/11] efi/libstub: Put the stub's writable data in unique sections for DRTM Jason Gunthorpe
@ 2026-11-20 17:51 ` Jason Gunthorpe
  2026-11-20 17:52 ` [PATCH v3 07/11] arm64: drtm: Update the linker script for EFI_STUB_DRTM Jason Gunthorpe
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:51 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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

Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 arch/arm64/include/asm/drtm.h | 305 ++++++++++++++++++++++++++++++++++
 1 file changed, 305 insertions(+)
 create mode 100644 arch/arm64/include/asm/drtm.h

diff --git a/arch/arm64/include/asm/drtm.h b/arch/arm64/include/asm/drtm.h
new file mode 100644
index 00000000000000..34201bc092bcc6
--- /dev/null
+++ b/arch/arm64/include/asm/drtm.h
@@ -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 */
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 07/11] arm64: drtm: Update the linker script for EFI_STUB_DRTM
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (5 preceding siblings ...)
  2026-11-20 17:51 ` [PATCH v3 06/11] arm64: drtm: Add macro definitions for DEN0113 Jason Gunthorpe
@ 2026-11-20 17:52 ` Jason Gunthorpe
  2026-11-20 17:52 ` [PATCH v3 08/11] arm64: drtm: Add drtm_entry point to head.S Jason Gunthorpe
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:52 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 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(-)

diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 1869b44ae19095..f4e9fd90cd1b12 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -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
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index e404f29693c51c..154d2aefe6505f 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -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);
diff --git a/drivers/firmware/efi/libstub/image_info.S b/drivers/firmware/efi/libstub/image_info.S
index 587a3eaff1c363..065bbfdf8fabad 100644
--- a/drivers/firmware/efi/libstub/image_info.S
+++ b/drivers/firmware/efi/libstub/image_info.S
@@ -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
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 08/11] arm64: drtm: Add drtm_entry point to head.S
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (6 preceding siblings ...)
  2026-11-20 17:52 ` [PATCH v3 07/11] arm64: drtm: Update the linker script for EFI_STUB_DRTM Jason Gunthorpe
@ 2026-11-20 17:52 ` Jason Gunthorpe
  2026-11-20 17:52 ` [PATCH v3 09/11] arm64: drtm: Call UNPROTECT_MEMORY Jason Gunthorpe
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:52 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 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

diff --git a/arch/arm64/include/asm/drtm.h b/arch/arm64/include/asm/drtm.h
index 34201bc092bcc6..e74328808466c0 100644
--- a/arch/arm64/include/asm/drtm.h
+++ b/arch/arm64/include/asm/drtm.h
@@ -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 */
diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S
index d6ba3c0af81190..dbc8a7cc0938f4 100644
--- a/arch/arm64/kernel/head.S
+++ b/arch/arm64/kernel/head.S
@@ -21,6 +21,7 @@
 #include <asm/asm-offsets.h>
 #include <asm/cache.h>
 #include <asm/cputype.h>
+#include <asm/drtm.h>
 #include <asm/el2_setup.h>
 #include <asm/elf.h>
 #include <asm/image.h>
@@ -35,6 +36,7 @@
 #include <asm/stacktrace/frame.h>
 #include <asm/thread_info.h>
 #include <asm/virt.h>
+#include <uapi/linux/psci.h>
 
 #include "efi-header.S"
 
@@ -131,6 +133,87 @@ SYM_CODE_START(primary_entry)
 SYM_CODE_END(primary_entry)
 
 	__INIT
+#ifdef CONFIG_ARM64_DRTM
+/*
+ * Entry point used by a DRTM launch. Verify that information provided by the
+ * EFI stub matches expectations. We rely on an untrusted-write/trusted-verify
+ * approach so this doesn't have to deal with writing to cache-off memory. Per
+ * DEN0113 R45380, the launch supplies:
+ *
+ *   x0 = physical base of the DLME region
+ *   x1 = offset of thee DLME Data, which is several tables written by
+ *        the launch to pass trusted information to the post launch kernel
+ *
+ * These values must match the link layout in vmlinux.lds.S or the launch is
+ * invalid. The EFI stub will get them from the efi_image_info and pass them
+ * into arm_drtm_parameters.
+ *
+ * DEN0113 R45450-R45470 specify the remaining entry state as the PSCI CPU_ON
+ * initial state: AArch64 at the highest Non-secure EL, little-endian, with the
+ * MMU and D-cache disabled and Non-secure asynchronous/debug exceptions
+ * masked.
+ *
+ * Per DEN0113 R45220 the launch cleans and invalidates the whole "DLME region"
+ * which is everything from _text till past __drtm_dlme_start before
+ * it measures the image. Thus the handoff data written into the region by the
+ * EFI stub is safely readable here with the MMU off.
+ */
+SYM_CODE_START(drtm_entry)
+	/* Verify that x0 is the runtime physical address of _text */
+	adr_l	x2, _text
+	cmp	x0, x2
+	b.ne	.Ldrtm_bad_entry
+
+	/* x0 + x1 must be the runtime physical start of the DLME data */
+	add	x3, x0, x1
+	adr_l	x4, __drtm_dlme_start
+	cmp	x3, x4
+	b.ne	.Ldrtm_bad_entry
+
+	/* Stub must set drtm_enabled */
+	adr_l	x4, drtm_handoff
+	ldrb	w2, [x4, #ARM64_DRTM_HANDOFF_ENABLED_OFFSET]
+	cbz	x2, .Ldrtm_bad_entry
+
+	/* primary_entry(handoff->fdt_addr, 0, 0, 0) */
+	ldr	x0, [x4, #ARM64_DRTM_HANDOFF_FDT_ADDR_OFFSET]
+	mov	x1, xzr
+	mov	x2, xzr
+	mov	x3, xzr
+	b	primary_entry
+
+	/*
+	 * Can't continue, launch is corrupted, spec says the DLME can call
+	 * SET_ERROR which should forward the error to the next boot for
+	 * display.
+	 */
+.Ldrtm_bad_entry:
+	ldr	w0, =ARM_DRTM_SMC_SET_ERROR
+	mov	x1, #ARM_DRTM_MAKE_ERROR(ARM_DRTM_ERROR_PHASE_DLME,
+					 ARM_DRTM_ERROR_ID_VENDOR, 0)
+	mov	x2, xzr
+	mov	x3, xzr
+	mov	x4, xzr
+	mov	x5, xzr
+	mov	x6, xzr
+	mov	x7, xzr
+	smc	#0
+	/* Cold-reset the machine as required after reporting a DLME error */
+	ldr	w0, =PSCI_0_2_FN_SYSTEM_RESET
+	mov	x1, xzr
+	mov	x2, xzr
+	mov	x3, xzr
+	mov	x4, xzr
+	mov	x5, xzr
+	mov	x6, xzr
+	mov	x7, xzr
+	smc	#0
+	/* FW is really broken, just hang. */
+1:	wfe
+	b	1b
+SYM_CODE_END(drtm_entry)
+#endif
+
 SYM_CODE_START_LOCAL(record_mmu_state)
 	mrs	x19, CurrentEL
 	cmp	x19, #CurrentEL_EL2
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index f4e9fd90cd1b12..a68881189f2313 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -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")
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile
index 50b3383131522d..7f74fd1bf75bda 100644
--- a/drivers/firmware/Makefile
+++ b/drivers/firmware/Makefile
@@ -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/
diff --git a/drivers/firmware/drtm/Makefile b/drivers/firmware/drtm/Makefile
new file mode 100644
index 00000000000000..658798bfd17801
--- /dev/null
+++ b/drivers/firmware/drtm/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_ARM64_DRTM) += arm64.o
diff --git a/drivers/firmware/drtm/arm64.c b/drivers/firmware/drtm/arm64.c
new file mode 100644
index 00000000000000..bf736ceb7f0368
--- /dev/null
+++ b/drivers/firmware/drtm/arm64.c
@@ -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;
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index 154d2aefe6505f..386d65a3943af7 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -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,
diff --git a/drivers/firmware/efi/libstub/image_info.S b/drivers/firmware/efi/libstub/image_info.S
index 065bbfdf8fabad..adf82ad083b354 100644
--- a/drivers/firmware/efi/libstub/image_info.S
+++ b/drivers/firmware/efi/libstub/image_info.S
@@ -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
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 09/11] arm64: drtm: Call UNPROTECT_MEMORY
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (7 preceding siblings ...)
  2026-11-20 17:52 ` [PATCH v3 08/11] arm64: drtm: Add drtm_entry point to head.S Jason Gunthorpe
@ 2026-11-20 17:52 ` Jason Gunthorpe
  2026-11-20 17:52 ` [PATCH v3 10/11] efi/arm64: Add a way to detect if SMCC 1.1 can be used Jason Gunthorpe
  2026-11-20 17:52 ` [PATCH v3 11/11] efi/arm64: Implement ARM64 DRTM in the stub Jason Gunthorpe
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:52 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/firmware/drtm/arm64.c | 39 +++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/drivers/firmware/drtm/arm64.c b/drivers/firmware/drtm/arm64.c
index bf736ceb7f0368..07783d55275fd0 100644
--- a/drivers/firmware/drtm/arm64.c
+++ b/drivers/firmware/drtm/arm64.c
@@ -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);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 10/11] efi/arm64: Add a way to detect if SMCC 1.1 can be used
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (8 preceding siblings ...)
  2026-11-20 17:52 ` [PATCH v3 09/11] arm64: drtm: Call UNPROTECT_MEMORY Jason Gunthorpe
@ 2026-11-20 17:52 ` Jason Gunthorpe
  2026-11-20 17:52 ` [PATCH v3 11/11] efi/arm64: Implement ARM64 DRTM in the stub Jason Gunthorpe
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:52 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 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

diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index 207c8f99c5e299..2ae86429315e0e 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -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
diff --git a/drivers/firmware/efi/libstub/arm64-smcc.c b/drivers/firmware/efi/libstub/arm64-smcc.c
new file mode 100644
index 00000000000000..a722522e1af85e
--- /dev/null
+++ b/drivers/firmware/efi/libstub/arm64-smcc.c
@@ -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;
+}
+
+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++) {
+		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;
+}
+
+/**
+ * efi_arm64_smc_conduit_available() - Discover an advertised SMC conduit
+ *
+ * Very lightly parse the ACPI tables to reach FADT. The goal is to parse things
+ * that are valid and fail off for odd looking stuff. The checksums are used to
+ * double check the limited parsing hasn't gone off the rails.
+ *
+ * Return: true only if a valid ACPI 2.0-or-newer table hierarchy contains a
+ * FADT advertising PSCI over SMC.  This establishes that issuing an SMCCC SMC
+ * is permitted
+ */
+static bool efi_arm64_smc_conduit_available(void)
+{
+	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");
+		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");
+		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))) {
+		efi_err("DRTM: ACPI RSDP legacy checksum is invalid\n");
+		return false;
+	}
+
+	if (!efi_acpi_checksum_valid(rsdp, length)) {
+		efi_err("DRTM: ACPI RSDP extended checksum is invalid\n");
+		return false;
+	}
+
+	xsdt = (const void *)rsdp->xsdt_physical_address;
+	if (!xsdt) {
+		efi_err("DRTM: ACPI RSDP does not provide an XSDT\n");
+		return false;
+	}
+
+	if (memcmp(xsdt->header.signature, ACPI_SIG_XSDT,
+		   sizeof(xsdt->header.signature))) {
+		efi_err("DRTM: ACPI XSDT signature is invalid\n");
+		return false;
+	}
+
+	length = xsdt->header.length;
+	if (length < offsetof(struct acpi_table_xsdt, table_offset_entry) ||
+	    (length - offsetof(struct acpi_table_xsdt, table_offset_entry)) %
+		    sizeof(xsdt->table_offset_entry[0])) {
+		efi_err("DRTM: ACPI XSDT length is invalid\n");
+		return false;
+	}
+	if (!efi_acpi_checksum_valid(xsdt, length)) {
+		efi_err("DRTM: ACPI XSDT checksum is invalid\n");
+		return false;
+	}
+
+	fadt = efi_acpi_find_fadt(xsdt);
+	if (!fadt) {
+		efi_err("DRTM: ACPI XSDT does not contain a FADT\n");
+		return false;
+	}
+
+	length = fadt->header.length;
+
+	/*
+	 * No FADT revision check is needed, the Arm boot flags land on bytes
+	 * that every pre-5.1 revision defines as reserved and zero, which
+	 * simply reads back as 'no PSCI'.
+	 */
+	if (length < offsetofend(struct acpi_table_fadt, arm_boot_flags)) {
+		efi_debug("DRTM: ACPI FADT does not contain Arm boot flags\n");
+		return false;
+	}
+	if (!efi_acpi_checksum_valid(fadt, length)) {
+		efi_err("DRTM: ACPI FADT checksum is invalid\n");
+		return false;
+	}
+
+	flags = fadt->arm_boot_flags;
+	if (!(flags & ACPI_FADT_PSCI_COMPLIANT)) {
+		efi_debug("DRTM: ACPI FADT does not advertise PSCI\n");
+		return false;
+	}
+
+	if (flags & ACPI_FADT_PSCI_USE_HVC) {
+		efi_debug(
+			"DRTM: ACPI FADT advertises PSCI over HVC, not SMC\n");
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * Discovery must tolerate SMCCC 1.0 clobbering x0-x17. The EFI stub cannot
+ * call the kernel's out-of-line SMCCC helper, so issue these calls inline.
+ */
+static u32 efi_arm64_smc_1_0(u32 function, u32 arg)
+{
+	register unsigned long x0 asm("x0") = function;
+	register unsigned long x1 asm("x1") = arg;
+
+	asm volatile("smc #0"
+		     : "+r" (x0), "+r" (x1)
+		     :
+		     : "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9",
+		       "x10", "x11", "x12", "x13", "x14", "x15", "x16",
+		       "x17", "cc", "memory");
+
+	return x0;
+}
+
+/* True if SMCCC 1.1 or later in the 1.x series is available. */
+bool efi_arm64_smccc_1_1(void)
+{
+	u32 version;
+	s32 status;
+
+	if (!efi_arm64_smc_conduit_available())
+		return false;
+
+	version = efi_arm64_smc_1_0(PSCI_0_2_FN_PSCI_VERSION, 0);
+	if ((s32)version < 0)
+		return false;
+
+	if (PSCI_VERSION_MAJOR(version) < 1)
+		return false;
+
+	status = efi_arm64_smc_1_0(PSCI_1_0_FN_PSCI_FEATURES,
+				   ARM_SMCCC_VERSION_FUNC_ID);
+	if (status < 0)
+		return false;
+
+	version = efi_arm64_smc_1_0(ARM_SMCCC_VERSION_FUNC_ID, 0);
+	return (version >> 16) == 1 && version >= ARM_SMCCC_VERSION_1_1;
+}
diff --git a/drivers/firmware/efi/libstub/arm64-stub.h b/drivers/firmware/efi/libstub/arm64-stub.h
new file mode 100644
index 00000000000000..f55e56ecd305bd
--- /dev/null
+++ b/drivers/firmware/efi/libstub/arm64-stub.h
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+bool efi_arm64_smccc_1_1(void);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 11/11] efi/arm64: Implement ARM64 DRTM in the stub
  2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
                   ` (9 preceding siblings ...)
  2026-11-20 17:52 ` [PATCH v3 10/11] efi/arm64: Add a way to detect if SMCC 1.1 can be used Jason Gunthorpe
@ 2026-11-20 17:52 ` Jason Gunthorpe
  10 siblings, 0 replies; 13+ messages in thread
From: Jason Gunthorpe @ 2026-11-20 17:52 UTC (permalink / raw)
  To: Alexandre Ghiti, Albert Ou, Ard Biesheuvel, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 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

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 9b08b2457a555f..412fc29f4e1cf0 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -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
diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index 2ae86429315e0e..125159d8136f33 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -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
diff --git a/drivers/firmware/efi/libstub/arm64-drtm.c b/drivers/firmware/efi/libstub/arm64-drtm.c
new file mode 100644
index 00000000000000..73f1ce38394d51
--- /dev/null
+++ b/drivers/firmware/efi/libstub/arm64-drtm.c
@@ -0,0 +1,383 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES
+ *
+ * EFI Stub implementation for ARM DEN 0113 "DRTM Architecture for Arm"
+ *
+ * This code runs on the untrusted side of the boot in the EFI stub. Nothing it
+ * does should impact the integrity of the post-launch world and it does not
+ * need to take special security precautions.
+ */
+#include <linux/bitfield.h>
+#include <linux/efi.h>
+#include <linux/kconfig.h>
+#include <linux/overflow.h>
+#include <linux/tpm_command.h>
+
+#include <asm/cputype.h>
+#include <asm/drtm.h>
+
+#include "efistub.h"
+#include "arm64-stub.h"
+
+struct efi_drtm_config {
+	u32 launch_features;
+	unsigned long dlme_data_size;
+	unsigned long nw_dce_size;
+	struct arm_drtm_parameters *params_addr;
+	struct arm64_drtm_handoff *handoff;
+};
+static struct efi_drtm_config drtm_cfg;
+
+static bool efi_drtm_query_feature(u64 feature, u64 *value)
+{
+	s64 status;
+
+	status = arm_drtm_query_feature(feature, value);
+	if (status < 1) {
+		efi_err("DRTM: feature %llx query failed (x0=%lld)\n", feature,
+			status);
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * For the kernel to boot it must have the matching hash algorithm built in to
+ * be able to validate the TCB hashes. Assume the TPM hash algorithm is the one
+ * that TCB_HASH_TABLE will be using.
+ */
+static bool efi_drtm_hash_algorithm_supported(u16 algorithm)
+{
+	switch (algorithm) {
+	case TPM_ALG_SHA256:
+		return IS_BUILTIN(CONFIG_CRYPTO_SHA256);
+	case TPM_ALG_SHA384:
+	case TPM_ALG_SHA512:
+		return IS_BUILTIN(CONFIG_CRYPTO_SHA512);
+	default:
+		return false;
+	}
+}
+
+static bool efi_drtm_probe_tpm(void)
+{
+	u16 algorithm;
+	u8 schemas;
+	u64 value;
+
+	if (!efi_drtm_query_feature(ARM_DRTM_FEATURE_TPM, &value))
+		return false;
+	algorithm = FIELD_GET(ARM_DRTM_TPM_ALG_MASK, value);
+	schemas = FIELD_GET(ARM_DRTM_PCR_SCHEMA_MASK, value);
+
+	if (!efi_drtm_hash_algorithm_supported(algorithm)) {
+		efi_err("DRTM: firmware hash algorithm 0x%x is unavailable (feature 0x%llx)\n",
+			algorithm, value);
+		return false;
+	}
+
+	if (!(schemas & BIT(ARM_DRTM_PCR_SCHEMA_DEFAULT))) {
+		efi_err("DRTM: default PCR schema is unavailable (feature 0x%llx)\n",
+			value);
+		return false;
+	}
+
+	efi_debug("DRTM: TPM algorithm 0x%x, TPM hashing %s, PCR schemas 0x%x\n",
+		  algorithm,
+		  value & ARM_DRTM_TPM_HASHING ? "available" : "unavailable",
+		  schemas);
+	return true;
+}
+
+static bool efi_drtm_probe_memory(void)
+{
+	u64 value;
+
+	if (!efi_drtm_query_feature(ARM_DRTM_FEATURE_MIN_MEMORY, &value))
+		return false;
+
+	drtm_cfg.dlme_data_size =
+		FIELD_GET(ARM_DRTM_DLME_DATA_PAGES_MASK, value) *
+		ARM_DRTM_PAGE_SIZE;
+	drtm_cfg.nw_dce_size = FIELD_GET(ARM_DRTM_NW_DCE_PAGES_MASK, value) *
+			       ARM_DRTM_PAGE_SIZE;
+
+	efi_debug("DRTM: minimum DLME data %lu bytes, Normal-world DCE %lu bytes\n",
+		  drtm_cfg.dlme_data_size, drtm_cfg.nw_dce_size);
+	return true;
+}
+
+static bool efi_drtm_probe_dma(void)
+{
+	u64 value;
+
+	if (!efi_drtm_query_feature(ARM_DRTM_FEATURE_DMA_PROTECTION, &value))
+		return false;
+
+	if (!(FIELD_GET(ARM_DRTM_DMA_PROTECTION_MASK, value) &
+	      BIT(ARM_DRTM_DMA_PROTECTION_COMPLETE))) {
+		efi_err("DRTM: complete DMA protection is unavailable (feature 0x%llx)\n",
+			value);
+		return false;
+	}
+	return true;
+}
+
+/* Sanity check nothing has gone wrong */
+static bool efi_drtm_probe_boot_pe(void)
+{
+	u64 current_pe = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
+	u64 boot_pe;
+
+	if (!efi_drtm_query_feature(ARM_DRTM_FEATURE_BOOT_PE, &boot_pe))
+		return false;
+
+	if ((boot_pe & MPIDR_HWID_BITMASK) != current_pe) {
+		efi_err("DRTM: boot PE 0x%llx does not match current PE 0x%llx\n",
+			boot_pe, current_pe);
+		return false;
+	}
+
+	return true;
+}
+
+static void efi_drtm_report_previous_error(void)
+{
+	s64 error_code;
+	s64 status;
+
+	status = arm_drtm_query_function(ARM_DRTM_SMC_GET_ERROR);
+	if (status != ARM_DRTM_SUCCESS) {
+		efi_debug("DRTM: GET_ERROR is unavailable (x0=%lld)\n", status);
+		return;
+	}
+
+	status = arm_drtm_get_error(&error_code);
+	if (status != ARM_DRTM_SUCCESS) {
+		efi_warn("DRTM: failed to read previous error (x0=%lld)\n",
+			 status);
+		return;
+	}
+
+	if (error_code) {
+		efi_warn("DRTM: firmware reports previous launch error 0x%llx\n",
+			 error_code);
+		if (efi_drtm_policy != EFI_DRTM_ENFORCE)
+			efi_drtm_policy = EFI_DRTM_OFF;
+	}
+}
+
+static efi_status_t efi_drtm_failure(void)
+{
+	if (efi_drtm_policy == EFI_DRTM_AUTO) {
+		efi_warn("DRTM: preparation failed, continuing without DRTM\n");
+		efi_drtm_policy = EFI_DRTM_OFF;
+		return EFI_SUCCESS;
+	}
+
+	efi_err("DRTM: enforced preparation failed\n");
+	return EFI_UNSUPPORTED;
+}
+
+efi_status_t efi_drtm_prepare(void)
+{
+	s64 feature_status;
+	u16 major, minor;
+	s32 status;
+
+	if (efi_drtm_policy == EFI_DRTM_OFF)
+		return EFI_SUCCESS;
+
+	if (!efi_arm64_smccc_1_1())
+		return efi_drtm_failure();
+
+	/*
+	 * v1.4B section 3.2.1 "DRTM_VERSION usage" explains that a new major
+	 * ABI version may "Change behavior of existing functions". Verify the
+	 * major version before calling anything so we don't trigger unknown
+	 * behavior.
+	 */
+	status = arm_drtm_version(&major, &minor);
+	if (status != ARM_DRTM_SUCCESS) {
+		efi_err("DRTM: failed to read interface version (x0=%d)\n",
+			status);
+		return efi_drtm_failure();
+	}
+	if (major != ARM_DRTM_VERSION_MAJOR) {
+		efi_err("DRTM: unsupported interface version %u.%u\n", major,
+			minor);
+		return efi_drtm_failure();
+	}
+	efi_info("DRTM: interface version %u.%u\n", major, minor);
+
+	efi_drtm_report_previous_error();
+	if (efi_drtm_policy == EFI_DRTM_OFF)
+		return EFI_SUCCESS;
+
+	feature_status = arm_drtm_query_function(ARM_DRTM_SMC_DYNAMIC_LAUNCH);
+	if (feature_status != ARM_DRTM_SUCCESS) {
+		efi_err("DRTM: dynamic launch is unavailable (x0=%lld)\n",
+			feature_status);
+		return efi_drtm_failure();
+	}
+
+	if (!efi_drtm_probe_tpm() || !efi_drtm_probe_memory() ||
+	    !efi_drtm_probe_dma() || !efi_drtm_probe_boot_pe())
+		return efi_drtm_failure();
+
+	drtm_cfg.launch_features =
+		ARM_DRTM_LAUNCH_HASH_FIRMWARE |
+		FIELD_PREP(ARM_DRTM_LAUNCH_PCR_SCHEMA_MASK,
+			   ARM_DRTM_PCR_SCHEMA_DEFAULT) |
+		FIELD_PREP(ARM_DRTM_LAUNCH_DMA_PROTECTION_MASK,
+			   ARM_DRTM_DMA_PROTECTION_COMPLETE) |
+		ARM_DRTM_LAUNCH_NO_AUTH | ARM_DRTM_LAUNCH_KEEP_SECURE_IRQS;
+
+	return EFI_SUCCESS;
+}
+
+unsigned long efi_drtm_get_extra_size(void)
+{
+	/*
+	 * See arch/arm64/kernel/vmlinux.lds.S for the DRTM Memory layout.
+	 *
+	 * v1.4B Table 6 "Return values for DRTM features", feature 0x2 reports
+	 * both minimum sizes in 4 KiB pages.
+	 *
+	 * The linker places the DLME data at the 4 KiB-aligned end of the
+	 * static Image as required by R314030 in Table 13 "DLME region
+	 * requirements".
+	 *
+	 * The DLME region must include all of that data (R45200 in Table 30
+	 * "DCE requirements for the DLME"), and an optional Normal-world DCE
+	 * follows it at another 4 KiB-aligned address as required by R312080 in
+	 * Table 10 "DRTM_PARAMETERS requirements".
+	 *
+	 * A final page holds the 4 KiB-aligned DRTM_PARAMETERS required by
+	 * R312010 in the same table. Only the first area is part of the DLME
+	 * region.
+	 */
+	if (efi_drtm_policy == EFI_DRTM_OFF)
+		return 0;
+	return drtm_cfg.dlme_data_size + drtm_cfg.nw_dce_size +
+	       ARM_DRTM_PAGE_SIZE;
+}
+
+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);
+	struct arm_drtm_parameters *params;
+	unsigned long measured_offset;
+	unsigned long measured_size;
+	unsigned long image_base;
+	unsigned long image_size;
+	unsigned long dlme_start;
+	unsigned long dlme_end;
+	unsigned long dce_end;
+	unsigned long entry;
+
+	if (efi_drtm_policy == EFI_DRTM_OFF)
+		return EFI_SUCCESS;
+
+	image_base = efistub_kernel_base(image_info);
+	image_size = efistub_kernel_memsize(image_info);
+	measured_offset = efistub_drtm_measured_offset(image_info);
+	measured_size = efistub_drtm_measured_size(image_info);
+	entry = efistub_drtm_entry_offset(image_info);
+	dlme_start = image_base + image_size;
+	dlme_end = dlme_start + drtm_cfg.dlme_data_size;
+	dce_end = dlme_end + drtm_cfg.nw_dce_size;
+
+	/* Quick checks something didn't go wrong during image construction */
+	if (entry < measured_offset ||
+	    entry - measured_offset >= measured_size ||
+	    !IS_ALIGNED(image_base, ARM_DRTM_PAGE_SIZE) ||
+	    !IS_ALIGNED(image_size, ARM_DRTM_PAGE_SIZE) ||
+	    !IS_ALIGNED(measured_offset, ARM_DRTM_PAGE_SIZE) ||
+	    !IS_ALIGNED(dlme_start, ARM_DRTM_PAGE_SIZE) ||
+	    !IS_ALIGNED(dlme_end, ARM_DRTM_PAGE_SIZE) ||
+	    !IS_ALIGNED(dce_end, ARM_DRTM_PAGE_SIZE)) {
+		efi_err("DRTM: final Image layout is invalid\n");
+		return efi_drtm_failure();
+	}
+
+	/*
+	 * For robustness zero all the trailing space at the end of the
+	 * alocation. This contains the DRTM_PARAMETERS structure too, so
+	 * must be done before filling it.
+	 */
+	memset((void *)dlme_start, 0, efi_drtm_get_extra_size());
+	params = (void *)dce_end;
+	params->revision = cpu_to_le16(1);
+	/* PCR authorities, image authentication and secure IRQ disable need v2. */
+	if (drtm_cfg.launch_features &
+	    (ARM_DRTM_LAUNCH_PCR_SCHEMA_MASK | ARM_DRTM_LAUNCH_AUTH |
+	     ARM_DRTM_LAUNCH_DISABLE_SECURE_IRQS))
+		params->revision = cpu_to_le16(2);
+	params->launch_features = cpu_to_le32(drtm_cfg.launch_features);
+	params->dlme_region_address = cpu_to_le64(image_base);
+	params->dlme_region_size = cpu_to_le64(dlme_end - image_base);
+	params->dlme_image_start = cpu_to_le64(measured_offset);
+	params->dlme_entry_point_offset = cpu_to_le64(entry - measured_offset);
+	params->dlme_image_size = cpu_to_le64(measured_size);
+	params->dlme_data_offset = cpu_to_le64(image_size);
+	if (drtm_cfg.nw_dce_size) {
+		params->nw_dce_region_address = cpu_to_le64(dlme_end);
+		params->nw_dce_region_size = cpu_to_le64(drtm_cfg.nw_dce_size);
+	}
+
+	/*
+	 * The DLME Data contains its own size in a trusted header, so the
+	 * handoff doesn't need to include extra_size. The DCE Data is only
+	 * temporary so the kernel also does not need to know about it.
+	 */
+	handoff->fdt_addr = cpu_to_le64(fdt_addr);
+	handoff->drtm_enabled = 1;
+
+	efi_debug("DRTM: will launch, selected launch features 0x%x\n",
+		  drtm_cfg.launch_features);
+
+	drtm_cfg.params_addr = params;
+	drtm_cfg.handoff = handoff;
+	return EFI_SUCCESS;
+}
+
+static void efi_drtm_fallback(void)
+{
+	drtm_cfg.handoff->drtm_enabled = 0;
+}
+
+void efi_drtm_launch(void)
+{
+	if (efi_drtm_policy == EFI_DRTM_OFF)
+		return;
+
+	/*
+	 * No cache maintenance is required before the launch. v1.4B R42130 in
+	 * Table 22 "DCE preamble requirements" requires the DRTM_PARAMETERS to
+	 * be accessible as Normal Write-Back Cacheable, Inner Shareable memory,
+	 * so the DCE reads them coherently with the writes made above.  R45220
+	 * in Table 30 "DCE requirements for the DLME" then has the DCE clean
+	 * and invalidate the whole DLME region to the Point of Coherency before
+	 * it measures the DLME image, which covers both the Image itself and
+	 * the handoff struct placed in the DLME region. Thus once we jump into
+	 * the kernel with MMU and caches off the CPU will see everything the
+	 * stub wrote.
+	 */
+	arm_drtm_dynamic_launch(drtm_cfg.params_addr);
+
+	/*
+	 * v1.4B section 3.4 "DRTM_DYNAMIC_LAUNCH" says it returns from
+	 * DYNAMIC_LAUNCH only on error. Boot services and their diagnostics are
+	 * no longer available, so hang.
+	 */
+	if (efi_drtm_policy == EFI_DRTM_ENFORCE) {
+		for (;;)
+			asm volatile("wfe");
+	}
+
+	efi_drtm_fallback();
+}
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size
  2026-11-20 17:51 ` [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size Jason Gunthorpe
@ 2026-11-20 23:33   ` Ard Biesheuvel
  0 siblings, 0 replies; 13+ messages in thread
From: Ard Biesheuvel @ 2026-11-20 23:33 UTC (permalink / raw)
  To: Jason Gunthorpe, Alexandre Ghiti, Albert Ou, Catalin Marinas,
	Jonathan Corbet, Ilias Apalodimas, Justin Stitt, linux-arm-kernel,
	linux-doc, linux-efi, linux-riscv, llvm, Mark Rutland,
	Bill Wendling, Nathan Chancellor, Nick Desaulniers,
	Palmer Dabbelt, Paul Walmsley, Randy Dunlap, Shuah Khan,
	Will Deacon
  Cc: Jonathan Cameron, linux-integrity, patches, Piotr Król,
	Ross Philipson

Hi Jason,

On Fri, 20 Nov 2026, at 18:51, Jason Gunthorpe wrote:
> 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?


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-11-20 23:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 01/11] efi/libstub: Add generic arch callbacks for DRTM Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size Jason Gunthorpe
2026-11-20 23:33   ` Ard Biesheuvel
2026-11-20 17:51 ` [PATCH v3 04/11] efi: Add a __efi_data_handoff section annotation Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 05/11] efi/libstub: Put the stub's writable data in unique sections for DRTM Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 06/11] arm64: drtm: Add macro definitions for DEN0113 Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 07/11] arm64: drtm: Update the linker script for EFI_STUB_DRTM Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 08/11] arm64: drtm: Add drtm_entry point to head.S Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 09/11] arm64: drtm: Call UNPROTECT_MEMORY Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 10/11] efi/arm64: Add a way to detect if SMCC 1.1 can be used Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 11/11] efi/arm64: Implement ARM64 DRTM in the stub Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox