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