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