]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/start.s
xnu-1486.2.11.tar.gz
[apple/xnu.git] / osfmk / i386 / start.s
1 /*
2 * Copyright (c) 2000 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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 #include <platforms.h>
60
61 #include <i386/asm.h>
62 #include <i386/proc_reg.h>
63 #include <i386/postcode.h>
64 #include <assym.s>
65
66 #define CX(addr,reg) addr(,reg,4)
67
68 #include <i386/lapic.h>
69 #include <i386/acpi.h>
70 #include <i386/cpuid.h>
71
72 /*
73 * Interrupt and bootup stack for initial processor.
74 */
75
76 /* in the __HIB section since the hibernate restore code uses this stack. */
77 .section __HIB, __data
78 .align 12
79
80 .globl EXT(low_intstack)
81 EXT(low_intstack):
82 .globl EXT(gIOHibernateRestoreStack)
83 EXT(gIOHibernateRestoreStack):
84
85 .set ., .+INTSTACK_SIZE
86
87 .globl EXT(low_eintstack)
88 EXT(low_eintstack:)
89 .globl EXT(gIOHibernateRestoreStackEnd)
90 EXT(gIOHibernateRestoreStackEnd):
91
92 /*
93 * Pointers to GDT and IDT. These contain linear addresses.
94 */
95 .align ALIGN
96 .globl EXT(gdtptr)
97 /* align below properly */
98 .word 0
99 LEXT(gdtptr)
100 .word Times(8,GDTSZ)-1
101 .long EXT(master_gdt)
102
103 /* back to the regular __DATA section. */
104
105 .section __DATA, __data
106
107 /*
108 * Stack for last-gasp double-fault handler.
109 */
110 .align 12
111 .globl EXT(df_task_stack)
112 EXT(df_task_stack):
113 .set ., .+INTSTACK_SIZE
114 .globl EXT(df_task_stack_end)
115 EXT(df_task_stack_end):
116
117
118 /*
119 * Stack for machine-check handler.
120 */
121 .align 12
122 .globl EXT(mc_task_stack)
123 EXT(mc_task_stack):
124 .set ., .+INTSTACK_SIZE
125 .globl EXT(mc_task_stack_end)
126 EXT(mc_task_stack_end):
127
128 #if MACH_KDB
129 /*
130 * Kernel debugger stack for each processor.
131 */
132 .align 12
133 .globl EXT(db_stack_store)
134 EXT(db_stack_store):
135 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
136
137 /*
138 * Stack for last-ditch debugger task for each processor.
139 */
140 .align 12
141 .globl EXT(db_task_stack_store)
142 EXT(db_task_stack_store):
143 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
144
145 /*
146 * per-processor kernel debugger stacks
147 */
148 .align ALIGN
149 .globl EXT(kgdb_stack_store)
150 EXT(kgdb_stack_store):
151 .set ., .+(INTSTACK_SIZE*MAX_CPUS)
152 #endif /* MACH_KDB */
153
154
155
156 /*
157 * BSP CPU start here.
158 * eax points to kernbootstruct
159 *
160 * Environment:
161 * protected mode, no paging, flat 32-bit address space.
162 * (Code/data/stack segments have base == 0, limit == 4G)
163 */
164 .text
165 .align ALIGN
166 .globl EXT(_start)
167 LEXT(_start)
168 mov %ds, %bx
169 mov %bx, %es
170 mov %eax, %ebp /* Move kernbootstruct to ebp */
171 mov %eax, %ebx /* get pointer to kernbootstruct */
172
173 mov $EXT(low_eintstack),%esp /* switch to the bootup stack */
174
175 POSTCODE(PSTART_ENTRY)
176
177 lgdt EXT(gdtptr) /* load GDT */
178
179 mov $(KERNEL_DS),%ax /* set kernel data segment */
180 mov %ax, %ds
181 mov %ax, %es
182 mov %ax, %ss
183 xor %ax, %ax /* fs must be zeroed; */
184 mov %ax, %fs /* some bootstrappers don`t do this */
185 mov %ax, %gs
186 cld
187
188 /* "The Aussie Maneuver" ("Myria" variant) */
189 pushl $(0xcb<<24)|KERNEL32_CS /* reload CS */
190 call .-1
191
192 paging:
193 andl $0xfffffff0, %esp /* align stack */
194 subl $0xc, %esp
195 pushl %ebp /* push boot args addr */
196 xorl %ebp, %ebp /* zero frame pointer */
197
198 POSTCODE(PSTART_BEFORE_PAGING)
199
200 /*
201 * Turn on paging.
202 */
203 movl $EXT(IdlePDPT), %eax /* CR3 */
204 movl %eax, %cr3
205 movl %cr4, %eax /* PAE */
206 orl $(CR4_PAE), %eax
207 movl %eax, %cr4
208 movl %cr0,%eax /* paging */
209 orl $(CR0_PG|CR0_WP),%eax
210 movl %eax,%cr0
211
212 call EXT(vstart) /* run C code */
213 /*NOTREACHED*/
214 hlt
215
216 /*
217 * AP (slave) CPUs enter here.
218 *
219 * Environment:
220 * protected mode, no paging, flat 32-bit address space.
221 * (Code/data/stack segments have base == 0, limit == 4G)
222 */
223 .align ALIGN
224 .globl EXT(slave_pstart)
225 LEXT(slave_pstart)
226 cli /* disable interrupts, so we don`t */
227 /* need IDT for a while */
228 xor %ebp, %ebp // zero boot cpu
229 mov $EXT(mp_slave_stack)+PAGE_SIZE, %esp;
230 jmp paging
231
232
233 /* Code to get from real mode to protected mode */
234
235 #define operand_size_prefix .byte 0x66
236 #define address_size_prefix .byte 0x67
237 #define cs_base_prefix .byte 0x2e
238
239 #undef LJMP
240 #define LJMP(segment,address) \
241 operand_size_prefix ;\
242 .byte 0xea ;\
243 .long address-EXT(real_mode_bootstrap_base) ;\
244 .word segment
245
246 #define LGDT(address) \
247 cs_base_prefix ;\
248 address_size_prefix ;\
249 operand_size_prefix ;\
250 .word 0x010f ;\
251 .byte 0x15 ;\
252 .long address-EXT(real_mode_bootstrap_base)
253
254 .section __HIB,__text
255 .align 12 /* Page align for single bcopy_phys() */
256 .code32
257 Entry(real_mode_bootstrap_base)
258 cli
259
260 LGDT(EXT(protected_mode_gdtr))
261
262 /* set the PE bit of CR0 */
263 mov %cr0, %eax
264 inc %eax
265 mov %eax, %cr0
266
267 /* reload CS register */
268 LJMP(KERNEL32_CS, 1f + REAL_MODE_BOOTSTRAP_OFFSET)
269 1:
270
271 /* we are in protected mode now */
272 /* set up the segment registers */
273 mov $KERNEL_DS, %eax
274 movw %ax, %ds
275 movw %ax, %es
276 movw %ax, %ss
277 mov $0, %ax
278 movw %ax, %fs
279 movw %ax, %gs
280
281 POSTCODE(SLAVE_STARTPROG_ENTRY);
282
283 mov PROT_MODE_START+REAL_MODE_BOOTSTRAP_OFFSET, %ecx
284 jmp *%ecx
285
286 Entry(protected_mode_gdtr)
287 .short 160 /* limit (8*6 segs) */
288 .long EXT(master_gdt)
289
290 Entry(real_mode_bootstrap_end)
291
292 .section __HIB,__text
293 .align ALIGN
294 .globl EXT(hibernate_machine_entrypoint)
295 LEXT(hibernate_machine_entrypoint)
296 mov %eax, %edi // save header pointer
297 /* restore gdt */
298 lgdt EXT(protected_mode_gdtr)
299
300 /* setup the protected mode segment registers */
301 mov $KERNEL_DS, %eax
302 movw %ax, %ds
303 movw %ax, %es
304 movw %ax, %ss
305 mov $0,%ax /* fs must be zeroed; */
306 mov %ax,%fs
307 mov %ax,%gs
308
309 /* set up the page tables to use BootstrapPTD
310 * as done in idle_pt.c, but this must be done programatically */
311 mov $EXT(IdlePDPT), %eax
312 mov $EXT(BootstrapPTD) + (INTEL_PTE_VALID), %ecx
313 mov $0x0, %edx
314 mov %ecx, (0*8+0)(%eax)
315 mov %edx, (0*8+4)(%eax)
316 add $(PAGE_SIZE), %ecx
317 mov %ecx, (1*8+0)(%eax)
318 mov %edx, (1*8+4)(%eax)
319 add $(PAGE_SIZE), %ecx
320 mov %ecx, (2*8+0)(%eax)
321 mov %edx, (2*8+4)(%eax)
322 add $(PAGE_SIZE), %ecx
323 mov %ecx, (3*8+0)(%eax)
324 mov %edx, (3*8+4)(%eax)
325 mov %eax, %cr3
326
327
328 movl %cr4,%eax
329 orl $(CR4_PAE),%eax
330 movl %eax,%cr4 /* enable page size extensions */
331
332 movl $(MSR_IA32_EFER), %ecx /* MSR number in ecx */
333 rdmsr /* MSR value return in edx: eax */
334 orl $(MSR_IA32_EFER_NXE), %eax /* Set NXE bit in low 32-bits */
335 wrmsr /* Update Extended Feature Enable reg */
336
337 movl %cr0, %eax
338 orl $(CR0_PG|CR0_WP), %eax
339 movl %eax, %cr0 /* ready paging */
340
341 mov $EXT(gIOHibernateRestoreStackEnd), %esp /* setup stack */
342 xorl %ebp, %ebp /* zero frame pointer */
343
344 ljmpl $(KERNEL32_CS), $Ltemp
345 Ltemp:
346 xorl %eax, %eax /* Video memory - N/A */
347 pushl %eax
348 pushl %eax
349 pushl %eax
350 mov %edi, %eax /* Pointer to hibernate header */
351 pushl %eax
352 call EXT(hibernate_kernel_entrypoint)
353 /* NOTREACHED */
354 hlt