2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * Copyright (c) 2003 Peter Wemm
5 * Copyright (c) 2008-2009 Jung-uk Kim <jkim@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <machine/asmacros.h>
35 #include <machine/specialreg.h>
40 * Resume entry point for real mode.
42 * If XFirmwareWakingVector is zero and FirmwareWakingVector is non-zero
43 * in FACS, the BIOS enters here in real mode after POST with CS set to
44 * (FirmwareWakingVector >> 4) and IP set to (FirmwareWakingVector & 0xf).
45 * Depending on the previous sleep state, we may need to initialize more
46 * of the system (i.e., S3 suspend-to-RAM vs. S4 suspend-to-disk).
48 * Note: If XFirmwareWakingVector is non-zero, it should disable address
49 * translation/paging and interrupts, load all segment registers with
50 * a flat 4 GB address space, and set EFLAGS.IF to zero. Currently
51 * this mode is not supported by this code.
54 .data /* So we can modify it */
60 * Set up segment registers for real mode, a small stack for
61 * any calls we make, and clear any flags.
63 cli /* make sure no interrupts */
65 mov %cs, %ax /* copy %cs to %ds. Remember these */
66 mov %ax, %ds /* are offsets rather than selectors */
68 movw $PAGE_SIZE - 8, %sp
73 /* To debug resume hangs, beep the speaker if the user requested. */
74 testb $~0, resume_beep - wakeup_start
76 movb $0, resume_beep - wakeup_start
86 /* Re-initialize video BIOS if the reset_video tunable is set. */
87 testb $~0, reset_video - wakeup_start
89 movb $0, reset_video - wakeup_start
92 /* Re-start in case the previous BIOS call clobbers them. */
97 * Find relocation base and patch the gdt descript and ljmp targets
101 sall $4, %ebx /* %ebx is now our relocation base */
104 * Load the descriptor table pointer. We'll need it when running
105 * in 16-bit protected mode.
107 lgdtl bootgdtdesc - wakeup_start
109 /* Enable protected mode */
114 * Now execute a far jump to turn on protected mode. This
115 * causes the segment registers to turn into selectors and causes
116 * %cs to be loaded from the gdt.
118 * The following instruction is:
119 * ljmpl $bootcode32 - bootgdt, $wakeup_32 - wakeup_start
120 * but gas cannot assemble that. And besides, we patch the targets
121 * in early startup and its a little clearer what we are patching.
124 .byte 0x66 /* size override to 32 bits */
125 .byte 0xea /* opcode for far jump */
126 .long wakeup_32 - wakeup_start /* offset in segment */
127 .word bootcode32 - bootgdt /* index in gdt for 32 bit code */
130 * At this point, we are running in 32 bit legacy protected mode.
135 mov $bootdata32 - bootgdt, %eax
138 /* Turn on the PAE and PSE bits for when paging is enabled */
140 orl $(CR4_PAE | CR4_PSE), %eax
144 * Enable EFER.LME so that we get long mode when all the prereqs are
145 * in place. In this case, it turns on when CR0_PG is finally enabled.
146 * Pick up a few other EFER bits that we'll use need we're here.
150 orl $EFER_LME | EFER_SCE, %eax
154 * Point to the embedded page tables for startup. Note that this
155 * only gets accessed after we're actually in 64 bit mode, however
156 * we can only set the bottom 32 bits of %cr3 in this state. This
157 * means we are required to use a temporary page table that is below
158 * the 4GB limit. %ebx is still our relocation base. We could just
159 * subtract 3 * PAGE_SIZE, but that would be too easy.
161 leal wakeup_pagetables - wakeup_start(%ebx), %eax
166 * Finally, switch to long bit mode by enabling paging. We have
167 * to be very careful here because all the segmentation disappears
168 * out from underneath us. The spec says we can depend on the
169 * subsequent pipelined branch to execute, but *only if* everthing
170 * is still identity mapped. If any mappings change, the pipeline
178 * At this point paging is enabled, and we are in "compatability" mode.
179 * We do another far jump to reload %cs with the 64 bit selector.
180 * %cr3 points to a 4-level page table page.
181 * We cannot yet jump all the way to the kernel because we can only
182 * specify a 32 bit linear address. So, yet another trampoline.
184 * The following instruction is:
185 * ljmp $bootcode64 - bootgdt, $wakeup_64 - wakeup_start
186 * but gas cannot assemble that. And besides, we patch the targets
187 * in early startup and its a little clearer what we are patching.
190 .byte 0xea /* opcode for far jump */
191 .long wakeup_64 - wakeup_start /* offset in segment */
192 .word bootcode64 - bootgdt /* index in gdt for 64 bit code */
195 * Yeehar! We're running in 64-bit mode! We can mostly ignore our
196 * segment registers, and get on with it.
197 * Note that we are running at the correct virtual address, but with
198 * a 1:1 1GB mirrored mapping over entire address space. We had better
199 * switch to a real %cr3 promptly so that we can get to the direct map
200 * space. Remember that jmp is relative and that we've been relocated,
201 * so use an indirect jump.
206 mov $bootdata64 - bootgdt, %eax
209 /* Restore arguments and return. */
210 movq wakeup_ctx - wakeup_start(%rbx), %rdi
211 movq wakeup_kpml4 - wakeup_start(%rbx), %rsi
212 movq wakeup_retaddr - wakeup_start(%rbx), %rax
254 .word bootgdtend - bootgdt /* Length */
255 .long bootgdt - wakeup_start /* Offset plus %ds << 4 */