Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Dave Jones | 835c34a | 2007-10-12 21:10:53 -0400 | [diff] [blame] | 3 | * prepare to run common code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Andrey Ryabinin | be3606f | 2017-03-13 19:33:37 +0300 | [diff] [blame] | 8 | #define DISABLE_BRANCH_PROFILING |
Kirill A. Shutemov | ad3fe52 | 2018-05-18 13:35:23 +0300 | [diff] [blame] | 9 | |
| 10 | /* cpu_feature_enabled() cannot be used this early */ |
| 11 | #define USE_EARLY_PGTABLE_L5 |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/linkage.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/percpu.h> |
Thomas Gleixner | eaf76e8 | 2008-01-30 13:30:19 +0100 | [diff] [blame] | 19 | #include <linux/start_kernel.h> |
Huang, Ying | 8b664aa | 2008-03-28 10:49:44 +0800 | [diff] [blame] | 20 | #include <linux/io.h> |
Yinghai Lu | 72d7c3b | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 21 | #include <linux/memblock.h> |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 22 | #include <linux/mem_encrypt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/processor.h> |
| 25 | #include <asm/proto.h> |
| 26 | #include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/setup.h> |
| 28 | #include <asm/desc.h> |
Siddha, Suresh B | f6c2e33 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 29 | #include <asm/pgtable.h> |
Vivek Goyal | cfd243d | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 30 | #include <asm/tlbflush.h> |
Andi Kleen | 2bc0414 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 31 | #include <asm/sections.h> |
Thomas Gleixner | 718fc13 | 2008-01-30 13:30:17 +0100 | [diff] [blame] | 32 | #include <asm/kdebug.h> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 33 | #include <asm/e820/api.h> |
Thomas Gleixner | 47a3d5d | 2009-08-29 15:03:59 +0200 | [diff] [blame] | 34 | #include <asm/bios_ebda.h> |
H. Peter Anvin | 5dcd14e | 2013-01-29 01:05:24 -0800 | [diff] [blame] | 35 | #include <asm/bootparam_utils.h> |
Fenghua Yu | feddc9d | 2012-12-20 23:44:30 -0800 | [diff] [blame] | 36 | #include <asm/microcode.h> |
Andrey Ryabinin | ef7f0d6 | 2015-02-13 14:39:25 -0800 | [diff] [blame] | 37 | #include <asm/kasan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Manage page tables very early on. |
| 41 | */ |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 42 | extern pmd_t early_dynamic_pgts[EARLY_DYNAMIC_PAGE_TABLES][PTRS_PER_PMD]; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 43 | static unsigned int __initdata next_early_pgt; |
Linus Torvalds | 5e427ec | 2013-05-20 11:36:03 -0700 | [diff] [blame] | 44 | pmdval_t early_pmd_flags = __PAGE_KERNEL_LARGE & ~(_PAGE_GLOBAL | _PAGE_NX); |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 45 | |
Kirill A. Shutemov | e626e6b | 2018-02-14 14:16:51 +0300 | [diff] [blame] | 46 | #ifdef CONFIG_X86_5LEVEL |
Kirill A. Shutemov | 51be133 | 2018-06-23 01:08:40 +0300 | [diff] [blame] | 47 | unsigned int __pgtable_l5_enabled __ro_after_init; |
Kirill A. Shutemov | b16e770 | 2018-02-14 21:25:35 +0300 | [diff] [blame] | 48 | unsigned int pgdir_shift __ro_after_init = 39; |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 49 | EXPORT_SYMBOL(pgdir_shift); |
Kirill A. Shutemov | b16e770 | 2018-02-14 21:25:35 +0300 | [diff] [blame] | 50 | unsigned int ptrs_per_p4d __ro_after_init = 1; |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 51 | EXPORT_SYMBOL(ptrs_per_p4d); |
Kirill A. Shutemov | e626e6b | 2018-02-14 14:16:51 +0300 | [diff] [blame] | 52 | #endif |
| 53 | |
Kirill A. Shutemov | eedb92a | 2018-02-14 14:16:50 +0300 | [diff] [blame] | 54 | #ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT |
Kirill A. Shutemov | 4fa5662 | 2018-02-14 21:25:36 +0300 | [diff] [blame] | 55 | unsigned long page_offset_base __ro_after_init = __PAGE_OFFSET_BASE_L4; |
Kirill A. Shutemov | eedb92a | 2018-02-14 14:16:50 +0300 | [diff] [blame] | 56 | EXPORT_SYMBOL(page_offset_base); |
Kirill A. Shutemov | a741254 | 2018-02-14 21:25:37 +0300 | [diff] [blame] | 57 | unsigned long vmalloc_base __ro_after_init = __VMALLOC_BASE_L4; |
Kirill A. Shutemov | eedb92a | 2018-02-14 14:16:50 +0300 | [diff] [blame] | 58 | EXPORT_SYMBOL(vmalloc_base); |
Kirill A. Shutemov | 9b46a05 | 2018-02-14 21:25:38 +0300 | [diff] [blame] | 59 | unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4; |
Kirill A. Shutemov | eedb92a | 2018-02-14 14:16:50 +0300 | [diff] [blame] | 60 | EXPORT_SYMBOL(vmemmap_base); |
| 61 | #endif |
| 62 | |
Kirill A. Shutemov | 2617967 | 2017-06-16 14:30:24 +0300 | [diff] [blame] | 63 | #define __head __section(.head.text) |
| 64 | |
| 65 | static void __head *fixup_pointer(void *ptr, unsigned long physaddr) |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 66 | { |
| 67 | return ptr - (void *)_text + (void *)physaddr; |
| 68 | } |
| 69 | |
Kirill A. Shutemov | 4fa5662 | 2018-02-14 21:25:36 +0300 | [diff] [blame] | 70 | static unsigned long __head *fixup_long(void *ptr, unsigned long physaddr) |
| 71 | { |
| 72 | return fixup_pointer(ptr, physaddr); |
| 73 | } |
| 74 | |
Kirill A. Shutemov | 4c2b405 | 2018-02-14 21:25:34 +0300 | [diff] [blame] | 75 | #ifdef CONFIG_X86_5LEVEL |
| 76 | static unsigned int __head *fixup_int(void *ptr, unsigned long physaddr) |
| 77 | { |
| 78 | return fixup_pointer(ptr, physaddr); |
| 79 | } |
| 80 | |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 81 | static bool __head check_la57_support(unsigned long physaddr) |
Kirill A. Shutemov | 4c2b405 | 2018-02-14 21:25:34 +0300 | [diff] [blame] | 82 | { |
Kirill A. Shutemov | 372fddf | 2018-05-18 13:35:25 +0300 | [diff] [blame] | 83 | /* |
| 84 | * 5-level paging is detected and enabled at kernel decomression |
| 85 | * stage. Only check if it has been enabled there. |
| 86 | */ |
| 87 | if (!(native_read_cr4() & X86_CR4_LA57)) |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 88 | return false; |
Kirill A. Shutemov | 4c2b405 | 2018-02-14 21:25:34 +0300 | [diff] [blame] | 89 | |
Kirill A. Shutemov | ad3fe52 | 2018-05-18 13:35:23 +0300 | [diff] [blame] | 90 | *fixup_int(&__pgtable_l5_enabled, physaddr) = 1; |
Kirill A. Shutemov | b16e770 | 2018-02-14 21:25:35 +0300 | [diff] [blame] | 91 | *fixup_int(&pgdir_shift, physaddr) = 48; |
| 92 | *fixup_int(&ptrs_per_p4d, physaddr) = 512; |
Kirill A. Shutemov | 4fa5662 | 2018-02-14 21:25:36 +0300 | [diff] [blame] | 93 | *fixup_long(&page_offset_base, physaddr) = __PAGE_OFFSET_BASE_L5; |
Kirill A. Shutemov | a741254 | 2018-02-14 21:25:37 +0300 | [diff] [blame] | 94 | *fixup_long(&vmalloc_base, physaddr) = __VMALLOC_BASE_L5; |
Kirill A. Shutemov | 9b46a05 | 2018-02-14 21:25:38 +0300 | [diff] [blame] | 95 | *fixup_long(&vmemmap_base, physaddr) = __VMEMMAP_BASE_L5; |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 96 | |
| 97 | return true; |
Kirill A. Shutemov | 4c2b405 | 2018-02-14 21:25:34 +0300 | [diff] [blame] | 98 | } |
| 99 | #else |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 100 | static bool __head check_la57_support(unsigned long physaddr) |
| 101 | { |
| 102 | return false; |
| 103 | } |
Kirill A. Shutemov | 4c2b405 | 2018-02-14 21:25:34 +0300 | [diff] [blame] | 104 | #endif |
| 105 | |
Alexander Potapenko | 4a09f02 | 2018-05-09 11:18:22 +0200 | [diff] [blame] | 106 | /* Code in __startup_64() can be relocated during execution, but the compiler |
| 107 | * doesn't have to generate PC-relative relocations when accessing globals from |
| 108 | * that function. Clang actually does not generate them, which leads to |
| 109 | * boot-time crashes. To work around this problem, every global pointer must |
| 110 | * be adjusted using fixup_pointer(). |
| 111 | */ |
Tom Lendacky | aca20d5 | 2017-07-17 16:10:35 -0500 | [diff] [blame] | 112 | unsigned long __head __startup_64(unsigned long physaddr, |
| 113 | struct boot_params *bp) |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 114 | { |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 115 | unsigned long load_delta, *p; |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 116 | unsigned long pgtable_flags; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 117 | pgdval_t *pgd; |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 118 | p4dval_t *p4d; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 119 | pudval_t *pud; |
| 120 | pmdval_t *pmd, pmd_entry; |
Alexander Potapenko | 4a09f02 | 2018-05-09 11:18:22 +0200 | [diff] [blame] | 121 | pteval_t *mask_ptr; |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 122 | bool la57; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 123 | int i; |
Alexander Potapenko | 187e91f | 2017-08-16 21:08:08 +0200 | [diff] [blame] | 124 | unsigned int *next_pgt_ptr; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 125 | |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 126 | la57 = check_la57_support(physaddr); |
Kirill A. Shutemov | 4c2b405 | 2018-02-14 21:25:34 +0300 | [diff] [blame] | 127 | |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 128 | /* Is the address too large? */ |
| 129 | if (physaddr >> MAX_PHYSMEM_BITS) |
| 130 | for (;;); |
| 131 | |
| 132 | /* |
| 133 | * Compute the delta between the address I am compiled to run at |
| 134 | * and the address I am actually running at. |
| 135 | */ |
| 136 | load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map); |
| 137 | |
| 138 | /* Is the address not 2M aligned? */ |
| 139 | if (load_delta & ~PMD_PAGE_MASK) |
| 140 | for (;;); |
| 141 | |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 142 | /* Activate Secure Memory Encryption (SME) if supported and enabled */ |
Tom Lendacky | aca20d5 | 2017-07-17 16:10:35 -0500 | [diff] [blame] | 143 | sme_enable(bp); |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 144 | |
| 145 | /* Include the SME encryption mask in the fixup value */ |
| 146 | load_delta += sme_get_me_mask(); |
| 147 | |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 148 | /* Fixup the physical addresses in the page table */ |
| 149 | |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 150 | pgd = fixup_pointer(&early_top_pgt, physaddr); |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 151 | p = pgd + pgd_index(__START_KERNEL_map); |
| 152 | if (la57) |
| 153 | *p = (unsigned long)level4_kernel_pgt; |
| 154 | else |
| 155 | *p = (unsigned long)level3_kernel_pgt; |
| 156 | *p += _PAGE_TABLE_NOENC - __START_KERNEL_map + load_delta; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 157 | |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 158 | if (la57) { |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 159 | p4d = fixup_pointer(&level4_kernel_pgt, physaddr); |
| 160 | p4d[511] += load_delta; |
| 161 | } |
| 162 | |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 163 | pud = fixup_pointer(&level3_kernel_pgt, physaddr); |
| 164 | pud[510] += load_delta; |
| 165 | pud[511] += load_delta; |
| 166 | |
| 167 | pmd = fixup_pointer(level2_fixmap_pgt, physaddr); |
| 168 | pmd[506] += load_delta; |
| 169 | |
| 170 | /* |
| 171 | * Set up the identity mapping for the switchover. These |
| 172 | * entries should *NOT* have the global bit set! This also |
| 173 | * creates a bunch of nonsense entries but that is fine -- |
| 174 | * it avoids problems around wraparound. |
| 175 | */ |
| 176 | |
Alexander Potapenko | 187e91f | 2017-08-16 21:08:08 +0200 | [diff] [blame] | 177 | next_pgt_ptr = fixup_pointer(&next_early_pgt, physaddr); |
| 178 | pud = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr); |
| 179 | pmd = fixup_pointer(early_dynamic_pgts[(*next_pgt_ptr)++], physaddr); |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 180 | |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 181 | pgtable_flags = _KERNPG_TABLE_NOENC + sme_get_me_mask(); |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 182 | |
Kirill A. Shutemov | 6f9dd32 | 2018-02-14 21:25:39 +0300 | [diff] [blame] | 183 | if (la57) { |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 184 | p4d = fixup_pointer(early_dynamic_pgts[next_early_pgt++], physaddr); |
| 185 | |
| 186 | i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD; |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 187 | pgd[i + 0] = (pgdval_t)p4d + pgtable_flags; |
| 188 | pgd[i + 1] = (pgdval_t)p4d + pgtable_flags; |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 189 | |
| 190 | i = (physaddr >> P4D_SHIFT) % PTRS_PER_P4D; |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 191 | p4d[i + 0] = (pgdval_t)pud + pgtable_flags; |
| 192 | p4d[i + 1] = (pgdval_t)pud + pgtable_flags; |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 193 | } else { |
| 194 | i = (physaddr >> PGDIR_SHIFT) % PTRS_PER_PGD; |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 195 | pgd[i + 0] = (pgdval_t)pud + pgtable_flags; |
| 196 | pgd[i + 1] = (pgdval_t)pud + pgtable_flags; |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 197 | } |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 198 | |
| 199 | i = (physaddr >> PUD_SHIFT) % PTRS_PER_PUD; |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 200 | pud[i + 0] = (pudval_t)pmd + pgtable_flags; |
| 201 | pud[i + 1] = (pudval_t)pmd + pgtable_flags; |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 202 | |
| 203 | pmd_entry = __PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL; |
Dave Hansen | fb43d6c | 2018-04-06 13:55:09 -0700 | [diff] [blame] | 204 | /* Filter out unsupported __PAGE_KERNEL_* bits: */ |
Alexander Potapenko | 4a09f02 | 2018-05-09 11:18:22 +0200 | [diff] [blame] | 205 | mask_ptr = fixup_pointer(&__supported_pte_mask, physaddr); |
| 206 | pmd_entry &= *mask_ptr; |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 207 | pmd_entry += sme_get_me_mask(); |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 208 | pmd_entry += physaddr; |
| 209 | |
| 210 | for (i = 0; i < DIV_ROUND_UP(_end - _text, PMD_SIZE); i++) { |
| 211 | int idx = i + (physaddr >> PMD_SHIFT) % PTRS_PER_PMD; |
| 212 | pmd[idx] = pmd_entry + i * PMD_SIZE; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Fixup the kernel text+data virtual addresses. Note that |
| 217 | * we might write invalid pmds, when the kernel is relocated |
| 218 | * cleanup_highmap() fixes this up along with the mappings |
| 219 | * beyond _end. |
| 220 | */ |
| 221 | |
| 222 | pmd = fixup_pointer(level2_kernel_pgt, physaddr); |
| 223 | for (i = 0; i < PTRS_PER_PMD; i++) { |
| 224 | if (pmd[i] & _PAGE_PRESENT) |
| 225 | pmd[i] += load_delta; |
| 226 | } |
| 227 | |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 228 | /* |
| 229 | * Fixup phys_base - remove the memory encryption mask to obtain |
| 230 | * the true physical address. |
| 231 | */ |
Kirill A. Shutemov | 4fa5662 | 2018-02-14 21:25:36 +0300 | [diff] [blame] | 232 | *fixup_long(&phys_base, physaddr) += load_delta - sme_get_me_mask(); |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 233 | |
Tom Lendacky | 107cd25 | 2018-01-10 13:26:34 -0600 | [diff] [blame] | 234 | /* Encrypt the kernel and related (if SME is active) */ |
| 235 | sme_encrypt_kernel(bp); |
Tom Lendacky | 5868f36 | 2017-07-17 16:10:05 -0500 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * Return the SME encryption mask (if SME is active) to be used as a |
| 239 | * modifier for the initial pgdir entry programmed into CR3. |
| 240 | */ |
| 241 | return sme_get_me_mask(); |
| 242 | } |
| 243 | |
| 244 | unsigned long __startup_secondary_64(void) |
| 245 | { |
| 246 | /* |
| 247 | * Return the SME encryption mask (if SME is active) to be used as a |
| 248 | * modifier for the initial pgdir entry programmed into CR3. |
| 249 | */ |
| 250 | return sme_get_me_mask(); |
Kirill A. Shutemov | c88d7150 | 2017-06-06 14:31:26 +0300 | [diff] [blame] | 251 | } |
| 252 | |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 253 | /* Wipe all early page tables except for the kernel symbol map */ |
| 254 | static void __init reset_early_page_tables(void) |
Vivek Goyal | cfd243d | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 255 | { |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 256 | memset(early_top_pgt, 0, sizeof(pgd_t)*(PTRS_PER_PGD-1)); |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 257 | next_early_pgt = 0; |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 258 | write_cr3(__sme_pa_nodebug(early_top_pgt)); |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* Create a new PMD entry */ |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 262 | int __init __early_make_pgtable(unsigned long address, pmdval_t pmd) |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 263 | { |
| 264 | unsigned long physaddr = address - __PAGE_OFFSET; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 265 | pgdval_t pgd, *pgd_p; |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 266 | p4dval_t p4d, *p4d_p; |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 267 | pudval_t pud, *pud_p; |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 268 | pmdval_t *pmd_p; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 269 | |
| 270 | /* Invalid address or early pgt is done ? */ |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 271 | if (physaddr >= MAXMEM || read_cr3_pa() != __pa_nodebug(early_top_pgt)) |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 272 | return -1; |
| 273 | |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 274 | again: |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 275 | pgd_p = &early_top_pgt[pgd_index(address)].pgd; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 276 | pgd = *pgd_p; |
| 277 | |
| 278 | /* |
| 279 | * The use of __START_KERNEL_map rather than __PAGE_OFFSET here is |
| 280 | * critical -- __PAGE_OFFSET would point us back into the dynamic |
| 281 | * range and we might end up looping forever... |
| 282 | */ |
Kirill A. Shutemov | ed7588d | 2018-05-18 13:35:24 +0300 | [diff] [blame] | 283 | if (!pgtable_l5_enabled()) |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 284 | p4d_p = pgd_p; |
| 285 | else if (pgd) |
| 286 | p4d_p = (p4dval_t *)((pgd & PTE_PFN_MASK) + __START_KERNEL_map - phys_base); |
| 287 | else { |
| 288 | if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) { |
| 289 | reset_early_page_tables(); |
| 290 | goto again; |
| 291 | } |
| 292 | |
| 293 | p4d_p = (p4dval_t *)early_dynamic_pgts[next_early_pgt++]; |
| 294 | memset(p4d_p, 0, sizeof(*p4d_p) * PTRS_PER_P4D); |
| 295 | *pgd_p = (pgdval_t)p4d_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE; |
| 296 | } |
| 297 | p4d_p += p4d_index(address); |
| 298 | p4d = *p4d_p; |
| 299 | |
| 300 | if (p4d) |
| 301 | pud_p = (pudval_t *)((p4d & PTE_PFN_MASK) + __START_KERNEL_map - phys_base); |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 302 | else { |
| 303 | if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) { |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 304 | reset_early_page_tables(); |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 305 | goto again; |
| 306 | } |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 307 | |
| 308 | pud_p = (pudval_t *)early_dynamic_pgts[next_early_pgt++]; |
Alexander Kuleshov | a91bbe0 | 2016-02-09 19:44:54 +0600 | [diff] [blame] | 309 | memset(pud_p, 0, sizeof(*pud_p) * PTRS_PER_PUD); |
Kirill A. Shutemov | 032370b | 2017-06-06 14:31:28 +0300 | [diff] [blame] | 310 | *p4d_p = (p4dval_t)pud_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 311 | } |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 312 | pud_p += pud_index(address); |
| 313 | pud = *pud_p; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 314 | |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 315 | if (pud) |
| 316 | pmd_p = (pmdval_t *)((pud & PTE_PFN_MASK) + __START_KERNEL_map - phys_base); |
| 317 | else { |
| 318 | if (next_early_pgt >= EARLY_DYNAMIC_PAGE_TABLES) { |
| 319 | reset_early_page_tables(); |
| 320 | goto again; |
| 321 | } |
| 322 | |
| 323 | pmd_p = (pmdval_t *)early_dynamic_pgts[next_early_pgt++]; |
Alexander Kuleshov | a91bbe0 | 2016-02-09 19:44:54 +0600 | [diff] [blame] | 324 | memset(pmd_p, 0, sizeof(*pmd_p) * PTRS_PER_PMD); |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 325 | *pud_p = (pudval_t)pmd_p - __START_KERNEL_map + phys_base + _KERNPG_TABLE; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 326 | } |
Yinghai Lu | 6b9c75a | 2013-01-24 12:19:53 -0800 | [diff] [blame] | 327 | pmd_p[pmd_index(address)] = pmd; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 328 | |
| 329 | return 0; |
Vivek Goyal | cfd243d | 2007-05-02 19:27:07 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 332 | int __init early_make_pgtable(unsigned long address) |
| 333 | { |
| 334 | unsigned long physaddr = address - __PAGE_OFFSET; |
| 335 | pmdval_t pmd; |
| 336 | |
| 337 | pmd = (physaddr & PMD_MASK) + early_pmd_flags; |
| 338 | |
| 339 | return __early_make_pgtable(address, pmd); |
| 340 | } |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* Don't add a printk in there. printk relies on the PDA which is not initialized |
| 343 | yet. */ |
| 344 | static void __init clear_bss(void) |
| 345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | memset(__bss_start, 0, |
Andi Kleen | 2bc0414 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 347 | (unsigned long) __bss_stop - (unsigned long) __bss_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Yinghai Lu | f1da834 | 2013-01-24 12:19:57 -0800 | [diff] [blame] | 350 | static unsigned long get_cmd_line_ptr(void) |
| 351 | { |
| 352 | unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr; |
| 353 | |
Yinghai Lu | ee92d81 | 2013-01-28 20:16:44 -0800 | [diff] [blame] | 354 | cmd_line_ptr |= (u64)boot_params.ext_cmd_line_ptr << 32; |
| 355 | |
Yinghai Lu | f1da834 | 2013-01-24 12:19:57 -0800 | [diff] [blame] | 356 | return cmd_line_ptr; |
| 357 | } |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | static void __init copy_bootdata(char *real_mode_data) |
| 360 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | char * command_line; |
Yinghai Lu | f1da834 | 2013-01-24 12:19:57 -0800 | [diff] [blame] | 362 | unsigned long cmd_line_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 364 | /* |
| 365 | * If SME is active, this will create decrypted mappings of the |
| 366 | * boot data in advance of the copy operations. |
| 367 | */ |
| 368 | sme_map_bootdata(real_mode_data); |
| 369 | |
H. Peter Anvin | 30c8264 | 2007-10-15 17:13:22 -0700 | [diff] [blame] | 370 | memcpy(&boot_params, real_mode_data, sizeof boot_params); |
H. Peter Anvin | 5dcd14e | 2013-01-29 01:05:24 -0800 | [diff] [blame] | 371 | sanitize_boot_params(&boot_params); |
Yinghai Lu | f1da834 | 2013-01-24 12:19:57 -0800 | [diff] [blame] | 372 | cmd_line_ptr = get_cmd_line_ptr(); |
| 373 | if (cmd_line_ptr) { |
| 374 | command_line = __va(cmd_line_ptr); |
H. Peter Anvin | 30c8264 | 2007-10-15 17:13:22 -0700 | [diff] [blame] | 375 | memcpy(boot_command_line, command_line, COMMAND_LINE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * The old boot data is no longer needed and won't be reserved, |
| 380 | * freeing up that memory for use by the system. If SME is active, |
| 381 | * we need to remove the mappings that were created so that the |
| 382 | * memory doesn't remain mapped as decrypted. |
| 383 | */ |
| 384 | sme_unmap_bootdata(real_mode_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Andi Kleen | 2605fc2 | 2014-05-02 00:44:37 +0200 | [diff] [blame] | 387 | asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Ingo Molnar | b4e0409 | 2008-02-21 13:45:16 +0100 | [diff] [blame] | 389 | /* |
| 390 | * Build-time sanity checks on the kernel image and module |
| 391 | * area mappings. (these are purely build-time and produce no code) |
| 392 | */ |
Borislav Petkov | 8e3c2a8 | 2013-03-04 21:16:17 +0100 | [diff] [blame] | 393 | BUILD_BUG_ON(MODULES_VADDR < __START_KERNEL_map); |
| 394 | BUILD_BUG_ON(MODULES_VADDR - __START_KERNEL_map < KERNEL_IMAGE_SIZE); |
Ingo Molnar | b4e0409 | 2008-02-21 13:45:16 +0100 | [diff] [blame] | 395 | BUILD_BUG_ON(MODULES_LEN + KERNEL_IMAGE_SIZE > 2*PUD_SIZE); |
Borislav Petkov | 8e3c2a8 | 2013-03-04 21:16:17 +0100 | [diff] [blame] | 396 | BUILD_BUG_ON((__START_KERNEL_map & ~PMD_MASK) != 0); |
Ingo Molnar | b4e0409 | 2008-02-21 13:45:16 +0100 | [diff] [blame] | 397 | BUILD_BUG_ON((MODULES_VADDR & ~PMD_MASK) != 0); |
| 398 | BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL)); |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 399 | MAYBE_BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) == |
Ingo Molnar | b4e0409 | 2008-02-21 13:45:16 +0100 | [diff] [blame] | 400 | (__START_KERNEL & PGDIR_MASK))); |
Jan Beulich | 66d4bdf | 2008-07-31 16:48:31 +0100 | [diff] [blame] | 401 | BUILD_BUG_ON(__fix_to_virt(__end_of_fixed_addresses) <= MODULES_END); |
Ingo Molnar | b4e0409 | 2008-02-21 13:45:16 +0100 | [diff] [blame] | 402 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 403 | cr4_init_shadow(); |
| 404 | |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 405 | /* Kill off the identity-map trampoline */ |
| 406 | reset_early_page_tables(); |
| 407 | |
Yinghai Lu | 3df0af0 | 2006-12-07 02:14:12 +0100 | [diff] [blame] | 408 | clear_bss(); |
| 409 | |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 410 | clear_page(init_top_pgt); |
Andrey Ryabinin | d0f77d4 | 2015-07-02 12:09:33 +0300 | [diff] [blame] | 411 | |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 412 | /* |
| 413 | * SME support may update early_pmd_flags to include the memory |
| 414 | * encryption mask, so it needs to be called before anything |
| 415 | * that may generate a page fault. |
| 416 | */ |
| 417 | sme_early_init(); |
| 418 | |
Alexander Popov | 5d5aa3c | 2015-07-02 12:09:34 +0300 | [diff] [blame] | 419 | kasan_early_init(); |
| 420 | |
Thomas Gleixner | 588787f | 2017-08-28 08:47:47 +0200 | [diff] [blame] | 421 | idt_setup_early_handler(); |
Siddha, Suresh B | f6c2e33 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 422 | |
Yinghai Lu | fa2bbce | 2013-01-24 12:19:49 -0800 | [diff] [blame] | 423 | copy_bootdata(__va(real_mode_data)); |
| 424 | |
Fenghua Yu | feddc9d | 2012-12-20 23:44:30 -0800 | [diff] [blame] | 425 | /* |
| 426 | * Load microcode early on BSP. |
| 427 | */ |
| 428 | load_ucode_bsp(); |
| 429 | |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 430 | /* set init_top_pgt kernel high mapping*/ |
| 431 | init_top_pgt[511] = early_top_pgt[511]; |
H. Peter Anvin | 8170e6b | 2013-01-24 12:19:52 -0800 | [diff] [blame] | 432 | |
Jeremy Fitzhardinge | f97013f | 2008-06-25 00:19:18 -0400 | [diff] [blame] | 433 | x86_64_start_reservations(real_mode_data); |
| 434 | } |
| 435 | |
| 436 | void __init x86_64_start_reservations(char *real_mode_data) |
| 437 | { |
Yinghai Lu | fa2bbce | 2013-01-24 12:19:49 -0800 | [diff] [blame] | 438 | /* version is always not zero if it is copied */ |
| 439 | if (!boot_params.hdr.version) |
| 440 | copy_bootdata(__va(real_mode_data)); |
Yinghai Lu | 9de819f | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 441 | |
Luis R. Rodriguez | 8d152e7 | 2016-04-13 17:04:34 -0700 | [diff] [blame] | 442 | x86_early_init_platform_quirks(); |
Andi Kleen | 7517527 | 2008-01-30 13:33:17 +0100 | [diff] [blame] | 443 | |
Andy Shevchenko | 3fda5bb | 2016-01-15 22:11:07 +0200 | [diff] [blame] | 444 | switch (boot_params.hdr.hardware_subarch) { |
| 445 | case X86_SUBARCH_INTEL_MID: |
| 446 | x86_intel_mid_early_setup(); |
| 447 | break; |
| 448 | default: |
| 449 | break; |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | start_kernel(); |
| 453 | } |