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

powerpc: Relocate prom_init.c on 64bit

The ppc64 kernel can get loaded at any address which means
our very early init code in prom_init.c must be relocatable. We do
this with a pretty nasty RELOC() macro that we wrap accesses of
variables with. It is very fragile and sometimes we forget to add a
RELOC() to an uncommon path or sometimes a compiler change breaks it.

32bit has a much more elegant solution where we build prom_init.c
with -mrelocatable and then process the relocations manually.
Unfortunately we can't do the equivalent on 64bit and we would
have to build the entire kernel relocatable (-pie), resulting in a
large increase in kernel footprint (megabytes of relocation data).
The relocation data will be marked __initdata but it still creates
more pressure on our already tight memory layout at boot.

Alan Modra pointed out that the 64bit ABI is relocatable even
if we don't build with -pie, we just need to relocate the TOC.
This patch implements that idea and relocates the TOC entries of
prom_init.c. An added bonus is there are very few relocations to
process which helps keep boot times on simulators down.

gcc does not put 64bit integer constants into the TOC but to be
safe we may want a build time script which passes through the
prom_init.c TOC entries to make sure everything looks reasonable.

Signed-off-by: Anton Blanchard <anton@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>

authored by

Anton Blanchard and committed by
Benjamin Herrenschmidt
5ac47f7a e95e09ce

