10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52768Ssl108498 * Common Development and Distribution License (the "License").
62768Ssl108498 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12725SMenno.Lageman@Sun.COM * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
253247Sgjelinek
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * System calls for creating and inquiring about tasks and projects
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/errno.h>
330Sstevel@tonic-gate #include <sys/thread.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/task.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/project.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/policy.h>
400Sstevel@tonic-gate #include <sys/zone.h>
412768Ssl108498 #include <sys/rctl.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * Limit projlist to 256k projects.
450Sstevel@tonic-gate */
460Sstevel@tonic-gate #define MAX_PROJLIST_BUFSIZE 1048576
470Sstevel@tonic-gate
480Sstevel@tonic-gate typedef struct projlist_walk {
490Sstevel@tonic-gate projid_t *pw_buf;
500Sstevel@tonic-gate size_t pw_bufsz;
510Sstevel@tonic-gate } projlist_walk_t;
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * taskid_t tasksys_settaskid(projid_t projid, uint_t flags);
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * Overview
570Sstevel@tonic-gate * Place the calling process in a new task if sufficiently privileged. If the
580Sstevel@tonic-gate * present task is finalized, the process may not create a new task.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * Return values
610Sstevel@tonic-gate * 0 on success, errno on failure.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate static long
tasksys_settaskid(projid_t projid,uint_t flags)640Sstevel@tonic-gate tasksys_settaskid(projid_t projid, uint_t flags)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
670Sstevel@tonic-gate kproject_t *oldpj;
680Sstevel@tonic-gate kproject_t *kpj;
690Sstevel@tonic-gate task_t *tk, *oldtk;
700Sstevel@tonic-gate rctl_entity_p_t e;
710Sstevel@tonic-gate zone_t *zone;
720Sstevel@tonic-gate int rctlfail = 0;
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (secpolicy_tasksys(CRED()) != 0)
750Sstevel@tonic-gate return (set_errno(EPERM));
760Sstevel@tonic-gate
770Sstevel@tonic-gate if (projid < 0 || projid > MAXPROJID)
780Sstevel@tonic-gate return (set_errno(EINVAL));
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (flags & ~TASK_FINAL)
810Sstevel@tonic-gate return (set_errno(EINVAL));
820Sstevel@tonic-gate
830Sstevel@tonic-gate mutex_enter(&pidlock);
840Sstevel@tonic-gate if (p->p_task->tk_flags & TASK_FINAL) {
850Sstevel@tonic-gate mutex_exit(&pidlock);
860Sstevel@tonic-gate return (set_errno(EACCES));
870Sstevel@tonic-gate }
880Sstevel@tonic-gate mutex_exit(&pidlock);
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * Try to stop all other lwps in the process while we're changing
920Sstevel@tonic-gate * our project. This way, curthread doesn't need to grab its own
930Sstevel@tonic-gate * thread_lock to find its project ID (see curprojid()). If this
940Sstevel@tonic-gate * is the /proc agent lwp, we know that the other lwps are already
950Sstevel@tonic-gate * held. If we failed to hold all lwps, bail out and return EINTR.
960Sstevel@tonic-gate */
970Sstevel@tonic-gate if (curthread != p->p_agenttp && !holdlwps(SHOLDFORK1))
980Sstevel@tonic-gate return (set_errno(EINTR));
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * Put a hold on our new project and make sure that nobody is
1010Sstevel@tonic-gate * trying to bind it to a pool while we're joining.
1020Sstevel@tonic-gate */
1033247Sgjelinek kpj = project_hold_by_id(projid, p->p_zone, PROJECT_HOLD_INSERT);
1040Sstevel@tonic-gate e.rcep_p.proj = kpj;
1050Sstevel@tonic-gate e.rcep_t = RCENTITY_PROJECT;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate mutex_enter(&p->p_lock);
1080Sstevel@tonic-gate oldpj = p->p_task->tk_proj;
1090Sstevel@tonic-gate zone = p->p_zone;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock);
1123247Sgjelinek mutex_enter(&zone->zone_mem_lock);
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate if (kpj->kpj_nlwps + p->p_lwpcnt > kpj->kpj_nlwps_ctl)
1150Sstevel@tonic-gate if (rctl_test_entity(rc_project_nlwps, kpj->kpj_rctls, p, &e,
1160Sstevel@tonic-gate p->p_lwpcnt, 0) & RCT_DENY)
1170Sstevel@tonic-gate rctlfail = 1;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (kpj->kpj_ntasks + 1 > kpj->kpj_ntasks_ctl)
1200Sstevel@tonic-gate if (rctl_test_entity(rc_project_ntasks, kpj->kpj_rctls, p, &e,
1210Sstevel@tonic-gate 1, 0) & RCT_DENY)
1220Sstevel@tonic-gate rctlfail = 1;
1230Sstevel@tonic-gate
124*12725SMenno.Lageman@Sun.COM if (kpj != proj0p && kpj->kpj_nprocs + 1 > kpj->kpj_nprocs_ctl)
125*12725SMenno.Lageman@Sun.COM if (rctl_test_entity(rc_project_nprocs, kpj->kpj_rctls, p, &e,
126*12725SMenno.Lageman@Sun.COM 1, 0) & RCT_DENY)
127*12725SMenno.Lageman@Sun.COM rctlfail = 1;
128*12725SMenno.Lageman@Sun.COM
1293916Skrishna if (kpj->kpj_data.kpd_locked_mem + p->p_locked_mem >
1303916Skrishna kpj->kpj_data.kpd_locked_mem_ctl)
1312768Ssl108498 if (rctl_test_entity(rc_project_locked_mem, kpj->kpj_rctls, p,
1323916Skrishna &e, p->p_locked_mem, 0) & RCT_DENY)
1333916Skrishna rctlfail = 1;
1343916Skrishna
1353916Skrishna mutex_enter(&(kpj->kpj_data.kpd_crypto_lock));
1363916Skrishna if (kpj->kpj_data.kpd_crypto_mem + p->p_crypto_mem >
1373916Skrishna kpj->kpj_data.kpd_crypto_mem_ctl)
1383916Skrishna if (rctl_test_entity(rc_project_crypto_mem, kpj->kpj_rctls, p,
1393916Skrishna &e, p->p_crypto_mem, 0) & RCT_DENY)
1402768Ssl108498 rctlfail = 1;
1412768Ssl108498
1420Sstevel@tonic-gate if (rctlfail) {
1433916Skrishna mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
1443247Sgjelinek mutex_exit(&zone->zone_mem_lock);
1450Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock);
1460Sstevel@tonic-gate if (curthread != p->p_agenttp)
1470Sstevel@tonic-gate continuelwps(p);
1480Sstevel@tonic-gate mutex_exit(&p->p_lock);
1490Sstevel@tonic-gate return (set_errno(EAGAIN));
1500Sstevel@tonic-gate }
1513916Skrishna kpj->kpj_data.kpd_crypto_mem += p->p_crypto_mem;
1523916Skrishna mutex_exit(&(kpj->kpj_data.kpd_crypto_lock));
1532768Ssl108498 kpj->kpj_data.kpd_locked_mem += p->p_locked_mem;
1540Sstevel@tonic-gate kpj->kpj_nlwps += p->p_lwpcnt;
1550Sstevel@tonic-gate kpj->kpj_ntasks++;
156*12725SMenno.Lageman@Sun.COM kpj->kpj_nprocs++;
1570Sstevel@tonic-gate
1582768Ssl108498 oldpj->kpj_data.kpd_locked_mem -= p->p_locked_mem;
1593916Skrishna mutex_enter(&(oldpj->kpj_data.kpd_crypto_lock));
1603916Skrishna oldpj->kpj_data.kpd_crypto_mem -= p->p_crypto_mem;
1613916Skrishna mutex_exit(&(oldpj->kpj_data.kpd_crypto_lock));
1620Sstevel@tonic-gate oldpj->kpj_nlwps -= p->p_lwpcnt;
163*12725SMenno.Lageman@Sun.COM oldpj->kpj_nprocs--;
1640Sstevel@tonic-gate
1653247Sgjelinek mutex_exit(&zone->zone_mem_lock);
1660Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock);
1670Sstevel@tonic-gate mutex_exit(&p->p_lock);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate mutex_enter(&kpj->kpj_poolbind);
1700Sstevel@tonic-gate tk = task_create(projid, curproc->p_zone);
1710Sstevel@tonic-gate mutex_enter(&cpu_lock);
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * Returns with p_lock held.
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate oldtk = task_join(tk, flags);
1760Sstevel@tonic-gate if (curthread != p->p_agenttp)
1770Sstevel@tonic-gate continuelwps(p);
1780Sstevel@tonic-gate mutex_exit(&p->p_lock);
1790Sstevel@tonic-gate mutex_exit(&cpu_lock);
1800Sstevel@tonic-gate mutex_exit(&kpj->kpj_poolbind);
1810Sstevel@tonic-gate task_rele(oldtk);
1820Sstevel@tonic-gate project_rele(kpj);
1830Sstevel@tonic-gate return (tk->tk_tkid);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate * taskid_t tasksys_gettaskid(void);
1880Sstevel@tonic-gate *
1890Sstevel@tonic-gate * Overview
1900Sstevel@tonic-gate * Return the current task ID for this process.
1910Sstevel@tonic-gate *
1920Sstevel@tonic-gate * Return value
1930Sstevel@tonic-gate * The ID for the task to which the current process belongs.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate static long
tasksys_gettaskid()1960Sstevel@tonic-gate tasksys_gettaskid()
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate long ret;
1990Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate mutex_enter(&pidlock);
2020Sstevel@tonic-gate ret = p->p_task->tk_tkid;
2030Sstevel@tonic-gate mutex_exit(&pidlock);
2040Sstevel@tonic-gate return (ret);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * projid_t tasksys_getprojid(void);
2090Sstevel@tonic-gate *
2100Sstevel@tonic-gate * Overview
2110Sstevel@tonic-gate * Return the current project ID for this process.
2120Sstevel@tonic-gate *
2130Sstevel@tonic-gate * Return value
2140Sstevel@tonic-gate * The ID for the project to which the current process belongs.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate static long
tasksys_getprojid()2170Sstevel@tonic-gate tasksys_getprojid()
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate long ret;
2200Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate mutex_enter(&pidlock);
2230Sstevel@tonic-gate ret = p->p_task->tk_proj->kpj_id;
2240Sstevel@tonic-gate mutex_exit(&pidlock);
2250Sstevel@tonic-gate return (ret);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate static int
tasksys_projlist_cb(kproject_t * kp,void * buf)2290Sstevel@tonic-gate tasksys_projlist_cb(kproject_t *kp, void *buf)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate projlist_walk_t *pw = (projlist_walk_t *)buf;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (pw && pw->pw_bufsz >= sizeof (projid_t)) {
2340Sstevel@tonic-gate *pw->pw_buf = kp->kpj_id;
2350Sstevel@tonic-gate pw->pw_buf++;
2360Sstevel@tonic-gate pw->pw_bufsz -= sizeof (projid_t);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate return (0);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate * long tasksys_projlist(void *buf, size_t bufsz)
2440Sstevel@tonic-gate *
2450Sstevel@tonic-gate * Overview
2460Sstevel@tonic-gate * Return a buffer containing the project IDs of all currently active projects
2470Sstevel@tonic-gate * in the current zone.
2480Sstevel@tonic-gate *
2490Sstevel@tonic-gate * Return values
2500Sstevel@tonic-gate * The minimum size of a buffer sufficiently large to contain all of the
2510Sstevel@tonic-gate * active project IDs, or -1 if an error occurs during copyout.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate static long
tasksys_projlist(void * buf,size_t bufsz)2540Sstevel@tonic-gate tasksys_projlist(void *buf, size_t bufsz)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate long ret = 0;
2570Sstevel@tonic-gate projlist_walk_t pw;
2580Sstevel@tonic-gate void *kbuf;
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate if (buf == NULL || bufsz == 0)
2610Sstevel@tonic-gate return (project_walk_all(getzoneid(), tasksys_projlist_cb,
2620Sstevel@tonic-gate NULL));
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate if (bufsz > MAX_PROJLIST_BUFSIZE)
2650Sstevel@tonic-gate return (set_errno(ENOMEM));
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate kbuf = pw.pw_buf = kmem_zalloc(bufsz, KM_SLEEP);
2680Sstevel@tonic-gate pw.pw_bufsz = bufsz;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate ret = project_walk_all(getzoneid(), tasksys_projlist_cb, &pw);
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (copyout(kbuf, buf, bufsz) == -1)
2730Sstevel@tonic-gate ret = set_errno(EFAULT);
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate kmem_free(kbuf, bufsz);
2760Sstevel@tonic-gate return (ret);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate long
tasksys(int code,projid_t projid,uint_t flags,void * projidbuf,size_t pbufsz)2800Sstevel@tonic-gate tasksys(int code, projid_t projid, uint_t flags, void *projidbuf, size_t pbufsz)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate switch (code) {
2830Sstevel@tonic-gate case 0:
2840Sstevel@tonic-gate return (tasksys_settaskid(projid, flags));
2850Sstevel@tonic-gate case 1:
2860Sstevel@tonic-gate return (tasksys_gettaskid());
2870Sstevel@tonic-gate case 2:
2880Sstevel@tonic-gate return (tasksys_getprojid());
2890Sstevel@tonic-gate case 3:
2900Sstevel@tonic-gate return (tasksys_projlist(projidbuf, pbufsz));
2910Sstevel@tonic-gate default:
2920Sstevel@tonic-gate return (set_errno(EINVAL));
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate }
295