]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/acpi_wakeup.s
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / i386 / acpi_wakeup.s
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <i386/asm.h>
32 #include <i386/proc_reg.h>
33 #include <i386/postcode.h>
34 #include <i386/acpi.h>
35 #include <assym.s>
36
37 .file "acpi_wakeup.s"
38
39 .text
40 .align 12 /* Page align for single bcopy_phys() */
41
42 #define LJMP(segment, address) \
43 .byte 0xea ;\
44 .long address - EXT(acpi_wake_start) ;\
45 .word segment
46
47 #define PA(addr) (addr)
48
49 /*
50 * acpi_wake_start
51 *
52 * The code from acpi_wake_start to acpi_wake_end is copied to
53 * memory below 1MB. The firmware waking vector is updated to
54 * point at acpi_wake_start in low memory before sleeping.
55 */
56
57 ENTRY(acpi_wake_start)
58 /*
59 * CPU woke up from sleep, and is back in real mode.
60 * Initialize it just enough to get back to protected mode.
61 */
62 cli
63
64 POSTCODE(ACPI_WAKE_START_ENTRY)
65
66 /* set up DS to match CS */
67 movw %cs, %ax
68 movw %ax, %ds
69
70 /*
71 * Must initialize GDTR before entering protected mode.
72 * Use a temporary GDT that is 0 based, 4GB limit, code and data.
73 * Restoring the actual GDT will come later.
74 */
75 addr16
76 data16
77 lgdt EXT(acpi_gdtr) - EXT(acpi_wake_start)
78
79 /* set CR0.PE to enter protected mode */
80 mov %cr0, %eax
81 data16
82 or $(CR0_PE), %eax
83 mov %eax, %cr0
84
85 /*
86 * Make intra-segment jump to flush pipeline and reload CS register.
87 * If GDT is bogus, it will blow up here.
88 */
89 data16
90 LJMP(0x8, acpi_wake_prot + ACPI_WAKE_ADDR)
91
92 acpi_wake_prot:
93
94 /* protected mode, paging disabled */
95
96 /* setup the protected mode segment registers */
97 mov $0x10, %eax
98 movw %ax, %ds
99 movw %ax, %es
100 movw %ax, %ss
101 movw %ax, %fs
102 movw %ax, %gs
103
104 /* jump back to the sleep function in the kernel */
105 movl PA(saved_eip), %eax
106 jmp *%eax
107
108 /* Segment Descriptor
109 *
110 * 31 24 19 16 7 0
111 * ------------------------------------------------------------
112 * | | |B| |A| | | |1|0|E|W|A| |
113 * | BASE 31..24 |G|/|0|V| LIMIT |P|DPL| TYPE | BASE 23:16 |
114 * | | |D| |L| 19..16| | |1|1|C|R|A| |
115 * ------------------------------------------------------------
116 * | | |
117 * | BASE 15..0 | LIMIT 15..0 |
118 * | | |
119 * ------------------------------------------------------------
120 */
121 ENTRY(acpi_gdt)
122 .word 0, 0 /* 0x0 : null */
123 .byte 0, 0, 0, 0
124
125 .word 0xffff, 0x0000 /* 0x8 : code */
126 .byte 0, 0x9e, 0xcf, 0
127
128 .word 0xffff, 0x0000 /* 0x10 : data */
129 .byte 0, 0x92, 0xcf, 0
130
131 ENTRY(acpi_gdtr)
132 .word 24 /* limit (8*3 segs) */
133 .long EXT(acpi_gdt) - EXT(acpi_wake_start) + ACPI_WAKE_ADDR
134
135 ENTRY(acpi_wake_end)
136
137
138 /*
139 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
140 *
141 * Save CPU state before platform sleep. Restore CPU state
142 * following wake up.
143 */
144
145 ENTRY(acpi_sleep_cpu)
146 pushl %ebp
147 movl %esp, %ebp
148
149 /* save flags */
150 pushfl
151
152 /* save general purpose registers */
153 pushal
154 movl %esp, saved_esp
155
156 /* make sure tlb is flushed */
157 movl %cr3,%eax
158 movl %eax,%cr3
159
160 /* save control registers */
161 movl %cr0, %eax
162 movl %eax, saved_cr0
163 movl %cr2, %eax
164 movl %eax, saved_cr2
165 movl %cr3, %eax
166 movl %eax, saved_cr3
167 movl %cr4, %eax
168 movl %eax, saved_cr4
169
170 /* save segment registers */
171 movw %es, saved_es
172 movw %fs, saved_fs
173 movw %gs, saved_gs
174 movw %ss, saved_ss
175
176 /* save descriptor table registers */
177 sgdt saved_gdt
178 sldt saved_ldt
179 sidt saved_idt
180 str saved_tr
181
182 /*
183 * When system wakes up, the real mode wake handler will revert to
184 * protected mode, then jump to the address stored at saved_eip.
185 */
186 movl $(PA(wake_prot)), saved_eip
187
188 /*
189 * Call ACPI function provided by the caller to sleep the platform.
190 * This call will not return on success.
191 */
192 pushl B_ARG1
193 movl B_ARG0, %edi
194 call *%edi
195 popl %edi
196
197 /* sleep failed, no cpu context lost */
198 jmp wake_restore
199
200 wake_prot:
201 /* protected mode, paging disabled */
202 POSTCODE(ACPI_WAKE_PROT_ENTRY)
203
204 movl PA(saved_cr3), %ebx
205 movl PA(saved_cr4), %ecx
206 /*
207 * restore cr3, PAE and NXE states in an orderly fashion
208 */
209 movl %ebx, %cr3
210 movl %ecx, %cr4
211
212 movl $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
213 rdmsr /* MSR value return in edx: eax */
214 orl $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
215 wrmsr /* Update Extended Feature Enable reg */
216
217 /* restore kernel GDT */
218 lgdt PA(saved_gdt)
219
220 movl PA(saved_cr2), %eax
221 movl %eax, %cr2
222
223 /* restore CR0, paging enabled */
224 movl PA(saved_cr0), %eax
225 movl %eax, %cr0
226
227 /* switch to kernel code segment */
228 ljmpl $(KERNEL_CS), $wake_paged
229
230 wake_paged:
231
232 /* protected mode, paging enabled */
233 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
234
235 /* switch to kernel data segment */
236 movw $(KERNEL_DS), %ax
237 movw %ax, %ds
238
239 /* restore local and interrupt descriptor tables */
240 lldt saved_ldt
241 lidt saved_idt
242
243 /* restore segment registers */
244 movw saved_es, %es
245 movw saved_fs, %fs
246 movw saved_gs, %gs
247 movw saved_ss, %ss
248
249 /*
250 * Restore task register. Before doing this, clear the busy flag
251 * in the TSS descriptor set by the CPU.
252 */
253 movl $saved_gdt, %eax
254 movl 2(%eax), %edx /* GDT base, skip limit word */
255 movl $(KERNEL_TSS), %eax /* TSS segment selector */
256 movb $(K_TSS), 5(%edx, %eax) /* clear busy flag */
257 ltr saved_tr /* restore TR */
258
259 wake_restore:
260
261 /* restore general purpose registers */
262 movl saved_esp, %esp
263 popal
264
265 /* restore flags */
266 popfl
267
268 leave
269 ret
270
271
272 .section __HIB, __text
273 .align 2
274
275 .globl EXT(acpi_wake_prot_entry)
276 ENTRY(acpi_wake_prot_entry)
277 /* protected mode, paging enabled */
278
279 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
280
281 /* restore kernel GDT */
282 lgdt saved_gdt
283
284 POSTCODE(0x40)
285
286 /* restore control registers */
287
288 movl saved_cr0, %eax
289 movl %eax, %cr0
290
291 movl saved_cr2, %eax
292 movl %eax, %cr2
293
294 POSTCODE(0x3E)
295
296 /* restore real PDE base */
297 movl saved_cr3, %eax
298 movl saved_cr4, %edx
299 movl %eax, %cr3
300 movl %edx, %cr4
301 movl %eax, %cr3
302
303 /* switch to kernel data segment */
304 movw $(KERNEL_DS), %ax
305 movw %ax, %ds
306
307 POSTCODE(0x3C)
308 /* restore local and interrupt descriptor tables */
309 lldt saved_ldt
310 lidt saved_idt
311
312 POSTCODE(0x3B)
313 /* restore segment registers */
314 movw saved_es, %es
315 movw saved_fs, %fs
316 movw saved_gs, %gs
317 movw saved_ss, %ss
318
319 POSTCODE(0x3A)
320 /*
321 * Restore task register. Before doing this, clear the busy flag
322 * in the TSS descriptor set by the CPU.
323 */
324 movl $saved_gdt, %eax
325 movl 2(%eax), %edx /* GDT base, skip limit word */
326 movl $(KERNEL_TSS), %eax /* TSS segment selector */
327 movb $(K_TSS), 5(%edx, %eax) /* clear busy flag */
328 ltr saved_tr /* restore TR */
329
330 /* restore general purpose registers */
331 movl saved_esp, %esp
332 popal
333
334 /* restore flags */
335 popfl
336
337 /* make sure interrupts are disabled */
338 cli
339
340 movl $2, %eax
341
342 leave
343 ret
344
345
346 .data
347 .section __HIB, __data
348 .align 2
349
350
351 /*
352 * CPU registers saved across sleep/wake.
353 */
354
355 saved_esp: .long 0
356 saved_es: .word 0
357 saved_fs: .word 0
358 saved_gs: .word 0
359 saved_ss: .word 0
360 saved_cr0: .long 0
361 saved_cr2: .long 0
362 saved_cr3: .long 0
363 saved_cr4: .long 0
364 saved_gdt: .word 0
365 .long 0
366 saved_idt: .word 0
367 .long 0
368 saved_ldt: .word 0
369 saved_tr: .word 0
370 saved_eip: .long 0
371