mpboot.S revision 126246
1/*- 2 * Copyright (c) 2003 Peter Wemm 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/amd64/amd64/mpboot.S 126246 2004-02-25 23:12:39Z peter $ 27 */ 28 29#include <machine/asmacros.h> /* miscellaneous asm macros */ 30#include <machine/specialreg.h> 31 32#include "assym.s" 33 34 .data /* So we can modify it */ 35 36 .p2align 4,0 37 .globl mptramp_start 38mptramp_start: 39 .code16 40 /* 41 * The AP enters here in response to the startup IPI. 42 * We are in real mode. %cs is the only segment register set. 43 */ 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 */ 47 mov %ax, %ss 48 49 /* 50 * Find relocation base and patch the gdt descript and ljmp targets 51 */ 52 xorl %ebx,%ebx 53 mov %cs, %bx 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 58 59 /* 60 * Load the descriptor table pointer. We'll need it when running 61 * in 16 bit protected mode. 62 */ 63 lgdt lgdt_desc-mptramp_start 64 65 /* Enable protected mode */ 66 movl $CR0_PE, %eax 67 mov %eax, %cr0 68 69 /* 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. 73 * 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. 78 */ 79jmp_32: 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 */ 84 85 /* 86 * At this point, we are running in 32 bit legacy protected mode. 87 */ 88 .code32 89protmode: 90 mov $bootdata-gdt, %eax 91 mov %ax, %ds 92 93 /* Turn on the PAE, PSE and PGE bits for when paging is enabled */ 94 mov %cr4, %eax 95 orl $(CR4_PAE | CR4_PSE), %eax 96 mov %eax, %cr4 97 98 /* 99 * Enable EFER.LME so that we get long mode when all the prereqs are 100 * in place. In this case, it turns on when CR0_PG is finally enabled. 101 * Pick up a few other EFER bits that we'll use need we're here. 102 */ 103 movl $MSR_EFER, %ecx 104 rdmsr 105#if 0 /* not till we test the NX cpuid bits */ 106 orl $EFER_LME | EFER_SCE | EFER_NXE, %eax 107#else 108 orl $EFER_LME | EFER_SCE, %eax 109#endif 110 wrmsr 111 112 /* 113 * Point to the embedded page tables for startup. Note that this 114 * only gets accessed after we're actually in 64 bit mode, however 115 * we can only set the bottom 32 bits of %cr3 in this state. This 116 * means we are required to use a temporary page table that is below 117 * the 4GB limit. %ebx is still our relocation base. We could just 118 * subtract 3 * PAGE_SIZE, but that would be too easy. 119 */ 120 leal mptramp_pagetables-mptramp_start(%ebx),%eax 121 movl (%eax), %eax 122 mov %eax, %cr3 123 124 /* 125 * Finally, switch to long bit mode by enabling paging. We have 126 * to be very careful here because all the segmentation disappears 127 * out from underneath us. The spec says we can depend on the 128 * subsequent pipelined branch to execute, but *only if* everthing 129 * is still identity mapped. If any mappings change, the pipeline 130 * will flush. 131 */ 132 mov %cr0, %eax 133 orl $CR0_PG, %eax 134 mov %eax, %cr0 135 136 /* 137 * At this point paging is enabled, and we are in "compatability" mode. 138 * We do another far jump to reload %cs with the 64 bit selector. 139 * %cr3 points to a 4-level page table page. 140 * We cannot yet jump all the way to the kernel because we can only 141 * specify a 32 bit linear address. So, yet another trampoline. 142 * 143 * The following instruction is: 144 * ljmp $kernelcode-gdt, $tramp_64-mptramp_start 145 * but gas cannot assemble that. And besides, we patch the targets 146 * in early startup and its a little clearer what we are patching. 147 */ 148jmp_64: 149 .byte 0xea /* opcode for far jump */ 150 .long tramp_64-mptramp_start /* offset in segment */ 151 .word kernelcode-gdt /* index in gdt for 64 bit code */ 152 153 /* 154 * Yeehar! We're running in 64 bit mode! We can mostly ignore our 155 * segment registers, and get on with it. 156 * Note that we are running at the correct virtual address, but with 157 * a 1:1 1GB mirrored mapping over entire address space. We had better 158 * switch to a real %cr3 promptly so that we can get to the direct map 159 * space. Remember that jmp is relative and that we've been relocated, 160 * so use an indirect jump. 161 */ 162 .code64 163tramp_64: 164 movabsq $entry_64,%rax /* 64 bit immediate load */ 165 jmp *%rax 166 167 .p2align 4,0 168gdt: 169 /* 170 * All segment descriptor tables start with a null descriptor 171 */ 172 .long 0x00000000 173 .long 0x00000000 174 175 /* 176 * This is the 64 bit long mode code descriptor. There is no 177 * 64 bit data descriptor. 178 */ 179kernelcode: 180 .long 0x00000000 181 .long 0x00209800 182 183 /* 184 * This is the descriptor for the 32 bit boot code. 185 * %cs: +A, +R, -C, DPL=0, +P, +D, +G 186 * Accessed, Readable, Present, 32 bit, 4G granularity 187 */ 188bootcode: 189 .long 0x0000ffff 190 .long 0x00cf9b00 191 192 /* 193 * This is the descriptor for the 32 bit boot data. 194 * We load it into %ds and %ss. The bits for each selector 195 * are interpreted slightly differently. 196 * %ds: +A, +W, -E, DPL=0, +P, +D, +G 197 * %ss: +A, +W, -E, DPL=0, +P, +B, +G 198 * Accessed, Writeable, Expand up, Present, 32 bit, 4GB 199 * For %ds, +D means 'default operand size is 32 bit'. 200 * For %ss, +B means the stack register is %esp rather than %sp. 201 */ 202bootdata: 203 .long 0x0000ffff 204 .long 0x00cf9300 205 206gdtend: 207 208 /* 209 * The address of our page table pages that the boot code 210 * uses to trampoline up to kernel address space. 211 */ 212 .globl mptramp_pagetables 213mptramp_pagetables: 214 .long 0 215 216 /* 217 * The pseudo descriptor for lgdt to use. 218 */ 219lgdt_desc: 220 .word gdtend-gdt /* Length */ 221 .long gdt-mptramp_start /* Offset plus %ds << 4 */ 222 223 .globl mptramp_end 224mptramp_end: 225 226 /* 227 * From here on down is executed in the kernel .text section. 228 * 229 * Load a real %cr3 that has all the direct map stuff and switches 230 * off the 1GB replicated mirror. Load a stack pointer and jump 231 * into AP startup code in C. 232 */ 233 .text 234 .code64 235 .p2align 4,0 236entry_64: 237 movq KPML4phys, %rax 238 movq %rax, %cr3 239 movq bootSTK, %rsp 240 jmp init_secondary 241