]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/startup64.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / osfmk / i386 / startup64.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <string.h>
24
25 #include <mach/machine/vm_types.h>
26
27 #include <mach/boolean.h>
28 #include <kern/thread.h>
29 #include <kern/zalloc.h>
30
31 #include <kern/lock.h>
32 #include <kern/kalloc.h>
33 #include <kern/spl.h>
34
35 #include <vm/pmap.h>
36 #include <vm/vm_map.h>
37 #include <vm/vm_kern.h>
38 #include <mach/vm_param.h>
39 #include <mach/vm_prot.h>
40 #include <vm/vm_object.h>
41 #include <vm/vm_page.h>
42
43 #include <mach/machine/vm_param.h>
44 #include <machine/thread.h>
45
46 #include <kern/misc_protos.h> /* prototyping */
47 #include <i386/misc_protos.h>
48
49 #include <i386/cpuid.h>
50 #include <i386/cpu_data.h>
51 #include <i386/mp.h>
52 #include <i386/cpu_number.h>
53 #include <i386/machine_cpu.h>
54 #include <i386/mp_slave_boot.h>
55 #include <i386/seg.h>
56
57 #include <vm/vm_protos.h>
58
59 #include <sys/kdebug.h>
60
61 #include <i386/postcode.h>
62
63 void
64 cpu_IA32e_enable(cpu_data_t *cdp)
65 {
66 uint32_t cr0 = get_cr0();
67 uint64_t efer = rdmsr64(MSR_IA32_EFER);
68
69 assert(!ml_get_interrupts_enabled());
70
71 postcode(CPU_IA32_ENABLE_ENTRY);
72
73 /* Turn paging off - works because we're identity mapped */
74 set_cr0(cr0 & ~CR0_PG);
75
76 /* pop in new top level phys pg addr */
77 set_cr3((vm_offset_t) kernel64_cr3);
78
79 wrmsr64(MSR_IA32_EFER, efer | MSR_IA32_EFER_LME); /* set mode */
80
81 /* Turn paging on */
82 set_cr0(cr0 | CR0_PG);
83
84 /* this call is required to re-activate paging */
85 kprintf("cpu_IA32e_enable(%p)\n", cdp);
86
87 if ((rdmsr64(MSR_IA32_EFER) & MSR_IA32_EFER_LMA) == 0)
88 panic("cpu_IA32e_enable() MSR_IA32_EFER_LMA not asserted");
89
90 cdp->cpu_kernel_cr3 = kernel64_cr3;
91
92 postcode(CPU_IA32_ENABLE_EXIT);
93 }
94
95 void
96 cpu_IA32e_disable(cpu_data_t *cdp)
97 {
98 uint32_t cr0 = get_cr0();
99 uint64_t efer = rdmsr64(MSR_IA32_EFER);
100
101 assert(!ml_get_interrupts_enabled());
102
103 postcode(CPU_IA32_DISABLE_ENTRY);
104
105 if ((rdmsr64(MSR_IA32_EFER) & MSR_IA32_EFER_LMA) == 0)
106 panic("cpu_IA32e_disable() MSR_IA32_EFER_LMA clear on entry");
107
108 /* Turn paging off - works because we're identity mapped */
109 set_cr0(cr0 & ~CR0_PG);
110
111 /* pop in legacy top level phys pg addr */
112 set_cr3((vm_offset_t) lo_kernel_cr3);
113
114 wrmsr64(MSR_IA32_EFER, efer & ~MSR_IA32_EFER_LME); /* reset mode */
115
116 /* Turn paging on */
117 set_cr0(cr0 | CR0_PG);
118
119 /* this call is required to re-activate paging */
120 kprintf("cpu_IA32e_disable(%p)\n", cdp);
121
122 if ((rdmsr64(MSR_IA32_EFER) & MSR_IA32_EFER_LMA) != 0)
123 panic("cpu_IA32e_disable() MSR_IA32_EFER_LMA not cleared");
124
125 cdp->cpu_kernel_cr3 = 0ULL;
126
127 postcode(CPU_IA32_DISABLE_EXIT);
128 }
129
130 void
131 fix_desc64(void *descp, int count)
132 {
133 struct fake_descriptor64 *fakep;
134 union {
135 struct real_gate64 gate;
136 struct real_descriptor64 desc;
137 } real;
138 int i;
139
140 fakep = (struct fake_descriptor64 *) descp;
141
142 for (i = 0; i < count; i++, fakep++) {
143 /*
144 * Construct the real decriptor locally.
145 */
146
147 bzero((void *) &real, sizeof(real));
148
149 switch (fakep->access & ACC_TYPE) {
150 case 0:
151 break;
152 case ACC_CALL_GATE:
153 case ACC_INTR_GATE:
154 case ACC_TRAP_GATE:
155 real.gate.offset_low16 = fakep->offset[0] & 0xFFFF;
156 real.gate.selector16 = fakep->lim_or_seg & 0xFFFF;
157 real.gate.IST = fakep->size_or_IST & 0x7;
158 real.gate.access8 = fakep->access;
159 real.gate.offset_high16 = (fakep->offset[0]>>16)&0xFFFF;
160 real.gate.offset_top32 = (uint32_t)fakep->offset[1];
161 break;
162 default: /* Otherwise */
163 real.desc.limit_low16 = fakep->lim_or_seg & 0xFFFF;
164 real.desc.base_low16 = fakep->offset[0] & 0xFFFF;
165 real.desc.base_med8 = (fakep->offset[0] >> 16) & 0xFF;
166 real.desc.access8 = fakep->access;
167 real.desc.limit_high4 = (fakep->lim_or_seg >> 16) & 0xFF;
168 real.desc.granularity4 = fakep->size_or_IST;
169 real.desc.base_high8 = (fakep->offset[0] >> 24) & 0xFF;
170 real.desc.base_top32 = (uint32_t) fakep->offset[1];
171 }
172
173 /*
174 * Now copy back over the fake structure.
175 */
176 bcopy((void *) &real, (void *) fakep, sizeof(real));
177 }
178 }
179
180 #if DEBUG
181 extern void dump_gdt(void *);
182 extern void dump_ldt(void *);
183 extern void dump_idt(void *);
184 extern void dump_tss(void *);
185 extern void dump_frame32(x86_saved_state_compat32_t *scp);
186 extern void dump_frame64(x86_saved_state64_t *scp);
187
188 void
189 dump_frame32(x86_saved_state_compat32_t *scp)
190 {
191 unsigned int i;
192 uint32_t *ip = (uint32_t *) scp;
193
194 kprintf("dump_frame32(0x%08x):\n", scp);
195
196 for (i = 0;
197 i < sizeof(x86_saved_state_compat32_t)/sizeof(uint32_t);
198 i++, ip++)
199 kprintf("0x%08x: 0x%08x\n", ip, *ip);
200
201 kprintf("scp->isf64.err: 0x%016llx\n", scp->isf64.err);
202 kprintf("scp->isf64.rip: 0x%016llx\n", scp->isf64.rip);
203 kprintf("scp->isf64.cs: 0x%016llx\n", scp->isf64.cs);
204 kprintf("scp->isf64.rflags: 0x%016llx\n", scp->isf64.rflags);
205 kprintf("scp->isf64.rsp: 0x%016llx\n", scp->isf64.rsp);
206 kprintf("scp->isf64.ss: 0x%016llx\n", scp->isf64.ss);
207
208 kprintf("scp->iss32.tag: 0x%08x\n", scp->iss32.tag);
209 kprintf("scp->iss32.state.gs: 0x%08x\n", scp->iss32.state.gs);
210 kprintf("scp->iss32.state.fs: 0x%08x\n", scp->iss32.state.fs);
211 kprintf("scp->iss32.state.es: 0x%08x\n", scp->iss32.state.es);
212 kprintf("scp->iss32.state.ds: 0x%08x\n", scp->iss32.state.ds);
213 kprintf("scp->iss32.state.edi: 0x%08x\n", scp->iss32.state.edi);
214 kprintf("scp->iss32.state.esi: 0x%08x\n", scp->iss32.state.esi);
215 kprintf("scp->iss32.state.ebp: 0x%08x\n", scp->iss32.state.ebp);
216 kprintf("scp->iss32.state.cr2: 0x%08x\n", scp->iss32.state.cr2);
217 kprintf("scp->iss32.state.ebx: 0x%08x\n", scp->iss32.state.ebx);
218 kprintf("scp->iss32.state.edx: 0x%08x\n", scp->iss32.state.edx);
219 kprintf("scp->iss32.state.ecx: 0x%08x\n", scp->iss32.state.ecx);
220 kprintf("scp->iss32.state.eax: 0x%08x\n", scp->iss32.state.eax);
221 kprintf("scp->iss32.state.trapno: 0x%08x\n", scp->iss32.state.eax);
222 kprintf("scp->iss32.state.eip: 0x%08x\n", scp->iss32.state.eip);
223 kprintf("scp->iss32.state.cs: 0x%08x\n", scp->iss32.state.cs);
224 kprintf("scp->iss32.state.efl: 0x%08x\n", scp->iss32.state.efl);
225 kprintf("scp->iss32.state.uesp: 0x%08x\n", scp->iss32.state.uesp);
226 kprintf("scp->iss32.state.ss: 0x%08x\n", scp->iss32.state.ss);
227
228 postcode(0x99);
229 }
230
231 void
232 dump_frame64(x86_saved_state64_t *scp)
233 {
234 unsigned int i;
235 uint64_t *ip = (uint64_t *) scp;
236
237 kprintf("dump_frame64(0x%08x):\n", scp);
238
239 for (i = 0;
240 i < sizeof(x86_saved_state64_t)/sizeof(uint64_t);
241 i++, ip++)
242 kprintf("0x%08x: 0x%016x\n", ip, *ip);
243
244 kprintf("scp->isf.trapno: 0x%08x\n", scp->isf.trapno);
245 kprintf("scp->isf.trapfn: 0x%08x\n", scp->isf.trapfn);
246 kprintf("scp->isf.err: 0x%016llx\n", scp->isf.err);
247 kprintf("scp->isf.rip: 0x%016llx\n", scp->isf.rip);
248 kprintf("scp->isf.cs: 0x%016llx\n", scp->isf.cs);
249 kprintf("scp->isf.rflags: 0x%016llx\n", scp->isf.rflags);
250 kprintf("scp->isf.rsp: 0x%016llx\n", scp->isf.rsp);
251 kprintf("scp->isf.ss: 0x%016llx\n", scp->isf.ss);
252
253 kprintf("scp->fs: 0x%016llx\n", scp->fs);
254 kprintf("scp->gs: 0x%016llx\n", scp->gs);
255 kprintf("scp->rax: 0x%016llx\n", scp->rax);
256 kprintf("scp->rcx: 0x%016llx\n", scp->rcx);
257 kprintf("scp->rbx: 0x%016llx\n", scp->rbx);
258 kprintf("scp->rbp: 0x%016llx\n", scp->rbp);
259 kprintf("scp->r11: 0x%016llx\n", scp->r11);
260 kprintf("scp->r12: 0x%016llx\n", scp->r12);
261 kprintf("scp->r13: 0x%016llx\n", scp->r13);
262 kprintf("scp->r14: 0x%016llx\n", scp->r14);
263 kprintf("scp->r15: 0x%016llx\n", scp->r15);
264 kprintf("scp->cr2: 0x%016llx\n", scp->cr2);
265 kprintf("scp->v_arg8: 0x%016llx\n", scp->v_arg8);
266 kprintf("scp->v_arg7: 0x%016llx\n", scp->v_arg7);
267 kprintf("scp->v_arg6: 0x%016llx\n", scp->v_arg6);
268 kprintf("scp->r9: 0x%016llx\n", scp->r9);
269 kprintf("scp->r8: 0x%016llx\n", scp->r8);
270 kprintf("scp->r10: 0x%016llx\n", scp->r10);
271 kprintf("scp->rdx: 0x%016llx\n", scp->rdx);
272 kprintf("scp->rsi: 0x%016llx\n", scp->rsi);
273 kprintf("scp->rdi: 0x%016llx\n", scp->rdi);
274
275 postcode(0x98);
276 }
277
278 void
279 dump_gdt(void *gdtp)
280 {
281 unsigned int i;
282 uint32_t *ip = (uint32_t *) gdtp;
283
284 kprintf("GDT:\n", ip);
285 for (i = 0; i < GDTSZ; i++, ip += 2) {
286 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
287 kprintf("%p: 0x%08x\n", ip+1, *(ip+1));
288 }
289 }
290
291 void
292 dump_ldt(void *ldtp)
293 {
294 unsigned int i;
295 uint32_t *ip = (uint32_t *) ldtp;
296
297 kprintf("LDT:\n", ip);
298 for (i = 0; i < LDTSZ_MIN; i++, ip += 2) {
299 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
300 kprintf("%p: 0x%08x\n", ip+1, *(ip+1));
301 }
302 }
303
304 void
305 dump_idt(void *idtp)
306 {
307 unsigned int i;
308 uint32_t *ip = (uint32_t *) idtp;
309
310 kprintf("IDT64:\n", ip);
311 for (i = 0; i < 16; i++, ip += 4) {
312 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
313 kprintf("%p: 0x%08x\n", ip+1, *(ip+1));
314 kprintf("%p: 0x%08x\n", ip+2, *(ip+2));
315 kprintf("%p: 0x%08x\n", ip+3, *(ip+3));
316 }
317 }
318
319 void
320 dump_tss(void *tssp)
321 {
322 unsigned int i;
323 uint32_t *ip = (uint32_t *) tssp;
324
325 kprintf("TSS64:\n", ip);
326 for (i = 0; i < sizeof(master_ktss64)/sizeof(uint32_t); i++, ip++) {
327 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
328 }
329 }
330 #endif /* DEBUG */