]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/startup.c
xnu-4570.61.1.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>
fe8ab488 83#include <kern/coalition.h>
1c79356b 84#include <kern/cpu_number.h>
91447636 85#include <kern/ledger.h>
1c79356b
A
86#include <kern/machine.h>
87#include <kern/processor.h>
88#include <kern/sched_prim.h>
3e170ce0 89#if CONFIG_SCHED_SFI
fe8ab488 90#include <kern/sfi.h>
3e170ce0 91#endif
1c79356b
A
92#include <kern/startup.h>
93#include <kern/task.h>
94#include <kern/thread.h>
95#include <kern/timer.h>
39236c6e
A
96#if CONFIG_TELEMETRY
97#include <kern/telemetry.h>
98#endif
1c79356b
A
99#include <kern/xpr.h>
100#include <kern/zalloc.h>
91447636 101#include <kern/locks.h>
3e170ce0
A
102#include <kern/debug.h>
103#include <corpses/task_corpse.h>
fe8ab488 104#include <prng/random.h>
0c530ab8 105#include <console/serial_protos.h>
1c79356b
A
106#include <vm/vm_kern.h>
107#include <vm/vm_init.h>
108#include <vm/vm_map.h>
109#include <vm/vm_object.h>
110#include <vm/vm_page.h>
111#include <vm/vm_pageout.h>
2d21ac55 112#include <vm/vm_shared_region.h>
1c79356b 113#include <machine/pmap.h>
43866e37 114#include <machine/commpage.h>
91447636 115#include <libkern/version.h>
39236c6e 116#include <sys/codesign.h>
6d2010ae 117#include <sys/kdebug.h>
fe8ab488 118#include <sys/random.h>
39037602 119#include <sys/ktrace.h>
fe8ab488 120
39037602 121#include <kern/ltable.h>
3e170ce0 122#include <kern/waitq.h>
39037602
A
123#include <ipc/ipc_voucher.h>
124#include <voucher/ipc_pthread_priority_internal.h>
cc8bc92a 125#include <mach/host_info.h>
3e170ce0
A
126
127
fe8ab488
A
128#if CONFIG_ATM
129#include <atm/atm_internal.h>
130#endif
131
132#if CONFIG_CSR
133#include <sys/csr.h>
134#endif
135
fe8ab488 136#include <bank/bank_internal.h>
fe8ab488
A
137
138#if ALTERNATE_DEBUGGER
139#include <arm64/alternate_debugger.h>
140#endif
1c79356b 141
593a1d5f
A
142#if MACH_KDP
143#include <kdp/kdp.h>
144#endif
145
2d21ac55
A
146#if CONFIG_MACF
147#include <security/mac_mach_internal.h>
5ba3f43e
A
148#if CONFIG_VNGUARD
149extern void vnguard_policy_init(void);
150#endif
2d21ac55
A
151#endif
152
39236c6e
A
153#if KPC
154#include <kern/kpc.h>
155#endif
156
fe8ab488
A
157#if HYPERVISOR
158#include <kern/hv_support.h>
159#endif
160
5ba3f43e
A
161#include <san/kasan.h>
162
163#if defined(__arm__) || defined(__arm64__)
164#include <arm/misc_protos.h> // for arm_vm_prot_finalize
165#endif
39236c6e 166
bd504ef0 167#include <i386/pmCPU.h>
91447636 168static void kernel_bootstrap_thread(void);
1c79356b 169
91447636
A
170static void load_context(
171 thread_t thread);
b0d623f7 172#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
2d21ac55
A
173extern void cpu_userwindow_init(int);
174extern void cpu_physwindow_init(int);
0c530ab8
A
175#endif
176
fe8ab488
A
177#if CONFIG_ECC_LOGGING
178#include <kern/ecc.h>
179#endif
180
181#if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
182#include <i386/vmx/vmx_cpu.h>
183#endif
184
b0d623f7
A
185// libkern/OSKextLib.cpp
186extern void OSKextRemoveKextBootstrap(void);
187
6d2010ae
A
188void scale_setup(void);
189extern void bsd_scale_setup(int);
b0d623f7 190extern unsigned int semaphore_max;
39037602
A
191extern void stackshot_init(void);
192extern void ktrace_init(void);
193extern void oslog_init(void);
b0d623f7 194
1c79356b
A
195/*
196 * Running in virtual memory, on the interrupt stack.
1c79356b 197 */
0c530ab8 198
6d2010ae
A
199extern int serverperfmode;
200
201/* size of kernel trace buffer, disabled by default */
202unsigned int new_nkdbufs = 0;
bd504ef0 203unsigned int wake_nkdbufs = 0;
fe8ab488 204unsigned int write_trace_on_panic = 0;
39037602 205static char trace_typefilter[64] = { 0 };
a39ff7e2 206unsigned int trace_wrap = 0;
39037602 207boolean_t trace_serial = FALSE;
5ba3f43e 208boolean_t early_boot_complete = FALSE;
6d2010ae
A
209
210/* mach leak logging */
211int log_leaks = 0;
6d2010ae 212
fe8ab488
A
213static inline void
214kernel_bootstrap_log(const char *message)
215{
216// kprintf("kernel_bootstrap: %s\n", message);
39037602 217 kernel_debug_string_early(message);
fe8ab488
A
218}
219
220static inline void
221kernel_bootstrap_thread_log(const char *message)
222{
223// kprintf("kernel_bootstrap_thread: %s\n", message);
39037602 224 kernel_debug_string_early(message);
fe8ab488 225}
6d2010ae
A
226
227void
228kernel_early_bootstrap(void)
229{
15129b1c
A
230 /* serverperfmode is needed by timer setup */
231 if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof (serverperfmode))) {
232 serverperfmode = 1;
233 }
6d2010ae
A
234
235 lck_mod_init();
236
237 /*
238 * Initialize the timer callout world
239 */
39236c6e 240 timer_call_init();
fe8ab488 241
3e170ce0 242#if CONFIG_SCHED_SFI
fe8ab488
A
243 /*
244 * Configure SFI classes
245 */
246 sfi_early_init();
3e170ce0 247#endif
6d2010ae
A
248}
249
3e170ce0
A
250extern boolean_t IORamDiskBSDRoot(void);
251extern kern_return_t cpm_preallocate_early(void);
b0d623f7 252
1c79356b 253void
91447636 254kernel_bootstrap(void)
1c79356b 255{
91447636 256 kern_return_t result;
6d2010ae
A
257 thread_t thread;
258 char namep[16];
1c79356b 259
2d21ac55
A
260 printf("%s\n", version); /* log kernel version */
261
6d2010ae 262 if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */
39037602 263 log_leaks = 1;
b0d623f7 264
6d2010ae 265 PE_parse_boot_argn("trace", &new_nkdbufs, sizeof (new_nkdbufs));
bd504ef0 266 PE_parse_boot_argn("trace_wake", &wake_nkdbufs, sizeof (wake_nkdbufs));
fe8ab488 267 PE_parse_boot_argn("trace_panic", &write_trace_on_panic, sizeof(write_trace_on_panic));
39037602 268 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter, sizeof(trace_typefilter));
a39ff7e2 269 PE_parse_boot_argn("trace_wrap", &trace_wrap, sizeof(trace_wrap));
bd504ef0 270
6d2010ae 271 scale_setup();
2d21ac55 272
fe8ab488 273 kernel_bootstrap_log("vm_mem_bootstrap");
b0d623f7
A
274 vm_mem_bootstrap();
275
fe8ab488 276 kernel_bootstrap_log("cs_init");
39236c6e
A
277 cs_init();
278
fe8ab488 279 kernel_bootstrap_log("vm_mem_init");
b0d623f7
A
280 vm_mem_init();
281
282 machine_info.memory_size = (uint32_t)mem_size;
283 machine_info.max_mem = max_mem;
284 machine_info.major_version = version_major;
285 machine_info.minor_version = version_minor;
286
39037602 287 oslog_init();
3e170ce0 288
5ba3f43e
A
289#if KASAN
290 kernel_bootstrap_log("kasan_late_init");
291 kasan_late_init();
292#endif
293
39236c6e 294#if CONFIG_TELEMETRY
fe8ab488 295 kernel_bootstrap_log("telemetry_init");
39236c6e
A
296 telemetry_init();
297#endif
298
3e170ce0
A
299#if CONFIG_CSR
300 kernel_bootstrap_log("csr_init");
301 csr_init();
302#endif
303
4bd07ac2
A
304 if (PE_i_can_has_debugger(NULL) &&
305 PE_parse_boot_argn("-show_pointers", &namep, sizeof (namep))) {
306 doprnt_hide_pointers = FALSE;
307 }
308
490019cf
A
309 kernel_bootstrap_log("console_init");
310 console_init();
311
39037602
A
312 kernel_bootstrap_log("stackshot_init");
313 stackshot_init();
39236c6e 314
fe8ab488 315 kernel_bootstrap_log("sched_init");
1c79356b 316 sched_init();
2d21ac55 317
39037602
A
318 kernel_bootstrap_log("ltable_bootstrap");
319 ltable_bootstrap();
320
3e170ce0
A
321 kernel_bootstrap_log("waitq_bootstrap");
322 waitq_bootstrap();
2d21ac55 323
fe8ab488 324 kernel_bootstrap_log("ipc_bootstrap");
1c79356b 325 ipc_bootstrap();
2d21ac55 326
2d21ac55 327#if CONFIG_MACF
3e170ce0 328 kernel_bootstrap_log("mac_policy_init");
2d21ac55
A
329 mac_policy_init();
330#endif
3e170ce0 331
fe8ab488 332 kernel_bootstrap_log("ipc_init");
1c79356b 333 ipc_init();
1c79356b
A
334
335 /*
336 * As soon as the virtual memory system is up, we record
337 * that this CPU is using the kernel pmap.
338 */
fe8ab488 339 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
1c79356b
A
340 PMAP_ACTIVATE_KERNEL(master_cpu);
341
fe8ab488 342 kernel_bootstrap_log("mapping_free_prime");
1c79356b 343 mapping_free_prime(); /* Load up with temporary mapping blocks */
1c79356b 344
fe8ab488 345 kernel_bootstrap_log("machine_init");
1c79356b 346 machine_init();
2d21ac55 347
fe8ab488 348 kernel_bootstrap_log("clock_init");
1c79356b
A
349 clock_init();
350
316670eb 351 ledger_init();
1c79356b
A
352
353 /*
354 * Initialize the IPC, task, and thread subsystems.
355 */
5ba3f43e 356
fe8ab488 357#if CONFIG_COALITIONS
3e170ce0
A
358 kernel_bootstrap_log("coalitions_init");
359 coalitions_init();
fe8ab488
A
360#endif
361
362 kernel_bootstrap_log("task_init");
1c79356b 363 task_init();
2d21ac55 364
fe8ab488 365 kernel_bootstrap_log("thread_init");
1c79356b 366 thread_init();
fe8ab488
A
367
368#if CONFIG_ATM
369 /* Initialize the Activity Trace Resource Manager. */
370 kernel_bootstrap_log("atm_init");
371 atm_init();
372#endif
39037602
A
373 kernel_bootstrap_log("mach_init_activity_id");
374 mach_init_activity_id();
fe8ab488 375
fe8ab488
A
376 /* Initialize the BANK Manager. */
377 kernel_bootstrap_log("bank_init");
378 bank_init();
39037602
A
379
380 kernel_bootstrap_log("ipc_pthread_priority_init");
381 ipc_pthread_priority_init();
382
3e170ce0
A
383 /* initialize the corpse config based on boot-args */
384 corpses_init();
385
cc8bc92a
A
386 /* initialize host_statistics */
387 host_statistics_init();
388
1c79356b 389 /*
91447636 390 * Create a kernel thread to execute the kernel bootstrap.
1c79356b 391 */
fe8ab488 392 kernel_bootstrap_log("kernel_thread_create");
91447636 393 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
0c530ab8
A
394
395 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
9bccf70c 396
91447636 397 thread->state = TH_RUN;
3e170ce0 398 thread->last_made_runnable_time = mach_absolute_time();
91447636 399 thread_deallocate(thread);
55e303ae 400
fe8ab488 401 kernel_bootstrap_log("load_context - done");
91447636 402 load_context(thread);
1c79356b 403 /*NOTREACHED*/
1c79356b
A
404}
405
2d21ac55
A
406int kth_started = 0;
407
316670eb 408vm_offset_t vm_kernel_addrperm;
39236c6e 409vm_offset_t buf_kernel_addrperm;
3e170ce0 410vm_offset_t vm_kernel_addrperm_ext;
5ba3f43e
A
411uint64_t vm_kernel_addrhash_salt;
412uint64_t vm_kernel_addrhash_salt_ext;
316670eb 413
1c79356b 414/*
91447636
A
415 * Now running in a thread. Kick off other services,
416 * invoke user bootstrap, enter pageout loop.
1c79356b 417 */
91447636
A
418static void
419kernel_bootstrap_thread(void)
1c79356b 420{
91447636 421 processor_t processor = current_processor();
1c79356b 422
2d21ac55 423#define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
fe8ab488 424 kernel_bootstrap_thread_log("idle_thread_create");
1c79356b 425 /*
91447636 426 * Create the idle processor thread.
1c79356b 427 */
91447636 428 idle_thread_create(processor);
0b4e3aa0
A
429
430 /*
91447636
A
431 * N.B. Do not stick anything else
432 * before this point.
433 *
434 * Start up the scheduler services.
0b4e3aa0 435 */
fe8ab488 436 kernel_bootstrap_thread_log("sched_startup");
91447636 437 sched_startup();
0b4e3aa0 438
6d2010ae
A
439 /*
440 * Thread lifecycle maintenance (teardown, stack allocation)
441 */
fe8ab488 442 kernel_bootstrap_thread_log("thread_daemon_init");
6d2010ae 443 thread_daemon_init();
39236c6e
A
444
445 /* Create kernel map entry reserve */
446 vm_kernel_reserved_entry_init();
447
6d2010ae
A
448 /*
449 * Thread callout service.
450 */
fe8ab488 451 kernel_bootstrap_thread_log("thread_call_initialize");
6d2010ae 452 thread_call_initialize();
fe8ab488 453
1c79356b 454 /*
91447636
A
455 * Remain on current processor as
456 * additional processors come online.
1c79356b 457 */
fe8ab488 458 kernel_bootstrap_thread_log("thread_bind");
2d21ac55 459 thread_bind(processor);
1c79356b 460
5ba3f43e
A
461#if __arm64__
462 if (IORamDiskBSDRoot()) {
463 cpm_preallocate_early();
464 }
465#endif /* __arm64__ */
39037602 466
fe8ab488
A
467 /*
468 * Initialize ipc thread call support.
469 */
470 kernel_bootstrap_thread_log("ipc_thread_call_init");
471 ipc_thread_call_init();
472
1c79356b 473 /*
91447636 474 * Kick off memory mapping adjustments.
1c79356b 475 */
fe8ab488 476 kernel_bootstrap_thread_log("mapping_adjust");
1c79356b 477 mapping_adjust();
1c79356b 478
1c79356b
A
479 /*
480 * Create the clock service.
481 */
fe8ab488 482 kernel_bootstrap_thread_log("clock_service_create");
1c79356b
A
483 clock_service_create();
484
485 /*
486 * Create the device service.
487 */
488 device_service_create();
489
2d21ac55 490 kth_started = 1;
39037602 491
b0d623f7 492#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
2d21ac55
A
493 /*
494 * Create and initialize the physical copy window for processor 0
495 * This is required before starting kicking off IOKit.
496 */
497 cpu_physwindow_init(0);
498#endif
1c79356b 499
39236c6e 500
7ddcb079 501
fe8ab488
A
502#if MACH_KDP
503 kernel_bootstrap_log("kdp_init");
7ddcb079
A
504 kdp_init();
505#endif
506
39236c6e
A
507#if ALTERNATE_DEBUGGER
508 alternate_debugger_init();
509#endif
510
39236c6e
A
511#if KPC
512 kpc_init();
513#endif
514
fe8ab488
A
515#if CONFIG_ECC_LOGGING
516 ecc_log_init();
517#endif
518
fe8ab488
A
519#if HYPERVISOR
520 hv_support_init();
521#endif
522
39236c6e 523#if CONFIG_TELEMETRY
fe8ab488 524 kernel_bootstrap_log("bootprofile_init");
39236c6e
A
525 bootprofile_init();
526#endif
527
fe8ab488
A
528#if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
529 vmx_init();
530#endif
531
39037602
A
532 kernel_bootstrap_thread_log("ktrace_init");
533 ktrace_init();
39236c6e 534
a39ff7e2 535 kdebug_init(new_nkdbufs, trace_typefilter, trace_wrap);
6d2010ae 536
fe8ab488
A
537 kernel_bootstrap_log("prng_init");
538 prng_cpu_init(master_cpu);
539
39037602
A
540#ifdef MACH_BSD
541 kernel_bootstrap_log("bsd_early_init");
542 bsd_early_init();
543#endif
544
5ba3f43e
A
545#if defined(__arm64__)
546 ml_lockdown_init();
547#endif
548
1c79356b 549#ifdef IOKIT
39037602 550 kernel_bootstrap_log("PE_init_iokit");
2d21ac55 551 PE_init_iokit();
1c79356b 552#endif
fe8ab488
A
553
554 assert(ml_get_interrupts_enabled() == FALSE);
39037602 555
5ba3f43e
A
556 /*
557 * Past this point, kernel subsystems that expect to operate with
558 * interrupts or preemption enabled may begin enforcement.
559 */
560 early_boot_complete = TRUE;
39037602 561
5ba3f43e
A
562#if INTERRUPT_MASKED_DEBUG
563 // Reset interrupts masked timeout before we enable interrupts
564 ml_spin_debug_clear_self();
565#endif
43866e37
A
566 (void) spllo(); /* Allow interruptions */
567
b0d623f7 568#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
0c530ab8 569 /*
2d21ac55
A
570 * Create and initialize the copy window for processor 0
571 * This also allocates window space for all other processors.
572 * However, this is dependent on the number of processors - so this call
573 * must be after IOKit has been started because IOKit performs processor
574 * discovery.
0c530ab8 575 */
2d21ac55
A
576 cpu_userwindow_init(0);
577#endif
0c530ab8 578
0c530ab8 579 /*
2d21ac55 580 * Initialize the shared region module.
0c530ab8 581 */
2d21ac55
A
582 vm_shared_region_init();
583 vm_commpage_init();
316670eb 584 vm_commpage_text_init();
2d21ac55
A
585
586#if CONFIG_MACF
fe8ab488 587 kernel_bootstrap_log("mac_policy_initmach");
2d21ac55 588 mac_policy_initmach();
5ba3f43e
A
589#if CONFIG_VNGUARD
590 vnguard_policy_init();
591#endif
0c530ab8 592#endif
1c79356b 593
5ba3f43e
A
594#if defined(__arm__) || defined(__arm64__)
595#if CONFIG_KERNEL_INTEGRITY
596 machine_lockdown_preflight();
597#endif
598 /*
599 * Finalize protections on statically mapped pages now that comm page mapping is established.
600 */
601 arm_vm_prot_finalize(PE_state.bootArgs);
602#endif
4bd07ac2 603
3e170ce0 604#if CONFIG_SCHED_SFI
fe8ab488
A
605 kernel_bootstrap_log("sfi_init");
606 sfi_init();
3e170ce0 607#endif
fe8ab488 608
316670eb 609 /*
3e170ce0 610 * Initialize the globals used for permuting kernel
316670eb 611 * addresses that may be exported to userland as tokens
3e170ce0
A
612 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
613 * Force the random number to be odd to avoid mapping a non-zero
316670eb 614 * word-aligned address to zero via addition.
fe8ab488
A
615 * Note: at this stage we can use the cryptographically secure PRNG
616 * rather than early_random().
316670eb 617 */
fe8ab488
A
618 read_random(&vm_kernel_addrperm, sizeof(vm_kernel_addrperm));
619 vm_kernel_addrperm |= 1;
620 read_random(&buf_kernel_addrperm, sizeof(buf_kernel_addrperm));
621 buf_kernel_addrperm |= 1;
3e170ce0
A
622 read_random(&vm_kernel_addrperm_ext, sizeof(vm_kernel_addrperm_ext));
623 vm_kernel_addrperm_ext |= 1;
5ba3f43e
A
624 read_random(&vm_kernel_addrhash_salt, sizeof(vm_kernel_addrhash_salt));
625 read_random(&vm_kernel_addrhash_salt_ext, sizeof(vm_kernel_addrhash_salt_ext));
3e170ce0
A
626
627 vm_set_restrictions();
628
629
316670eb 630
9d749ea3 631
1c79356b
A
632 /*
633 * Start the user bootstrap.
634 */
1c79356b 635#ifdef MACH_BSD
2d21ac55
A
636 bsd_init();
637#endif
638
b0d623f7
A
639 /*
640 * Get rid of segments used to bootstrap kext loading. This removes
641 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
642 */
643 OSKextRemoveKextBootstrap();
1c79356b 644
5ba3f43e
A
645 /*
646 * Get rid of pages used for early boot tracing.
647 */
648 kdebug_free_early_buf();
649
55e303ae 650 serial_keyboard_init(); /* Start serial keyboard if wanted */
55e303ae 651
b0d623f7 652 vm_page_init_local_q();
3e170ce0 653
2d21ac55 654 thread_bind(PROCESSOR_NULL);
1c79356b
A
655
656 /*
657 * Become the pageout daemon.
658 */
1c79356b
A
659 vm_pageout();
660 /*NOTREACHED*/
661}
662
91447636
A
663/*
664 * slave_main:
665 *
666 * Load the first thread to start a processor.
667 */
1c79356b 668void
593a1d5f 669slave_main(void *machine_param)
1c79356b 670{
91447636 671 processor_t processor = current_processor();
1c79356b
A
672 thread_t thread;
673
91447636
A
674 /*
675 * Use the idle processor thread if there
676 * is no dedicated start up thread.
677 */
678 if (processor->next_thread == THREAD_NULL) {
679 thread = processor->idle_thread;
680 thread->continuation = (thread_continue_t)processor_start_thread;
593a1d5f 681 thread->parameter = machine_param;
91447636
A
682 }
683 else {
684 thread = processor->next_thread;
685 processor->next_thread = THREAD_NULL;
1c79356b 686 }
9bccf70c 687
91447636 688 load_context(thread);
1c79356b 689 /*NOTREACHED*/
1c79356b
A
690}
691
692/*
91447636
A
693 * processor_start_thread:
694 *
695 * First thread to execute on a started processor.
696 *
697 * Called at splsched.
1c79356b
A
698 */
699void
593a1d5f 700processor_start_thread(void *machine_param)
1c79356b 701{
91447636
A
702 processor_t processor = current_processor();
703 thread_t self = current_thread();
704
593a1d5f 705 slave_machine_init(machine_param);
1c79356b 706
91447636
A
707 /*
708 * If running the idle processor thread,
709 * reenter the idle loop, else terminate.
710 */
711 if (self == processor->idle_thread)
712 thread_block((thread_continue_t)idle_thread);
713
714 thread_terminate(self);
715 /*NOTREACHED*/
1c79356b
A
716}
717
718/*
91447636
A
719 * load_context:
720 *
721 * Start the first thread on a processor.
1c79356b 722 */
5ba3f43e 723static void __attribute__((noreturn))
91447636 724load_context(
1c79356b
A
725 thread_t thread)
726{
91447636 727 processor_t processor = current_processor();
1c79356b 728
2d21ac55
A
729
730#define load_context_kprintf(x...) /* kprintf("load_context: " x) */
731
fe8ab488 732 load_context_kprintf("machine_set_current_thread\n");
91447636 733 machine_set_current_thread(thread);
2d21ac55 734
fe8ab488 735 load_context_kprintf("processor_up\n");
91447636 736 processor_up(processor);
1c79356b 737
b0d623f7 738 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
91447636
A
739
740 /*
741 * Acquire a stack if none attached. The panic
742 * should never occur since the thread is expected
743 * to have reserved stack.
744 */
39236c6e 745 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread,
593a1d5f 746 thread->kernel_stack, thread->machine.kstackptr);
91447636 747 if (!thread->kernel_stack) {
fe8ab488 748 load_context_kprintf("stack_alloc_try\n");
91447636
A
749 if (!stack_alloc_try(thread))
750 panic("load_context");
751 }
752
753 /*
754 * The idle processor threads are not counted as
755 * running for load calculations.
756 */
757 if (!(thread->state & TH_IDLE))
fe8ab488 758 sched_run_incr(thread);
1c79356b 759
55e303ae 760 processor->active_thread = thread;
5ba3f43e
A
761 processor_state_update_explicit(processor, thread->sched_pri,
762 SFI_CLASS_KERNEL, PSET_SMP, thread_get_perfcontrol_class(thread));
39037602 763 processor->starting_pri = thread->sched_pri;
91447636
A
764 processor->deadline = UINT64_MAX;
765 thread->last_processor = processor;
766
767 processor->last_dispatch = mach_absolute_time();
2d21ac55
A
768 timer_start(&thread->system_timer, processor->last_dispatch);
769 PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
770
771 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
772 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
1c79356b 773
5ba3f43e 774
b0d623f7 775 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
1c79356b 776
fe8ab488 777 load_context_kprintf("machine_load_context\n");
55e303ae 778 machine_load_context(thread);
1c79356b
A
779 /*NOTREACHED*/
780}
b0d623f7
A
781
782void
6d2010ae 783scale_setup()
b0d623f7
A
784{
785 int scale = 0;
786#if defined(__LP64__)
6d2010ae
A
787 typeof(task_max) task_max_base = task_max;
788
789 /* Raise limits for servers with >= 16G */
790 if ((serverperfmode != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
b0d623f7
A
791 scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 *1024ULL));
792 /* limit to 128 G */
793 if (scale > 16)
794 scale = 16;
6d2010ae 795 task_max_base = 2500;
5ba3f43e
A
796 /* Raise limits for machines with >= 3GB */
797 } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL)) {
798 if ((uint64_t)sane_size < (uint64_t)(8 * 1024 * 1024 *1024ULL)) {
799 scale = 2;
800 } else {
801 /* limit to 64GB */
802 scale = MIN(16, (int)((uint64_t)sane_size / (uint64_t)(4 * 1024 * 1024 *1024ULL)));
803 }
804 }
6d2010ae
A
805
806 task_max = MAX(task_max, task_max_base * scale);
807
808 if (scale != 0) {
b0d623f7 809 task_threadmax = task_max;
6d2010ae
A
810 thread_max = task_max * 5;
811 }
812
b0d623f7 813#endif
6d2010ae
A
814
815 bsd_scale_setup(scale);
b0d623f7
A
816
817 ipc_space_max = SPACE_MAX;
b0d623f7
A
818 ipc_port_max = PORT_MAX;
819 ipc_pset_max = SET_MAX;
820 semaphore_max = SEMAPHORE_MAX;
821}
822