2 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <machine/asmacros.h>
33 #include <machine/specialreg.h>
38 * Resume entry point. The BIOS enters here in real mode after POST with
39 * CS set to the page where we stored this code. It should configure the
40 * segment registers with a flat 4 GB address space and EFLAGS.IF = 0.
41 * Depending on the previous sleep state, we may need to initialize more
42 * of the system (i.e., S3 suspend-to-RAM vs. S4 suspend-to-disk).
52 * Set up segment registers for real mode, a small stack for
53 * any calls we make, and clear any flags.
62 /* To debug resume hangs, beep the speaker if the user requested. */
74 /* Re-initialize video BIOS if the reset_video tunable is set. */
80 * Set up segment registers for real mode again in case the
81 * previous BIOS call clobbers them.
88 /* Load GDT for real mode. Use 32 bit prefix for addresses >16 MB. */
91 /* Restore CR2, CR3 and CR4 */
92 movl previous_cr2,%eax
94 movl previous_cr3,%eax
96 movl previous_cr4,%eax
99 /* Transfer some values to protected mode with an inline stack */
101 #define TRANSFER_STACK32(val, idx) \
103 movl %eax,wakeup_32stack+(idx+1)+(idx*4)
105 TRANSFER_STACK32(previous_ss, (NVALUES - 9))
106 TRANSFER_STACK32(previous_fs, (NVALUES - 8))
107 TRANSFER_STACK32(previous_ds, (NVALUES - 7))
108 TRANSFER_STACK32(physical_gdt+2, (NVALUES - 6))
109 TRANSFER_STACK32(where_to_recover, (NVALUES - 5))
110 TRANSFER_STACK32(previous_idt+2, (NVALUES - 4))
111 TRANSFER_STACK32(previous_ldt, (NVALUES - 3))
112 TRANSFER_STACK32(previous_gdt+2, (NVALUES - 2))
113 TRANSFER_STACK32(previous_tr, (NVALUES - 1))
114 TRANSFER_STACK32(previous_cr0, (NVALUES - 0))
116 mov physical_esp,%esi /* to be used in 32bit code */
118 /* Enable protected mode */
124 /* Switch to protected mode by intersegmental jump */
125 ljmpl $KCSEL,$0x12345678 /* Code location, to be replaced */
128 * Now switched to protected mode without paging enabled.
129 * %esi: KERNEL stack pointer (physical address)
135 /* Set up segment registers for protected mode */
136 movw $KDSEL,%ax /* KDSEL to segment registers */
141 movw $KPSEL,%ax /* KPSEL to %fs */
143 movl %esi,%esp /* physical address stack pointer */
146 /* Operands are overwritten in 16 bit code by TRANSFER_STACK32 macro */
147 pushl $0xabcdef09 /* ss + dummy */
148 pushl $0xabcdef08 /* fs + gs */
149 pushl $0xabcdef07 /* ds + es */
150 pushl $0xabcdef06 /* gdt:base (physical address) */
151 pushl $0xabcdef05 /* recover address */
152 pushl $0xabcdef04 /* idt:base */
153 pushl $0xabcdef03 /* ldt + idt:limit */
154 pushl $0xabcdef02 /* gdt:base */
155 pushl $0xabcdef01 /* TR + gdt:limit */
156 pushl $0xabcdef00 /* CR0 */
159 #define CR0_REGISTER 0(%ebp)
160 #define TASK_REGISTER 4(%ebp)
161 #define PREVIOUS_GDT 6(%ebp)
162 #define PREVIOUS_LDT 12(%ebp)
163 #define PREVIOUS_IDT 14(%ebp)
164 #define RECOVER_ADDR 20(%ebp)
165 #define PHYSICAL_GDT_BASE 24(%ebp)
166 #define PREVIOUS_DS 28(%ebp)
167 #define PREVIOUS_ES 30(%ebp)
168 #define PREVIOUS_FS 32(%ebp)
169 #define PREVIOUS_GS 34(%ebp)
170 #define PREVIOUS_SS 36(%ebp)
172 /* Fixup TSS type field */
173 #define TSS_TYPEFIX_MASK 0xf9
175 movl PHYSICAL_GDT_BASE,%ebx
176 movw TASK_REGISTER,%si
177 leal (%ebx,%esi),%eax /* get TSS segment descriptor */
178 andb $TSS_TYPEFIX_MASK,5(%eax)
180 /* Prepare to return to sleep/wakeup code point */
184 /* Pack values from the GDT to be loaded into segment registers. */
185 movl PREVIOUS_DS,%ebx
186 movl PREVIOUS_FS,%ecx
187 movl PREVIOUS_SS,%edx
188 movw TASK_REGISTER,%si
190 movw PREVIOUS_LDT,%si
191 movl RECOVER_ADDR,%edi
193 /* Enable paging and etc. */
194 movl CR0_REGISTER,%eax
197 /* Flush the prefetch queue */
203 * Now we are in kernel virtual memory addressing with the following
204 * original register values:
209 * %edi: recover address
210 * We'll load these back into the segment registers now.
214 movl %esi,%eax /* LDTR + TR */
215 lldt %ax /* load LDT register */
217 ltr %ax /* load task register */
219 /* Restore segment registers */
220 movl %ebx,%eax /* ds + es */
224 movl %ecx,%eax /* fs + gs */
228 movl %edx,%eax /* ss */
231 /* Jump to acpi_restorecpu() */
234 /* used in real mode */
235 physical_gdt: .word 0
237 physical_esp: .long 0
238 previous_cr2: .long 0
239 previous_cr3: .long 0
240 previous_cr4: .long 0
245 * Transfer from real mode to protected mode. The order of these variables
246 * is very important, DO NOT INSERT OR CHANGE unless you know why.
248 previous_cr0: .long 0
250 previous_gdt: .word 0
252 previous_ldt: .word 0
253 previous_idt: .word 0
255 where_to_recover: .long 0