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.
76 * Note: we switch to a dynamically allocated interrupt stack once VM is up.
79 /* in the __HIB section since the hibernate restore code uses this stack. */
80 .section __HIB, __data
83 .globl EXT(low_intstack)
85 .globl EXT(gIOHibernateRestoreStack)
86 EXT(gIOHibernateRestoreStack):
90 .globl EXT(low_eintstack)
92 .globl EXT(gIOHibernateRestoreStackEnd)
93 EXT(gIOHibernateRestoreStackEnd):
95 /* back to the regular __DATA section. */
97 .section __DATA, __data
100 * Stack for machine-check handler.
103 .globl EXT(mc_task_stack)
106 .globl EXT(mc_task_stack_end)
107 EXT(mc_task_stack_end):
109 /* Must not clobber EDI */
110 #define SWITCH_TO_64BIT_MODE \
111 movl $(CR4_PAE),%eax /* enable PAE */ ;\
113 movl $MSR_IA32_EFER,%ecx ;\
115 /* enable long mode, NX */ ;\
116 orl $(MSR_IA32_EFER_LME | MSR_IA32_EFER_NXE),%eax ;\
118 movl $EXT(BootPML4),%eax ;\
121 orl $(CR0_PG|CR0_WP),%eax /* enable paging */ ;\
123 ljmpl $KERNEL64_CS,$64f ;\
128 * BSP CPU start here.
129 * eax points to kernbootstruct
132 * protected mode, no paging, flat 32-bit address space.
133 * (Code/data/stack segments have base == 0, limit == 4G)
138 .section __HIB, __text
146 * Here we do the minimal setup to switch from 32 bit mode to 64 bit long mode.
148 * Initial memory layout:
150 * -------------------------
152 * | Kernel text/data |
154 * |-----------------------| Kernel text base addr - 2MB-aligned
156 * |-----------------------|
158 * |-----------------------| Page-aligned
162 * ------------------------- 0
165 mov %eax, %edi /* save kernbootstruct */
167 /* Use low 32-bits of address as 32-bit stack */
168 movl $EXT(low_eintstack), %esp
170 POSTCODE(PSTART_ENTRY)
173 * Set up segmentation
175 movl $EXT(protected_mode_gdtr), %eax
179 * Rebase Boot page tables to kernel base address.
181 movl $EXT(BootPML4), %eax // Level 4:
182 add %eax, 0*8+0(%eax) // - 1:1
183 add %eax, KERNEL_PML4_INDEX*8+0(%eax) // - kernel space
185 movl $EXT(BootPDPT), %edx // Level 3:
186 add %eax, 0*8+0(%edx)
187 add %eax, 1*8+0(%edx)
188 add %eax, 2*8+0(%edx)
189 add %eax, 3*8+0(%edx)
191 POSTCODE(PSTART_REBASE)
193 /* the following code is shared by the master CPU and all slave CPUs */
196 * switch to 64 bit mode
200 /* Flush data segment selectors */
208 test %edi, %edi /* Populate stack canary on BSP */
213 test $(1 << 30), %ecx
215 RDRAND_RAX /* RAX := 64 bits of DRBG entropy */
216 jnc Lnon_rdrand /* TODO: complain if DRBG fails at this stage */
219 xor %ah, %ah /* Security: zero second byte of stack canary */
220 movq %rax, ___stack_chk_guard(%rip)
221 /* %edi = boot_args_start if BSP */
224 POSTCODE(PSTART_VSTART)
226 /* %edi = boot_args_start */
228 leaq _vstart(%rip), %rcx
229 movq $0xffffff8000000000, %rax /* adjust pointer up high */
230 or %rax, %rsp /* and stack pointer up there */
232 andq $0xfffffffffffffff0, %rsp /* align stack */
233 xorq %rbp, %rbp /* zero frame pointer */
237 rdtsc /* EDX:EAX := TSC */
238 /* Distribute low order bits */
245 /* Incorporate ASLR entropy, if any */
253 ror %cl, %edx /* Right rotate EDX (TSC&0xFF ^ (TSC>>8 & 0xFF))&1F */
257 jmp Lstore_random_guard
259 * AP (slave) CPUs enter here.
262 * protected mode, no paging, flat 32-bit address space.
263 * (Code/data/stack segments have base == 0, limit == 4G)
266 .globl EXT(slave_pstart)
269 cli /* disable interrupts, so we don`t */
270 /* need IDT for a while */
271 POSTCODE(SLAVE_PSTART)
273 movl $EXT(mp_slave_stack) + PAGE_SIZE, %esp
275 xor %edi, %edi /* AP, no "kernbootstruct" */
277 jmp L_pstart_common /* hop a ride to vstart() */
280 /* BEGIN HIBERNATE CODE */
282 .section __HIB, __text
284 * This code is linked into the kernel but part of the "__HIB" section,
285 * which means it's used by code running in the special context of restoring
286 * the kernel text and data from the hibernation image read by the booter.
287 * hibernate_kernel_entrypoint() and everything it calls or references
288 * (ie. hibernate_restore_phys_page()) needs to be careful to only touch
289 * memory also in the "__HIB" section.
293 .globl EXT(hibernate_machine_entrypoint)
295 LEXT(hibernate_machine_entrypoint)
296 movl %eax, %edi /* regparm(1) calling convention */
298 /* Use low 32-bits of address as 32-bit stack */
299 movl $EXT(low_eintstack), %esp
304 movl $EXT(master_gdtr), %eax
307 /* Switch to 64-bit on the Boot PTs */
310 leaq EXT(hibernate_kernel_entrypoint)(%rip),%rcx
312 /* adjust the pointers to be up high */
313 movq $0xffffff8000000000, %rax
317 /* %edi is already filled with header pointer */
318 xorl %esi, %esi /* zero 2nd arg */
319 xorl %edx, %edx /* zero 3rd arg */
320 xorl %ecx, %ecx /* zero 4th arg */
321 andq $0xfffffffffffffff0, %rsp /* align stack */
323 /* call instead of jmp to keep the required stack alignment */
324 xorq %rbp, %rbp /* zero frame pointer */
330 /* END HIBERNATE CODE */
333 /* BEGIN ACPI WAKEUP CODE */
335 #include <i386/acpi.h>
342 .section __TEXT,__text
346 * acpi_sleep_cpu(acpi_sleep_callback func, void * refcon)
348 * Save CPU state before platform sleep. Restore CPU state
352 ENTRY(acpi_sleep_cpu)
359 /* save general purpose registers */
376 mov %rsp, saved_rsp(%rip)
378 /* make sure tlb is flushed */
382 /* save control registers */
384 mov %rax, saved_cr0(%rip)
386 mov %rax, saved_cr2(%rip)
388 mov %rax, saved_cr3(%rip)
390 mov %rax, saved_cr4(%rip)
392 /* save segment registers */
393 movw %es, saved_es(%rip)
394 movw %fs, saved_fs(%rip)
395 movw %gs, saved_gs(%rip)
396 movw %ss, saved_ss(%rip)
398 /* save the 64bit user and kernel gs base */
399 /* note: user's curently swapped into kernel base MSR */
400 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
402 movl %eax, saved_ugs_base(%rip)
403 movl %edx, saved_ugs_base+4(%rip)
406 movl %eax, saved_kgs_base(%rip)
407 movl %edx, saved_kgs_base+4(%rip)
410 /* save descriptor table registers */
417 * Call ACPI function provided by the caller to sleep the platform.
418 * This call will not return on success.
424 /* sleep failed, no cpu context lost */
427 .section __HIB, __text
429 .globl EXT(acpi_wake_prot)
431 /* protected mode, paging disabled */
432 movl $EXT(low_eintstack), %esp
438 .section __TEXT,__text
441 .globl EXT(acpi_wake_prot_entry)
442 EXT(acpi_wake_prot_entry):
443 POSTCODE(ACPI_WAKE_PROT_ENTRY)
444 /* Return from hibernate code in iokit/Kernel/IOHibernateRestoreKernel.c
448 * restore cr4, PAE and NXE states in an orderly fashion
450 mov saved_cr4(%rip), %rcx
453 mov $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
454 rdmsr /* MSR value in edx:eax */
455 or $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
458 movq saved_cr2(%rip), %rax
461 /* restore CR0, paging enabled */
462 mov saved_cr0(%rip), %rax
465 /* restore the page tables */
466 mov saved_cr3(%rip), %rax
469 /* protected mode, paging enabled */
470 POSTCODE(ACPI_WAKE_PAGED_ENTRY)
472 /* load null segment selectors */
477 /* restore descriptor tables */
482 /* restore segment registers */
483 movw saved_es(%rip), %es
484 movw saved_fs(%rip), %fs
485 movw saved_gs(%rip), %gs
486 movw saved_ss(%rip), %ss
488 /* restore the 64bit kernel and user gs base */
489 mov $MSR_IA32_KERNEL_GS_BASE, %rcx
490 movl saved_kgs_base(%rip), %eax
491 movl saved_kgs_base+4(%rip), %edx
494 movl saved_ugs_base(%rip), %eax
495 movl saved_ugs_base+4(%rip), %edx
499 * Restore task register. Before doing this, clear the busy flag
500 * in the TSS descriptor set by the CPU.
502 lea saved_gdt(%rip), %rax
503 movq 2(%rax), %rdx /* GDT base, skip limit word */
504 movl $(KERNEL_TSS), %eax /* TSS segment selector */
505 movb $(K_TSS), 5(%rdx, %rax) /* clear busy flag */
507 ltr saved_tr(%rip) /* restore TR */
510 mov saved_rsp(%rip), %rsp
512 /* restore general purpose registers */
535 /* END ACPI WAKEUP CODE */
536 #endif /* CONFIG_SLEEP */
538 /* Code to get from real mode to protected mode */
540 #define operand_size_prefix .byte 0x66
541 #define address_size_prefix .byte 0x67
542 #define cs_base_prefix .byte 0x2e
544 #define LJMP(segment,address) \
545 operand_size_prefix ;\
547 .long address-EXT(real_mode_bootstrap_base) ;\
550 #define LGDT(address) \
552 address_size_prefix ;\
553 operand_size_prefix ;\
556 .long address-EXT(real_mode_bootstrap_base)
558 .section __HIB, __text
559 .align 12 /* Page align for single bcopy_phys() */
561 Entry(real_mode_bootstrap_base)
564 LGDT(EXT(protected_mode_gdtr))
566 /* set the PE bit of CR0 */
571 /* reload CS register */
572 LJMP(KERNEL32_CS, 1f + REAL_MODE_BOOTSTRAP_OFFSET)
575 /* we are in protected mode now */
576 /* set up the segment registers */
585 POSTCODE(SLAVE_STARTPROG_ENTRY);
587 mov PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET, %ecx
590 Entry(protected_mode_gdtr)
591 .short 160 /* limit (8*20 segs) */
592 .quad EXT(master_gdt)
594 Entry(real_mode_bootstrap_end)
596 /* Save area used across sleep/wake */
597 .section __HIB, __data
600 /* gdtr for real address of master_gdt in HIB (not the aliased address) */
602 .word 160 /* limit (8*20 segs) */
603 .quad EXT(master_gdt)
620 saved_kgs_base: .quad 0
621 saved_ugs_base: .quad 0