Skip to contentSkip to conversation

efi_loader: ARM: add support for ARMV7_NONSEC=y

PATCH New Mark Kettenis 3 participants 12 messages Jun 15, 2018 – Jun 19, 2018 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

v4 cover efi_loader: ARM: add support for ARMV7_NONSEC=y

This series makes it possible to run EFI applications in non-secure mode. It allows me to run OpenBSD on the Technexion PICO-PI-IMX7 and Banana Pi boards using the PSCI implementation provided by U-Boot.

The second version avoids using r3 to pass the original stack pointer. For some reason that register gets clobbered on the Banana Pi. Instead this version just migrates SP_svc to SP_hyp.

The third version avoids saving r3 on the stack and fixes an include guard as suggested by Alexander Graf.

This fourth version enables ARMV7_LPAE if HYP mode is supported sych that we enable the MMU and caches in HYP mode. It also makes sure we don't attempt to enter non-secure mode twice if we execute a 2nd EFI payload.

Mark Kettenis (5):
ARM: HYP/non-sec: migrate stack
efi_loader: ARM: run EFI payloads non-secure
efi_loader: ARM: don't attempt to enter non-secure mode twice
ARM: HYP/non-sec: enable ARMV7_LPAE if HYP mode is supported Revert "efi_loader: no support for ARMV7_NONSEC=y"

 arch/arm/cpu/armv7/Kconfig       |  2 +-
 arch/arm/cpu/armv7/nonsec_virt.S |  2 ++
 cmd/bootefi.c                    | 36 ++++++++++++++++++++++++++++++++++++
 doc/README.uefi                  |  2 --
 lib/efi_loader/Kconfig           |  2 --
 5 files changed, 39 insertions(+), 5 deletions(-)
Signature · 1 line
2.16.2
#1 lore raw

PATCH v4 1/5 ARM: HYP/non-sec: migrate stack

The current code that switches into HYP mode doesn't bother to set up a stack for HYP mode. This doesn't work for EFI applications as they expect a usable stack. Fix this by migrating the stack pointer from SP_svc to SP_hyp while in Monitor mode. This restores the stack pointer when we drop into HYP mode.

---
 arch/arm/cpu/armv7/nonsec_virt.S | 2 ++
 1 file changed, 2 insertions(+)
arch/arm/cpu/armv7/nonsec_virt.S +2 -0
index 56bdba1d38..1773fae205 100644
@@ -80,6 +80,8 @@ _secure_monitor:
 #ifdef CONFIG_ARMV7_VIRT
 	orreq	r5, r5, #0x100			@ allow HVC instruction
 	moveq	r6, #HYP_MODE			@ Enter the kernel as HYP
+	mrseq	r3, sp_svc
+	msreq	sp_hyp, r3			@ migrate SP
 #endif
 
 	mcr	p15, 0, r5, c1, c1, 0		@ write SCR (with NS bit set)
Signature · 1 line
2.16.2
#2 lore raw

PATCH v4 1/5 ARM: HYP/non-sec: migrate stack

Quoted 7 lines

The current code that switches into HYP mode doesn't bother to set up a stack for HYP mode. This doesn't work for EFI applications as they expect a usable stack. Fix this by migrating the stack pointer from SP_svc to SP_hyp while in Monitor mode. This restores the stack pointer when we drop into HYP mode.

Thanks, applied to efi-next

Alex

#3 lore raw

PATCH v4 2/5 efi_loader: ARM: run EFI payloads non-secure

In reply to Mark Kettenis

If desired (and possible) switch into HYP mode or non-secure SVC mode before calling the entry point of an EFI application. This allows U-Boot to provide a usable PSCI implementation and makes it possible to boot kernels into hypervisor mode using an EFI bootloader.

Based on diffs from Heinrich Schuchardt and Alexander Graf.

---
 cmd/bootefi.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
cmd/bootefi.c +32 -0
index 707d159bac..12a6b84ce6 100644
@@ -20,6 +20,11 @@
 #include <asm-generic/unaligned.h>
 #include <linux/linkage.h>
 
+#ifdef CONFIG_ARMV7_NONSEC
+#include <asm/armv7.h>
+#include <asm/secure.h>
+#endif
+
 DECLARE_GLOBAL_DATA_PTR;
 
 #define OBJ_LIST_NOT_INITIALIZED 1
@@ -189,6 +194,18 @@ static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
 }
 #endif
 
