xref: /titanic_50/usr/src/uts/common/syscall/tasksys.c (revision 8b464eb836173b92f2b7a65623cd06c8c3c59289)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
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 #include <sys/rctl.h>
45 
46 /*
47  * Limit projlist to 256k projects.
48  */
49 #define	MAX_PROJLIST_BUFSIZE		1048576
50 
51 typedef struct projlist_walk {
52 	projid_t	*pw_buf;
53 	size_t		pw_bufsz;
54 } projlist_walk_t;
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, p->p_zone, 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 	mutex_enter(&zone->zone_mem_lock);
116 
117 	if (kpj->kpj_nlwps + p->p_lwpcnt > kpj->kpj_nlwps_ctl)
118 		if (rctl_test_entity(rc_project_nlwps, kpj->kpj_rctls, p, &e,
119 		    p->p_lwpcnt, 0) & RCT_DENY)
120 			rctlfail = 1;
121 
122 	if (kpj->kpj_ntasks + 1 > kpj->kpj_ntasks_ctl)
123 		if (rctl_test_entity(rc_project_ntasks, kpj->kpj_rctls, p, &e,
124 		    1, 0) & RCT_DENY)
125 			rctlfail = 1;
126 
127 	if (kpj->kpj_data.kpd_locked_mem + p->p_locked_mem
128 	    > kpj->kpj_data.kpd_locked_mem_ctl)
129 		if (rctl_test_entity(rc_project_locked_mem, kpj->kpj_rctls, p,
130 		    &e, p->p_locked_mem, 0) &RCT_DENY)
131 			rctlfail = 1;
132 
133 	if (rctlfail) {
134 		mutex_exit(&zone->zone_mem_lock);
135 		mutex_exit(&zone->zone_nlwps_lock);
136 		if (curthread != p->p_agenttp)
137 			continuelwps(p);
138 		mutex_exit(&p->p_lock);
139 		return (set_errno(EAGAIN));
140 	}
141 	kpj->kpj_data.kpd_locked_mem += p->p_locked_mem;
142 	kpj->kpj_nlwps += p->p_lwpcnt;
143 	kpj->kpj_ntasks++;
144 
145 	oldpj->kpj_data.kpd_locked_mem -= p->p_locked_mem;
146 	oldpj->kpj_nlwps -= p->p_lwpcnt;
147 
148 	mutex_exit(&zone->zone_mem_lock);
149 	mutex_exit(&zone->zone_nlwps_lock);
150 	mutex_exit(&p->p_lock);
151 
152 	mutex_enter(&kpj->kpj_poolbind);
153 	tk = task_create(projid, curproc->p_zone);
154 	mutex_enter(&cpu_lock);
155 	/*
156 	 * Returns with p_lock held.
157 	 */
158 	oldtk = task_join(tk, flags);
159 	if (curthread != p->p_agenttp)
160 		continuelwps(p);
161 	mutex_exit(&p->p_lock);
162 	mutex_exit(&cpu_lock);
163 	mutex_exit(&kpj->kpj_poolbind);
164 	task_rele(oldtk);
165 	project_rele(kpj);
166 	return (tk->tk_tkid);
167 }
168 
169 /*
170  * taskid_t tasksys_gettaskid(void);
171  *
172  * Overview
173  *   Return the current task ID for this process.
174  *
175  * Return value
176  *   The ID for the task to which the current process belongs.
177  */
178 static long
179 tasksys_gettaskid()
180 {
181 	long ret;
182 	proc_t *p = ttoproc(curthread);
183 
184 	mutex_enter(&pidlock);
185 	ret = p->p_task->tk_tkid;
186 	mutex_exit(&pidlock);
187 	return (ret);
188 }
189 
190 /*
191  * projid_t tasksys_getprojid(void);
192  *
193  * Overview
194  *   Return the current project ID for this process.
195  *
196  * Return value
197  *   The ID for the project to which the current process belongs.
198  */
199 static long
200 tasksys_getprojid()
201 {
202 	long ret;
203 	proc_t *p = ttoproc(curthread);
204 
205 	mutex_enter(&pidlock);
206 	ret = p->p_task->tk_proj->kpj_id;
207 	mutex_exit(&pidlock);
208 	return (ret);
209 }
210 
211 static int
212 tasksys_projlist_cb(kproject_t *kp, void *buf)
213 {
214 	projlist_walk_t *pw = (projlist_walk_t *)buf;
215 
216 	if (pw && pw->pw_bufsz >= sizeof (projid_t)) {
217 		*pw->pw_buf = kp->kpj_id;
218 		pw->pw_buf++;
219 		pw->pw_bufsz -= sizeof (projid_t);
220 	}
221 
222 	return (0);
223 }
224 
225 /*
226  * long tasksys_projlist(void *buf, size_t bufsz)
227  *
228  * Overview
229  *   Return a buffer containing the project IDs of all currently active projects
230  *   in the current zone.
231  *
232  * Return values
233  *   The minimum size of a buffer sufficiently large to contain all of the
234  *   active project IDs, or -1 if an error occurs during copyout.
235  */
236 static long
237 tasksys_projlist(void *buf, size_t bufsz)
238 {
239 	long ret = 0;
240 	projlist_walk_t pw;
241 	void *kbuf;
242 
243 	if (buf == NULL || bufsz == 0)
244 		return (project_walk_all(getzoneid(), tasksys_projlist_cb,
245 		    NULL));
246 
247 	if (bufsz > MAX_PROJLIST_BUFSIZE)
248 		return (set_errno(ENOMEM));
249 
250 	kbuf = pw.pw_buf = kmem_zalloc(bufsz, KM_SLEEP);
251 	pw.pw_bufsz = bufsz;
252 
253 	ret = project_walk_all(getzoneid(), tasksys_projlist_cb, &pw);
254 
255 	if (copyout(kbuf, buf, bufsz) == -1)
256 		ret = set_errno(EFAULT);
257 
258 	kmem_free(kbuf, bufsz);
259 	return (ret);
260 }
261 
262 long
263 tasksys(int code, projid_t projid, uint_t flags, void *projidbuf, size_t pbufsz)
264 {
265 	switch (code) {
266 	case 0:
267 		return (tasksys_settaskid(projid, flags));
268 	case 1:
269 		return (tasksys_gettaskid());
270 	case 2:
271 		return (tasksys_getprojid());
272 	case 3:
273 		return (tasksys_projlist(projidbuf, pbufsz));
274 	default:
275 		return (set_errno(EINVAL));
276 	}
277 }
278