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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/sysmacros.h> 36 #include <sys/signal.h> 37 #include <sys/cred.h> 38 #include <sys/policy.h> 39 #include <sys/user.h> 40 #include <sys/systm.h> 41 #include <sys/cpuvar.h> 42 #include <sys/vfs.h> 43 #include <sys/vnode.h> 44 #include <sys/file.h> 45 #include <sys/errno.h> 46 #include <sys/time.h> 47 #include <sys/proc.h> 48 #include <sys/cmn_err.h> 49 #include <sys/acct.h> 50 #include <sys/tuneable.h> 51 #include <sys/class.h> 52 #include <sys/kmem.h> 53 #include <sys/session.h> 54 #include <sys/ucontext.h> 55 #include <sys/stack.h> 56 #include <sys/procfs.h> 57 #include <sys/prsystm.h> 58 #include <sys/vmsystm.h> 59 #include <sys/vtrace.h> 60 #include <sys/debug.h> 61 #include <sys/shm_impl.h> 62 #include <sys/door_data.h> 63 #include <vm/as.h> 64 #include <vm/rm.h> 65 #include <c2/audit.h> 66 #include <sys/var.h> 67 #include <sys/schedctl.h> 68 #include <sys/utrap.h> 69 #include <sys/task.h> 70 #include <sys/resource.h> 71 #include <sys/cyclic.h> 72 #include <sys/lgrp.h> 73 #include <sys/rctl.h> 74 #include <sys/contract_impl.h> 75 #include <sys/contract/process_impl.h> 76 #include <sys/list.h> 77 #include <sys/dtrace.h> 78 #include <sys/pool.h> 79 #include <sys/zone.h> 80 #include <sys/sdt.h> 81 #include <sys/class.h> 82 #include <sys/corectl.h> 83 #include <sys/brand.h> 84 85 static int64_t cfork(int, int); 86 static int getproc(proc_t **, int); 87 static void fork_fail(proc_t *); 88 static void forklwp_fail(proc_t *); 89 90 int fork_fail_pending; 91 92 extern struct kmem_cache *process_cache; 93 94 /* 95 * forkall system call. 96 */ 97 int64_t 98 forkall(void) 99 { 100 return (cfork(0, 0)); 101 } 102 103 /* 104 * The parent is stopped until the child invokes relvm(). 105 */ 106 int64_t 107 vfork(void) 108 { 109 curthread->t_post_sys = 1; /* so vfwait() will be called */ 110 return (cfork(1, 1)); 111 } 112 113 /* 114 * fork1 system call 115 */ 116 int64_t 117 fork1(void) 118 { 119 return (cfork(0, 1)); 120 } 121 122 /* ARGSUSED */ 123 static int64_t 124 cfork(int isvfork, int isfork1) 125 { 126 proc_t *p = ttoproc(curthread); 127 struct as *as; 128 proc_t *cp, **orphpp; 129 klwp_t *clone; 130 kthread_t *t; 131 task_t *tk; 132 rval_t r; 133 int error; 134 int i; 135 rctl_set_t *dup_set; 136 rctl_alloc_gp_t *dup_gp; 137 rctl_entity_p_t e; 138 lwpdir_t *ldp; 139 lwpent_t *lep; 140 lwpent_t *clep; 141 142 /* 143 * fork is not supported for the /proc agent lwp. 144 */ 145 if (curthread == p->p_agenttp) { 146 error = ENOTSUP; 147 goto forkerr; 148 } 149 150 if ((error = secpolicy_basic_fork(CRED())) != 0) 151 goto forkerr; 152 153 /* 154 * If the calling lwp is doing a fork1() then the 155 * other lwps in this process are not duplicated and 156 * don't need to be held where their kernel stacks can be 157 * cloned. If doing forkall(), the process is held with 158 * SHOLDFORK, so that the lwps are at a point where their 159 * stacks can be copied which is on entry or exit from 160 * the kernel. 161 */ 162 if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) { 163 aston(curthread); 164 error = EINTR; 165 goto forkerr; 166 } 167 168 #if defined(__sparc) 169 /* 170 * Ensure that the user stack is fully constructed 171 * before creating the child process structure. 172 */ 173 (void) flush_user_windows_to_stack(NULL); 174 #endif 175 176 mutex_enter(&p->p_lock); 177 /* 178 * If this is vfork(), cancel any suspend request we might 179 * have gotten from some other thread via lwp_suspend(). 180 * Otherwise we could end up with a deadlock on return 181 * from the vfork() in both the parent and the child. 182 */ 183 if (isvfork) 184 curthread->t_proc_flag &= ~TP_HOLDLWP; 185 /* 186 * Prevent our resource set associations from being changed during fork. 187 */ 188 pool_barrier_enter(); 189 mutex_exit(&p->p_lock); 190 191 /* 192 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes. 193 */ 194 if (getproc(&cp, 0) < 0) { 195 mutex_enter(&p->p_lock); 196 pool_barrier_exit(); 197 continuelwps(p); 198 mutex_exit(&p->p_lock); 199 error = EAGAIN; 200 goto forkerr; 201 } 202 203 TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p); 204 205 /* 206 * Assign an address space to child 207 */ 208 if (isvfork) { 209 /* 210 * Clear any watched areas and remember the 211 * watched pages for restoring in vfwait(). 212 */ 213 as = p->p_as; 214 if (avl_numnodes(&as->a_wpage) != 0) { 215 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 216 as_clearwatch(as); 217 p->p_wpage = as->a_wpage; 218 avl_create(&as->a_wpage, wp_compare, 219 sizeof (struct watched_page), 220 offsetof(struct watched_page, wp_link)); 221 AS_LOCK_EXIT(as, &as->a_lock); 222 } 223 cp->p_as = as; 224 cp->p_flag |= SVFORK; 225 } else { 226 /* 227 * We need to hold P_PR_LOCK until the address space has 228 * been duplicated and we've had a chance to remove from the 229 * child any DTrace probes that were in the parent. Holding 230 * P_PR_LOCK prevents any new probes from being added and any 231 * extant probes from being removed. 232 */ 233 mutex_enter(&p->p_lock); 234 sprlock_proc(p); 235 p->p_flag |= SFORKING; 236 mutex_exit(&p->p_lock); 237 238 error = as_dup(p->p_as, &cp->p_as); 239 if (error != 0) { 240 fork_fail(cp); 241 mutex_enter(&pidlock); 242 orphpp = &p->p_orphan; 243 while (*orphpp != cp) 244 orphpp = &(*orphpp)->p_nextorph; 245 *orphpp = cp->p_nextorph; 246 if (p->p_child == cp) 247 p->p_child = cp->p_sibling; 248 if (cp->p_sibling) 249 cp->p_sibling->p_psibling = cp->p_psibling; 250 if (cp->p_psibling) 251 cp->p_psibling->p_sibling = cp->p_sibling; 252 mutex_enter(&cp->p_lock); 253 tk = cp->p_task; 254 task_detach(cp); 255 ASSERT(cp->p_pool->pool_ref > 0); 256 atomic_add_32(&cp->p_pool->pool_ref, -1); 257 mutex_exit(&cp->p_lock); 258 pid_exit(cp); 259 mutex_exit(&pidlock); 260 task_rele(tk); 261 262 mutex_enter(&p->p_lock); 263 p->p_flag &= ~SFORKING; 264 pool_barrier_exit(); 265 continuelwps(p); 266 sprunlock(p); 267 /* 268 * Preserve ENOMEM error condition but 269 * map all others to EAGAIN. 270 */ 271 error = (error == ENOMEM) ? ENOMEM : EAGAIN; 272 goto forkerr; 273 } 274 cp->p_as->a_proc = cp; 275 276 /* Duplicate parent's shared memory */ 277 if (p->p_segacct) 278 shmfork(p, cp); 279 280 /* 281 * Remove all DTrace tracepoints from the child process. We 282 * need to do this _before_ duplicating USDT providers since 283 * any associated probes may be immediately enabled. 284 */ 285 if (p->p_dtrace_count > 0) 286 dtrace_fasttrap_fork(p, cp); 287 288 /* 289 * Duplicate any helper actions and providers. The SFORKING 290 * we set above informs the code to enable USDT probes that 291 * sprlock() may fail because the child is being forked. 292 */ 293 if (p->p_dtrace_helpers != NULL) { 294 ASSERT(dtrace_helpers_fork != NULL); 295 (*dtrace_helpers_fork)(p, cp); 296 } 297 298 mutex_enter(&p->p_lock); 299 p->p_flag &= ~SFORKING; 300 sprunlock(p); 301 } 302 303 /* 304 * Duplicate parent's resource controls. 305 */ 306 dup_set = rctl_set_create(); 307 for (;;) { 308 dup_gp = rctl_set_dup_prealloc(p->p_rctls); 309 mutex_enter(&p->p_rctls->rcs_lock); 310 if (rctl_set_dup_ready(p->p_rctls, dup_gp)) 311 break; 312 mutex_exit(&p->p_rctls->rcs_lock); 313 rctl_prealloc_destroy(dup_gp); 314 } 315 e.rcep_p.proc = cp; 316 e.rcep_t = RCENTITY_PROCESS; 317 cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp, 318 RCD_DUP | RCD_CALLBACK); 319 mutex_exit(&p->p_rctls->rcs_lock); 320 321 rctl_prealloc_destroy(dup_gp); 322 323 /* 324 * Allocate the child's lwp directory and lwpid hash table. 325 */ 326 if (isfork1) 327 cp->p_lwpdir_sz = 2; 328 else 329 cp->p_lwpdir_sz = p->p_lwpdir_sz; 330 cp->p_lwpdir = cp->p_lwpfree = ldp = 331 kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP); 332 for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++) 333 ldp->ld_next = ldp + 1; 334 cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2; 335 cp->p_tidhash = 336 kmem_zalloc(cp->p_tidhash_sz * sizeof (lwpdir_t *), KM_SLEEP); 337 338 /* 339 * Duplicate parent's lwps. 340 * Mutual exclusion is not needed because the process is 341 * in the hold state and only the current lwp is running. 342 */ 343 klgrpset_clear(cp->p_lgrpset); 344 if (isfork1) { 345 clone = forklwp(ttolwp(curthread), cp, curthread->t_tid); 346 if (clone == NULL) 347 goto forklwperr; 348 /* 349 * Inherit only the lwp_wait()able flag, 350 * Daemon threads should not call fork1(), but oh well... 351 */ 352 lwptot(clone)->t_proc_flag |= 353 (curthread->t_proc_flag & TP_TWAIT); 354 } else { 355 /* this is forkall(), no one can be in lwp_wait() */ 356 ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0); 357 /* for each entry in the parent's lwp directory... */ 358 for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) { 359 klwp_t *clwp; 360 kthread_t *ct; 361 362 if ((lep = ldp->ld_entry) == NULL) 363 continue; 364 365 if ((t = lep->le_thread) != NULL) { 366 clwp = forklwp(ttolwp(t), cp, t->t_tid); 367 if (clwp == NULL) 368 goto forklwperr; 369 ct = lwptot(clwp); 370 /* 371 * Inherit lwp_wait()able and daemon flags. 372 */ 373 ct->t_proc_flag |= 374 (t->t_proc_flag & (TP_TWAIT|TP_DAEMON)); 375 /* 376 * Keep track of the clone of curthread to 377 * post return values through lwp_setrval(). 378 * Mark other threads for special treatment 379 * by lwp_rtt() / post_syscall(). 380 */ 381 if (t == curthread) 382 clone = clwp; 383 else 384 ct->t_flag |= T_FORKALL; 385 } else { 386 /* 387 * Replicate zombie lwps in the child. 388 */ 389 clep = kmem_zalloc(sizeof (*clep), KM_SLEEP); 390 clep->le_lwpid = lep->le_lwpid; 391 clep->le_start = lep->le_start; 392 lwp_hash_in(cp, clep); 393 } 394 } 395 } 396 397 /* 398 * Put new process in the parent's process contract, or put it 399 * in a new one if there is an active process template. Send a 400 * fork event (if requested) to whatever contract the child is 401 * a member of. Fails if the parent has been SIGKILLed. 402 */ 403 if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL) 404 goto forklwperr; 405 406 /* 407 * No fork failures occur beyond this point. 408 */ 409 410 cp->p_lwpid = p->p_lwpid; 411 if (!isfork1) { 412 cp->p_lwpdaemon = p->p_lwpdaemon; 413 cp->p_zombcnt = p->p_zombcnt; 414 /* 415 * If the parent's lwp ids have wrapped around, so have the 416 * child's. 417 */ 418 cp->p_flag |= p->p_flag & SLWPWRAP; 419 } 420 421 mutex_enter(&p->p_lock); 422 corectl_path_hold(cp->p_corefile = p->p_corefile); 423 corectl_content_hold(cp->p_content = p->p_content); 424 mutex_exit(&p->p_lock); 425 426 /* 427 * Duplicate process context ops, if any. 428 */ 429 if (p->p_pctx) 430 forkpctx(p, cp); 431 432 #ifdef __sparc 433 utrap_dup(p, cp); 434 #endif 435 /* 436 * If the child process has been marked to stop on exit 437 * from this fork, arrange for all other lwps to stop in 438 * sympathy with the active lwp. 439 */ 440 if (PTOU(cp)->u_systrap && 441 prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) { 442 mutex_enter(&cp->p_lock); 443 t = cp->p_tlist; 444 do { 445 t->t_proc_flag |= TP_PRSTOP; 446 aston(t); /* so TP_PRSTOP will be seen */ 447 } while ((t = t->t_forw) != cp->p_tlist); 448 mutex_exit(&cp->p_lock); 449 } 450 /* 451 * If the parent process has been marked to stop on exit 452 * from this fork, and its asynchronous-stop flag has not 453 * been set, arrange for all other lwps to stop before 454 * they return back to user level. 455 */ 456 if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap && 457 prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) { 458 mutex_enter(&p->p_lock); 459 t = p->p_tlist; 460 do { 461 t->t_proc_flag |= TP_PRSTOP; 462 aston(t); /* so TP_PRSTOP will be seen */ 463 } while ((t = t->t_forw) != p->p_tlist); 464 mutex_exit(&p->p_lock); 465 } 466 467 if (PROC_IS_BRANDED(p)) 468 BROP(p)->b_lwp_setrval(clone, p->p_pid, 1); 469 else 470 lwp_setrval(clone, p->p_pid, 1); 471 472 /* set return values for parent */ 473 r.r_val1 = (int)cp->p_pid; 474 r.r_val2 = 0; 475 476 /* 477 * pool_barrier_exit() can now be called because the child process has: 478 * - all identifying features cloned or set (p_pid, p_task, p_pool) 479 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset) 480 * - any other fields set which are used in resource set binding. 481 */ 482 mutex_enter(&p->p_lock); 483 pool_barrier_exit(); 484 mutex_exit(&p->p_lock); 485 486 mutex_enter(&pidlock); 487 mutex_enter(&cp->p_lock); 488 489 /* 490 * Now that there are lwps and threads attached, add the new 491 * process to the process group. 492 */ 493 pgjoin(cp, p->p_pgidp); 494 cp->p_stat = SRUN; 495 /* 496 * We are now done with all the lwps in the child process. 497 */ 498 t = cp->p_tlist; 499 do { 500 /* 501 * Set the lwp_suspend()ed lwps running. 502 * They will suspend properly at syscall exit. 503 */ 504 if (t->t_proc_flag & TP_HOLDLWP) 505 lwp_create_done(t); 506 else { 507 /* set TS_CREATE to allow continuelwps() to work */ 508 thread_lock(t); 509 ASSERT(t->t_state == TS_STOPPED && 510 !(t->t_schedflag & (TS_CREATE|TS_CSTART))); 511 t->t_schedflag |= TS_CREATE; 512 thread_unlock(t); 513 } 514 } while ((t = t->t_forw) != cp->p_tlist); 515 mutex_exit(&cp->p_lock); 516 517 if (isvfork) { 518 CPU_STATS_ADDQ(CPU, sys, sysvfork, 1); 519 mutex_enter(&p->p_lock); 520 p->p_flag |= SVFWAIT; 521 DTRACE_PROC1(create, proc_t *, cp); 522 cv_broadcast(&pr_pid_cv[p->p_slot]); /* inform /proc */ 523 mutex_exit(&p->p_lock); 524 /* 525 * Grab child's p_lock before dropping pidlock to ensure 526 * the process will not disappear before we set it running. 527 */ 528 mutex_enter(&cp->p_lock); 529 mutex_exit(&pidlock); 530 sigdefault(cp); 531 continuelwps(cp); 532 mutex_exit(&cp->p_lock); 533 } else { 534 CPU_STATS_ADDQ(CPU, sys, sysfork, 1); 535 DTRACE_PROC1(create, proc_t *, cp); 536 /* 537 * It is CL_FORKRET's job to drop pidlock. 538 * If we do it here, the process could be set running 539 * and disappear before CL_FORKRET() is called. 540 */ 541 CL_FORKRET(curthread, cp->p_tlist); 542 ASSERT(MUTEX_NOT_HELD(&pidlock)); 543 } 544 545 return (r.r_vals); 546 547 forklwperr: 548 if (isvfork) { 549 if (avl_numnodes(&p->p_wpage) != 0) { 550 /* restore watchpoints to parent */ 551 as = p->p_as; 552 AS_LOCK_ENTER(as, &as->a_lock, 553 RW_WRITER); 554 as->a_wpage = p->p_wpage; 555 avl_create(&p->p_wpage, wp_compare, 556 sizeof (struct watched_page), 557 offsetof(struct watched_page, wp_link)); 558 as_setwatch(as); 559 AS_LOCK_EXIT(as, &as->a_lock); 560 } 561 } else { 562 if (cp->p_segacct) 563 shmexit(cp); 564 as = cp->p_as; 565 cp->p_as = &kas; 566 as_free(as); 567 } 568 569 if (cp->p_lwpdir) { 570 for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++) 571 if ((lep = ldp->ld_entry) != NULL) 572 kmem_free(lep, sizeof (*lep)); 573 kmem_free(cp->p_lwpdir, 574 cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir)); 575 } 576 cp->p_lwpdir = NULL; 577 cp->p_lwpfree = NULL; 578 cp->p_lwpdir_sz = 0; 579 580 if (cp->p_tidhash) 581 kmem_free(cp->p_tidhash, 582 cp->p_tidhash_sz * sizeof (*cp->p_tidhash)); 583 cp->p_tidhash = NULL; 584 cp->p_tidhash_sz = 0; 585 586 forklwp_fail(cp); 587 fork_fail(cp); 588 rctl_set_free(cp->p_rctls); 589 mutex_enter(&pidlock); 590 591 /* 592 * Detach failed child from task. 593 */ 594 mutex_enter(&cp->p_lock); 595 tk = cp->p_task; 596 task_detach(cp); 597 ASSERT(cp->p_pool->pool_ref > 0); 598 atomic_add_32(&cp->p_pool->pool_ref, -1); 599 mutex_exit(&cp->p_lock); 600 601 orphpp = &p->p_orphan; 602 while (*orphpp != cp) 603 orphpp = &(*orphpp)->p_nextorph; 604 *orphpp = cp->p_nextorph; 605 if (p->p_child == cp) 606 p->p_child = cp->p_sibling; 607 if (cp->p_sibling) 608 cp->p_sibling->p_psibling = cp->p_psibling; 609 if (cp->p_psibling) 610 cp->p_psibling->p_sibling = cp->p_sibling; 611 pid_exit(cp); 612 mutex_exit(&pidlock); 613 614 task_rele(tk); 615 616 mutex_enter(&p->p_lock); 617 pool_barrier_exit(); 618 continuelwps(p); 619 mutex_exit(&p->p_lock); 620 error = EAGAIN; 621 forkerr: 622 return ((int64_t)set_errno(error)); 623 } 624 625 /* 626 * Free allocated resources from getproc() if a fork failed. 627 */ 628 static void 629 fork_fail(proc_t *cp) 630 { 631 uf_info_t *fip = P_FINFO(cp); 632 633 fcnt_add(fip, -1); 634 sigdelq(cp, NULL, 0); 635 636 mutex_enter(&pidlock); 637 upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred)); 638 mutex_exit(&pidlock); 639 640 /* 641 * single threaded, so no locking needed here 642 */ 643 crfree(cp->p_cred); 644 645 kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t)); 646 647 VN_RELE(u.u_cdir); 648 if (u.u_rdir) 649 VN_RELE(u.u_rdir); 650 if (cp->p_exec) 651 VN_RELE(cp->p_exec); 652 if (cp->p_execdir) 653 VN_RELE(cp->p_execdir); 654 if (u.u_cwd) 655 refstr_rele(u.u_cwd); 656 } 657 658 /* 659 * Clean up the lwps already created for this child process. 660 * The fork failed while duplicating all the lwps of the parent 661 * and those lwps already created must be freed. 662 * This process is invisible to the rest of the system, 663 * so we don't need to hold p->p_lock to protect the list. 664 */ 665 static void 666 forklwp_fail(proc_t *p) 667 { 668 kthread_t *t; 669 task_t *tk; 670 671 while ((t = p->p_tlist) != NULL) { 672 /* 673 * First remove the lwp from the process's p_tlist. 674 */ 675 if (t != t->t_forw) 676 p->p_tlist = t->t_forw; 677 else 678 p->p_tlist = NULL; 679 p->p_lwpcnt--; 680 t->t_forw->t_back = t->t_back; 681 t->t_back->t_forw = t->t_forw; 682 683 tk = p->p_task; 684 mutex_enter(&p->p_zone->zone_nlwps_lock); 685 tk->tk_nlwps--; 686 tk->tk_proj->kpj_nlwps--; 687 p->p_zone->zone_nlwps--; 688 mutex_exit(&p->p_zone->zone_nlwps_lock); 689 690 ASSERT(t->t_schedctl == NULL); 691 692 if (t->t_door != NULL) { 693 kmem_free(t->t_door, sizeof (door_data_t)); 694 t->t_door = NULL; 695 } 696 lwp_ctmpl_clear(ttolwp(t)); 697 698 /* 699 * Remove the thread from the all threads list. 700 * We need to hold pidlock for this. 701 */ 702 mutex_enter(&pidlock); 703 t->t_next->t_prev = t->t_prev; 704 t->t_prev->t_next = t->t_next; 705 CL_EXIT(t); /* tell the scheduler that we're exiting */ 706 cv_broadcast(&t->t_joincv); /* tell anyone in thread_join */ 707 mutex_exit(&pidlock); 708 709 /* 710 * Let the lgroup load averages know that this thread isn't 711 * going to show up (i.e. un-do what was done on behalf of 712 * this thread by the earlier lgrp_move_thread()). 713 */ 714 kpreempt_disable(); 715 lgrp_move_thread(t, NULL, 1); 716 kpreempt_enable(); 717 718 /* 719 * The thread was created TS_STOPPED. 720 * We change it to TS_FREE to avoid an 721 * ASSERT() panic in thread_free(). 722 */ 723 t->t_state = TS_FREE; 724 thread_rele(t); 725 thread_free(t); 726 } 727 } 728 729 extern struct as kas; 730 731 /* 732 * fork a kernel process. 733 */ 734 int 735 newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct) 736 { 737 proc_t *p; 738 struct user *up; 739 klwp_t *lwp; 740 cont_process_t *ctp = NULL; 741 rctl_entity_p_t e; 742 743 ASSERT(!(cid == syscid && ct != NULL)); 744 if (cid == syscid) { 745 rctl_alloc_gp_t *init_gp; 746 rctl_set_t *init_set; 747 748 if (getproc(&p, 1) < 0) 749 return (EAGAIN); 750 751 p->p_flag |= SNOWAIT; 752 p->p_exec = NULL; 753 p->p_execdir = NULL; 754 755 init_set = rctl_set_create(); 756 init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS); 757 758 /* 759 * kernel processes do not inherit /proc tracing flags. 760 */ 761 sigemptyset(&p->p_sigmask); 762 premptyset(&p->p_fltmask); 763 up = PTOU(p); 764 up->u_systrap = 0; 765 premptyset(&(up->u_entrymask)); 766 premptyset(&(up->u_exitmask)); 767 mutex_enter(&p->p_lock); 768 e.rcep_p.proc = p; 769 e.rcep_t = RCENTITY_PROCESS; 770 p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set, 771 init_gp); 772 mutex_exit(&p->p_lock); 773 774 rctl_prealloc_destroy(init_gp); 775 } else { 776 rctl_alloc_gp_t *init_gp, *default_gp; 777 rctl_set_t *init_set; 778 task_t *tk, *tk_old; 779 780 if (getproc(&p, 0) < 0) 781 return (EAGAIN); 782 /* 783 * init creates a new task, distinct from the task 784 * containing kernel "processes". 785 */ 786 tk = task_create(0, p->p_zone); 787 mutex_enter(&tk->tk_zone->zone_nlwps_lock); 788 tk->tk_proj->kpj_ntasks++; 789 mutex_exit(&tk->tk_zone->zone_nlwps_lock); 790 791 default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS); 792 init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS); 793 init_set = rctl_set_create(); 794 795 mutex_enter(&pidlock); 796 mutex_enter(&p->p_lock); 797 tk_old = p->p_task; /* switch to new task */ 798 799 task_detach(p); 800 task_begin(tk, p); 801 mutex_exit(&pidlock); 802 803 e.rcep_p.proc = p; 804 e.rcep_t = RCENTITY_PROCESS; 805 p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set, 806 init_gp); 807 rctlproc_default_init(p, default_gp); 808 mutex_exit(&p->p_lock); 809 810 task_rele(tk_old); 811 rctl_prealloc_destroy(default_gp); 812 rctl_prealloc_destroy(init_gp); 813 } 814 815 p->p_as = &kas; 816 817 if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri, 818 &curthread->t_hold, cid, 1)) == NULL) { 819 task_t *tk; 820 fork_fail(p); 821 mutex_enter(&pidlock); 822 mutex_enter(&p->p_lock); 823 tk = p->p_task; 824 task_detach(p); 825 ASSERT(p->p_pool->pool_ref > 0); 826 atomic_add_32(&p->p_pool->pool_ref, -1); 827 mutex_exit(&p->p_lock); 828 pid_exit(p); 829 mutex_exit(&pidlock); 830 task_rele(tk); 831 832 return (EAGAIN); 833 } 834 835 if (cid != syscid) { 836 ctp = contract_process_fork(sys_process_tmpl, p, curproc, 837 B_FALSE); 838 ASSERT(ctp != NULL); 839 if (ct != NULL) 840 *ct = &ctp->conp_contract; 841 } 842 843 p->p_lwpid = 1; 844 mutex_enter(&pidlock); 845 pgjoin(p, curproc->p_pgidp); 846 p->p_stat = SRUN; 847 mutex_enter(&p->p_lock); 848 lwptot(lwp)->t_proc_flag &= ~TP_HOLDLWP; 849 lwp_create_done(lwptot(lwp)); 850 mutex_exit(&p->p_lock); 851 mutex_exit(&pidlock); 852 return (0); 853 } 854 855 /* 856 * create a child proc struct. 857 */ 858 static int 859 getproc(proc_t **cpp, int kernel) 860 { 861 proc_t *pp, *cp; 862 pid_t newpid; 863 struct user *uarea; 864 extern uint_t nproc; 865 struct cred *cr; 866 uid_t ruid; 867 zoneid_t zoneid; 868 869 if (!page_mem_avail(tune.t_minarmem)) 870 return (-1); 871 if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) 872 return (-1); /* no point in starting new processes */ 873 874 pp = curproc; 875 cp = kmem_cache_alloc(process_cache, KM_SLEEP); 876 bzero(cp, sizeof (proc_t)); 877 878 /* 879 * Make proc entry for child process 880 */ 881 mutex_init(&cp->p_splock, NULL, MUTEX_DEFAULT, NULL); 882 mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL); 883 mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL); 884 #if defined(__x86) 885 mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL); 886 #endif 887 mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL); 888 cp->p_stat = SIDL; 889 cp->p_mstart = gethrtime(); 890 /* 891 * p_zone must be set before we call pid_allocate since the process 892 * will be visible after that and code such as prfind_zone will 893 * look at the p_zone field. 894 */ 895 cp->p_zone = pp->p_zone; 896 897 if ((newpid = pid_allocate(cp, PID_ALLOC_PROC)) == -1) { 898 if (nproc == v.v_proc) { 899 CPU_STATS_ADDQ(CPU, sys, procovf, 1); 900 cmn_err(CE_WARN, "out of processes"); 901 } 902 goto bad; 903 } 904 905 /* 906 * If not privileged make sure that this user hasn't exceeded 907 * v.v_maxup processes, and that users collectively haven't 908 * exceeded v.v_maxupttl processes. 909 */ 910 mutex_enter(&pidlock); 911 ASSERT(nproc < v.v_proc); /* otherwise how'd we get our pid? */ 912 cr = CRED(); 913 ruid = crgetruid(cr); 914 zoneid = crgetzoneid(cr); 915 if (nproc >= v.v_maxup && /* short-circuit; usually false */ 916 (nproc >= v.v_maxupttl || 917 upcount_get(ruid, zoneid) >= v.v_maxup) && 918 secpolicy_newproc(cr) != 0) { 919 mutex_exit(&pidlock); 920 zcmn_err(zoneid, CE_NOTE, 921 "out of per-user processes for uid %d", ruid); 922 goto bad; 923 } 924 925 /* 926 * Everything is cool, put the new proc on the active process list. 927 * It is already on the pid list and in /proc. 928 * Increment the per uid process count (upcount). 929 */ 930 nproc++; 931 upcount_inc(ruid, zoneid); 932 933 cp->p_next = practive; 934 practive->p_prev = cp; 935 practive = cp; 936 937 cp->p_ignore = pp->p_ignore; 938 cp->p_siginfo = pp->p_siginfo; 939 cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD); 940 cp->p_sessp = pp->p_sessp; 941 sess_hold(pp); 942 cp->p_exec = pp->p_exec; 943 cp->p_execdir = pp->p_execdir; 944 cp->p_brand = pp->p_brand; 945 if (PROC_IS_BRANDED(pp)) 946 BROP(pp)->b_copy_procdata(cp, pp); 947 948 cp->p_bssbase = pp->p_bssbase; 949 cp->p_brkbase = pp->p_brkbase; 950 cp->p_brksize = pp->p_brksize; 951 cp->p_brkpageszc = pp->p_brkpageszc; 952 cp->p_stksize = pp->p_stksize; 953 cp->p_stkpageszc = pp->p_stkpageszc; 954 cp->p_stkprot = pp->p_stkprot; 955 cp->p_datprot = pp->p_datprot; 956 cp->p_usrstack = pp->p_usrstack; 957 cp->p_model = pp->p_model; 958 cp->p_ppid = pp->p_pid; 959 cp->p_ancpid = pp->p_pid; 960 cp->p_portcnt = pp->p_portcnt; 961 962 /* 963 * Initialize watchpoint structures 964 */ 965 avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area), 966 offsetof(struct watched_area, wa_link)); 967 968 /* 969 * Initialize immediate resource control values. 970 */ 971 cp->p_stk_ctl = pp->p_stk_ctl; 972 cp->p_fsz_ctl = pp->p_fsz_ctl; 973 cp->p_vmem_ctl = pp->p_vmem_ctl; 974 cp->p_fno_ctl = pp->p_fno_ctl; 975 976 /* 977 * Link up to parent-child-sibling chain. No need to lock 978 * in general since only a call to freeproc() (done by the 979 * same parent as newproc()) diddles with the child chain. 980 */ 981 cp->p_sibling = pp->p_child; 982 if (pp->p_child) 983 pp->p_child->p_psibling = cp; 984 985 cp->p_parent = pp; 986 pp->p_child = cp; 987 988 cp->p_child_ns = NULL; 989 cp->p_sibling_ns = NULL; 990 991 cp->p_nextorph = pp->p_orphan; 992 cp->p_nextofkin = pp; 993 pp->p_orphan = cp; 994 995 /* 996 * Inherit profiling state; do not inherit REALPROF profiling state. 997 */ 998 cp->p_prof = pp->p_prof; 999 cp->p_rprof_cyclic = CYCLIC_NONE; 1000 1001 /* 1002 * Inherit pool pointer from the parent. Kernel processes are 1003 * always bound to the default pool. 1004 */ 1005 mutex_enter(&pp->p_lock); 1006 if (kernel) { 1007 cp->p_pool = pool_default; 1008 cp->p_flag |= SSYS; 1009 } else { 1010 cp->p_pool = pp->p_pool; 1011 } 1012 atomic_add_32(&cp->p_pool->pool_ref, 1); 1013 mutex_exit(&pp->p_lock); 1014 1015 /* 1016 * Add the child process to the current task. Kernel processes 1017 * are always attached to task0. 1018 */ 1019 mutex_enter(&cp->p_lock); 1020 if (kernel) 1021 task_attach(task0p, cp); 1022 else 1023 task_attach(pp->p_task, cp); 1024 mutex_exit(&cp->p_lock); 1025 mutex_exit(&pidlock); 1026 1027 avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t), 1028 offsetof(contract_t, ct_ctlist)); 1029 1030 /* 1031 * Duplicate any audit information kept in the process table 1032 */ 1033 #ifdef C2_AUDIT 1034 if (audit_active) /* copy audit data to cp */ 1035 audit_newproc(cp); 1036 #endif 1037 1038 crhold(cp->p_cred = cr); 1039 1040 /* 1041 * Bump up the counts on the file structures pointed at by the 1042 * parent's file table since the child will point at them too. 1043 */ 1044 fcnt_add(P_FINFO(pp), 1); 1045 1046 VN_HOLD(u.u_cdir); 1047 if (u.u_rdir) 1048 VN_HOLD(u.u_rdir); 1049 if (u.u_cwd) 1050 refstr_hold(u.u_cwd); 1051 1052 /* 1053 * copy the parent's uarea. 1054 */ 1055 uarea = PTOU(cp); 1056 bcopy(PTOU(pp), uarea, sizeof (user_t)); 1057 flist_fork(P_FINFO(pp), P_FINFO(cp)); 1058 1059 gethrestime(&uarea->u_start); 1060 uarea->u_ticks = lbolt; 1061 uarea->u_mem = rm_asrss(pp->p_as); 1062 uarea->u_acflag = AFORK; 1063 1064 /* 1065 * If inherit-on-fork, copy /proc tracing flags to child. 1066 */ 1067 if ((pp->p_proc_flag & P_PR_FORK) != 0) { 1068 cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK); 1069 cp->p_sigmask = pp->p_sigmask; 1070 cp->p_fltmask = pp->p_fltmask; 1071 } else { 1072 sigemptyset(&cp->p_sigmask); 1073 premptyset(&cp->p_fltmask); 1074 uarea->u_systrap = 0; 1075 premptyset(&uarea->u_entrymask); 1076 premptyset(&uarea->u_exitmask); 1077 } 1078 /* 1079 * If microstate accounting is being inherited, mark child 1080 */ 1081 if ((pp->p_flag & SMSFORK) != 0) 1082 cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT); 1083 1084 /* 1085 * Inherit fixalignment flag from the parent 1086 */ 1087 cp->p_fixalignment = pp->p_fixalignment; 1088 1089 if (cp->p_exec) 1090 VN_HOLD(cp->p_exec); 1091 if (cp->p_execdir) 1092 VN_HOLD(cp->p_execdir); 1093 *cpp = cp; 1094 return (0); 1095 1096 bad: 1097 ASSERT(MUTEX_NOT_HELD(&pidlock)); 1098 1099 mutex_destroy(&cp->p_crlock); 1100 mutex_destroy(&cp->p_pflock); 1101 #if defined(__x86) 1102 mutex_destroy(&cp->p_ldtlock); 1103 #endif 1104 if (newpid != -1) { 1105 proc_entry_free(cp->p_pidp); 1106 (void) pid_rele(cp->p_pidp); 1107 } 1108 kmem_cache_free(process_cache, cp); 1109 1110 /* 1111 * We most likely got into this situation because some process is 1112 * forking out of control. As punishment, put it to sleep for a 1113 * bit so it can't eat the machine alive. Sleep interval is chosen 1114 * to allow no more than one fork failure per cpu per clock tick 1115 * on average (yes, I just made this up). This has two desirable 1116 * properties: (1) it sets a constant limit on the fork failure 1117 * rate, and (2) the busier the system is, the harsher the penalty 1118 * for abusing it becomes. 1119 */ 1120 INCR_COUNT(&fork_fail_pending, &pidlock); 1121 delay(fork_fail_pending / ncpus + 1); 1122 DECR_COUNT(&fork_fail_pending, &pidlock); 1123 1124 return (-1); /* out of memory or proc slots */ 1125 } 1126 1127 /* 1128 * Release virtual memory. 1129 * In the case of vfork(), the child was given exclusive access to its 1130 * parent's address space. The parent is waiting in vfwait() for the 1131 * child to release its exclusive claim via relvm(). 1132 */ 1133 void 1134 relvm() 1135 { 1136 proc_t *p = curproc; 1137 1138 ASSERT((unsigned)p->p_lwpcnt <= 1); 1139 1140 prrelvm(); /* inform /proc */ 1141 1142 if (p->p_flag & SVFORK) { 1143 proc_t *pp = p->p_parent; 1144 /* 1145 * The child process is either exec'ing or exit'ing. 1146 * The child is now separated from the parent's address 1147 * space. The parent process is made dispatchable. 1148 * 1149 * This is a delicate locking maneuver, involving 1150 * both the parent's p_lock and the child's p_lock. 1151 * As soon as the SVFORK flag is turned off, the 1152 * parent is free to run, but it must not run until 1153 * we wake it up using its p_cv because it might 1154 * exit and we would be referencing invalid memory. 1155 * Therefore, we hold the parent with its p_lock 1156 * while protecting our p_flags with our own p_lock. 1157 */ 1158 try_again: 1159 mutex_enter(&p->p_lock); /* grab child's lock first */ 1160 prbarrier(p); /* make sure /proc is blocked out */ 1161 mutex_enter(&pp->p_lock); 1162 1163 /* 1164 * Check if parent is locked by /proc. 1165 */ 1166 if (pp->p_proc_flag & P_PR_LOCK) { 1167 /* 1168 * Delay until /proc is done with the parent. 1169 * We must drop our (the child's) p->p_lock, wait 1170 * via prbarrier() on the parent, then start over. 1171 */ 1172 mutex_exit(&p->p_lock); 1173 prbarrier(pp); 1174 mutex_exit(&pp->p_lock); 1175 goto try_again; 1176 } 1177 p->p_flag &= ~SVFORK; 1178 kpreempt_disable(); 1179 p->p_as = &kas; 1180 1181 /* 1182 * notify hat of change in thread's address space 1183 */ 1184 hat_thread_exit(curthread); 1185 kpreempt_enable(); 1186 1187 /* 1188 * child sizes are copied back to parent because 1189 * child may have grown. 1190 */ 1191 pp->p_brkbase = p->p_brkbase; 1192 pp->p_brksize = p->p_brksize; 1193 pp->p_stksize = p->p_stksize; 1194 /* 1195 * The parent is no longer waiting for the vfork()d child. 1196 * Restore the parent's watched pages, if any. This is 1197 * safe because we know the parent is not locked by /proc 1198 */ 1199 pp->p_flag &= ~SVFWAIT; 1200 if (avl_numnodes(&pp->p_wpage) != 0) { 1201 pp->p_as->a_wpage = pp->p_wpage; 1202 avl_create(&pp->p_wpage, wp_compare, 1203 sizeof (struct watched_page), 1204 offsetof(struct watched_page, wp_link)); 1205 } 1206 cv_signal(&pp->p_cv); 1207 mutex_exit(&pp->p_lock); 1208 mutex_exit(&p->p_lock); 1209 } else { 1210 if (p->p_as != &kas) { 1211 struct as *as; 1212 1213 if (p->p_segacct) 1214 shmexit(p); 1215 1216 /* 1217 * We grab p_lock for the benefit of /proc 1218 */ 1219 kpreempt_disable(); 1220 mutex_enter(&p->p_lock); 1221 prbarrier(p); /* make sure /proc is blocked out */ 1222 as = p->p_as; 1223 p->p_as = &kas; 1224 mutex_exit(&p->p_lock); 1225 1226 /* 1227 * notify hat of change in thread's address space 1228 */ 1229 hat_thread_exit(curthread); 1230 kpreempt_enable(); 1231 1232 as_free(as); 1233 } 1234 } 1235 } 1236 1237 /* 1238 * Wait for child to exec or exit. 1239 * Called by parent of vfork'ed process. 1240 * See important comments in relvm(), above. 1241 */ 1242 void 1243 vfwait(pid_t pid) 1244 { 1245 int signalled = 0; 1246 proc_t *pp = ttoproc(curthread); 1247 proc_t *cp; 1248 1249 /* 1250 * Wait for child to exec or exit. 1251 */ 1252 for (;;) { 1253 mutex_enter(&pidlock); 1254 cp = prfind(pid); 1255 if (cp == NULL || cp->p_parent != pp) { 1256 /* 1257 * Child has exit()ed. 1258 */ 1259 mutex_exit(&pidlock); 1260 break; 1261 } 1262 /* 1263 * Grab the child's p_lock before releasing pidlock. 1264 * Otherwise, the child could exit and we would be 1265 * referencing invalid memory. 1266 */ 1267 mutex_enter(&cp->p_lock); 1268 mutex_exit(&pidlock); 1269 if (!(cp->p_flag & SVFORK)) { 1270 /* 1271 * Child has exec()ed or is exit()ing. 1272 */ 1273 mutex_exit(&cp->p_lock); 1274 break; 1275 } 1276 mutex_enter(&pp->p_lock); 1277 mutex_exit(&cp->p_lock); 1278 /* 1279 * We might be waked up spuriously from the cv_wait(). 1280 * We have to do the whole operation over again to be 1281 * sure the child's SVFORK flag really is turned off. 1282 * We cannot make reference to the child because it can 1283 * exit before we return and we would be referencing 1284 * invalid memory. 1285 * 1286 * Because this is potentially a very long-term wait, 1287 * we call cv_wait_sig() (for its jobcontrol and /proc 1288 * side-effects) unless there is a current signal, in 1289 * which case we use cv_wait() because we cannot return 1290 * from this function until the child has released the 1291 * address space. Calling cv_wait_sig() with a current 1292 * signal would lead to an indefinite loop here because 1293 * cv_wait_sig() returns immediately in this case. 1294 */ 1295 if (signalled) 1296 cv_wait(&pp->p_cv, &pp->p_lock); 1297 else 1298 signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock); 1299 mutex_exit(&pp->p_lock); 1300 } 1301 1302 /* restore watchpoints to parent */ 1303 if (pr_watch_active(pp)) { 1304 struct as *as = pp->p_as; 1305 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1306 as_setwatch(as); 1307 AS_LOCK_EXIT(as, &as->a_lock); 1308 } 1309 1310 mutex_enter(&pp->p_lock); 1311 prbarrier(pp); /* barrier against /proc locking */ 1312 continuelwps(pp); 1313 mutex_exit(&pp->p_lock); 1314 } 1315