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