xref: /illumos-gate/usr/src/uts/common/syscall/lwp_create.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
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 2009 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 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/systm.h>
33 #include <sys/errno.h>
34 #include <sys/syscall.h>
35 #include <sys/proc.h>
36 #include <sys/processor.h>
37 #include <sys/fault.h>
38 #include <sys/ucontext.h>
39 #include <sys/signal.h>
40 #include <sys/unistd.h>
41 #include <sys/procfs.h>
42 #include <sys/prsystm.h>
43 #include <sys/cmn_err.h>
44 #include <sys/debug.h>
45 #include <sys/klwp.h>
46 #include <sys/pool.h>
47 
48 /*
49  * System call to create an lwp.
50  *
51  * Notes on the LWP_DETACHED and LWP_DAEMON flags:
52  *
53  * A detached lwp (LWP_DETACHED) cannot be the specific target of
54  * lwp_wait() (it is not joinable), but lwp_wait(0, ...) is required
55  * to sleep until all non-daemon detached lwps have terminated before
56  * returning EDEADLK because a detached lwp might create a non-detached lwp
57  * that could then be returned by lwp_wait(0, ...).  See also lwp_detach().
58  *
59  * A daemon lwp (LWP_DAEMON) is a detached lwp that has the additional
60  * property that it does not affect the termination condition of the
61  * process:  The last non-daemon lwp to call lwp_exit() causes the process
62  * to exit and lwp_wait(0, ...) does not sleep waiting for daemon lwps
63  * to terminate.  See the block comment before lwp_wait().
64  */
65 int
66 syslwp_create(ucontext_t *ucp, int flags, id_t *new_lwp)
67 {
68 	klwp_t *lwp;
69 	proc_t *p = ttoproc(curthread);
70 	kthread_t *t;
71 	ucontext_t uc;
72 #ifdef _SYSCALL32_IMPL
73 	ucontext32_t uc32;
74 #endif /* _SYSCALL32_IMPL */
75 	k_sigset_t sigmask;
76 	int	tid;
77 	model_t model = get_udatamodel();
78 	uintptr_t thrptr = 0;
79 
80 	if (flags & ~(LWP_DAEMON|LWP_DETACHED|LWP_SUSPENDED))
81 		return (set_errno(EINVAL));
82 
83 	/*
84 	 * lwp_create() is disallowed for the /proc agent lwp.
85 	 */
86 	if (curthread == p->p_agenttp)
87 		return (set_errno(ENOTSUP));
88 
89 	if (model == DATAMODEL_NATIVE) {
90 		if (copyin(ucp, &uc, sizeof (ucontext_t)))
91 			return (set_errno(EFAULT));
92 		sigutok(&uc.uc_sigmask, &sigmask);
93 	}
94 #ifdef _SYSCALL32_IMPL
95 	else {
96 		if (copyin(ucp, &uc32, sizeof (ucontext32_t)))
97 			return (set_errno(EFAULT));
98 		sigutok(&uc32.uc_sigmask, &sigmask);
99 #if defined(__sparc)
100 		ucontext_32ton(&uc32, &uc, NULL, NULL);
101 #else	/* __amd64 */
102 		ucontext_32ton(&uc32, &uc);
103 		/*
104 		 * libc stashed thrptr into unused kernel %sp.
105 		 * See setup_context() in libc.
106 		 */
107 		thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP];
108 #endif
109 	}
110 #endif /* _SYSCALL32_IMPL */
111 
112 	/*
113 	 * Tell machine specific code that we are creating a new lwp
114 	 */
115 	LWP_MMODEL_NEWLWP();
116 
117 	(void) save_syscall_args();	/* save args for tracing first */
118 
119 	mutex_enter(&curproc->p_lock);
120 	pool_barrier_enter();
121 	mutex_exit(&curproc->p_lock);
122 	lwp = lwp_create(lwp_rtt, NULL, 0, curproc, TS_STOPPED,
123 	    curthread->t_pri, &sigmask, curthread->t_cid, 0);
124 	mutex_enter(&curproc->p_lock);
125 	pool_barrier_exit();
126 	mutex_exit(&curproc->p_lock);
127 	if (lwp == NULL)
128 		return (set_errno(EAGAIN));
129 
130 	lwp_load(lwp, uc.uc_mcontext.gregs, thrptr);
131 
132 	t = lwptot(lwp);
133 	/*
134 	 * Copy the new lwp's lwpid into the caller's specified buffer.
135 	 */
136 	if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) {
137 		/*
138 		 * caller's buffer is not writable, return
139 		 * EFAULT, and terminate new lwp.
140 		 */
141 		mutex_enter(&p->p_lock);
142 		t->t_proc_flag |= TP_EXITLWP;
143 		t->t_sig_check = 1;
144 		t->t_sysnum = 0;
145 		t->t_proc_flag &= ~TP_HOLDLWP;
146 		lwp_create_done(t);
147 		mutex_exit(&p->p_lock);
148 		return (set_errno(EFAULT));
149 	}
150 
151 	/*
152 	 * clone callers context, if any.  must be invoked
153 	 * while -not- holding p_lock.
154 	 */
155 	if (curthread->t_ctx)
156 		lwp_createctx(curthread, t);
157 
158 	/*
159 	 * copy current contract templates
160 	 */
161 	lwp_ctmpl_copy(lwp, ttolwp(curthread));
162 
163 	mutex_enter(&p->p_lock);
164 	/*
165 	 * Copy the syscall arguments to the new lwp's arg area
166 	 * for the benefit of debuggers.
167 	 */
168 	t->t_sysnum = SYS_lwp_create;
169 	lwp->lwp_ap = lwp->lwp_arg;
170 	lwp->lwp_arg[0] = (long)ucp;
171 	lwp->lwp_arg[1] = (long)flags;
172 	lwp->lwp_arg[2] = (long)new_lwp;
173 	lwp->lwp_argsaved = 1;
174 
175 	if (!(flags & (LWP_DETACHED|LWP_DAEMON)))
176 		t->t_proc_flag |= TP_TWAIT;
177 	if (flags & LWP_DAEMON) {
178 		t->t_proc_flag |= TP_DAEMON;
179 		p->p_lwpdaemon++;
180 	}
181 
182 	tid = (int)t->t_tid;	/* for /proc debuggers */
183 
184 	/*
185 	 * We now set the newly-created lwp running.
186 	 * If it is being created as LWP_SUSPENDED, we leave its
187 	 * TP_HOLDLWP flag set so it will stop in system call exit.
188 	 */
189 	if (!(flags & LWP_SUSPENDED))
190 		t->t_proc_flag &= ~TP_HOLDLWP;
191 	lwp_create_done(t);
192 	mutex_exit(&p->p_lock);
193 
194 	return (tid);
195 }
196 
197 /*
198  * Exit the calling lwp
199  */
200 void
201 syslwp_exit()
202 {
203 	proc_t *p = ttoproc(curthread);
204 
205 	mutex_enter(&p->p_lock);
206 	lwp_exit();
207 	/* NOTREACHED */
208 }
209