1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */ 2250c2277SThomas Gleixner/* 35b171e82SAlexander Kuleshov * linux/arch/x86/kernel/head_64.S -- start in 32bit and switch to 64bit 4250c2277SThomas Gleixner * 5250c2277SThomas Gleixner * Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE 6250c2277SThomas Gleixner * Copyright (C) 2000 Pavel Machek <pavel@suse.cz> 7250c2277SThomas Gleixner * Copyright (C) 2000 Karsten Keil <kkeil@suse.de> 8250c2277SThomas Gleixner * Copyright (C) 2001,2002 Andi Kleen <ak@suse.de> 9250c2277SThomas Gleixner * Copyright (C) 2005 Eric Biederman <ebiederm@xmission.com> 10250c2277SThomas Gleixner */ 11250c2277SThomas Gleixner 12250c2277SThomas Gleixner 13250c2277SThomas Gleixner#include <linux/linkage.h> 14250c2277SThomas Gleixner#include <linux/threads.h> 15250c2277SThomas Gleixner#include <linux/init.h> 16ca5999fdSMike Rapoport#include <linux/pgtable.h> 1765fddcfcSMike Rapoport#include <asm/segment.h> 18250c2277SThomas Gleixner#include <asm/page.h> 19250c2277SThomas Gleixner#include <asm/msr.h> 20250c2277SThomas Gleixner#include <asm/cache.h> 21369101daSCyrill Gorcunov#include <asm/processor-flags.h> 22b12d8db8STejun Heo#include <asm/percpu.h> 239900aa2fSH. Peter Anvin#include <asm/nops.h> 247bbcdb1cSAndy Lutomirski#include "../entry/calling.h" 25784d5699SAl Viro#include <asm/export.h> 26bd89004fSPeter Zijlstra#include <asm/nospec-branch.h> 2705ab1d8aSFeng Tang#include <asm/fixmap.h> 28250c2277SThomas Gleixner 2975da04f7SThomas Gleixner/* 3075da04f7SThomas Gleixner * We are not able to switch in one step to the final KERNEL ADDRESS SPACE 31250c2277SThomas Gleixner * because we need identity-mapped pages. 32250c2277SThomas Gleixner */ 33b9952ec7SKirill A. Shutemov#define l4_index(x) (((x) >> 39) & 511) 34a6523748SEduardo Habkost#define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1)) 35a6523748SEduardo Habkost 36b9952ec7SKirill A. ShutemovL4_PAGE_OFFSET = l4_index(__PAGE_OFFSET_BASE_L4) 37b9952ec7SKirill A. ShutemovL4_START_KERNEL = l4_index(__START_KERNEL_map) 38b9952ec7SKirill A. Shutemov 39a6523748SEduardo HabkostL3_START_KERNEL = pud_index(__START_KERNEL_map) 40a6523748SEduardo Habkost 41250c2277SThomas Gleixner .text 424ae59b91STim Abbott __HEAD 43250c2277SThomas Gleixner .code64 4437818afdSJiri SlabySYM_CODE_START_NOALIGN(startup_64) 452704fbb6SJosh Poimboeuf UNWIND_HINT_EMPTY 46250c2277SThomas Gleixner /* 471256276cSKonrad Rzeszutek Wilk * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, 48250c2277SThomas Gleixner * and someone has loaded an identity mapped page table 49250c2277SThomas Gleixner * for us. These identity mapped page tables map all of the 50250c2277SThomas Gleixner * kernel pages and possibly all of memory. 51250c2277SThomas Gleixner * 528170e6beSH. Peter Anvin * %rsi holds a physical pointer to real_mode_data. 53250c2277SThomas Gleixner * 54250c2277SThomas Gleixner * We come here either directly from a 64bit bootloader, or from 555b171e82SAlexander Kuleshov * arch/x86/boot/compressed/head_64.S. 56250c2277SThomas Gleixner * 57250c2277SThomas Gleixner * We only come here initially at boot nothing else comes here. 58250c2277SThomas Gleixner * 59250c2277SThomas Gleixner * Since we may be loaded at an address different from what we were 60250c2277SThomas Gleixner * compiled to run at we first fixup the physical addresses in our page 61250c2277SThomas Gleixner * tables and then reload them. 62250c2277SThomas Gleixner */ 63250c2277SThomas Gleixner 6422dc3918SJosh Poimboeuf /* Set up the stack for verify_cpu(), similar to initial_stack below */ 656627eb25SH. Peter Anvin (Intel) leaq (__end_init_task - FRAME_SIZE)(%rip), %rsp 6691ed140dSBorislav Petkov 67866b556eSJoerg Roedel leaq _text(%rip), %rdi 68469693d8SMichael Roth 69469693d8SMichael Roth /* 70469693d8SMichael Roth * initial_gs points to initial fixed_percpu_data struct with storage for 71469693d8SMichael Roth * the stack protector canary. Global pointer fixups are needed at this 72469693d8SMichael Roth * stage, so apply them as is done in fixup_pointer(), and initialize %gs 73469693d8SMichael Roth * such that the canary can be accessed at %gs:40 for subsequent C calls. 74469693d8SMichael Roth */ 75469693d8SMichael Roth movl $MSR_GS_BASE, %ecx 76469693d8SMichael Roth movq initial_gs(%rip), %rax 77469693d8SMichael Roth movq $_text, %rdx 78469693d8SMichael Roth subq %rdx, %rax 79469693d8SMichael Roth addq %rdi, %rax 80469693d8SMichael Roth movq %rax, %rdx 81469693d8SMichael Roth shrq $32, %rdx 82469693d8SMichael Roth wrmsr 83469693d8SMichael Roth 84866b556eSJoerg Roedel pushq %rsi 85866b556eSJoerg Roedel call startup_64_setup_env 86866b556eSJoerg Roedel popq %rsi 87866b556eSJoerg Roedel 88bcce8290SMichael Roth#ifdef CONFIG_AMD_MEM_ENCRYPT 89bcce8290SMichael Roth /* 90bcce8290SMichael Roth * Activate SEV/SME memory encryption if supported/enabled. This needs to 91bcce8290SMichael Roth * be done now, since this also includes setup of the SEV-SNP CPUID table, 92bcce8290SMichael Roth * which needs to be done before any CPUID instructions are executed in 93bcce8290SMichael Roth * subsequent code. 94bcce8290SMichael Roth */ 95bcce8290SMichael Roth movq %rsi, %rdi 96bcce8290SMichael Roth pushq %rsi 97bcce8290SMichael Roth call sme_enable 98bcce8290SMichael Roth popq %rsi 99bcce8290SMichael Roth#endif 100bcce8290SMichael Roth 101866b556eSJoerg Roedel /* Now switch to __KERNEL_CS so IRET works reliably */ 102866b556eSJoerg Roedel pushq $__KERNEL_CS 103866b556eSJoerg Roedel leaq .Lon_kernel_cs(%rip), %rax 104866b556eSJoerg Roedel pushq %rax 105866b556eSJoerg Roedel lretq 106866b556eSJoerg Roedel 107866b556eSJoerg Roedel.Lon_kernel_cs: 108866b556eSJoerg Roedel UNWIND_HINT_EMPTY 109866b556eSJoerg Roedel 11004633df0SBorislav Petkov /* Sanitize CPU configuration */ 11104633df0SBorislav Petkov call verify_cpu 11204633df0SBorislav Petkov 1135868f365STom Lendacky /* 1145868f365STom Lendacky * Perform pagetable fixups. Additionally, if SME is active, encrypt 1155868f365STom Lendacky * the kernel and retrieve the modifier (SME encryption mask if SME 1165868f365STom Lendacky * is active) to be added to the initial pgdir entry that will be 1175868f365STom Lendacky * programmed into CR3. 1185868f365STom Lendacky */ 119250c2277SThomas Gleixner leaq _text(%rip), %rdi 120c88d7150SKirill A. Shutemov pushq %rsi 121c88d7150SKirill A. Shutemov call __startup_64 122c88d7150SKirill A. Shutemov popq %rsi 123250c2277SThomas Gleixner 1245868f365STom Lendacky /* Form the CR3 value being sure to include the CR3 modifier */ 1255868f365STom Lendacky addq $(early_top_pgt - __START_KERNEL_map), %rax 1268170e6beSH. Peter Anvin jmp 1f 12737818afdSJiri SlabySYM_CODE_END(startup_64) 12837818afdSJiri Slaby 129bc7b11c0SJiri SlabySYM_CODE_START(secondary_startup_64) 1302704fbb6SJosh Poimboeuf UNWIND_HINT_EMPTY 1313e3f0695SPeter Zijlstra ANNOTATE_NOENDBR 132250c2277SThomas Gleixner /* 1331256276cSKonrad Rzeszutek Wilk * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, 134250c2277SThomas Gleixner * and someone has loaded a mapped page table. 135250c2277SThomas Gleixner * 1368170e6beSH. Peter Anvin * %rsi holds a physical pointer to real_mode_data. 137250c2277SThomas Gleixner * 138250c2277SThomas Gleixner * We come here either from startup_64 (using physical addresses) 139250c2277SThomas Gleixner * or from trampoline.S (using virtual addresses). 140250c2277SThomas Gleixner * 141250c2277SThomas Gleixner * Using virtual addresses from trampoline.S removes the need 142250c2277SThomas Gleixner * to have any identity mapped pages in the kernel page table 143250c2277SThomas Gleixner * after the boot processor executes this code. 144250c2277SThomas Gleixner */ 145250c2277SThomas Gleixner 14604633df0SBorislav Petkov /* Sanitize CPU configuration */ 14704633df0SBorislav Petkov call verify_cpu 14804633df0SBorislav Petkov 1495868f365STom Lendacky /* 1503ecacdbdSJoerg Roedel * The secondary_startup_64_no_verify entry point is only used by 1513ecacdbdSJoerg Roedel * SEV-ES guests. In those guests the call to verify_cpu() would cause 1523ecacdbdSJoerg Roedel * #VC exceptions which can not be handled at this stage of secondary 1533ecacdbdSJoerg Roedel * CPU bringup. 1543ecacdbdSJoerg Roedel * 1553ecacdbdSJoerg Roedel * All non SEV-ES systems, especially Intel systems, need to execute 1563ecacdbdSJoerg Roedel * verify_cpu() above to make sure NX is enabled. 1573ecacdbdSJoerg Roedel */ 1583ecacdbdSJoerg RoedelSYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) 1593ecacdbdSJoerg Roedel UNWIND_HINT_EMPTY 1603e3f0695SPeter Zijlstra ANNOTATE_NOENDBR 1613ecacdbdSJoerg Roedel 1623ecacdbdSJoerg Roedel /* 1635868f365STom Lendacky * Retrieve the modifier (SME encryption mask if SME is active) to be 1645868f365STom Lendacky * added to the initial pgdir entry that will be programmed into CR3. 1655868f365STom Lendacky */ 166469693d8SMichael Roth#ifdef CONFIG_AMD_MEM_ENCRYPT 167469693d8SMichael Roth movq sme_me_mask, %rax 168469693d8SMichael Roth#else 169469693d8SMichael Roth xorq %rax, %rax 170469693d8SMichael Roth#endif 1715868f365STom Lendacky 1725868f365STom Lendacky /* Form the CR3 value being sure to include the CR3 modifier */ 1735868f365STom Lendacky addq $(init_top_pgt - __START_KERNEL_map), %rax 1748170e6beSH. Peter Anvin1: 1758170e6beSH. Peter Anvin 17677a512e3SSean Christopherson#ifdef CONFIG_X86_MCE 17777a512e3SSean Christopherson /* 17877a512e3SSean Christopherson * Preserve CR4.MCE if the kernel will enable #MC support. 17977a512e3SSean Christopherson * Clearing MCE may fault in some environments (that also force #MC 18077a512e3SSean Christopherson * support). Any machine check that occurs before #MC support is fully 18177a512e3SSean Christopherson * configured will crash the system regardless of the CR4.MCE value set 18277a512e3SSean Christopherson * here. 18377a512e3SSean Christopherson */ 18477a512e3SSean Christopherson movq %cr4, %rcx 18577a512e3SSean Christopherson andl $X86_CR4_MCE, %ecx 18677a512e3SSean Christopherson#else 18777a512e3SSean Christopherson movl $0, %ecx 18877a512e3SSean Christopherson#endif 18977a512e3SSean Christopherson 190032370b9SKirill A. Shutemov /* Enable PAE mode, PGE and LA57 */ 19177a512e3SSean Christopherson orl $(X86_CR4_PAE | X86_CR4_PGE), %ecx 192032370b9SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL 19339b95522SKirill A. Shutemov testl $1, __pgtable_l5_enabled(%rip) 1946f9dd329SKirill A. Shutemov jz 1f 195032370b9SKirill A. Shutemov orl $X86_CR4_LA57, %ecx 1966f9dd329SKirill A. Shutemov1: 197032370b9SKirill A. Shutemov#endif 1988170e6beSH. Peter Anvin movq %rcx, %cr4 199250c2277SThomas Gleixner 200032370b9SKirill A. Shutemov /* Setup early boot stage 4-/5-level pagetables. */ 201250c2277SThomas Gleixner addq phys_base(%rip), %rax 202c9f09539SJoerg Roedel 203c9f09539SJoerg Roedel /* 204c9f09539SJoerg Roedel * For SEV guests: Verify that the C-bit is correct. A malicious 205c9f09539SJoerg Roedel * hypervisor could lie about the C-bit position to perform a ROP 206c9f09539SJoerg Roedel * attack on the guest by writing to the unencrypted stack and wait for 207c9f09539SJoerg Roedel * the next RET instruction. 208c9f09539SJoerg Roedel * %rsi carries pointer to realmode data and is callee-clobbered. Save 209c9f09539SJoerg Roedel * and restore it. 210c9f09539SJoerg Roedel */ 211c9f09539SJoerg Roedel pushq %rsi 212c9f09539SJoerg Roedel movq %rax, %rdi 213c9f09539SJoerg Roedel call sev_verify_cbit 214c9f09539SJoerg Roedel popq %rsi 215c9f09539SJoerg Roedel 216f154f290SJoerg Roedel /* 217f154f290SJoerg Roedel * Switch to new page-table 218f154f290SJoerg Roedel * 219f154f290SJoerg Roedel * For the boot CPU this switches to early_top_pgt which still has the 220f154f290SJoerg Roedel * indentity mappings present. The secondary CPUs will switch to the 221f154f290SJoerg Roedel * init_top_pgt here, away from the trampoline_pgd and unmap the 222f154f290SJoerg Roedel * indentity mapped ranges. 223f154f290SJoerg Roedel */ 224250c2277SThomas Gleixner movq %rax, %cr3 225250c2277SThomas Gleixner 226f154f290SJoerg Roedel /* 227f154f290SJoerg Roedel * Do a global TLB flush after the CR3 switch to make sure the TLB 228f154f290SJoerg Roedel * entries from the identity mapping are flushed. 229f154f290SJoerg Roedel */ 230f154f290SJoerg Roedel movq %cr4, %rcx 231f154f290SJoerg Roedel movq %rcx, %rax 232f154f290SJoerg Roedel xorq $X86_CR4_PGE, %rcx 233f154f290SJoerg Roedel movq %rcx, %cr4 234f154f290SJoerg Roedel movq %rax, %cr4 235f154f290SJoerg Roedel 236250c2277SThomas Gleixner /* Ensure I am executing from virtual addresses */ 237250c2277SThomas Gleixner movq $1f, %rax 238bd89004fSPeter Zijlstra ANNOTATE_RETPOLINE_SAFE 239250c2277SThomas Gleixner jmp *%rax 240250c2277SThomas Gleixner1: 2412704fbb6SJosh Poimboeuf UNWIND_HINT_EMPTY 2423e3f0695SPeter Zijlstra ANNOTATE_NOENDBR // above 243250c2277SThomas Gleixner 244e04b8833SJoerg Roedel /* 245e04b8833SJoerg Roedel * We must switch to a new descriptor in kernel space for the GDT 246e04b8833SJoerg Roedel * because soon the kernel won't have access anymore to the userspace 247e04b8833SJoerg Roedel * addresses where we're currently running on. We have to do that here 248e04b8833SJoerg Roedel * because in 32bit we couldn't load a 64bit linear address. 249e04b8833SJoerg Roedel */ 250e04b8833SJoerg Roedel lgdt early_gdt_descr(%rip) 251e04b8833SJoerg Roedel 2527b99819dSJoerg Roedel /* set up data segments */ 2537b99819dSJoerg Roedel xorl %eax,%eax 2547b99819dSJoerg Roedel movl %eax,%ds 2557b99819dSJoerg Roedel movl %eax,%ss 2567b99819dSJoerg Roedel movl %eax,%es 2577b99819dSJoerg Roedel 2587b99819dSJoerg Roedel /* 2597b99819dSJoerg Roedel * We don't really need to load %fs or %gs, but load them anyway 2607b99819dSJoerg Roedel * to kill any stale realmode selectors. This allows execution 2617b99819dSJoerg Roedel * under VT hardware. 2627b99819dSJoerg Roedel */ 2637b99819dSJoerg Roedel movl %eax,%fs 2647b99819dSJoerg Roedel movl %eax,%gs 2657b99819dSJoerg Roedel 2667b99819dSJoerg Roedel /* Set up %gs. 2677b99819dSJoerg Roedel * 2687b99819dSJoerg Roedel * The base of %gs always points to fixed_percpu_data. If the 2697b99819dSJoerg Roedel * stack protector canary is enabled, it is located at %gs:40. 2707b99819dSJoerg Roedel * Note that, on SMP, the boot cpu uses init data section until 2717b99819dSJoerg Roedel * the per cpu areas are set up. 2727b99819dSJoerg Roedel */ 2737b99819dSJoerg Roedel movl $MSR_GS_BASE,%ecx 2747b99819dSJoerg Roedel movl initial_gs(%rip),%eax 2757b99819dSJoerg Roedel movl initial_gs+4(%rip),%edx 2767b99819dSJoerg Roedel wrmsr 2777b99819dSJoerg Roedel 2783add38cbSJoerg Roedel /* 2793add38cbSJoerg Roedel * Setup a boot time stack - Any secondary CPU will have lost its stack 2803add38cbSJoerg Roedel * by now because the cr3-switch above unmaps the real-mode stack 2813add38cbSJoerg Roedel */ 2823add38cbSJoerg Roedel movq initial_stack(%rip), %rsp 2833add38cbSJoerg Roedel 284f5963ba7SJoerg Roedel /* Setup and Load IDT */ 285f5963ba7SJoerg Roedel pushq %rsi 286f5963ba7SJoerg Roedel call early_setup_idt 287f5963ba7SJoerg Roedel popq %rsi 288f5963ba7SJoerg Roedel 289250c2277SThomas Gleixner /* Check if nx is implemented */ 290250c2277SThomas Gleixner movl $0x80000001, %eax 291250c2277SThomas Gleixner cpuid 292250c2277SThomas Gleixner movl %edx,%edi 293250c2277SThomas Gleixner 294250c2277SThomas Gleixner /* Setup EFER (Extended Feature Enable Register) */ 295250c2277SThomas Gleixner movl $MSR_EFER, %ecx 296250c2277SThomas Gleixner rdmsr 29777a512e3SSean Christopherson /* 29877a512e3SSean Christopherson * Preserve current value of EFER for comparison and to skip 29977a512e3SSean Christopherson * EFER writes if no change was made (for TDX guest) 30077a512e3SSean Christopherson */ 30177a512e3SSean Christopherson movl %eax, %edx 302250c2277SThomas Gleixner btsl $_EFER_SCE, %eax /* Enable System Call */ 303250c2277SThomas Gleixner btl $20,%edi /* No Execute supported? */ 304250c2277SThomas Gleixner jnc 1f 305250c2277SThomas Gleixner btsl $_EFER_NX, %eax 30678d77df7SH. Peter Anvin btsq $_PAGE_BIT_NX,early_pmd_flags(%rip) 307250c2277SThomas Gleixner 30877a512e3SSean Christopherson /* Avoid writing EFER if no change was made (for TDX guest) */ 30977a512e3SSean Christopherson1: cmpl %edx, %eax 31077a512e3SSean Christopherson je 1f 31177a512e3SSean Christopherson xor %edx, %edx 31277a512e3SSean Christopherson wrmsr /* Make changes effective */ 31377a512e3SSean Christopherson1: 314250c2277SThomas Gleixner /* Setup cr0 */ 315369101daSCyrill Gorcunov movl $CR0_STATE, %eax 316250c2277SThomas Gleixner /* Make changes effective */ 317250c2277SThomas Gleixner movq %rax, %cr0 318250c2277SThomas Gleixner 319250c2277SThomas Gleixner /* zero EFLAGS after setting rsp */ 320250c2277SThomas Gleixner pushq $0 321250c2277SThomas Gleixner popfq 322250c2277SThomas Gleixner 3238170e6beSH. Peter Anvin /* rsi is pointer to real mode structure with interesting info. 324250c2277SThomas Gleixner pass it to C */ 3258170e6beSH. Peter Anvin movq %rsi, %rdi 326250c2277SThomas Gleixner 32779d243a0SBorislav Petkov.Ljump_to_C_code: 328a9468df5SJosh Poimboeuf /* 329a9468df5SJosh Poimboeuf * Jump to run C code and to be on a real kernel address. 330250c2277SThomas Gleixner * Since we are running on identity-mapped space we have to jump 331250c2277SThomas Gleixner * to the full 64bit address, this is only possible as indirect 332250c2277SThomas Gleixner * jump. In addition we need to ensure %cs is set so we make this 333250c2277SThomas Gleixner * a far return. 3348170e6beSH. Peter Anvin * 3358170e6beSH. Peter Anvin * Note: do not change to far jump indirect with 64bit offset. 3368170e6beSH. Peter Anvin * 3378170e6beSH. Peter Anvin * AMD does not support far jump indirect with 64bit offset. 3388170e6beSH. Peter Anvin * AMD64 Architecture Programmer's Manual, Volume 3: states only 3398170e6beSH. Peter Anvin * JMP FAR mem16:16 FF /5 Far jump indirect, 3408170e6beSH. Peter Anvin * with the target specified by a far pointer in memory. 3418170e6beSH. Peter Anvin * JMP FAR mem16:32 FF /5 Far jump indirect, 3428170e6beSH. Peter Anvin * with the target specified by a far pointer in memory. 3438170e6beSH. Peter Anvin * 3448170e6beSH. Peter Anvin * Intel64 does support 64bit offset. 3458170e6beSH. Peter Anvin * Software Developer Manual Vol 2: states: 3468170e6beSH. Peter Anvin * FF /5 JMP m16:16 Jump far, absolute indirect, 3478170e6beSH. Peter Anvin * address given in m16:16 3488170e6beSH. Peter Anvin * FF /5 JMP m16:32 Jump far, absolute indirect, 3498170e6beSH. Peter Anvin * address given in m16:32. 3508170e6beSH. Peter Anvin * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect, 3518170e6beSH. Peter Anvin * address given in m16:64. 352250c2277SThomas Gleixner */ 35331dcfec1SJosh Poimboeuf pushq $.Lafter_lret # put return address on stack for unwinder 354a7bea830SJan Beulich xorl %ebp, %ebp # clear frame pointer 355250c2277SThomas Gleixner movq initial_code(%rip), %rax 356250c2277SThomas Gleixner pushq $__KERNEL_CS # set correct cs 357250c2277SThomas Gleixner pushq %rax # target address in negative space 358250c2277SThomas Gleixner lretq 35931dcfec1SJosh Poimboeuf.Lafter_lret: 3603e3f0695SPeter Zijlstra ANNOTATE_NOENDBR 361bc7b11c0SJiri SlabySYM_CODE_END(secondary_startup_64) 362250c2277SThomas Gleixner 36304633df0SBorislav Petkov#include "verify_cpu.S" 364c9f09539SJoerg Roedel#include "sev_verify_cbit.S" 36504633df0SBorislav Petkov 36642e78e97SFenghua Yu#ifdef CONFIG_HOTPLUG_CPU 36742e78e97SFenghua Yu/* 36842e78e97SFenghua Yu * Boot CPU0 entry point. It's called from play_dead(). Everything has been set 36942e78e97SFenghua Yu * up already except stack. We just set up stack here. Then call 37079d243a0SBorislav Petkov * start_secondary() via .Ljump_to_C_code. 37142e78e97SFenghua Yu */ 372bc7b11c0SJiri SlabySYM_CODE_START(start_cpu0) 373*e81dc127SThomas Gleixner ANNOTATE_NOENDBR 3742704fbb6SJosh Poimboeuf UNWIND_HINT_EMPTY 37561a73f5cSJosh Poimboeuf movq initial_stack(%rip), %rsp 37679d243a0SBorislav Petkov jmp .Ljump_to_C_code 377bc7b11c0SJiri SlabySYM_CODE_END(start_cpu0) 37842e78e97SFenghua Yu#endif 37942e78e97SFenghua Yu 3801aa9aa8eSJoerg Roedel#ifdef CONFIG_AMD_MEM_ENCRYPT 3811aa9aa8eSJoerg Roedel/* 3821aa9aa8eSJoerg Roedel * VC Exception handler used during early boot when running on kernel 3831aa9aa8eSJoerg Roedel * addresses, but before the switch to the idt_table can be made. 3841aa9aa8eSJoerg Roedel * The early_idt_handler_array can't be used here because it calls into a lot 3851aa9aa8eSJoerg Roedel * of __init code and this handler is also used during CPU offlining/onlining. 3861aa9aa8eSJoerg Roedel * Therefore this handler ends up in the .text section so that it stays around 3871aa9aa8eSJoerg Roedel * when .init.text is freed. 3881aa9aa8eSJoerg Roedel */ 3891aa9aa8eSJoerg RoedelSYM_CODE_START_NOALIGN(vc_boot_ghcb) 3901aa9aa8eSJoerg Roedel UNWIND_HINT_IRET_REGS offset=8 391e8d61bdfSPeter Zijlstra ENDBR 3921aa9aa8eSJoerg Roedel 393a09a6e23SPeter Zijlstra ANNOTATE_UNRET_END 394a09a6e23SPeter Zijlstra 3951aa9aa8eSJoerg Roedel /* Build pt_regs */ 3961aa9aa8eSJoerg Roedel PUSH_AND_CLEAR_REGS 3971aa9aa8eSJoerg Roedel 3981aa9aa8eSJoerg Roedel /* Call C handler */ 3991aa9aa8eSJoerg Roedel movq %rsp, %rdi 4001aa9aa8eSJoerg Roedel movq ORIG_RAX(%rsp), %rsi 4011aa9aa8eSJoerg Roedel movq initial_vc_handler(%rip), %rax 4021aa9aa8eSJoerg Roedel ANNOTATE_RETPOLINE_SAFE 4031aa9aa8eSJoerg Roedel call *%rax 4041aa9aa8eSJoerg Roedel 4051aa9aa8eSJoerg Roedel /* Unwind pt_regs */ 4061aa9aa8eSJoerg Roedel POP_REGS 4071aa9aa8eSJoerg Roedel 4081aa9aa8eSJoerg Roedel /* Remove Error Code */ 4091aa9aa8eSJoerg Roedel addq $8, %rsp 4101aa9aa8eSJoerg Roedel 4111aa9aa8eSJoerg Roedel iretq 4121aa9aa8eSJoerg RoedelSYM_CODE_END(vc_boot_ghcb) 4131aa9aa8eSJoerg Roedel#endif 4141aa9aa8eSJoerg Roedel 415b32f96c7SJosh Poimboeuf /* Both SMP bootup and ACPI suspend change these variables */ 416da5968aeSSam Ravnborg __REFDATA 4178170e6beSH. Peter Anvin .balign 8 418b1bd27b9SJiri SlabySYM_DATA(initial_code, .quad x86_64_start_kernel) 419b1bd27b9SJiri SlabySYM_DATA(initial_gs, .quad INIT_PER_CPU_VAR(fixed_percpu_data)) 4201aa9aa8eSJoerg Roedel#ifdef CONFIG_AMD_MEM_ENCRYPT 4211aa9aa8eSJoerg RoedelSYM_DATA(initial_vc_handler, .quad handle_vc_boot_ghcb) 4221aa9aa8eSJoerg Roedel#endif 423b1bd27b9SJiri Slaby 42422dc3918SJosh Poimboeuf/* 4256627eb25SH. Peter Anvin (Intel) * The FRAME_SIZE gap is a convention which helps the in-kernel unwinder 426b1bd27b9SJiri Slaby * reliably detect the end of the stack. 42722dc3918SJosh Poimboeuf */ 4286627eb25SH. Peter Anvin (Intel)SYM_DATA(initial_stack, .quad init_thread_union + THREAD_SIZE - FRAME_SIZE) 429b9af7c0dSSuresh Siddha __FINITDATA 430250c2277SThomas Gleixner 4318170e6beSH. Peter Anvin __INIT 432bc7b11c0SJiri SlabySYM_CODE_START(early_idt_handler_array) 433749c970aSAndi Kleen i = 0 434749c970aSAndi Kleen .rept NUM_EXCEPTION_VECTORS 43582c62fa0SJosh Poimboeuf .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0 4362704fbb6SJosh Poimboeuf UNWIND_HINT_IRET_REGS 4378f93402bSPeter Zijlstra ENDBR 4389900aa2fSH. Peter Anvin pushq $0 # Dummy error code, to make stack frame uniform 4392704fbb6SJosh Poimboeuf .else 4402704fbb6SJosh Poimboeuf UNWIND_HINT_IRET_REGS offset=8 4418f93402bSPeter Zijlstra ENDBR 4429900aa2fSH. Peter Anvin .endif 4439900aa2fSH. Peter Anvin pushq $i # 72(%rsp) Vector number 444cdeb6048SAndy Lutomirski jmp early_idt_handler_common 4452704fbb6SJosh Poimboeuf UNWIND_HINT_IRET_REGS 446749c970aSAndi Kleen i = i + 1 447cdeb6048SAndy Lutomirski .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc 448749c970aSAndi Kleen .endr 449bc7b11c0SJiri SlabySYM_CODE_END(early_idt_handler_array) 4505b2fc515SPeter Zijlstra ANNOTATE_NOENDBR // early_idt_handler_array[NUM_EXCEPTION_VECTORS] 4518866cd9dSRoland McGrath 452ef77e688SJiri SlabySYM_CODE_START_LOCAL(early_idt_handler_common) 4538f93402bSPeter Zijlstra UNWIND_HINT_IRET_REGS offset=16 454a09a6e23SPeter Zijlstra ANNOTATE_UNRET_END 455cdeb6048SAndy Lutomirski /* 456cdeb6048SAndy Lutomirski * The stack is the hardware frame, an error code or zero, and the 457cdeb6048SAndy Lutomirski * vector number. 458cdeb6048SAndy Lutomirski */ 4599900aa2fSH. Peter Anvin cld 4609900aa2fSH. Peter Anvin 461250c2277SThomas Gleixner incl early_recursion_flag(%rip) 4629900aa2fSH. Peter Anvin 4637bbcdb1cSAndy Lutomirski /* The vector number is currently in the pt_regs->di slot. */ 4647bbcdb1cSAndy Lutomirski pushq %rsi /* pt_regs->si */ 4657bbcdb1cSAndy Lutomirski movq 8(%rsp), %rsi /* RSI = vector number */ 4667bbcdb1cSAndy Lutomirski movq %rdi, 8(%rsp) /* pt_regs->di = RDI */ 4677bbcdb1cSAndy Lutomirski pushq %rdx /* pt_regs->dx */ 4687bbcdb1cSAndy Lutomirski pushq %rcx /* pt_regs->cx */ 4697bbcdb1cSAndy Lutomirski pushq %rax /* pt_regs->ax */ 4707bbcdb1cSAndy Lutomirski pushq %r8 /* pt_regs->r8 */ 4717bbcdb1cSAndy Lutomirski pushq %r9 /* pt_regs->r9 */ 4727bbcdb1cSAndy Lutomirski pushq %r10 /* pt_regs->r10 */ 4737bbcdb1cSAndy Lutomirski pushq %r11 /* pt_regs->r11 */ 4747bbcdb1cSAndy Lutomirski pushq %rbx /* pt_regs->bx */ 4757bbcdb1cSAndy Lutomirski pushq %rbp /* pt_regs->bp */ 4767bbcdb1cSAndy Lutomirski pushq %r12 /* pt_regs->r12 */ 4777bbcdb1cSAndy Lutomirski pushq %r13 /* pt_regs->r13 */ 4787bbcdb1cSAndy Lutomirski pushq %r14 /* pt_regs->r14 */ 4797bbcdb1cSAndy Lutomirski pushq %r15 /* pt_regs->r15 */ 4802704fbb6SJosh Poimboeuf UNWIND_HINT_REGS 4819900aa2fSH. Peter Anvin 4827bbcdb1cSAndy Lutomirski movq %rsp,%rdi /* RDI = pt_regs; RSI is already trapnr */ 4834b47cdbdSJoerg Roedel call do_early_exception 4849900aa2fSH. Peter Anvin 4859900aa2fSH. Peter Anvin decl early_recursion_flag(%rip) 48626c4ef9cSAndy Lutomirski jmp restore_regs_and_return_to_kernel 487ef77e688SJiri SlabySYM_CODE_END(early_idt_handler_common) 4889900aa2fSH. Peter Anvin 48974d8d9d5SJoerg Roedel#ifdef CONFIG_AMD_MEM_ENCRYPT 49074d8d9d5SJoerg Roedel/* 49174d8d9d5SJoerg Roedel * VC Exception handler used during very early boot. The 49274d8d9d5SJoerg Roedel * early_idt_handler_array can't be used because it returns via the 49374d8d9d5SJoerg Roedel * paravirtualized INTERRUPT_RETURN and pv-ops don't work that early. 49474d8d9d5SJoerg Roedel * 4958b87d8ceSPeter Zijlstra * XXX it does, fix this. 4968b87d8ceSPeter Zijlstra * 49774d8d9d5SJoerg Roedel * This handler will end up in the .init.text section and not be 49874d8d9d5SJoerg Roedel * available to boot secondary CPUs. 49974d8d9d5SJoerg Roedel */ 50074d8d9d5SJoerg RoedelSYM_CODE_START_NOALIGN(vc_no_ghcb) 50174d8d9d5SJoerg Roedel UNWIND_HINT_IRET_REGS offset=8 502e8d61bdfSPeter Zijlstra ENDBR 50374d8d9d5SJoerg Roedel 504a09a6e23SPeter Zijlstra ANNOTATE_UNRET_END 505a09a6e23SPeter Zijlstra 50674d8d9d5SJoerg Roedel /* Build pt_regs */ 50774d8d9d5SJoerg Roedel PUSH_AND_CLEAR_REGS 50874d8d9d5SJoerg Roedel 50974d8d9d5SJoerg Roedel /* Call C handler */ 51074d8d9d5SJoerg Roedel movq %rsp, %rdi 51174d8d9d5SJoerg Roedel movq ORIG_RAX(%rsp), %rsi 51274d8d9d5SJoerg Roedel call do_vc_no_ghcb 51374d8d9d5SJoerg Roedel 51474d8d9d5SJoerg Roedel /* Unwind pt_regs */ 51574d8d9d5SJoerg Roedel POP_REGS 51674d8d9d5SJoerg Roedel 51774d8d9d5SJoerg Roedel /* Remove Error Code */ 51874d8d9d5SJoerg Roedel addq $8, %rsp 51974d8d9d5SJoerg Roedel 52074d8d9d5SJoerg Roedel /* Pure iret required here - don't use INTERRUPT_RETURN */ 52174d8d9d5SJoerg Roedel iretq 52274d8d9d5SJoerg RoedelSYM_CODE_END(vc_no_ghcb) 52374d8d9d5SJoerg Roedel#endif 524b1bd27b9SJiri Slaby 525b1bd27b9SJiri Slaby#define SYM_DATA_START_PAGE_ALIGNED(name) \ 526b1bd27b9SJiri Slaby SYM_START(name, SYM_L_GLOBAL, .balign PAGE_SIZE) 527250c2277SThomas Gleixner 528d9e9a641SDave Hansen#ifdef CONFIG_PAGE_TABLE_ISOLATION 529d9e9a641SDave Hansen/* 530d9e9a641SDave Hansen * Each PGD needs to be 8k long and 8k aligned. We do not 531d9e9a641SDave Hansen * ever go out to userspace with these, so we do not 532d9e9a641SDave Hansen * strictly *need* the second page, but this allows us to 533d9e9a641SDave Hansen * have a single set_pgd() implementation that does not 534d9e9a641SDave Hansen * need to worry about whether it has 4k or 8k to work 535d9e9a641SDave Hansen * with. 536d9e9a641SDave Hansen * 537d9e9a641SDave Hansen * This ensures PGDs are 8k long: 538d9e9a641SDave Hansen */ 539d9e9a641SDave Hansen#define PTI_USER_PGD_FILL 512 540d9e9a641SDave Hansen/* This ensures they are 8k-aligned: */ 541b1bd27b9SJiri Slaby#define SYM_DATA_START_PTI_ALIGNED(name) \ 542b1bd27b9SJiri Slaby SYM_START(name, SYM_L_GLOBAL, .balign 2 * PAGE_SIZE) 543d9e9a641SDave Hansen#else 544b1bd27b9SJiri Slaby#define SYM_DATA_START_PTI_ALIGNED(name) \ 545b1bd27b9SJiri Slaby SYM_DATA_START_PAGE_ALIGNED(name) 546d9e9a641SDave Hansen#define PTI_USER_PGD_FILL 0 547d9e9a641SDave Hansen#endif 548d9e9a641SDave Hansen 549250c2277SThomas Gleixner/* Automate the creation of 1 to 1 mapping pmd entries */ 550250c2277SThomas Gleixner#define PMDS(START, PERM, COUNT) \ 551250c2277SThomas Gleixner i = 0 ; \ 552250c2277SThomas Gleixner .rept (COUNT) ; \ 5530e192b99SCyrill Gorcunov .quad (START) + (i << PMD_SHIFT) + (PERM) ; \ 554250c2277SThomas Gleixner i = i + 1 ; \ 555250c2277SThomas Gleixner .endr 556250c2277SThomas Gleixner 5578170e6beSH. Peter Anvin __INITDATA 5581a8770b7SJiri Slaby .balign 4 5591a8770b7SJiri Slaby 560b1bd27b9SJiri SlabySYM_DATA_START_PTI_ALIGNED(early_top_pgt) 5616f9dd329SKirill A. Shutemov .fill 512,8,0 562d9e9a641SDave Hansen .fill PTI_USER_PGD_FILL,8,0 563b1bd27b9SJiri SlabySYM_DATA_END(early_top_pgt) 5648170e6beSH. Peter Anvin 565b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(early_dynamic_pgts) 5668170e6beSH. Peter Anvin .fill 512*EARLY_DYNAMIC_PAGE_TABLES,8,0 567b1bd27b9SJiri SlabySYM_DATA_END(early_dynamic_pgts) 5688170e6beSH. Peter Anvin 569b1bd27b9SJiri SlabySYM_DATA(early_recursion_flag, .long 0) 5701a8770b7SJiri Slaby 571b9af7c0dSSuresh Siddha .data 5728170e6beSH. Peter Anvin 5737733607fSMaran Wilson#if defined(CONFIG_XEN_PV) || defined(CONFIG_PVH) 574b1bd27b9SJiri SlabySYM_DATA_START_PTI_ALIGNED(init_top_pgt) 57521729f81STom Lendacky .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC 576b9952ec7SKirill A. Shutemov .org init_top_pgt + L4_PAGE_OFFSET*8, 0 57721729f81STom Lendacky .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC 578b9952ec7SKirill A. Shutemov .org init_top_pgt + L4_START_KERNEL*8, 0 579250c2277SThomas Gleixner /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */ 58021729f81STom Lendacky .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC 581d9e9a641SDave Hansen .fill PTI_USER_PGD_FILL,8,0 582b1bd27b9SJiri SlabySYM_DATA_END(init_top_pgt) 583250c2277SThomas Gleixner 584b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level3_ident_pgt) 58521729f81STom Lendacky .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC 586250c2277SThomas Gleixner .fill 511, 8, 0 587b1bd27b9SJiri SlabySYM_DATA_END(level3_ident_pgt) 588b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level2_ident_pgt) 589430d4005SDave Hansen /* 590430d4005SDave Hansen * Since I easily can, map the first 1G. 5918170e6beSH. Peter Anvin * Don't set NX because code runs from these pages. 592430d4005SDave Hansen * 593430d4005SDave Hansen * Note: This sets _PAGE_GLOBAL despite whether 594430d4005SDave Hansen * the CPU supports it or it is enabled. But, 595430d4005SDave Hansen * the CPU should ignore the bit. 5968170e6beSH. Peter Anvin */ 5978170e6beSH. Peter Anvin PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD) 598b1bd27b9SJiri SlabySYM_DATA_END(level2_ident_pgt) 5994375c299SKirill A. Shutemov#else 600b1bd27b9SJiri SlabySYM_DATA_START_PTI_ALIGNED(init_top_pgt) 6014375c299SKirill A. Shutemov .fill 512,8,0 602d9e9a641SDave Hansen .fill PTI_USER_PGD_FILL,8,0 603b1bd27b9SJiri SlabySYM_DATA_END(init_top_pgt) 6048170e6beSH. Peter Anvin#endif 605250c2277SThomas Gleixner 606032370b9SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL 607b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level4_kernel_pgt) 608032370b9SKirill A. Shutemov .fill 511,8,0 60921729f81STom Lendacky .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC 610b1bd27b9SJiri SlabySYM_DATA_END(level4_kernel_pgt) 611032370b9SKirill A. Shutemov#endif 612032370b9SKirill A. Shutemov 613b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level3_kernel_pgt) 614a6523748SEduardo Habkost .fill L3_START_KERNEL,8,0 615250c2277SThomas Gleixner /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */ 61621729f81STom Lendacky .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC 61721729f81STom Lendacky .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC 618b1bd27b9SJiri SlabySYM_DATA_END(level3_kernel_pgt) 619250c2277SThomas Gleixner 620b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level2_kernel_pgt) 62188f3aec7SIngo Molnar /* 622ea3186b9SArvind Sankar * Kernel high mapping. 62388f3aec7SIngo Molnar * 624ea3186b9SArvind Sankar * The kernel code+data+bss must be located below KERNEL_IMAGE_SIZE in 625ea3186b9SArvind Sankar * virtual address space, which is 1 GiB if RANDOMIZE_BASE is enabled, 626ea3186b9SArvind Sankar * 512 MiB otherwise. 62788f3aec7SIngo Molnar * 628ea3186b9SArvind Sankar * (NOTE: after that starts the module area, see MODULES_VADDR.) 629430d4005SDave Hansen * 630ea3186b9SArvind Sankar * This table is eventually used by the kernel during normal runtime. 631ea3186b9SArvind Sankar * Care must be taken to clear out undesired bits later, like _PAGE_RW 632ea3186b9SArvind Sankar * or _PAGE_GLOBAL in some cases. 63388f3aec7SIngo Molnar */ 634ea3186b9SArvind Sankar PMDS(0, __PAGE_KERNEL_LARGE_EXEC, KERNEL_IMAGE_SIZE/PMD_SIZE) 635b1bd27b9SJiri SlabySYM_DATA_END(level2_kernel_pgt) 636250c2277SThomas Gleixner 637b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level2_fixmap_pgt) 63805ab1d8aSFeng Tang .fill (512 - 4 - FIXMAP_PMD_NUM),8,0 63905ab1d8aSFeng Tang pgtno = 0 64005ab1d8aSFeng Tang .rept (FIXMAP_PMD_NUM) 64105ab1d8aSFeng Tang .quad level1_fixmap_pgt + (pgtno << PAGE_SHIFT) - __START_KERNEL_map \ 64205ab1d8aSFeng Tang + _PAGE_TABLE_NOENC; 64305ab1d8aSFeng Tang pgtno = pgtno + 1 64405ab1d8aSFeng Tang .endr 64505ab1d8aSFeng Tang /* 6 MB reserved space + a 2MB hole */ 64605ab1d8aSFeng Tang .fill 4,8,0 647b1bd27b9SJiri SlabySYM_DATA_END(level2_fixmap_pgt) 6488170e6beSH. Peter Anvin 649b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level1_fixmap_pgt) 65005ab1d8aSFeng Tang .rept (FIXMAP_PMD_NUM) 651250c2277SThomas Gleixner .fill 512,8,0 65205ab1d8aSFeng Tang .endr 653b1bd27b9SJiri SlabySYM_DATA_END(level1_fixmap_pgt) 654250c2277SThomas Gleixner 655250c2277SThomas Gleixner#undef PMDS 656250c2277SThomas Gleixner 657250c2277SThomas Gleixner .data 658250c2277SThomas Gleixner .align 16 659250c2277SThomas Gleixner 660b1bd27b9SJiri SlabySYM_DATA(early_gdt_descr, .word GDT_ENTRIES*8-1) 661b1bd27b9SJiri SlabySYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) 662b1bd27b9SJiri Slaby 663b1bd27b9SJiri Slaby .align 16 664250c2277SThomas Gleixner/* This must match the first entry in level2_kernel_pgt */ 665b1bd27b9SJiri SlabySYM_DATA(phys_base, .quad 0x0) 666784d5699SAl ViroEXPORT_SYMBOL(phys_base) 667250c2277SThomas Gleixner 6688c5e5ac3SJeremy Fitzhardinge#include "../../x86/xen/xen-head.S" 669250c2277SThomas Gleixner 67002b7da37STim Abbott __PAGE_ALIGNED_BSS 671b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(empty_zero_page) 672250c2277SThomas Gleixner .skip PAGE_SIZE 673b1bd27b9SJiri SlabySYM_DATA_END(empty_zero_page) 674784d5699SAl ViroEXPORT_SYMBOL(empty_zero_page) 675ef7f0d6aSAndrey Ryabinin 676