+68 -15
+1
arch/powerpc/Makefile
··· 136 136 head-$(CONFIG_PPC64) += arch/powerpc/kernel/entry_64.o 137 137 head-$(CONFIG_PPC_FPU) += arch/powerpc/kernel/fpu.o 138 138 head-$(CONFIG_ALTIVEC) += arch/powerpc/kernel/vector.o 139 + head-$(CONFIG_PPC_OF_BOOT_TRAMPOLINE) += arch/powerpc/kernel/prom_init.o 139 140 140 141 core-y += arch/powerpc/kernel/ \ 141 142 arch/powerpc/mm/ \
+3
arch/powerpc/include/asm/sections.h
··· 10 10 11 11 extern char __end_interrupts[]; 12 12 13 + extern char __prom_init_toc_start[]; 14 + extern char __prom_init_toc_end[]; 15 + 13 16 static inline int in_kernel_text(unsigned long addr) 14 17 { 15 18 if (addr >= (unsigned long)_stext && addr < (unsigned long)__init_end)
+1 -1
arch/powerpc/kernel/Makefile
··· 91 91 obj-$(CONFIG_PPC32) += entry_32.o setup_32.o 92 92 obj-$(CONFIG_PPC64) += dma-iommu.o iommu.o 93 93 obj-$(CONFIG_KGDB) += kgdb.o 94 - obj-$(CONFIG_PPC_OF_BOOT_TRAMPOLINE) += prom_init.o 95 94 obj-$(CONFIG_MODULES) += ppc_ksyms.o 96 95 obj-$(CONFIG_BOOTX_TEXT) += btext.o 97 96 obj-$(CONFIG_SMP) += smp.o ··· 141 142 extra-$(CONFIG_PPC_FPU) += fpu.o 142 143 extra-$(CONFIG_ALTIVEC) += vector.o 143 144 extra-$(CONFIG_PPC64) += entry_64.o 145 + extra-$(CONFIG_PPC_OF_BOOT_TRAMPOLINE) += prom_init.o 144 146 145 147 extra-y += systbl_chk.i 146 148 $(obj)/systbl.o: systbl_chk
+57 -13
arch/powerpc/kernel/prom_init.c
··· 66 66 * is running at whatever address it has been loaded at. 67 67 * On ppc32 we compile with -mrelocatable, which means that references 68 68 * to extern and static variables get relocated automatically. 69 - * On ppc64 we have to relocate the references explicitly with 70 - * RELOC. (Note that strings count as static variables.) 69 + * ppc64 objects are always relocatable, we just need to relocate the 70 + * TOC. 71 71 * 72 72 * Because OF may have mapped I/O devices into the area starting at 73 73 * KERNELBASE, particularly on CHRP machines, we can't safely call ··· 79 79 * On ppc64, 64 bit values are truncated to 32 bits (and 80 80 * fortunately don't get interpreted as two arguments). 81 81 */ 82 + #define RELOC(x) (x) 83 + #define ADDR(x) (u32)(unsigned long)(x) 84 + 82 85 #ifdef CONFIG_PPC64 83 - #define RELOC(x) (*PTRRELOC(&(x))) 84 - #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x)) 85 86 #define OF_WORKAROUNDS 0 86 87 #else 87 - #define RELOC(x) (x) 88 - #define ADDR(x) (u32) (x) 89 88 #define OF_WORKAROUNDS of_workarounds 90 89 int of_workarounds; 91 90 #endif ··· 333 334 struct prom_t *_prom = &RELOC(prom); 334 335 335 336 va_start(args, format); 336 - #ifdef CONFIG_PPC64 337 - format = PTRRELOC(format); 338 - #endif 339 337 for (p = format; *p != 0; p = q) { 340 338 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q) 341 339 ; ··· 433 437 434 438 static void __init __attribute__((noreturn)) prom_panic(const char *reason) 435 439 { 436 - #ifdef CONFIG_PPC64 437 - reason = PTRRELOC(reason); 438 - #endif 439 440 prom_print(reason); 440 441 /* Do not call exit because it clears the screen on pmac 441 442 * it also causes some sort of double-fault on early pmacs */ ··· 922 929 * (we assume this is the same for all cores) and use it to 923 930 * divide NR_CPUS. 924 931 */ 925 - cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]); 932 + cores = (u32 *)&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]; 926 933 if (*cores != NR_CPUS) { 927 934 prom_printf("WARNING ! " 928 935 "ibm_architecture_vec structure inconsistent: %lu!\n", ··· 2843 2850 #endif /* CONFIG_BLK_DEV_INITRD */ 2844 2851 } 2845 2852 2853 + #ifdef CONFIG_PPC64 2854 + #ifdef CONFIG_RELOCATABLE 2855 + static void reloc_toc(void) 2856 + { 2857 + } 2858 + 2859 + static void unreloc_toc(void) 2860 + { 2861 + } 2862 + #else 2863 + static void __reloc_toc(void *tocstart, unsigned long offset, 2864 + unsigned long nr_entries) 2865 + { 2866 + unsigned long i; 2867 + unsigned long *toc_entry = (unsigned long *)tocstart; 2868 + 2869 + for (i = 0; i < nr_entries; i++) { 2870 + *toc_entry = *toc_entry + offset; 2871 + toc_entry++; 2872 + } 2873 + } 2874 + 2875 + static void reloc_toc(void) 2876 + { 2877 + unsigned long offset = reloc_offset(); 2878 + unsigned long nr_entries = 2879 + (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long); 2880 + 2881 + /* Need to add offset to get at __prom_init_toc_start */ 2882 + __reloc_toc(__prom_init_toc_start + offset, offset, nr_entries); 2883 + 2884 + mb(); 2885 + } 2886 + 2887 + static void unreloc_toc(void) 2888 + { 2889 + unsigned long offset = reloc_offset(); 2890 + unsigned long nr_entries = 2891 + (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long); 2892 + 2893 + mb(); 2894 + 2895 + /* __prom_init_toc_start has been relocated, no need to add offset */ 2896 + __reloc_toc(__prom_init_toc_start, -offset, nr_entries); 2897 + } 2898 + #endif 2899 + #endif 2846 2900 2847 2901 /* 2848 2902 * We enter here early on, when the Open Firmware prom is still ··· 2907 2867 #ifdef CONFIG_PPC32 2908 2868 unsigned long offset = reloc_offset(); 2909 2869 reloc_got2(offset); 2870 + #else 2871 + reloc_toc(); 2910 2872 #endif 2911 2873 2912 2874 _prom = &RELOC(prom); ··· 3103 3061 3104 3062 #ifdef CONFIG_PPC32 3105 3063 reloc_got2(-offset); 3064 + #else 3065 + unreloc_toc(); 3106 3066 #endif 3107 3067 3108 3068 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
+1 -1
arch/powerpc/kernel/prom_init_check.sh
··· 22 22 strcmp strcpy strlcpy strlen strncmp strstr logo_linux_clut224 23 23 reloc_got2 kernstart_addr memstart_addr linux_banner _stext 24 24 opal_query_takeover opal_do_takeover opal_enter_rtas opal_secondary_entry 25 - boot_command_line" 25 + boot_command_line __prom_init_toc_start __prom_init_toc_end" 26 26 27 27 NM="$1" 28 28 OBJ="$2"
+5
arch/powerpc/kernel/vmlinux.lds.S
··· 218 218 219 219 .got : AT(ADDR(.got) - LOAD_OFFSET) { 220 220 __toc_start = .; 221 + #ifndef CONFIG_RELOCATABLE 222 + __prom_init_toc_start = .; 223 + arch/powerpc/kernel/prom_init.o*(.toc .got) 224 + __prom_init_toc_end = .; 225 + #endif 221 226 *(.got) 222 227 *(.toc) 223 228 }