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