2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001-2012 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. The BIOS enters here in real mode after POST with
41 * CS set to the page where we stored this code. It should configure the
42 * segment registers with a flat 4 GB address space and EFLAGS.IF = 0.
43 * Depending on the previous sleep state, we may need to initialize more
44 * of the system (i.e., S3 suspend-to-RAM vs. S4 suspend-to-disk).
47 .data /* So we can modify it */
53 * Set up segment registers for real mode, a small stack for
54 * any calls we make, and clear any flags.
56 cli /* make sure no interrupts */
57 mov %cs, %ax /* copy %cs to %ds. Remember these */
58 mov %ax, %ds /* are offsets rather than selectors */
65 /* To debug resume hangs, beep the speaker if the user requested. */
66 testb $~0, resume_beep - wakeup_start
68 movb $0, resume_beep - wakeup_start
70 /* Set PIC timer2 to beep. */
71 movb $(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT), %al
74 /* Turn on speaker. */
81 outb %al, $TIMER_CNTR2
83 outb %al, $TIMER_CNTR2
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 /* When we reach here, int 0x10 should be ready. Hide cursor. */
97 /* Re-start in case the previous BIOS call clobbers them. */
102 * Find relocation base and patch the gdt descript and ljmp targets
106 sall $4, %ebx /* %ebx is now our relocation base */
109 * Load the descriptor table pointer. We'll need it when running
110 * in 16-bit protected mode.
112 lgdtl bootgdtdesc - wakeup_start
114 /* Enable protected mode */
119 * Now execute a far jump to turn on protected mode. This
120 * causes the segment registers to turn into selectors and causes
121 * %cs to be loaded from the gdt.
123 * The following instruction is:
124 * ljmpl $bootcode32 - bootgdt, $wakeup_32 - wakeup_start
125 * but gas cannot assemble that. And besides, we patch the targets
126 * in early startup and its a little clearer what we are patching.
129 .byte 0x66 /* size override to 32 bits */
130 .byte 0xea /* opcode for far jump */
131 .long wakeup_32 - wakeup_start /* offset in segment */
132 .word bootcode32 - bootgdt /* index in gdt for 32 bit code */
135 * At this point, we are running in 32 bit legacy protected mode.
141 mov $bootdata32 - bootgdt, %eax
144 /* Get PCB and return address. */
145 movl wakeup_pcb - wakeup_start(%ebx), %ecx
146 movl wakeup_ret - wakeup_start(%ebx), %edx
148 /* Restore CR4 and CR3. */
149 movl wakeup_cr4 - wakeup_start(%ebx), %eax
151 movl wakeup_cr3 - wakeup_start(%ebx), %eax
155 * Finally, switch to long bit mode by enabling paging. We have
156 * to be very careful here because all the segmentation disappears
157 * out from underneath us. The spec says we can depend on the
158 * subsequent pipelined branch to execute, but *only if* everthing
159 * is still identity mapped. If any mappings change, the pipeline
168 /* Jump to return address. */
193 .word bootgdtend - bootgdt /* Length */
194 .long bootgdt - wakeup_start /* Offset plus %ds << 4 */
205 wakeup_gdt: /* not used */