GNU Linux-libre 4.14.251-gnu1
[releases.git] / arch / x86 / kernel / head64.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  prepare to run common code
4  *
5  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6  */
7
8 #define DISABLE_BRANCH_PROFILING
9 #include <linux/init.h>
10 #include <linux/linkage.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/percpu.h>
15 #include <linux/start_kernel.h>
16 #include <linux/io.h>
17 #include <linux/memblock.h>
18 #include <linux/mem_encrypt.h>
19
20 #include <asm/processor.h>
21 #include <asm/proto.h>
22 #include <asm/smp.h>
23 #include <asm/setup.h>
24 #include <asm/desc.h>
25 #include <asm/pgtable.h>
26 #include <asm/tlbflush.h>
27 #include <asm/sections.h>
28 #include <asm/kdebug.h>
29 #include <asm/e820/api.h>
30 #include <asm/bios_ebda.h>
31 #include <asm/bootparam_utils.h>
32 #include <asm/microcode.h>
33 #include <asm/kasan.h>
34 #include <asm/fixmap.h>
35
36 /*
37  * Manage page tables very early on.
38  */
39 extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
40 static unsigned int __initdata next_early_pgt;
41 pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
42
43 #define __head  __section(.head.text)
44
45 static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
46 {
47         return ptr - (void *)_text + (void *)physaddr;
48 }
49
50 unsigned long __head __startup_64(unsigned long physaddr,
51                                   struct boot_params *bp)
52 {
53         unsigned long load_delta, *p;
54         unsigned long pgtable_flags;
55         pgdval_t *pgd;
56         p4dval_t *p4d;
57         pudval_t *pud;
58         pmdval_t *pmd, pmd_entry;
59         int i;
60         unsigned int *next_pgt_ptr;
61
62         /* Is the address too large? */
63         if (physaddr >> MAX_PHYSMEM_BITS)
64                 for (;;);
65
66         /*
67          * Compute the delta between the address I am compiled to run at
68          * and the address I am actually running at.
69          */
70         load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
71
72         /* Is the address not 2M aligned? */
73         if (load_delta & ~PMD_PAGE_MASK)
74                 for (;;);
75
76         /* Activate Secure Memory Encryption (SME) if supported and enabled */
77         sme_enable(bp);
78
79         /* Include the SME encryption mask in the fixup value */
80         load_delta += sme_get_me_mask();
81
82         /* Fixup the physical addresses in the page table */
83
84         pgd = fixup_pointer(&early_top_pgt, physaddr);
85         pgd[pgd_index(__START_KERNEL_map)] += load_delta;
86
87         if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
88                 p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
89                 p4d[511] += load_delta;
90         }
91
92         pud = fixup_pointer(&level3_kernel_pgt, physaddr);
93         pud[510] += load_delta;
94         pud[511] += load_delta;
95
96         pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
97         for (i = FIXMAP_PMD_TOP; i > FIXMAP_PMD_TOP - FIXMAP_PMD_NUM; i--)
98                 pmd[i] += load_delta;
99
100         /*
101          * Set up the identity mapping for the switchover.  These
102          * entries should *NOT* have the global bit set!  This also
103          * creates a bunch of nonsense entries but that is fine --
104          * it avoids problems around wraparound.
105          */
106
107         next_pgt_ptr = fixup_pointer(&next_early_pgt, physaddr);
108         pud = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
109         pmd = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr);
110
111         pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
112
113         if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
114                 p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
115
116                 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
117                 pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
118                 pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
119
120                 i = physaddr >> P4D_SHIFT;
121                 p4d[(i + 0) % PTRS_PER_P4D] = (pgdval_t)pud + pgtable_flags;
122                 p4d[(i + 1) % PTRS_PER_P4D] = (pgdval_t)pud + pgtable_flags;
123         } else {
124                 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
125                 pgd[i + 0] = (pgdval_t)pud + pgtable_flags;
126                 pgd[i + 1] = (pgdval_t)pud + pgtable_flags;
127         }
128
129         i = physaddr >> PUD_SHIFT;
130         pud[(i + 0) % PTRS_PER_PUD] = (pudval_t)pmd + pgtable_flags;
131         pud[(i + 1) % PTRS_PER_PUD] = (pudval_t)pmd + pgtable_flags;
132
133         pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
134         pmd_entry += sme_get_me_mask();
135         pmd_entry +=  physaddr;
136
137         for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
138                 int idx = i + (physaddr >> PMD_SHIFT);
139
140                 pmd[idx % PTRS_PER_PMD] = pmd_entry + i * PMD_SIZE;
141         }
142
143         /*
144          * Fixup the kernel text+data virtual addresses. Note that
145          * we might write invalid pmds, when the kernel is relocated
146          * cleanup_highmap() fixes this up along with the mappings
147          * beyond _end.
148          *
149          * Only the region occupied by the kernel image has so far
150          * been checked against the table of usable memory regions
151          * provided by the firmware, so invalidate pages outside that
152          * region. A page table entry that maps to a reserved area of
153          * memory would allow processor speculation into that area,
154          * and on some hardware (particularly the UV platform) even
155          * speculative access to some reserved areas is caught as an
156          * error, causing the BIOS to halt the system.
157          */
158
159         pmd = fixup_pointer(level2_kernel_pgt, physaddr);
160
161         /* invalidate pages before the kernel image */
162         for (i = 0; i < pmd_index((unsigned long)_text); i++)
163                 pmd[i] &= ~_PAGE_PRESENT;
164
165         /* fixup pages that are part of the kernel image */
166         for (; i <= pmd_index((unsigned long)_end); i++)
167                 if (pmd[i] & _PAGE_PRESENT)
168                         pmd[i] += load_delta;
169
170         /* invalidate pages after the kernel image */
171         for (; i < PTRS_PER_PMD; i++)
172                 pmd[i] &= ~_PAGE_PRESENT;
173
174         /*
175          * Fixup phys_base - remove the memory encryption mask to obtain
176          * the true physical address.
177          */
178         p = fixup_pointer(&phys_base, physaddr);
179         *p += load_delta - sme_get_me_mask();
180
181         /* Encrypt the kernel and related (if SME is active) */
182         sme_encrypt_kernel(bp);
183
184         /*
185          * Return the SME encryption mask (if SME is active) to be used as a
186          * modifier for the initial pgdir entry programmed into CR3.
187          */
188         return sme_get_me_mask();
189 }
190
191 unsigned long __startup_secondary_64(void)
192 {
193         /*
194          * Return the SME encryption mask (if SME is active) to be used as a
195          * modifier for the initial pgdir entry programmed into CR3.
196          */
197         return sme_get_me_mask();
198 }
199
200 /* Wipe all early page tables except for the kernel symbol map */
201 static void __init reset_early_page_tables(void)
202 {
203         memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
204         next_early_pgt = 0;
205         write_cr3(__sme_pa_nodebug(early_top_pgt));
206 }
207
208 /* Create a new PMD entry */
209 int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)
210 {
211         unsigned long physaddr = address - __PAGE_OFFSET;
212         pgdval_t pgd, *pgd_p;
213         p4dval_t p4d, *p4d_p;
214         pudval_t pud, *pud_p;
215         pmdval_t *pmd_p;
216
217         /* Invalid address or early pgt is done ?  */
218         if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
219                 return -1;
220
221 again:
222         pgd_p = &early_top_pgt[pgd_index(address)].pgd;
223         pgd = *pgd_p;
224
225         /*
226          * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
227          * critical -- __PAGE_OFFSET would point us back into the dynamic
228          * range and we might end up looping forever...
229          */
230         if (!IS_ENABLED(CONFIG_X86_5LEVEL))
231                 p4d_p = pgd_p;
232         else if (pgd)
233                 p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
234         else {
235                 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
236                         reset_early_page_tables();
237                         goto again;
238                 }
239
240                 p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
241                 memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
242                 *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
243         }
244         p4d_p += p4d_index(address);
245         p4d = *p4d_p;
246
247         if (p4d)
248                 pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
249         else {
250                 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
251                         reset_early_page_tables();
252                         goto again;
253                 }
254
255                 pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
256                 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
257                 *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
258         }
259         pud_p += pud_index(address);
260         pud = *pud_p;
261
262         if (pud)
263                 pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
264         else {
265                 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
266                         reset_early_page_tables();
267                         goto again;
268                 }
269
270                 pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
271                 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
272                 *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
273         }
274         pmd_p[pmd_index(address)] = pmd;
275
276         return 0;
277 }
278
279 int __init early_make_pgtable(unsigned long address)
280 {
281         unsigned long physaddr = address - __PAGE_OFFSET;
282         pmdval_t pmd;
283
284         pmd = (physaddr & PMD_MASK) + early_pmd_flags;
285
286         return __early_make_pgtable(address, pmd);
287 }
288
289 /* Don't add a printk in there. printk relies on the PDA which is not initialized 
290    yet. */
291 static void __init clear_bss(void)
292 {
293         memset(__bss_start, 0,
294                (unsigned long) __bss_stop - (unsigned long) __bss_start);
295 }
296
297 static unsigned long get_cmd_line_ptr(void)
298 {
299         unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
300
301         cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
302
303         return cmd_line_ptr;
304 }
305
306 static void __init copy_bootdata(char *real_mode_data)
307 {
308         char * command_line;
309         unsigned long cmd_line_ptr;
310
311         /*
312          * If SME is active, this will create decrypted mappings of the
313          * boot data in advance of the copy operations.
314          */
315         sme_map_bootdata(real_mode_data);
316
317         memcpy(&boot_params, real_mode_data, sizeof boot_params);
318         sanitize_boot_params(&boot_params);
319         cmd_line_ptr = get_cmd_line_ptr();
320         if (cmd_line_ptr) {
321                 command_line = __va(cmd_line_ptr);
322                 memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
323         }
324
325         /*
326          * The old boot data is no longer needed and won't be reserved,
327          * freeing up that memory for use by the system. If SME is active,
328          * we need to remove the mappings that were created so that the
329          * memory doesn't remain mapped as decrypted.
330          */
331         sme_unmap_bootdata(real_mode_data);
332 }
333
334 asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
335 {
336         /*
337          * Build-time sanity checks on the kernel image and module
338          * area mappings. (these are purely build-time and produce no code)
339          */
340         BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
341         BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
342         BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
343         BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
344         BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
345         BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
346         BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
347                                 (__START_KERNEL & PGDIR_MASK)));
348         BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
349
350         cr4_init_shadow();
351
352         /* Kill off the identity-map trampoline */
353         reset_early_page_tables();
354
355         clear_bss();
356
357         clear_page(init_top_pgt);
358
359         /*
360          * SME support may update early_pmd_flags to include the memory
361          * encryption mask, so it needs to be called before anything
362          * that may generate a page fault.
363          */
364         sme_early_init();
365
366         kasan_early_init();
367
368         idt_setup_early_handler();
369
370         copy_bootdata(__va(real_mode_data));
371
372         /*
373          * Load microcode early on BSP.
374          */
375         load_ucode_bsp();
376
377         /* set init_top_pgt kernel high mapping*/
378         init_top_pgt[511] = early_top_pgt[511];
379
380         x86_64_start_reservations(real_mode_data);
381 }
382
383 void __init x86_64_start_reservations(char *real_mode_data)
384 {
385         /* version is always not zero if it is copied */
386         if (!boot_params.hdr.version)
387                 copy_bootdata(__va(real_mode_data));
388
389         x86_early_init_platform_quirks();
390
391         switch (boot_params.hdr.hardware_subarch) {
392         case X86_SUBARCH_INTEL_MID:
393                 x86_intel_mid_early_setup();
394                 break;
395         default:
396                 break;
397         }
398
399         start_kernel();
400 }