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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
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>
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>
49 * System call to create an lwp.
51 * Notes on the LWP_DETACHED and LWP_DAEMON flags:
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().
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().
66 syslwp_create(ucontext_t
*ucp
, int flags
, id_t
*new_lwp
)
69 proc_t
*p
= ttoproc(curthread
);
72 #ifdef _SYSCALL32_IMPL
74 #endif /* _SYSCALL32_IMPL */
77 model_t model
= get_udatamodel();
80 if (flags
& ~(LWP_DAEMON
|LWP_DETACHED
|LWP_SUSPENDED
))
81 return (set_errno(EINVAL
));
84 * lwp_create() is disallowed for the /proc agent lwp.
86 if (curthread
== p
->p_agenttp
)
87 return (set_errno(ENOTSUP
));
89 if (model
== DATAMODEL_NATIVE
) {
90 if (copyin(ucp
, &uc
, sizeof (ucontext_t
)))
91 return (set_errno(EFAULT
));
92 sigutok(&uc
.uc_sigmask
, &sigmask
);
95 * libc stashed thrptr into unused kernel %sp.
96 * See setup_context() in libc.
98 thrptr
= (uint32_t)uc
.uc_mcontext
.gregs
[ESP
];
101 #ifdef _SYSCALL32_IMPL
103 if (copyin(ucp
, &uc32
, sizeof (ucontext32_t
)))
104 return (set_errno(EFAULT
));
105 sigutok(&uc32
.uc_sigmask
, &sigmask
);
107 ucontext_32ton(&uc32
, &uc
, NULL
, NULL
);
109 ucontext_32ton(&uc32
, &uc
);
111 * libc stashed thrptr into unused kernel %sp.
112 * See setup_context() in libc.
114 thrptr
= (uint32_t)uc32
.uc_mcontext
.gregs
[ESP
];
117 #endif /* _SYSCALL32_IMPL */
120 * Tell machine specific code that we are creating a new lwp
124 (void) save_syscall_args(); /* save args for tracing first */
126 mutex_enter(&curproc
->p_lock
);
127 pool_barrier_enter();
128 mutex_exit(&curproc
->p_lock
);
129 lwp
= lwp_create(lwp_rtt
, NULL
, 0, curproc
, TS_STOPPED
,
130 curthread
->t_pri
, &sigmask
, curthread
->t_cid
, 0);
131 mutex_enter(&curproc
->p_lock
);
133 mutex_exit(&curproc
->p_lock
);
135 return (set_errno(EAGAIN
));
137 lwp_load(lwp
, uc
.uc_mcontext
.gregs
, thrptr
);
141 * Copy the new lwp's lwpid into the caller's specified buffer.
143 if (new_lwp
&& copyout(&t
->t_tid
, new_lwp
, sizeof (id_t
))) {
145 * caller's buffer is not writable, return
146 * EFAULT, and terminate new lwp.
148 mutex_enter(&p
->p_lock
);
149 t
->t_proc_flag
|= TP_EXITLWP
;
152 t
->t_proc_flag
&= ~TP_HOLDLWP
;
154 mutex_exit(&p
->p_lock
);
155 return (set_errno(EFAULT
));
159 * clone callers context, if any. must be invoked
160 * while -not- holding p_lock.
162 if (curthread
->t_ctx
)
163 lwp_createctx(curthread
, t
);
166 * copy current contract templates
168 lwp_ctmpl_copy(lwp
, ttolwp(curthread
));
170 mutex_enter(&p
->p_lock
);
172 * Copy the syscall arguments to the new lwp's arg area
173 * for the benefit of debuggers.
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;
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
;
189 tid
= (int)t
->t_tid
; /* for /proc debuggers */
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.
196 if (!(flags
& LWP_SUSPENDED
))
197 t
->t_proc_flag
&= ~TP_HOLDLWP
;
199 mutex_exit(&p
->p_lock
);
205 * Exit the calling lwp
210 proc_t
*p
= ttoproc(curthread
);
212 mutex_enter(&p
->p_lock
);