Enable EFI support
Subsystems (6)
v2 cover Enable EFI support
This series starts with some fixes for backtraces for bugs found when tracing with riscv EFI builds. The series then brings EFI support to riscv, basing the approach heavily on arm64's support (including arm64's improvements[1]). It should now be possible to launch tests from EFI-capable bootloaders.
[1] https://lore.kernel.org/all/20240305164623.379149-20-andrew.jones at linux.dev/
v2:
- Rebase on v3 of arm's efi improvement series
- Just make base_address a weak function rather than duplicate it
- Always preserve .so files (they're useful for debug)
- Build the sieve test for EFI
- Pick up a couple tags
Thanks,
drew
Andrew Jones (13):
riscv: Call abort instead of assert on unhandled exceptions
riscv: show_regs: Prepare for EFI images
treewide: lib/stack: Fix backtrace
treewide: lib/stack: Make base_address arch specific
riscv: Import gnu-efi files
riscv: Tweak the gnu-efi imported code
riscv: Enable building for EFI
riscv: efi: Switch stack in _start
efi: Add support for obtaining the boot hartid
riscv: Refactor setup code
riscv: Enable EFI boot
riscv: efi: Add run script
riscv: efi: Use efi-direct by default
configure | 12 +- lib/arm/stack.c | 13 +-- lib/arm64/stack.c | 12 +- lib/efi.c | 33 ++++++ lib/elf.h | 5 + lib/riscv/asm/setup.h | 5 + lib/riscv/processor.c | 11 +- lib/riscv/setup.c | 170 +++++++++++++++++++++------- lib/riscv/stack.c | 30 +++-- lib/s390x/stack.c | 12 +- lib/stack.c | 8 +- lib/stack.h | 24 +++- lib/x86/stack.c | 12 +- riscv/Makefile | 27 ++++- riscv/cstart.S | 4 + riscv/efi/crt0-efi-riscv64.S | 205 ++++++++++++++++++++++++++++++++++ riscv/efi/elf_riscv64_efi.lds | 142 +++++++++++++++++++++++ riscv/efi/reloc_riscv64.c | 91 +++++++++++++++ riscv/efi/run | 106 ++++++++++++++++++ riscv/flat.lds | 1 + riscv/run | 2 +- 21 files changed, 828 insertions(+), 97 deletions(-) create mode 100644 riscv/efi/crt0-efi-riscv64.S create mode 100644 riscv/efi/elf_riscv64_efi.lds create mode 100644 riscv/efi/reloc_riscv64.c create mode 100755 riscv/efi/run
Signature · 1 line
2.44.0
PATCH v2 1/13 riscv: Call abort instead of assert on unhandled exceptions
We should call abort() instead of assert() on an unhandled exception since assert() calls abort() anyway after a useless "assert failed: 0" message. We can also skip dumping the exception stack and just unwind from the stack frame where the exception occurred.
--- lib/riscv/processor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/riscv/processor.c +2 -1
@@ -43,7 +43,8 @@ void do_handle_exception(struct pt_regs *regs) }show_regs (regs); -assert (0 ); +dump_frame_stack ((void *)regs->epc, (void *)regs->s0); +abort (); }void install_exception_handler (unsigned long cause,void (*handler)(struct pt_regs *))
Signature · 1 line
2.44.0
PATCH v2 2/13 riscv: show_regs: Prepare for EFI images
In reply to Andrew JonesEFI images start with a header page and then _text, so the load address should use 'ImageBase' instead of _text. Just add the ImageBase symbol to the non-efi build too and then change show_regs() to use it instead. While there, add a couple convenience calculations for the PC and return address (pre-subtract the load address from them) in order to make it quicker for looking them up in an objdump.
--- lib/riscv/processor.c | 8 ++++---- riscv/flat.lds | 1 + 2 files changed, 5 insertions(+), 4 deletions(-)
lib/riscv/processor.c +4 -4
@@ -8,20 +8,20 @@#include <asm/processor.h> #include <asm/setup.h> -extern unsigned long _text; +extern unsigned long ImageBase;void show_regs (struct pt_regs *regs) {struct thread_info *info =current_thread_info (); -uintptr_t text = (uintptr_t )&_text; +uintptr_t loadaddr = (uintptr_t )&ImageBase;unsigned int w = __riscv_xlen /4 ; -printf ("Load address: %" PRIxPTR"\n" , text); +printf ("Load address: %" PRIxPTR"\n" , loadaddr);printf ("CPU%3d : hartid=%lx\n" , info->cpu, info->hartid);printf ("status : %.*lx\n" , w, regs->status);printf ("cause : %.*lx\n" , w, regs->cause);printf ("badaddr: %.*lx\n" , w, regs->badaddr); -printf ("pc: %.*lx ra: %.*lx\n" , w, regs->epc, w, regs->ra); +printf ("pc: %.*lx (%lx) ra: %.*lx (%lx)\n" , w, regs->epc, regs->epc - loadaddr, w, regs->ra, regs->ra - loadaddr);printf ("sp: %.*lx gp: %.*lx tp : %.*lx\n" , w, regs->sp, w, regs->gp, w, regs->tp);printf ("a0: %.*lx a1: %.*lx a2 : %.*lx a3 : %.*lx\n" , w, regs->a0, w, regs->a1, w, regs->a2, w, regs->a3);printf ("a4: %.*lx a5: %.*lx a6 : %.*lx a7 : %.*lx\n" , w, regs->a4, w, regs->a5, w, regs->a6, w, regs->a7);
riscv/flat.lds +1 -0
@@ -30,6 +30,7 @@ PHDRS SECTIONS { + PROVIDE(ImageBase = .); PROVIDE(_text = .); .text : { *(.init) *(.text) *(.text.*) } :text . = ALIGN(4K);
Signature · 1 line
2.44.0
PATCH v2 3/13 treewide: lib/stack: Fix backtrace
In reply to Andrew JonesWe should never pass the result of __builtin_frame_address(0) to another function since the compiler is within its rights to pop the frame to which it points before making the function call, as may be done for tail calls. Nobody has complained about backtrace(), so likely all compilations have been inlining backtrace_frame(), not dropping the frame on the tail call, or nobody is looking at traces. However, for riscv, when built for EFI, it does drop the frame on the tail call, and it was noticed. Preemptively fix backtrace() for all architectures.
--- lib/arm/stack.c | 13 +++++-------- lib/arm64/stack.c | 12 +++++------- lib/riscv/stack.c | 12 +++++------- lib/s390x/stack.c | 12 +++++------- lib/stack.h | 24 +++++++++++++++++------- lib/x86/stack.c | 12 +++++------- 6 files changed, 42 insertions(+), 43 deletions(-)
lib/arm/stack.c +5 -8
@@ -8,13 +8,16 @@#include <libcflat.h> #include <stack.h> -int backtrace_frame (const void *frame,const void **return_addrs, -int max_depth) +int arch_backtrace_frame (const void *frame,const void **return_addrs, +int max_depth,bool current_frame) {static int walking;int depth;const unsigned long *fp = (unsigned long *)frame; +if (current_frame) + fp =__builtin_frame_address (0 ); +if (walking) {printf ("RECURSIVE STACK WALK!!!\n" );return 0 ;
@@ -33,9 +36,3 @@ int backtrace_frame(const void *frame, const void **return_addrs, walking =0 ;return depth; } - -int backtrace (const void **return_addrs,int max_depth) -{ -return backtrace_frame (__builtin_frame_address (0 ), - return_addrs, max_depth); -}
lib/arm64/stack.c +5 -7
@@ -8,7 +8,8 @@extern char vector_stub_start, vector_stub_end; -int backtrace_frame (const void *frame,const void **return_addrs,int max_depth) +int arch_backtrace_frame (const void *frame,const void **return_addrs, +int max_depth,bool current_frame) {const void *fp = frame;static bool walking;
@@ -17,6 +18,9 @@ int backtrace_frame(const void *frame, const void **return_addrs, int max_depth)bool is_exception = false;unsigned long addr; +if (current_frame) + fp =__builtin_frame_address (0 ); +if (walking) {printf ("RECURSIVE STACK WALK!!!\n" );return 0 ;
@@ -54,9 +58,3 @@ int backtrace_frame(const void *frame, const void **return_addrs, int max_depth) walking = false;return depth; } - -int backtrace (const void **return_addrs,int max_depth) -{ -return backtrace_frame (__builtin_frame_address (0 ), - return_addrs, max_depth); -}
lib/riscv/stack.c +5 -7
@@ -2,12 +2,16 @@#include <libcflat.h> #include <stack.h> -int backtrace_frame (const void *frame,const void **return_addrs,int max_depth) +int arch_backtrace_frame (const void *frame,const void **return_addrs, +int max_depth,bool current_frame) {static bool walking;const unsigned long *fp = (unsigned long *)frame;int depth; +if (current_frame) + fp =__builtin_frame_address (0 ); +if (walking) {printf ("RECURSIVE STACK WALK!!!\n" );return 0 ;
@@ -24,9 +28,3 @@ int backtrace_frame(const void *frame, const void **return_addrs, int max_depth) walking = false;return depth; } - -int backtrace (const void **return_addrs,int max_depth) -{ -return backtrace_frame (__builtin_frame_address (0 ), - return_addrs, max_depth); -}
lib/s390x/stack.c +5 -7
@@ -14,11 +14,15 @@#include <stack.h> #include <asm/arch_def.h> -int backtrace_frame (const void *frame,const void **return_addrs,int max_depth) +int arch_backtrace_frame (const void *frame,const void **return_addrs, +int max_depth,bool current_frame) {int depth =0 ;struct stack_frame *stack = (struct stack_frame *)frame; +if (current_frame) + stack =__builtin_frame_address (0 ); +for (depth =0 ; stack && depth < max_depth; depth++) { return_addrs[depth] = (void *)stack->grs[8 ]; stack = stack->back_chain;
@@ -28,9 +32,3 @@ int backtrace_frame(const void *frame, const void **return_addrs, int max_depth)return depth; } - -int backtrace (const void **return_addrs,int max_depth) -{ -return backtrace_frame (__builtin_frame_address (0 ), - return_addrs, max_depth); -}
lib/stack.h +17 -7
@@ -11,17 +11,27 @@#include <asm/stack.h> #ifdef HAVE_ARCH_BACKTRACE_FRAME -extern int backtrace_frame (const void *frame,const void **return_addrs, -int max_depth); +extern int arch_backtrace_frame (const void *frame,const void **return_addrs, +int max_depth,bool current_frame); + +static inline int backtrace_frame (const void *frame,const void **return_addrs, +int max_depth) +{ +return arch_backtrace_frame (frame, return_addrs, max_depth, false); +} + +static inline int backtrace (const void **return_addrs,int max_depth) +{ +return arch_backtrace_frame (NULL , return_addrs, max_depth, true); +}#else -static inline int -backtrace_frame(const void *frame __unused, const void **return_addrs __unused, - int max_depth __unused) +extern int backtrace (const void **return_addrs,int max_depth); + +static inline int backtrace_frame (const void *frame,const void **return_addrs, +int max_depth) {return 0 ; }#endif -extern int backtrace (const void **return_addrs,int max_depth); -#endif
lib/x86/stack.c +5 -7
@@ -1,12 +1,16 @@#include <libcflat.h> #include <stack.h> -int backtrace_frame (const void *frame,const void **return_addrs,int max_depth) +int arch_backtrace_frame (const void *frame,const void **return_addrs, +int max_depth,bool current_frame) {static int walking;int depth =0 ;const unsigned long *bp = (unsigned long *) frame; +if (current_frame) + bp =__builtin_frame_address (0 ); +if (walking) {printf ("RECURSIVE STACK WALK!!!\n" );return 0 ;
@@ -23,9 +27,3 @@ int backtrace_frame(const void *frame, const void **return_addrs, int max_depth) walking =0 ;return depth; } - -int backtrace (const void **return_addrs,int max_depth) -{ -return backtrace_frame (__builtin_frame_address (0 ), return_addrs, - max_depth); -}
Signature · 1 line
2.44.0
PATCH v2 4/13 treewide: lib/stack: Make base_address arch specific
In reply to Andrew JonesCalculating the offset of an address is image specific, which is architecture specific. Until now, all architectures and architecture configurations which select CONFIG_RELOC were able to subtract _etext, but the EFI configuration of riscv cannot (it must subtract ImageBase). Make this function weak, such that an architecture may override it when necessary, to accommodate the image layout. Then, immediately supply the riscv override.
--- lib/riscv/stack.c | 18 ++++++++++++++++++ lib/stack.c | 8 ++++---- lib/stack.h | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-)
lib/riscv/stack.c +18 -0
@@ -2,6 +2,24 @@#include <libcflat.h> #include <stack.h> +#ifdef CONFIG_RELOC +extern char ImageBase, _text, _etext; + +bool arch_base_address (const void *rebased_addr,unsigned long *addr) +{ +unsigned long ra = (unsigned long )rebased_addr; +unsigned long base = (unsigned long )&ImageBase; +unsigned long start = (unsigned long )&_text; +unsigned long end = (unsigned long )&_etext; + +if (ra < start || ra >= end) +return false; + + *addr = ra - base; +return true; +} +#endif +int arch_backtrace_frame (const void *frame,const void **return_addrs,int max_depth,bool current_frame) {
lib/stack.c +4 -4
@@ -14,7 +14,7 @@#ifdef CONFIG_RELOC extern char _text, _etext; -static bool base_address (const void *rebased_addr,unsigned long *addr) +bool __attribute__((weak))arch_base_address (const void *rebased_addr,unsigned long *addr) {unsigned long ra = (unsigned long )rebased_addr;unsigned long start = (unsigned long )&_text;
@@ -27,7 +27,7 @@ static bool base_address(const void *rebased_addr, unsigned long *addr) return true; }#else -static bool base_address (const void *rebased_addr,unsigned long *addr) +bool __attribute__((weak))arch_base_address (const void *rebased_addr,unsigned long *addr) { *addr = (unsigned long )rebased_addr;return true;
@@ -45,13 +45,13 @@ static void print_stack(const void **return_addrs, int depth,/* @addr indicates a non-return address, as expected by the stack * pretty printer script. */ if (depth >0 && !top_is_return_address) { -if (base_address (return_addrs[0 ], &addr)) +if (arch_base_address (return_addrs[0 ], &addr))printf (" @%lx" , addr); i++; }for (; i < depth; i++) { -if (base_address (return_addrs[i], &addr)) +if (arch_base_address (return_addrs[i], &addr))printf (" %lx" , addr); }printf ("\n" );
lib/stack.h +2 -0
@@ -34,4 +34,6 @@ static inline int backtrace_frame(const void *frame, const void **return_addrs, }#endif +bool __attribute__((weak))arch_base_address (const void *rebased_addr,unsigned long *addr); +#endif
Signature · 1 line
2.44.0
PATCH v2 5/13 riscv: Import gnu-efi files
In reply to Andrew Jones--- riscv/efi/crt0-efi-riscv64.S | 187 ++++++++++++++++++++++++++++++++++ riscv/efi/elf_riscv64_efi.lds | 142 ++++++++++++++++++++++++++ riscv/efi/reloc_riscv64.c | 90 ++++++++++++++++ 3 files changed, 419 insertions(+) create mode 100644 riscv/efi/crt0-efi-riscv64.S create mode 100644 riscv/efi/elf_riscv64_efi.lds create mode 100644 riscv/efi/reloc_riscv64.c
riscv/efi/crt0-efi-riscv64.S new file +187 -0
@@ -0,0 +1,187 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */ +/* + * Copright (C) 2014 Linaro Ltd. <[email protected]> + * Copright (C) 2018 Alexander Graf <[email protected]> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice and this list of conditions, without modification. + * 2. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License as published by the Free Software Foundation; + * either version 2 of the License, or (at your option) any later version. + */ + +#ifndef EFI_SUBSYSTEM +#define EFI_SUBSYSTEM 10 +#endif + + .section .text.head + + /* + * Magic "MZ" signature for PE/COFF + */ + .globl ImageBase +ImageBase: + .ascii "MZ" + .skip 58 // 'MZ' + pad + offset == 64 + .4byte pe_header - ImageBase // Offset to the PE header. +pe_header: + .ascii "PE" + .2byte 0 +coff_header: + .2byte 0x5064 // riscv64 + .2byte 4 // nr_sections + .4byte 0 // TimeDateStamp + .4byte 0 // PointerToSymbolTable + .4byte 0 // NumberOfSymbols + .2byte section_table - optional_header // SizeOfOptionalHeader + .2byte 0x206 // Characteristics. + // IMAGE_FILE_DEBUG_STRIPPED | + // IMAGE_FILE_EXECUTABLE_IMAGE | + // IMAGE_FILE_LINE_NUMS_STRIPPED +optional_header: + .2byte 0x20b // PE32+ format + .byte 0x02 // MajorLinkerVersion + .byte 0x14 // MinorLinkerVersion + .4byte _text_size - ImageBase // SizeOfCode + .4byte _alldata_size - ImageBase // SizeOfInitializedData + .4byte 0 // SizeOfUninitializedData + .4byte _start - ImageBase // AddressOfEntryPoint + .4byte _text - ImageBase // BaseOfCode + +extra_header_fields: + .8byte 0 // ImageBase + .4byte 0x1000 // SectionAlignment + .4byte 0x1000 // FileAlignment + .2byte 0 // MajorOperatingSystemVersion + .2byte 0 // MinorOperatingSystemVersion + .2byte 0 // MajorImageVersion + .2byte 0 // MinorImageVersion + .2byte 0 // MajorSubsystemVersion + .2byte 0 // MinorSubsystemVersion + .4byte 0 // Win32VersionValue + + .4byte _image_end - ImageBase // SizeOfImage + + // Everything before the kernel image is considered part of the header + .4byte _text - ImageBase // SizeOfHeaders + .4byte 0 // CheckSum + .2byte EFI_SUBSYSTEM // Subsystem + .2byte 0 // DllCharacteristics + .8byte 0 // SizeOfStackReserve + .8byte 0 // SizeOfStackCommit + .8byte 0 // SizeOfHeapReserve + .8byte 0 // SizeOfHeapCommit + .4byte 0 // LoaderFlags + .4byte 0x10 // NumberOfRvaAndSizes + + .8byte 0 // ExportTable + .8byte 0 // ImportTable + .8byte 0 // ResourceTable + .8byte 0 // ExceptionTable + .8byte 0 // CertificationTable + .4byte _reloc - ImageBase // BaseRelocationTable (VirtualAddress) + .4byte _reloc_vsize - ImageBase // BaseRelocationTable (Size) + .8byte 0 // Debug + .8byte 0 // Architecture + .8byte 0 // Global Ptr + .8byte 0 // TLS Table + .8byte 0 // Load Config Table + .8byte 0 // Bound Import + .8byte 0 // IAT + .8byte 0 // Delay Import Descriptor + .8byte 0 // CLR Runtime Header + .8byte 0 // Reserved, must be zero + + // Section table +section_table: + + .ascii ".text\0\0\0" + .4byte _text_vsize - ImageBase // VirtualSize + .4byte _text - ImageBase // VirtualAddress + .4byte _text_size - ImageBase // SizeOfRawData + .4byte _text - ImageBase // PointerToRawData + .4byte 0 // PointerToRelocations (0 for executables) + .4byte 0 // PointerToLineNumbers (0 for executables) + .2byte 0 // NumberOfRelocations (0 for executables) + .2byte 0 // NumberOfLineNumbers (0 for executables) + .4byte 0x60000020 // Characteristics (section flags) + + /* + * The EFI application loader requires a relocation section + * because EFI applications must be relocatable. This is a + * dummy section as far as we are concerned. + */ + .ascii ".reloc\0\0" + .4byte _reloc_vsize - ImageBase // VirtualSize + .4byte _reloc - ImageBase // VirtualAddress + .4byte _reloc_size - ImageBase // SizeOfRawData + .4byte _reloc - ImageBase // PointerToRawData + .4byte 0 // PointerToRelocations + .4byte 0 // PointerToLineNumbers + .2byte 0 // NumberOfRelocations + .2byte 0 // NumberOfLineNumbers + .4byte 0x42000040 // Characteristics (section flags) + + .ascii ".data\0\0\0" + .4byte _data_vsize - ImageBase // VirtualSize + .4byte _data - ImageBase // VirtualAddress + .4byte _data_size - ImageBase // SizeOfRawData + .4byte _data - ImageBase // PointerToRawData + .4byte 0 // PointerToRelocations + .4byte 0 // PointerToLineNumbers + .2byte 0 // NumberOfRelocations + .2byte 0 // NumberOfLineNumbers + .4byte 0xC0000040 // Characteristics (section flags) + + .ascii ".rodata\0" + .4byte _rodata_vsize - ImageBase // VirtualSize + .4byte _rodata - ImageBase // VirtualAddress + .4byte _rodata_size - ImageBase // SizeOfRawData + .4byte _rodata - ImageBase // PointerToRawData + .4byte 0 // PointerToRelocations + .4byte 0 // PointerToLineNumbers + .2byte 0 // NumberOfRelocations + .2byte 0 // NumberOfLineNumbers + .4byte 0x40000040 // Characteristics (section flags) + + .text + .globl _start + .type _start,%function +_start: + addi sp, sp, -24 + sd a0, 0(sp) + sd a1, 8(sp) + sd ra, 16(sp) + lla a0, ImageBase + lla a1, _DYNAMIC + call _relocate + bne a0, zero, 0f + ld a1, 8(sp) + ld a0, 0(sp) + call _entry + ld ra, 16(sp) +0: addi sp, sp, 24 + ret + +// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: + + .data +dummy: .4byte 0 + +#define IMAGE_REL_ABSOLUTE 0 + .section .reloc, "a" +label1: + .4byte dummy-label1 // Page RVA + .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits + .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy + .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy + +#if defined(__ELF__) && defined(__linux__) + .section .note.GNU-stack,"",%progbits +#endif
riscv/efi/elf_riscv64_efi.lds new file +142 -0
142 lines not on this page · raw message
riscv/efi/reloc_riscv64.c new file +90 -0
90 lines not on this page · raw message
Signature · 1 line
2.44.0
PATCH v2 6/13 riscv: Tweak the gnu-efi imported code
In reply to Andrew JonesChange _relocate to match the prototype in efi.h (it doesn't matter that 'handle' and 'sys_tab' are unused). Also add R_RISCV_RELATIVE to efi.h and replace '_entry' with 'efi_main'.
--- lib/elf.h | 5 +++++ riscv/efi/crt0-efi-riscv64.S | 2 +- riscv/efi/reloc_riscv64.c | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-)
lib/elf.h +5 -0
@@ -65,4 +65,9 @@ typedef struct elf64_rela {/* The following are used with relocations */ #define ELF64_R_TYPE (i) ((i) & 0xffffffff) +/* +* riscv static relocation types. +*/ +#define R_RISCV_RELATIVE 3 +#endif /* _ELF_H_ */
riscv/efi/crt0-efi-riscv64.S +1 -1
@@ -164,7 +164,7 @@ _start: bne a0, zero, 0f ld a1, 8(sp) ld a0, 0(sp) - call _entry + call efi_main ld ra, 16(sp) 0: addi sp, sp, 24 ret
riscv/efi/reloc_riscv64.c +2 -1
@@ -44,7 +44,8 @@#define Elf_Rela Elf64_Rela#define ELF_R_TYPE ELF64_R_TYPE -EFI_STATUS EFIAPI _relocate(long ldbase, Elf_Dyn *dyn) +efi_status_t _relocate (long ldbase,Elf64_Dyn *dyn,efi_handle_t handle, +efi_system_table_t *sys_tab) {long relsz =0 , relent =0 ;Elf_Rela *rel =NULL ;
Signature · 1 line
2.44.0
PATCH v2 7/13 riscv: Enable building for EFI
In reply to Andrew JonesMimicking arm64 support, add configure and makefile changes to build for EFI. Since the linker script is replaced also replace the initial cstart code (also done like arm64). Finally, provide a stub for setup_efi() in order to allow compiling to complete (even though tests can't yet run).
--- configure | 3 ++- lib/riscv/asm/setup.h | 5 +++++ riscv/Makefile | 27 +++++++++++++++++++++++++-- riscv/cstart.S | 4 ++++ 4 files changed, 36 insertions(+), 3 deletions(-)
configure +2 -1
@@ -231,7 +231,8 @@ else fi fi -if [ "$efi" ] && [ "$arch" != "x86_64" ] && [ "$arch" != "arm64" ]; then +if [ "$efi" ] && [ "$arch" != "x86_64" ] && + [ "$arch" != "arm64" ] && [ "$arch" != "riscv64" ]; then echo "--[enable|disable]-efi is not supported for $arch" usage fi
lib/riscv/asm/setup.h +5 -0
@@ -12,4 +12,9 @@ int hartid_to_cpu(unsigned long hartid);void io_init (void );void setup (const void *fdt,phys_addr_t freemem_start); +#ifdef CONFIG_EFI +#include <efi.h> +static inline efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo) {return 0 ; } +#endif +#endif /* _ASMRISCV_SETUP_H_ */
riscv/Makefile +25 -2
@@ -17,7 +17,8 @@ tests += $(TEST_DIR)/sieve.$(exe) all: $(tests) -$(TEST_DIR)/sieve.elf: AUXFLAGS = 0x1 +# When built for EFI sieve needs extra memory, run with e.g. '-m 256' on QEMU +$(TEST_DIR)/sieve.$(exe): AUXFLAGS = 0x1 cstart.o = $(TEST_DIR)/cstart.o
@@ -85,7 +86,29 @@ include $(SRCDIR)/scripts/asm-offsets.mak -DPROGNAME=\"$(notdir $(@:.aux.o=.$(exe)))\" -DAUXFLAGS=$(AUXFLAGS) ifeq ($(CONFIG_EFI),y) - # TODO +# avoid jump tables before all relocations have been processed +riscv/efi/reloc_riscv64.o: CFLAGS += -fno-jump-tables +cflatobjs += riscv/efi/reloc_riscv64.o +cflatobjs += lib/acpi.o +cflatobjs += lib/efi.o + +.PRECIOUS: %.so + +%.so: EFI_LDFLAGS += -defsym=EFI_SUBSYSTEM=0xa --no-undefined +%.so: %.o $(FLATLIBS) $(SRCDIR)/riscv/efi/elf_riscv64_efi.lds $(cstart.o) %.aux.o + $(LD) $(EFI_LDFLAGS) -o $@ -T $(SRCDIR)/riscv/efi/elf_riscv64_efi.lds \ + $(filter %.o, $^) $(FLATLIBS) $(EFI_LIBS) + +%.efi: %.so + $(call arch_elf_check, $^) + $(OBJCOPY) --only-keep-debug $^ [email protected] + $(OBJCOPY) --strip-debug $^ + $(OBJCOPY) [email protected] $^ + $(OBJCOPY) \ + -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym \ + -j .rel -j .rela -j .rel.* -j .rela.* -j .rel* -j .rela* \ + -j .reloc \ + -O binary $^ $@ else %.elf: LDFLAGS += -pie -n -z notext %.elf: %.o $(FLATLIBS) $(SRCDIR)/riscv/flat.lds $(cstart.o) %.aux.o
riscv/cstart.S +4 -0
@@ -42,6 +42,9 @@ 9997: .endm +#ifdef CONFIG_EFI +#include "efi/crt0-efi-riscv64.S" +#else .section .init /*
@@ -109,6 +112,7 @@ start: call exit j halt +#endif /* !CONFIG_EFI */ .text .balign 4
Signature · 1 line
2.44.0
PATCH v2 8/13 riscv: efi: Switch stack in _start
In reply to Andrew JonesModify gnu-efi's _start to switch the stack. This allows us to not map memory regions which have EFI memory type EFI_BOOT_SERVICES_DATA, as the stack will be in the EFI_LOADER_CODE region instead. We'll still map the stack as R/W instead of R/X because we'll split the EFI_LOADER_CODE region on the _etext boundary and map addresses before _etext as R/X and the rest as R/W.
--- riscv/efi/crt0-efi-riscv64.S | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
riscv/efi/crt0-efi-riscv64.S +18 -0
@@ -164,7 +164,20 @@ _start: bne a0, zero, 0f ld a1, 8(sp) ld a0, 0(sp) + + /* Switch to our own stack */ + mv a2, sp + la sp, stacktop + mv fp, zero + push_fp zero + addi sp, sp, -16 + sd a2, 0(sp) + call efi_main + + /* Restore sp */ + ld sp, 0(sp) + ld ra, 16(sp) 0: addi sp, sp, 24 ret
@@ -172,6 +185,11 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data + +.balign 16384 +.space 16384 +stacktop: + dummy: .4byte 0 #define IMAGE_REL_ABSOLUTE 0
Signature · 1 line
2.44.0
PATCH v2 9/13 efi: Add support for obtaining the boot hartid
In reply to Andrew Jonesriscv needs to use an EFI protocol to get the boot hartid.
--- lib/efi.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
lib/efi.c +33 -0
@@ -29,6 +29,31 @@ extern int main(int argc, char **argv, char **envp);efi_system_table_t *efi_system_table =NULL ; +#ifdef __riscv +#define RISCV_EFI_BOOT_PROTOCOL_GUID EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf) + +unsigned long boot_hartid; + +struct riscv_efi_boot_protocol { +u64 revision; +efi_status_t (*get_boot_hartid)(struct riscv_efi_boot_protocol *, +unsigned long *boot_hartid); +}; + +static efi_status_t efi_get_boot_hartid (void ) +{ +efi_guid_t boot_protocol_guid =RISCV_EFI_BOOT_PROTOCOL_GUID ; +struct riscv_efi_boot_protocol *boot_protocol; +efi_status_t status; + + status =efi_bs_call (locate_protocol, &boot_protocol_guid,NULL , + (void **)&boot_protocol); +if (status !=EFI_SUCCESS ) +return status; +return efi_call_proto (boot_protocol, get_boot_hartid, &boot_hartid); +} +#endif +static void efi_free_pool (void *ptr) {efi_bs_call (free_pool, ptr);
@@ -421,6 +446,14 @@ efi_status_t efi_main(efi_handle_t handle, efi_system_table_t *sys_tab) goto efi_main_error; } +#ifdef __riscv + status =efi_get_boot_hartid (); +if (status !=EFI_SUCCESS ) { +printf ("Failed to get boot haritd\n" ); + goto efi_main_error; + } +#endif +/* * Exit EFI boot services, let kvm-unit-tests take full control of the * guest
Signature · 1 line
2.44.0
PATCH v2 10/13 riscv: Refactor setup code
In reply to Andrew JonesTo prepare for EFI setup, move code that will be shared into functions. This is the same type of code and the exact same function names which were created when refactoring Arm's EFI setup, so riscv setup is still following Arm's setup patterns.
--- lib/riscv/setup.c | 109 +++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 41 deletions(-)
lib/riscv/setup.c +68 -41
@@ -31,6 +31,8 @@#define MAX_DT_MEM_REGIONS 16#define NR_MEM_REGIONS (MAX_DT_MEM_REGIONS + 16) +extern unsigned long _etext; +char *initrd;u32 initrd_size;
@@ -81,25 +83,12 @@ static void cpu_init(void) cpu0_calls_idle = true; } -extern unsigned long _etext; - -static void mem_init (phys_addr_t freemem_start) +static void mem_allocator_init (phys_addr_t freemem_start,phys_addr_t freemem_end) { -struct mem_region *freemem, *code, *data; -phys_addr_t freemem_end, base, top; - -memregions_init (riscv_mem_regions,NR_MEM_REGIONS ); -memregions_add_dt_regions (MAX_DT_MEM_REGIONS ); +phys_addr_t base, top; -/* Split the region with the code into two regions; code and data */ -memregions_split ((unsigned long )&_etext, &code, &data); -assert (code); - code->flags |=MR_F_CODE ; - - freemem =memregions_find (freemem_start); -assert (freemem && !(freemem->flags & (MR_F_IO |MR_F_CODE ))); - - freemem_end = freemem->end &PAGE_MASK ; + freemem_start =PAGE_ALIGN (freemem_start); + freemem_end &=PAGE_MASK ;/* * The assert below is mostly checking that the free memory doesn't
@@ -129,6 +118,64 @@ static void mem_init(phys_addr_t freemem_start)page_alloc_ops_enable (); } +static void mem_init (phys_addr_t freemem_start) +{ +struct mem_region *freemem, *code, *data; + +memregions_init (riscv_mem_regions,NR_MEM_REGIONS ); +memregions_add_dt_regions (MAX_DT_MEM_REGIONS ); + +/* Split the region with the code into two regions; code and data */ +memregions_split ((unsigned long )&_etext, &code, &data); +assert (code); + code->flags |=MR_F_CODE ; + + freemem =memregions_find (freemem_start); +assert (freemem && !(freemem->flags & (MR_F_IO |MR_F_CODE ))); + +mem_allocator_init (freemem_start, freemem->end); +} + +static void freemem_push_fdt (void **freemem,const void *fdt) +{ +u32 fdt_size; +int ret; + + fdt_size =fdt_totalsize (fdt); + ret =fdt_move (fdt, *freemem, fdt_size); +assert (ret ==0 ); + ret =dt_init (*freemem); +assert (ret ==0 ); + *freemem += fdt_size; +} + +static void freemem_push_dt_initrd (void **freemem) +{ +const char *tmp; +int ret; + + ret =dt_get_initrd (&tmp, &initrd_size); +assert (ret ==0 || ret == -FDT_ERR_NOTFOUND ); +if (ret ==0 ) { + initrd = *freemem; +memmove (initrd, tmp, initrd_size); + *freemem += initrd_size; + } +} + +static void initrd_setup (void ) +{ +char *env; + +if (!initrd) +return ; + +/* environ is currently the only file in the initrd */ + env =malloc (initrd_size); +memcpy (env, initrd, initrd_size); +setup_env (env, initrd_size); +} +static void banner (void ) {puts ("\n" );
@@ -141,29 +188,14 @@ static void banner(void)void setup (const void *fdt,phys_addr_t freemem_start) {void *freemem; -const char *bootargs, *tmp; -u32 fdt_size; +const char *bootargs;int ret;assert (sizeof (long ) ==8 || freemem_start <VA_BASE ); freemem =__va (freemem_start); -/* Move the FDT to the base of free memory */ - fdt_size =fdt_totalsize (fdt); - ret =fdt_move (fdt, freemem, fdt_size); -assert (ret ==0 ); - ret =dt_init (freemem); -assert (ret ==0 ); - freemem += fdt_size; - -/* Move the initrd to the top of the FDT */ - ret =dt_get_initrd (&tmp, &initrd_size); -assert (ret ==0 || ret == -FDT_ERR_NOTFOUND ); -if (ret ==0 ) { - initrd = freemem; -memmove (initrd, tmp, initrd_size); - freemem += initrd_size; - } +freemem_push_fdt (&freemem, fdt); +freemem_push_dt_initrd (&freemem);mem_init (PAGE_ALIGN (__pa (freemem)));cpu_init ();
@@ -174,12 +206,7 @@ void setup(const void *fdt, phys_addr_t freemem_start)assert (ret ==0 || ret == -FDT_ERR_NOTFOUND );setup_args_progname (bootargs); -if (initrd) { -/* environ is currently the only file in the initrd */ -char *env =malloc (initrd_size); -memcpy (env, initrd, initrd_size); -setup_env (env, initrd_size); - } +initrd_setup ();if (!(auxinfo.flags &AUXINFO_MMU_OFF ))setup_vm ();
Signature · 1 line
2.44.0
PATCH v2 11/13 riscv: Enable EFI boot
In reply to Andrew JonesMimicking Arm's setup_efi() and duplicating some code from riscv's setup(), add the EFI setup code needed to boot unit tests from EFI- capable bootloaders. The selftest unit test can now be run with
qemu-system-riscv64 \
-nodefaults -nographic -serial mon:stdio \
-accel tcg -cpu max \
-machine virt,pflash0=pflash0 \
-blockdev node-name=pflash0,driver=file,read-only=on,filename=RISCV_VIRT_CODE.fd \
-smp 16 \
-kernel riscv/selftest.efi \
-initrd test-env \
-append 'selftest.efi foo bar baz' \
-machine acpi=offwhere test-env has the environment variables
$ cat test-env FOO=foo BAR=bar BAZ=baz
--- lib/riscv/asm/setup.h | 2 +- lib/riscv/setup.c | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-)
lib/riscv/asm/setup.h +1 -1
@@ -14,7 +14,7 @@ void setup(const void *fdt, phys_addr_t freemem_start);#ifdef CONFIG_EFI #include <efi.h> -static inline efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo) {return 0 ; } +efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo);#endif #endif /* _ASMRISCV_SETUP_H_ */
lib/riscv/setup.c +63 -0
66 lines not on this page · raw message
Signature · 1 line
2.44.0
PATCH v2 12/13 riscv: efi: Add run script
In reply to Andrew JonesAdapt Arm's efi run script to riscv. We can now run efi tests with run_tests.sh.
--- configure | 2 +- riscv/efi/run | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ riscv/run | 2 +- 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100755 riscv/efi/run
configure +1 -1
@@ -95,7 +95,7 @@ usage() { Select whether to run EFI tests directly with QEMU's -kernel option. When not enabled, tests will be placed in an EFI file system and run from the UEFI shell. Ignored when efi isn't enabled. - (arm64 only) + (arm64 and riscv64 only) EOF exit 1 }
riscv/efi/run new file +106 -0
106 lines not on this page · raw message
riscv/run +1 -1
@@ -33,7 +33,7 @@ command="$qemu -nodefaults -nographic -serial mon:stdio" command+=" $mach $acc $firmware -cpu $processor " command="$(migration_cmd) $(timeout_cmd) $command" -if [ "$EFI_RUN" = "y" ]; then +if [ "$UEFI_SHELL_RUN" = "y" ]; then ENVIRON_DEFAULT=n run_qemu_status $command "$@" else # We return the exit code via stdout, not via the QEMU return code
Signature · 1 line
2.44.0
PATCH v2 13/13 riscv: efi: Use efi-direct by default
In reply to Andrew Jonesefi-direct is much faster and riscv makes a lot of use of environment variables, which can't be as easily used without '-initrd'. When efi is configured for riscv, assume efi-direct is also desired. Users can switch to running from the UEFI shell by configuring with --disable-efi-direct.
--- configure | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
configure +6 -1
@@ -94,7 +94,8 @@ usage() { --[enable|disable]-efi-direct Select whether to run EFI tests directly with QEMU's -kernel option. When not enabled, tests will be placed in an EFI file - system and run from the UEFI shell. Ignored when efi isn't enabled. + system and run from the UEFI shell. Ignored when efi isn't enabled + and defaults to enabled when efi is enabled for riscv64. (arm64 and riscv64 only) EOF exit 1
@@ -237,6 +238,10 @@ if [ "$efi" ] && [ "$arch" != "x86_64" ] && usage fi +if [ "$efi" ] && [ "$arch" = "riscv64" ] && [ -z "$efi_direct" ]; then + efi_direct=y +fi + if [ -z "$page_size" ]; then if [ "$efi" = 'y' ] && [ "$arch" = "arm64" ]; then page_size="4096"
Signature · 1 line
2.44.0
Re: [kvm-unit-tests PATCH v2 04/13] treewide: lib/stack: Make base_address arch specific
In reply to Andrew JonesQuoted 8 lines · Andrew Jones
Calculating the offset of an address is image specific, which is architecture specific. Until now, all architectures and architecture configurations which select
CONFIG_RELOCwere able to subtract_etext, but the EFI configuration of riscv cannot (it must subtract ImageBase). Make this function weak, such that an architecture may override it when necessary, to accommodate the image layout. Then, immediately supply the riscv override.
Thanks for making these changes. Looks good. For the generic parts,
Thanks,
Nick
Quoted 85 lines
Signed-off-by Andrew Jones <[email protected]>--- lib/riscv/stack.c | 18 ++++++++++++++++++ lib/stack.c | 8 ++++---- lib/stack.h | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-)lib/riscv/stack.c +18
-0@@ -2,6 +2,24 @@#include <libcflat.h> #include <stack.h> +#ifdef CONFIG_RELOC +extern char ImageBase, _text, _etext; + +bool arch_base_address (const void *rebased_addr,unsigned long *addr) +{ +unsigned long ra = (unsigned long )rebased_addr; +unsigned long base = (unsigned long )&ImageBase; +unsigned long start = (unsigned long )&_text; +unsigned long end = (unsigned long )&_etext; + +if (ra < start || ra >= end) +return false; + + *addr = ra - base; +return true; +} +#endif +int arch_backtrace_frame (const void *frame,const void **return_addrs,int max_depth,bool current_frame) {lib/stack.c +4
-4@@ -14,7 +14,7 @@#ifdef CONFIG_RELOC extern char _text, _etext; -static bool base_address (const void *rebased_addr,unsigned long *addr) +bool __attribute__((weak))arch_base_address (const void *rebased_addr,unsigned long *addr) {unsigned long ra = (unsigned long )rebased_addr;unsigned long start = (unsigned long )&_text;@@ -27,7 +27,7 @@ static bool base_address(const void *rebased_addr, unsigned long *addr) return true; }#else -static bool base_address (const void *rebased_addr,unsigned long *addr) +bool __attribute__((weak))arch_base_address (const void *rebased_addr,unsigned long *addr) { *addr = (unsigned long )rebased_addr;return true;@@ -45,13 +45,13 @@ static void print_stack(const void **return_addrs, int depth,/* @addr indicates a non-return address, as expected by the stack * pretty printer script. */ if (depth >0 && !top_is_return_address) { -if (base_address (return_addrs[0 ], &addr)) +if (arch_base_address (return_addrs[0 ], &addr))printf (" @%lx" , addr); i++; }for (; i < depth; i++) { -if (base_address (return_addrs[i], &addr)) +if (arch_base_address (return_addrs[i], &addr))printf (" %lx" , addr); }printf ("\n" );lib/stack.h +2
-0@@ -34,4 +34,6 @@ static inline int backtrace_frame(const void *frame, const void **return_addrs, }#endif +bool __attribute__((weak))arch_base_address (const void *rebased_addr,unsigned long *addr); +#endif
Quoted 18 lines · Andrew Jones
This series starts with some fixes for backtraces for bugs found when tracing with riscv EFI builds. The series then brings EFI support to riscv, basing the approach heavily on arm64's support (including arm64's improvements[1]). It should now be possible to launch tests from EFI-capable bootloaders.
[1] https://lore.kernel.org/all/20240305164623.379149-20-andrew.jones at linux.dev/
v2:
- Rebase on v3 of arm's efi improvement series
- Just makebase_addressa weak function rather than duplicate it
- Always preserve .so files (they're useful for debug)
- Build the sieve test for EFI
- Pick up a couple tagsThanks,
drew
Quoted 15 lines
Andrew Jones (13):
riscv: Call abort instead of assert on unhandled exceptions
riscv:show_regs: Prepare for EFI images
treewide: lib/stack: Fix backtrace
treewide: lib/stack: Makebase_addressarch specific
riscv: Import gnu-efi files
riscv: Tweak the gnu-efi imported code
riscv: Enable building for EFI
riscv: efi: Switch stack in_start
efi: Add support for obtaining the boot hartid
riscv: Refactor setup code
riscv: Enable EFI boot
riscv: efi: Add run script
riscv: efi: Use efi-direct by default
Merged.
Thanks,
drew