xref: /titanic_51/usr/src/uts/common/syscall/tasksys.c (revision c6939658adb0a356a77bc28f7df252ceb4a8f6cc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*c6939658Ssl108498  * Common Development and Distribution License (the "License").
6*c6939658Ssl108498  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*c6939658Ssl108498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * System calls for creating and inquiring about tasks and projects
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/thread.h>
367c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377c478bd9Sstevel@tonic-gate #include <sys/task.h>
387c478bd9Sstevel@tonic-gate #include <sys/systm.h>
397c478bd9Sstevel@tonic-gate #include <sys/project.h>
407c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
417c478bd9Sstevel@tonic-gate #include <sys/policy.h>
427c478bd9Sstevel@tonic-gate #include <sys/zone.h>
43*c6939658Ssl108498 #include <sys/rctl.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Limit projlist to 256k projects.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate #define	MAX_PROJLIST_BUFSIZE		1048576
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate typedef struct projlist_walk {
517c478bd9Sstevel@tonic-gate 	projid_t	*pw_buf;
527c478bd9Sstevel@tonic-gate 	size_t		pw_bufsz;
537c478bd9Sstevel@tonic-gate } projlist_walk_t;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * taskid_t tasksys_settaskid(projid_t projid, uint_t flags);
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * Overview
597c478bd9Sstevel@tonic-gate  *   Place the calling process in a new task if sufficiently privileged.  If the
607c478bd9Sstevel@tonic-gate  *   present task is finalized, the process may not create a new task.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * Return values
637c478bd9Sstevel@tonic-gate  *   0 on success, errno on failure.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static long
667c478bd9Sstevel@tonic-gate tasksys_settaskid(projid_t projid, uint_t flags)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
697c478bd9Sstevel@tonic-gate 	kproject_t *oldpj;
707c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
717c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
727c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
737c478bd9Sstevel@tonic-gate 	zone_t *zone;
747c478bd9Sstevel@tonic-gate 	int rctlfail = 0;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (secpolicy_tasksys(CRED()) != 0)
777c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (projid < 0 || projid > MAXPROJID)
807c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (flags & ~TASK_FINAL)
837c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
867c478bd9Sstevel@tonic-gate 	if (p->p_task->tk_flags & TASK_FINAL) {
877c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
887c478bd9Sstevel@tonic-gate 		return (set_errno(EACCES));
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Try to stop all other lwps in the process while we're changing
947c478bd9Sstevel@tonic-gate 	 * our project.  This way, curthread doesn't need to grab its own
957c478bd9Sstevel@tonic-gate 	 * thread_lock to find its project ID (see curprojid()).  If this
967c478bd9Sstevel@tonic-gate 	 * is the /proc agent lwp, we know that the other lwps are already
977c478bd9Sstevel@tonic-gate 	 * held.  If we failed to hold all lwps, bail out and return EINTR.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	if (curthread != p->p_agenttp && !holdlwps(SHOLDFORK1))
1007c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * Put a hold on our new project and make sure that nobody is
1037c478bd9Sstevel@tonic-gate 	 * trying to bind it to a pool while we're joining.
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	kpj = project_hold_by_id(projid, getzoneid(), PROJECT_HOLD_INSERT);
1067c478bd9Sstevel@tonic-gate 	e.rcep_p.proj = kpj;
1077c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROJECT;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1107c478bd9Sstevel@tonic-gate 	oldpj = p->p_task->tk_proj;
1117c478bd9Sstevel@tonic-gate 	zone = p->p_zone;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
114*c6939658Ssl108498 	mutex_enter(&zone->zone_rctl_lock);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (kpj->kpj_nlwps + p->p_lwpcnt > kpj->kpj_nlwps_ctl)
1177c478bd9Sstevel@tonic-gate 		if (rctl_test_entity(rc_project_nlwps, kpj->kpj_rctls, p, &e,
1187c478bd9Sstevel@tonic-gate 		    p->p_lwpcnt, 0) & RCT_DENY)
1197c478bd9Sstevel@tonic-gate 			rctlfail = 1;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (kpj->kpj_ntasks + 1 > kpj->kpj_ntasks_ctl)
1227c478bd9Sstevel@tonic-gate 		if (rctl_test_entity(rc_project_ntasks, kpj->kpj_rctls, p, &e,
1237c478bd9Sstevel@tonic-gate 		    1, 0) & RCT_DENY)
1247c478bd9Sstevel@tonic-gate 			rctlfail = 1;
1257c478bd9Sstevel@tonic-gate 
126*c6939658Ssl108498 	if (kpj->kpj_data.kpd_locked_mem + p->p_locked_mem
127*c6939658Ssl108498 	    > kpj->kpj_data.kpd_locked_mem_ctl)
128*c6939658Ssl108498 		if (rctl_test_entity(rc_project_locked_mem, kpj->kpj_rctls, p,
129*c6939658Ssl108498 		    &e, p->p_locked_mem, 0) &RCT_DENY)
130*c6939658Ssl108498 			rctlfail = 1;
131*c6939658Ssl108498 
1327c478bd9Sstevel@tonic-gate 	if (rctlfail) {
133*c6939658Ssl108498 		mutex_exit(&zone->zone_rctl_lock);
1347c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_nlwps_lock);
1357c478bd9Sstevel@tonic-gate 		if (curthread != p->p_agenttp)
1367c478bd9Sstevel@tonic-gate 			continuelwps(p);
1377c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1387c478bd9Sstevel@tonic-gate 		return (set_errno(EAGAIN));
1397c478bd9Sstevel@tonic-gate 	}
140*c6939658Ssl108498 	kpj->kpj_data.kpd_locked_mem += p->p_locked_mem;
1417c478bd9Sstevel@tonic-gate 	kpj->kpj_nlwps += p->p_lwpcnt;
1427c478bd9Sstevel@tonic-gate 	kpj->kpj_ntasks++;
1437c478bd9Sstevel@tonic-gate 
144*c6939658Ssl108498 	oldpj->kpj_data.kpd_locked_mem -= p->p_locked_mem;
1457c478bd9Sstevel@tonic-gate 	oldpj->kpj_nlwps -= p->p_lwpcnt;
1467c478bd9Sstevel@tonic-gate 
147*c6939658Ssl108498 	mutex_exit(&zone->zone_rctl_lock);
1487c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
1497c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	mutex_enter(&kpj->kpj_poolbind);
1527c478bd9Sstevel@tonic-gate 	tk = task_create(projid, curproc->p_zone);
1537c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Returns with p_lock held.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, flags);
1587c478bd9Sstevel@tonic-gate 	if (curthread != p->p_agenttp)
1597c478bd9Sstevel@tonic-gate 		continuelwps(p);
1607c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1617c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1627c478bd9Sstevel@tonic-gate 	mutex_exit(&kpj->kpj_poolbind);
1637c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
1647c478bd9Sstevel@tonic-gate 	project_rele(kpj);
1657c478bd9Sstevel@tonic-gate 	return (tk->tk_tkid);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * taskid_t tasksys_gettaskid(void);
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * Overview
1727c478bd9Sstevel@tonic-gate  *   Return the current task ID for this process.
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * Return value
1757c478bd9Sstevel@tonic-gate  *   The ID for the task to which the current process belongs.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate static long
1787c478bd9Sstevel@tonic-gate tasksys_gettaskid()
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	long ret;
1817c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
1847c478bd9Sstevel@tonic-gate 	ret = p->p_task->tk_tkid;
1857c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1867c478bd9Sstevel@tonic-gate 	return (ret);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * projid_t tasksys_getprojid(void);
1917c478bd9Sstevel@tonic-gate  *
1927c478bd9Sstevel@tonic-gate  * Overview
1937c478bd9Sstevel@tonic-gate  *   Return the current project ID for this process.
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  * Return value
1967c478bd9Sstevel@tonic-gate  *   The ID for the project to which the current process belongs.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate static long
1997c478bd9Sstevel@tonic-gate tasksys_getprojid()
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	long ret;
2027c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
2057c478bd9Sstevel@tonic-gate 	ret = p->p_task->tk_proj->kpj_id;
2067c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
2077c478bd9Sstevel@tonic-gate 	return (ret);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static int
2117c478bd9Sstevel@tonic-gate tasksys_projlist_cb(kproject_t *kp, void *buf)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	projlist_walk_t *pw = (projlist_walk_t *)buf;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (pw && pw->pw_bufsz >= sizeof (projid_t)) {
2167c478bd9Sstevel@tonic-gate 		*pw->pw_buf = kp->kpj_id;
2177c478bd9Sstevel@tonic-gate 		pw->pw_buf++;
2187c478bd9Sstevel@tonic-gate 		pw->pw_bufsz -= sizeof (projid_t);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return (0);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * long tasksys_projlist(void *buf, size_t bufsz)
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  * Overview
2287c478bd9Sstevel@tonic-gate  *   Return a buffer containing the project IDs of all currently active projects
2297c478bd9Sstevel@tonic-gate  *   in the current zone.
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * Return values
2327c478bd9Sstevel@tonic-gate  *   The minimum size of a buffer sufficiently large to contain all of the
2337c478bd9Sstevel@tonic-gate  *   active project IDs, or -1 if an error occurs during copyout.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static long
2367c478bd9Sstevel@tonic-gate tasksys_projlist(void *buf, size_t bufsz)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	long ret = 0;
2397c478bd9Sstevel@tonic-gate 	projlist_walk_t pw;
2407c478bd9Sstevel@tonic-gate 	void *kbuf;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (buf == NULL || bufsz == 0)
2437c478bd9Sstevel@tonic-gate 		return (project_walk_all(getzoneid(), tasksys_projlist_cb,
2447c478bd9Sstevel@tonic-gate 		    NULL));
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (bufsz > MAX_PROJLIST_BUFSIZE)
2477c478bd9Sstevel@tonic-gate 		return (set_errno(ENOMEM));
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	kbuf = pw.pw_buf = kmem_zalloc(bufsz, KM_SLEEP);
2507c478bd9Sstevel@tonic-gate 	pw.pw_bufsz = bufsz;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	ret = project_walk_all(getzoneid(), tasksys_projlist_cb, &pw);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (copyout(kbuf, buf, bufsz) == -1)
2557c478bd9Sstevel@tonic-gate 		ret = set_errno(EFAULT);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, bufsz);
2587c478bd9Sstevel@tonic-gate 	return (ret);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate long
2627c478bd9Sstevel@tonic-gate tasksys(int code, projid_t projid, uint_t flags, void *projidbuf, size_t pbufsz)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	switch (code) {
2657c478bd9Sstevel@tonic-gate 	case 0:
2667c478bd9Sstevel@tonic-gate 		return (tasksys_settaskid(projid, flags));
2677c478bd9Sstevel@tonic-gate 	case 1:
2687c478bd9Sstevel@tonic-gate 		return (tasksys_gettaskid());
2697c478bd9Sstevel@tonic-gate 	case 2:
2707c478bd9Sstevel@tonic-gate 		return (tasksys_getprojid());
2717c478bd9Sstevel@tonic-gate 	case 3:
2727c478bd9Sstevel@tonic-gate 		return (tasksys_projlist(projidbuf, pbufsz));
2737c478bd9Sstevel@tonic-gate 	default:
2747c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate }
277