1/* 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 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. 14 * 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 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 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. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 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 */ 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 */ 62/* 63 */ 64 65/* 66 * Mach kernel startup. 67 */ 68 69#include <debug.h> 70#include <xpr_debug.h> 71#include <mach_kdp.h> 72 73#include <mach/boolean.h> 74#include <mach/machine.h> 75#include <mach/thread_act.h> 76#include <mach/task_special_ports.h> 77#include <mach/vm_param.h> 78#include <ipc/ipc_init.h> 79#include <kern/assert.h> 80#include <kern/mach_param.h> 81#include <kern/misc_protos.h> 82#include <kern/clock.h> 83#include <kern/coalition.h> 84#include <kern/cpu_number.h> 85#include <kern/ledger.h> 86#include <kern/machine.h> 87#include <kern/processor.h> 88#include <kern/sched_prim.h> 89#include <kern/sfi.h> 90#include <kern/startup.h> 91#include <kern/task.h> 92#include <kern/thread.h> 93#include <kern/timer.h> 94#if CONFIG_TELEMETRY 95#include <kern/telemetry.h> 96#endif 97#include <kern/wait_queue.h> 98#include <kern/xpr.h> 99#include <kern/zalloc.h> 100#include <kern/locks.h> 101#include <prng/random.h> 102#include <console/serial_protos.h> 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> 109#include <vm/vm_shared_region.h> 110#include <machine/pmap.h> 111#include <machine/commpage.h> 112#include <libkern/version.h> 113#include <sys/codesign.h> 114#include <sys/kdebug.h> 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 132 133#if MACH_KDP 134#include <kdp/kdp.h> 135#endif 136 137#if CONFIG_MACF 138#include <security/mac_mach_internal.h> 139#endif 140 141#if CONFIG_COUNTERS 142#include <pmc/pmc.h> 143#endif 144 145#if KPC 146#include <kern/kpc.h> 147#endif 148 149#if KPERF 150#include <kperf/kperf.h> 151#endif 152 153#if HYPERVISOR 154#include <kern/hv_support.h> 155#endif 156 157 158#include <i386/pmCPU.h> 159static void kernel_bootstrap_thread(void); 160 161static void load_context( 162 thread_t thread); 163#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0 164extern void cpu_userwindow_init(int); 165extern void cpu_physwindow_init(int); 166#endif 167 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 176// libkern/OSKextLib.cpp 177extern void OSKextRemoveKextBootstrap(void); 178 179void scale_setup(void); 180extern void bsd_scale_setup(int); 181extern unsigned int semaphore_max; 182extern void stackshot_lock_init(void); 183 184/* 185 * Running in virtual memory, on the interrupt stack. 186 */ 187 188extern int serverperfmode; 189 190/* size of kernel trace buffer, disabled by default */ 191unsigned int new_nkdbufs = 0; 192unsigned int wake_nkdbufs = 0; 193unsigned int write_trace_on_panic = 0; 194unsigned int trace_typefilter = 0; 195 196/* mach leak logging */ 197int log_leaks = 0; 198int turn_on_log_leaks = 0; 199 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} 213 214void 215kernel_early_bootstrap(void) 216{ 217 /* serverperfmode is needed by timer setup */ 218 if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof (serverperfmode))) { 219 serverperfmode = 1; 220 } 221 222 lck_mod_init(); 223 224 /* 225 * Initialize the timer callout world 226 */ 227 timer_call_init(); 228 229 /* 230 * Configure SFI classes 231 */ 232 sfi_early_init(); 233} 234 235 236void 237kernel_bootstrap(void) 238{ 239 kern_return_t result; 240 thread_t thread; 241 char namep[16]; 242 243 printf("%s\n", version); /* log kernel version */ 244 245 if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */ 246 turn_on_log_leaks = 1; 247 248 PE_parse_boot_argn("trace", &new_nkdbufs, sizeof (new_nkdbufs)); 249 PE_parse_boot_argn("trace_wake", &wake_nkdbufs, sizeof (wake_nkdbufs)); 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)); 252 253 scale_setup(); 254 255 kernel_bootstrap_log("vm_mem_bootstrap"); 256 vm_mem_bootstrap(); 257 258 kernel_bootstrap_log("cs_init"); 259 cs_init(); 260 261 kernel_bootstrap_log("vm_mem_init"); 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 269#if CONFIG_TELEMETRY 270 kernel_bootstrap_log("telemetry_init"); 271 telemetry_init(); 272#endif 273 274 kernel_bootstrap_log("stackshot_lock_init"); 275 stackshot_lock_init(); 276 277 kernel_bootstrap_log("sched_init"); 278 sched_init(); 279 280 kernel_bootstrap_log("wait_queue_bootstrap"); 281 wait_queue_bootstrap(); 282 283 kernel_bootstrap_log("ipc_bootstrap"); 284 ipc_bootstrap(); 285 286#if CONFIG_MACF 287 mac_policy_init(); 288#endif 289 kernel_bootstrap_log("ipc_init"); 290 ipc_init(); 291 292 /* 293 * As soon as the virtual memory system is up, we record 294 * that this CPU is using the kernel pmap. 295 */ 296 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL"); 297 PMAP_ACTIVATE_KERNEL(master_cpu); 298 299 kernel_bootstrap_log("mapping_free_prime"); 300 mapping_free_prime(); /* Load up with temporary mapping blocks */ 301 302 kernel_bootstrap_log("machine_init"); 303 machine_init(); 304 305 kernel_bootstrap_log("clock_init"); 306 clock_init(); 307 308 ledger_init(); 309 310 /* 311 * Initialize the IPC, task, and thread subsystems. 312 */ 313#if CONFIG_COALITIONS 314 kernel_bootstrap_log("coalition_init"); 315 coalition_init(); 316#endif 317 318 kernel_bootstrap_log("task_init"); 319 task_init(); 320 321 kernel_bootstrap_log("thread_init"); 322 thread_init(); 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 340 341 /* 342 * Create a kernel thread to execute the kernel bootstrap. 343 */ 344 kernel_bootstrap_log("kernel_thread_create"); 345 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread); 346 347 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result); 348 349 thread->state = TH_RUN; 350 thread_deallocate(thread); 351 352 kernel_bootstrap_log("load_context - done"); 353 load_context(thread); 354 /*NOTREACHED*/ 355} 356 357int kth_started = 0; 358 359vm_offset_t vm_kernel_addrperm; 360vm_offset_t buf_kernel_addrperm; 361 362/* 363 * Now running in a thread. Kick off other services, 364 * invoke user bootstrap, enter pageout loop. 365 */ 366static void 367kernel_bootstrap_thread(void) 368{ 369 processor_t processor = current_processor(); 370 371#define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */ 372 kernel_bootstrap_thread_log("idle_thread_create"); 373 /* 374 * Create the idle processor thread. 375 */ 376 idle_thread_create(processor); 377 378 /* 379 * N.B. Do not stick anything else 380 * before this point. 381 * 382 * Start up the scheduler services. 383 */ 384 kernel_bootstrap_thread_log("sched_startup"); 385 sched_startup(); 386 387 /* 388 * Thread lifecycle maintenance (teardown, stack allocation) 389 */ 390 kernel_bootstrap_thread_log("thread_daemon_init"); 391 thread_daemon_init(); 392 393 /* Create kernel map entry reserve */ 394 vm_kernel_reserved_entry_init(); 395 396 /* 397 * Thread callout service. 398 */ 399 kernel_bootstrap_thread_log("thread_call_initialize"); 400 thread_call_initialize(); 401 402 /* 403 * Remain on current processor as 404 * additional processors come online. 405 */ 406 kernel_bootstrap_thread_log("thread_bind"); 407 thread_bind(processor); 408 409 /* 410 * Initialize ipc thread call support. 411 */ 412 kernel_bootstrap_thread_log("ipc_thread_call_init"); 413 ipc_thread_call_init(); 414 415 /* 416 * Kick off memory mapping adjustments. 417 */ 418 kernel_bootstrap_thread_log("mapping_adjust"); 419 mapping_adjust(); 420 421 /* 422 * Create the clock service. 423 */ 424 kernel_bootstrap_thread_log("clock_service_create"); 425 clock_service_create(); 426 427 /* 428 * Create the device service. 429 */ 430 device_service_create(); 431 432 kth_started = 1; 433 434#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0 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 441 442 443 444#if MACH_KDP 445 kernel_bootstrap_log("kdp_init"); 446 kdp_init(); 447#endif 448 449#if ALTERNATE_DEBUGGER 450 alternate_debugger_init(); 451#endif 452 453#if CONFIG_COUNTERS 454 pmc_bootstrap(); 455#endif 456 457#if KPC 458 kpc_init(); 459#endif 460 461#if CONFIG_ECC_LOGGING 462 ecc_log_init(); 463#endif 464 465#if KPERF 466 kperf_bootstrap(); 467#endif 468 469#if HYPERVISOR 470 hv_support_init(); 471#endif 472 473#if CONFIG_TELEMETRY 474 kernel_bootstrap_log("bootprofile_init"); 475 bootprofile_init(); 476#endif 477 478#if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX 479 vmx_init(); 480#endif 481 482#if (defined(__i386__) || defined(__x86_64__)) 483 if (turn_on_log_leaks && !new_nkdbufs) 484 new_nkdbufs = 200000; 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); 491 if (turn_on_log_leaks) 492 log_leaks = 1; 493 494#endif 495 496 kernel_bootstrap_log("prng_init"); 497 prng_cpu_init(master_cpu); 498 499#ifdef IOKIT 500 PE_init_iokit(); 501#endif 502 503 assert(ml_get_interrupts_enabled() == FALSE); 504 (void) spllo(); /* Allow interruptions */ 505 506#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0 507 /* 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. 513 */ 514 cpu_userwindow_init(0); 515#endif 516 517#if (!defined(__i386__) && !defined(__x86_64__)) 518 if (turn_on_log_leaks && !new_nkdbufs) 519 new_nkdbufs = 200000; 520 if (trace_typefilter) 521 start_kern_tracing_with_typefilter(new_nkdbufs, FALSE, trace_typefilter); 522 else 523 start_kern_tracing(new_nkdbufs, FALSE); 524 if (turn_on_log_leaks) 525 log_leaks = 1; 526#endif 527 528 /* 529 * Initialize the shared region module. 530 */ 531 vm_shared_region_init(); 532 vm_commpage_init(); 533 vm_commpage_text_init(); 534 535 536#if CONFIG_MACF 537 kernel_bootstrap_log("mac_policy_initmach"); 538 mac_policy_initmach(); 539#endif 540 541 kernel_bootstrap_log("sfi_init"); 542 sfi_init(); 543 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. 550 * Note: at this stage we can use the cryptographically secure PRNG 551 * rather than early_random(). 552 */ 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; 557 558 /* 559 * Start the user bootstrap. 560 */ 561#ifdef MACH_BSD 562 bsd_init(); 563#endif 564 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(); 570 571 serial_keyboard_init(); /* Start serial keyboard if wanted */ 572 573 vm_page_init_local_q(); 574 575 thread_bind(PROCESSOR_NULL); 576 577 /* 578 * Become the pageout daemon. 579 */ 580 vm_pageout(); 581 /*NOTREACHED*/ 582} 583 584/* 585 * slave_main: 586 * 587 * Load the first thread to start a processor. 588 */ 589void 590slave_main(void *machine_param) 591{ 592 processor_t processor = current_processor(); 593 thread_t thread; 594 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; 602 thread->parameter = machine_param; 603 } 604 else { 605 thread = processor->next_thread; 606 processor->next_thread = THREAD_NULL; 607 } 608 609 load_context(thread); 610 /*NOTREACHED*/ 611} 612 613/* 614 * processor_start_thread: 615 * 616 * First thread to execute on a started processor. 617 * 618 * Called at splsched. 619 */ 620void 621processor_start_thread(void *machine_param) 622{ 623 processor_t processor = current_processor(); 624 thread_t self = current_thread(); 625 626 slave_machine_init(machine_param); 627 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*/ 637} 638 639/* 640 * load_context: 641 * 642 * Start the first thread on a processor. 643 */ 644static void 645load_context( 646 thread_t thread) 647{ 648 processor_t processor = current_processor(); 649 650 651#define load_context_kprintf(x...) /* kprintf("load_context: " x) */ 652 653 load_context_kprintf("machine_set_current_thread\n"); 654 machine_set_current_thread(thread); 655 656 load_context_kprintf("processor_up\n"); 657 processor_up(processor); 658 659 PMAP_ACTIVATE_KERNEL(processor->cpu_id); 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 */ 666 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread, 667 thread->kernel_stack, thread->machine.kstackptr); 668 if (!thread->kernel_stack) { 669 load_context_kprintf("stack_alloc_try\n"); 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)) 679 sched_run_incr(thread); 680 681 processor->active_thread = thread; 682 processor->current_pri = thread->sched_pri; 683 processor->current_thmode = thread->sched_mode; 684 processor->deadline = UINT64_MAX; 685 thread->last_processor = processor; 686 687 processor->last_dispatch = mach_absolute_time(); 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); 693 694 PMAP_ACTIVATE_USER(thread, processor->cpu_id); 695 696 load_context_kprintf("machine_load_context\n"); 697 machine_load_context(thread); 698 /*NOTREACHED*/ 699} 700 701void 702scale_setup() 703{ 704 int scale = 0; 705#if defined(__LP64__) 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))) { 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; 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) { 721 task_threadmax = task_max; 722 thread_max = task_max * 5; 723 } 724 725#endif 726 727 bsd_scale_setup(scale); 728 729 ipc_space_max = SPACE_MAX; 730 ipc_port_max = PORT_MAX; 731 ipc_pset_max = SET_MAX; 732 semaphore_max = SEMAPHORE_MAX; 733} 734 735