xref: /titanic_51/usr/src/uts/common/syscall/lwp_create.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 #if defined(__i386)
94 		/*
95 		 * libc stashed thrptr into unused kernel %sp.
96 		 * See setup_context() in libc.
97 		 */
98 		thrptr = (uint32_t)uc.uc_mcontext.gregs[ESP];
99 #endif
100 	}
101 #ifdef _SYSCALL32_IMPL
102 	else {
103 		if (copyin(ucp, &uc32, sizeof (ucontext32_t)))
104 			return (set_errno(EFAULT));
105 		sigutok(&uc32.uc_sigmask, &sigmask);
106 #if defined(__sparc)
107 		ucontext_32ton(&uc32, &uc, NULL, NULL);
108 #else	/* __amd64 */
109 		ucontext_32ton(&uc32, &uc);
110 		/*
111 		 * libc stashed thrptr into unused kernel %sp.
112 		 * See setup_context() in libc.
113 		 */
114 		thrptr = (uint32_t)uc32.uc_mcontext.gregs[ESP];
115 #endif
116 	}
117 #endif /* _SYSCALL32_IMPL */
118 
119 	/*
120 	 * Tell machine specific code that we are creating a new lwp
121 	 */
122 	LWP_MMODEL_NEWLWP();
123 
124 	(void) save_syscall_args();	/* save args for tracing first */
125 
126 	mutex_enter(&curproc->p_lock);
127 	pool_barrier_enter();
128 	mutex_exit(&curproc->p_lock);
129 	lwp = lwp_create(lwp_rtt, NULL, NULL, curproc, TS_STOPPED,
130 	    curthread->t_pri, &sigmask, curthread->t_cid, 0);
131 	mutex_enter(&curproc->p_lock);
132 	pool_barrier_exit();
133 	mutex_exit(&curproc->p_lock);
134 	if (lwp == NULL)
135 		return (set_errno(EAGAIN));
136 
137 	lwp_load(lwp, uc.uc_mcontext.gregs, thrptr);
138 
139 	t = lwptot(lwp);
140 	/*
141 	 * Copy the new lwp's lwpid into the caller's specified buffer.
142 	 */
143 	if (new_lwp && copyout(&t->t_tid, new_lwp, sizeof (id_t))) {
144 		/*
145 		 * caller's buffer is not writable, return
146 		 * EFAULT, and terminate new lwp.
147 		 */
148 		mutex_enter(&p->p_lock);
149 		t->t_proc_flag |= TP_EXITLWP;
150 		t->t_sig_check = 1;
151 		t->t_sysnum = 0;
152 		t->t_proc_flag &= ~TP_HOLDLWP;
153 		lwp_create_done(t);
154 		mutex_exit(&p->p_lock);
155 		return (set_errno(EFAULT));
156 	}
157 
158 	/*
159 	 * clone callers context, if any.  must be invoked
160 	 * while -not- holding p_lock.
161 	 */
162 	if (curthread->t_ctx)
163 		lwp_createctx(curthread, t);
164 
165 	/*
166 	 * copy current contract templates
167 	 */
168 	lwp_ctmpl_copy(lwp, ttolwp(curthread));
169 
170 	mutex_enter(&p->p_lock);
171 	/*
172 	 * Copy the syscall arguments to the new lwp's arg area
173 	 * for the benefit of debuggers.
174 	 */
175 	t->t_sysnum = SYS_lwp_create;
176 	lwp->lwp_ap = lwp->lwp_arg;
177 	lwp->lwp_arg[0] = (long)ucp;
178 	lwp->lwp_arg[1] = (long)flags;
179 	lwp->lwp_arg[2] = (long)new_lwp;
180 	lwp->lwp_argsaved = 1;
181 
182 	if (!(flags & (LWP_DETACHED|LWP_DAEMON)))
183 		t->t_proc_flag |= TP_TWAIT;
184 	if (flags & LWP_DAEMON) {
185 		t->t_proc_flag |= TP_DAEMON;
186 		p->p_lwpdaemon++;
187 	}
188 
189 	tid = (int)t->t_tid;	/* for /proc debuggers */
190 
191 	/*
192 	 * We now set the newly-created lwp running.
193 	 * If it is being created as LWP_SUSPENDED, we leave its
194 	 * TP_HOLDLWP flag set so it will stop in system call exit.
195 	 */
196 	if (!(flags & LWP_SUSPENDED))
197 		t->t_proc_flag &= ~TP_HOLDLWP;
198 	lwp_create_done(t);
199 	mutex_exit(&p->p_lock);
200 
201 	return (tid);
202 }
203 
204 /*
205  * Exit the calling lwp
206  */
207 void
208 syslwp_exit()
209 {
210 	proc_t *p = ttoproc(curthread);
211 
212 	mutex_enter(&p->p_lock);
213 	lwp_exit();
214 	/* NOTREACHED */
215 }
216