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