2 * Copyright (c) 2004 Apple Computer, 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@
30 #include <i386/proc_reg.h>
32 #include <i386/postcode.h>
36 This code is linked into the kernel but part of the "__HIB" section, which means
37 its used by code running in the special context of restoring the kernel text and data
38 from the hibernation image read by the booter. hibernate_kernel_entrypoint() and everything
39 it calls or references (ie. hibernate_restore_phys_page())
40 needs to be careful to only touch memory also in the "__HIB" section.
44 * GAS won't handle an intersegment jump with a relocatable offset.
46 #define LJMP(segment,address) \
51 /* Location of temporary page tables */
52 #define HPTD (0x13000)
53 #define HPDPT (0x17000)
55 #define LAST_PAGE (0xFFE00000)
56 #define LAST_PAGE_PDE (0x7ff)
60 * eax = physical page address
61 * ebx = index into page table
62 * ecx = how many pages to map
63 * base = base address of page dir/table
64 * prot = protection bits
66 #define fillpse(base, prot) \
69 orl $(PTE_V|PTE_PS|0x60), %eax ; \
72 1: movl %eax,(%ebx) ; /* low 32b */ \
74 movl %edx,(%ebx) ; /* high 32b */ \
75 addl $(1 << PDESHIFT),%eax ; /* increment physical address 2Mb */ \
76 addl $4,%ebx ; /* next entry */ \
84 * ------------------------------------------------------------
85 * | | |B| |A| | | |1|0|E|W|A| |
86 * | BASE 31..24 |G|/|0|V| LIMIT |P|DPL| TYPE | BASE 23:16 |
87 * | | |D| |L| 19..16| | |1|1|C|R|A| |
88 * ------------------------------------------------------------
90 * | BASE 15..0 | LIMIT 15..0 |
92 * ------------------------------------------------------------
97 .word 0, 0 /* 0x0 : null */
100 .word 0xffff, 0x0000 /* 0x8 : code */
101 .byte 0, 0x9e, 0xcf, 0
103 .word 0xffff, 0x0000 /* 0x10 : data */
104 .byte 0, 0x92, 0xcf, 0
107 .word 24 /* limit (8*3 segs) */
111 * Hibernation code restarts here. Steal some pages from 0x10000
112 * to 0x90000 for pages tables and directories etc to temporarily
113 * map the hibernation code (put at 0x100000 (phys) by the booter
114 * and linked to 0xC0100000 by the linker) to 0xC0100000 so it can
115 * execute. It's self-contained and won't make any references outside
118 * On the way down it has to save IdlePTD (and if PAE also IdlePDPT)
119 * and after it runs it has to restore those and load IdlePTD (or
120 * IdlePDPT if PAE) into %cr3 to re-establish the original mappings
124 .globl EXT(hibernate_machine_entrypoint)
125 LEXT(hibernate_machine_entrypoint)
132 /* Map physical memory from zero to LAST_PAGE */
135 movl $(LAST_PAGE_PDE), %ecx
136 fillpse( $(HPTD), $(PTE_W) )
144 movl %eax,(%ebx) ; /* low 32b */ \
146 movl %edx,(%ebx) ; /* high 32b */ \
148 addl $(1 << 12),%eax ; /* increment physical address 1Gb */ \
150 movl %eax,(%ebx) ; /* low 32b */ \
152 movl %edx,(%ebx) ; /* high 32b */ \
154 addl $(1 << 12),%eax ; /* increment physical address 1Gb */ \
156 movl %eax,(%ebx) ; /* low 32b */ \
158 movl %edx,(%ebx) ; /* high 32b */ \
160 addl $(1 << 12),%eax ; /* increment physical address 1Gb */ \
162 movl %eax,(%ebx) ; /* low 32b */
164 movl %edx,(%ebx) ; /* high 32b */ \
166 addl $(1 << 12),%eax ; /* increment physical address 1Gb */ \
168 /* set page dir ptr table addr */
176 movl %eax,%cr4 /* enable page size extensions */
178 movl $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
179 rdmsr /* MSR value return in edx: eax */
180 orl $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
181 wrmsr /* Update Extended Feature Enable reg */
184 orl $(CR0_PG|CR0_WP|CR0_PE), %eax
185 movl %eax, %cr0 /* ready paging */
189 lgdt EXT(gdtptr) /* load GDT */
190 lidt EXT(idtptr) /* load IDT */
194 LJMP (KERNEL_CS,EXT(hstart)) /* paging on and go to correct vaddr */
196 /* Hib restart code now running with correct addresses */
200 mov $(KERNEL_DS),%ax /* set kernel data segment */
205 mov $0,%ax /* fs must be zeroed; */
206 mov %ax,%fs /* some bootstrappers don`t do this */
209 lea EXT(gIOHibernateRestoreStackEnd),%esp /* switch to the bootup stack */
213 xorl %eax, %eax /* Video memory - N/A */
217 mov %edi, %eax /* Pointer to hibernate header */
219 call EXT(hibernate_kernel_entrypoint)
225 hibernate_restore_phys_page(uint64_t src, uint64_t dst, uint32_t len, uint32_t procFlags);
229 .globl EXT(hibernate_restore_phys_page)
231 /* XXX can only deal with exactly one page */
232 LEXT(hibernate_restore_phys_page)
236 movl 8+ 4(%esp),%esi /* source virtual address */
238 jz 3f /* If source == 0, nothing to do */
240 movl 8+ 16(%esp),%eax /* destination physical address, high 32 bits */
241 movl 8+ 12(%esp),%edi /* destination physical address, low 32 bits */
243 jne 1f /* need to map, above LAST_PAGE */
245 cmpl $(LAST_PAGE), %edi
246 jb 2f /* no need to map, below LAST_PAGE */
248 /* Map physical address %eax:%edi to virt. address LAST_PAGE (4GB - 2MB) */
249 movl %eax, (HPTD + (LAST_PAGE_PDE * 8) + 4)
250 movl %edi, %eax /* destination physical address */
251 andl $(LAST_PAGE), %eax
252 orl $(PTE_V | PTE_PS | PTE_W), %eax
253 movl %eax, (HPTD + (LAST_PAGE_PDE * 8))
254 orl $(LAST_PAGE), %edi
258 movl 8+ 20(%esp),%edx /* number of bytes */