1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1988 AT&T */ 28 /* All Rights Reserved */ 29 30 #include <sys/types.h> 31 #include <sys/param.h> 32 #include <sys/sysmacros.h> 33 #include <sys/pcb.h> 34 #include <sys/systm.h> 35 #include <sys/signal.h> 36 #include <sys/cred.h> 37 #include <sys/user.h> 38 #include <sys/vfs.h> 39 #include <sys/vnode.h> 40 #include <sys/proc.h> 41 #include <sys/time.h> 42 #include <sys/file.h> 43 #include <sys/priocntl.h> 44 #include <sys/procset.h> 45 #include <sys/disp.h> 46 #include <sys/callo.h> 47 #include <sys/callb.h> 48 #include <sys/debug.h> 49 #include <sys/conf.h> 50 #include <sys/bootconf.h> 51 #include <sys/utsname.h> 52 #include <sys/cmn_err.h> 53 #include <sys/vmparam.h> 54 #include <sys/modctl.h> 55 #include <sys/vm.h> 56 #include <sys/callb.h> 57 #include <sys/ddi_timer.h> 58 #include <sys/kmem.h> 59 #include <sys/vmem.h> 60 #include <sys/cpuvar.h> 61 #include <sys/cladm.h> 62 #include <sys/corectl.h> 63 #include <sys/exec.h> 64 #include <sys/syscall.h> 65 #include <sys/reboot.h> 66 #include <sys/task.h> 67 #include <sys/exacct.h> 68 #include <sys/autoconf.h> 69 #include <sys/errorq.h> 70 #include <sys/class.h> 71 #include <sys/stack.h> 72 #include <sys/brand.h> 73 #include <sys/mmapobj.h> 74 75 #include <vm/as.h> 76 #include <vm/seg_kmem.h> 77 #include <sys/dc_ki.h> 78 79 #include <c2/audit.h> 80 #include <sys/bootprops.h> 81 82 /* well known processes */ 83 proc_t *proc_sched; /* memory scheduler */ 84 proc_t *proc_init; /* init */ 85 proc_t *proc_pageout; /* pageout daemon */ 86 proc_t *proc_fsflush; /* fsflush daemon */ 87 88 pgcnt_t maxmem; /* Maximum available memory in pages. */ 89 pgcnt_t freemem; /* Current available memory in pages. */ 90 int interrupts_unleashed; /* set when we do the first spl0() */ 91 92 kmem_cache_t *process_cache; /* kmem cache for proc structures */ 93 94 /* 95 * Indicates whether the auditing module (c2audit) is loaded. Possible 96 * values are: 97 * 0 - c2audit module is excluded in /etc/system and cannot be loaded 98 * 1 - c2audit module is not loaded but can be anytime 99 * 2 - c2audit module is loaded 100 */ 101 int audit_active = C2AUDIT_DISABLED; 102 103 /* 104 * Process 0's lwp directory and lwpid hash table. 105 */ 106 lwpdir_t p0_lwpdir[2]; 107 tidhash_t p0_tidhash[2]; 108 lwpent_t p0_lep; 109 110 /* 111 * Machine-independent initialization code 112 * Called from cold start routine as 113 * soon as a stack and segmentation 114 * have been established. 115 * Functions: 116 * clear and free user core 117 * turn on clock 118 * hand craft 0th process 119 * call all initialization routines 120 * fork - process 0 to schedule 121 * - process 1 execute bootstrap 122 * - process 2 to page out 123 * create system threads 124 */ 125 126 int cluster_bootflags = 0; 127 128 void 129 cluster_wrapper(void) 130 { 131 cluster(); 132 panic("cluster() returned"); 133 } 134 135 char initname[INITNAME_SZ] = "/sbin/init"; /* also referenced by zone0 */ 136 char initargs[BOOTARGS_MAX] = ""; /* also referenced by zone0 */ 137 extern int64_t lwp_sigmask(int, uint_t, uint_t); 138 139 /* 140 * Construct a stack for init containing the arguments to it, then 141 * pass control to exec_common. 142 */ 143 int 144 exec_init(const char *initpath, const char *args) 145 { 146 caddr32_t ucp; 147 caddr32_t *uap; 148 caddr32_t *argv; 149 caddr32_t exec_fnamep; 150 char *scratchargs; 151 int i, sarg; 152 size_t argvlen, alen; 153 boolean_t in_arg; 154 int argc = 0; 155 int error = 0, count = 0; 156 proc_t *p = ttoproc(curthread); 157 klwp_t *lwp = ttolwp(curthread); 158 int brand_action; 159 160 if (args == NULL) 161 args = ""; 162 163 alen = strlen(initpath) + 1 + strlen(args) + 1; 164 scratchargs = kmem_alloc(alen, KM_SLEEP); 165 (void) snprintf(scratchargs, alen, "%s %s", initpath, args); 166 167 /* 168 * We do a quick two state parse of the string to sort out how big 169 * argc should be. 170 */ 171 in_arg = B_FALSE; 172 for (i = 0; i < strlen(scratchargs); i++) { 173 if (scratchargs[i] == ' ' || scratchargs[i] == '\0') { 174 if (in_arg) { 175 in_arg = B_FALSE; 176 argc++; 177 } 178 } else { 179 in_arg = B_TRUE; 180 } 181 } 182 argvlen = sizeof (caddr32_t) * (argc + 1); 183 argv = kmem_zalloc(argvlen, KM_SLEEP); 184 185 /* 186 * We pull off a bit of a hack here. We work our way through the 187 * args string, putting nulls at the ends of space delimited tokens 188 * (boot args don't support quoting at this time). Then we just 189 * copy the whole mess to userland in one go. In other words, we 190 * transform this: "init -s -r\0" into this on the stack: 191 * 192 * -0x00 \0 193 * -0x01 r 194 * -0x02 - <--------. 195 * -0x03 \0 | 196 * -0x04 s | 197 * -0x05 - <------. | 198 * -0x06 \0 | | 199 * -0x07 t | | 200 * -0x08 i | | 201 * -0x09 n | | 202 * -0x0a i <---. | | 203 * -0x10 NULL | | | (argv[3]) 204 * -0x14 -----|--|-' (argv[2]) 205 * -0x18 ------|--' (argv[1]) 206 * -0x1c -------' (argv[0]) 207 * 208 * Since we know the value of ucp at the beginning of this process, 209 * we can trivially compute the argv[] array which we also need to 210 * place in userland: argv[i] = ucp - sarg(i), where ucp is the 211 * stack ptr, and sarg is the string index of the start of the 212 * argument. 213 */ 214 ucp = (caddr32_t)(uintptr_t)p->p_usrstack; 215 216 argc = 0; 217 in_arg = B_FALSE; 218 sarg = 0; 219 220 for (i = 0; i < alen; i++) { 221 if (scratchargs[i] == ' ' || scratchargs[i] == '\0') { 222 if (in_arg == B_TRUE) { 223 in_arg = B_FALSE; 224 scratchargs[i] = '\0'; 225 argv[argc++] = ucp - (alen - sarg); 226 } 227 } else if (in_arg == B_FALSE) { 228 in_arg = B_TRUE; 229 sarg = i; 230 } 231 } 232 ucp -= alen; 233 error |= copyout(scratchargs, (caddr_t)(uintptr_t)ucp, alen); 234 235 uap = (caddr32_t *)P2ALIGN((uintptr_t)ucp, sizeof (caddr32_t)); 236 uap--; /* advance to be below the word we're in */ 237 uap -= (argc + 1); /* advance argc words down, plus one for NULL */ 238 error |= copyout(argv, uap, argvlen); 239 240 if (error != 0) { 241 zcmn_err(p->p_zone->zone_id, CE_WARN, 242 "Could not construct stack for init.\n"); 243 kmem_free(argv, argvlen); 244 kmem_free(scratchargs, alen); 245 return (EFAULT); 246 } 247 248 exec_fnamep = argv[0]; 249 kmem_free(argv, argvlen); 250 kmem_free(scratchargs, alen); 251 252 /* 253 * Point at the arguments. 254 */ 255 lwp->lwp_ap = lwp->lwp_arg; 256 lwp->lwp_arg[0] = (uintptr_t)exec_fnamep; 257 lwp->lwp_arg[1] = (uintptr_t)uap; 258 lwp->lwp_arg[2] = NULL; 259 curthread->t_post_sys = 1; 260 curthread->t_sysnum = SYS_execve; 261 262 /* 263 * If we are executing init from zsched, we may have inherited its 264 * parent process's signal mask. Clear it now so that we behave in 265 * the same way as when started from the global zone. 266 */ 267 (void) lwp_sigmask(SIG_UNBLOCK, 0xffffffff, 0xffffffff); 268 269 brand_action = ZONE_IS_BRANDED(p->p_zone) ? EBA_BRAND : EBA_NONE; 270 again: 271 error = exec_common((const char *)(uintptr_t)exec_fnamep, 272 (const char **)(uintptr_t)uap, NULL, brand_action); 273 274 /* 275 * Normally we would just set lwp_argsaved and t_post_sys and 276 * let post_syscall reset lwp_ap for us. Unfortunately, 277 * exec_init isn't always called from a system call. Instead 278 * of making a mess of trap_cleanup, we just reset the args 279 * pointer here. 280 */ 281 reset_syscall_args(); 282 283 switch (error) { 284 case 0: 285 return (0); 286 287 case ENOENT: 288 zcmn_err(p->p_zone->zone_id, CE_WARN, 289 "exec(%s) failed (file not found).\n", initpath); 290 return (ENOENT); 291 292 case EAGAIN: 293 case EINTR: 294 ++count; 295 if (count < 5) { 296 zcmn_err(p->p_zone->zone_id, CE_WARN, 297 "exec(%s) failed with errno %d. Retrying...\n", 298 initpath, error); 299 goto again; 300 } 301 } 302 303 zcmn_err(p->p_zone->zone_id, CE_WARN, 304 "exec(%s) failed with errno %d.", initpath, error); 305 return (error); 306 } 307 308 /* 309 * This routine does all of the common setup for invoking init; global 310 * and non-global zones employ this routine for the functionality which is 311 * in common. 312 * 313 * This program (init, presumably) must be a 32-bit process. 314 */ 315 int 316 start_init_common() 317 { 318 proc_t *p = curproc; 319 ASSERT_STACK_ALIGNED(); 320 p->p_zone->zone_proc_initpid = p->p_pid; 321 322 p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0; 323 p->p_usrstack = (caddr_t)USRSTACK32; 324 p->p_model = DATAMODEL_ILP32; 325 p->p_stkprot = PROT_ZFOD & ~PROT_EXEC; 326 p->p_datprot = PROT_ZFOD & ~PROT_EXEC; 327 p->p_stk_ctl = INT32_MAX; 328 329 p->p_as = as_alloc(); 330 p->p_as->a_proc = p; 331 p->p_as->a_userlimit = (caddr_t)USERLIMIT32; 332 (void) hat_setup(p->p_as->a_hat, HAT_INIT); 333 334 init_core(); 335 336 init_mstate(curthread, LMS_SYSTEM); 337 return (exec_init(p->p_zone->zone_initname, p->p_zone->zone_bootargs)); 338 } 339 340 /* 341 * Start the initial user process for the global zone; once running, if 342 * init should subsequently fail, it will be automatically be caught in the 343 * exit(2) path, and restarted by restart_init(). 344 */ 345 static void 346 start_init(void) 347 { 348 proc_init = curproc; 349 350 ASSERT(curproc->p_zone->zone_initname != NULL); 351 352 if (start_init_common() != 0) 353 halt("unix: Could not start init"); 354 lwp_rtt(); 355 } 356 357 void 358 main(void) 359 { 360 proc_t *p = ttoproc(curthread); /* &p0 */ 361 int (**initptr)(); 362 extern void sched(); 363 extern void fsflush(); 364 extern int (*init_tbl[])(); 365 extern int (*mp_init_tbl[])(); 366 extern id_t syscid, defaultcid; 367 extern int swaploaded; 368 extern int netboot; 369 extern ib_boot_prop_t *iscsiboot_prop; 370 extern void vm_init(void); 371 extern void cbe_init_pre(void); 372 extern void cbe_init(void); 373 extern void clock_tick_init_pre(void); 374 extern void clock_tick_init_post(void); 375 extern void clock_init(void); 376 extern void physio_bufs_init(void); 377 extern void pm_cfb_setup_intr(void); 378 extern int pm_adjust_timestamps(dev_info_t *, void *); 379 extern void start_other_cpus(int); 380 extern void sysevent_evc_thrinit(); 381 #if defined(__x86) 382 extern void fastboot_post_startup(void); 383 #endif 384 /* 385 * In the horrible world of x86 in-lines, you can't get symbolic 386 * structure offsets a la genassym. This assertion is here so 387 * that the next poor slob who innocently changes the offset of 388 * cpu_thread doesn't waste as much time as I just did finding 389 * out that it's hard-coded in i86/ml/i86.il. Similarly for 390 * curcpup. You're welcome. 391 */ 392 ASSERT(CPU == CPU->cpu_self); 393 ASSERT(curthread == CPU->cpu_thread); 394 ASSERT_STACK_ALIGNED(); 395 396 /* 397 * Setup root lgroup and leaf lgroup for CPU 0 398 */ 399 lgrp_init(LGRP_INIT_STAGE2); 400 401 /* 402 * Once 'startup()' completes, the thread_reaper() daemon would be 403 * created(in thread_init()). After that, it is safe to create threads 404 * that could exit. These exited threads will get reaped. 405 */ 406 startup(); 407 segkmem_gc(); 408 callb_init(); 409 cbe_init_pre(); /* x86 must initialize gethrtimef before timer_init */ 410 timer_init(); /* timer must be initialized before cyclic starts */ 411 cbe_init(); 412 callout_init(); /* callout table MUST be init'd after cyclics */ 413 clock_tick_init_pre(); 414 clock_init(); 415 416 /* 417 * On some platforms, clkinitf() changes the timing source that 418 * gethrtime_unscaled() uses to generate timestamps. cbe_init() calls 419 * clkinitf(), so re-initialize the microstate counters after the 420 * timesource has been chosen. 421 */ 422 init_mstate(&t0, LMS_SYSTEM); 423 init_cpu_mstate(CPU, CMS_SYSTEM); 424 425 /* 426 * May need to probe to determine latencies from CPU 0 after 427 * gethrtime() comes alive in cbe_init() and before enabling interrupts 428 * and copy and release any temporary memory allocated with BOP_ALLOC() 429 * before release_bootstrap() frees boot memory 430 */ 431 lgrp_init(LGRP_INIT_STAGE3); 432 433 /* 434 * Call all system initialization functions. 435 */ 436 for (initptr = &init_tbl[0]; *initptr; initptr++) 437 (**initptr)(); 438 /* 439 * Load iSCSI boot properties 440 */ 441 ld_ib_prop(); 442 /* 443 * initialize vm related stuff. 444 */ 445 vm_init(); 446 447 /* 448 * initialize buffer pool for raw I/O requests 449 */ 450 physio_bufs_init(); 451 452 ttolwp(curthread)->lwp_error = 0; /* XXX kludge for SCSI driver */ 453 454 /* 455 * Drop the interrupt level and allow interrupts. At this point 456 * the DDI guarantees that interrupts are enabled. 457 */ 458 (void) spl0(); 459 interrupts_unleashed = 1; 460 461 /* 462 * Create kmem cache for proc structures 463 */ 464 process_cache = kmem_cache_create("process_cache", sizeof (proc_t), 465 0, NULL, NULL, NULL, NULL, NULL, 0); 466 467 vfs_mountroot(); /* Mount the root file system */ 468 errorq_init(); /* after vfs_mountroot() so DDI root is ready */ 469 cpu_kstat_init(CPU); /* after vfs_mountroot() so TOD is valid */ 470 ddi_walk_devs(ddi_root_node(), pm_adjust_timestamps, NULL); 471 /* after vfs_mountroot() so hrestime is valid */ 472 473 post_startup(); 474 swaploaded = 1; 475 476 /* 477 * Initialize Solaris Audit Subsystem 478 */ 479 audit_init(); 480 481 /* 482 * Plumb the protocol modules and drivers only if we are not 483 * networked booted, in this case we already did it in rootconf(). 484 */ 485 if (netboot == 0 && iscsiboot_prop == NULL) 486 (void) strplumb(); 487 488 gethrestime(&PTOU(curproc)->u_start); 489 curthread->t_start = PTOU(curproc)->u_start.tv_sec; 490 p->p_mstart = gethrtime(); 491 492 /* 493 * Perform setup functions that can only be done after root 494 * and swap have been set up. 495 */ 496 consconfig(); 497 #ifndef __sparc 498 release_bootstrap(); 499 #endif 500 501 /* 502 * attach drivers with ddi-forceattach prop 503 * It must be done early enough to load hotplug drivers (e.g. 504 * pcmcia nexus) so that devices enumerated via hotplug is 505 * available before I/O subsystem is fully initialized. 506 */ 507 i_ddi_forceattach_drivers(); 508 509 /* 510 * Set the scan rate and other parameters of the paging subsystem. 511 */ 512 setupclock(0); 513 514 /* 515 * Initialize process 0's lwp directory and lwpid hash table. 516 */ 517 p->p_lwpdir = p->p_lwpfree = p0_lwpdir; 518 p->p_lwpdir->ld_next = p->p_lwpdir + 1; 519 p->p_lwpdir_sz = 2; 520 p->p_tidhash = p0_tidhash; 521 p->p_tidhash_sz = 2; 522 p0_lep.le_thread = curthread; 523 p0_lep.le_lwpid = curthread->t_tid; 524 p0_lep.le_start = curthread->t_start; 525 lwp_hash_in(p, &p0_lep, p0_tidhash, 2, 0); 526 527 /* 528 * Initialize extended accounting. 529 */ 530 exacct_init(); 531 532 /* 533 * Initialize threads of sysevent event channels 534 */ 535 sysevent_evc_thrinit(); 536 537 /* 538 * This must be done after post_startup() but before 539 * start_other_cpus() 540 */ 541 lgrp_init(LGRP_INIT_STAGE4); 542 543 /* 544 * Perform MP initialization, if any. 545 */ 546 start_other_cpus(0); 547 548 #ifdef __sparc 549 /* 550 * Release bootstrap here since PROM interfaces are 551 * used to start other CPUs above. 552 */ 553 release_bootstrap(); 554 #endif 555 556 /* 557 * Finish lgrp initialization after all CPUS are brought online. 558 */ 559 lgrp_init(LGRP_INIT_STAGE5); 560 561 /* 562 * After mp_init(), number of cpus are known (this is 563 * true for the time being, when there are actually 564 * hot pluggable cpus then this scheme would not do). 565 * Any per cpu initialization is done here. 566 */ 567 kmem_mp_init(); 568 vmem_update(NULL); 569 570 clock_tick_init_post(); 571 572 for (initptr = &mp_init_tbl[0]; *initptr; initptr++) 573 (**initptr)(); 574 575 /* 576 * These must be called after start_other_cpus 577 */ 578 pm_cfb_setup_intr(); 579 #if defined(__x86) 580 fastboot_post_startup(); 581 #endif 582 583 /* 584 * Make init process; enter scheduling loop with system process. 585 * 586 * Note that we manually assign the pids for these processes, for 587 * historical reasons. If more pre-assigned pids are needed, 588 * FAMOUS_PIDS will have to be updated. 589 */ 590 591 /* create init process */ 592 if (newproc(start_init, NULL, defaultcid, 59, NULL, 593 FAMOUS_PID_INIT)) 594 panic("main: unable to fork init."); 595 596 /* create pageout daemon */ 597 if (newproc(pageout, NULL, syscid, maxclsyspri - 1, NULL, 598 FAMOUS_PID_PAGEOUT)) 599 panic("main: unable to fork pageout()"); 600 601 /* create fsflush daemon */ 602 if (newproc(fsflush, NULL, syscid, minclsyspri, NULL, 603 FAMOUS_PID_FSFLUSH)) 604 panic("main: unable to fork fsflush()"); 605 606 /* create cluster process if we're a member of one */ 607 if (cluster_bootflags & CLUSTER_BOOTED) { 608 if (newproc(cluster_wrapper, NULL, syscid, minclsyspri, 609 NULL, 0)) { 610 panic("main: unable to fork cluster()"); 611 } 612 } 613 614 /* 615 * Create system threads (threads are associated with p0) 616 */ 617 618 /* create module uninstall daemon */ 619 /* BugID 1132273. If swapping over NFS need a bigger stack */ 620 (void) thread_create(NULL, 0, (void (*)())mod_uninstall_daemon, 621 NULL, 0, &p0, TS_RUN, minclsyspri); 622 623 (void) thread_create(NULL, 0, seg_pasync_thread, 624 NULL, 0, &p0, TS_RUN, minclsyspri); 625 626 pid_setmin(); 627 628 bcopy("sched", PTOU(curproc)->u_psargs, 6); 629 bcopy("sched", PTOU(curproc)->u_comm, 5); 630 sched(); 631 /* NOTREACHED */ 632 } 633