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
30 #include <machine/asmacros.h>
31 #include <machine/specialreg.h>
36 * Resume entry point. The BIOS enters here in real mode after POST with
37 * CS set to the page where we stored this code. It should configure the
38 * segment registers with a flat 4 GB address space and EFLAGS.IF = 0.
39 * Depending on the previous sleep state, we may need to initialize more
40 * of the system (i.e., S3 suspend-to-RAM vs. S4 suspend-to-disk).
50 * Set up segment registers for real mode, a small stack for
51 * any calls we make, and clear any flags.
60 /* To debug resume hangs, beep the speaker if the user requested. */
72 /* Re-initialize video BIOS if the reset_video tunable is set. */
78 * Set up segment registers for real mode again in case the
79 * previous BIOS call clobbers them.
86 /* Load GDT for real mode. Use 32 bit prefix for addresses >16 MB. */
89 /* Restore CR2, CR3 and CR4 */
90 movl previous_cr2,%eax
92 movl previous_cr3,%eax
94 movl previous_cr4,%eax
97 /* Transfer some values to protected mode with an inline stack */
99 #define TRANSFER_STACK32(val, idx) \
101 movl %eax,wakeup_32stack+(idx+1)+(idx*4)
103 TRANSFER_STACK32(previous_ss, (NVALUES - 9))
104 TRANSFER_STACK32(previous_fs, (NVALUES - 8))
105 TRANSFER_STACK32(previous_ds, (NVALUES - 7))
106 TRANSFER_STACK32(physical_gdt+2, (NVALUES - 6))
107 TRANSFER_STACK32(where_to_recover, (NVALUES - 5))
108 TRANSFER_STACK32(previous_idt+2, (NVALUES - 4))
109 TRANSFER_STACK32(previous_ldt, (NVALUES - 3))
110 TRANSFER_STACK32(previous_gdt+2, (NVALUES - 2))
111 TRANSFER_STACK32(previous_tr, (NVALUES - 1))
112 TRANSFER_STACK32(previous_cr0, (NVALUES - 0))
114 mov physical_esp,%esi /* to be used in 32bit code */
116 /* Enable protected mode */
122 /* Switch to protected mode by intersegmental jump */
123 ljmpl $KCSEL,$0x12345678 /* Code location, to be replaced */
126 * Now switched to protected mode without paging enabled.
127 * %esi: KERNEL stack pointer (physical address)
133 /* Set up segment registers for protected mode */
134 movw $KDSEL,%ax /* KDSEL to segment registers */
139 movw $KPSEL,%ax /* KPSEL to %fs */
141 movl %esi,%esp /* physical address stack pointer */
144 /* Operands are overwritten in 16 bit code by TRANSFER_STACK32 macro */
145 pushl $0xabcdef09 /* ss + dummy */
146 pushl $0xabcdef08 /* fs + gs */
147 pushl $0xabcdef07 /* ds + es */
148 pushl $0xabcdef06 /* gdt:base (physical address) */
149 pushl $0xabcdef05 /* recover address */
150 pushl $0xabcdef04 /* idt:base */
151 pushl $0xabcdef03 /* ldt + idt:limit */
152 pushl $0xabcdef02 /* gdt:base */
153 pushl $0xabcdef01 /* TR + gdt:limit */
154 pushl $0xabcdef00 /* CR0 */
157 #define CR0_REGISTER 0(%ebp)
158 #define TASK_REGISTER 4(%ebp)
159 #define PREVIOUS_GDT 6(%ebp)
160 #define PREVIOUS_LDT 12(%ebp)
161 #define PREVIOUS_IDT 14(%ebp)
162 #define RECOVER_ADDR 20(%ebp)
163 #define PHYSICAL_GDT_BASE 24(%ebp)
164 #define PREVIOUS_DS 28(%ebp)
165 #define PREVIOUS_ES 30(%ebp)
166 #define PREVIOUS_FS 32(%ebp)
167 #define PREVIOUS_GS 34(%ebp)
168 #define PREVIOUS_SS 36(%ebp)
170 /* Fixup TSS type field */
171 #define TSS_TYPEFIX_MASK 0xf9
173 movl PHYSICAL_GDT_BASE,%ebx
174 movw TASK_REGISTER,%si
175 leal (%ebx,%esi),%eax /* get TSS segment descriptor */
176 andb $TSS_TYPEFIX_MASK,5(%eax)
178 /* Prepare to return to sleep/wakeup code point */
182 /* Pack values from the GDT to be loaded into segment registers. */
183 movl PREVIOUS_DS,%ebx
184 movl PREVIOUS_FS,%ecx
185 movl PREVIOUS_SS,%edx
186 movw TASK_REGISTER,%si
188 movw PREVIOUS_LDT,%si
189 movl RECOVER_ADDR,%edi
191 /* Enable paging and etc. */
192 movl CR0_REGISTER,%eax
195 /* Flush the prefetch queue */
201 * Now we are in kernel virtual memory addressing with the following
202 * original register values:
207 * %edi: recover address
208 * We'll load these back into the segment registers now.
212 movl %esi,%eax /* LDTR + TR */
213 lldt %ax /* load LDT register */
215 ltr %ax /* load task register */
217 /* Restore segment registers */
218 movl %ebx,%eax /* ds + es */
222 movl %ecx,%eax /* fs + gs */
226 movl %edx,%eax /* ss */
229 /* Jump to acpi_restorecpu() */
232 /* used in real mode */
233 physical_gdt: .word 0
235 physical_esp: .long 0
236 previous_cr2: .long 0
237 previous_cr3: .long 0
238 previous_cr4: .long 0
243 * Transfer from real mode to protected mode. The order of these variables
244 * is very important, DO NOT INSERT OR CHANGE unless you know why.
246 previous_cr0: .long 0
248 previous_gdt: .word 0
250 previous_ldt: .word 0
251 previous_idt: .word 0
253 where_to_recover: .long 0