]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/startup64.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / osfmk / i386 / startup64.c
1 /*
2 * Copyright (c) 2006-2012 Apple 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/seg.h>
61
62 #include <vm/vm_protos.h>
63
64 #include <sys/kdebug.h>
65
66 #include <i386/postcode.h>
67
68 #if DEBUG
69 extern void dump_regs64(void);
70 extern void dump_gdt(void *);
71 extern void dump_ldt(void *);
72 extern void dump_idt(void *);
73 extern void dump_tss(void *);
74 extern void dump_frame32(x86_saved_state32_t *sp);
75 extern void dump_frame64(x86_saved_state64_t *sp);
76 extern void dump_frame(x86_saved_state_t *sp);
77
78 void
79 dump_frame(x86_saved_state_t *sp)
80 {
81 if (is_saved_state32(sp))
82 dump_frame32(&sp->ss_32);
83 else if (is_saved_state64(sp))
84 dump_frame64(&sp->ss_64);
85 else
86 kprintf("dump_frame(%p) unknown type %d\n", sp, sp->flavor);
87 }
88
89 void
90 dump_frame32(x86_saved_state32_t *sp)
91 {
92 unsigned int i;
93 uint32_t *ip = (uint32_t *) sp;
94
95 kprintf("dump_frame32(%p):\n", sp);
96
97 for (i = 0;
98 i < sizeof(x86_saved_state32_t)/sizeof(uint32_t);
99 i++, ip++)
100 kprintf("%p: 0x%08x\n", ip, *ip);
101
102 kprintf("sp->gs: 0x%08x\n", sp->gs);
103 kprintf("sp->fs: 0x%08x\n", sp->fs);
104 kprintf("sp->es: 0x%08x\n", sp->es);
105 kprintf("sp->ds: 0x%08x\n", sp->ds);
106 kprintf("sp->edi: 0x%08x\n", sp->edi);
107 kprintf("sp->esi: 0x%08x\n", sp->esi);
108 kprintf("sp->ebp: 0x%08x\n", sp->ebp);
109 kprintf("sp->cr2: 0x%08x\n", sp->cr2);
110 kprintf("sp->ebx: 0x%08x\n", sp->ebx);
111 kprintf("sp->edx: 0x%08x\n", sp->edx);
112 kprintf("sp->ecx: 0x%08x\n", sp->ecx);
113 kprintf("sp->eax: 0x%08x\n", sp->eax);
114 kprintf("sp->trapno: 0x%08x\n", sp->eax);
115 kprintf("sp->eip: 0x%08x\n", sp->eip);
116 kprintf("sp->cs: 0x%08x\n", sp->cs);
117 kprintf("sp->efl: 0x%08x\n", sp->efl);
118 kprintf("sp->uesp: 0x%08x\n", sp->uesp);
119 kprintf("sp->ss: 0x%08x\n", sp->ss);
120
121 postcode(0x99);
122 }
123
124 void
125 dump_frame64(x86_saved_state64_t *sp)
126 {
127 unsigned int i;
128 uint64_t *ip = (uint64_t *) sp;
129
130 kprintf("dump_frame64(%p):\n", sp);
131
132 for (i = 0;
133 i < sizeof(x86_saved_state64_t)/sizeof(uint64_t);
134 i++, ip++)
135 kprintf("%p: 0x%016llx\n", ip, *ip);
136
137 kprintf("sp->isf.trapno: 0x%08x\n", sp->isf.trapno);
138 kprintf("sp->isf.trapfn: 0x%016llx\n", sp->isf.trapfn);
139 kprintf("sp->isf.err: 0x%016llx\n", sp->isf.err);
140 kprintf("sp->isf.rip: 0x%016llx\n", sp->isf.rip);
141 kprintf("sp->isf.cs: 0x%016llx\n", sp->isf.cs);
142 kprintf("sp->isf.rflags: 0x%016llx\n", sp->isf.rflags);
143 kprintf("sp->isf.rsp: 0x%016llx\n", sp->isf.rsp);
144 kprintf("sp->isf.ss: 0x%016llx\n", sp->isf.ss);
145
146 kprintf("sp->fs: 0x%016x\n", sp->fs);
147 kprintf("sp->gs: 0x%016x\n", sp->gs);
148 kprintf("sp->rax: 0x%016llx\n", sp->rax);
149 kprintf("sp->rcx: 0x%016llx\n", sp->rcx);
150 kprintf("sp->rbx: 0x%016llx\n", sp->rbx);
151 kprintf("sp->rbp: 0x%016llx\n", sp->rbp);
152 kprintf("sp->r11: 0x%016llx\n", sp->r11);
153 kprintf("sp->r12: 0x%016llx\n", sp->r12);
154 kprintf("sp->r13: 0x%016llx\n", sp->r13);
155 kprintf("sp->r14: 0x%016llx\n", sp->r14);
156 kprintf("sp->r15: 0x%016llx\n", sp->r15);
157 kprintf("sp->cr2: 0x%016llx\n", sp->cr2);
158 kprintf("sp->v_arg8: 0x%016llx\n", sp->v_arg8);
159 kprintf("sp->v_arg7: 0x%016llx\n", sp->v_arg7);
160 kprintf("sp->v_arg6: 0x%016llx\n", sp->v_arg6);
161 kprintf("sp->r9: 0x%016llx\n", sp->r9);
162 kprintf("sp->r8: 0x%016llx\n", sp->r8);
163 kprintf("sp->r10: 0x%016llx\n", sp->r10);
164 kprintf("sp->rdx: 0x%016llx\n", sp->rdx);
165 kprintf("sp->rsi: 0x%016llx\n", sp->rsi);
166 kprintf("sp->rdi: 0x%016llx\n", sp->rdi);
167
168 postcode(0x98);
169 }
170
171 void
172 dump_gdt(void *gdtp)
173 {
174 unsigned int i;
175 uint32_t *ip = (uint32_t *) gdtp;
176
177 kprintf("GDT:\n");
178 for (i = 0; i < GDTSZ; i++, ip += 2) {
179 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
180 kprintf("%p: 0x%08x\n", ip+1, *(ip+1));
181 }
182 }
183
184 void
185 dump_ldt(void *ldtp)
186 {
187 unsigned int i;
188 uint32_t *ip = (uint32_t *) ldtp;
189
190 kprintf("LDT:\n");
191 for (i = 0; i < LDTSZ_MIN; i++, ip += 2) {
192 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
193 kprintf("%p: 0x%08x\n", ip+1, *(ip+1));
194 }
195 }
196
197 void
198 dump_idt(void *idtp)
199 {
200 unsigned int i;
201 uint32_t *ip = (uint32_t *) idtp;
202
203 kprintf("IDT64:\n");
204 for (i = 0; i < 16; i++, ip += 4) {
205 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
206 kprintf("%p: 0x%08x\n", ip+1, *(ip+1));
207 kprintf("%p: 0x%08x\n", ip+2, *(ip+2));
208 kprintf("%p: 0x%08x\n", ip+3, *(ip+3));
209 }
210 }
211
212 void
213 dump_tss(void *tssp)
214 {
215 unsigned int i;
216 uint32_t *ip = (uint32_t *) tssp;
217
218 kprintf("TSS64:\n");
219 for (i = 0; i < sizeof(master_ktss64)/sizeof(uint32_t); i++, ip++) {
220 kprintf("%p: 0x%08x\n", ip+0, *(ip+0));
221 }
222 }
223
224 void dump_regs64(void)
225 {
226
227 #define SNAP_REG(reg) \
228 uint64_t reg; \
229 __asm__ volatile("mov %%" #reg ", %0" : "=m" (reg))
230
231 #define KPRINT_REG(reg) \
232 kprintf("%3s: %p\n", #reg, (void *) reg)
233
234 SNAP_REG(rsp);
235 SNAP_REG(rbp);
236 SNAP_REG(rax);
237 SNAP_REG(rbx);
238 SNAP_REG(rcx);
239 SNAP_REG(rdx);
240 SNAP_REG(rsi);
241 SNAP_REG(rdi);
242 SNAP_REG(r8);
243 SNAP_REG(r9);
244 SNAP_REG(r10);
245 SNAP_REG(r11);
246 SNAP_REG(r12);
247 SNAP_REG(r13);
248 SNAP_REG(r14);
249
250 KPRINT_REG(rsp);
251 KPRINT_REG(rbp);
252 KPRINT_REG(rax);
253 KPRINT_REG(rbx);
254 KPRINT_REG(rcx);
255 KPRINT_REG(rdx);
256 KPRINT_REG(rsi);
257 KPRINT_REG(rdi);
258 KPRINT_REG(r8);
259 KPRINT_REG(r9);
260 KPRINT_REG(r10);
261 KPRINT_REG(r11);
262 KPRINT_REG(r12);
263 KPRINT_REG(r13);
264 KPRINT_REG(r14);
265 }
266 #endif /* DEBUG */