xref: /titanic_44/usr/src/uts/common/syscall/tasksys.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * System calls for creating and inquiring about tasks and projects
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/project.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * Limit projlist to 256k projects.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate #define	MAX_PROJLIST_BUFSIZE		1048576
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate typedef struct projlist_walk {
51*7c478bd9Sstevel@tonic-gate 	projid_t	*pw_buf;
52*7c478bd9Sstevel@tonic-gate 	size_t		pw_bufsz;
53*7c478bd9Sstevel@tonic-gate } projlist_walk_t;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * taskid_t tasksys_settaskid(projid_t projid, uint_t flags);
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * Overview
60*7c478bd9Sstevel@tonic-gate  *   Place the calling process in a new task if sufficiently privileged.  If the
61*7c478bd9Sstevel@tonic-gate  *   present task is finalized, the process may not create a new task.
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  * Return values
64*7c478bd9Sstevel@tonic-gate  *   0 on success, errno on failure.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate static long
67*7c478bd9Sstevel@tonic-gate tasksys_settaskid(projid_t projid, uint_t flags)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
70*7c478bd9Sstevel@tonic-gate 	kproject_t *oldpj;
71*7c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
72*7c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
73*7c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
74*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
75*7c478bd9Sstevel@tonic-gate 	int rctlfail = 0;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (secpolicy_tasksys(CRED()) != 0)
78*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (projid < 0 || projid > MAXPROJID)
81*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	if (flags & ~TASK_FINAL)
84*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
87*7c478bd9Sstevel@tonic-gate 	if (p->p_task->tk_flags & TASK_FINAL) {
88*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
89*7c478bd9Sstevel@tonic-gate 		return (set_errno(EACCES));
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * Try to stop all other lwps in the process while we're changing
95*7c478bd9Sstevel@tonic-gate 	 * our project.  This way, curthread doesn't need to grab its own
96*7c478bd9Sstevel@tonic-gate 	 * thread_lock to find its project ID (see curprojid()).  If this
97*7c478bd9Sstevel@tonic-gate 	 * is the /proc agent lwp, we know that the other lwps are already
98*7c478bd9Sstevel@tonic-gate 	 * held.  If we failed to hold all lwps, bail out and return EINTR.
99*7c478bd9Sstevel@tonic-gate 	 */
100*7c478bd9Sstevel@tonic-gate 	if (curthread != p->p_agenttp && !holdlwps(SHOLDFORK1))
101*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	 * Put a hold on our new project and make sure that nobody is
104*7c478bd9Sstevel@tonic-gate 	 * trying to bind it to a pool while we're joining.
105*7c478bd9Sstevel@tonic-gate 	 */
106*7c478bd9Sstevel@tonic-gate 	kpj = project_hold_by_id(projid, getzoneid(), PROJECT_HOLD_INSERT);
107*7c478bd9Sstevel@tonic-gate 	e.rcep_p.proj = kpj;
108*7c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROJECT;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
111*7c478bd9Sstevel@tonic-gate 	oldpj = p->p_task->tk_proj;
112*7c478bd9Sstevel@tonic-gate 	zone = p->p_zone;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if (kpj->kpj_nlwps + p->p_lwpcnt > kpj->kpj_nlwps_ctl)
117*7c478bd9Sstevel@tonic-gate 		if (rctl_test_entity(rc_project_nlwps, kpj->kpj_rctls, p, &e,
118*7c478bd9Sstevel@tonic-gate 		    p->p_lwpcnt, 0) & RCT_DENY)
119*7c478bd9Sstevel@tonic-gate 			rctlfail = 1;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (kpj->kpj_ntasks + 1 > kpj->kpj_ntasks_ctl)
122*7c478bd9Sstevel@tonic-gate 		if (rctl_test_entity(rc_project_ntasks, kpj->kpj_rctls, p, &e,
123*7c478bd9Sstevel@tonic-gate 		    1, 0) & RCT_DENY)
124*7c478bd9Sstevel@tonic-gate 			rctlfail = 1;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (rctlfail) {
127*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_nlwps_lock);
128*7c478bd9Sstevel@tonic-gate 		if (curthread != p->p_agenttp)
129*7c478bd9Sstevel@tonic-gate 			continuelwps(p);
130*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
131*7c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	kpj->kpj_nlwps += p->p_lwpcnt;
134*7c478bd9Sstevel@tonic-gate 	kpj->kpj_ntasks++;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	oldpj->kpj_nlwps -= p->p_lwpcnt;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
139*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	mutex_enter(&kpj->kpj_poolbind);
142*7c478bd9Sstevel@tonic-gate 	tk = task_create(projid, curproc->p_zone);
143*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * Returns with p_lock held.
146*7c478bd9Sstevel@tonic-gate 	 */
147*7c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, flags);
148*7c478bd9Sstevel@tonic-gate 	if (curthread != p->p_agenttp)
149*7c478bd9Sstevel@tonic-gate 		continuelwps(p);
150*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
151*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
152*7c478bd9Sstevel@tonic-gate 	mutex_exit(&kpj->kpj_poolbind);
153*7c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
154*7c478bd9Sstevel@tonic-gate 	project_rele(kpj);
155*7c478bd9Sstevel@tonic-gate 	return (tk->tk_tkid);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate  * taskid_t tasksys_gettaskid(void);
160*7c478bd9Sstevel@tonic-gate  *
161*7c478bd9Sstevel@tonic-gate  * Overview
162*7c478bd9Sstevel@tonic-gate  *   Return the current task ID for this process.
163*7c478bd9Sstevel@tonic-gate  *
164*7c478bd9Sstevel@tonic-gate  * Return value
165*7c478bd9Sstevel@tonic-gate  *   The ID for the task to which the current process belongs.
166*7c478bd9Sstevel@tonic-gate  */
167*7c478bd9Sstevel@tonic-gate static long
168*7c478bd9Sstevel@tonic-gate tasksys_gettaskid()
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	long ret;
171*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
174*7c478bd9Sstevel@tonic-gate 	ret = p->p_task->tk_tkid;
175*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
176*7c478bd9Sstevel@tonic-gate 	return (ret);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /*
180*7c478bd9Sstevel@tonic-gate  * projid_t tasksys_getprojid(void);
181*7c478bd9Sstevel@tonic-gate  *
182*7c478bd9Sstevel@tonic-gate  * Overview
183*7c478bd9Sstevel@tonic-gate  *   Return the current project ID for this process.
184*7c478bd9Sstevel@tonic-gate  *
185*7c478bd9Sstevel@tonic-gate  * Return value
186*7c478bd9Sstevel@tonic-gate  *   The ID for the project to which the current process belongs.
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate static long
189*7c478bd9Sstevel@tonic-gate tasksys_getprojid()
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	long ret;
192*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
195*7c478bd9Sstevel@tonic-gate 	ret = p->p_task->tk_proj->kpj_id;
196*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
197*7c478bd9Sstevel@tonic-gate 	return (ret);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate static int
201*7c478bd9Sstevel@tonic-gate tasksys_projlist_cb(kproject_t *kp, void *buf)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	projlist_walk_t *pw = (projlist_walk_t *)buf;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	if (pw && pw->pw_bufsz >= sizeof (projid_t)) {
206*7c478bd9Sstevel@tonic-gate 		*pw->pw_buf = kp->kpj_id;
207*7c478bd9Sstevel@tonic-gate 		pw->pw_buf++;
208*7c478bd9Sstevel@tonic-gate 		pw->pw_bufsz -= sizeof (projid_t);
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	return (0);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate /*
215*7c478bd9Sstevel@tonic-gate  * long tasksys_projlist(void *buf, size_t bufsz)
216*7c478bd9Sstevel@tonic-gate  *
217*7c478bd9Sstevel@tonic-gate  * Overview
218*7c478bd9Sstevel@tonic-gate  *   Return a buffer containing the project IDs of all currently active projects
219*7c478bd9Sstevel@tonic-gate  *   in the current zone.
220*7c478bd9Sstevel@tonic-gate  *
221*7c478bd9Sstevel@tonic-gate  * Return values
222*7c478bd9Sstevel@tonic-gate  *   The minimum size of a buffer sufficiently large to contain all of the
223*7c478bd9Sstevel@tonic-gate  *   active project IDs, or -1 if an error occurs during copyout.
224*7c478bd9Sstevel@tonic-gate  */
225*7c478bd9Sstevel@tonic-gate static long
226*7c478bd9Sstevel@tonic-gate tasksys_projlist(void *buf, size_t bufsz)
227*7c478bd9Sstevel@tonic-gate {
228*7c478bd9Sstevel@tonic-gate 	long ret = 0;
229*7c478bd9Sstevel@tonic-gate 	projlist_walk_t pw;
230*7c478bd9Sstevel@tonic-gate 	void *kbuf;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (buf == NULL || bufsz == 0)
233*7c478bd9Sstevel@tonic-gate 		return (project_walk_all(getzoneid(), tasksys_projlist_cb,
234*7c478bd9Sstevel@tonic-gate 		    NULL));
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	if (bufsz > MAX_PROJLIST_BUFSIZE)
237*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOMEM));
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	kbuf = pw.pw_buf = kmem_zalloc(bufsz, KM_SLEEP);
240*7c478bd9Sstevel@tonic-gate 	pw.pw_bufsz = bufsz;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	ret = project_walk_all(getzoneid(), tasksys_projlist_cb, &pw);
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	if (copyout(kbuf, buf, bufsz) == -1)
245*7c478bd9Sstevel@tonic-gate 		ret = set_errno(EFAULT);
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, bufsz);
248*7c478bd9Sstevel@tonic-gate 	return (ret);
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate long
252*7c478bd9Sstevel@tonic-gate tasksys(int code, projid_t projid, uint_t flags, void *projidbuf, size_t pbufsz)
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 	switch (code) {
255*7c478bd9Sstevel@tonic-gate 	case 0:
256*7c478bd9Sstevel@tonic-gate 		return (tasksys_settaskid(projid, flags));
257*7c478bd9Sstevel@tonic-gate 	case 1:
258*7c478bd9Sstevel@tonic-gate 		return (tasksys_gettaskid());
259*7c478bd9Sstevel@tonic-gate 	case 2:
260*7c478bd9Sstevel@tonic-gate 		return (tasksys_getprojid());
261*7c478bd9Sstevel@tonic-gate 	case 3:
262*7c478bd9Sstevel@tonic-gate 		return (tasksys_projlist(projidbuf, pbufsz));
263*7c478bd9Sstevel@tonic-gate 	default:
264*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate }
267