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