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.
62 #include <i386/proc_reg.h>
63 #include <i386/postcode.h>
67 #include <i386/cpuid.h>
68 #include <i386/acpi.h>
74 * Interrupt and bootup stack for initial processor.
75 * Note: we switch to a dynamically allocated interrupt stack once VM is up.
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 machine-check handler.
102 .globl EXT(mc_task_stack)
105 .globl EXT(mc_task_stack_end)
106 EXT(mc_task_stack_end):
108 /* Must not clobber EDI */
109 #define SWITCH_TO_64BIT_MODE \
110 movl $(CR4_PAE),%eax /* enable PAE */ ;\
112 movl $MSR_IA32_EFER,%ecx ;\
114 /* enable long mode, NX */ ;\
115 orl $(MSR_IA32_EFER_LME | MSR_IA32_EFER_NXE),%eax ;\
117 movl $EXT(BootPML4),%eax ;\
120 orl $(CR0_PG|CR0_WP),%eax /* enable paging */ ;\
122 ljmpl $KERNEL64_CS,$64f ;\
127 * BSP CPU start here.
128 * eax points to kernbootstruct
131 * protected mode, no paging, flat 32-bit address space.
132 * (Code/data/stack segments have base == 0, limit == 4G)
137 .section __HIB, __text
145 * Here we do the minimal setup to switch from 32 bit mode to 64 bit long mode.
147 * Initial memory layout:
149 * -------------------------
151 * | Kernel text/data |
153 * |-----------------------| Kernel text base addr - 2MB-aligned
155 * |-----------------------|
157 * |-----------------------| Page-aligned
161 * ------------------------- 0
164 mov %eax, %edi /* save kernbootstruct */
166 /* Use low 32-bits of address as 32-bit stack */
167 movl $EXT(low_eintstack), %esp
169 POSTCODE(PSTART_ENTRY)
172 * Set up segmentation
174 movl $EXT(protected_mode_gdtr), %eax
178 * Rebase Boot page tables to kernel base address.
180 movl $EXT(BootPML4), %eax // Level 4:
181 add %eax, 0*8+0(%eax) // - 1:1
182 add %eax, KERNEL_PML4_INDEX*8+0(%eax) // - kernel space
184 movl $EXT(BootPDPT), %edx // Level 3:
185 add %eax, 0*8+0(%edx)
186 add %eax, 1*8+0(%edx)
187 add %eax, 2*8+0(%edx)
188 add %eax, 3*8+0(%edx)
190 POSTCODE(PSTART_REBASE)
192 /* the following code is shared by the master CPU and all slave CPUs */
195 * switch to 64 bit mode
199 /* Flush data segment selectors */
207 test %edi, %edi /* Populate stack canary on BSP */
212 test $(1 << 30), %ecx
214 rdrand %rax /* RAX := 64 bits of DRBG entropy */
215 jnc Lnon_rdrand /* TODO: complain if DRBG fails at this stage */
218 xor %ah, %ah /* Security: zero second byte of stack canary */
219 movq %rax, ___stack_chk_guard(%rip)
220 /* %edi = boot_args_start if BSP */
223 POSTCODE(PSTART_VSTART)
225 /* %edi = boot_args_start */
227 leaq _vstart(%rip), %rcx
228 movq $0xffffff8000000000, %rax /* adjust pointer up high */
229 or %rax, %rsp /* and stack pointer up there */
231 andq $0xfffffffffffffff0, %rsp /* align stack */
232 xorq %rbp, %rbp /* zero frame pointer */
236 rdtsc /* EDX:EAX := TSC */
237 /* Distribute low order bits */
244 /* Incorporate ASLR entropy, if any */
252 ror %cl, %edx /* Right rotate EDX (TSC&0xFF ^ (TSC>>8 & 0xFF))&1F */
256 jmp Lstore_random_guard
258 * AP (slave) CPUs enter here.
261 * protected mode, no paging, flat 32-bit address space.
262 * (Code/data/stack segments have base == 0, limit == 4G)
265 .globl EXT(slave_pstart)
268 cli /* disable interrupts, so we don`t */
269 /* need IDT for a while */
270 POSTCODE(SLAVE_PSTART)
272 movl $EXT(mp_slave_stack) + PAGE_SIZE, %esp
274 xor %edi, %edi /* AP, no "kernbootstruct" */
276 jmp L_pstart_common /* hop a ride to vstart() */
279 /* BEGIN HIBERNATE CODE */
281 .section __HIB, __text
283 * This code is linked into the kernel but part of the "__HIB" section,
284 * which means it's used by code running in the special context of restoring
285 * the kernel text and data from the hibernation image read by the booter.
286 * hibernate_kernel_entrypoint() and everything it calls or references
287 * (ie. hibernate_restore_phys_page()) needs to be careful to only touch
288 * memory also in the "__HIB" section.
292 .globl EXT(hibernate_machine_entrypoint)
294 LEXT(hibernate_machine_entrypoint)
295 movl %eax, %edi /* regparm(1) calling convention */
297 /* Use low 32-bits of address as 32-bit stack */
298 movl $EXT(low_eintstack), %esp
303 movl $EXT(master_gdtr), %eax
306 /* Switch to 64-bit on the Boot PTs */
309 leaq EXT(hibernate_kernel_entrypoint)(%rip),%rcx
311 /* adjust the pointers to be up high */
312 movq $0xffffff8000000000, %rax
316 /* %edi is already filled with header pointer */
317 xorl %esi, %esi /* zero 2nd arg */
318 xorl %edx, %edx /* zero 3rd arg */
319 xorl %ecx, %ecx /* zero 4th arg */
320 andq $0xfffffffffffffff0, %rsp /* align stack */
322 /* call instead of jmp to keep the required stack alignment */
323 xorq %rbp, %rbp /* zero frame pointer */
329 /* END HIBERNATE CODE */
332 /* BEGIN ACPI WAKEUP CODE */
334 #include <i386/acpi.h>
341 .section __TEXT,__text
345 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
347 * Save CPU state before platform sleep. Restore CPU state
351 ENTRY(acpi_sleep_cpu)
358 /* save general purpose registers */
375 mov %rsp, saved_rsp(%rip)
377 /* make sure tlb is flushed */
381 /* save control registers */
383 mov %rax, saved_cr0(%rip)
385 mov %rax, saved_cr2(%rip)
387 mov %rax, saved_cr3(%rip)
389 mov %rax, saved_cr4(%rip)
391 /* save segment registers */
392 movw %es, saved_es(%rip)
393 movw %fs, saved_fs(%rip)
394 movw %gs, saved_gs(%rip)
395 movw %ss, saved_ss(%rip)
397 /* save the 64bit user and kernel gs base */
398 /* note: user's curently swapped into kernel base MSR */
399 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
401 movl %eax, saved_ugs_base(%rip)
402 movl %edx, saved_ugs_base+4(%rip)
405 movl %eax, saved_kgs_base(%rip)
406 movl %edx, saved_kgs_base+4(%rip)
409 /* save descriptor table registers */
416 * Call ACPI function provided by the caller to sleep the platform.
417 * This call will not return on success.
423 /* sleep failed, no cpu context lost */
426 .section __HIB, __text
428 .globl EXT(acpi_wake_prot)
430 /* protected mode, paging disabled */
431 movl $EXT(low_eintstack), %esp
437 .section __TEXT,__text
440 .globl EXT(acpi_wake_prot_entry)
441 EXT(acpi_wake_prot_entry):
442 POSTCODE(ACPI_WAKE_PROT_ENTRY)
443 /* Return from hibernate code in iokit/Kernel/IOHibernateRestoreKernel.c
447 * restore cr4, PAE and NXE states in an orderly fashion
449 mov saved_cr4(%rip), %rcx
452 mov $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
453 rdmsr /* MSR value in edx:eax */
454 or $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
457 movq saved_cr2(%rip), %rax
460 /* restore CR0, paging enabled */
461 mov saved_cr0(%rip), %rax
464 /* restore the page tables */
465 mov saved_cr3(%rip), %rax
468 /* protected mode, paging enabled */
469 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
471 /* load null segment selectors */
476 /* restore descriptor tables */
481 /* restore segment registers */
482 movw saved_es(%rip), %es
483 movw saved_fs(%rip), %fs
484 movw saved_gs(%rip), %gs
485 movw saved_ss(%rip), %ss
487 /* restore the 64bit kernel and user gs base */
488 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
489 movl saved_kgs_base(%rip), %eax
490 movl saved_kgs_base+4(%rip), %edx
493 movl saved_ugs_base(%rip), %eax
494 movl saved_ugs_base+4(%rip), %edx
498 * Restore task register. Before doing this, clear the busy flag
499 * in the TSS descriptor set by the CPU.
501 lea saved_gdt(%rip), %rax
502 movq 2(%rax), %rdx /* GDT base, skip limit word */
503 movl $(KERNEL_TSS), %eax /* TSS segment selector */
504 movb $(K_TSS), 5(%rdx, %rax) /* clear busy flag */
506 ltr saved_tr(%rip) /* restore TR */
509 mov saved_rsp(%rip), %rsp
511 /* restore general purpose registers */
534 /* END ACPI WAKEUP CODE */
535 #endif /* CONFIG_SLEEP */
537 /* Code to get from real mode to protected mode */
539 #define operand_size_prefix .byte 0x66
540 #define address_size_prefix .byte 0x67
541 #define cs_base_prefix .byte 0x2e
543 #define LJMP(segment,address) \
544 operand_size_prefix ;\
546 .long address-EXT(real_mode_bootstrap_base) ;\
549 #define LGDT(address) \
551 address_size_prefix ;\
552 operand_size_prefix ;\
555 .long address-EXT(real_mode_bootstrap_base)
557 .section __HIB, __text
558 .align 12 /* Page align for single bcopy_phys() */
560 Entry(real_mode_bootstrap_base)
563 LGDT(EXT(protected_mode_gdtr))
565 /* set the PE bit of CR0 */
570 /* reload CS register */
571 LJMP(KERNEL32_CS, 1f + REAL_MODE_BOOTSTRAP_OFFSET)
574 /* we are in protected mode now */
575 /* set up the segment registers */
584 POSTCODE(SLAVE_STARTPROG_ENTRY);
586 mov PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET, %ecx
589 Entry(protected_mode_gdtr)
590 .short 160 /* limit (8*20 segs) */
591 .quad EXT(master_gdt)
593 Entry(real_mode_bootstrap_end)
595 /* Save area used across sleep/wake */
596 .section __HIB, __data
599 /* gdtr for real address of master_gdt in HIB (not the aliased address) */
601 .word 160 /* limit (8*20 segs) */
602 .quad EXT(master_gdt)
619 saved_kgs_base: .quad 0
620 saved_ugs_base: .quad 0