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