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