1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1995 Terrence R. Lambert |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993 |
---|
8 | * The Regents of the University of California. All rights reserved. |
---|
9 | * (c) UNIX System Laboratories, Inc. |
---|
10 | * All or some portions of this file are derived from material licensed |
---|
11 | * to the University of California by American Telephone and Telegraph |
---|
12 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
---|
13 | * the permission of UNIX System Laboratories, Inc. |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * 3. All advertising materials mentioning features or use of this software |
---|
24 | * must display the following acknowledgement: |
---|
25 | * This product includes software developed by the University of |
---|
26 | * California, Berkeley and its contributors. |
---|
27 | * 4. Neither the name of the University nor the names of its contributors |
---|
28 | * may be used to endorse or promote products derived from this software |
---|
29 | * without specific prior written permission. |
---|
30 | * |
---|
31 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
32 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
34 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
35 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
39 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
40 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
41 | * SUCH DAMAGE. |
---|
42 | * |
---|
43 | * @(#)init_main.c 8.9 (Berkeley) 1/21/94 |
---|
44 | */ |
---|
45 | |
---|
46 | #include <sys/cdefs.h> |
---|
47 | __FBSDID("$FreeBSD$"); |
---|
48 | |
---|
49 | #include <rtems/bsd/local/opt_ddb.h> |
---|
50 | #include <rtems/bsd/local/opt_init_path.h> |
---|
51 | |
---|
52 | #include <rtems/bsd/sys/param.h> |
---|
53 | #include <sys/kernel.h> |
---|
54 | #include <sys/exec.h> |
---|
55 | #include <sys/file.h> |
---|
56 | #include <sys/filedesc.h> |
---|
57 | #include <sys/jail.h> |
---|
58 | #include <sys/ktr.h> |
---|
59 | #include <rtems/bsd/sys/lock.h> |
---|
60 | #include <sys/loginclass.h> |
---|
61 | #include <sys/mount.h> |
---|
62 | #include <sys/mutex.h> |
---|
63 | #include <sys/syscallsubr.h> |
---|
64 | #include <sys/sysctl.h> |
---|
65 | #include <sys/proc.h> |
---|
66 | #include <sys/racct.h> |
---|
67 | #include <sys/resourcevar.h> |
---|
68 | #include <sys/systm.h> |
---|
69 | #include <sys/signalvar.h> |
---|
70 | #include <sys/vnode.h> |
---|
71 | #include <sys/sysent.h> |
---|
72 | #include <sys/reboot.h> |
---|
73 | #include <sys/sched.h> |
---|
74 | #include <sys/sx.h> |
---|
75 | #include <sys/sysproto.h> |
---|
76 | #include <sys/vmmeter.h> |
---|
77 | #include <rtems/bsd/sys/unistd.h> |
---|
78 | #include <sys/malloc.h> |
---|
79 | #include <sys/conf.h> |
---|
80 | #include <rtems/bsd/sys/cpuset.h> |
---|
81 | |
---|
82 | #include <machine/cpu.h> |
---|
83 | |
---|
84 | #include <security/audit/audit.h> |
---|
85 | #include <security/mac/mac_framework.h> |
---|
86 | |
---|
87 | #include <vm/vm.h> |
---|
88 | #include <vm/vm_param.h> |
---|
89 | #include <vm/pmap.h> |
---|
90 | #include <vm/vm_map.h> |
---|
91 | #include <sys/copyright.h> |
---|
92 | |
---|
93 | #include <ddb/ddb.h> |
---|
94 | #include <ddb/db_sym.h> |
---|
95 | |
---|
96 | void mi_startup(void); /* Should be elsewhere */ |
---|
97 | |
---|
98 | #ifndef __rtems__ |
---|
99 | /* Components of the first process -- never freed. */ |
---|
100 | static struct session session0; |
---|
101 | static struct pgrp pgrp0; |
---|
102 | struct proc proc0; |
---|
103 | struct thread thread0 __aligned(16); |
---|
104 | struct vmspace vmspace0; |
---|
105 | struct proc *initproc; |
---|
106 | |
---|
107 | int boothowto = 0; /* initialized so that it can be patched */ |
---|
108 | SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0, |
---|
109 | "Boot control flags, passed from loader"); |
---|
110 | int bootverbose; |
---|
111 | SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0, |
---|
112 | "Control the output of verbose kernel messages"); |
---|
113 | |
---|
114 | /* |
---|
115 | * This ensures that there is at least one entry so that the sysinit_set |
---|
116 | * symbol is not undefined. A sybsystem ID of SI_SUB_DUMMY is never |
---|
117 | * executed. |
---|
118 | */ |
---|
119 | SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL); |
---|
120 | |
---|
121 | /* |
---|
122 | * The sysinit table itself. Items are checked off as the are run. |
---|
123 | * If we want to register new sysinit types, add them to newsysinit. |
---|
124 | */ |
---|
125 | #endif /* __rtems__ */ |
---|
126 | SET_DECLARE(sysinit_set, struct sysinit); |
---|
127 | #ifndef __rtems__ |
---|
128 | struct sysinit **sysinit, **sysinit_end; |
---|
129 | struct sysinit **newsysinit, **newsysinit_end; |
---|
130 | |
---|
131 | /* |
---|
132 | * Merge a new sysinit set into the current set, reallocating it if |
---|
133 | * necessary. This can only be called after malloc is running. |
---|
134 | */ |
---|
135 | void |
---|
136 | sysinit_add(struct sysinit **set, struct sysinit **set_end) |
---|
137 | { |
---|
138 | struct sysinit **newset; |
---|
139 | struct sysinit **sipp; |
---|
140 | struct sysinit **xipp; |
---|
141 | int count; |
---|
142 | |
---|
143 | count = set_end - set; |
---|
144 | if (newsysinit) |
---|
145 | count += newsysinit_end - newsysinit; |
---|
146 | else |
---|
147 | count += sysinit_end - sysinit; |
---|
148 | newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT); |
---|
149 | if (newset == NULL) |
---|
150 | panic("cannot malloc for sysinit"); |
---|
151 | xipp = newset; |
---|
152 | if (newsysinit) |
---|
153 | for (sipp = newsysinit; sipp < newsysinit_end; sipp++) |
---|
154 | *xipp++ = *sipp; |
---|
155 | else |
---|
156 | for (sipp = sysinit; sipp < sysinit_end; sipp++) |
---|
157 | *xipp++ = *sipp; |
---|
158 | for (sipp = set; sipp < set_end; sipp++) |
---|
159 | *xipp++ = *sipp; |
---|
160 | if (newsysinit) |
---|
161 | free(newsysinit, M_TEMP); |
---|
162 | newsysinit = newset; |
---|
163 | newsysinit_end = newset + count; |
---|
164 | } |
---|
165 | #endif /* __rtems__ */ |
---|
166 | |
---|
167 | #if defined (DDB) && defined(VERBOSE_SYSINIT) |
---|
168 | static const char * |
---|
169 | symbol_name(vm_offset_t va, db_strategy_t strategy) |
---|
170 | { |
---|
171 | const char *name; |
---|
172 | c_db_sym_t sym; |
---|
173 | db_expr_t offset; |
---|
174 | |
---|
175 | if (va == 0) |
---|
176 | return (NULL); |
---|
177 | sym = db_search_symbol(va, strategy, &offset); |
---|
178 | if (offset != 0) |
---|
179 | return (NULL); |
---|
180 | db_symbol_values(sym, &name, NULL); |
---|
181 | return (name); |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | /* |
---|
186 | * System startup; initialize the world, create process 0, mount root |
---|
187 | * filesystem, and fork to create init and pagedaemon. Most of the |
---|
188 | * hard work is done in the lower-level initialization routines including |
---|
189 | * startup(), which does memory initialization and autoconfiguration. |
---|
190 | * |
---|
191 | * This allows simple addition of new kernel subsystems that require |
---|
192 | * boot time initialization. It also allows substitution of subsystem |
---|
193 | * (for instance, a scheduler, kernel profiler, or VM system) by object |
---|
194 | * module. Finally, it allows for optional "kernel threads". |
---|
195 | */ |
---|
196 | void |
---|
197 | mi_startup(void) |
---|
198 | { |
---|
199 | |
---|
200 | register struct sysinit **sipp; /* system initialization*/ |
---|
201 | register struct sysinit **xipp; /* interior loop of sort*/ |
---|
202 | register struct sysinit *save; /* bubble*/ |
---|
203 | #ifdef __rtems__ |
---|
204 | struct sysinit **sysinit = NULL; |
---|
205 | struct sysinit **sysinit_end = NULL; |
---|
206 | #endif /* __rtems__ */ |
---|
207 | |
---|
208 | #if defined(VERBOSE_SYSINIT) |
---|
209 | int last; |
---|
210 | int verbose; |
---|
211 | #endif |
---|
212 | |
---|
213 | #ifndef __rtems__ |
---|
214 | if (boothowto & RB_VERBOSE) |
---|
215 | bootverbose++; |
---|
216 | #endif /* __rtems__ */ |
---|
217 | |
---|
218 | if (sysinit == NULL) { |
---|
219 | sysinit = SET_BEGIN(sysinit_set); |
---|
220 | sysinit_end = SET_LIMIT(sysinit_set); |
---|
221 | } |
---|
222 | |
---|
223 | #ifndef __rtems__ |
---|
224 | restart: |
---|
225 | #endif /* __rtems__ */ |
---|
226 | /* |
---|
227 | * Perform a bubble sort of the system initialization objects by |
---|
228 | * their subsystem (primary key) and order (secondary key). |
---|
229 | */ |
---|
230 | for (sipp = sysinit; sipp < sysinit_end; sipp++) { |
---|
231 | for (xipp = sipp + 1; xipp < sysinit_end; xipp++) { |
---|
232 | if ((*sipp)->subsystem < (*xipp)->subsystem || |
---|
233 | ((*sipp)->subsystem == (*xipp)->subsystem && |
---|
234 | (*sipp)->order <= (*xipp)->order)) |
---|
235 | continue; /* skip*/ |
---|
236 | save = *sipp; |
---|
237 | *sipp = *xipp; |
---|
238 | *xipp = save; |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | #if defined(VERBOSE_SYSINIT) |
---|
243 | last = SI_SUB_COPYRIGHT; |
---|
244 | verbose = 0; |
---|
245 | #if !defined(DDB) |
---|
246 | printf("VERBOSE_SYSINIT: DDB not enabled, symbol lookups disabled.\n"); |
---|
247 | #endif |
---|
248 | #endif |
---|
249 | |
---|
250 | /* |
---|
251 | * Traverse the (now) ordered list of system initialization tasks. |
---|
252 | * Perform each task, and continue on to the next task. |
---|
253 | * |
---|
254 | * The last item on the list is expected to be the scheduler, |
---|
255 | * which will not return. |
---|
256 | */ |
---|
257 | for (sipp = sysinit; sipp < sysinit_end; sipp++) { |
---|
258 | |
---|
259 | if ((*sipp)->subsystem == SI_SUB_DUMMY) |
---|
260 | continue; /* skip dummy task(s)*/ |
---|
261 | |
---|
262 | if ((*sipp)->subsystem == SI_SUB_DONE) |
---|
263 | continue; |
---|
264 | |
---|
265 | #if defined(VERBOSE_SYSINIT) |
---|
266 | if ((*sipp)->subsystem > last) { |
---|
267 | verbose = 1; |
---|
268 | last = (*sipp)->subsystem; |
---|
269 | printf("subsystem %x\n", last); |
---|
270 | } |
---|
271 | if (verbose) { |
---|
272 | #if defined(DDB) |
---|
273 | const char *func, *data; |
---|
274 | |
---|
275 | func = symbol_name((vm_offset_t)(*sipp)->func, |
---|
276 | DB_STGY_PROC); |
---|
277 | data = symbol_name((vm_offset_t)(*sipp)->udata, |
---|
278 | DB_STGY_ANY); |
---|
279 | if (func != NULL && data != NULL) |
---|
280 | printf(" %s(&%s)... ", func, data); |
---|
281 | else if (func != NULL) |
---|
282 | printf(" %s(%p)... ", func, (*sipp)->udata); |
---|
283 | else |
---|
284 | #endif |
---|
285 | printf(" %p(%p)... ", (*sipp)->func, |
---|
286 | (*sipp)->udata); |
---|
287 | } |
---|
288 | #endif |
---|
289 | |
---|
290 | /* Call function */ |
---|
291 | (*((*sipp)->func))((*sipp)->udata); |
---|
292 | |
---|
293 | #if defined(VERBOSE_SYSINIT) |
---|
294 | if (verbose) |
---|
295 | printf("done.\n"); |
---|
296 | #endif |
---|
297 | |
---|
298 | /* Check off the one we're just done */ |
---|
299 | (*sipp)->subsystem = SI_SUB_DONE; |
---|
300 | |
---|
301 | #ifndef __rtems__ |
---|
302 | /* Check if we've installed more sysinit items via KLD */ |
---|
303 | if (newsysinit != NULL) { |
---|
304 | if (sysinit != SET_BEGIN(sysinit_set)) |
---|
305 | free(sysinit, M_TEMP); |
---|
306 | sysinit = newsysinit; |
---|
307 | sysinit_end = newsysinit_end; |
---|
308 | newsysinit = NULL; |
---|
309 | newsysinit_end = NULL; |
---|
310 | goto restart; |
---|
311 | } |
---|
312 | #endif /* __rtems__ */ |
---|
313 | } |
---|
314 | |
---|
315 | #ifndef __rtems__ |
---|
316 | panic("Shouldn't get here!"); |
---|
317 | /* NOTREACHED*/ |
---|
318 | #endif /* __rtems__ */ |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | #ifndef __rtems__ |
---|
323 | /* |
---|
324 | *************************************************************************** |
---|
325 | **** |
---|
326 | **** The following SYSINIT's belong elsewhere, but have not yet |
---|
327 | **** been moved. |
---|
328 | **** |
---|
329 | *************************************************************************** |
---|
330 | */ |
---|
331 | static void |
---|
332 | print_caddr_t(void *data) |
---|
333 | { |
---|
334 | printf("%s", (char *)data); |
---|
335 | } |
---|
336 | |
---|
337 | static void |
---|
338 | print_version(void *data __unused) |
---|
339 | { |
---|
340 | int len; |
---|
341 | |
---|
342 | /* Strip a trailing newline from version. */ |
---|
343 | len = strlen(version); |
---|
344 | while (len > 0 && version[len - 1] == '\n') |
---|
345 | len--; |
---|
346 | printf("%.*s %s\n", len, version, machine); |
---|
347 | printf("%s\n", compiler_version); |
---|
348 | } |
---|
349 | |
---|
350 | SYSINIT(announce, SI_SUB_COPYRIGHT, SI_ORDER_FIRST, print_caddr_t, |
---|
351 | copyright); |
---|
352 | SYSINIT(trademark, SI_SUB_COPYRIGHT, SI_ORDER_SECOND, print_caddr_t, |
---|
353 | trademark); |
---|
354 | SYSINIT(version, SI_SUB_COPYRIGHT, SI_ORDER_THIRD, print_version, NULL); |
---|
355 | |
---|
356 | #ifdef WITNESS |
---|
357 | static char wit_warn[] = |
---|
358 | "WARNING: WITNESS option enabled, expect reduced performance.\n"; |
---|
359 | SYSINIT(witwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 1, |
---|
360 | print_caddr_t, wit_warn); |
---|
361 | SYSINIT(witwarn2, SI_SUB_RUN_SCHEDULER, SI_ORDER_THIRD + 1, |
---|
362 | print_caddr_t, wit_warn); |
---|
363 | #endif |
---|
364 | |
---|
365 | #ifdef DIAGNOSTIC |
---|
366 | static char diag_warn[] = |
---|
367 | "WARNING: DIAGNOSTIC option enabled, expect reduced performance.\n"; |
---|
368 | SYSINIT(diagwarn, SI_SUB_COPYRIGHT, SI_ORDER_THIRD + 2, |
---|
369 | print_caddr_t, diag_warn); |
---|
370 | SYSINIT(diagwarn2, SI_SUB_RUN_SCHEDULER, SI_ORDER_THIRD + 2, |
---|
371 | print_caddr_t, diag_warn); |
---|
372 | #endif |
---|
373 | |
---|
374 | static int |
---|
375 | null_fetch_syscall_args(struct thread *td __unused, |
---|
376 | struct syscall_args *sa __unused) |
---|
377 | { |
---|
378 | |
---|
379 | panic("null_fetch_syscall_args"); |
---|
380 | } |
---|
381 | |
---|
382 | static void |
---|
383 | null_set_syscall_retval(struct thread *td __unused, int error __unused) |
---|
384 | { |
---|
385 | |
---|
386 | panic("null_set_syscall_retval"); |
---|
387 | } |
---|
388 | |
---|
389 | struct sysentvec null_sysvec = { |
---|
390 | .sv_size = 0, |
---|
391 | .sv_table = NULL, |
---|
392 | .sv_mask = 0, |
---|
393 | .sv_sigsize = 0, |
---|
394 | .sv_sigtbl = NULL, |
---|
395 | .sv_errsize = 0, |
---|
396 | .sv_errtbl = NULL, |
---|
397 | .sv_transtrap = NULL, |
---|
398 | .sv_fixup = NULL, |
---|
399 | .sv_sendsig = NULL, |
---|
400 | .sv_sigcode = NULL, |
---|
401 | .sv_szsigcode = NULL, |
---|
402 | .sv_prepsyscall = NULL, |
---|
403 | .sv_name = "null", |
---|
404 | .sv_coredump = NULL, |
---|
405 | .sv_imgact_try = NULL, |
---|
406 | .sv_minsigstksz = 0, |
---|
407 | .sv_pagesize = PAGE_SIZE, |
---|
408 | .sv_minuser = VM_MIN_ADDRESS, |
---|
409 | .sv_maxuser = VM_MAXUSER_ADDRESS, |
---|
410 | .sv_usrstack = USRSTACK, |
---|
411 | .sv_psstrings = PS_STRINGS, |
---|
412 | .sv_stackprot = VM_PROT_ALL, |
---|
413 | .sv_copyout_strings = NULL, |
---|
414 | .sv_setregs = NULL, |
---|
415 | .sv_fixlimit = NULL, |
---|
416 | .sv_maxssiz = NULL, |
---|
417 | .sv_flags = 0, |
---|
418 | .sv_set_syscall_retval = null_set_syscall_retval, |
---|
419 | .sv_fetch_syscall_args = null_fetch_syscall_args, |
---|
420 | .sv_syscallnames = NULL, |
---|
421 | .sv_schedtail = NULL, |
---|
422 | }; |
---|
423 | #endif /* __rtems__ */ |
---|
424 | |
---|
425 | /* |
---|
426 | *************************************************************************** |
---|
427 | **** |
---|
428 | **** The two following SYSINIT's are proc0 specific glue code. I am not |
---|
429 | **** convinced that they can not be safely combined, but their order of |
---|
430 | **** operation has been maintained as the same as the original init_main.c |
---|
431 | **** for right now. |
---|
432 | **** |
---|
433 | **** These probably belong in init_proc.c or kern_proc.c, since they |
---|
434 | **** deal with proc0 (the fork template process). |
---|
435 | **** |
---|
436 | *************************************************************************** |
---|
437 | */ |
---|
438 | /* ARGSUSED*/ |
---|
439 | static void |
---|
440 | proc0_init(void *dummy __unused) |
---|
441 | { |
---|
442 | #ifndef __rtems__ |
---|
443 | struct proc *p; |
---|
444 | struct thread *td; |
---|
445 | vm_paddr_t pageablemem; |
---|
446 | int i; |
---|
447 | |
---|
448 | GIANT_REQUIRED; |
---|
449 | p = &proc0; |
---|
450 | td = &thread0; |
---|
451 | |
---|
452 | /* |
---|
453 | * Initialize magic number and osrel. |
---|
454 | */ |
---|
455 | p->p_magic = P_MAGIC; |
---|
456 | p->p_osrel = osreldate; |
---|
457 | |
---|
458 | /* |
---|
459 | * Initialize thread and process structures. |
---|
460 | */ |
---|
461 | procinit(); /* set up proc zone */ |
---|
462 | threadinit(); /* set up UMA zones */ |
---|
463 | |
---|
464 | /* |
---|
465 | * Initialise scheduler resources. |
---|
466 | * Add scheduler specific parts to proc, thread as needed. |
---|
467 | */ |
---|
468 | schedinit(); /* scheduler gets its house in order */ |
---|
469 | /* |
---|
470 | * Initialize sleep queue hash table |
---|
471 | */ |
---|
472 | sleepinit(); |
---|
473 | |
---|
474 | /* |
---|
475 | * additional VM structures |
---|
476 | */ |
---|
477 | vm_init2(); |
---|
478 | |
---|
479 | /* |
---|
480 | * Create process 0 (the swapper). |
---|
481 | */ |
---|
482 | LIST_INSERT_HEAD(&allproc, p, p_list); |
---|
483 | LIST_INSERT_HEAD(PIDHASH(0), p, p_hash); |
---|
484 | mtx_init(&pgrp0.pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); |
---|
485 | p->p_pgrp = &pgrp0; |
---|
486 | LIST_INSERT_HEAD(PGRPHASH(0), &pgrp0, pg_hash); |
---|
487 | LIST_INIT(&pgrp0.pg_members); |
---|
488 | LIST_INSERT_HEAD(&pgrp0.pg_members, p, p_pglist); |
---|
489 | |
---|
490 | pgrp0.pg_session = &session0; |
---|
491 | mtx_init(&session0.s_mtx, "session", NULL, MTX_DEF); |
---|
492 | refcount_init(&session0.s_count, 1); |
---|
493 | session0.s_leader = p; |
---|
494 | |
---|
495 | p->p_sysent = &null_sysvec; |
---|
496 | p->p_flag = P_SYSTEM | P_INMEM; |
---|
497 | p->p_state = PRS_NORMAL; |
---|
498 | knlist_init_mtx(&p->p_klist, &p->p_mtx); |
---|
499 | STAILQ_INIT(&p->p_ktr); |
---|
500 | p->p_nice = NZERO; |
---|
501 | /* pid_max cannot be greater than PID_MAX */ |
---|
502 | td->td_tid = PID_MAX + 1; |
---|
503 | LIST_INSERT_HEAD(TIDHASH(td->td_tid), td, td_hash); |
---|
504 | td->td_state = TDS_RUNNING; |
---|
505 | td->td_pri_class = PRI_TIMESHARE; |
---|
506 | td->td_user_pri = PUSER; |
---|
507 | td->td_base_user_pri = PUSER; |
---|
508 | td->td_lend_user_pri = PRI_MAX; |
---|
509 | td->td_priority = PVM; |
---|
510 | td->td_base_pri = PVM; |
---|
511 | td->td_oncpu = 0; |
---|
512 | td->td_flags = TDF_INMEM|TDP_KTHREAD; |
---|
513 | td->td_cpuset = cpuset_thread0(); |
---|
514 | prison0.pr_cpuset = cpuset_ref(td->td_cpuset); |
---|
515 | p->p_peers = 0; |
---|
516 | p->p_leader = p; |
---|
517 | |
---|
518 | |
---|
519 | strncpy(p->p_comm, "kernel", sizeof (p->p_comm)); |
---|
520 | strncpy(td->td_name, "swapper", sizeof (td->td_name)); |
---|
521 | |
---|
522 | callout_init_mtx(&p->p_itcallout, &p->p_mtx, 0); |
---|
523 | callout_init_mtx(&p->p_limco, &p->p_mtx, 0); |
---|
524 | callout_init(&td->td_slpcallout, CALLOUT_MPSAFE); |
---|
525 | |
---|
526 | /* Create credentials. */ |
---|
527 | p->p_ucred = crget(); |
---|
528 | p->p_ucred->cr_ngroups = 1; /* group 0 */ |
---|
529 | p->p_ucred->cr_uidinfo = uifind(0); |
---|
530 | p->p_ucred->cr_ruidinfo = uifind(0); |
---|
531 | p->p_ucred->cr_prison = &prison0; |
---|
532 | p->p_ucred->cr_loginclass = loginclass_find("default"); |
---|
533 | #ifdef AUDIT |
---|
534 | audit_cred_kproc0(p->p_ucred); |
---|
535 | #endif |
---|
536 | #ifdef MAC |
---|
537 | mac_cred_create_swapper(p->p_ucred); |
---|
538 | #endif |
---|
539 | td->td_ucred = crhold(p->p_ucred); |
---|
540 | |
---|
541 | /* Create sigacts. */ |
---|
542 | p->p_sigacts = sigacts_alloc(); |
---|
543 | |
---|
544 | /* Initialize signal state for process 0. */ |
---|
545 | siginit(&proc0); |
---|
546 | |
---|
547 | /* Create the file descriptor table. */ |
---|
548 | p->p_fd = fdinit(NULL); |
---|
549 | p->p_fdtol = NULL; |
---|
550 | |
---|
551 | /* Create the limits structures. */ |
---|
552 | p->p_limit = lim_alloc(); |
---|
553 | for (i = 0; i < RLIM_NLIMITS; i++) |
---|
554 | p->p_limit->pl_rlimit[i].rlim_cur = |
---|
555 | p->p_limit->pl_rlimit[i].rlim_max = RLIM_INFINITY; |
---|
556 | p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_cur = |
---|
557 | p->p_limit->pl_rlimit[RLIMIT_NOFILE].rlim_max = maxfiles; |
---|
558 | p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_cur = |
---|
559 | p->p_limit->pl_rlimit[RLIMIT_NPROC].rlim_max = maxproc; |
---|
560 | p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz; |
---|
561 | p->p_limit->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz; |
---|
562 | p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz; |
---|
563 | p->p_limit->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz; |
---|
564 | /* Cast to avoid overflow on i386/PAE. */ |
---|
565 | pageablemem = ptoa((vm_paddr_t)cnt.v_free_count); |
---|
566 | p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_cur = |
---|
567 | p->p_limit->pl_rlimit[RLIMIT_RSS].rlim_max = pageablemem; |
---|
568 | p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_cur = pageablemem / 3; |
---|
569 | p->p_limit->pl_rlimit[RLIMIT_MEMLOCK].rlim_max = pageablemem; |
---|
570 | p->p_cpulimit = RLIM_INFINITY; |
---|
571 | |
---|
572 | /* Initialize resource accounting structures. */ |
---|
573 | racct_create(&p->p_racct); |
---|
574 | |
---|
575 | p->p_stats = pstats_alloc(); |
---|
576 | |
---|
577 | /* Allocate a prototype map so we have something to fork. */ |
---|
578 | pmap_pinit0(vmspace_pmap(&vmspace0)); |
---|
579 | p->p_vmspace = &vmspace0; |
---|
580 | vmspace0.vm_refcnt = 1; |
---|
581 | |
---|
582 | /* |
---|
583 | * proc0 is not expected to enter usermode, so there is no special |
---|
584 | * handling for sv_minuser here, like is done for exec_new_vmspace(). |
---|
585 | */ |
---|
586 | vm_map_init(&vmspace0.vm_map, vmspace_pmap(&vmspace0), |
---|
587 | p->p_sysent->sv_minuser, p->p_sysent->sv_maxuser); |
---|
588 | |
---|
589 | /* |
---|
590 | * Call the init and ctor for the new thread and proc. We wait |
---|
591 | * to do this until all other structures are fairly sane. |
---|
592 | */ |
---|
593 | EVENTHANDLER_INVOKE(process_init, p); |
---|
594 | EVENTHANDLER_INVOKE(thread_init, td); |
---|
595 | EVENTHANDLER_INVOKE(process_ctor, p); |
---|
596 | EVENTHANDLER_INVOKE(thread_ctor, td); |
---|
597 | |
---|
598 | /* |
---|
599 | * Charge root for one process. |
---|
600 | */ |
---|
601 | (void)chgproccnt(p->p_ucred->cr_ruidinfo, 1, 0); |
---|
602 | PROC_LOCK(p); |
---|
603 | racct_add_force(p, RACCT_NPROC, 1); |
---|
604 | PROC_UNLOCK(p); |
---|
605 | #endif /* __rtems__ */ |
---|
606 | } |
---|
607 | SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL); |
---|
608 | |
---|
609 | #ifndef __rtems__ |
---|
610 | /* ARGSUSED*/ |
---|
611 | static void |
---|
612 | proc0_post(void *dummy __unused) |
---|
613 | { |
---|
614 | struct timespec ts; |
---|
615 | struct proc *p; |
---|
616 | struct rusage ru; |
---|
617 | struct thread *td; |
---|
618 | |
---|
619 | /* |
---|
620 | * Now we can look at the time, having had a chance to verify the |
---|
621 | * time from the filesystem. Pretend that proc0 started now. |
---|
622 | */ |
---|
623 | sx_slock(&allproc_lock); |
---|
624 | FOREACH_PROC_IN_SYSTEM(p) { |
---|
625 | microuptime(&p->p_stats->p_start); |
---|
626 | PROC_SLOCK(p); |
---|
627 | rufetch(p, &ru); /* Clears thread stats */ |
---|
628 | PROC_SUNLOCK(p); |
---|
629 | p->p_rux.rux_runtime = 0; |
---|
630 | p->p_rux.rux_uticks = 0; |
---|
631 | p->p_rux.rux_sticks = 0; |
---|
632 | p->p_rux.rux_iticks = 0; |
---|
633 | FOREACH_THREAD_IN_PROC(p, td) { |
---|
634 | td->td_runtime = 0; |
---|
635 | } |
---|
636 | } |
---|
637 | sx_sunlock(&allproc_lock); |
---|
638 | PCPU_SET(switchtime, cpu_ticks()); |
---|
639 | PCPU_SET(switchticks, ticks); |
---|
640 | |
---|
641 | /* |
---|
642 | * Give the ``random'' number generator a thump. |
---|
643 | */ |
---|
644 | nanotime(&ts); |
---|
645 | srandom(ts.tv_sec ^ ts.tv_nsec); |
---|
646 | } |
---|
647 | SYSINIT(p0post, SI_SUB_INTRINSIC_POST, SI_ORDER_FIRST, proc0_post, NULL); |
---|
648 | |
---|
649 | static void |
---|
650 | random_init(void *dummy __unused) |
---|
651 | { |
---|
652 | |
---|
653 | /* |
---|
654 | * After CPU has been started we have some randomness on most |
---|
655 | * platforms via get_cyclecount(). For platforms that don't |
---|
656 | * we will reseed random(9) in proc0_post() as well. |
---|
657 | */ |
---|
658 | srandom(get_cyclecount()); |
---|
659 | } |
---|
660 | SYSINIT(random, SI_SUB_RANDOM, SI_ORDER_FIRST, random_init, NULL); |
---|
661 | |
---|
662 | /* |
---|
663 | *************************************************************************** |
---|
664 | **** |
---|
665 | **** The following SYSINIT's and glue code should be moved to the |
---|
666 | **** respective files on a per subsystem basis. |
---|
667 | **** |
---|
668 | *************************************************************************** |
---|
669 | */ |
---|
670 | |
---|
671 | |
---|
672 | /* |
---|
673 | *************************************************************************** |
---|
674 | **** |
---|
675 | **** The following code probably belongs in another file, like |
---|
676 | **** kern/init_init.c. |
---|
677 | **** |
---|
678 | *************************************************************************** |
---|
679 | */ |
---|
680 | |
---|
681 | /* |
---|
682 | * List of paths to try when searching for "init". |
---|
683 | */ |
---|
684 | static char init_path[MAXPATHLEN] = |
---|
685 | #ifdef INIT_PATH |
---|
686 | __XSTRING(INIT_PATH); |
---|
687 | #else |
---|
688 | "/sbin/init:/sbin/oinit:/sbin/init.bak:/rescue/init:/stand/sysinstall"; |
---|
689 | #endif |
---|
690 | SYSCTL_STRING(_kern, OID_AUTO, init_path, CTLFLAG_RD, init_path, 0, |
---|
691 | "Path used to search the init process"); |
---|
692 | |
---|
693 | /* |
---|
694 | * Shutdown timeout of init(8). |
---|
695 | * Unused within kernel, but used to control init(8), hence do not remove. |
---|
696 | */ |
---|
697 | #ifndef INIT_SHUTDOWN_TIMEOUT |
---|
698 | #define INIT_SHUTDOWN_TIMEOUT 120 |
---|
699 | #endif |
---|
700 | static int init_shutdown_timeout = INIT_SHUTDOWN_TIMEOUT; |
---|
701 | SYSCTL_INT(_kern, OID_AUTO, init_shutdown_timeout, |
---|
702 | CTLFLAG_RW, &init_shutdown_timeout, 0, "Shutdown timeout of init(8). " |
---|
703 | "Unused within kernel, but used to control init(8)"); |
---|
704 | |
---|
705 | /* |
---|
706 | * Start the initial user process; try exec'ing each pathname in init_path. |
---|
707 | * The program is invoked with one argument containing the boot flags. |
---|
708 | */ |
---|
709 | static void |
---|
710 | start_init(void *dummy) |
---|
711 | { |
---|
712 | vm_offset_t addr; |
---|
713 | struct execve_args args; |
---|
714 | int options, error; |
---|
715 | char *var, *path, *next, *s; |
---|
716 | char *ucp, **uap, *arg0, *arg1; |
---|
717 | struct thread *td; |
---|
718 | struct proc *p; |
---|
719 | |
---|
720 | mtx_lock(&Giant); |
---|
721 | |
---|
722 | GIANT_REQUIRED; |
---|
723 | |
---|
724 | td = curthread; |
---|
725 | p = td->td_proc; |
---|
726 | |
---|
727 | vfs_mountroot(); |
---|
728 | |
---|
729 | /* |
---|
730 | * Need just enough stack to hold the faked-up "execve()" arguments. |
---|
731 | */ |
---|
732 | addr = p->p_sysent->sv_usrstack - PAGE_SIZE; |
---|
733 | if (vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, PAGE_SIZE, |
---|
734 | FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != 0) |
---|
735 | panic("init: couldn't allocate argument space"); |
---|
736 | p->p_vmspace->vm_maxsaddr = (caddr_t)addr; |
---|
737 | p->p_vmspace->vm_ssize = 1; |
---|
738 | |
---|
739 | if ((var = getenv("init_path")) != NULL) { |
---|
740 | strlcpy(init_path, var, sizeof(init_path)); |
---|
741 | freeenv(var); |
---|
742 | } |
---|
743 | |
---|
744 | for (path = init_path; *path != '\0'; path = next) { |
---|
745 | while (*path == ':') |
---|
746 | path++; |
---|
747 | if (*path == '\0') |
---|
748 | break; |
---|
749 | for (next = path; *next != '\0' && *next != ':'; next++) |
---|
750 | /* nothing */ ; |
---|
751 | if (bootverbose) |
---|
752 | printf("start_init: trying %.*s\n", (int)(next - path), |
---|
753 | path); |
---|
754 | |
---|
755 | /* |
---|
756 | * Move out the boot flag argument. |
---|
757 | */ |
---|
758 | options = 0; |
---|
759 | ucp = (char *)p->p_sysent->sv_usrstack; |
---|
760 | (void)subyte(--ucp, 0); /* trailing zero */ |
---|
761 | if (boothowto & RB_SINGLE) { |
---|
762 | (void)subyte(--ucp, 's'); |
---|
763 | options = 1; |
---|
764 | } |
---|
765 | #ifdef notyet |
---|
766 | if (boothowto & RB_FASTBOOT) { |
---|
767 | (void)subyte(--ucp, 'f'); |
---|
768 | options = 1; |
---|
769 | } |
---|
770 | #endif |
---|
771 | |
---|
772 | #ifdef BOOTCDROM |
---|
773 | (void)subyte(--ucp, 'C'); |
---|
774 | options = 1; |
---|
775 | #endif |
---|
776 | |
---|
777 | if (options == 0) |
---|
778 | (void)subyte(--ucp, '-'); |
---|
779 | (void)subyte(--ucp, '-'); /* leading hyphen */ |
---|
780 | arg1 = ucp; |
---|
781 | |
---|
782 | /* |
---|
783 | * Move out the file name (also arg 0). |
---|
784 | */ |
---|
785 | (void)subyte(--ucp, 0); |
---|
786 | for (s = next - 1; s >= path; s--) |
---|
787 | (void)subyte(--ucp, *s); |
---|
788 | arg0 = ucp; |
---|
789 | |
---|
790 | /* |
---|
791 | * Move out the arg pointers. |
---|
792 | */ |
---|
793 | uap = (char **)((intptr_t)ucp & ~(sizeof(intptr_t)-1)); |
---|
794 | (void)suword((caddr_t)--uap, (long)0); /* terminator */ |
---|
795 | (void)suword((caddr_t)--uap, (long)(intptr_t)arg1); |
---|
796 | (void)suword((caddr_t)--uap, (long)(intptr_t)arg0); |
---|
797 | |
---|
798 | /* |
---|
799 | * Point at the arguments. |
---|
800 | */ |
---|
801 | args.fname = arg0; |
---|
802 | args.argv = uap; |
---|
803 | args.envv = NULL; |
---|
804 | |
---|
805 | /* |
---|
806 | * Now try to exec the program. If can't for any reason |
---|
807 | * other than it doesn't exist, complain. |
---|
808 | * |
---|
809 | * Otherwise, return via fork_trampoline() all the way |
---|
810 | * to user mode as init! |
---|
811 | */ |
---|
812 | if ((error = sys_execve(td, &args)) == 0) { |
---|
813 | mtx_unlock(&Giant); |
---|
814 | return; |
---|
815 | } |
---|
816 | if (error != ENOENT) |
---|
817 | printf("exec %.*s: error %d\n", (int)(next - path), |
---|
818 | path, error); |
---|
819 | } |
---|
820 | printf("init: not found in path %s\n", init_path); |
---|
821 | panic("no init"); |
---|
822 | } |
---|
823 | |
---|
824 | /* |
---|
825 | * Like kproc_create(), but runs in it's own address space. |
---|
826 | * We do this early to reserve pid 1. |
---|
827 | * |
---|
828 | * Note special case - do not make it runnable yet. Other work |
---|
829 | * in progress will change this more. |
---|
830 | */ |
---|
831 | static void |
---|
832 | create_init(const void *udata __unused) |
---|
833 | { |
---|
834 | struct ucred *newcred, *oldcred; |
---|
835 | int error; |
---|
836 | |
---|
837 | error = fork1(&thread0, RFFDG | RFPROC | RFSTOPPED, 0, &initproc, |
---|
838 | NULL, 0); |
---|
839 | if (error) |
---|
840 | panic("cannot fork init: %d\n", error); |
---|
841 | KASSERT(initproc->p_pid == 1, ("create_init: initproc->p_pid != 1")); |
---|
842 | /* divorce init's credentials from the kernel's */ |
---|
843 | newcred = crget(); |
---|
844 | PROC_LOCK(initproc); |
---|
845 | initproc->p_flag |= P_SYSTEM | P_INMEM; |
---|
846 | oldcred = initproc->p_ucred; |
---|
847 | crcopy(newcred, oldcred); |
---|
848 | #ifdef MAC |
---|
849 | mac_cred_create_init(newcred); |
---|
850 | #endif |
---|
851 | #ifdef AUDIT |
---|
852 | audit_cred_proc1(newcred); |
---|
853 | #endif |
---|
854 | initproc->p_ucred = newcred; |
---|
855 | PROC_UNLOCK(initproc); |
---|
856 | crfree(oldcred); |
---|
857 | cred_update_thread(FIRST_THREAD_IN_PROC(initproc)); |
---|
858 | cpu_set_fork_handler(FIRST_THREAD_IN_PROC(initproc), start_init, NULL); |
---|
859 | } |
---|
860 | SYSINIT(init, SI_SUB_CREATE_INIT, SI_ORDER_FIRST, create_init, NULL); |
---|
861 | |
---|
862 | /* |
---|
863 | * Make it runnable now. |
---|
864 | */ |
---|
865 | static void |
---|
866 | kick_init(const void *udata __unused) |
---|
867 | { |
---|
868 | struct thread *td; |
---|
869 | |
---|
870 | td = FIRST_THREAD_IN_PROC(initproc); |
---|
871 | thread_lock(td); |
---|
872 | TD_SET_CAN_RUN(td); |
---|
873 | sched_add(td, SRQ_BORING); |
---|
874 | thread_unlock(td); |
---|
875 | } |
---|
876 | SYSINIT(kickinit, SI_SUB_KTHREAD_INIT, SI_ORDER_FIRST, kick_init, NULL); |
---|
877 | #endif /* __rtems__ */ |
---|