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