blob: 5cd0b72a02834ca1f56efae4c4681220a56a0572 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Jones835c34a2007-10-12 21:10:53 -04002 * prepare to run common code
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Andrey Ryabininbe3606f2017-03-13 19:33:37 +03007#define DISABLE_BRANCH_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
Thomas Gleixnereaf76e82008-01-30 13:30:19 +010014#include <linux/start_kernel.h>
Huang, Ying8b664aa2008-03-28 10:49:44 +080015#include <linux/io.h>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070016#include <linux/memblock.h>
Tom Lendacky5868f362017-07-17 16:10:05 -050017#include <linux/mem_encrypt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/processor.h>
20#include <asm/proto.h>
21#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/setup.h>
23#include <asm/desc.h>
Siddha, Suresh Bf6c2e332005-11-05 17:25:53 +010024#include <asm/pgtable.h>
Vivek Goyalcfd243d2007-05-02 19:27:07 +020025#include <asm/tlbflush.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010026#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010027#include <asm/kdebug.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010028#include <asm/e820/api.h>
Thomas Gleixner47a3d5d2009-08-29 15:03:59 +020029#include <asm/bios_ebda.h>
H. Peter Anvin5dcd14e2013-01-29 01:05:24 -080030#include <asm/bootparam_utils.h>
Fenghua Yufeddc9d2012-12-20 23:44:30 -080031#include <asm/microcode.h>
Andrey Ryabininef7f0d62015-02-13 14:39:25 -080032#include <asm/kasan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
H. Peter Anvin8170e6b2013-01-24 12:19:52 -080034/*
35 * Manage page tables very early on.
36 */
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +030037extern pgd_t early_top_pgt[PTRS_PER_PGD];
H. Peter Anvin8170e6b2013-01-24 12:19:52 -080038extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD];
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030039static unsigned int __initdata next_early_pgt;
Linus Torvalds5e427ec2013-05-20 11:36:03 -070040pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -080041
Kirill A. Shutemov26179672017-06-16 14:30:24 +030042#define __head __section(.head.text)
43
44static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030045{
46 return ptr - (void *)_text + (void *)physaddr;
47}
48
Tom Lendacky5868f362017-07-17 16:10:05 -050049unsigned long __head __startup_64(unsigned long physaddr)
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030050{
51 unsigned long load_delta, *p;
Tom Lendacky5868f362017-07-17 16:10:05 -050052 unsigned long pgtable_flags;
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030053 pgdval_t *pgd;
Kirill A. Shutemov032370b2017-06-06 14:31:28 +030054 p4dval_t *p4d;
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030055 pudval_t *pud;
56 pmdval_t *pmd, pmd_entry;
57 int i;
58
59 /* Is the address too large? */
60 if (physaddr >> MAX_PHYSMEM_BITS)
61 for (;;);
62
63 /*
64 * Compute the delta between the address I am compiled to run at
65 * and the address I am actually running at.
66 */
67 load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map);
68
69 /* Is the address not 2M aligned? */
70 if (load_delta & ~PMD_PAGE_MASK)
71 for (;;);
72
Tom Lendacky5868f362017-07-17 16:10:05 -050073 /* Activate Secure Memory Encryption (SME) if supported and enabled */
74 sme_enable();
75
76 /* Include the SME encryption mask in the fixup value */
77 load_delta += sme_get_me_mask();
78
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030079 /* Fixup the physical addresses in the page table */
80
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +030081 pgd = fixup_pointer(&early_top_pgt, physaddr);
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030082 pgd[pgd_index(__START_KERNEL_map)] += load_delta;
83
Kirill A. Shutemov032370b2017-06-06 14:31:28 +030084 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
85 p4d = fixup_pointer(&level4_kernel_pgt, physaddr);
86 p4d[511] += load_delta;
87 }
88
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +030089 pud = fixup_pointer(&level3_kernel_pgt, physaddr);
90 pud[510] += load_delta;
91 pud[511] += load_delta;
92
93 pmd = fixup_pointer(level2_fixmap_pgt, physaddr);
94 pmd[506] += load_delta;
95
96 /*
97 * Set up the identity mapping for the switchover. These
98 * entries should *NOT* have the global bit set! This also
99 * creates a bunch of nonsense entries but that is fine --
100 * it avoids problems around wraparound.
101 */
102
103 pud = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
104 pmd = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
Tom Lendacky21729f82017-07-17 16:10:07 -0500105 pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask();
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +0300106
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300107 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
108 p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr);
109
110 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
Tom Lendacky5868f362017-07-17 16:10:05 -0500111 pgd[i + 0] = (pgdval_t)p4d + pgtable_flags;
112 pgd[i + 1] = (pgdval_t)p4d + pgtable_flags;
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300113
114 i = (physaddr >> P4D_SHIFT) % PTRS_PER_P4D;
Tom Lendacky5868f362017-07-17 16:10:05 -0500115 p4d[i + 0] = (pgdval_t)pud + pgtable_flags;
116 p4d[i + 1] = (pgdval_t)pud + pgtable_flags;
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300117 } else {
118 i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD;
Tom Lendacky5868f362017-07-17 16:10:05 -0500119 pgd[i + 0] = (pgdval_t)pud + pgtable_flags;
120 pgd[i + 1] = (pgdval_t)pud + pgtable_flags;
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300121 }
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +0300122
123 i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD;
Tom Lendacky5868f362017-07-17 16:10:05 -0500124 pud[i + 0] = (pudval_t)pmd + pgtable_flags;
125 pud[i + 1] = (pudval_t)pmd + pgtable_flags;
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +0300126
127 pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL;
Tom Lendacky5868f362017-07-17 16:10:05 -0500128 pmd_entry += sme_get_me_mask();
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +0300129 pmd_entry += physaddr;
130
131 for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) {
132 int idx = i + (physaddr >> PMD_SHIFT) % PTRS_PER_PMD;
133 pmd[idx] = pmd_entry + i * PMD_SIZE;
134 }
135
136 /*
137 * Fixup the kernel text+data virtual addresses. Note that
138 * we might write invalid pmds, when the kernel is relocated
139 * cleanup_highmap() fixes this up along with the mappings
140 * beyond _end.
141 */
142
143 pmd = fixup_pointer(level2_kernel_pgt, physaddr);
144 for (i = 0; i < PTRS_PER_PMD; i++) {
145 if (pmd[i] & _PAGE_PRESENT)
146 pmd[i] += load_delta;
147 }
148
Tom Lendacky5868f362017-07-17 16:10:05 -0500149 /*
150 * Fixup phys_base - remove the memory encryption mask to obtain
151 * the true physical address.
152 */
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +0300153 p = fixup_pointer(&phys_base, physaddr);
Tom Lendacky5868f362017-07-17 16:10:05 -0500154 *p += load_delta - sme_get_me_mask();
155
156 /* Encrypt the kernel (if SME is active) */
157 sme_encrypt_kernel();
158
159 /*
160 * Return the SME encryption mask (if SME is active) to be used as a
161 * modifier for the initial pgdir entry programmed into CR3.
162 */
163 return sme_get_me_mask();
164}
165
166unsigned long __startup_secondary_64(void)
167{
168 /*
169 * Return the SME encryption mask (if SME is active) to be used as a
170 * modifier for the initial pgdir entry programmed into CR3.
171 */
172 return sme_get_me_mask();
Kirill A. Shutemovc88d7152017-06-06 14:31:26 +0300173}
174
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800175/* Wipe all early page tables except for the kernel symbol map */
176static void __init reset_early_page_tables(void)
Vivek Goyalcfd243d2007-05-02 19:27:07 +0200177{
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300178 memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1));
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800179 next_early_pgt = 0;
Tom Lendacky21729f82017-07-17 16:10:07 -0500180 write_cr3(__sme_pa_nodebug(early_top_pgt));
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800181}
182
183/* Create a new PMD entry */
184int __init early_make_pgtable(unsigned long address)
185{
186 unsigned long physaddr = address - __PAGE_OFFSET;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800187 pgdval_t pgd, *pgd_p;
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300188 p4dval_t p4d, *p4d_p;
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800189 pudval_t pud, *pud_p;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800190 pmdval_t pmd, *pmd_p;
191
192 /* Invalid address or early pgt is done ? */
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300193 if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt))
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800194 return -1;
195
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800196again:
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300197 pgd_p = &early_top_pgt[pgd_index(address)].pgd;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800198 pgd = *pgd_p;
199
200 /*
201 * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is
202 * critical -- __PAGE_OFFSET would point us back into the dynamic
203 * range and we might end up looping forever...
204 */
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300205 if (!IS_ENABLED(CONFIG_X86_5LEVEL))
206 p4d_p = pgd_p;
207 else if (pgd)
208 p4d_p = (p4dval_t *)((pgd & 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 p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++];
216 memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D);
217 *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
218 }
219 p4d_p += p4d_index(address);
220 p4d = *p4d_p;
221
222 if (p4d)
223 pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800224 else {
225 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800226 reset_early_page_tables();
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800227 goto again;
228 }
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800229
230 pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++];
Alexander Kuleshova91bbe02016-02-09 19:44:54 +0600231 memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD);
Kirill A. Shutemov032370b2017-06-06 14:31:28 +0300232 *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800233 }
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800234 pud_p += pud_index(address);
235 pud = *pud_p;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800236
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800237 if (pud)
238 pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base);
239 else {
240 if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) {
241 reset_early_page_tables();
242 goto again;
243 }
244
245 pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++];
Alexander Kuleshova91bbe02016-02-09 19:44:54 +0600246 memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD);
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800247 *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800248 }
H. Peter Anvin78d77df2013-05-02 10:33:46 -0700249 pmd = (physaddr & PMD_MASK) + early_pmd_flags;
Yinghai Lu6b9c75a2013-01-24 12:19:53 -0800250 pmd_p[pmd_index(address)] = pmd;
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800251
252 return 0;
Vivek Goyalcfd243d2007-05-02 19:27:07 +0200253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* Don't add a printk in there. printk relies on the PDA which is not initialized
256 yet. */
257static void __init clear_bss(void)
258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 memset(__bss_start, 0,
Andi Kleen2bc04142005-11-05 17:25:53 +0100260 (unsigned long) __bss_stop - (unsigned long) __bss_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Yinghai Luf1da8342013-01-24 12:19:57 -0800263static unsigned long get_cmd_line_ptr(void)
264{
265 unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
266
Yinghai Luee92d812013-01-28 20:16:44 -0800267 cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32;
268
Yinghai Luf1da8342013-01-24 12:19:57 -0800269 return cmd_line_ptr;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static void __init copy_bootdata(char *real_mode_data)
273{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 char * command_line;
Yinghai Luf1da8342013-01-24 12:19:57 -0800275 unsigned long cmd_line_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
H. Peter Anvin30c82642007-10-15 17:13:22 -0700277 memcpy(&boot_params, real_mode_data, sizeof boot_params);
H. Peter Anvin5dcd14e2013-01-29 01:05:24 -0800278 sanitize_boot_params(&boot_params);
Yinghai Luf1da8342013-01-24 12:19:57 -0800279 cmd_line_ptr = get_cmd_line_ptr();
280 if (cmd_line_ptr) {
281 command_line = __va(cmd_line_ptr);
H. Peter Anvin30c82642007-10-15 17:13:22 -0700282 memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Andi Kleen2605fc22014-05-02 00:44:37 +0200286asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int i;
289
Ingo Molnarb4e04092008-02-21 13:45:16 +0100290 /*
291 * Build-time sanity checks on the kernel image and module
292 * area mappings. (these are purely build-time and produce no code)
293 */
Borislav Petkov8e3c2a82013-03-04 21:16:17 +0100294 BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map);
295 BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE);
Ingo Molnarb4e04092008-02-21 13:45:16 +0100296 BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE);
Borislav Petkov8e3c2a82013-03-04 21:16:17 +0100297 BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0);
Ingo Molnarb4e04092008-02-21 13:45:16 +0100298 BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0);
299 BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
300 BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
301 (__START_KERNEL & PGDIR_MASK)));
Jan Beulich66d4bdf2008-07-31 16:48:31 +0100302 BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END);
Ingo Molnarb4e04092008-02-21 13:45:16 +0100303
Andy Lutomirski1e02ce42014-10-24 15:58:08 -0700304 cr4_init_shadow();
305
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800306 /* Kill off the identity-map trampoline */
307 reset_early_page_tables();
308
Yinghai Lu3df0af02006-12-07 02:14:12 +0100309 clear_bss();
310
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300311 clear_page(init_top_pgt);
Andrey Ryabinind0f77d42015-07-02 12:09:33 +0300312
Tom Lendacky21729f82017-07-17 16:10:07 -0500313 /*
314 * SME support may update early_pmd_flags to include the memory
315 * encryption mask, so it needs to be called before anything
316 * that may generate a page fault.
317 */
318 sme_early_init();
319
Alexander Popov5d5aa3c2015-07-02 12:09:34 +0300320 kasan_early_init();
321
Linus Torvaldsac630dd2013-02-22 13:09:51 -0800322 for (i = 0; i < NUM_EXCEPTION_VECTORS; i++)
Andy Lutomirski425be562015-05-22 16:15:47 -0700323 set_intr_gate(i, early_idt_handler_array[i]);
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +0200324 load_idt((const struct desc_ptr *)&idt_descr);
Siddha, Suresh Bf6c2e332005-11-05 17:25:53 +0100325
Yinghai Lufa2bbce2013-01-24 12:19:49 -0800326 copy_bootdata(__va(real_mode_data));
327
Fenghua Yufeddc9d2012-12-20 23:44:30 -0800328 /*
329 * Load microcode early on BSP.
330 */
331 load_ucode_bsp();
332
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300333 /* set init_top_pgt kernel high mapping*/
334 init_top_pgt[511] = early_top_pgt[511];
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800335
Jeremy Fitzhardingef97013f2008-06-25 00:19:18 -0400336 x86_64_start_reservations(real_mode_data);
337}
338
339void __init x86_64_start_reservations(char *real_mode_data)
340{
Yinghai Lufa2bbce2013-01-24 12:19:49 -0800341 /* version is always not zero if it is copied */
342 if (!boot_params.hdr.version)
343 copy_bootdata(__va(real_mode_data));
Yinghai Lu9de819f2008-01-30 13:30:46 +0100344
Luis R. Rodriguez8d152e72016-04-13 17:04:34 -0700345 x86_early_init_platform_quirks();
Andi Kleen75175272008-01-30 13:33:17 +0100346
Andy Shevchenko3fda5bb2016-01-15 22:11:07 +0200347 switch (boot_params.hdr.hardware_subarch) {
348 case X86_SUBARCH_INTEL_MID:
349 x86_intel_mid_early_setup();
350 break;
351 default:
352 break;
353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 start_kernel();
356}