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