2 * Copyright (c) 2003 Peter Wemm
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <machine/asmacros.h> /* miscellaneous asm macros */
30 #include <machine/specialreg.h>
34 .data /* So we can modify it */
41 * The AP enters here in response to the startup IPI.
42 * We are in real mode. %cs is the only segment register set.
44 cli /* make sure no interrupts */
45 mov %cs, %ax /* copy %cs to %ds. Remember these */
46 mov %ax, %ds /* are offsets rather than selectors */
50 * Find relocation base and patch the gdt descriptor and ljmp targets
54 sall $4, %ebx /* %ebx is now our relocation base */
55 orl %ebx, lgdt_desc-mptramp_start+2
56 orl %ebx, jmp_32-mptramp_start+2
57 orl %ebx, jmp_64-mptramp_start+1
60 * Load the descriptor table pointer. We'll need it when running
61 * in 16 bit protected mode.
63 lgdt lgdt_desc-mptramp_start
65 /* Enable protected mode */
70 * Now execute a far jump to turn on protected mode. This
71 * causes the segment registers to turn into selectors and causes
72 * %cs to be loaded from the gdt.
74 * The following instruction is:
75 * ljmpl $bootcode-gdt, $protmode-mptramp_start
76 * but gas cannot assemble that. And besides, we patch the targets
77 * in early startup and its a little clearer what we are patching.
80 .byte 0x66 /* size override to 32 bits */
81 .byte 0xea /* opcode for far jump */
82 .long protmode-mptramp_start /* offset in segment */
83 .word bootcode-gdt /* index in gdt for 32 bit code */
86 * At this point, we are running in 32 bit legacy protected mode.
90 mov $bootdata-gdt, %eax
94 * Turn on the PAE bit and optionally the LA57 bit for when paging
98 orl $(CR4_PAE | CR4_PGE), %eax
99 cmpb $0, mptramp_la57-mptramp_start(%ebx)
105 * If the BSP reported NXE support, enable EFER.NXE for all APs
106 * prior to loading %cr3. This avoids page faults if the AP
107 * encounters memory marked with the NX bit prior to detecting and
108 * enabling NXE support.
110 cmpb $0,mptramp_nx-mptramp_start(%ebx)
118 * Enable EFER.LME so that we get long mode when all the prereqs are
119 * in place. In this case, it turns on when CR0_PG is finally enabled.
120 * Pick up a few other EFER bits that we'll use need we're here.
124 orl $EFER_LME | EFER_SCE, %eax
128 * Load kernel page table pointer into %cr3.
129 * %ebx is still our relocation base.
131 * Note that this only gets accessed after we're actually in 64 bit
132 * mode, however we can only set the bottom 32 bits of %cr3 in this
133 * state. This means we depend on the kernel page table being
134 * allocated from the low 4G.
136 leal mptramp_pagetables-mptramp_start(%ebx),%eax
141 * Finally, switch to long bit mode by enabling paging. We have
142 * to be very careful here because all the segmentation disappears
143 * out from underneath us. The spec says we can depend on the
144 * subsequent pipelined branch to execute, but *only if* everything
145 * is still identity mapped. If any mappings change, the pipeline
153 * At this point paging is enabled, and we are in "compatibility" mode.
154 * We do another far jump to reload %cs with the 64 bit selector.
155 * %cr3 points to a 4- or 5-level page table.
156 * We cannot yet jump all the way to the kernel because we can only
157 * specify a 32 bit linear address. So, we use yet another trampoline.
159 * The following instruction is:
160 * ljmp $kernelcode-gdt, $tramp_64-mptramp_start
161 * but gas cannot assemble that. And besides, we patch the targets
162 * in early startup and its a little clearer what we are patching.
165 .byte 0xea /* opcode for far jump */
166 .long tramp_64-mptramp_start /* offset in segment */
167 .word kernelcode-gdt /* index in gdt for 64 bit code */
170 * Yeehar! We're running in 64 bit mode! We can mostly ignore our
171 * segment registers, and get on with it.
172 * We are running at the correct virtual address space.
173 * Note that the jmp is relative and that we've been relocated,
174 * so use an indirect jump.
178 movabsq $entry_64,%rax /* 64 bit immediate load */
184 * All segment descriptor tables start with a null descriptor
190 * This is the 64 bit long mode code descriptor. There is no
191 * 64 bit data descriptor.
198 * This is the descriptor for the 32 bit boot code.
199 * %cs: +A, +R, -C, DPL=0, +P, +D, +G
200 * Accessed, Readable, Present, 32 bit, 4G granularity
207 * This is the descriptor for the 32 bit boot data.
208 * We load it into %ds and %ss. The bits for each selector
209 * are interpreted slightly differently.
210 * %ds: +A, +W, -E, DPL=0, +P, +D, +G
211 * %ss: +A, +W, -E, DPL=0, +P, +B, +G
212 * Accessed, Writeable, Expand up, Present, 32 bit, 4GB
213 * For %ds, +D means 'default operand size is 32 bit'.
214 * For %ss, +B means the stack register is %esp rather than %sp.
223 * The address of our page table pages that the boot code
224 * uses to trampoline up to kernel address space.
226 .globl mptramp_pagetables
230 /* 5-level paging ? */
240 * The pseudo descriptor for lgdt to use.
243 .word gdtend-gdt /* Length */
244 .long gdt-mptramp_start /* Offset plus %ds << 4 */
248 * The size of the trampoline code that needs to be relocated
249 * below the 1MiB boundary.
253 .long mptramp_end - mptramp_start
256 * From here on down is executed in the kernel .text section.
265 * Initialize the segment register used for the PCPU area. The PCPU
266 * area will be initialized by init_secondary(), but it should be
267 * accessible before that to support sanitizer instrumentation which
268 * accesses per-CPU variables.
270 * Note that GS.base is loaded again in init_secondary(). This is not
271 * redundant: lgdt() loads a selector into %gs and this has the side
272 * effect of clearing GS.base.
274 movl $MSR_GSBASE, %ecx