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