1 // SPDX-License-Identifier: GPL-2.0
3 * prepare to run common code
5 * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
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>
17 #include <linux/memblock.h>
18 #include <linux/mem_encrypt.h>
20 #include <asm/processor.h>
21 #include <asm/proto.h>
23 #include <asm/setup.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>
37 * Manage page tables very early on.
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
);
43 #define __head __section(.head.text)
45 static void __head
*fixup_pointer(void *ptr
, unsigned long physaddr
)
47 return ptr
- (void *)_text
+ (void *)physaddr
;
50 unsigned long __head
__startup_64(unsigned long physaddr
,
51 struct boot_params
*bp
)
53 unsigned long load_delta
, *p
;
54 unsigned long pgtable_flags
;
58 pmdval_t
*pmd
, pmd_entry
;
60 unsigned int *next_pgt_ptr
;
62 /* Is the address too large? */
63 if (physaddr
>> MAX_PHYSMEM_BITS
)
67 * Compute the delta between the address I am compiled to run at
68 * and the address I am actually running at.
70 load_delta
= physaddr
- (unsigned long)(_text
- __START_KERNEL_map
);
72 /* Is the address not 2M aligned? */
73 if (load_delta
& ~PMD_PAGE_MASK
)
76 /* Activate Secure Memory Encryption (SME) if supported and enabled */
79 /* Include the SME encryption mask in the fixup value */
80 load_delta
+= sme_get_me_mask();
82 /* Fixup the physical addresses in the page table */
84 pgd
= fixup_pointer(&early_top_pgt
, physaddr
);
85 pgd
[pgd_index(__START_KERNEL_map
)] += load_delta
;
87 if (IS_ENABLED(CONFIG_X86_5LEVEL
)) {
88 p4d
= fixup_pointer(&level4_kernel_pgt
, physaddr
);
89 p4d
[511] += load_delta
;
92 pud
= fixup_pointer(&level3_kernel_pgt
, physaddr
);
93 pud
[510] += load_delta
;
94 pud
[511] += load_delta
;
96 pmd
= fixup_pointer(level2_fixmap_pgt
, physaddr
);
97 for (i
= FIXMAP_PMD_TOP
; i
> FIXMAP_PMD_TOP
- FIXMAP_PMD_NUM
; i
--)
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.
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
);
111 pgtable_flags
= _KERNPG_TABLE_NOENC
+ sme_get_me_mask();
113 if (IS_ENABLED(CONFIG_X86_5LEVEL
)) {
114 p4d
= fixup_pointer(early_dynamic_pgts
[next_early_pgt
++], physaddr
);
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
;
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
;
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
;
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
;
133 pmd_entry
= __PAGE_KERNEL_LARGE_EXEC
& ~_PAGE_GLOBAL
;
134 pmd_entry
+= sme_get_me_mask();
135 pmd_entry
+= physaddr
;
137 for (i
= 0; i
< DIV_ROUND_UP(_end
- _text
, PMD_SIZE
); i
++) {
138 int idx
= i
+ (physaddr
>> PMD_SHIFT
);
140 pmd
[idx
% PTRS_PER_PMD
] = pmd_entry
+ i
* PMD_SIZE
;
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
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.
159 pmd
= fixup_pointer(level2_kernel_pgt
, physaddr
);
161 /* invalidate pages before the kernel image */
162 for (i
= 0; i
< pmd_index((unsigned long)_text
); i
++)
163 pmd
[i
] &= ~_PAGE_PRESENT
;
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
;
170 /* invalidate pages after the kernel image */
171 for (; i
< PTRS_PER_PMD
; i
++)
172 pmd
[i
] &= ~_PAGE_PRESENT
;
175 * Fixup phys_base - remove the memory encryption mask to obtain
176 * the true physical address.
178 p
= fixup_pointer(&phys_base
, physaddr
);
179 *p
+= load_delta
- sme_get_me_mask();
181 /* Encrypt the kernel and related (if SME is active) */
182 sme_encrypt_kernel(bp
);
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.
188 return sme_get_me_mask();
191 unsigned long __startup_secondary_64(void)
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.
197 return sme_get_me_mask();
200 /* Wipe all early page tables except for the kernel symbol map */
201 static void __init
reset_early_page_tables(void)
203 memset(early_top_pgt
, 0, sizeof(pgd_t
)*(PTRS_PER_PGD
-1));
205 write_cr3(__sme_pa_nodebug(early_top_pgt
));
208 /* Create a new PMD entry */
209 int __init
__early_make_pgtable(unsigned long address
, pmdval_t pmd
)
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
;
217 /* Invalid address or early pgt is done ? */
218 if (physaddr
>= MAXMEM
|| read_cr3_pa() != __pa_nodebug(early_top_pgt
))
222 pgd_p
= &early_top_pgt
[pgd_index(address
)].pgd
;
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...
230 if (!IS_ENABLED(CONFIG_X86_5LEVEL
))
233 p4d_p
= (p4dval_t
*)((pgd
& PTE_PFN_MASK
) + __START_KERNEL_map
- phys_base
);
235 if (next_early_pgt
>= EARLY_DYNAMIC_PAGE_TABLES
) {
236 reset_early_page_tables();
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
;
244 p4d_p
+= p4d_index(address
);
248 pud_p
= (pudval_t
*)((p4d
& PTE_PFN_MASK
) + __START_KERNEL_map
- phys_base
);
250 if (next_early_pgt
>= EARLY_DYNAMIC_PAGE_TABLES
) {
251 reset_early_page_tables();
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
;
259 pud_p
+= pud_index(address
);
263 pmd_p
= (pmdval_t
*)((pud
& PTE_PFN_MASK
) + __START_KERNEL_map
- phys_base
);
265 if (next_early_pgt
>= EARLY_DYNAMIC_PAGE_TABLES
) {
266 reset_early_page_tables();
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
;
274 pmd_p
[pmd_index(address
)] = pmd
;
279 int __init
early_make_pgtable(unsigned long address
)
281 unsigned long physaddr
= address
- __PAGE_OFFSET
;
284 pmd
= (physaddr
& PMD_MASK
) + early_pmd_flags
;
286 return __early_make_pgtable(address
, pmd
);
289 /* Don't add a printk in there. printk relies on the PDA which is not initialized
291 static void __init
clear_bss(void)
293 memset(__bss_start
, 0,
294 (unsigned long) __bss_stop
- (unsigned long) __bss_start
);
297 static unsigned long get_cmd_line_ptr(void)
299 unsigned long cmd_line_ptr
= boot_params
.hdr
.cmd_line_ptr
;
301 cmd_line_ptr
|= (u64
)boot_params
.ext_cmd_line_ptr
<< 32;
306 static void __init
copy_bootdata(char *real_mode_data
)
309 unsigned long cmd_line_ptr
;
312 * If SME is active, this will create decrypted mappings of the
313 * boot data in advance of the copy operations.
315 sme_map_bootdata(real_mode_data
);
317 memcpy(&boot_params
, real_mode_data
, sizeof boot_params
);
318 sanitize_boot_params(&boot_params
);
319 cmd_line_ptr
= get_cmd_line_ptr();
321 command_line
= __va(cmd_line_ptr
);
322 memcpy(boot_command_line
, command_line
, COMMAND_LINE_SIZE
);
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.
331 sme_unmap_bootdata(real_mode_data
);
334 asmlinkage __visible
void __init
x86_64_start_kernel(char * real_mode_data
)
337 * Build-time sanity checks on the kernel image and module
338 * area mappings. (these are purely build-time and produce no code)
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
);
352 /* Kill off the identity-map trampoline */
353 reset_early_page_tables();
357 clear_page(init_top_pgt
);
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.
368 idt_setup_early_handler();
370 copy_bootdata(__va(real_mode_data
));
373 * Load microcode early on BSP.
377 /* set init_top_pgt kernel high mapping*/
378 init_top_pgt
[511] = early_top_pgt
[511];
380 x86_64_start_reservations(real_mode_data
);
383 void __init
x86_64_start_reservations(char *real_mode_data
)
385 /* version is always not zero if it is copied */
386 if (!boot_params
.hdr
.version
)
387 copy_bootdata(__va(real_mode_data
));
389 x86_early_init_platform_quirks();
391 switch (boot_params
.hdr
.hardware_subarch
) {
392 case X86_SUBARCH_INTEL_MID
:
393 x86_intel_mid_early_setup();