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