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