Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/64s: Make prom_init require RELOCATABLE

When we boot from open firmware (OF) using PPC_OF_BOOT_TRAMPOLINE, aka.
prom_init, we run parts of the kernel at an address other than the link
address. That happens because OF loads the kernel above zero (OF is at
zero) and we run prom_init before copying the kernel down to zero.

Currently that works even for non-relocatable kernels, because we do
various fixups to the prom_init code to make it run where it's loaded.

However those fixups are not sufficient if the kernel becomes large
enough. In that case prom_init()'s final call to __start() can end up
generating a plt branch:

bl c000000002000018 <00000078.plt_branch.__start>

That results in the kernel jumping to the linked address of __start,
0xc000000000000000, when really it needs to jump to the
0xc000000000000000 + the runtime address because the kernel is still
running at the load address.

We could do further shenanigans to handle that, see Jordan's patch for
example:
https://lore.kernel.org/linuxppc-dev/20210421021721.1539289-1-jniethe5@gmail.com

However it is much simpler to just require a kernel with prom_init() to
be built relocatable. The result works in all configurations without
further work, and requires less code.

This should have no effect on most people, as our defconfigs and
essentially all distro configs already have RELOCATABLE enabled.

Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20210623130454.2542945-1-mpe@ellerman.id.au

+3 -56
+2 -56
arch/powerpc/kernel/prom_init.c
··· 3243 3243 #endif /* CONFIG_BLK_DEV_INITRD */ 3244 3244 } 3245 3245 3246 - #ifdef CONFIG_PPC64 3247 - #ifdef CONFIG_RELOCATABLE 3248 - static void reloc_toc(void) 3249 - { 3250 - } 3251 - 3252 - static void unreloc_toc(void) 3253 - { 3254 - } 3255 - #else 3256 - static void __reloc_toc(unsigned long offset, unsigned long nr_entries) 3257 - { 3258 - unsigned long i; 3259 - unsigned long *toc_entry; 3260 - 3261 - /* Get the start of the TOC by using r2 directly. */ 3262 - asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry)); 3263 - 3264 - for (i = 0; i < nr_entries; i++) { 3265 - *toc_entry = *toc_entry + offset; 3266 - toc_entry++; 3267 - } 3268 - } 3269 - 3270 - static void reloc_toc(void) 3271 - { 3272 - unsigned long offset = reloc_offset(); 3273 - unsigned long nr_entries = 3274 - (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long); 3275 - 3276 - __reloc_toc(offset, nr_entries); 3277 - 3278 - mb(); 3279 - } 3280 - 3281 - static void unreloc_toc(void) 3282 - { 3283 - unsigned long offset = reloc_offset(); 3284 - unsigned long nr_entries = 3285 - (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long); 3286 - 3287 - mb(); 3288 - 3289 - __reloc_toc(-offset, nr_entries); 3290 - } 3291 - #endif 3292 - #endif 3293 - 3294 3246 #ifdef CONFIG_PPC_SVM 3295 3247 /* 3296 3248 * Perform the Enter Secure Mode ultracall. ··· 3276 3324 * relocated it so the check will fail. Restore the original image by 3277 3325 * relocating it back to the kernel virtual base address. 3278 3326 */ 3279 - if (IS_ENABLED(CONFIG_RELOCATABLE)) 3280 - relocate(KERNELBASE); 3327 + relocate(KERNELBASE); 3281 3328 3282 3329 ret = enter_secure_mode(kbase, fdt); 3283 3330 3284 3331 /* Relocate the kernel again. */ 3285 - if (IS_ENABLED(CONFIG_RELOCATABLE)) 3286 - relocate(kbase); 3332 + relocate(kbase); 3287 3333 3288 3334 if (ret != U_SUCCESS) { 3289 3335 prom_printf("Returned %d from switching to secure mode.\n", ret); ··· 3309 3359 #ifdef CONFIG_PPC32 3310 3360 unsigned long offset = reloc_offset(); 3311 3361 reloc_got2(offset); 3312 - #else 3313 - reloc_toc(); 3314 3362 #endif 3315 3363 3316 3364 /* ··· 3485 3537 3486 3538 #ifdef CONFIG_PPC32 3487 3539 reloc_got2(-offset); 3488 - #else 3489 - unreloc_toc(); 3490 3540 #endif 3491 3541 3492 3542 /* Move to secure memory if we're supposed to be secure guests. */
+1
arch/powerpc/platforms/Kconfig
··· 51 51 config PPC_OF_BOOT_TRAMPOLINE 52 52 bool "Support booting from Open Firmware or yaboot" 53 53 depends on PPC_BOOK3S_32 || PPC64 54 + select RELOCATABLE if PPC64 54 55 default y 55 56 help 56 57 Support from booting from Open Firmware or yaboot using an