2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
25 #include <mach/machine/vm_types.h>
27 #include <mach/boolean.h>
28 #include <kern/thread.h>
29 #include <kern/zalloc.h>
31 #include <kern/lock.h>
32 #include <kern/kalloc.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>
43 #include <mach/machine/vm_param.h>
44 #include <machine/thread.h>
46 #include <kern/misc_protos.h> /* prototyping */
47 #include <i386/misc_protos.h>
49 #include <i386/cpuid.h>
50 #include <i386/cpu_data.h>
52 #include <i386/cpu_number.h>
53 #include <i386/machine_cpu.h>
54 #include <i386/mp_slave_boot.h>
57 #include <vm/vm_protos.h>
59 #include <sys/kdebug.h>
61 #include <i386/postcode.h>
64 cpu_IA32e_enable(cpu_data_t
*cdp
)
66 uint32_t cr0
= get_cr0();
67 uint64_t efer
= rdmsr64(MSR_IA32_EFER
);
69 assert(!ml_get_interrupts_enabled());
71 postcode(CPU_IA32_ENABLE_ENTRY
);
73 /* Turn paging off - works because we're identity mapped */
74 set_cr0(cr0
& ~CR0_PG
);
76 /* pop in new top level phys pg addr */
77 set_cr3((vm_offset_t
) kernel64_cr3
);
79 wrmsr64(MSR_IA32_EFER
, efer
| MSR_IA32_EFER_LME
); /* set mode */
82 set_cr0(cr0
| CR0_PG
);
84 /* this call is required to re-activate paging */
85 kprintf("cpu_IA32e_enable(%p)\n", cdp
);
87 if ((rdmsr64(MSR_IA32_EFER
) & MSR_IA32_EFER_LMA
) == 0)
88 panic("cpu_IA32e_enable() MSR_IA32_EFER_LMA not asserted");
90 cdp
->cpu_kernel_cr3
= kernel64_cr3
;
92 postcode(CPU_IA32_ENABLE_EXIT
);
96 cpu_IA32e_disable(cpu_data_t
*cdp
)
98 uint32_t cr0
= get_cr0();
99 uint64_t efer
= rdmsr64(MSR_IA32_EFER
);
101 assert(!ml_get_interrupts_enabled());
103 postcode(CPU_IA32_DISABLE_ENTRY
);
105 if ((rdmsr64(MSR_IA32_EFER
) & MSR_IA32_EFER_LMA
) == 0)
106 panic("cpu_IA32e_disable() MSR_IA32_EFER_LMA clear on entry");
108 /* Turn paging off - works because we're identity mapped */
109 set_cr0(cr0
& ~CR0_PG
);
111 /* pop in legacy top level phys pg addr */
112 set_cr3((vm_offset_t
) lo_kernel_cr3
);
114 wrmsr64(MSR_IA32_EFER
, efer
& ~MSR_IA32_EFER_LME
); /* reset mode */
117 set_cr0(cr0
| CR0_PG
);
119 /* this call is required to re-activate paging */
120 kprintf("cpu_IA32e_disable(%p)\n", cdp
);
122 if ((rdmsr64(MSR_IA32_EFER
) & MSR_IA32_EFER_LMA
) != 0)
123 panic("cpu_IA32e_disable() MSR_IA32_EFER_LMA not cleared");
125 cdp
->cpu_kernel_cr3
= 0ULL;
127 postcode(CPU_IA32_DISABLE_EXIT
);
131 fix_desc64(void *descp
, int count
)
133 struct fake_descriptor64
*fakep
;
135 struct real_gate64 gate
;
136 struct real_descriptor64 desc
;
140 fakep
= (struct fake_descriptor64
*) descp
;
142 for (i
= 0; i
< count
; i
++, fakep
++) {
144 * Construct the real decriptor locally.
147 bzero((void *) &real
, sizeof(real
));
149 switch (fakep
->access
& ACC_TYPE
) {
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];
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];
174 * Now copy back over the fake structure.
176 bcopy((void *) &real
, (void *) fakep
, sizeof(real
));
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
);
189 dump_frame32(x86_saved_state_compat32_t
*scp
)
192 uint32_t *ip
= (uint32_t *) scp
;
194 kprintf("dump_frame32(0x%08x):\n", scp
);
197 i
< sizeof(x86_saved_state_compat32_t
)/sizeof(uint32_t);
199 kprintf("0x%08x: 0x%08x\n", ip
, *ip
);
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
);
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
);
232 dump_frame64(x86_saved_state64_t
*scp
)
235 uint64_t *ip
= (uint64_t *) scp
;
237 kprintf("dump_frame64(0x%08x):\n", scp
);
240 i
< sizeof(x86_saved_state64_t
)/sizeof(uint64_t);
242 kprintf("0x%08x: 0x%016x\n", ip
, *ip
);
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
);
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
);
282 uint32_t *ip
= (uint32_t *) gdtp
;
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));
295 uint32_t *ip
= (uint32_t *) ldtp
;
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));
308 uint32_t *ip
= (uint32_t *) idtp
;
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));
323 uint32_t *ip
= (uint32_t *) tssp
;
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));