x86/boot/64/clang: Use fixup_pointer() to access 'next_early_pgt'
[karo-tx-linux.git] / arch / x86 / kernel / head64.c
1 /*
2  *  prepare to run common code
3  *
4  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5  */
6
7 #define DISABLE_BRANCH_PROFILING
8 #include <linux/init.h>
9 #include <linux/linkage.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/percpu.h>
14 #include <linux/start_kernel.h>
15 #include <linux/io.h>
16 #include <linux/memblock.h>
17
18 #include <asm/processor.h>
19 #include <asm/proto.h>
20 #include <asm/smp.h>
21 #include <asm/setup.h>
22 #include <asm/desc.h>
23 #include <asm/pgtable.h>
24 #include <asm/tlbflush.h>
25 #include <asm/sections.h>
26 #include <asm/kdebug.h>
27 #include <asm/e820/api.h>
28 #include <asm/bios_ebda.h>
29 #include <asm/bootparam_utils.h>
30 #include <asm/microcode.h>
31 #include <asm/kasan.h>
32
33 /*
34  * Manage page tables very early on.
35  */
36 extern pgd_t early_top_pgt[PTRS_PER_PGD];
37 extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
38 static unsigned int __initdata next_early_pgt;
39 pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
40
41 #define __head  __section(.head.text)
42
43 static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
44 {
45         return ptr - (void *)_text + (void *)physaddr;
46 }
47
48 void __head __startup_64(unsigned long physaddr)
49 {
50         unsigned long load_delta, *p;
51         pgdval_t *pgd;
52         p4dval_t *p4d;
53         pudval_t *pud;
54         pmdval_t *pmd, pmd_entry;
55         int i;
56         unsigned int *next_pgt_ptr;
57
58         /* Is the address too large? */
59         if (physaddr >> MAX_PHYSMEM_BITS)
60                 for (;;);
61
62         /*
63          * Compute the delta between the address I am compiled to run at
64          * and the address I am actually running at.
65          */
66         load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
67
68         /* Is the address not 2M aligned? */
69         if (load_delta & ~PMD_PAGE_MASK)
70                 for (;;);
71
72         /* Fixup the physical addresses in the page table */
73
74         pgd = fixup_pointer(&early_top_pgt, physaddr);
75         pgd[pgd_index(__START_KERNEL_map)] += load_delta;
76
77         if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
78                 p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
79                 p4d[511] += load_delta;
80         }
81
82         pud = fixup_pointer(&level3_kernel_pgt, physaddr);
83         pud[510] += load_delta;
84         pud[511] += load_delta;
85
86         pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
87         pmd[506] += load_delta;
88
89         /*
90          * Set up the identity mapping for the switchover.  These
91          * entries should *NOT* have the global bit set!  This also
92          * creates a bunch of nonsense entries but that is fine --
93          * it avoids problems around wraparound.
94          */
95         next_pgt_ptr = fixup_pointer(&next_early_pgt, physaddr);
96         pud = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
97         pmd = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
98
99         if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
100                 p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
101
102                 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
103                 pgd[i + 0] = (pgdval_t)p4d + _KERNPG_TABLE;
104                 pgd[i + 1] = (pgdval_t)p4d + _KERNPG_TABLE;
105
106                 i = (physaddr >> P4D_SHIFT) % PTRS_PER_P4D;
107                 p4d[i + 0] = (pgdval_t)pud + _KERNPG_TABLE;
108                 p4d[i + 1] = (pgdval_t)pud + _KERNPG_TABLE;
109         } else {
110                 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
111                 pgd[i + 0] = (pgdval_t)pud + _KERNPG_TABLE;
112                 pgd[i + 1] = (pgdval_t)pud + _KERNPG_TABLE;
113         }
114
115         i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD;
116         pud[i + 0] = (pudval_t)pmd + _KERNPG_TABLE;
117         pud[i + 1] = (pudval_t)pmd + _KERNPG_TABLE;
118
119         pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
120         pmd_entry +=  physaddr;
121
122         for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
123                 int idx = i + (physaddr >> PMD_SHIFT) % PTRS_PER_PMD;
124                 pmd[idx] = pmd_entry + i * PMD_SIZE;
125         }
126
127         /*
128          * Fixup the kernel text+data virtual addresses. Note that
129          * we might write invalid pmds, when the kernel is relocated
130          * cleanup_highmap() fixes this up along with the mappings
131          * beyond _end.
132          */
133
134         pmd = fixup_pointer(level2_kernel_pgt, physaddr);
135         for (i = 0; i < PTRS_PER_PMD; i++) {
136                 if (pmd[i] & _PAGE_PRESENT)
137                         pmd[i] += load_delta;
138         }
139
140         /* Fixup phys_base */
141         p = fixup_pointer(&phys_base, physaddr);
142         *p += load_delta;
143 }
144
145 /* Wipe all early page tables except for the kernel symbol map */
146 static void __init reset_early_page_tables(void)
147 {
148         memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
149         next_early_pgt = 0;
150         write_cr3(__pa_nodebug(early_top_pgt));
151 }
152
153 /* Create a new PMD entry */
154 int __init early_make_pgtable(unsigned long address)
155 {
156         unsigned long physaddr = address - __PAGE_OFFSET;
157         pgdval_t pgd, *pgd_p;
158         p4dval_t p4d, *p4d_p;
159         pudval_t pud, *pud_p;
160         pmdval_t pmd, *pmd_p;
161
162         /* Invalid address or early pgt is done ?  */
163         if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
164                 return -1;
165
166 again:
167         pgd_p = &early_top_pgt[pgd_index(address)].pgd;
168         pgd = *pgd_p;
169
170         /*
171          * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
172          * critical -- __PAGE_OFFSET would point us back into the dynamic
173          * range and we might end up looping forever...
174          */
175         if (!IS_ENABLED(CONFIG_X86_5LEVEL))
176                 p4d_p = pgd_p;
177         else if (pgd)
178                 p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
179         else {
180                 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
181                         reset_early_page_tables();
182                         goto again;
183                 }
184
185                 p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
186                 memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
187                 *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
188         }
189         p4d_p += p4d_index(address);
190         p4d = *p4d_p;
191
192         if (p4d)
193                 pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
194         else {
195                 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
196                         reset_early_page_tables();
197                         goto again;
198                 }
199
200                 pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
201                 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
202                 *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
203         }
204         pud_p += pud_index(address);
205         pud = *pud_p;
206
207         if (pud)
208                 pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
209         else {
210                 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
211                         reset_early_page_tables();
212                         goto again;
213                 }
214
215                 pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
216                 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
217                 *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
218         }
219         pmd = (physaddr & PMD_MASK) + early_pmd_flags;
220         pmd_p[pmd_index(address)] = pmd;
221
222         return 0;
223 }
224
225 /* Don't add a printk in there. printk relies on the PDA which is not initialized 
226    yet. */
227 static void __init clear_bss(void)
228 {
229         memset(__bss_start, 0,
230                (unsigned long) __bss_stop - (unsigned long) __bss_start);
231 }
232
233 static unsigned long get_cmd_line_ptr(void)
234 {
235         unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
236
237         cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
238
239         return cmd_line_ptr;
240 }
241
242 static void __init copy_bootdata(char *real_mode_data)
243 {
244         char * command_line;
245         unsigned long cmd_line_ptr;
246
247         memcpy(&boot_params, real_mode_data, sizeof boot_params);
248         sanitize_boot_params(&boot_params);
249         cmd_line_ptr = get_cmd_line_ptr();
250         if (cmd_line_ptr) {
251                 command_line = __va(cmd_line_ptr);
252                 memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
253         }
254 }
255
256 asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
257 {
258         int i;
259
260         /*
261          * Build-time sanity checks on the kernel image and module
262          * area mappings. (these are purely build-time and produce no code)
263          */
264         BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
265         BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
266         BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
267         BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
268         BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
269         BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
270         BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
271                                 (__START_KERNEL & PGDIR_MASK)));
272         BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
273
274         cr4_init_shadow();
275
276         /* Kill off the identity-map trampoline */
277         reset_early_page_tables();
278
279         clear_bss();
280
281         clear_page(init_top_pgt);
282
283         kasan_early_init();
284
285         for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
286                 set_intr_gate(i, early_idt_handler_array[i]);
287         load_idt((const struct desc_ptr *)&idt_descr);
288
289         copy_bootdata(__va(real_mode_data));
290
291         /*
292          * Load microcode early on BSP.
293          */
294         load_ucode_bsp();
295
296         /* set init_top_pgt kernel high mapping*/
297         init_top_pgt[511] = early_top_pgt[511];
298
299         x86_64_start_reservations(real_mode_data);
300 }
301
302 void __init x86_64_start_reservations(char *real_mode_data)
303 {
304         /* version is always not zero if it is copied */
305         if (!boot_params.hdr.version)
306                 copy_bootdata(__va(real_mode_data));
307
308         x86_early_init_platform_quirks();
309
310         switch (boot_params.hdr.hardware_subarch) {
311         case X86_SUBARCH_INTEL_MID:
312                 x86_intel_mid_early_setup();
313                 break;
314         default:
315                 break;
316         }
317
318         start_kernel();
319 }