Re: Make kernel self-relocatable [PATCH]
Emmanuel Dreyfus <[email protected]> Tue, 2 May 2023 00:41:09 +0000
| Newsgroups | gmane.os.netbsd.ports.x86-64 |
|---|---|
| Message-ID | <[email protected]> |
On Tue, May 02, 2023 at 12:39:44AM +0000, Emmanuel Dreyfus wrote: > Here is an additional change to add a SELFRELOC kernel option, enabled > by defaut, for the sake of documentation clarity. > > Instead of telling that x86/boot(8) reloc command needs a kernel able > to self relocate, we can tell it needs a kernel built with the > SELFRELOC option. This keeps the reader from wondering what could > make a kernel able to self relocate. And here is the missing patch Index: share/man/man4/options.4 =================================================================== RCS file: /cvsroot/src/share/man/man4/options.4,v retrieving revision 1.523 diff -U4 -r1.523 options.4 --- share/man/man4/options.4 28 Aug 2022 14:29:05 -0000 1.523 +++ share/man/man4/options.4 2 May 2023 00:24:48 -0000 @@ -1306,8 +1306,16 @@ .Xr vnd 4 and .Xr vnconfig 8 for more information. +.It Cd options SELFRELOC +Make the kernel able to self relocate at bootstrap, so that it can +run whatever its load address is. +This is intented to be used withe the +.Ic reloc +boostrap command documented in +.Xr x86/boot 8 , +to workaround UEFI bugs, and is only available on amd64. .It Cd options SPLDEBUG Help the kernel programmer find bugs related to the interrupt priority level. When Index: sys/arch/amd64/amd64/locore.S =================================================================== RCS file: /cvsroot/src/sys/arch/amd64/amd64/locore.S,v retrieving revision 1.219 diff -U4 -r1.219 locore.S --- sys/arch/amd64/amd64/locore.S 20 Apr 2023 00:42:23 -0000 1.219 +++ sys/arch/amd64/amd64/locore.S 2 May 2023 00:24:48 -0000 @@ -155,8 +155,9 @@ #include "opt_ddb.h" #include "opt_ddbparam.h" #include "opt_modular.h" #include "opt_realmem.h" +#include "opt_selfreloc.h" #include "opt_compat_netbsd.h" #include "opt_compat_netbsd32.h" #include "opt_xen.h" @@ -455,15 +456,17 @@ ENTRY(start) #ifndef XENPV .code32 +#ifdef SELFRELOC 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 +#endif /* SELFRELOC */ /* Warm boot */ movw $0x1234,0x472 @@ -1765,8 +1768,9 @@ LABEL(nomds_leave) NOMDS_LEAVE LABEL(nomds_leave_end) +#ifdef SELFRELOC /* * selfreloc(loadddr edi) * This is adapted from sys/arch/i386/i386/locore.S */ @@ -1899,4 +1903,5 @@ .word gdtr - gdt gdtrr: .quad END(selfreloc_start) +#endif /* SELFRELOC */ Index: sys/arch/amd64/conf/files.amd64 =================================================================== RCS file: /cvsroot/src/sys/arch/amd64/conf/files.amd64,v retrieving revision 1.120 diff -U4 -r1.120 files.amd64 --- sys/arch/amd64/conf/files.amd64 21 Oct 2020 13:31:51 -0000 1.120 +++ sys/arch/amd64/conf/files.amd64 2 May 2023 00:24:48 -0000 @@ -19,8 +19,11 @@ # Enable GCC spectre V2 mitigation options defflag opt_spectre.h SPECTRE_V2_GCC_MITIGATION +# Enable kernel self-relocation at bootstrap +defflag opt_selfreloc.h SELFRELOC + # # XXX these are just here at the moment so that we can share files # with the i386 (they include the opt_*.h for these) # Index: sys/arch/amd64/conf/std.amd64 =================================================================== RCS file: /cvsroot/src/sys/arch/amd64/conf/std.amd64,v retrieving revision 1.12 diff -U4 -r1.12 std.amd64 --- sys/arch/amd64/conf/std.amd64 25 Apr 2020 15:26:16 -0000 1.12 +++ sys/arch/amd64/conf/std.amd64 2 May 2023 00:24:48 -0000 @@ -14,8 +14,10 @@ options CHILD_MAX=1024 # 160 is too few options OPEN_MAX=1024 # 128 is too few +options SELFRELOC + mainbus0 at root cpu* at mainbus? ioapic* at mainbus? apid ? Index: share/man/man8/man8.x86/boot.8 =================================================================== RCS file: /cvsroot/src/share/man/man8/man8.x86/boot.8,v retrieving revision 1.27 diff -U4 -r1.27 boot.8 --- share/man/man8/man8.x86/boot.8 24 Apr 2023 13:55:45 -0000 1.27 +++ share/man/man8/man8.x86/boot.8 2 May 2023 00:24:48 -0000 @@ -654,10 +654,12 @@ Reboot the system. .It Ic reloc Op Va default No \(or Va none No \(or Va address [Only UEFI boot] Sets where the kernel is copied by bootstrap before it is started. Values other than default require a kernel -that can relocate itself at the right address, otherwise a crash -occurs at boot time. +built with the +.Cd SELFRELOC +option, so that can relocate itself at the right address, +otherwise a crash occurs at boot time. .Bl -tag -width default .It Va default Copy the kernel at ELF header load address, this is the historical behavior. -- Emmanuel Dreyfus [email protected]