This source file includes following definitions.
- main
- check_console
- start_init
- start_update
- start_cleaner
- start_reaper
- start_crypto
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 #include <sys/param.h>
42 #include <sys/filedesc.h>
43 #include <sys/file.h>
44 #include <sys/errno.h>
45 #include <sys/exec.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
48 #include <sys/mount.h>
49 #include <sys/proc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/systm.h>
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 #include <sys/tty.h>
56 #include <sys/conf.h>
57 #include <sys/buf.h>
58 #include <sys/device.h>
59 #include <sys/socketvar.h>
60 #include <sys/lockf.h>
61 #include <sys/protosw.h>
62 #include <sys/reboot.h>
63 #include <sys/user.h>
64 #ifdef SYSVSHM
65 #include <sys/shm.h>
66 #endif
67 #ifdef SYSVSEM
68 #include <sys/sem.h>
69 #endif
70 #ifdef SYSVMSG
71 #include <sys/msg.h>
72 #endif
73 #include <sys/domain.h>
74 #include <sys/mbuf.h>
75 #include <sys/pipe.h>
76 #include <sys/workq.h>
77
78 #include <sys/syscall.h>
79 #include <sys/syscallargs.h>
80
81 #include <dev/rndvar.h>
82
83 #include <ufs/ufs/quota.h>
84
85 #include <machine/cpu.h>
86
87 #include <uvm/uvm.h>
88
89 #include <net/if.h>
90 #include <net/raw_cb.h>
91
92 #if defined(CRYPTO)
93 #include <crypto/cryptodev.h>
94 #include <crypto/cryptosoft.h>
95 #endif
96
97 #if defined(NFSSERVER) || defined(NFSCLIENT)
98 extern void nfs_init(void);
99 #endif
100
101 #include "softraid.h"
102
103 const char copyright[] =
104 "Copyright (c) 1982, 1986, 1989, 1991, 1993\n"
105 "\tThe Regents of the University of California. All rights reserved.\n"
106 "Copyright (c) 1995-2007 OpenBSD. All rights reserved. http://www.OpenBSD.org\n";
107
108
109 struct session session0;
110 struct pgrp pgrp0;
111 struct proc proc0;
112 struct process process0;
113 struct pcred cred0;
114 struct plimit limit0;
115 struct vmspace vmspace0;
116 struct sigacts sigacts0;
117 struct proc *initproc;
118
119 int cmask = CMASK;
120 extern struct user *proc0paddr;
121
122 struct vnode *rootvp, *swapdev_vp;
123 int boothowto;
124 struct timeval boottime;
125 int ncpus = 1;
126 __volatile int start_init_exec;
127
128 #if !defined(NO_PROPOLICE)
129 long __guard[8];
130 #endif
131
132
133 int main(void *);
134 void check_console(struct proc *);
135 void start_init(void *);
136 void start_cleaner(void *);
137 void start_update(void *);
138 void start_reaper(void *);
139 void start_crypto(void *);
140 void init_exec(void);
141 void kqueue_init(void);
142 void workq_init(void);
143
144 extern char sigcode[], esigcode[];
145 #ifdef SYSCALL_DEBUG
146 extern char *syscallnames[];
147 #endif
148
149 struct emul emul_native = {
150 "native",
151 NULL,
152 sendsig,
153 SYS_syscall,
154 SYS_MAXSYSCALL,
155 sysent,
156 #ifdef SYSCALL_DEBUG
157 syscallnames,
158 #else
159 NULL,
160 #endif
161 0,
162 copyargs,
163 setregs,
164 NULL,
165 sigcode,
166 esigcode,
167 EMUL_ENABLED | EMUL_NATIVE,
168 };
169
170
171
172
173
174
175
176
177
178 int
179 main(void *framep)
180 {
181 struct proc *p;
182 struct pdevinit *pdev;
183 struct timeval rtv;
184 quad_t lim;
185 int s, i;
186 extern struct pdevinit pdevinit[];
187 extern void scheduler_start(void);
188 extern void disk_init(void);
189 extern void endtsleep(void *);
190 extern void realitexpire(void *);
191
192
193
194
195
196 curproc = p = &proc0;
197 p->p_cpu = curcpu();
198
199
200
201
202 timeout_startup();
203
204
205
206
207
208 config_init();
209 consinit();
210
211 printf("%s\n", copyright);
212
213 KERNEL_LOCK_INIT();
214
215 uvm_init();
216 disk_init();
217 tty_init();
218 cpu_startup();
219
220
221
222
223
224 mbinit();
225
226
227 soinit();
228
229
230
231
232 procinit();
233
234
235 lf_init();
236
237
238
239
240 filedesc_init();
241
242
243
244
245 pipe_init();
246
247
248
249
250 kqueue_init();
251
252
253
254
255
256 process0.ps_mainproc = p;
257 TAILQ_INIT(&process0.ps_threads);
258 TAILQ_INSERT_TAIL(&process0.ps_threads, p, p_thr_link);
259 p->p_p = &process0;
260
261 LIST_INSERT_HEAD(&allproc, p, p_list);
262 p->p_pgrp = &pgrp0;
263 LIST_INSERT_HEAD(PIDHASH(0), p, p_hash);
264 LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash);
265 LIST_INIT(&pgrp0.pg_members);
266 LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist);
267
268 pgrp0.pg_session = &session0;
269 session0.s_count = 1;
270 session0.s_leader = p;
271
272 atomic_setbits_int(&p->p_flag, P_SYSTEM | P_NOCLDWAIT);
273 p->p_stat = SONPROC;
274 p->p_nice = NZERO;
275 p->p_emul = &emul_native;
276 bcopy("swapper", p->p_comm, sizeof ("swapper"));
277
278
279 timeout_set(&p->p_sleep_to, endtsleep, p);
280 timeout_set(&p->p_realit_to, realitexpire, p);
281
282
283 cred0.p_refcnt = 1;
284 p->p_cred = &cred0;
285 p->p_ucred = crget();
286 p->p_ucred->cr_ngroups = 1;
287
288
289 signal_init();
290 p->p_sigacts = &sigacts0;
291 siginit(p);
292
293
294 p->p_fd = fdinit(NULL);
295
296
297 p->p_p->ps_limit = &limit0;
298 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
299 limit0.pl_rlimit[i].rlim_cur =
300 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
301 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_cur = NOFILE;
302 limit0.pl_rlimit[RLIMIT_NOFILE].rlim_max = MIN(NOFILE_MAX,
303 (maxfiles - NOFILE > NOFILE) ? maxfiles - NOFILE : NOFILE);
304 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
305 lim = ptoa(uvmexp.free);
306 limit0.pl_rlimit[RLIMIT_RSS].rlim_max = lim;
307 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_max = lim;
308 limit0.pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = lim / 3;
309 limit0.p_refcnt = 1;
310
311
312 uvmspace_init(&vmspace0, pmap_kernel(), round_page(VM_MIN_ADDRESS),
313 trunc_page(VM_MAX_ADDRESS), TRUE);
314 p->p_vmspace = &vmspace0;
315
316 p->p_addr = proc0paddr;
317
318
319
320
321
322 p->p_stats = &p->p_addr->u_stats;
323
324
325
326
327 (void)chgproccnt(0, 1);
328
329
330 rqinit();
331
332
333 workq_init();
334
335
336 cpu_configure();
337
338
339 uvm_init_limits(p);
340
341
342 #if defined(NFSSERVER) || defined(NFSCLIENT)
343 nfs_init();
344 #endif
345 vfsinit();
346
347
348 initclocks();
349
350
351 KERNEL_PROC_LOCK(p);
352
353 #ifdef SYSVSHM
354
355 shminit();
356 #endif
357
358 #ifdef SYSVSEM
359
360 seminit();
361 #endif
362
363 #ifdef SYSVMSG
364
365 msginit();
366 #endif
367
368
369 randomattach();
370 for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
371 if (pdev->pdev_count > 0)
372 (*pdev->pdev_attach)(pdev->pdev_count);
373
374 #ifdef CRYPTO
375 swcr_init();
376 #endif
377
378
379
380
381
382 s = splnet();
383 ifinit();
384 domaininit();
385 if_attachdomain();
386 splx(s);
387
388 #ifdef GPROF
389
390 kmstartup();
391 #endif
392
393 #if !defined(NO_PROPOLICE)
394 {
395 volatile long newguard[8];
396 int i;
397
398 arc4random_bytes((long *)newguard, sizeof(newguard));
399
400 for (i = sizeof(__guard)/sizeof(__guard[0]) - 1; i; i--)
401 __guard[i] = newguard[i];
402 }
403 #endif
404
405
406 init_exec();
407
408
409 scheduler_start();
410
411
412
413
414
415
416
417
418
419
420 if (fork1(p, SIGCHLD, FORK_FORK, NULL, 0, start_init, NULL, NULL,
421 &initproc))
422 panic("fork init");
423
424
425
426
427
428 kthread_run_deferred_queue();
429
430
431
432
433
434
435
436 while (config_pending)
437 (void) tsleep((void *)&config_pending, PWAIT, "cfpend", 0);
438
439 dostartuphooks();
440
441 #if NSOFTRAID > 0
442 config_rootfound("softraid", NULL);
443 #endif
444
445
446 diskconf();
447
448
449 if (vfs_mountroot())
450 panic("cannot mount root");
451 CIRCLEQ_FIRST(&mountlist)->mnt_flag |= MNT_ROOTFS;
452
453
454 if (VFS_ROOT(CIRCLEQ_FIRST(&mountlist), &rootvnode))
455 panic("cannot find root vnode");
456 p->p_fd->fd_cdir = rootvnode;
457 VREF(p->p_fd->fd_cdir);
458 VOP_UNLOCK(rootvnode, 0, p);
459 p->p_fd->fd_rdir = NULL;
460
461
462
463
464
465
466 initproc->p_fd->fd_cdir = rootvnode;
467 VREF(initproc->p_fd->fd_cdir);
468 initproc->p_fd->fd_rdir = NULL;
469
470
471
472
473
474
475 #ifdef __HAVE_TIMECOUNTER
476 microtime(&boottime);
477 #else
478 boottime = mono_time = time;
479 #endif
480 LIST_FOREACH(p, &allproc, p_list) {
481 p->p_stats->p_start = boottime;
482 microuptime(&p->p_cpu->ci_schedstate.spc_runtime);
483 p->p_rtime.tv_sec = p->p_rtime.tv_usec = 0;
484 }
485
486 uvm_swap_init();
487
488
489 if (kthread_create(uvm_pageout, NULL, NULL, "pagedaemon"))
490 panic("fork pagedaemon");
491
492
493 if (kthread_create(start_reaper, NULL, NULL, "reaper"))
494 panic("fork reaper");
495
496
497 if (kthread_create(start_cleaner, NULL, NULL, "cleaner"))
498 panic("fork cleaner");
499
500
501 if (kthread_create(start_update, NULL, NULL, "update"))
502 panic("fork update");
503
504
505 if (kthread_create(uvm_aiodone_daemon, NULL, NULL, "aiodoned"))
506 panic("fork aiodoned");
507
508 #ifdef CRYPTO
509
510 if (kthread_create(start_crypto, NULL, NULL, "crypto"))
511 panic("crypto thread");
512 #endif
513
514 microtime(&rtv);
515 srandom((u_long)(rtv.tv_sec ^ rtv.tv_usec));
516
517 randompid = 1;
518
519 #if defined(MULTIPROCESSOR)
520
521 cpu_boot_secondary_processors();
522 #endif
523
524 domountroothooks();
525
526
527
528
529 start_init_exec = 1;
530 wakeup((void *)&start_init_exec);
531
532
533 uvm_scheduler();
534
535 }
536
537
538
539
540 static char *initpaths[] = {
541 "/sbin/init",
542 "/sbin/oinit",
543 "/sbin/init.bak",
544 NULL,
545 };
546
547 void
548 check_console(struct proc *p)
549 {
550 struct nameidata nd;
551 int error;
552
553 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/console", p);
554 error = namei(&nd);
555 if (error) {
556 if (error == ENOENT)
557 printf("warning: /dev/console does not exist\n");
558 else
559 printf("warning: /dev/console error %d\n", error);
560 } else
561 vrele(nd.ni_vp);
562 }
563
564
565
566
567
568 void
569 start_init(void *arg)
570 {
571 struct proc *p = arg;
572 vaddr_t addr;
573 struct sys_execve_args
574
575
576
577 args;
578 int options, error;
579 long i;
580 register_t retval[2];
581 char flags[4], *flagsp;
582 char **pathp, *path, *ucp, **uap, *arg0, *arg1 = NULL;
583
584
585
586
587
588
589
590
591 while (start_init_exec == 0)
592 (void) tsleep((void *)&start_init_exec, PWAIT, "initexec", 0);
593
594 check_console(p);
595
596
597
598
599 #ifdef MACHINE_STACK_GROWS_UP
600 addr = USRSTACK;
601 #else
602 addr = USRSTACK - PAGE_SIZE;
603 #endif
604 if (uvm_map(&p->p_vmspace->vm_map, &addr, PAGE_SIZE,
605 NULL, UVM_UNKNOWN_OFFSET, 0,
606 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_ALL, UVM_INH_COPY,
607 UVM_ADV_NORMAL, UVM_FLAG_FIXED|UVM_FLAG_OVERLAY|UVM_FLAG_COPYONW)))
608 panic("init: couldn't allocate argument space");
609 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
610
611 for (pathp = &initpaths[0]; (path = *pathp) != NULL; pathp++) {
612 #ifdef MACHINE_STACK_GROWS_UP
613 ucp = (char *)addr;
614 #else
615 ucp = (char *)(addr + PAGE_SIZE);
616 #endif
617
618
619
620 flagsp = flags;
621 *flagsp++ = '-';
622 options = 0;
623
624 if (boothowto & RB_SINGLE) {
625 *flagsp++ = 's';
626 options = 1;
627 }
628 #ifdef notyet
629 if (boothowto & RB_FASTBOOT) {
630 *flagsp++ = 'f';
631 options = 1;
632 }
633 #endif
634
635
636
637
638 if (options != 0) {
639 *flagsp++ = '\0';
640 i = flagsp - flags;
641 #ifdef DEBUG
642 printf("init: copying out flags `%s' %d\n", flags, i);
643 #endif
644 #ifdef MACHINE_STACK_GROWS_UP
645 arg1 = ucp;
646 (void)copyout((caddr_t)flags, (caddr_t)ucp, i);
647 ucp += i;
648 #else
649 (void)copyout((caddr_t)flags, (caddr_t)(ucp -= i), i);
650 arg1 = ucp;
651 #endif
652 }
653
654
655
656
657 i = strlen(path) + 1;
658 #ifdef DEBUG
659 printf("init: copying out path `%s' %d\n", path, i);
660 #endif
661 #ifdef MACHINE_STACK_GROWS_UP
662 arg0 = ucp;
663 (void)copyout((caddr_t)path, (caddr_t)ucp, i);
664 ucp += i;
665 ucp = (caddr_t)ALIGN((u_long)ucp);
666 uap = (char **)ucp + 3;
667 #else
668 (void)copyout((caddr_t)path, (caddr_t)(ucp -= i), i);
669 arg0 = ucp;
670 uap = (char **)((u_long)ucp & ~ALIGNBYTES);
671 #endif
672
673
674
675
676 i = 0;
677 copyout(&i, (caddr_t)--uap, sizeof(register_t));
678 if (options != 0)
679 copyout(&arg1, (caddr_t)--uap, sizeof(register_t));
680 copyout(&arg0, (caddr_t)--uap, sizeof(register_t));
681
682
683
684
685 SCARG(&args, path) = arg0;
686 SCARG(&args, argp) = uap;
687 SCARG(&args, envp) = NULL;
688
689
690
691
692
693 if ((error = sys_execve(p, &args, retval)) == 0) {
694 KERNEL_PROC_UNLOCK(p);
695 return;
696 }
697 if (error != ENOENT)
698 printf("exec %s: error %d\n", path, error);
699 }
700 printf("init: not found\n");
701 panic("no init");
702 }
703
704 void
705 start_update(void *arg)
706 {
707 sched_sync(curproc);
708
709 }
710
711 void
712 start_cleaner(void *arg)
713 {
714 buf_daemon(curproc);
715
716 }
717
718 void
719 start_reaper(void *arg)
720 {
721 reaper();
722
723 }
724
725 #ifdef CRYPTO
726 void
727 start_crypto(void *arg)
728 {
729 crypto_thread();
730
731 }
732 #endif