/
init_main.c
1189 lines (1000 loc) · 29.1 KB
/
init_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: init_main.c,v 1.538 2022/03/19 13:51:35 hannken Exp $ */
/*-
* Copyright (c) 2008, 2009, 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)init_main.c 8.16 (Berkeley) 5/14/95
*/
/*
* Copyright (c) 1995 Christopher G. Demetriou. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)init_main.c 8.16 (Berkeley) 5/14/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.538 2022/03/19 13:51:35 hannken Exp $");
#include "opt_cnmagic.h"
#include "opt_ddb.h"
#include "opt_inet.h"
#include "opt_ipsec.h"
#include "opt_modular.h"
#include "opt_ntp.h"
#include "opt_pipe.h"
#include "opt_syscall_debug.h"
#include "opt_sysv.h"
#include "opt_fileassoc.h"
#include "opt_ktrace.h"
#include "opt_pax.h"
#include "opt_compat_netbsd.h"
#include "opt_wapbl.h"
#include "opt_ptrace.h"
#include "opt_splash.h"
#include "opt_kernhist.h"
#include "opt_gprof.h"
#if defined(SPLASHSCREEN) && defined(makeoptions_SPLASHSCREEN_IMAGE)
extern void *_binary_splash_image_start;
extern void *_binary_splash_image_end;
#endif
#include "ksyms.h"
#include "veriexec.h"
#include <sys/param.h>
#include <sys/acct.h>
#include <sys/filedesc.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/callout.h>
#include <sys/cpu.h>
#include <sys/cpufreq.h>
#include <sys/spldebug.h>
#include <sys/kernel.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/resourcevar.h>
#include <sys/signalvar.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/fstrans.h>
#include <sys/tty.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/exec.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/percpu.h>
#include <sys/pserialize.h>
#include <sys/pset.h>
#include <sys/sysctl.h>
#include <sys/reboot.h>
#include <sys/event.h>
#include <sys/mbuf.h>
#include <sys/sched.h>
#include <sys/sleepq.h>
#include <sys/ipi.h>
#include <sys/iostat.h>
#include <sys/vmem.h>
#include <sys/uuid.h>
#include <sys/extent.h>
#include <sys/disk.h>
#include <sys/msgbuf.h>
#include <sys/module.h>
#include <sys/module_hook.h>
#include <sys/event.h>
#include <sys/lockf.h>
#include <sys/once.h>
#include <sys/kcpuset.h>
#include <sys/ksyms.h>
#include <sys/uidinfo.h>
#include <sys/kprintf.h>
#include <sys/bufq.h>
#include <sys/threadpool.h>
#include <sys/futex.h>
#ifdef IPSEC
#include <netipsec/ipsec.h>
#endif
#include <sys/domain.h>
#include <sys/namei.h>
#include <sys/rnd.h>
#include <sys/pipe.h>
#if NVERIEXEC > 0
#include <sys/verified_exec.h>
#endif /* NVERIEXEC > 0 */
#ifdef KTRACE
#include <sys/ktrace.h>
#endif
#include <sys/kauth.h>
#include <net80211/ieee80211_netbsd.h>
#include <sys/cprng.h>
#include <sys/psref.h>
#include <sys/radixtree.h>
#include <sys/syscall.h>
#include <sys/syscallargs.h>
#include <sys/pax.h>
#include <dev/clock_subr.h>
#include <secmodel/secmodel.h>
#include <ufs/ufs/quota.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/specfs/specdev.h>
#include <sys/cpu.h>
#include <uvm/uvm.h> /* extern struct uvm uvm */
#include <dev/cons.h>
#include <dev/splash/splash.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/pfil.h>
#include <net/raw_cb.h>
#include <net/if_llatbl.h>
#include <prop/proplib.h>
#include <sys/userconf.h>
extern struct lwp lwp0;
extern time_t rootfstime;
#ifndef curlwp
struct lwp *curlwp = &lwp0;
#endif
struct proc *initproc;
struct vnode *rootvp, *swapdev_vp;
int boothowto;
int cold __read_mostly = 1; /* still working on startup */
int shutting_down __read_mostly; /* system is shutting down */
int start_init_exec; /* semaphore for start_init() */
static void check_console(struct lwp *l);
static void start_init(void *);
static void configure(void);
static void configure2(void);
static void configure3(void);
void main(void);
/*
* System startup; initialize the world, create process 0, mount root
* filesystem, and fork to create init and pagedaemon. Most of the
* hard work is done in the lower-level initialization routines including
* startup(), which does memory initialization and autoconfiguration.
*/
void
main(void)
{
struct timespec time;
struct lwp *l;
struct proc *p;
int s, error;
#ifdef NVNODE_IMPLICIT
int usevnodes;
#endif
CPU_INFO_ITERATOR cii;
struct cpu_info *ci;
#ifdef DIAGNOSTIC
/*
* Verify that CPU_INFO_FOREACH() knows about the boot CPU
* and only the boot CPU at this point.
*/
int cpucount = 0;
for (CPU_INFO_FOREACH(cii, ci)) {
KASSERT(ci == curcpu());
cpucount++;
}
KASSERT(cpucount == 1);
#endif
l = &lwp0;
#ifndef LWP0_CPU_INFO
l->l_cpu = curcpu();
#endif
l->l_pflag |= LP_RUNNING;
/*
* Attempt to find console and initialize
* in case of early panic or other messages.
*/
consinit();
#ifdef CNMAGIC
cn_set_magic(CNMAGIC);
#endif
kernel_lock_init();
once_init();
todr_init();
mi_cpu_init();
kernconfig_lock_init();
kthread_sysinit();
/* Initialize the device switch tables. */
devsw_init();
/* Initialize event counters. */
evcnt_init();
uvm_init();
ubchist_init();
kcpuset_sysinit();
prop_kern_init();
#if ((NKSYMS > 0) || (NDDB > 0) || (NMODULAR > 0))
ksyms_init();
#endif
kprintf_init();
percpu_init();
/* Initialize lock caches. */
mutex_obj_init();
/* Initialize radix trees (used by numerous subsystems). */
radix_tree_init();
/* Passive serialization. */
pserialize_init();
/* Initialize the extent manager. */
extent_init();
/* Do machine-dependent initialization. */
cpu_startup();
/* Initialize the sysctl subsystem. */
sysctl_init();
/* Initialize callouts, part 1. */
callout_startup();
/* Initialize the kernel authorization subsystem. */
kauth_init();
secmodel_init();
spec_init();
/*
* Set BPF op vector. Can't do this in bpf attach, since
* network drivers attach before bpf.
*/
bpf_setops();
/* Initialize what we can in ipi(9) before CPUs are detected. */
ipi_sysinit();
/* Start module system. */
module_init();
module_hook_init();
/*
* Initialize the kernel authorization subsystem and start the
* default security model, if any. We need to do this early
* enough so that subsystems relying on any of the aforementioned
* can work properly. Since the security model may dictate the
* credential inheritance policy, it is needed at least before
* any process is created, specifically proc0.
*/
module_init_class(MODULE_CLASS_SECMODEL);
/* Initialize the buffer cache */
bufinit();
biohist_init();
#ifdef KERNHIST
sysctl_kernhist_init();
#endif
#if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_IMAGE)
size_t splash_size = (&_binary_splash_image_end -
&_binary_splash_image_start) * sizeof(void *);
splash_setimage(&_binary_splash_image_start, splash_size);
#endif
/* Initialize sockets. */
soinit();
/*
* The following things must be done before autoconfiguration.
*/
rnd_init(); /* initialize entropy pool */
cprng_init(); /* initialize cryptographic PRNG */
/* Initialize process and pgrp structures. */
procinit();
lwpinit();
/* Must be called after lwpinit (lwpinit_specificdata) */
psref_init();
/* Initialize signal-related data structures. */
signal_init();
/* Initialize resource management. */
resource_init();
/* Create process 0. */
proc0_init();
lwp0_init();
/* Disable preemption during boot. */
kpreempt_disable();
/* Initialize the threadpool system. */
threadpools_init();
/* Initialize the UID hash table. */
uid_init();
/* Charge root for one process. */
(void)chgproccnt(0, 1);
/* Initialize the run queues, turnstiles and sleep queues. */
sched_rqinit();
turnstile_init();
sleeptab_init(&sleeptab);
sched_init();
/* Initialize processor-sets */
psets_init();
/* Initialize cpufreq(9) */
cpufreq_init();
/* MI initialization of the boot cpu */
error = mi_cpu_attach(curcpu());
KASSERT(error == 0);
/* Initialize timekeeping. */
time_init();
/*
* Initialize mbuf's. Do this now because we might attempt to
* allocate mbufs or mbuf clusters during autoconfiguration.
*/
mbinit();
/* Initialize I/O statistics. */
iostat_init();
/* Initialize the log device. */
loginit();
/* Second part of module system initialization. */
module_start_unload_thread();
/* Initialize autoconf data structures before any modules are loaded */
config_init_mi();
/* Initialize the file systems. */
#ifdef NVNODE_IMPLICIT
/*
* If maximum number of vnodes in namei vnode cache is not explicitly
* defined in kernel config, adjust the number such as we use roughly
* 10% of memory for vnodes and associated data structures in the
* assumed worst case. Do not provide fewer than NVNODE vnodes.
*/
usevnodes = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC),
10, VNODE_KMEM_MAXPCT) / VNODE_COST;
if (usevnodes > desiredvnodes)
desiredvnodes = usevnodes;
#endif /* NVNODE_IMPLICIT */
#ifdef MAXFILES_IMPLICIT
/*
* If maximum number of files is not explicitly defined in
* kernel config, adjust the number so that it is somewhat
* more reasonable on machines with larger memory sizes.
* Arbitrary numbers are 20,000 files for 16GB RAM or more
* and 10,000 files for 1GB RAM or more.
*
* XXXtodo: adjust this and other values totally dynamically
*/
if (ctob((uint64_t)physmem) >= 16ULL * 1024 * 1024 * 1024)
maxfiles = MAX(maxfiles, 20000);
if (ctob((uint64_t)physmem) >= 1024 * 1024 * 1024)
maxfiles = MAX(maxfiles, 10000);
#endif /* MAXFILES_IMPLICIT */
/* Initialize fstrans. */
fstrans_init();
vfsinit();
lf_init();
/* Initialize the file descriptor system. */
fd_sys_init();
/* Initialize cwd structures */
cwd_sys_init();
/* Initialize kqueue. */
kqueue_init();
inittimecounter();
ntp_init();
/* Initialize tty subsystem. */
tty_init();
ttyldisc_init();
/* Initialize the buffer cache, part 2. */
bufinit2();
/* Initialize the disk wedge subsystem. */
dkwedge_init();
/* Initialize pfil */
pfil_init();
/* Initialize interfaces. */
ifinit1();
spldebug_start();
/* Initialize sockets thread(s) */
soinit1();
/*
* Initialize the bufq strategy sub-system and any built-in
* strategy modules - they may be needed by some devices during
* auto-configuration
*/
bufq_init();
module_init_class(MODULE_CLASS_BUFQ);
/* Configure the system hardware. This will enable interrupts. */
configure();
#ifdef __HAVE_LEGACY_INTRCNT
evcnt_attach_legacy_intrcnt();
#endif
/* Enable deferred processing of RNG samples */
rnd_init_softint();
/* Once all CPUs are detected, initialize the per-CPU cprng_fast. */
cprng_fast_init();
ssp_init();
ubc_init(); /* must be after autoconfig */
mm_init();
configure2();
/* Initialize the rest of ipi(9) after CPUs have been detected. */
ipi_percpu_init();
futex_sys_init();
/* Now timer is working. Enable preemption. */
kpreempt_enable();
/* Get the threads going and into any sleeps before continuing. */
yield();
vmem_rehash_start(); /* must be before exec_init */
/* Initialize exec structures */
exec_init(1); /* seminit calls exithook_establish() */
#if NVERIEXEC > 0
/*
* Initialise the Veriexec subsystem.
*/
veriexec_init();
#endif /* NVERIEXEC > 0 */
pax_init();
#ifdef IPSEC
/* Attach network crypto subsystem */
ipsec_attach();
#endif
/*
* Initialize protocols. Block reception of incoming packets
* until everything is ready.
*/
s = splnet();
ifinit();
#if defined(INET) || defined(INET6)
lltableinit();
#endif
domaininit(true);
ifinit_post();
if_attachdomain();
splx(s);
#ifdef GPROF
/* Initialize kernel profiling. */
kmstartup();
#endif
/* Initialize system accounting. */
acct_init();
#ifndef PIPE_SOCKETPAIR
/* Initialize pipes. */
pipe_init();
#endif
#ifdef KTRACE
/* Initialize ktrace. */
ktrinit();
#endif
machdep_init();
procinit_sysctl();
scdebug_init();
/*
* Create process 1 (init(8)). We do this now, as Unix has
* historically had init be process 1, and changing this would
* probably upset a lot of people.
*
* Note that process 1 won't immediately exec init(8), but will
* wait for us to inform it that the root file system has been
* mounted.
*/
if (fork1(l, 0, SIGCHLD, NULL, 0, start_init, NULL, NULL))
panic("fork init");
/*
* The initproc variable cannot be initialized in start_init as there
* is a race between vfs_mountroot and start_init.
*/
mutex_enter(&proc_lock);
initproc = proc_find_raw(1);
mutex_exit(&proc_lock);
/*
* Load any remaining builtin modules, and hand back temporary
* storage to the VM system. Then require force when loading any
* remaining un-init'ed built-in modules to avoid later surprises.
*/
module_init_class(MODULE_CLASS_ANY);
module_builtin_require_force();
/*
* Finalize configuration now that all real devices have been
* found. This needs to be done before the root device is
* selected, since finalization may create the root device.
*/
config_finalize();
sysctl_finalize();
/*
* Now that autoconfiguration has completed, we can determine
* the root and dump devices.
*/
cpu_rootconf();
cpu_dumpconf();
/* Mount the root file system. */
do {
domountroothook(root_device);
if ((error = vfs_mountroot())) {
printf("cannot mount root, error = %d\n", error);
boothowto |= RB_ASKNAME;
setroot(root_device,
(rootdev != NODEV) ? DISKPART(rootdev) : 0);
}
} while (error != 0);
mountroothook_destroy();
configure3();
/*
* Initialise the time-of-day clock, passing the time recorded
* in the root filesystem (if any) for use by systems that
* don't have a non-volatile time-of-day device.
*/
inittodr(rootfstime);
/*
* Now can look at time, having had a chance to verify the time
* from the file system. Reset l->l_rtime as it may have been
* munched in mi_switch() after the time got set.
*/
getnanotime(&time);
mutex_enter(&proc_lock);
LIST_FOREACH(p, &allproc, p_list) {
KASSERT((p->p_flag & PK_MARKER) == 0);
mutex_enter(p->p_lock);
TIMESPEC_TO_TIMEVAL(&p->p_stats->p_start, &time);
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
lwp_lock(l);
memset(&l->l_rtime, 0, sizeof(l->l_rtime));
lwp_unlock(l);
}
mutex_exit(p->p_lock);
}
mutex_exit(&proc_lock);
binuptime(&curlwp->l_stime);
for (CPU_INFO_FOREACH(cii, ci)) {
ci->ci_schedstate.spc_lastmod = time_second;
}
/* Create the pageout daemon kernel thread. */
uvm_swap_init();
if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL, uvm_pageout,
NULL, NULL, "pgdaemon"))
panic("fork pagedaemon");
/* Create the filesystem syncer kernel thread. */
if (kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL, sched_sync,
NULL, NULL, "ioflush"))
panic("fork syncer");
/* Wait for final configure threads to complete. */
config_finalize_mountroot();
/*
* Okay, now we can let init(8) exec! It's off to userland!
*/
mutex_enter(&proc_lock);
start_init_exec = 1;
cv_broadcast(&lbolt);
mutex_exit(&proc_lock);
/* The scheduler is an infinite loop. */
uvm_scheduler();
/* NOTREACHED */
}
/*
* Configure the system's hardware.
*/
static void
configure(void)
{
/*
* XXX
* callout_setfunc() requires mutex(9) so it can't be in config_init()
* on amiga and atari which use config_init() and autoconf(9) fucntions
* to initialize console devices.
*/
config_twiddle_init();
pmf_init();
/* Initialize driver modules */
module_init_class(MODULE_CLASS_DRIVER);
userconf_init();
if (boothowto & RB_USERCONF)
userconf_prompt();
if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
printf_nolog("Detecting hardware...");
}
/*
* Do the machine-dependent portion of autoconfiguration. This
* sets the configuration machinery here in motion by "finding"
* the root bus. When this function returns, we expect interrupts
* to be enabled.
*/
cpu_configure();
}
static void
configure2(void)
{
CPU_INFO_ITERATOR cii;
struct cpu_info *ci;
int s;
/* Fix up CPU topology info, which has all been collected by now. */
cpu_topology_init();
/*
* Now that we've found all the hardware, start the real time
* and statistics clocks.
*/
initclocks();
cold = 0; /* clocks are running, we're warm now! */
s = splsched();
curcpu()->ci_schedstate.spc_flags |= SPCF_RUNNING;
splx(s);
/* Setup the runqueues and scheduler. */
runq_init();
synch_init();
/* Boot the secondary processors. */
for (CPU_INFO_FOREACH(cii, ci)) {
uvm_cpu_attach(ci);
}
/* Decide how to partition free memory. */
uvm_page_rebucket();
mp_online = true;
#if defined(MULTIPROCESSOR)
cpu_boot_secondary_processors();
#endif
/*
* Bus scans can make it appear as if the system has paused, so
* twiddle constantly while config_interrupts() jobs are running.
*/
config_twiddle_fn(NULL);
/*
* Create threads to call back and finish configuration for
* devices that want interrupts enabled.
*/
config_create_interruptthreads();
}
static void
configure3(void)
{
/*
* Create threads to call back and finish configuration for
* devices that want the mounted root file system.
*/
config_create_mountrootthreads();
/* Get the threads going and into any sleeps before continuing. */
yield();
}
static void
rootconf_handle_wedges(void)
{
struct disklabel label;
struct partition *p;
struct vnode *vp;
daddr_t startblk;
uint64_t nblks;
device_t dev;
int error;
if (booted_nblks) {
/*
* bootloader passed geometry
*/
dev = booted_device;
startblk = booted_startblk;
nblks = booted_nblks;
/*
* keep booted_device and booted_partition
* in case the kernel doesn't identify a wedge
*/
} else {
/*
* bootloader passed partition number
*
* We cannot ask the partition device directly when it is
* covered by a wedge. Instead we look up the geometry in
* the disklabel.
*/
vp = opendisk(booted_device);
if (vp == NULL)
return;
VOP_UNLOCK(vp);
error = VOP_IOCTL(vp, DIOCGDINFO, &label, FREAD, NOCRED);
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
VOP_CLOSE(vp, FREAD, NOCRED);
vput(vp);
if (error)
return;
KASSERT(booted_partition >= 0
&& booted_partition < MAXPARTITIONS);
p = &label.d_partitions[booted_partition];
dev = booted_device;
startblk = p->p_offset;
nblks = p->p_size;
}
dev = dkwedge_find_partition(dev, startblk, nblks);
if (dev != NULL) {
booted_device = dev;
booted_partition = 0;
}
}
void
rootconf(void)
{
if (booted_device != NULL)
rootconf_handle_wedges();
setroot(booted_device, booted_partition);
}
static void
check_console(struct lwp *l)
{
struct vnode *vp;
int error;
error = namei_simple_kernel("/dev/console",
NSM_FOLLOW_NOEMULROOT, &vp);
if (error == 0) {
vrele(vp);
} else if (error == ENOENT) {
if (boothowto & (AB_VERBOSE|AB_DEBUG))
printf("warning: no /dev/console\n");
} else {
printf("warning: lookup /dev/console: error %d\n", error);
}
}
/*
* List of paths to try when searching for "init".
*/
static const char * const initpaths[] = {
"/sbin/init",
"/sbin/oinit",
"/sbin/init.bak",
"/rescue/init",
NULL,
};
/*
* Start the initial user process; try exec'ing each pathname in "initpaths".
* The program is invoked with one argument containing the boot flags.
*/
static void
start_init(void *arg)
{
struct lwp *l = arg;
struct proc *p = l->l_proc;
vaddr_t addr;
struct sys_execve_args /* {
syscallarg(const char *) path;
syscallarg(char * const *) argp;
syscallarg(char * const *) envp;
} */ args;
int options, i, error;
register_t retval[2];
char flags[4], *flagsp;
const char *path, *slash;
char *ucp, **uap, *arg0, *arg1, *argv[3];
char ipath[129];
int ipx, len;
/*
* Now in process 1.
*/
strncpy(p->p_comm, "init", MAXCOMLEN);
/*
* Wait for main() to tell us that it's safe to exec.
*/
mutex_enter(&proc_lock);
while (start_init_exec == 0)
cv_wait(&lbolt, &proc_lock);
mutex_exit(&proc_lock);
/*
* This is not the right way to do this. We really should
* hand-craft a descriptor onto /dev/console to hand to init,
* but that's a _lot_ more work, and the benefit from this easy
* hack makes up for the "good is the enemy of the best" effect.
*/
check_console(l);
/*