diff options
| author | manu <manu@NetBSD.org> | 2023-04-20 00:42:23 +0000 |
|---|---|---|
| committer | manu <manu@NetBSD.org> | 2023-04-20 00:42:23 +0000 |
| commit | 8e8d3674e86d2eda55d877e07afabaf2f50a3c06 (patch) | |
| tree | eeb8efa69cf53041b750b75fd09a09f45c25f14e /sys | |
| parent | 5e3074af0614bf6a8d6a185ba6773e8277acd887 (diff) | |
Add reloc keyworkd to let EFI bootstrap load amd64 kernel at any address
EFI bootstrap assumes it can copy the amd64 kernel to its ELF load
address (that is KERNTEXTOFF - KERNBASE = 0x200000), but it can
clash with previous UEFI memory allocation, as described here:
http://mail-index.netbsd.org/tech-kern/2023/04/07/msg028833.html
This change adds a reloc keyword for controling where the EFI
boostrap will copy the kernel image. Possible values are:
default - the default and prior behavior, copy at 0x200000.
none - do not copy and use the kernel image where it was loaded.
address - specify an explicit address where to copy the kernel.
This comes with an amd64 kernel patch that makes it self-relocatable.
It first discover where it was loaded in memory, and if this is
different than the expected 0x200000, hhe the kernel relocates
itself and start over at the right address.
Diffstat (limited to 'sys')
| -rw-r--r-- | sys/arch/amd64/amd64/locore.S | 145 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/boot.c | 47 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/bootia32/efibootia32.c | 4 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/bootia32/startprog32.S | 7 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/bootx64/efibootx64.c | 4 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/bootx64/startprog64.S | 6 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/efiboot.c | 6 | ||||
| -rw-r--r-- | sys/arch/i386/stand/efiboot/efiboot.h | 10 | ||||
| -rw-r--r-- | sys/arch/i386/stand/lib/exec.c | 23 |
9 files changed, 238 insertions, 14 deletions
diff --git a/sys/arch/amd64/amd64/locore.S b/sys/arch/amd64/amd64/locore.S index 08aa9d549e0..1771349f0e7 100644 --- a/sys/arch/amd64/amd64/locore.S +++ b/sys/arch/amd64/amd64/locore.S @@ -1,4 +1,4 @@ -/* $NetBSD: locore.S,v 1.218 2023/03/03 14:32:48 riastradh Exp $ */ +/* $NetBSD: locore.S,v 1.219 2023/04/20 00:42:23 manu Exp $ */ /* * Copyright-o-rama! @@ -456,6 +456,14 @@ ENTRY(start) #ifndef XENPV .code32 + call next +next: pop %edi + sub $(next - kernel_text), %edi + + /* If not KERNBASE, reloc ourselves to KERNBASE */ + cmpl $(KERNTEXTOFF_LO - KERNBASE_LO), %edi + jne selfreloc_start + /* Warm boot */ movw $0x1234,0x472 @@ -1757,3 +1765,138 @@ LABEL(mds_leave_end) LABEL(nomds_leave) NOMDS_LEAVE LABEL(nomds_leave_end) + +/* + * selfreloc(loadddr edi) + * This is adapted from sys/arch/i386/i386/locore.S + */ + .code32 +ENTRY(selfreloc_start) + movl %edi, %ebx /* loadaddr saved in ebx */ + movl %edi, %esi /* src */ + movl $_RELOC(kernel_text), %edi /* dest */ + movl 16(%esp),%ecx /* esym */ + subl $_RELOC(kernel_text), %ecx /* size */ + +#if defined(NO_OVERLAP) + movl %ecx, %eax +#else + movl %edi, %eax + subl %esi, %eax + cmpl %ecx, %eax /* overlapping? */ + movl %ecx, %eax + jb .Lbackwards +#endif + /* nope, copy forwards. */ + shrl $2, %ecx /* copy by words */ + rep + movsl + and $3, %eax /* any bytes left? */ + jnz .Ltrailing + jmp .Lcopy_done + +.Ltrailing: + cmp $2, %eax + jb 11f + movw (%esi), %ax + movw %ax, (%edi) + je .Lcopy_done + movb 2(%esi), %al + movb %al, 2(%edi) + jmp .Lcopy_done +11: movb (%esi), %al + movb %al, (%edi) + jmp .Lcopy_done + +#if !defined(NO_OVERLAP) +.Lbackwards: + addl %ecx, %edi /* copy backwards. */ + addl %ecx, %esi + and $3, %eax /* any fractional bytes? */ + jnz .Lback_align +.Lback_aligned: + shrl $2, %ecx + subl $4, %esi + subl $4, %edi + std + rep + movsl + cld + jmp .Lcopy_done + +.Lback_align: + sub %eax, %esi + sub %eax, %edi + cmp $2, %eax + jb 11f + je 12f + movb 2(%esi), %al + movb %al, 2(%edi) +12: movw (%esi), %ax + movw %ax, (%edi) + jmp .Lback_aligned +11: movb (%esi), %al + movb %al, (%edi) + jmp .Lback_aligned +#endif + /* End of copy kernel */ +.Lcopy_done: + cld /* LynxOS depends on it */ + + /* load current selfreloc_start addesss in $edi */ + movl %ebx, %edi /* loadaddr was saved in ebx */ + addl $(selfreloc_start - kernel_text), %edi + + /* Prepare jump address */ + lea (selfreloc_start32a - selfreloc_start)(%edi), %eax + movl %eax, (selfreloc_start32r - selfreloc_start)(%edi) + + /* Setup GDT */ + lea (gdt - selfreloc_start)(%edi), %eax + mov %eax, (gdtrr - selfreloc_start)(%edi) + lgdt (gdtr - selfreloc_start)(%edi) + + /* Jump to set %cs */ + ljmp *(selfreloc_start32r - selfreloc_start)(%edi) + + .align 4 +selfreloc_start32a: + movl $0x10, %eax /* #define DATA_SEGMENT 0x10 */ + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs + movw %ax, %ss + + /* Disable Paging in CR0 */ + movl %cr0, %eax + andl $(~CR0_PG), %eax + movl %eax, %cr0 + + /* Disable PAE in CR4 */ + movl %cr4, %eax + andl $(~CR4_PAE), %eax + movl %eax, %cr4 + + jmp selfreloc_start32b + + .align 4 +selfreloc_start32b: + xor %eax, %eax + movl $_RELOC(start), %esi + jmp *%esi + + .align 16 +selfreloc_start32r: + .long 0 + .long 0x08 /* #define CODE_SEGMENT 0x08 */ + .align 16 +gdt: + .long 0, 0 + .byte 0xff, 0xff, 0x00, 0x00, 0x00, 0x9f, 0xcf, 0x00 + .byte 0xff, 0xff, 0x00, 0x00, 0x00, 0x93, 0xcf, 0x00 +gdtr: + .word gdtr - gdt +gdtrr: + .quad +END(selfreloc_start) diff --git a/sys/arch/i386/stand/efiboot/boot.c b/sys/arch/i386/stand/efiboot/boot.c index e5f467c08a0..3978a4c40e4 100644 --- a/sys/arch/i386/stand/efiboot/boot.c +++ b/sys/arch/i386/stand/efiboot/boot.c @@ -1,4 +1,4 @@ -/* $NetBSD: boot.c,v 1.21 2022/06/08 21:43:45 wiz Exp $ */ +/* $NetBSD: boot.c,v 1.22 2023/04/20 00:42:24 manu Exp $ */ /*- * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org> @@ -83,6 +83,7 @@ void command_menu(char *); #endif void command_modules(char *); void command_multiboot(char *); +void command_reloc(char *); void command_text(char *); void command_version(char *); @@ -109,6 +110,7 @@ const struct bootblk_command commands[] = { #endif { "modules", command_modules }, { "multiboot", command_multiboot }, + { "reloc", command_reloc }, { "rndseed", rnd_add }, { "splash", splash_add }, { "text", command_text }, @@ -406,6 +408,7 @@ command_help(char *arg) #endif "modules {on|off|enabled|disabled}\n" "multiboot [dev:][filename] [<args>]\n" + "reloc {address|none|default}\n" "rndseed {path_to_rndseed_file}\n" "splash {path_to_image_file}\n" "text [{modenum|list}]\n" @@ -641,6 +644,48 @@ command_multiboot(char *arg) } void +command_reloc(char *arg) +{ + char *ep; + + if (*arg == '\0') { + switch (efi_reloc_type) { + case RELOC_NONE: + printf("reloc: none\n"); + break; + case RELOC_ADDR: + printf("reloc: %p\n", (void *)efi_kernel_reloc); + break; + case RELOC_DEFAULT: + default: + printf("reloc: default\n"); + break; + } + goto out; + } + + if (strcmp(arg, "default") == 0) { + efi_reloc_type = RELOC_DEFAULT; + goto out; + } + + if (strcmp(arg, "none") == 0) { + efi_reloc_type = RELOC_NONE; + goto out; + } + + errno = 0; + efi_kernel_reloc = strtoul(arg, &ep, 0); + if (ep == arg || *ep != '\0' || errno) + printf("could not parse address \"%s\"\n", arg); + else + efi_reloc_type = RELOC_ADDR; +out: + return; + +} + +void command_version(char *arg) { CHAR16 *path; diff --git a/sys/arch/i386/stand/efiboot/bootia32/efibootia32.c b/sys/arch/i386/stand/efiboot/bootia32/efibootia32.c index 8677ac963dd..57ac7be8084 100644 --- a/sys/arch/i386/stand/efiboot/bootia32/efibootia32.c +++ b/sys/arch/i386/stand/efiboot/bootia32/efibootia32.c @@ -1,4 +1,4 @@ -/* $NetBSD: efibootia32.c,v 1.5 2019/09/13 02:19:45 manu Exp $ */ +/* $NetBSD: efibootia32.c,v 1.6 2023/04/20 00:42:24 manu Exp $ */ /*- * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org> @@ -76,7 +76,7 @@ startprog(physaddr_t entry, uint32_t argc, uint32_t *argv, physaddr_t sp) (*startprog32)(entry, argc, argv, (physaddr_t)startprog32 + startprog32_size, - efi_kernel_start, efi_kernel_start + efi_loadaddr, + efi_kernel_start, efi_load_start, efi_kernel_size, startprog32); } diff --git a/sys/arch/i386/stand/efiboot/bootia32/startprog32.S b/sys/arch/i386/stand/efiboot/bootia32/startprog32.S index 99f7632c567..51ef1322c36 100644 --- a/sys/arch/i386/stand/efiboot/bootia32/startprog32.S +++ b/sys/arch/i386/stand/efiboot/bootia32/startprog32.S @@ -1,4 +1,4 @@ -/* $NetBSD: startprog32.S,v 1.2 2017/02/24 12:24:25 nonaka Exp $ */ +/* $NetBSD: startprog32.S,v 1.3 2023/04/20 00:42:24 manu Exp $ */ /* NetBSD: startprog.S,v 1.4 2016/12/04 08:21:08 maxv Exp */ /* @@ -117,6 +117,11 @@ start: movl 24(%ebp), %edi /* dest */ movl 28(%ebp), %esi /* src */ movl 32(%ebp), %ecx /* size */ + + /* skip copy if same source and destination */ + cmpl %edi,%esi + jz .Lcopy_done + #if defined(NO_OVERLAP) movl %ecx, %eax #else diff --git a/sys/arch/i386/stand/efiboot/bootx64/efibootx64.c b/sys/arch/i386/stand/efiboot/bootx64/efibootx64.c index e5a2ee21b5c..2fdc8e959a3 100644 --- a/sys/arch/i386/stand/efiboot/bootx64/efibootx64.c +++ b/sys/arch/i386/stand/efiboot/bootx64/efibootx64.c @@ -1,4 +1,4 @@ -/* $NetBSD: efibootx64.c,v 1.5 2019/09/13 02:19:46 manu Exp $ */ +/* $NetBSD: efibootx64.c,v 1.6 2023/04/20 00:42:24 manu Exp $ */ /*- * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org> @@ -80,7 +80,7 @@ startprog(physaddr_t entry, uint32_t argc, uint32_t *argv, physaddr_t sp) memcpy(newsp, argv, sizeof(*argv) * argc); } - (*startprog64)(efi_kernel_start, efi_kernel_start + efi_loadaddr, + (*startprog64)(efi_kernel_start, efi_load_start, (physaddr_t)newsp, efi_kernel_size, startprog64, entry); } diff --git a/sys/arch/i386/stand/efiboot/bootx64/startprog64.S b/sys/arch/i386/stand/efiboot/bootx64/startprog64.S index 6c4f9dcac29..9a1236fd5ca 100644 --- a/sys/arch/i386/stand/efiboot/bootx64/startprog64.S +++ b/sys/arch/i386/stand/efiboot/bootx64/startprog64.S @@ -1,4 +1,4 @@ -/* $NetBSD: startprog64.S,v 1.3 2017/02/11 10:23:39 nonaka Exp $ */ +/* $NetBSD: startprog64.S,v 1.4 2023/04/20 00:42:24 manu Exp $ */ /* NetBSD: startprog.S,v 1.3 2003/02/01 14:48:18 dsl Exp */ /* starts program in protected mode / flat space @@ -97,6 +97,10 @@ start: cli + /* skip copy if same source and destination */ + cmpq %rdi,%rsi + jz .Lcopy_done + /* Copy kernel */ mov %rcx, %r12 /* original kernel size */ movq %rdi, %r11 /* for misaligned check */ diff --git a/sys/arch/i386/stand/efiboot/efiboot.c b/sys/arch/i386/stand/efiboot/efiboot.c index f13ede13d9c..7c6b659bb1c 100644 --- a/sys/arch/i386/stand/efiboot/efiboot.c +++ b/sys/arch/i386/stand/efiboot/efiboot.c @@ -1,4 +1,4 @@ -/* $NetBSD: efiboot.c,v 1.12 2020/02/09 12:13:39 jmcneill Exp $ */ +/* $NetBSD: efiboot.c,v 1.13 2023/04/20 00:42:24 manu Exp $ */ /*- * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org> @@ -36,7 +36,9 @@ EFI_DEVICE_PATH *efi_bootdp; enum efi_boot_device_type efi_bootdp_type = BOOT_DEVICE_TYPE_HD; EFI_LOADED_IMAGE *efi_li; uintptr_t efi_main_sp; -physaddr_t efi_loadaddr, efi_kernel_start; +physaddr_t efi_loadaddr, efi_kernel_start, efi_load_start; +physaddr_t efi_kernel_reloc = 0; +enum efi_reloc_type efi_reloc_type = RELOC_DEFAULT; u_long efi_kernel_size; bool efi_cleanuped; struct btinfo_efimemmap *btinfo_efimemmap = NULL; diff --git a/sys/arch/i386/stand/efiboot/efiboot.h b/sys/arch/i386/stand/efiboot/efiboot.h index 50cfc53e3ed..07c8b25605e 100644 --- a/sys/arch/i386/stand/efiboot/efiboot.h +++ b/sys/arch/i386/stand/efiboot/efiboot.h @@ -1,4 +1,4 @@ -/* $NetBSD: efiboot.h,v 1.11 2021/09/07 11:41:31 nia Exp $ */ +/* $NetBSD: efiboot.h,v 1.12 2023/04/20 00:42:24 manu Exp $ */ /*- * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org> @@ -52,7 +52,13 @@ extern enum efi_boot_device_type { } efi_bootdp_type; extern EFI_LOADED_IMAGE *efi_li; extern uintptr_t efi_main_sp; -extern physaddr_t efi_loadaddr, efi_kernel_start; +extern physaddr_t efi_loadaddr, efi_kernel_start, efi_load_start; +extern physaddr_t efi_kernel_reloc; +extern enum efi_reloc_type { + RELOC_DEFAULT, + RELOC_NONE, + RELOC_ADDR, +} efi_reloc_type; extern u_long efi_kernel_size; extern bool efi_cleanuped; void efi_cleanup(void); diff --git a/sys/arch/i386/stand/lib/exec.c b/sys/arch/i386/stand/lib/exec.c index 244b5f40a7a..ae442f86f14 100644 --- a/sys/arch/i386/stand/lib/exec.c +++ b/sys/arch/i386/stand/lib/exec.c @@ -1,4 +1,4 @@ -/* $NetBSD: exec.c,v 1.78 2022/09/21 14:29:45 riastradh Exp $ */ +/* $NetBSD: exec.c,v 1.79 2023/04/20 00:42:24 manu Exp $ */ /* * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -465,6 +465,7 @@ exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy, struct btinfo_symtab btinfo_symtab; u_long extmem; u_long basemem; + u_long entry; int error; #ifdef EFIBOOT int i; @@ -497,6 +498,8 @@ exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy, goto out; } #ifdef EFIBOOT + efi_load_start = marks[MARK_START]; + /* adjust to the real load address */ marks[MARK_START] -= efi_loadaddr; marks[MARK_ENTRY] -= efi_loadaddr; @@ -552,6 +555,8 @@ exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy, if (callback != NULL) (*callback)(); + + entry = marks[MARK_ENTRY]; #ifdef EFIBOOT /* Copy bootinfo to safe arena. */ for (i = 0; i < bootinfo->nentries; i++) { @@ -563,8 +568,22 @@ exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy, efi_kernel_start = marks[MARK_START]; efi_kernel_size = image_end - (efi_loadaddr + efi_kernel_start); + + switch (efi_reloc_type) { + case RELOC_NONE: + entry += (efi_load_start - efi_kernel_start); + efi_kernel_start = efi_load_start; + break; + case RELOC_ADDR: + entry += (efi_kernel_reloc - efi_kernel_start); + efi_kernel_start = efi_kernel_reloc; + break; + case RELOC_DEFAULT: + default: + break; + } #endif - startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv, + startprog(entry, BOOT_NARGS, boot_argv, x86_trunc_page(basemem * 1024)); panic("exec returned"); |
