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