]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/i386_init.c
xnu-3248.50.21.tar.gz
[apple/xnu.git] / osfmk / i386 / i386_init.c
1 /*
2 * Copyright (c) 2003-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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989, 1988 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 #include <mach/i386/vm_param.h>
59
60 #include <string.h>
61 #include <mach/vm_param.h>
62 #include <mach/vm_prot.h>
63 #include <mach/machine.h>
64 #include <mach/time_value.h>
65 #include <kern/spl.h>
66 #include <kern/assert.h>
67 #include <kern/debug.h>
68 #include <kern/misc_protos.h>
69 #include <kern/startup.h>
70 #include <kern/clock.h>
71 #include <kern/pms.h>
72 #include <kern/xpr.h>
73 #include <kern/cpu_data.h>
74 #include <kern/processor.h>
75 #include <sys/kdebug.h>
76 #include <console/serial_protos.h>
77 #include <vm/vm_page.h>
78 #include <vm/pmap.h>
79 #include <vm/vm_kern.h>
80 #include <machine/pal_routines.h>
81 #include <i386/fpu.h>
82 #include <i386/pmap.h>
83 #include <i386/misc_protos.h>
84 #include <i386/cpu_threads.h>
85 #include <i386/cpuid.h>
86 #include <i386/lapic.h>
87 #include <i386/mp.h>
88 #include <i386/mp_desc.h>
89 #if CONFIG_MTRR
90 #include <i386/mtrr.h>
91 #endif
92 #include <i386/machine_routines.h>
93 #if CONFIG_MCA
94 #include <i386/machine_check.h>
95 #endif
96 #include <i386/ucode.h>
97 #include <i386/postcode.h>
98 #include <i386/Diagnostics.h>
99 #include <i386/pmCPU.h>
100 #include <i386/tsc.h>
101 #include <i386/locks.h> /* LcksOpts */
102 #if DEBUG
103 #include <machine/pal_routines.h>
104 #endif
105 #if DEBUG
106 #define DBG(x...) kprintf(x)
107 #else
108 #define DBG(x...)
109 #endif
110
111 int debug_task;
112
113 static boot_args *kernelBootArgs;
114
115 extern int disableConsoleOutput;
116 extern const char version[];
117 extern const char version_variant[];
118 extern int nx_enabled;
119
120 uint64_t physmap_base, physmap_max;
121
122 pd_entry_t *KPTphys;
123 pd_entry_t *IdlePTD;
124 pdpt_entry_t *IdlePDPT;
125 pml4_entry_t *IdlePML4;
126
127 char *physfree;
128
129 /*
130 * Note: ALLOCPAGES() can only be used safely within Idle_PTs_init()
131 * due to the mutation of physfree.
132 */
133 static void *
134 ALLOCPAGES(int npages)
135 {
136 uintptr_t tmp = (uintptr_t)physfree;
137 bzero(physfree, npages * PAGE_SIZE);
138 physfree += npages * PAGE_SIZE;
139 tmp += VM_MIN_KERNEL_ADDRESS & ~LOW_4GB_MASK;
140 return (void *)tmp;
141 }
142
143 static void
144 fillkpt(pt_entry_t *base, int prot, uintptr_t src, int index, int count)
145 {
146 int i;
147 for (i=0; i<count; i++) {
148 base[index] = src | prot | INTEL_PTE_VALID;
149 src += PAGE_SIZE;
150 index++;
151 }
152 }
153
154 extern pmap_paddr_t first_avail;
155
156 int break_kprintf = 0;
157
158 uint64_t
159 x86_64_pre_sleep(void)
160 {
161 IdlePML4[0] = IdlePML4[KERNEL_PML4_INDEX];
162 uint64_t oldcr3 = get_cr3_raw();
163 set_cr3_raw((uint32_t) (uintptr_t)ID_MAP_VTOP(IdlePML4));
164 return oldcr3;
165 }
166
167 void
168 x86_64_post_sleep(uint64_t new_cr3)
169 {
170 IdlePML4[0] = 0;
171 set_cr3_raw((uint32_t) new_cr3);
172 }
173
174
175
176
177 // Set up the physical mapping - NPHYSMAP GB of memory mapped at a high address
178 // NPHYSMAP is determined by the maximum supported RAM size plus 4GB to account
179 // the PCI hole (which is less 4GB but not more).
180
181 /* Compile-time guard: NPHYSMAP is capped to 256GiB, accounting for
182 * randomisation
183 */
184 extern int maxphymapsupported[NPHYSMAP <= (PTE_PER_PAGE/2) ? 1 : -1];
185
186 static void
187 physmap_init(void)
188 {
189 pt_entry_t *physmapL3 = ALLOCPAGES(1);
190 struct {
191 pt_entry_t entries[PTE_PER_PAGE];
192 } * physmapL2 = ALLOCPAGES(NPHYSMAP);
193
194 uint64_t i;
195 uint8_t phys_random_L3 = early_random() & 0xFF;
196
197 /* We assume NX support. Mark all levels of the PHYSMAP NX
198 * to avoid granting executability via a single bit flip.
199 */
200 #if DEVELOPMENT || DEBUG
201 uint32_t reg[4];
202 do_cpuid(0x80000000, reg);
203 if (reg[eax] >= 0x80000001) {
204 do_cpuid(0x80000001, reg);
205 assert(reg[edx] & CPUID_EXTFEATURE_XD);
206 }
207 #endif /* DEVELOPMENT || DEBUG */
208
209 for(i = 0; i < NPHYSMAP; i++) {
210 physmapL3[i + phys_random_L3] =
211 ((uintptr_t)ID_MAP_VTOP(&physmapL2[i]))
212 | INTEL_PTE_VALID
213 | INTEL_PTE_NX
214 | INTEL_PTE_WRITE;
215
216 uint64_t j;
217 for(j = 0; j < PTE_PER_PAGE; j++) {
218 physmapL2[i].entries[j] =
219 ((i * PTE_PER_PAGE + j) << PDSHIFT)
220 | INTEL_PTE_PS
221 | INTEL_PTE_VALID
222 | INTEL_PTE_NX
223 | INTEL_PTE_WRITE;
224 }
225 }
226
227 IdlePML4[KERNEL_PHYSMAP_PML4_INDEX] =
228 ((uintptr_t)ID_MAP_VTOP(physmapL3))
229 | INTEL_PTE_VALID
230 | INTEL_PTE_NX
231 | INTEL_PTE_WRITE;
232
233 physmap_base = KVADDR(KERNEL_PHYSMAP_PML4_INDEX, phys_random_L3, 0, 0);
234 physmap_max = physmap_base + NPHYSMAP * GB;
235 DBG("Physical address map base: 0x%qx\n", physmap_base);
236 DBG("Physical map idlepml4[%d]: 0x%llx\n",
237 KERNEL_PHYSMAP_PML4_INDEX, IdlePML4[KERNEL_PHYSMAP_PML4_INDEX]);
238 }
239
240 static void
241 descriptor_alias_init()
242 {
243 vm_offset_t master_gdt_phys;
244 vm_offset_t master_gdt_alias_phys;
245 vm_offset_t master_idt_phys;
246 vm_offset_t master_idt_alias_phys;
247
248 assert(((vm_offset_t)master_gdt & PAGE_MASK) == 0);
249 assert(((vm_offset_t)master_idt64 & PAGE_MASK) == 0);
250
251 master_gdt_phys = (vm_offset_t) ID_MAP_VTOP(master_gdt);
252 master_idt_phys = (vm_offset_t) ID_MAP_VTOP(master_idt64);
253 master_gdt_alias_phys = (vm_offset_t) ID_MAP_VTOP(MASTER_GDT_ALIAS);
254 master_idt_alias_phys = (vm_offset_t) ID_MAP_VTOP(MASTER_IDT_ALIAS);
255
256 DBG("master_gdt_phys: %p\n", (void *) master_gdt_phys);
257 DBG("master_idt_phys: %p\n", (void *) master_idt_phys);
258 DBG("master_gdt_alias_phys: %p\n", (void *) master_gdt_alias_phys);
259 DBG("master_idt_alias_phys: %p\n", (void *) master_idt_alias_phys);
260
261 KPTphys[atop_kernel(master_gdt_alias_phys)] = master_gdt_phys |
262 INTEL_PTE_VALID | INTEL_PTE_NX | INTEL_PTE_WRITE;
263 KPTphys[atop_kernel(master_idt_alias_phys)] = master_idt_phys |
264 INTEL_PTE_VALID | INTEL_PTE_NX; /* read-only */
265 }
266
267 static void
268 Idle_PTs_init(void)
269 {
270 /* Allocate the "idle" kernel page tables: */
271 KPTphys = ALLOCPAGES(NKPT); /* level 1 */
272 IdlePTD = ALLOCPAGES(NPGPTD); /* level 2 */
273 IdlePDPT = ALLOCPAGES(1); /* level 3 */
274 IdlePML4 = ALLOCPAGES(1); /* level 4 */
275
276 // Fill the lowest level with everything up to physfree
277 fillkpt(KPTphys,
278 INTEL_PTE_WRITE, 0, 0, (int)(((uintptr_t)physfree) >> PAGE_SHIFT));
279
280 /* IdlePTD */
281 fillkpt(IdlePTD,
282 INTEL_PTE_WRITE, (uintptr_t)ID_MAP_VTOP(KPTphys), 0, NKPT);
283
284 // IdlePDPT entries
285 fillkpt(IdlePDPT,
286 INTEL_PTE_WRITE, (uintptr_t)ID_MAP_VTOP(IdlePTD), 0, NPGPTD);
287
288 // IdlePML4 single entry for kernel space.
289 fillkpt(IdlePML4 + KERNEL_PML4_INDEX,
290 INTEL_PTE_WRITE, (uintptr_t)ID_MAP_VTOP(IdlePDPT), 0, 1);
291
292 postcode(VSTART_PHYSMAP_INIT);
293
294 physmap_init();
295
296 postcode(VSTART_DESC_ALIAS_INIT);
297
298 descriptor_alias_init();
299
300 postcode(VSTART_SET_CR3);
301
302 // Switch to the page tables..
303 set_cr3_raw((uintptr_t)ID_MAP_VTOP(IdlePML4));
304
305 }
306
307
308 /*
309 * vstart() is called in the natural mode (64bit for K64, 32 for K32)
310 * on a set of bootstrap pagetables which use large, 2MB pages to map
311 * all of physical memory in both. See idle_pt.c for details.
312 *
313 * In K64 this identity mapping is mirrored the top and bottom 512GB
314 * slots of PML4.
315 *
316 * The bootstrap processor called with argument boot_args_start pointing to
317 * the boot-args block. The kernel's (4K page) page tables are allocated and
318 * initialized before switching to these.
319 *
320 * Non-bootstrap processors are called with argument boot_args_start NULL.
321 * These processors switch immediately to the existing kernel page tables.
322 */
323 void
324 vstart(vm_offset_t boot_args_start)
325 {
326 boolean_t is_boot_cpu = !(boot_args_start == 0);
327 int cpu;
328 uint32_t lphysfree;
329
330 postcode(VSTART_ENTRY);
331
332 if (is_boot_cpu) {
333 /*
334 * Get startup parameters.
335 */
336 kernelBootArgs = (boot_args *)boot_args_start;
337 lphysfree = kernelBootArgs->kaddr + kernelBootArgs->ksize;
338 physfree = (void *)(uintptr_t)((lphysfree + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1));
339
340 #if DEVELOPMENT || DEBUG
341 pal_serial_init();
342 #endif
343 DBG("revision 0x%x\n", kernelBootArgs->Revision);
344 DBG("version 0x%x\n", kernelBootArgs->Version);
345 DBG("command line %s\n", kernelBootArgs->CommandLine);
346 DBG("memory map 0x%x\n", kernelBootArgs->MemoryMap);
347 DBG("memory map sz 0x%x\n", kernelBootArgs->MemoryMapSize);
348 DBG("kaddr 0x%x\n", kernelBootArgs->kaddr);
349 DBG("ksize 0x%x\n", kernelBootArgs->ksize);
350 DBG("physfree %p\n", physfree);
351 DBG("bootargs: %p, &ksize: %p &kaddr: %p\n",
352 kernelBootArgs,
353 &kernelBootArgs->ksize,
354 &kernelBootArgs->kaddr);
355 DBG("SMBIOS mem sz 0x%llx\n", kernelBootArgs->PhysicalMemorySize);
356
357 /*
358 * Setup boot args given the physical start address.
359 * Note: PE_init_platform needs to be called before Idle_PTs_init
360 * because access to the DeviceTree is required to read the
361 * random seed before generating a random physical map slide.
362 */
363 kernelBootArgs = (boot_args *)
364 ml_static_ptovirt(boot_args_start);
365 DBG("i386_init(0x%lx) kernelBootArgs=%p\n",
366 (unsigned long)boot_args_start, kernelBootArgs);
367 PE_init_platform(FALSE, kernelBootArgs);
368 postcode(PE_INIT_PLATFORM_D);
369
370 Idle_PTs_init();
371 postcode(VSTART_IDLE_PTS_INIT);
372
373 first_avail = (vm_offset_t)ID_MAP_VTOP(physfree);
374
375 cpu = 0;
376 cpu_data_alloc(TRUE);
377 } else {
378 /* Switch to kernel's page tables (from the Boot PTs) */
379 set_cr3_raw((uintptr_t)ID_MAP_VTOP(IdlePML4));
380 /* Find our logical cpu number */
381 cpu = lapic_to_cpu[(LAPIC_READ(ID)>>LAPIC_ID_SHIFT) & LAPIC_ID_MASK];
382 DBG("CPU: %d, GSBASE initial value: 0x%llx\n", cpu, rdmsr64(MSR_IA32_GS_BASE));
383 }
384
385 postcode(VSTART_CPU_DESC_INIT);
386 if(is_boot_cpu)
387 cpu_desc_init64(cpu_datap(cpu));
388 cpu_desc_load64(cpu_datap(cpu));
389 postcode(VSTART_CPU_MODE_INIT);
390 if (is_boot_cpu)
391 cpu_mode_init(current_cpu_datap()); /* cpu_mode_init() will be
392 * invoked on the APs
393 * via i386_init_slave()
394 */
395 postcode(VSTART_EXIT);
396 x86_init_wrapper(is_boot_cpu ? (uintptr_t) i386_init
397 : (uintptr_t) i386_init_slave,
398 cpu_datap(cpu)->cpu_int_stack_top);
399 }
400
401 void
402 pstate_trace(void)
403 {
404 }
405
406 /*
407 * Cpu initialization. Running virtual, but without MACH VM
408 * set up.
409 */
410 void
411 i386_init(void)
412 {
413 unsigned int maxmem;
414 uint64_t maxmemtouse;
415 unsigned int cpus = 0;
416 boolean_t fidn;
417 boolean_t IA32e = TRUE;
418
419 postcode(I386_INIT_ENTRY);
420
421 pal_i386_init();
422 tsc_init();
423 rtclock_early_init(); /* mach_absolute_time() now functionsl */
424
425 kernel_debug_string_simple("i386_init");
426 pstate_trace();
427
428 #if CONFIG_MCA
429 /* Initialize machine-check handling */
430 mca_cpu_init();
431 #endif
432
433 master_cpu = 0;
434 cpu_init();
435
436 postcode(CPU_INIT_D);
437
438 printf_init(); /* Init this in case we need debugger */
439 panic_init(); /* Init this in case we need debugger */
440
441 /* setup debugging output if one has been chosen */
442 kernel_debug_string_simple("PE_init_kprintf");
443 PE_init_kprintf(FALSE);
444
445 kernel_debug_string_simple("kernel_early_bootstrap");
446 kernel_early_bootstrap();
447
448 if (!PE_parse_boot_argn("diag", &dgWork.dgFlags, sizeof (dgWork.dgFlags)))
449 dgWork.dgFlags = 0;
450
451 serialmode = 0;
452 if(PE_parse_boot_argn("serial", &serialmode, sizeof (serialmode))) {
453 /* We want a serial keyboard and/or console */
454 kprintf("Serial mode specified: %08X\n", serialmode);
455 }
456 if(serialmode & 1) {
457 (void)switch_to_serial_console();
458 disableConsoleOutput = FALSE; /* Allow printfs to happen */
459 }
460
461 /* setup console output */
462 kernel_debug_string_simple("PE_init_printf");
463 PE_init_printf(FALSE);
464
465 kprintf("version_variant = %s\n", version_variant);
466 kprintf("version = %s\n", version);
467
468 if (!PE_parse_boot_argn("maxmem", &maxmem, sizeof (maxmem)))
469 maxmemtouse = 0;
470 else
471 maxmemtouse = ((uint64_t)maxmem) * MB;
472
473 if (PE_parse_boot_argn("cpus", &cpus, sizeof (cpus))) {
474 if ((0 < cpus) && (cpus < max_ncpus))
475 max_ncpus = cpus;
476 }
477
478 /*
479 * debug support for > 4G systems
480 */
481 PE_parse_boot_argn("himemory_mode", &vm_himemory_mode, sizeof (vm_himemory_mode));
482 if (vm_himemory_mode != 0)
483 kprintf("himemory_mode: %d\n", vm_himemory_mode);
484
485 if (!PE_parse_boot_argn("immediate_NMI", &fidn, sizeof (fidn)))
486 force_immediate_debugger_NMI = FALSE;
487 else
488 force_immediate_debugger_NMI = fidn;
489
490 #if DEBUG
491 nanoseconds_to_absolutetime(URGENCY_NOTIFICATION_ASSERT_NS, &urgency_notification_assert_abstime_threshold);
492 #endif
493 PE_parse_boot_argn("urgency_notification_abstime",
494 &urgency_notification_assert_abstime_threshold,
495 sizeof(urgency_notification_assert_abstime_threshold));
496
497 if (!(cpuid_extfeatures() & CPUID_EXTFEATURE_XD))
498 nx_enabled = 0;
499
500 /*
501 * VM initialization, after this we're using page tables...
502 * Thn maximum number of cpus must be set beforehand.
503 */
504 kernel_debug_string_simple("i386_vm_init");
505 i386_vm_init(maxmemtouse, IA32e, kernelBootArgs);
506
507 /* create the console for verbose or pretty mode */
508 /* Note: doing this prior to tsc_init() allows for graceful panic! */
509 PE_init_platform(TRUE, kernelBootArgs);
510 PE_create_console();
511
512 kernel_debug_string_simple("power_management_init");
513 power_management_init();
514 processor_bootstrap();
515 thread_bootstrap();
516
517 pstate_trace();
518 kernel_debug_string_simple("machine_startup");
519 machine_startup();
520 pstate_trace();
521 }
522
523 static void
524 do_init_slave(boolean_t fast_restart)
525 {
526 void *init_param = FULL_SLAVE_INIT;
527
528 postcode(I386_INIT_SLAVE);
529
530 if (!fast_restart) {
531 /* Ensure that caching and write-through are enabled */
532 set_cr0(get_cr0() & ~(CR0_NW|CR0_CD));
533
534 DBG("i386_init_slave() CPU%d: phys (%d) active.\n",
535 get_cpu_number(), get_cpu_phys_number());
536
537 assert(!ml_get_interrupts_enabled());
538
539 cpu_mode_init(current_cpu_datap());
540 pmap_cpu_init();
541
542 #if CONFIG_MCA
543 mca_cpu_init();
544 #endif
545
546 LAPIC_INIT();
547 lapic_configure();
548 LAPIC_DUMP();
549 LAPIC_CPU_MAP_DUMP();
550
551 init_fpu();
552
553 #if CONFIG_MTRR
554 mtrr_update_cpu();
555 #endif
556 /* update CPU microcode */
557 ucode_update_wake();
558 } else
559 init_param = FAST_SLAVE_INIT;
560
561 #if CONFIG_VMX
562 /* resume VT operation */
563 vmx_resume(FALSE);
564 #endif
565
566 #if CONFIG_MTRR
567 if (!fast_restart)
568 pat_init();
569 #endif
570
571 cpu_thread_init(); /* not strictly necessary */
572
573 cpu_init(); /* Sets cpu_running which starter cpu waits for */
574 slave_main(init_param);
575
576 panic("do_init_slave() returned from slave_main()");
577 }
578
579 /*
580 * i386_init_slave() is called from pstart.
581 * We're in the cpu's interrupt stack with interrupts disabled.
582 * At this point we are in legacy mode. We need to switch on IA32e
583 * if the mode is set to 64-bits.
584 */
585 void
586 i386_init_slave(void)
587 {
588 do_init_slave(FALSE);
589 }
590
591 /*
592 * i386_init_slave_fast() is called from pmCPUHalt.
593 * We're running on the idle thread and need to fix up
594 * some accounting and get it so that the scheduler sees this
595 * CPU again.
596 */
597 void
598 i386_init_slave_fast(void)
599 {
600 do_init_slave(TRUE);
601 }
602
603