+#ifdef CONFIG_ARMV7_NONSEC
+static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
+			efi_handle_t image_handle, struct efi_system_table *st),
+			efi_handle_t image_handle, struct efi_system_table *st)
+{
+	/* Enable caches again */
+	dcache_enable();
+
+	return efi_do_enter(image_handle, st, entry);
+}
+#endif
+
 /* Carve out DT reserved memory ranges */
 static efi_status_t efi_carve_out_dt_rsv(void *fdt)
 {
@@ -338,6 +355,21 @@ static efi_status_t do_bootefi_exec(void *efi,
 	}
 #endif
 
+#ifdef CONFIG_ARMV7_NONSEC
+	if (armv7_boot_nonsec()) {
+		dcache_disable();	/* flush cache before switch to HYP */
+
+		armv7_init_nonsec();
+		secure_ram_addr(_do_nonsec_entry)(efi_run_in_hyp,
+				(uintptr_t)entry,
+				(uintptr_t)loaded_image_info_obj.handle,
+				(uintptr_t)&systab);
+
+		/* Should never reach here, efi exits with longjmp */
+		while (1) { }
+	}
+#endif
+
 	ret = efi_do_enter(loaded_image_info_obj.handle, &systab, entry);
 
 exit:
Signature · 1 line
2.16.2
#4 lore raw

PATCH v4 2/5 efi_loader: ARM: run EFI payloads non-secure

Quoted 8 lines

If desired (and possible) switch into HYP mode or non-secure SVC mode before calling the entry point of an EFI application. This allows U-Boot to provide a usable PSCI implementation and makes it possible to boot kernels into hypervisor mode using an EFI bootloader.

Based on diffs from Heinrich Schuchardt and Alexander Graf.

Thanks, applied to efi-next

Alex

#5 lore raw

PATCH v4 3/5 efi_loader: ARM: don't attempt to enter non-secure mode twice

In reply to Mark Kettenis

Multiple EFI binaries may be executed in sequence. So if we already are in non-secure mode after running the first one we should skip the switching code since it no longer works once we're non-secure.

---
 cmd/bootefi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
cmd/bootefi.c +5 -1
index 12a6b84ce6..12081cee46 100644
@@ -195,6 +195,8 @@ static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
 #endif
 
 #ifdef CONFIG_ARMV7_NONSEC
+static bool is_nonsec;
+
 static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
 			efi_handle_t image_handle, struct efi_system_table *st),
 			efi_handle_t image_handle, struct efi_system_table *st)
@@ -202,6 +204,8 @@ static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
 	/* Enable caches again */
 	dcache_enable();
 
+	is_nonsec = true;
+
 	return efi_do_enter(image_handle, st, entry);
 }
 #endif
