]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
47290e3d8ac421c08349b48cc86e8b7036f573f4
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2009 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 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 */
62 /*
63 */
64
65 /*
66 * Mach kernel startup.
67 */
68
69 #include <debug.h>
70 #include <xpr_debug.h>
71 #include <mach_kdp.h>
72 #include <norma_vm.h>
73
74 #include <mach/boolean.h>
75 #include <mach/machine.h>
76 #include <mach/thread_act.h>
77 #include <mach/task_special_ports.h>
78 #include <mach/vm_param.h>
79 #include <ipc/ipc_init.h>
80 #include <kern/assert.h>
81 #include <kern/mach_param.h>
82 #include <kern/misc_protos.h>
83 #include <kern/clock.h>
84 #include <kern/cpu_number.h>
85 #include <kern/ledger.h>
86 #include <kern/machine.h>
87 #include <kern/processor.h>
88 #include <kern/sched_prim.h>
89 #include <kern/startup.h>
90 #include <kern/task.h>
91 #include <kern/thread.h>
92 #include <kern/timer.h>
93 #include <kern/wait_queue.h>
94 #include <kern/xpr.h>
95 #include <kern/zalloc.h>
96 #include <kern/locks.h>
97 #include <console/serial_protos.h>
98 #include <vm/vm_kern.h>
99 #include <vm/vm_init.h>
100 #include <vm/vm_map.h>
101 #include <vm/vm_object.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_pageout.h>
104 #include <vm/vm_shared_region.h>
105 #include <machine/pmap.h>
106 #include <machine/commpage.h>
107 #include <libkern/version.h>
108 #include <sys/kdebug.h>
109
110 #if MACH_KDP
111 #include <kdp/kdp.h>
112 #endif
113
114 #if CONFIG_MACF
115 #include <security/mac_mach_internal.h>
116 #endif
117
118 #if CONFIG_COUNTERS
119 #include <pmc/pmc.h>
120 #endif
121
122 static void kernel_bootstrap_thread(void);
123
124 static void load_context(
125 thread_t thread);
126 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
127 extern void cpu_userwindow_init(int);
128 extern void cpu_physwindow_init(int);
129 #endif
130
131 // libkern/OSKextLib.cpp
132 extern void OSKextRemoveKextBootstrap(void);
133
134 void scale_setup(void);
135 extern void bsd_scale_setup(int);
136 extern unsigned int semaphore_max;
137
138 /*
139 * Running in virtual memory, on the interrupt stack.
140 */
141
142 extern int serverperfmode;
143
144 /* size of kernel trace buffer, disabled by default */
145 unsigned int new_nkdbufs = 0;
146
147 /* mach leak logging */
148 int log_leaks = 0;
149 int turn_on_log_leaks = 0;
150
151
152 void
153 kernel_early_bootstrap(void)
154 {
155
156 lck_mod_init();
157
158 /*
159 * Initialize the timer callout world
160 */
161 timer_call_initialize();
162 }
163
164
165 void
166 kernel_bootstrap(void)
167 {
168 kern_return_t result;
169 thread_t thread;
170 char namep[16];
171
172 printf("%s\n", version); /* log kernel version */
173
174 #define kernel_bootstrap_kprintf(x...) /* kprintf("kernel_bootstrap: " x) */
175
176 if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */
177 turn_on_log_leaks = 1;
178
179 PE_parse_boot_argn("trace", &new_nkdbufs, sizeof (new_nkdbufs));
180
181 /* i386_vm_init already checks for this ; do it aagin anyway */
182 if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof (serverperfmode))) {
183 serverperfmode = 1;
184 }
185 scale_setup();
186
187 kernel_bootstrap_kprintf("calling vm_mem_bootstrap\n");
188 vm_mem_bootstrap();
189
190 kernel_bootstrap_kprintf("calling vm_mem_init\n");
191 vm_mem_init();
192
193 machine_info.memory_size = (uint32_t)mem_size;
194 machine_info.max_mem = max_mem;
195 machine_info.major_version = version_major;
196 machine_info.minor_version = version_minor;
197
198 kernel_bootstrap_kprintf("calling sched_init\n");
199 sched_init();
200
201 kernel_bootstrap_kprintf("calling wait_queue_bootstrap\n");
202 wait_queue_bootstrap();
203
204 kernel_bootstrap_kprintf("calling ipc_bootstrap\n");
205 ipc_bootstrap();
206
207 #if CONFIG_MACF
208 mac_policy_init();
209 #endif
210 kernel_bootstrap_kprintf("calling ipc_init\n");
211 ipc_init();
212
213 /*
214 * As soon as the virtual memory system is up, we record
215 * that this CPU is using the kernel pmap.
216 */
217 kernel_bootstrap_kprintf("calling PMAP_ACTIVATE_KERNEL\n");
218 PMAP_ACTIVATE_KERNEL(master_cpu);
219
220 kernel_bootstrap_kprintf("calling mapping_free_prime\n");
221 mapping_free_prime(); /* Load up with temporary mapping blocks */
222
223 kernel_bootstrap_kprintf("calling machine_init\n");
224 machine_init();
225
226 kernel_bootstrap_kprintf("calling clock_init\n");
227 clock_init();
228
229
230 /*
231 * Initialize the IPC, task, and thread subsystems.
232 */
233 kernel_bootstrap_kprintf("calling ledger_init\n");
234 ledger_init();
235
236 kernel_bootstrap_kprintf("calling task_init\n");
237 task_init();
238
239 kernel_bootstrap_kprintf("calling thread_init\n");
240 thread_init();
241
242 /*
243 * Create a kernel thread to execute the kernel bootstrap.
244 */
245 kernel_bootstrap_kprintf("calling kernel_thread_create\n");
246 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
247
248 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
249
250 thread->state = TH_RUN;
251 thread_deallocate(thread);
252
253 /* transfer statistics from init thread to kernel */
254 thread_t init_thread = current_thread();
255 kernel_task->tkm_private.alloc = init_thread->tkm_private.alloc;
256 kernel_task->tkm_private.free = init_thread->tkm_private.free;
257 kernel_task->tkm_shared.alloc = init_thread->tkm_shared.alloc;
258 kernel_task->tkm_shared.free = init_thread->tkm_shared.free;
259
260 kernel_bootstrap_kprintf("calling load_context - done\n");
261 load_context(thread);
262 /*NOTREACHED*/
263 }
264
265 int kth_started = 0;
266
267 /*
268 * Now running in a thread. Kick off other services,
269 * invoke user bootstrap, enter pageout loop.
270 */
271 static void
272 kernel_bootstrap_thread(void)
273 {
274 processor_t processor = current_processor();
275
276 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
277 kernel_bootstrap_thread_kprintf("calling idle_thread_create\n");
278 /*
279 * Create the idle processor thread.
280 */
281 idle_thread_create(processor);
282
283 /*
284 * N.B. Do not stick anything else
285 * before this point.
286 *
287 * Start up the scheduler services.
288 */
289 kernel_bootstrap_thread_kprintf("calling sched_startup\n");
290 sched_startup();
291
292 /*
293 * Thread lifecycle maintenance (teardown, stack allocation)
294 */
295 kernel_bootstrap_thread_kprintf("calling thread_daemon_init\n");
296 thread_daemon_init();
297
298 /*
299 * Thread callout service.
300 */
301 kernel_bootstrap_thread_kprintf("calling thread_call_initialize\n");
302 thread_call_initialize();
303
304 /*
305 * Remain on current processor as
306 * additional processors come online.
307 */
308 kernel_bootstrap_thread_kprintf("calling thread_bind\n");
309 thread_bind(processor);
310
311 /*
312 * Kick off memory mapping adjustments.
313 */
314 kernel_bootstrap_thread_kprintf("calling mapping_adjust\n");
315 mapping_adjust();
316
317 /*
318 * Create the clock service.
319 */
320 kernel_bootstrap_thread_kprintf("calling clock_service_create\n");
321 clock_service_create();
322
323 /*
324 * Create the device service.
325 */
326 device_service_create();
327
328 kth_started = 1;
329
330 #if MACH_KDP
331 kernel_bootstrap_kprintf("calling kdp_init\n");
332 kdp_init();
333 #endif
334
335 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
336 /*
337 * Create and initialize the physical copy window for processor 0
338 * This is required before starting kicking off IOKit.
339 */
340 cpu_physwindow_init(0);
341 #endif
342
343 #if CONFIG_COUNTERS
344 pmc_bootstrap();
345 #endif
346
347 #if (defined(__i386__) || defined(__x86_64__))
348 if (turn_on_log_leaks && !new_nkdbufs)
349 new_nkdbufs = 200000;
350 start_kern_tracing(new_nkdbufs);
351 if (turn_on_log_leaks)
352 log_leaks = 1;
353 #endif
354
355 #ifdef IOKIT
356 PE_init_iokit();
357 #endif
358
359 (void) spllo(); /* Allow interruptions */
360
361 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
362 /*
363 * Create and initialize the copy window for processor 0
364 * This also allocates window space for all other processors.
365 * However, this is dependent on the number of processors - so this call
366 * must be after IOKit has been started because IOKit performs processor
367 * discovery.
368 */
369 cpu_userwindow_init(0);
370 #endif
371
372 #if (!defined(__i386__) && !defined(__x86_64__))
373 if (turn_on_log_leaks && !new_nkdbufs)
374 new_nkdbufs = 200000;
375 start_kern_tracing(new_nkdbufs);
376 if (turn_on_log_leaks)
377 log_leaks = 1;
378 #endif
379
380 /*
381 * Initialize the shared region module.
382 */
383 vm_shared_region_init();
384 vm_commpage_init();
385
386 #if CONFIG_MACF
387 mac_policy_initmach();
388 #endif
389
390 /*
391 * Start the user bootstrap.
392 */
393 #ifdef MACH_BSD
394 bsd_init();
395 #endif
396
397 /*
398 * Get rid of segments used to bootstrap kext loading. This removes
399 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
400 */
401 OSKextRemoveKextBootstrap();
402
403 serial_keyboard_init(); /* Start serial keyboard if wanted */
404
405 vm_page_init_local_q();
406
407 thread_bind(PROCESSOR_NULL);
408
409 /*
410 * Become the pageout daemon.
411 */
412 vm_pageout();
413 /*NOTREACHED*/
414 }
415
416 /*
417 * slave_main:
418 *
419 * Load the first thread to start a processor.
420 */
421 void
422 slave_main(void *machine_param)
423 {
424 processor_t processor = current_processor();
425 thread_t thread;
426
427 /*
428 * Use the idle processor thread if there
429 * is no dedicated start up thread.
430 */
431 if (processor->next_thread == THREAD_NULL) {
432 thread = processor->idle_thread;
433 thread->continuation = (thread_continue_t)processor_start_thread;
434 thread->parameter = machine_param;
435 }
436 else {
437 thread = processor->next_thread;
438 processor->next_thread = THREAD_NULL;
439 }
440
441 load_context(thread);
442 /*NOTREACHED*/
443 }
444
445 /*
446 * processor_start_thread:
447 *
448 * First thread to execute on a started processor.
449 *
450 * Called at splsched.
451 */
452 void
453 processor_start_thread(void *machine_param)
454 {
455 processor_t processor = current_processor();
456 thread_t self = current_thread();
457
458 slave_machine_init(machine_param);
459
460 /*
461 * If running the idle processor thread,
462 * reenter the idle loop, else terminate.
463 */
464 if (self == processor->idle_thread)
465 thread_block((thread_continue_t)idle_thread);
466
467 thread_terminate(self);
468 /*NOTREACHED*/
469 }
470
471 /*
472 * load_context:
473 *
474 * Start the first thread on a processor.
475 */
476 static void
477 load_context(
478 thread_t thread)
479 {
480 processor_t processor = current_processor();
481
482
483 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
484
485 load_context_kprintf("calling machine_set_current_thread\n");
486 machine_set_current_thread(thread);
487
488 load_context_kprintf("calling processor_up\n");
489 processor_up(processor);
490
491 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
492
493 /*
494 * Acquire a stack if none attached. The panic
495 * should never occur since the thread is expected
496 * to have reserved stack.
497 */
498 load_context_kprintf("stack %x, stackptr %x\n",
499 thread->kernel_stack, thread->machine.kstackptr);
500 if (!thread->kernel_stack) {
501 load_context_kprintf("calling stack_alloc_try\n");
502 if (!stack_alloc_try(thread))
503 panic("load_context");
504 }
505
506 /*
507 * The idle processor threads are not counted as
508 * running for load calculations.
509 */
510 if (!(thread->state & TH_IDLE))
511 sched_run_incr();
512
513 processor->active_thread = thread;
514 processor->current_pri = thread->sched_pri;
515 processor->current_thmode = thread->sched_mode;
516 processor->deadline = UINT64_MAX;
517 thread->last_processor = processor;
518
519 processor->last_dispatch = mach_absolute_time();
520 timer_start(&thread->system_timer, processor->last_dispatch);
521 PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
522
523 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
524 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
525
526 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
527
528 load_context_kprintf("calling machine_load_context\n");
529 machine_load_context(thread);
530 /*NOTREACHED*/
531 }
532
533 void
534 scale_setup()
535 {
536 int scale = 0;
537 #if defined(__LP64__)
538 typeof(task_max) task_max_base = task_max;
539
540 /* Raise limits for servers with >= 16G */
541 if ((serverperfmode != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
542 scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 *1024ULL));
543 /* limit to 128 G */
544 if (scale > 16)
545 scale = 16;
546 task_max_base = 2500;
547 } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL))
548 scale = 2;
549
550 task_max = MAX(task_max, task_max_base * scale);
551
552 if (scale != 0) {
553 task_threadmax = task_max;
554 thread_max = task_max * 5;
555 }
556
557 #endif
558
559 bsd_scale_setup(scale);
560
561 ipc_space_max = SPACE_MAX;
562 ipc_tree_entry_max = ITE_MAX;
563 ipc_port_max = PORT_MAX;
564 ipc_pset_max = SET_MAX;
565 semaphore_max = SEMAPHORE_MAX;
566 }
567