consistency with tty.c about value for "no pgrp"
[unix-history] / usr / src / sys / kern / init_main.c
CommitLineData
da7c5cc6 1/*
d7db4999 2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
2d9d8fa8 3 * All rights reserved.
da7c5cc6 4 *
2d9d8fa8
MK
5 * %sccs.include.redist.c%
6 *
2dd3075b 7 * @(#)init_main.c 7.41 (Berkeley) %G%
da7c5cc6 8 */
961945a8 9
94368568 10#include "param.h"
5e00df3b 11#include "filedesc.h"
94368568 12#include "kernel.h"
94368568
JB
13#include "mount.h"
14#include "map.h"
15#include "proc.h"
c040c6b1
MK
16#include "resourcevar.h"
17#include "signalvar.h"
18#include "systm.h"
475798b6 19#include "vnode.h"
94368568
JB
20#include "seg.h"
21#include "conf.h"
22#include "buf.h"
94368568 23#include "clist.h"
8fe87cbb 24#include "malloc.h"
94368568 25#include "protosw.h"
f378da4c 26#include "reboot.h"
c040c6b1
MK
27#include "user.h"
28
d301d150 29
d301d150 30#include "machine/cpu.h"
961945a8 31
d7db4999 32#include "vm/vm.h"
d7db4999 33
94d045fa
MK
34char copyright[] =
35"Copyright (c) 1982,1986,1989,1991 The Regents of the University of California.\nAll rights reserved.\n\n";
36
d7db4999
MK
37/*
38 * Components of process 0;
39 * never freed.
40 */
41struct session session0;
42struct pgrp pgrp0;
43struct proc proc0;
44struct pcred cred0;
c040c6b1 45struct filedesc0 filedesc0;
d7db4999
MK
46struct plimit limit0;
47struct vmspace vmspace0;
c040c6b1 48struct proc *curproc = &proc0;
d7db4999 49struct proc *initproc, *pageproc;
9d4095a1 50
28c7a4c5 51int cmask = CMASK;
f657c741 52extern struct user *proc0paddr;
475798b6 53extern int (*mountroot)();
362786f8 54
94d045fa
MK
55struct vnode *rootvp, *swapdev_vp;
56int boothowto;
57
c4708522 58/*
d7db4999
MK
59 * System startup; initialize the world, create process 0,
60 * mount root filesystem, and fork to create init and pagedaemon.
61 * Most of the hard work is done in the lower-level initialization
62 * routines including startup(), which does memory initialization
63 * and autoconfiguration.
c4708522 64 */
27bff27d 65main()
c4708522 66{
73248996 67 register int i;
dd4b582c 68 register struct proc *p;
c040c6b1 69 register struct filedesc0 *fdp;
d7db4999 70 int s, rval[2];
083baeb6 71
362786f8 72 /*
d7db4999
MK
73 * Initialize curproc before any possible traps/probes
74 * to simplify trap processing.
362786f8 75 */
d7db4999
MK
76 p = &proc0;
77 curproc = p;
9d4095a1 78 /*
d7db4999
MK
79 * Attempt to find console and initialize
80 * in case of early panic or other messages.
9d4095a1 81 */
d7db4999 82 consinit();
94d045fa 83 printf(copyright);
d7db4999 84
9d4095a1
KM
85 vm_mem_init();
86 kmeminit();
27bff27d 87 cpu_startup();
c4708522
BJ
88
89 /*
90 * set up system process 0 (swapper)
91 */
d7db4999
MK
92 p = &proc0;
93 curproc = p;
94
95 allproc = p;
96 p->p_prev = &allproc;
97 p->p_pgrp = &pgrp0;
98 pgrphash[0] = &pgrp0;
99 pgrp0.pg_mem = p;
100 pgrp0.pg_session = &session0;
101 session0.s_count = 1;
102 session0.s_leader = p;
103
104 p->p_flag = SLOAD|SSYS;
dd4b582c 105 p->p_stat = SRUN;
dd4b582c 106 p->p_nice = NZERO;
d7db4999
MK
107 bcopy("swapper", p->p_comm, sizeof ("swapper"));
108
9d4095a1 109 /*
d7db4999 110 * Setup credentials
14fda239 111 */
2dd3075b 112 cred0.p_refcnt = 1;
d7db4999
MK
113 p->p_cred = &cred0;
114 p->p_ucred = crget();
115 p->p_ucred->cr_ngroups = 1; /* group 0 */
fb1db32c 116
5e00df3b
KM
117 /*
118 * Create the file descriptor table for process 0.
119 */
d7db4999 120 fdp = &filedesc0;
c040c6b1
MK
121 p->p_fd = &fdp->fd_fd;
122 fdp->fd_fd.fd_refcnt = 1;
123 fdp->fd_fd.fd_cmask = cmask;
124 fdp->fd_fd.fd_ofiles = fdp->fd_dfiles;
125 fdp->fd_fd.fd_ofileflags = fdp->fd_dfileflags;
126 fdp->fd_fd.fd_nfiles = NDFILE;
d7db4999
MK
127
128 /*
129 * Set initial limits
130 */
131 p->p_limit = &limit0;
132 for (i = 0; i < sizeof(p->p_rlimit)/sizeof(p->p_rlimit[0]); i++)
133 limit0.pl_rlimit[i].rlim_cur =
134 limit0.pl_rlimit[i].rlim_max = RLIM_INFINITY;
135 limit0.pl_rlimit[RLIMIT_OFILE].rlim_cur = NOFILE;
136 limit0.pl_rlimit[RLIMIT_NPROC].rlim_cur = MAXUPRC;
137 limit0.p_refcnt = 1;
138
139 /*
140 * Allocate a prototype map so we have something to fork
141 */
142 p->p_vmspace = &vmspace0;
143 vmspace0.vm_refcnt = 1;
144 pmap_pinit(&vmspace0.vm_pmap);
145 vm_map_init(&p->p_vmspace->vm_map, round_page(VM_MIN_ADDRESS),
146 trunc_page(VM_MAX_ADDRESS), TRUE);
147 vmspace0.vm_map.pmap = &vmspace0.vm_pmap;
148 p->p_addr = proc0paddr; /* XXX */
149
150 /*
151 * We continue to place resource usage info
152 * and signal actions in the user struct so they're pageable.
153 */
f657c741
MK
154 p->p_stats = &p->p_addr->u_stats;
155 p->p_sigacts = &p->p_addr->u_sigacts;
d7db4999
MK
156
157 rqinit();
158
4898b838 159 /*
8ede3c1b
MK
160 * configure virtual memory system,
161 * set vm rlimits
4898b838 162 */
d7db4999 163 vm_init_limits(p);
8ede3c1b 164
475798b6 165 /*
85570e35
KM
166 * Initialize the file systems.
167 *
94d045fa 168 * Get vnodes for swapdev and rootdev.
475798b6 169 */
85570e35 170 vfsinit();
94d045fa 171 if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
475798b6
KM
172 panic("can't setup bdevvp's");
173
c70d0a8b 174 startrtclock();
5cb75b5d 175#if defined(vax)
f882447b
SL
176#include "kg.h"
177#if NKG > 0
d0ab60b1 178 startkgclock();
fb1db32c 179#endif
d0ab60b1 180#endif
c4708522
BJ
181
182 /*
d872f034 183 * Initialize tables, protocols, and set up well-known inodes.
c4708522 184 */
d872f034 185 mbinit();
a5829200 186 cinit();
933220e9
KM
187#ifdef SYSVSHM
188 shminit();
189#endif
730ff8ce
MK
190#include "sl.h"
191#if NSL > 0
192 slattach(); /* XXX */
193#endif
933220e9 194#include "loop.h"
4f4caf05
BJ
195#if NLOOP > 0
196 loattach(); /* XXX */
197#endif
4f083fd7
SL
198 /*
199 * Block reception of incoming packets
200 * until protocols have been initialized.
201 */
202 s = splimp();
fbf9a431 203 ifinit();
b7892118 204 domaininit();
4f083fd7 205 splx(s);
d7db4999 206
e6254ffb
BJ
207#ifdef GPROF
208 kmstartup();
209#endif
9d6d37ce 210
d7db4999 211 /* kick off timeout driven events by calling first time */
27b91f59
BJ
212 roundrobin();
213 schedcpu();
362786f8 214 enablertclock(); /* enable realtime clock interrupts */
27b91f59 215
d7db4999
MK
216 /*
217 * Set up the root file system and vnode.
218 */
475798b6
KM
219 if ((*mountroot)())
220 panic("cannot mount root");
221 /*
222 * Get vnode for '/'.
c040c6b1 223 * Setup rootdir and fdp->fd_fd.fd_cdir to point to it.
475798b6
KM
224 */
225 if (VFS_ROOT(rootfs, &rootdir))
226 panic("cannot find root vnode");
c040c6b1
MK
227 fdp->fd_fd.fd_cdir = rootdir;
228 VREF(fdp->fd_fd.fd_cdir);
3ca85ace 229 VOP_UNLOCK(rootdir);
c040c6b1 230 fdp->fd_fd.fd_rdir = NULL;
d7db4999 231 swapinit();
c4708522 232
28c7a4c5 233 /*
d7db4999
MK
234 * Now can look at time, having had a chance
235 * to verify the time from the file system.
28c7a4c5 236 */
d7db4999 237 boottime = p->p_stats->p_start = time;
28c7a4c5 238
d7db4999
MK
239 /*
240 * make init process
241 */
242 siginit(p);
243 if (fork(p, (void *) NULL, rval))
244 panic("fork init");
245 if (rval[1]) {
83cf086b
MK
246 static char initflags[] = "-sf";
247 char *ip = initflags + 1;
9d4095a1 248 vm_offset_t addr = 0;
94d045fa
MK
249 extern int icode[]; /* user init code */
250 extern int szicode; /* size of icode */
9d4095a1 251
d7db4999
MK
252 /*
253 * Now in process 1. Set init flags into icode,
254 * get a minimal address space, copy out "icode",
255 * and return to it to do an exec of init.
256 */
257 p = curproc;
258 initproc = p;
d7db4999
MK
259 if (boothowto&RB_SINGLE)
260 *ip++ = 's';
261#ifdef notyet
262 if (boothowto&RB_FASTBOOT)
263 *ip++ = 'f';
d7db4999 264#endif
83cf086b 265 *ip++ = '\0';
d7db4999
MK
266
267 if (vm_allocate(&p->p_vmspace->vm_map, &addr,
83cf086b
MK
268 round_page(szicode + sizeof(initflags)), FALSE) != 0 ||
269 addr != 0)
9d4095a1
KM
270 panic("init: couldn't allocate at zero");
271
272 /* need just enough stack to exec from */
f657c741 273 addr = trunc_page(USRSTACK - PAGE_SIZE);
d7db4999
MK
274 if (vm_allocate(&p->p_vmspace->vm_map, &addr,
275 PAGE_SIZE, FALSE) != KERN_SUCCESS)
276 panic("vm_allocate init stack");
277 p->p_vmspace->vm_maxsaddr = (caddr_t)addr;
28c7a4c5 278 (void) copyout((caddr_t)icode, (caddr_t)0, (unsigned)szicode);
83cf086b 279 (void) copyout(initflags, (caddr_t)szicode, sizeof(initflags));
d7db4999 280 return; /* returns to icode */
28c7a4c5 281 }
d7db4999 282
c4708522 283 /*
9d4095a1 284 * Start up pageout daemon (process 2).
c4708522 285 */
d7db4999
MK
286 if (fork(p, (void *) NULL, rval))
287 panic("fork pager");
288 if (rval[1]) {
289 /*
290 * Now in process 2.
291 */
292 p = curproc;
293 pageproc = p;
294 p->p_flag |= SLOAD|SSYS; /* XXX */
295 bcopy("pagedaemon", curproc->p_comm, sizeof ("pagedaemon"));
9d4095a1 296 vm_pageout();
b7892118 297 /*NOTREACHED*/
c4708522
BJ
298 }
299
300 /*
c4708522
BJ
301 * enter scheduling loop
302 */
c4708522
BJ
303 sched();
304}