xref: /openbmc/linux/arch/x86/kernel/head_64.S (revision e04b8833)
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
29fdc0269eSJuergen Gross#ifdef CONFIG_PARAVIRT_XXL
3049a69787SGlauber de Oliveira Costa#include <asm/asm-offsets.h>
3149a69787SGlauber de Oliveira Costa#include <asm/paravirt.h>
3275da04f7SThomas Gleixner#define GET_CR2_INTO(reg) GET_CR2_INTO_AX ; _ASM_MOV %_ASM_AX, reg
3349a69787SGlauber de Oliveira Costa#else
349900aa2fSH. Peter Anvin#define INTERRUPT_RETURN iretq
3575da04f7SThomas Gleixner#define GET_CR2_INTO(reg) _ASM_MOV %cr2, reg
3649a69787SGlauber de Oliveira Costa#endif
3749a69787SGlauber de Oliveira Costa
3875da04f7SThomas Gleixner/*
3975da04f7SThomas Gleixner * We are not able to switch in one step to the final KERNEL ADDRESS SPACE
40250c2277SThomas Gleixner * because we need identity-mapped pages.
41250c2277SThomas Gleixner */
42b9952ec7SKirill A. Shutemov#define l4_index(x)	(((x) >> 39) & 511)
43a6523748SEduardo Habkost#define pud_index(x)	(((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
44a6523748SEduardo Habkost
45b9952ec7SKirill A. ShutemovL4_PAGE_OFFSET = l4_index(__PAGE_OFFSET_BASE_L4)
46b9952ec7SKirill A. ShutemovL4_START_KERNEL = l4_index(__START_KERNEL_map)
47b9952ec7SKirill A. Shutemov
48a6523748SEduardo HabkostL3_START_KERNEL = pud_index(__START_KERNEL_map)
49a6523748SEduardo Habkost
50250c2277SThomas Gleixner	.text
514ae59b91STim Abbott	__HEAD
52250c2277SThomas Gleixner	.code64
5337818afdSJiri SlabySYM_CODE_START_NOALIGN(startup_64)
542704fbb6SJosh Poimboeuf	UNWIND_HINT_EMPTY
55250c2277SThomas Gleixner	/*
561256276cSKonrad Rzeszutek Wilk	 * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
57250c2277SThomas Gleixner	 * and someone has loaded an identity mapped page table
58250c2277SThomas Gleixner	 * for us.  These identity mapped page tables map all of the
59250c2277SThomas Gleixner	 * kernel pages and possibly all of memory.
60250c2277SThomas Gleixner	 *
618170e6beSH. Peter Anvin	 * %rsi holds a physical pointer to real_mode_data.
62250c2277SThomas Gleixner	 *
63250c2277SThomas Gleixner	 * We come here either directly from a 64bit bootloader, or from
645b171e82SAlexander Kuleshov	 * arch/x86/boot/compressed/head_64.S.
65250c2277SThomas Gleixner	 *
66250c2277SThomas Gleixner	 * We only come here initially at boot nothing else comes here.
67250c2277SThomas Gleixner	 *
68250c2277SThomas Gleixner	 * Since we may be loaded at an address different from what we were
69250c2277SThomas Gleixner	 * compiled to run at we first fixup the physical addresses in our page
70250c2277SThomas Gleixner	 * tables and then reload them.
71250c2277SThomas Gleixner	 */
72250c2277SThomas Gleixner
7322dc3918SJosh Poimboeuf	/* Set up the stack for verify_cpu(), similar to initial_stack below */
7422dc3918SJosh Poimboeuf	leaq	(__end_init_task - SIZEOF_PTREGS)(%rip), %rsp
7591ed140dSBorislav Petkov
76866b556eSJoerg Roedel	leaq	_text(%rip), %rdi
77866b556eSJoerg Roedel	pushq	%rsi
78866b556eSJoerg Roedel	call	startup_64_setup_env
79866b556eSJoerg Roedel	popq	%rsi
80866b556eSJoerg Roedel
81866b556eSJoerg Roedel	/* Now switch to __KERNEL_CS so IRET works reliably */
82866b556eSJoerg Roedel	pushq	$__KERNEL_CS
83866b556eSJoerg Roedel	leaq	.Lon_kernel_cs(%rip), %rax
84866b556eSJoerg Roedel	pushq	%rax
85866b556eSJoerg Roedel	lretq
86866b556eSJoerg Roedel
87866b556eSJoerg Roedel.Lon_kernel_cs:
88866b556eSJoerg Roedel	UNWIND_HINT_EMPTY
89866b556eSJoerg Roedel
9004633df0SBorislav Petkov	/* Sanitize CPU configuration */
9104633df0SBorislav Petkov	call verify_cpu
9204633df0SBorislav Petkov
935868f365STom Lendacky	/*
945868f365STom Lendacky	 * Perform pagetable fixups. Additionally, if SME is active, encrypt
955868f365STom Lendacky	 * the kernel and retrieve the modifier (SME encryption mask if SME
965868f365STom Lendacky	 * is active) to be added to the initial pgdir entry that will be
975868f365STom Lendacky	 * programmed into CR3.
985868f365STom Lendacky	 */
99250c2277SThomas Gleixner	leaq	_text(%rip), %rdi
100c88d7150SKirill A. Shutemov	pushq	%rsi
101c88d7150SKirill A. Shutemov	call	__startup_64
102c88d7150SKirill A. Shutemov	popq	%rsi
103250c2277SThomas Gleixner
1045868f365STom Lendacky	/* Form the CR3 value being sure to include the CR3 modifier */
1055868f365STom Lendacky	addq	$(early_top_pgt - __START_KERNEL_map), %rax
1068170e6beSH. Peter Anvin	jmp 1f
10737818afdSJiri SlabySYM_CODE_END(startup_64)
10837818afdSJiri Slaby
109bc7b11c0SJiri SlabySYM_CODE_START(secondary_startup_64)
1102704fbb6SJosh Poimboeuf	UNWIND_HINT_EMPTY
111250c2277SThomas Gleixner	/*
1121256276cSKonrad Rzeszutek Wilk	 * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
113250c2277SThomas Gleixner	 * and someone has loaded a mapped page table.
114250c2277SThomas Gleixner	 *
1158170e6beSH. Peter Anvin	 * %rsi holds a physical pointer to real_mode_data.
116250c2277SThomas Gleixner	 *
117250c2277SThomas Gleixner	 * We come here either from startup_64 (using physical addresses)
118250c2277SThomas Gleixner	 * or from trampoline.S (using virtual addresses).
119250c2277SThomas Gleixner	 *
120250c2277SThomas Gleixner	 * Using virtual addresses from trampoline.S removes the need
121250c2277SThomas Gleixner	 * to have any identity mapped pages in the kernel page table
122250c2277SThomas Gleixner	 * after the boot processor executes this code.
123250c2277SThomas Gleixner	 */
124250c2277SThomas Gleixner
12504633df0SBorislav Petkov	/* Sanitize CPU configuration */
12604633df0SBorislav Petkov	call verify_cpu
12704633df0SBorislav Petkov
1285868f365STom Lendacky	/*
1295868f365STom Lendacky	 * Retrieve the modifier (SME encryption mask if SME is active) to be
1305868f365STom Lendacky	 * added to the initial pgdir entry that will be programmed into CR3.
1315868f365STom Lendacky	 */
1325868f365STom Lendacky	pushq	%rsi
1335868f365STom Lendacky	call	__startup_secondary_64
1345868f365STom Lendacky	popq	%rsi
1355868f365STom Lendacky
1365868f365STom Lendacky	/* Form the CR3 value being sure to include the CR3 modifier */
1375868f365STom Lendacky	addq	$(init_top_pgt - __START_KERNEL_map), %rax
1388170e6beSH. Peter Anvin1:
1398170e6beSH. Peter Anvin
140032370b9SKirill A. Shutemov	/* Enable PAE mode, PGE and LA57 */
1418170e6beSH. Peter Anvin	movl	$(X86_CR4_PAE | X86_CR4_PGE), %ecx
142032370b9SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL
14339b95522SKirill A. Shutemov	testl	$1, __pgtable_l5_enabled(%rip)
1446f9dd329SKirill A. Shutemov	jz	1f
145032370b9SKirill A. Shutemov	orl	$X86_CR4_LA57, %ecx
1466f9dd329SKirill A. Shutemov1:
147032370b9SKirill A. Shutemov#endif
1488170e6beSH. Peter Anvin	movq	%rcx, %cr4
149250c2277SThomas Gleixner
150032370b9SKirill A. Shutemov	/* Setup early boot stage 4-/5-level pagetables. */
151250c2277SThomas Gleixner	addq	phys_base(%rip), %rax
152250c2277SThomas Gleixner	movq	%rax, %cr3
153250c2277SThomas Gleixner
154250c2277SThomas Gleixner	/* Ensure I am executing from virtual addresses */
155250c2277SThomas Gleixner	movq	$1f, %rax
156bd89004fSPeter Zijlstra	ANNOTATE_RETPOLINE_SAFE
157250c2277SThomas Gleixner	jmp	*%rax
158250c2277SThomas Gleixner1:
1592704fbb6SJosh Poimboeuf	UNWIND_HINT_EMPTY
160250c2277SThomas Gleixner
161e04b8833SJoerg Roedel	/*
162e04b8833SJoerg Roedel	 * We must switch to a new descriptor in kernel space for the GDT
163e04b8833SJoerg Roedel	 * because soon the kernel won't have access anymore to the userspace
164e04b8833SJoerg Roedel	 * addresses where we're currently running on. We have to do that here
165e04b8833SJoerg Roedel	 * because in 32bit we couldn't load a 64bit linear address.
166e04b8833SJoerg Roedel	 */
167e04b8833SJoerg Roedel	lgdt	early_gdt_descr(%rip)
168e04b8833SJoerg Roedel
169250c2277SThomas Gleixner	/* Check if nx is implemented */
170250c2277SThomas Gleixner	movl	$0x80000001, %eax
171250c2277SThomas Gleixner	cpuid
172250c2277SThomas Gleixner	movl	%edx,%edi
173250c2277SThomas Gleixner
174250c2277SThomas Gleixner	/* Setup EFER (Extended Feature Enable Register) */
175250c2277SThomas Gleixner	movl	$MSR_EFER, %ecx
176250c2277SThomas Gleixner	rdmsr
177250c2277SThomas Gleixner	btsl	$_EFER_SCE, %eax	/* Enable System Call */
178250c2277SThomas Gleixner	btl	$20,%edi		/* No Execute supported? */
179250c2277SThomas Gleixner	jnc     1f
180250c2277SThomas Gleixner	btsl	$_EFER_NX, %eax
18178d77df7SH. Peter Anvin	btsq	$_PAGE_BIT_NX,early_pmd_flags(%rip)
182250c2277SThomas Gleixner1:	wrmsr				/* Make changes effective */
183250c2277SThomas Gleixner
184250c2277SThomas Gleixner	/* Setup cr0 */
185369101daSCyrill Gorcunov	movl	$CR0_STATE, %eax
186250c2277SThomas Gleixner	/* Make changes effective */
187250c2277SThomas Gleixner	movq	%rax, %cr0
188250c2277SThomas Gleixner
189250c2277SThomas Gleixner	/* Setup a boot time stack */
190b32f96c7SJosh Poimboeuf	movq initial_stack(%rip), %rsp
191250c2277SThomas Gleixner
192250c2277SThomas Gleixner	/* zero EFLAGS after setting rsp */
193250c2277SThomas Gleixner	pushq $0
194250c2277SThomas Gleixner	popfq
195250c2277SThomas Gleixner
1968ec6993dSBrian Gerst	/* set up data segments */
1978ec6993dSBrian Gerst	xorl %eax,%eax
198250c2277SThomas Gleixner	movl %eax,%ds
199250c2277SThomas Gleixner	movl %eax,%ss
200250c2277SThomas Gleixner	movl %eax,%es
201250c2277SThomas Gleixner
202250c2277SThomas Gleixner	/*
203250c2277SThomas Gleixner	 * We don't really need to load %fs or %gs, but load them anyway
204250c2277SThomas Gleixner	 * to kill any stale realmode selectors.  This allows execution
205250c2277SThomas Gleixner	 * under VT hardware.
206250c2277SThomas Gleixner	 */
207250c2277SThomas Gleixner	movl %eax,%fs
208250c2277SThomas Gleixner	movl %eax,%gs
209250c2277SThomas Gleixner
210f32ff538STejun Heo	/* Set up %gs.
211f32ff538STejun Heo	 *
21238506573SCao jin	 * The base of %gs always points to fixed_percpu_data. If the
21338506573SCao jin	 * stack protector canary is enabled, it is located at %gs:40.
21438506573SCao jin	 * Note that, on SMP, the boot cpu uses init data section until
21538506573SCao jin	 * the per cpu areas are set up.
216250c2277SThomas Gleixner	 */
217250c2277SThomas Gleixner	movl	$MSR_GS_BASE,%ecx
218650fb439SBrian Gerst	movl	initial_gs(%rip),%eax
219650fb439SBrian Gerst	movl	initial_gs+4(%rip),%edx
220250c2277SThomas Gleixner	wrmsr
221250c2277SThomas Gleixner
2228170e6beSH. Peter Anvin	/* rsi is pointer to real mode structure with interesting info.
223250c2277SThomas Gleixner	   pass it to C */
2248170e6beSH. Peter Anvin	movq	%rsi, %rdi
225250c2277SThomas Gleixner
22679d243a0SBorislav Petkov.Ljump_to_C_code:
227a9468df5SJosh Poimboeuf	/*
228a9468df5SJosh Poimboeuf	 * Jump to run C code and to be on a real kernel address.
229250c2277SThomas Gleixner	 * Since we are running on identity-mapped space we have to jump
230250c2277SThomas Gleixner	 * to the full 64bit address, this is only possible as indirect
231250c2277SThomas Gleixner	 * jump.  In addition we need to ensure %cs is set so we make this
232250c2277SThomas Gleixner	 * a far return.
2338170e6beSH. Peter Anvin	 *
2348170e6beSH. Peter Anvin	 * Note: do not change to far jump indirect with 64bit offset.
2358170e6beSH. Peter Anvin	 *
2368170e6beSH. Peter Anvin	 * AMD does not support far jump indirect with 64bit offset.
2378170e6beSH. Peter Anvin	 * AMD64 Architecture Programmer's Manual, Volume 3: states only
2388170e6beSH. Peter Anvin	 *	JMP FAR mem16:16 FF /5 Far jump indirect,
2398170e6beSH. Peter Anvin	 *		with the target specified by a far pointer in memory.
2408170e6beSH. Peter Anvin	 *	JMP FAR mem16:32 FF /5 Far jump indirect,
2418170e6beSH. Peter Anvin	 *		with the target specified by a far pointer in memory.
2428170e6beSH. Peter Anvin	 *
2438170e6beSH. Peter Anvin	 * Intel64 does support 64bit offset.
2448170e6beSH. Peter Anvin	 * Software Developer Manual Vol 2: states:
2458170e6beSH. Peter Anvin	 *	FF /5 JMP m16:16 Jump far, absolute indirect,
2468170e6beSH. Peter Anvin	 *		address given in m16:16
2478170e6beSH. Peter Anvin	 *	FF /5 JMP m16:32 Jump far, absolute indirect,
2488170e6beSH. Peter Anvin	 *		address given in m16:32.
2498170e6beSH. Peter Anvin	 *	REX.W + FF /5 JMP m16:64 Jump far, absolute indirect,
2508170e6beSH. Peter Anvin	 *		address given in m16:64.
251250c2277SThomas Gleixner	 */
25231dcfec1SJosh Poimboeuf	pushq	$.Lafter_lret	# put return address on stack for unwinder
253a7bea830SJan Beulich	xorl	%ebp, %ebp	# clear frame pointer
254250c2277SThomas Gleixner	movq	initial_code(%rip), %rax
255250c2277SThomas Gleixner	pushq	$__KERNEL_CS	# set correct cs
256250c2277SThomas Gleixner	pushq	%rax		# target address in negative space
257250c2277SThomas Gleixner	lretq
25831dcfec1SJosh Poimboeuf.Lafter_lret:
259bc7b11c0SJiri SlabySYM_CODE_END(secondary_startup_64)
260250c2277SThomas Gleixner
26104633df0SBorislav Petkov#include "verify_cpu.S"
26204633df0SBorislav Petkov
26342e78e97SFenghua Yu#ifdef CONFIG_HOTPLUG_CPU
26442e78e97SFenghua Yu/*
26542e78e97SFenghua Yu * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
26642e78e97SFenghua Yu * up already except stack. We just set up stack here. Then call
26779d243a0SBorislav Petkov * start_secondary() via .Ljump_to_C_code.
26842e78e97SFenghua Yu */
269bc7b11c0SJiri SlabySYM_CODE_START(start_cpu0)
2702704fbb6SJosh Poimboeuf	UNWIND_HINT_EMPTY
27161a73f5cSJosh Poimboeuf	movq	initial_stack(%rip), %rsp
27279d243a0SBorislav Petkov	jmp	.Ljump_to_C_code
273bc7b11c0SJiri SlabySYM_CODE_END(start_cpu0)
27442e78e97SFenghua Yu#endif
27542e78e97SFenghua Yu
276b32f96c7SJosh Poimboeuf	/* Both SMP bootup and ACPI suspend change these variables */
277da5968aeSSam Ravnborg	__REFDATA
2788170e6beSH. Peter Anvin	.balign	8
279b1bd27b9SJiri SlabySYM_DATA(initial_code,	.quad x86_64_start_kernel)
280b1bd27b9SJiri SlabySYM_DATA(initial_gs,	.quad INIT_PER_CPU_VAR(fixed_percpu_data))
281b1bd27b9SJiri Slaby
28222dc3918SJosh Poimboeuf/*
283b1bd27b9SJiri Slaby * The SIZEOF_PTREGS gap is a convention which helps the in-kernel unwinder
284b1bd27b9SJiri Slaby * reliably detect the end of the stack.
28522dc3918SJosh Poimboeuf */
286b1bd27b9SJiri SlabySYM_DATA(initial_stack, .quad init_thread_union + THREAD_SIZE - SIZEOF_PTREGS)
287b9af7c0dSSuresh Siddha	__FINITDATA
288250c2277SThomas Gleixner
2898170e6beSH. Peter Anvin	__INIT
290bc7b11c0SJiri SlabySYM_CODE_START(early_idt_handler_array)
291749c970aSAndi Kleen	i = 0
292749c970aSAndi Kleen	.rept NUM_EXCEPTION_VECTORS
29382c62fa0SJosh Poimboeuf	.if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
2942704fbb6SJosh Poimboeuf		UNWIND_HINT_IRET_REGS
2959900aa2fSH. Peter Anvin		pushq $0	# Dummy error code, to make stack frame uniform
2962704fbb6SJosh Poimboeuf	.else
2972704fbb6SJosh Poimboeuf		UNWIND_HINT_IRET_REGS offset=8
2989900aa2fSH. Peter Anvin	.endif
2999900aa2fSH. Peter Anvin	pushq $i		# 72(%rsp) Vector number
300cdeb6048SAndy Lutomirski	jmp early_idt_handler_common
3012704fbb6SJosh Poimboeuf	UNWIND_HINT_IRET_REGS
302749c970aSAndi Kleen	i = i + 1
303cdeb6048SAndy Lutomirski	.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
304749c970aSAndi Kleen	.endr
3052704fbb6SJosh Poimboeuf	UNWIND_HINT_IRET_REGS offset=16
306bc7b11c0SJiri SlabySYM_CODE_END(early_idt_handler_array)
3078866cd9dSRoland McGrath
308ef77e688SJiri SlabySYM_CODE_START_LOCAL(early_idt_handler_common)
309cdeb6048SAndy Lutomirski	/*
310cdeb6048SAndy Lutomirski	 * The stack is the hardware frame, an error code or zero, and the
311cdeb6048SAndy Lutomirski	 * vector number.
312cdeb6048SAndy Lutomirski	 */
3139900aa2fSH. Peter Anvin	cld
3149900aa2fSH. Peter Anvin
315250c2277SThomas Gleixner	incl early_recursion_flag(%rip)
3169900aa2fSH. Peter Anvin
3177bbcdb1cSAndy Lutomirski	/* The vector number is currently in the pt_regs->di slot. */
3187bbcdb1cSAndy Lutomirski	pushq %rsi				/* pt_regs->si */
3197bbcdb1cSAndy Lutomirski	movq 8(%rsp), %rsi			/* RSI = vector number */
3207bbcdb1cSAndy Lutomirski	movq %rdi, 8(%rsp)			/* pt_regs->di = RDI */
3217bbcdb1cSAndy Lutomirski	pushq %rdx				/* pt_regs->dx */
3227bbcdb1cSAndy Lutomirski	pushq %rcx				/* pt_regs->cx */
3237bbcdb1cSAndy Lutomirski	pushq %rax				/* pt_regs->ax */
3247bbcdb1cSAndy Lutomirski	pushq %r8				/* pt_regs->r8 */
3257bbcdb1cSAndy Lutomirski	pushq %r9				/* pt_regs->r9 */
3267bbcdb1cSAndy Lutomirski	pushq %r10				/* pt_regs->r10 */
3277bbcdb1cSAndy Lutomirski	pushq %r11				/* pt_regs->r11 */
3287bbcdb1cSAndy Lutomirski	pushq %rbx				/* pt_regs->bx */
3297bbcdb1cSAndy Lutomirski	pushq %rbp				/* pt_regs->bp */
3307bbcdb1cSAndy Lutomirski	pushq %r12				/* pt_regs->r12 */
3317bbcdb1cSAndy Lutomirski	pushq %r13				/* pt_regs->r13 */
3327bbcdb1cSAndy Lutomirski	pushq %r14				/* pt_regs->r14 */
3337bbcdb1cSAndy Lutomirski	pushq %r15				/* pt_regs->r15 */
3342704fbb6SJosh Poimboeuf	UNWIND_HINT_REGS
3359900aa2fSH. Peter Anvin
3367bbcdb1cSAndy Lutomirski	cmpq $14,%rsi		/* Page fault? */
3378170e6beSH. Peter Anvin	jnz 10f
33855aedddbSPeter Zijlstra	GET_CR2_INTO(%rdi)	/* can clobber %rax if pv */
3398170e6beSH. Peter Anvin	call early_make_pgtable
3408170e6beSH. Peter Anvin	andl %eax,%eax
3417bbcdb1cSAndy Lutomirski	jz 20f			/* All good */
3428170e6beSH. Peter Anvin
3438170e6beSH. Peter Anvin10:
3447bbcdb1cSAndy Lutomirski	movq %rsp,%rdi		/* RDI = pt_regs; RSI is already trapnr */
3459900aa2fSH. Peter Anvin	call early_fixup_exception
3469900aa2fSH. Peter Anvin
3470e861fbbSAndy Lutomirski20:
3489900aa2fSH. Peter Anvin	decl early_recursion_flag(%rip)
34926c4ef9cSAndy Lutomirski	jmp restore_regs_and_return_to_kernel
350ef77e688SJiri SlabySYM_CODE_END(early_idt_handler_common)
3519900aa2fSH. Peter Anvin
352b1bd27b9SJiri Slaby
353b1bd27b9SJiri Slaby#define SYM_DATA_START_PAGE_ALIGNED(name)			\
354b1bd27b9SJiri Slaby	SYM_START(name, SYM_L_GLOBAL, .balign PAGE_SIZE)
355250c2277SThomas Gleixner
356d9e9a641SDave Hansen#ifdef CONFIG_PAGE_TABLE_ISOLATION
357d9e9a641SDave Hansen/*
358d9e9a641SDave Hansen * Each PGD needs to be 8k long and 8k aligned.  We do not
359d9e9a641SDave Hansen * ever go out to userspace with these, so we do not
360d9e9a641SDave Hansen * strictly *need* the second page, but this allows us to
361d9e9a641SDave Hansen * have a single set_pgd() implementation that does not
362d9e9a641SDave Hansen * need to worry about whether it has 4k or 8k to work
363d9e9a641SDave Hansen * with.
364d9e9a641SDave Hansen *
365d9e9a641SDave Hansen * This ensures PGDs are 8k long:
366d9e9a641SDave Hansen */
367d9e9a641SDave Hansen#define PTI_USER_PGD_FILL	512
368d9e9a641SDave Hansen/* This ensures they are 8k-aligned: */
369b1bd27b9SJiri Slaby#define SYM_DATA_START_PTI_ALIGNED(name) \
370b1bd27b9SJiri Slaby	SYM_START(name, SYM_L_GLOBAL, .balign 2 * PAGE_SIZE)
371d9e9a641SDave Hansen#else
372b1bd27b9SJiri Slaby#define SYM_DATA_START_PTI_ALIGNED(name) \
373b1bd27b9SJiri Slaby	SYM_DATA_START_PAGE_ALIGNED(name)
374d9e9a641SDave Hansen#define PTI_USER_PGD_FILL	0
375d9e9a641SDave Hansen#endif
376d9e9a641SDave Hansen
377250c2277SThomas Gleixner/* Automate the creation of 1 to 1 mapping pmd entries */
378250c2277SThomas Gleixner#define PMDS(START, PERM, COUNT)			\
379250c2277SThomas Gleixner	i = 0 ;						\
380250c2277SThomas Gleixner	.rept (COUNT) ;					\
3810e192b99SCyrill Gorcunov	.quad	(START) + (i << PMD_SHIFT) + (PERM) ;	\
382250c2277SThomas Gleixner	i = i + 1 ;					\
383250c2277SThomas Gleixner	.endr
384250c2277SThomas Gleixner
3858170e6beSH. Peter Anvin	__INITDATA
3861a8770b7SJiri Slaby	.balign 4
3871a8770b7SJiri Slaby
388b1bd27b9SJiri SlabySYM_DATA_START_PTI_ALIGNED(early_top_pgt)
3896f9dd329SKirill A. Shutemov	.fill	512,8,0
390d9e9a641SDave Hansen	.fill	PTI_USER_PGD_FILL,8,0
391b1bd27b9SJiri SlabySYM_DATA_END(early_top_pgt)
3928170e6beSH. Peter Anvin
393b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(early_dynamic_pgts)
3948170e6beSH. Peter Anvin	.fill	512*EARLY_DYNAMIC_PAGE_TABLES,8,0
395b1bd27b9SJiri SlabySYM_DATA_END(early_dynamic_pgts)
3968170e6beSH. Peter Anvin
397b1bd27b9SJiri SlabySYM_DATA(early_recursion_flag, .long 0)
3981a8770b7SJiri Slaby
399b9af7c0dSSuresh Siddha	.data
4008170e6beSH. Peter Anvin
4017733607fSMaran Wilson#if defined(CONFIG_XEN_PV) || defined(CONFIG_PVH)
402b1bd27b9SJiri SlabySYM_DATA_START_PTI_ALIGNED(init_top_pgt)
40321729f81STom Lendacky	.quad   level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC
404b9952ec7SKirill A. Shutemov	.org    init_top_pgt + L4_PAGE_OFFSET*8, 0
40521729f81STom Lendacky	.quad   level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC
406b9952ec7SKirill A. Shutemov	.org    init_top_pgt + L4_START_KERNEL*8, 0
407250c2277SThomas Gleixner	/* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
40821729f81STom Lendacky	.quad   level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC
409d9e9a641SDave Hansen	.fill	PTI_USER_PGD_FILL,8,0
410b1bd27b9SJiri SlabySYM_DATA_END(init_top_pgt)
411250c2277SThomas Gleixner
412b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level3_ident_pgt)
41321729f81STom Lendacky	.quad	level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC
414250c2277SThomas Gleixner	.fill	511, 8, 0
415b1bd27b9SJiri SlabySYM_DATA_END(level3_ident_pgt)
416b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level2_ident_pgt)
417430d4005SDave Hansen	/*
418430d4005SDave Hansen	 * Since I easily can, map the first 1G.
4198170e6beSH. Peter Anvin	 * Don't set NX because code runs from these pages.
420430d4005SDave Hansen	 *
421430d4005SDave Hansen	 * Note: This sets _PAGE_GLOBAL despite whether
422430d4005SDave Hansen	 * the CPU supports it or it is enabled.  But,
423430d4005SDave Hansen	 * the CPU should ignore the bit.
4248170e6beSH. Peter Anvin	 */
4258170e6beSH. Peter Anvin	PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD)
426b1bd27b9SJiri SlabySYM_DATA_END(level2_ident_pgt)
4274375c299SKirill A. Shutemov#else
428b1bd27b9SJiri SlabySYM_DATA_START_PTI_ALIGNED(init_top_pgt)
4294375c299SKirill A. Shutemov	.fill	512,8,0
430d9e9a641SDave Hansen	.fill	PTI_USER_PGD_FILL,8,0
431b1bd27b9SJiri SlabySYM_DATA_END(init_top_pgt)
4328170e6beSH. Peter Anvin#endif
433250c2277SThomas Gleixner
434032370b9SKirill A. Shutemov#ifdef CONFIG_X86_5LEVEL
435b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level4_kernel_pgt)
436032370b9SKirill A. Shutemov	.fill	511,8,0
43721729f81STom Lendacky	.quad	level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC
438b1bd27b9SJiri SlabySYM_DATA_END(level4_kernel_pgt)
439032370b9SKirill A. Shutemov#endif
440032370b9SKirill A. Shutemov
441b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level3_kernel_pgt)
442a6523748SEduardo Habkost	.fill	L3_START_KERNEL,8,0
443250c2277SThomas Gleixner	/* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
44421729f81STom Lendacky	.quad	level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE_NOENC
44521729f81STom Lendacky	.quad	level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE_NOENC
446b1bd27b9SJiri SlabySYM_DATA_END(level3_kernel_pgt)
447250c2277SThomas Gleixner
448b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level2_kernel_pgt)
44988f3aec7SIngo Molnar	/*
45085eb69a1SIngo Molnar	 * 512 MB kernel mapping. We spend a full page on this pagetable
45188f3aec7SIngo Molnar	 * anyway.
45288f3aec7SIngo Molnar	 *
45388f3aec7SIngo Molnar	 * The kernel code+data+bss must not be bigger than that.
45488f3aec7SIngo Molnar	 *
45585eb69a1SIngo Molnar	 * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
45688f3aec7SIngo Molnar	 *  If you want to increase this then increase MODULES_VADDR
45788f3aec7SIngo Molnar	 *  too.)
458430d4005SDave Hansen	 *
459430d4005SDave Hansen	 *  This table is eventually used by the kernel during normal
460430d4005SDave Hansen	 *  runtime.  Care must be taken to clear out undesired bits
461430d4005SDave Hansen	 *  later, like _PAGE_RW or _PAGE_GLOBAL in some cases.
46288f3aec7SIngo Molnar	 */
4638490638cSJeremy Fitzhardinge	PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
464d4afe414SIngo Molnar		KERNEL_IMAGE_SIZE/PMD_SIZE)
465b1bd27b9SJiri SlabySYM_DATA_END(level2_kernel_pgt)
466250c2277SThomas Gleixner
467b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level2_fixmap_pgt)
46805ab1d8aSFeng Tang	.fill	(512 - 4 - FIXMAP_PMD_NUM),8,0
46905ab1d8aSFeng Tang	pgtno = 0
47005ab1d8aSFeng Tang	.rept (FIXMAP_PMD_NUM)
47105ab1d8aSFeng Tang	.quad level1_fixmap_pgt + (pgtno << PAGE_SHIFT) - __START_KERNEL_map \
47205ab1d8aSFeng Tang		+ _PAGE_TABLE_NOENC;
47305ab1d8aSFeng Tang	pgtno = pgtno + 1
47405ab1d8aSFeng Tang	.endr
47505ab1d8aSFeng Tang	/* 6 MB reserved space + a 2MB hole */
47605ab1d8aSFeng Tang	.fill	4,8,0
477b1bd27b9SJiri SlabySYM_DATA_END(level2_fixmap_pgt)
4788170e6beSH. Peter Anvin
479b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(level1_fixmap_pgt)
48005ab1d8aSFeng Tang	.rept (FIXMAP_PMD_NUM)
481250c2277SThomas Gleixner	.fill	512,8,0
48205ab1d8aSFeng Tang	.endr
483b1bd27b9SJiri SlabySYM_DATA_END(level1_fixmap_pgt)
484250c2277SThomas Gleixner
485250c2277SThomas Gleixner#undef PMDS
486250c2277SThomas Gleixner
487250c2277SThomas Gleixner	.data
488250c2277SThomas Gleixner	.align 16
489250c2277SThomas Gleixner
490b1bd27b9SJiri SlabySYM_DATA(early_gdt_descr,		.word GDT_ENTRIES*8-1)
491b1bd27b9SJiri SlabySYM_DATA_LOCAL(early_gdt_descr_base,	.quad INIT_PER_CPU_VAR(gdt_page))
492b1bd27b9SJiri Slaby
493b1bd27b9SJiri Slaby	.align 16
494250c2277SThomas Gleixner/* This must match the first entry in level2_kernel_pgt */
495b1bd27b9SJiri SlabySYM_DATA(phys_base, .quad 0x0)
496784d5699SAl ViroEXPORT_SYMBOL(phys_base)
497250c2277SThomas Gleixner
4988c5e5ac3SJeremy Fitzhardinge#include "../../x86/xen/xen-head.S"
499250c2277SThomas Gleixner
50002b7da37STim Abbott	__PAGE_ALIGNED_BSS
501b1bd27b9SJiri SlabySYM_DATA_START_PAGE_ALIGNED(empty_zero_page)
502250c2277SThomas Gleixner	.skip PAGE_SIZE
503b1bd27b9SJiri SlabySYM_DATA_END(empty_zero_page)
504784d5699SAl ViroEXPORT_SYMBOL(empty_zero_page)
505ef7f0d6aSAndrey Ryabinin
506