2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 #include <platforms.h>
63 #include <i386/proc_reg.h>
64 #include <i386/postcode.h>
68 #include <i386/cpuid.h>
69 #include <i386/acpi.h>
75 * Interrupt and bootup stack for initial processor.
78 /* in the __HIB section since the hibernate restore code uses this stack. */
79 .section __HIB, __data
82 .globl EXT(low_intstack)
84 .globl EXT(gIOHibernateRestoreStack)
85 EXT(gIOHibernateRestoreStack):
89 .globl EXT(low_eintstack)
91 .globl EXT(gIOHibernateRestoreStackEnd)
92 EXT(gIOHibernateRestoreStackEnd):
94 /* back to the regular __DATA section. */
96 .section __DATA, __data
99 * Stack for last-gasp double-fault handler.
102 .globl EXT(df_task_stack)
105 .globl EXT(df_task_stack_end)
106 EXT(df_task_stack_end):
110 * Stack for machine-check handler.
113 .globl EXT(mc_task_stack)
116 .globl EXT(mc_task_stack_end)
117 EXT(mc_task_stack_end):
120 * BSP CPU start here.
121 * eax points to kernbootstruct
124 * protected mode, no paging, flat 32-bit address space.
125 * (Code/data/stack segments have base == 0, limit == 4G)
128 #define SWITCH_TO_64BIT_MODE \
129 movl $(CR4_PAE),%eax /* enable PAE */ ;\
131 movl $MSR_IA32_EFER,%ecx ;\
133 orl $MSR_IA32_EFER_LME,%eax /* enable long mode */ ;\
135 movl $INITPT_SEG_BASE,%eax ;\
138 orl $(CR0_PG|CR0_WP),%eax /* enable paging */ ;\
140 /* "The Aussie Maneuver" ("Myria" variant) */ ;\
141 pushl $(0xcb<<24)|KERNEL64_CS /* reload CS with 0x08 */ ;\
146 * [ We used to have a reason for the following statement; ]
147 * [ but the issue has been fixed. The line is true ]
148 * [ nevertheless, therefore it should remain there. ]
149 * This proves that Little Endian is superior to Big Endian.
163 mov $0x4D, %al; out %al, %dx
164 mov $0x49, %al; out %al, %dx
165 mov $0x53, %al; out %al, %dx
166 mov $0x54, %al; out %al, %dx
167 mov $0x0D, %al; out %al, %dx
168 mov $0x0A, %al; out %al, %dx
172 * Here we do the minimal setup to switch from 32 bit mode to 64 bit long mode.
174 * Initial memory layout:
176 * -------------------------
178 * | Kernel text/data |
180 * ------------------------- Kernel start addr
183 * ------------------------- 0
186 mov %eax, %edi /* save kernbootstruct */
188 /* Use low 32-bits of address as 32-bit stack */
189 movl $EXT(low_eintstack), %esp
192 * Set up segmentation
194 movl $EXT(protected_mode_gdtr), %eax
197 /* the following code is shared by the master CPU and all slave CPUs */
200 * switch to 64 bit mode
204 /* Flush data segment selectors */
212 /* %edi = boot_args_start */
214 leaq _vstart(%rip), %rcx
215 movq $0xffffff8000000000, %rax /* adjust the pointer to be up high */
216 or %rax, %rsp /* and stack pointer up there too */
218 andq $0xfffffffffffffff0, %rsp /* align stack */
219 xorq %rbp, %rbp /* zero frame pointer */
223 * AP (slave) CPUs enter here.
226 * protected mode, no paging, flat 32-bit address space.
227 * (Code/data/stack segments have base == 0, limit == 4G)
230 .globl EXT(slave_pstart)
233 cli /* disable interrupts, so we don`t */
234 /* need IDT for a while */
235 POSTCODE(SLAVE_PSTART_ENTRY)
237 movl $EXT(mp_slave_stack) + PAGE_SIZE, %esp
239 /* set up identity mapping of page tables */
240 movl $INITPT_SEG_BASE,%eax
241 movl (KERNEL_PML4_INDEX*8)(%eax), %esi
243 movl (KERNEL_PML4_INDEX*8+4)(%eax), %esi
244 movl %esi, (0+4)(%eax)
246 movl $0, %edi /* "no kernbootstruct" */
248 jmp L_pstart_common /* hop a ride to vstart() */
251 /* BEGIN HIBERNATE CODE */
253 .section __HIB, __text
255 This code is linked into the kernel but part of the "__HIB" section, which means
256 its used by code running in the special context of restoring the kernel text and data
257 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
258 it calls or references (ie. hibernate_restore_phys_page())
259 needs to be careful to only touch memory also in the "__HIB" section.
264 .globl EXT(hibernate_machine_entrypoint)
266 LEXT(hibernate_machine_entrypoint)
267 movl %eax, %edi /* regparm(1) calling convention */
270 mov $(SLEEP_SEG_BASE)+20, %eax // load saved_gdt, this may break
273 /* setup the protected mode segment registers */
282 /* set up the page tables to use BootstrapPTD
283 * as done in idle_pt.c, but this must be done programatically */
284 mov $(INITPT_SEG_BASE + PAGE_SIZE), %eax
285 mov $(INITPT_SEG_BASE + 2*PAGE_SIZE | INTEL_PTE_WRITE | INTEL_PTE_VALID), %ecx
287 mov %ecx, (0*8+0)(%eax)
288 mov %edx, (0*8+4)(%eax)
289 add $(PAGE_SIZE), %ecx
290 mov %ecx, (1*8+0)(%eax)
291 mov %edx, (1*8+4)(%eax)
292 add $(PAGE_SIZE), %ecx
293 mov %ecx, (2*8+0)(%eax)
294 mov %edx, (2*8+4)(%eax)
295 add $(PAGE_SIZE), %ecx
296 mov %ecx, (3*8+0)(%eax)
297 mov %edx, (3*8+4)(%eax)
299 /* Temporary stack */
300 mov $(REAL_MODE_BOOTSTRAP_OFFSET + PROT_MODE_START), %esp
304 leaq EXT(hibernate_kernel_entrypoint)(%rip),%rcx
305 leaq EXT(gIOHibernateRestoreStackEnd)(%rip),%rsp /* switch to the bootup stack */
306 movq $0xffffff8000000000, %rax /* adjust the pointer to be up high */
307 orq %rax, %rsp /* and stack pointer up there too :D */
308 orq %rcx, %rax /* put entrypoint in %rax */
309 /* %edi is already filled with header pointer */
310 xorl %esi, %esi /* zero 2nd arg */
311 xorl %edx, %edx /* zero 3rd arg */
312 xorl %ecx, %ecx /* zero 4th arg */
313 andq $0xfffffffffffffff0, %rsp /* align stack */
314 /* (future-proofing, stack should already be aligned) */
315 xorq %rbp, %rbp /* zero frame pointer */
316 call *%rax /* call instead of jmp to keep the required stack alignment */
320 /* END HIBERNATE CODE */
323 /* BEGIN ACPI WAKEUP CODE */
325 #include <i386/acpi.h>
330 #define PA(addr) (addr)
335 * The code from acpi_wake_start to acpi_wake_end is copied to
336 * memory below 1MB. The firmware waking vector is updated to
337 * point at acpi_wake_start in low memory before sleeping.
340 .section __TEXT,__text
342 .align 12 /* Page align for single bcopy_phys() */
344 .globl EXT(acpi_wake_prot)
346 /* protected mode, paging disabled */
348 /* jump to acpi_temp_alloc (stored in saved_tmp) */
349 mov $(SLEEP_SEG_BASE)+16, %eax
350 mov (%eax), %ecx // Load acpi_temp_reloc from saved_eip
353 mov $(SLEEP_SEG_BASE)+16, %esp /* setup stack for 64bit */
357 lea Lwake_64(%rip), %rax
358 movq $0xffffff8000000000, %rdx
366 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
368 * Save CPU state before platform sleep. Restore CPU state
372 ENTRY(acpi_sleep_cpu)
379 /* save general purpose registers */
396 mov %rsp, saved_rsp(%rip)
398 /* make sure tlb is flushed */
402 /* save control registers */
404 mov %rax, saved_cr0(%rip)
406 mov %rax, saved_cr2(%rip)
408 mov %rax, saved_cr4(%rip)
410 /* save segment registers */
411 movw %es, saved_es(%rip)
412 movw %fs, saved_fs(%rip)
413 movw %gs, saved_gs(%rip)
414 movw %ss, saved_ss(%rip)
416 /* save the 64bit user and kernel gs base */
417 /* note: user's curently swapped into kernel base MSR */
418 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
420 movl %eax, saved_ugs_base(%rip)
421 movl %edx, saved_ugs_base+4(%rip)
424 movl %eax, saved_kgs_base(%rip)
425 movl %edx, saved_kgs_base+4(%rip)
428 /* save descriptor table registers */
435 * When system wakes up, the real mode wake handler will revert to
436 * protected mode, then jump to the address stored at saved_eip.
438 leaq acpi_temp_reloc(%rip), %rax
439 mov %eax, saved_eip(%rip)
442 * Call ACPI function provided by the caller to sleep the platform.
443 * This call will not return on success.
449 /* sleep failed, no cpu context lost */
452 .globl EXT(acpi_wake_prot_entry)
453 EXT(acpi_wake_prot_entry):
454 POSTCODE(ACPI_WAKE_PROT_ENTRY)
455 /* Entry from the hibernate code in iokit/Kernel/IOHibernateRestoreKernel.c
457 * Reset the first 4 PDE's to point to entries in IdlePTD, as done in
458 * Idle_PTs_init() during startup */
459 leaq _IdlePDPT(%rip), %rax
460 movq _IdlePTD(%rip), %rcx
461 mov %ecx, %ecx /* zero top 32bits of %rcx */
462 orq $(INTEL_PTE_WRITE|INTEL_PTE_VALID), %rcx
467 movq %rcx, 0x10(%rax)
469 movq %rcx, 0x18(%rax)
475 * restore cr4, PAE and NXE states in an orderly fashion
477 mov saved_cr4(%rip), %rcx
480 mov $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
481 rdmsr /* MSR value return in edx: eax */
482 or $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
483 wrmsr /* Update Extended Feature Enable reg */
485 /* restore kernel GDT */
486 lgdt EXT(protected_mode_gdtr)(%rip)
488 movq saved_cr2(%rip), %rax
491 /* restore CR0, paging enabled */
492 mov saved_cr0(%rip), %rax
495 /* protected mode, paging enabled */
496 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
498 /* load null segment selectors */
503 /* restore local and interrupt descriptor tables */
507 /* restore segment registers */
508 movw saved_es(%rip), %es
509 movw saved_fs(%rip), %fs
510 movw saved_gs(%rip), %gs
511 movw saved_ss(%rip), %ss
513 /* restore the 64bit kernel and user gs base */
514 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
515 movl saved_kgs_base(%rip), %eax
516 movl saved_kgs_base+4(%rip), %edx
519 movl saved_ugs_base(%rip), %eax
520 movl saved_ugs_base+4(%rip), %edx
524 * Restore task register. Before doing this, clear the busy flag
525 * in the TSS descriptor set by the CPU.
527 lea saved_gdt(%rip), %rax
528 movq 2(%rax), %rdx /* GDT base, skip limit word */
529 movl $(KERNEL_TSS), %eax /* TSS segment selector */
530 movb $(K_TSS), 5(%rdx, %rax) /* clear busy flag */
532 ltr saved_tr(%rip) /* restore TR */
535 mov saved_rsp(%rip), %rsp
537 /* restore general purpose registers */
560 /* END ACPI WAKEUP CODE */
561 #endif /* CONFIG_SLEEP */
563 /* Code to get from real mode to protected mode */
565 #define operand_size_prefix .byte 0x66
566 #define address_size_prefix .byte 0x67
567 #define cs_base_prefix .byte 0x2e
569 #define LJMP(segment,address) \
570 operand_size_prefix ;\
572 .long address-EXT(real_mode_bootstrap_base) ;\
575 #define LGDT(address) \
577 address_size_prefix ;\
578 operand_size_prefix ;\
581 .long address-EXT(real_mode_bootstrap_base)
583 .section __TEXT,__text
584 .align 12 /* Page align for single bcopy_phys() */
586 Entry(real_mode_bootstrap_base)
589 LGDT(EXT(protected_mode_gdtr))
591 /* set the PE bit of CR0 */
596 /* reload CS register */
597 LJMP(KERNEL32_CS, 1f + REAL_MODE_BOOTSTRAP_OFFSET)
600 /* we are in protected mode now */
601 /* set up the segment registers */
610 POSTCODE(SLAVE_STARTPROG_ENTRY);
612 mov PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET, %ecx
615 Entry(protected_mode_gdtr)
616 .short 160 /* limit (8*6 segs) */
617 .quad EXT(master_gdt)
619 Entry(real_mode_bootstrap_end)
621 /* Save area used across sleep/wake */
622 .section __SLEEP, __data
642 saved_kgs_base: .quad 0
643 saved_ugs_base: .quad 0