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>
66 #include <i386/cpuid.h>
67 #include <i386/acpi.h>
73 * Interrupt and bootup stack for initial processor.
74 * Note: we switch to a dynamically allocated interrupt stack once VM is up.
77 /* in the __HIB section since the hibernate restore code uses this stack. */
78 .section __HIB, __data
81 .globl EXT(low_intstack)
83 .globl EXT(gIOHibernateRestoreStack)
84 EXT(gIOHibernateRestoreStack):
88 .globl EXT(low_eintstack)
90 .globl EXT(gIOHibernateRestoreStackEnd)
91 EXT(gIOHibernateRestoreStackEnd):
93 /* back to the regular __DATA section. */
95 .section __DATA, __data
98 * Stack for machine-check handler.
101 .globl EXT(mc_task_stack)
104 .globl EXT(mc_task_stack_end)
105 EXT(mc_task_stack_end):
107 /* Must not clobber EDI */
108 #define SWITCH_TO_64BIT_MODE \
109 movl $(CR4_PAE),%eax /* enable PAE */ ;\
111 movl $MSR_IA32_EFER,%ecx ;\
113 /* enable long mode, NX */ ;\
114 orl $(MSR_IA32_EFER_LME | MSR_IA32_EFER_NXE),%eax ;\
116 movl $EXT(BootPML4),%eax ;\
119 orl $(CR0_PG|CR0_WP),%eax /* enable paging */ ;\
121 ljmpl $KERNEL64_CS,$64f ;\
126 * BSP CPU start here.
127 * eax points to kernbootstruct
130 * protected mode, no paging, flat 32-bit address space.
131 * (Code/data/stack segments have base == 0, limit == 4G)
136 .section __HIB, __text
144 * Here we do the minimal setup to switch from 32 bit mode to 64 bit long mode.
146 * Initial memory layout:
148 * -------------------------
150 * | Kernel text/data |
152 * |-----------------------| Kernel text base addr - 2MB-aligned
154 * |-----------------------|
156 * |-----------------------| Page-aligned
160 * ------------------------- 0
163 mov %eax, %edi /* save kernbootstruct */
165 /* Use low 32-bits of address as 32-bit stack */
166 movl $EXT(low_eintstack), %esp
168 POSTCODE(PSTART_ENTRY)
171 * Set up segmentation
173 movl $EXT(protected_mode_gdtr), %eax
177 * Rebase Boot page tables to kernel base address.
179 movl $EXT(BootPML4), %eax // Level 4:
180 add %eax, 0*8+0(%eax) // - 1:1
181 add %eax, KERNEL_PML4_INDEX*8+0(%eax) // - kernel space
183 movl $EXT(BootPDPT), %edx // Level 3:
184 add %eax, 0*8+0(%edx)
185 add %eax, 1*8+0(%edx)
186 add %eax, 2*8+0(%edx)
187 add %eax, 3*8+0(%edx)
189 POSTCODE(PSTART_REBASE)
191 /* the following code is shared by the master CPU and all slave CPUs */
194 * switch to 64 bit mode
198 /* Flush data segment selectors */
206 test %edi, %edi /* Populate stack canary on BSP */
211 test $(1 << 30), %ecx
213 rdrand %rax /* RAX := 64 bits of DRBG entropy */
214 jnc Lnon_rdrand /* TODO: complain if DRBG fails at this stage */
217 xor %ah, %ah /* Security: zero second byte of stack canary */
218 movq %rax, ___stack_chk_guard(%rip)
219 /* %edi = boot_args_start if BSP */
222 POSTCODE(PSTART_VSTART)
224 /* %edi = boot_args_start */
226 leaq _vstart(%rip), %rcx
227 movq $0xffffff8000000000, %rax /* adjust pointer up high */
228 or %rax, %rsp /* and stack pointer up there */
230 andq $0xfffffffffffffff0, %rsp /* align stack */
231 xorq %rbp, %rbp /* zero frame pointer */
235 rdtsc /* EDX:EAX := TSC */
236 /* Distribute low order bits */
243 /* Incorporate ASLR entropy, if any */
251 ror %cl, %edx /* Right rotate EDX (TSC&0xFF ^ (TSC>>8 & 0xFF))&1F */
255 jmp Lstore_random_guard
257 * AP (slave) CPUs enter here.
260 * protected mode, no paging, flat 32-bit address space.
261 * (Code/data/stack segments have base == 0, limit == 4G)
264 .globl EXT(slave_pstart)
267 cli /* disable interrupts, so we don`t */
268 /* need IDT for a while */
269 POSTCODE(SLAVE_PSTART)
271 movl $EXT(mp_slave_stack) + PAGE_SIZE, %esp
273 xor %edi, %edi /* AP, no "kernbootstruct" */
275 jmp L_pstart_common /* hop a ride to vstart() */
278 /* BEGIN HIBERNATE CODE */
280 .section __HIB, __text
282 * This code is linked into the kernel but part of the "__HIB" section,
283 * which means it's used by code running in the special context of restoring
284 * the kernel text and data from the hibernation image read by the booter.
285 * hibernate_kernel_entrypoint() and everything it calls or references
286 * (ie. hibernate_restore_phys_page()) needs to be careful to only touch
287 * memory also in the "__HIB" section.
291 .globl EXT(hibernate_machine_entrypoint)
293 LEXT(hibernate_machine_entrypoint)
294 movl %eax, %edi /* regparm(1) calling convention */
296 /* Use low 32-bits of address as 32-bit stack */
297 movl $EXT(low_eintstack), %esp
302 movl $EXT(master_gdtr), %eax
305 /* Switch to 64-bit on the Boot PTs */
308 leaq EXT(hibernate_kernel_entrypoint)(%rip),%rcx
310 /* adjust the pointers to be up high */
311 movq $0xffffff8000000000, %rax
315 /* %edi is already filled with header pointer */
316 xorl %esi, %esi /* zero 2nd arg */
317 xorl %edx, %edx /* zero 3rd arg */
318 xorl %ecx, %ecx /* zero 4th arg */
319 andq $0xfffffffffffffff0, %rsp /* align stack */
321 /* call instead of jmp to keep the required stack alignment */
322 xorq %rbp, %rbp /* zero frame pointer */
328 /* END HIBERNATE CODE */
331 /* BEGIN ACPI WAKEUP CODE */
333 #include <i386/acpi.h>
340 .section __TEXT,__text
344 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
346 * Save CPU state before platform sleep. Restore CPU state
350 ENTRY(acpi_sleep_cpu)
357 /* save general purpose registers */
374 mov %rsp, saved_rsp(%rip)
376 /* make sure tlb is flushed */
380 /* save control registers */
382 mov %rax, saved_cr0(%rip)
384 mov %rax, saved_cr2(%rip)
386 mov %rax, saved_cr3(%rip)
388 mov %rax, saved_cr4(%rip)
390 /* save segment registers */
391 movw %es, saved_es(%rip)
392 movw %fs, saved_fs(%rip)
393 movw %gs, saved_gs(%rip)
394 movw %ss, saved_ss(%rip)
396 /* save the 64bit user and kernel gs base */
397 /* note: user's curently swapped into kernel base MSR */
398 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
400 movl %eax, saved_ugs_base(%rip)
401 movl %edx, saved_ugs_base+4(%rip)
404 movl %eax, saved_kgs_base(%rip)
405 movl %edx, saved_kgs_base+4(%rip)
408 /* save descriptor table registers */
415 * Call ACPI function provided by the caller to sleep the platform.
416 * This call will not return on success.
422 /* sleep failed, no cpu context lost */
425 .section __HIB, __text
427 .globl EXT(acpi_wake_prot)
429 /* protected mode, paging disabled */
430 movl $EXT(low_eintstack), %esp
436 .section __TEXT,__text
439 .globl EXT(acpi_wake_prot_entry)
440 EXT(acpi_wake_prot_entry):
441 POSTCODE(ACPI_WAKE_PROT_ENTRY)
442 /* Return from hibernate code in iokit/Kernel/IOHibernateRestoreKernel.c
446 * restore cr4, PAE and NXE states in an orderly fashion
448 mov saved_cr4(%rip), %rcx
451 mov $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
452 rdmsr /* MSR value in edx:eax */
453 or $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
456 movq saved_cr2(%rip), %rax
459 /* restore CR0, paging enabled */
460 mov saved_cr0(%rip), %rax
463 /* restore the page tables */
464 mov saved_cr3(%rip), %rax
467 /* protected mode, paging enabled */
468 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
470 /* load null segment selectors */
475 /* restore descriptor tables */
480 /* restore segment registers */
481 movw saved_es(%rip), %es
482 movw saved_fs(%rip), %fs
483 movw saved_gs(%rip), %gs
484 movw saved_ss(%rip), %ss
486 /* restore the 64bit kernel and user gs base */
487 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
488 movl saved_kgs_base(%rip), %eax
489 movl saved_kgs_base+4(%rip), %edx
492 movl saved_ugs_base(%rip), %eax
493 movl saved_ugs_base+4(%rip), %edx
497 * Restore task register. Before doing this, clear the busy flag
498 * in the TSS descriptor set by the CPU.
500 lea saved_gdt(%rip), %rax
501 movq 2(%rax), %rdx /* GDT base, skip limit word */
502 movl $(KERNEL_TSS), %eax /* TSS segment selector */
503 movb $(K_TSS), 5(%rdx, %rax) /* clear busy flag */
505 ltr saved_tr(%rip) /* restore TR */
508 mov saved_rsp(%rip), %rsp
510 /* restore general purpose registers */
533 /* END ACPI WAKEUP CODE */
534 #endif /* CONFIG_SLEEP */
536 /* Code to get from real mode to protected mode */
538 #define operand_size_prefix .byte 0x66
539 #define address_size_prefix .byte 0x67
540 #define cs_base_prefix .byte 0x2e
542 #define LJMP(segment,address) \
543 operand_size_prefix ;\
545 .long address-EXT(real_mode_bootstrap_base) ;\
548 #define LGDT(address) \
550 address_size_prefix ;\
551 operand_size_prefix ;\
554 .long address-EXT(real_mode_bootstrap_base)
556 .section __HIB, __text
557 .align 12 /* Page align for single bcopy_phys() */
559 Entry(real_mode_bootstrap_base)
562 LGDT(EXT(protected_mode_gdtr))
564 /* set the PE bit of CR0 */
569 /* reload CS register */
570 LJMP(KERNEL32_CS, 1f + REAL_MODE_BOOTSTRAP_OFFSET)
573 /* we are in protected mode now */
574 /* set up the segment registers */
583 POSTCODE(SLAVE_STARTPROG_ENTRY);
585 mov PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET, %ecx
588 Entry(protected_mode_gdtr)
589 .short 160 /* limit (8*20 segs) */
590 .quad EXT(master_gdt)
592 Entry(real_mode_bootstrap_end)
594 /* Save area used across sleep/wake */
595 .section __HIB, __data
598 /* gdtr for real address of master_gdt in HIB (not the aliased address) */
600 .word 160 /* limit (8*20 segs) */
601 .quad EXT(master_gdt)
618 saved_kgs_base: .quad 0
619 saved_ugs_base: .quad 0