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-2012 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
32 #include <machine/asmacros.h>
33 #include <machine/ppireg.h>
34 #include <machine/specialreg.h>
35 #include <machine/timerreg.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 */
64 mov %cs, %ax /* copy %cs to %ds. Remember these */
65 mov %ax, %ds /* are offsets rather than selectors */
72 /* To debug resume hangs, beep the speaker if the user requested. */
73 testb $~0, resume_beep - wakeup_start
75 movb $0, resume_beep - wakeup_start
77 /* Set PIC timer2 to beep. */
78 movb $(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT), %al
81 /* Turn on speaker. */
88 outb %al, $TIMER_CNTR2
90 outb %al, $TIMER_CNTR2
93 /* Re-initialize video BIOS if the reset_video tunable is set. */
94 testb $~0, reset_video - wakeup_start
96 movb $0, reset_video - wakeup_start
99 /* When we reach here, int 0x10 should be ready. Hide cursor. */
104 /* Re-start in case the previous BIOS call clobbers them. */
109 * Find relocation base and patch the gdt descript and ljmp targets
113 sall $4, %ebx /* %ebx is now our relocation base */
116 * Load the descriptor table pointer. We'll need it when running
117 * in 16-bit protected mode.
119 lgdtl bootgdtdesc - wakeup_start
121 /* Enable protected mode */
126 * Now execute a far jump to turn on protected mode. This
127 * causes the segment registers to turn into selectors and causes
128 * %cs to be loaded from the gdt.
130 * The following instruction is:
131 * ljmpl $bootcode32 - bootgdt, $wakeup_32 - wakeup_start
132 * but gas cannot assemble that. And besides, we patch the targets
133 * in early startup and its a little clearer what we are patching.
136 .byte 0x66 /* size override to 32 bits */
137 .byte 0xea /* opcode for far jump */
138 .long wakeup_32 - wakeup_start /* offset in segment */
139 .word bootcode32 - bootgdt /* index in gdt for 32 bit code */
142 * At this point, we are running in 32 bit legacy protected mode.
148 mov $bootdata32 - bootgdt, %eax
151 /* Turn on the PAE and PSE bits for when paging is enabled */
153 orl $(CR4_PAE | CR4_PSE), %eax
157 * Enable EFER.LME so that we get long mode when all the prereqs are
158 * in place. In this case, it turns on when CR0_PG is finally enabled.
159 * Pick up a few other EFER bits that we'll use need we're here.
163 orl $EFER_LME | EFER_SCE, %eax
167 * Point to the embedded page tables for startup. Note that this
168 * only gets accessed after we're actually in 64 bit mode, however
169 * we can only set the bottom 32 bits of %cr3 in this state. This
170 * means we are required to use a temporary page table that is below
171 * the 4GB limit. %ebx is still our relocation base. We could just
172 * subtract 3 * PAGE_SIZE, but that would be too easy.
174 leal wakeup_pagetables - wakeup_start(%ebx), %eax
179 * Finally, switch to long bit mode by enabling paging. We have
180 * to be very careful here because all the segmentation disappears
181 * out from underneath us. The spec says we can depend on the
182 * subsequent pipelined branch to execute, but *only if* everthing
183 * is still identity mapped. If any mappings change, the pipeline
191 * At this point paging is enabled, and we are in "compatability" mode.
192 * We do another far jump to reload %cs with the 64 bit selector.
193 * %cr3 points to a 4-level page table page.
194 * We cannot yet jump all the way to the kernel because we can only
195 * specify a 32 bit linear address. So, yet another trampoline.
197 * The following instruction is:
198 * ljmp $bootcode64 - bootgdt, $wakeup_64 - wakeup_start
199 * but gas cannot assemble that. And besides, we patch the targets
200 * in early startup and its a little clearer what we are patching.
203 .byte 0xea /* opcode for far jump */
204 .long wakeup_64 - wakeup_start /* offset in segment */
205 .word bootcode64 - bootgdt /* index in gdt for 64 bit code */
208 * Yeehar! We're running in 64-bit mode! We can mostly ignore our
209 * segment registers, and get on with it.
210 * Note that we are running at the correct virtual address, but with
211 * a 1:1 1GB mirrored mapping over entire address space. We had better
212 * switch to a real %cr3 promptly so that we can get to the direct map
213 * space. Remember that jmp is relative and that we've been relocated,
214 * so use an indirect jump.
219 mov $bootdata64 - bootgdt, %eax
222 /* Restore arguments. */
223 movq wakeup_pcb - wakeup_start(%rbx), %rdi
224 movq wakeup_ret - wakeup_start(%rbx), %rax
227 lgdt wakeup_gdt - wakeup_start(%rbx)
229 /* Jump to return address. */
271 .word bootgdtend - bootgdt /* Length */
272 .long bootgdt - wakeup_start /* Offset plus %ds << 4 */