@@ -356,7 +360,7 @@ static efi_status_t do_bootefi_exec(void *efi,
 #endif
 
 #ifdef CONFIG_ARMV7_NONSEC
-	if (armv7_boot_nonsec()) {
+	if (armv7_boot_nonsec() && !is_nonsec) {
 		dcache_disable();	/* flush cache before switch to HYP */
 
 		armv7_init_nonsec();
Signature · 1 line
2.16.2
#6 lore raw

PATCH v4 3/5 efi_loader: ARM: don't attempt to enter non-secure mode twice

Quoted 5 lines

Multiple EFI binaries may be executed in sequence. So if we already are in non-secure mode after running the first one we should skip the switching code since it no longer works once we're non-secure.

Thanks, applied to efi-next

Alex

#7 lore raw

PATCH v4 4/5 ARM: HYP/non-sec: enable ARMV7_LPAE if HYP mode is supported

In reply to Mark Kettenis

ARMV7_LPAE is required in order to enable the MMU in HYP mode. And we really want to enable the MMU in HYP mode such that we can enable the the caches. Otherwise U-Boot code (such as the EFI implementation) that runs in HYP mode will run at a snils pace.

---
 arch/arm/cpu/armv7/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/cpu/armv7/Kconfig +1 -1
index 37a0be932e..73d57a2aae 100644
@@ -53,7 +53,7 @@ config ARMV7_PSCI_NR_CPUS
 config ARMV7_LPAE
 	bool "Use LPAE page table format" if EXPERT
 	depends on CPU_V7A
-	default n
+	default y if ARMV7_VIRT
 	---help---
 	Say Y here to use the long descriptor page table format. This is
 	required if U-Boot runs in HYP mode.
Signature · 1 line
2.16.2
#8 lore raw

PATCH v4 4/5 ARM: HYP/non-sec: enable ARMV7_LPAE if HYP mode is supported

Quoted 6 lines

ARMV7_LPAE is required in order to enable the MMU in HYP mode. And we really want to enable the MMU in HYP mode such that we can enable the the caches. Otherwise U-Boot code (such as the EFI implementation) that runs in HYP mode will run at a snils pace.

Thanks, applied to efi-next

Alex

#9 lore raw

PATCH v4 5/5 Revert "efi_loader: no support for ARMV7_NONSEC=y"

In reply to Mark Kettenis

This reverts commit c524997acb3d322e1bbd36c06ad02ef589705e7c.

Booting ARMv7 in non-secure mode using bootefi works now.

---
 doc/README.uefi        | 2 --
 lib/efi_loader/Kconfig | 2 --
 2 files changed, 4 deletions(-)
doc/README.uefi +0 -2
index d4031ef8e8..6b9759cfed 100644
@@ -329,8 +329,6 @@ This driver is only available if U-Boot is configured with
   * persistence
   * runtime support
 
-* support bootefi booting ARMv7 in non-secure mode (CONFIG_ARMV7_NONSEC=y)
-
 ## Links
 
 * [1](http://uefi.org/specifications)
lib/efi_loader/Kconfig +0 -2
index df58e633d1..ce6a09f0b4 100644
@@ -1,8 +1,6 @@
 config EFI_LOADER
 	bool "Support running EFI Applications in U-Boot"
 	depends on (ARM || X86 || RISCV) && OF_LIBFDT
-	# We do not support bootefi booting ARMv7 in non-secure mode
-	depends on !ARMV7_NONSEC
 	# We need EFI_STUB_64BIT to be set on x86_64 with EFI_STUB
 	depends on !EFI_STUB || !X86_64 || EFI_STUB_64BIT
 	# We need EFI_STUB_32BIT to be set on x86_32 with EFI_STUB
Signature · 1 line
2.16.2
#10 lore raw

PATCH v4 5/5 Revert "efi_loader: no support for ARMV7_NONSEC=y"

Quoted 5 lines

This reverts commit c524997acb3d322e1bbd36c06ad02ef589705e7c.

Booting ARMv7 in non-secure mode using bootefi works now.

Thanks, applied to efi-next

Alex

#11 lore raw

v4 cover efi_loader: ARM: add support for ARMV7_NONSEC=y

In reply to Mark Kettenis
Quoted 23 lines · Mark Kettenis

This series makes it possible to run EFI applications in non-secure mode. It allows me to run OpenBSD on the Technexion PICO-PI-IMX7 and Banana Pi boards using the PSCI implementation provided by U-Boot.

The second version avoids using r3 to pass the original stack pointer. For some reason that register gets clobbered on the Banana Pi. Instead this version just migrates SP_svc to SP_hyp.

The third version avoids saving r3 on the stack and fixes an include guard as suggested by Alexander Graf.

This fourth version enables ARMV7_LPAE if HYP mode is supported sych that we enable the MMU and caches in HYP mode. It also makes sure we don't attempt to enter non-secure mode twice if we execute a 2nd EFI payload.

Mark Kettenis (5):
ARM: HYP/non-sec: migrate stack
efi_loader: ARM: run EFI payloads non-secure
efi_loader: ARM: don't attempt to enter non-secure mode twice
ARM: HYP/non-sec: enable ARMV7_LPAE if HYP mode is supported Revert "efi_loader: no support for ARMV7_NONSEC=y"

Quoted 7 lines
 arch/arm/cpu/armv7/Kconfig       |  2 +-
 arch/arm/cpu/armv7/nonsec_virt.S |  2 ++
 cmd/bootefi.c                    | 36 ++++++++++++++++++++++++++++++++++++
 doc/README.uefi                  |  2 --
 lib/efi_loader/Kconfig           |  2 --
 5 files changed, 39 insertions(+), 5 deletions(-)

Hello Mark,

I have built with your 5 patches added to git master (f58e779513be36e30ce46838fb467e12ac6a5539).

With your patch series iPXE starts normally.

In iPXE I can use network commands ping and nslookup.

I was not able to boot via iSCSI but I have the same problem without your patches 1-4.

Best regards

Heinrich