160727d8bSWarner Losh /*- 28a6472b7SPeter Dufault * Copyright (c) 1996, 1997, 1998 38a6472b7SPeter Dufault * HD Associates, Inc. All rights reserved. 48a6472b7SPeter Dufault * 58a6472b7SPeter Dufault * Redistribution and use in source and binary forms, with or without 68a6472b7SPeter Dufault * modification, are permitted provided that the following conditions 78a6472b7SPeter Dufault * are met: 88a6472b7SPeter Dufault * 1. Redistributions of source code must retain the above copyright 98a6472b7SPeter Dufault * notice, this list of conditions and the following disclaimer. 108a6472b7SPeter Dufault * 2. Redistributions in binary form must reproduce the above copyright 118a6472b7SPeter Dufault * notice, this list of conditions and the following disclaimer in the 128a6472b7SPeter Dufault * documentation and/or other materials provided with the distribution. 138a6472b7SPeter Dufault * 3. All advertising materials mentioning features or use of this software 148a6472b7SPeter Dufault * must display the following acknowledgement: 158a6472b7SPeter Dufault * This product includes software developed by HD Associates, Inc 168a6472b7SPeter Dufault * 4. Neither the name of the author nor the names of any co-contributors 178a6472b7SPeter Dufault * may be used to endorse or promote products derived from this software 188a6472b7SPeter Dufault * without specific prior written permission. 198a6472b7SPeter Dufault * 208a6472b7SPeter Dufault * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND 218a6472b7SPeter Dufault * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 228a6472b7SPeter Dufault * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 238a6472b7SPeter Dufault * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE 248a6472b7SPeter Dufault * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 258a6472b7SPeter Dufault * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 268a6472b7SPeter Dufault * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 278a6472b7SPeter Dufault * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 288a6472b7SPeter Dufault * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 298a6472b7SPeter Dufault * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 308a6472b7SPeter Dufault * SUCH DAMAGE. 318a6472b7SPeter Dufault */ 328a6472b7SPeter Dufault 338a6472b7SPeter Dufault /* p1003_1b: Real Time common code. 348a6472b7SPeter Dufault */ 358a6472b7SPeter Dufault 36f4636c59SDavid E. O'Brien #include <sys/cdefs.h> 37f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$"); 38f4636c59SDavid E. O'Brien 39b565fb9eSAlfred Perlstein #include "opt_posix.h" 40b565fb9eSAlfred Perlstein 418a6472b7SPeter Dufault #include <sys/param.h> 428a6472b7SPeter Dufault #include <sys/systm.h> 438a6472b7SPeter Dufault #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 458a6472b7SPeter Dufault #include <sys/module.h> 46fb919e4dSMark Murray #include <sys/mutex.h> 47fb919e4dSMark Murray #include <sys/proc.h> 48fe24ab5fSJohn Baldwin #include <sys/syscallsubr.h> 498a6472b7SPeter Dufault #include <sys/sysctl.h> 50fb919e4dSMark Murray #include <sys/sysent.h> 51fb919e4dSMark Murray #include <sys/syslog.h> 52fb919e4dSMark Murray #include <sys/sysproto.h> 538a6472b7SPeter Dufault 548a6472b7SPeter Dufault #include <posix4/posix4.h> 558a6472b7SPeter Dufault 568a6472b7SPeter Dufault MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B"); 578a6472b7SPeter Dufault 582a61a110SPeter Dufault /* The system calls return ENOSYS if an entry is called that is 592a61a110SPeter Dufault * not run-time supported. I am also logging since some programs 602a61a110SPeter Dufault * start to use this when they shouldn't. That will be removed if annoying. 612a61a110SPeter Dufault */ 628a6472b7SPeter Dufault int 63b40ce416SJulian Elischer syscall_not_present(struct thread *td, const char *s, struct nosys_args *uap) 648a6472b7SPeter Dufault { 658a6472b7SPeter Dufault log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n", 66b40ce416SJulian Elischer td->td_proc->p_comm, td->td_proc->p_pid, s); 672a61a110SPeter Dufault 682a61a110SPeter Dufault /* a " return nosys(p, uap); " here causes a core dump. 692a61a110SPeter Dufault */ 702a61a110SPeter Dufault 712a61a110SPeter Dufault return ENOSYS; 728a6472b7SPeter Dufault } 738a6472b7SPeter Dufault 748a6472b7SPeter Dufault #if !defined(_KPOSIX_PRIORITY_SCHEDULING) 758a6472b7SPeter Dufault 76e9189611SPeter Wemm /* Not configured but loadable via a module: 778a6472b7SPeter Dufault */ 788a6472b7SPeter Dufault 798a6472b7SPeter Dufault static int sched_attach(void) 808a6472b7SPeter Dufault { 818a6472b7SPeter Dufault return 0; 828a6472b7SPeter Dufault } 838a6472b7SPeter Dufault 848a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_setparam) 858a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_getparam) 868a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_setscheduler) 878a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_getscheduler) 888a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_yield) 898a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max) 908a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min) 918a6472b7SPeter Dufault SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval) 928a6472b7SPeter Dufault 938a6472b7SPeter Dufault #else 948a6472b7SPeter Dufault 958a6472b7SPeter Dufault /* Configured in kernel version: 968a6472b7SPeter Dufault */ 978a6472b7SPeter Dufault static struct ksched *ksched; 988a6472b7SPeter Dufault 998a6472b7SPeter Dufault static int sched_attach(void) 1008a6472b7SPeter Dufault { 1018a6472b7SPeter Dufault int ret = ksched_attach(&ksched); 1028a6472b7SPeter Dufault 1038a6472b7SPeter Dufault if (ret == 0) 1048a6472b7SPeter Dufault p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1); 1058a6472b7SPeter Dufault 1068a6472b7SPeter Dufault return ret; 1078a6472b7SPeter Dufault } 1088a6472b7SPeter Dufault 1092afac34dSMatthew Dillon /* 1102afac34dSMatthew Dillon * MPSAFE 1112afac34dSMatthew Dillon */ 112b40ce416SJulian Elischer int sched_setparam(struct thread *td, 1138a6472b7SPeter Dufault struct sched_setparam_args *uap) 1148a6472b7SPeter Dufault { 115b40ce416SJulian Elischer struct thread *targettd; 1161af55356SRobert Watson struct proc *targetp; 1178a6472b7SPeter Dufault int e; 118aebde782SPeter Dufault struct sched_param sched_param; 1192afac34dSMatthew Dillon 120e8f7a952SRobert Watson e = copyin(uap->param, &sched_param, sizeof(sched_param)); 121e8f7a952SRobert Watson if (e) 122e8f7a952SRobert Watson return (e); 123aebde782SPeter Dufault 124e84b7987SRobert Watson if (uap->pid == 0) { 125b40ce416SJulian Elischer targetp = td->td_proc; 126b40ce416SJulian Elischer targettd = td; 127e84b7987SRobert Watson PROC_LOCK(targetp); 128e84b7987SRobert Watson } else { 129e84b7987SRobert Watson targetp = pfind(uap->pid); 1302afac34dSMatthew Dillon if (targetp == NULL) { 1312afac34dSMatthew Dillon e = ESRCH; 1322afac34dSMatthew Dillon goto done2; 1332afac34dSMatthew Dillon } 134079b7badSJulian Elischer targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 135e84b7987SRobert Watson } 136e84b7987SRobert Watson 137f44d9e24SJohn Baldwin e = p_cansched(td, targetp); 1382afac34dSMatthew Dillon if (e == 0) { 139b40ce416SJulian Elischer e = ksched_setparam(&td->td_retval[0], ksched, targettd, 14064e55bf4SRobert Watson (const struct sched_param *)&sched_param); 1412afac34dSMatthew Dillon } 142e631cff3SDavid Xu PROC_UNLOCK(targetp); 1432afac34dSMatthew Dillon done2: 14464e55bf4SRobert Watson return (e); 1458a6472b7SPeter Dufault } 1468a6472b7SPeter Dufault 1472afac34dSMatthew Dillon /* 1482afac34dSMatthew Dillon * MPSAFE 1492afac34dSMatthew Dillon */ 150b40ce416SJulian Elischer int sched_getparam(struct thread *td, 1518a6472b7SPeter Dufault struct sched_getparam_args *uap) 1528a6472b7SPeter Dufault { 1538a6472b7SPeter Dufault int e; 154aebde782SPeter Dufault struct sched_param sched_param; 155b40ce416SJulian Elischer struct thread *targettd; 1561af55356SRobert Watson struct proc *targetp; 1578a6472b7SPeter Dufault 158e84b7987SRobert Watson if (uap->pid == 0) { 159b40ce416SJulian Elischer targetp = td->td_proc; 160b40ce416SJulian Elischer targettd = td; 161e84b7987SRobert Watson PROC_LOCK(targetp); 162e84b7987SRobert Watson } else { 163e84b7987SRobert Watson targetp = pfind(uap->pid); 1642afac34dSMatthew Dillon if (targetp == NULL) { 1652afac34dSMatthew Dillon e = ESRCH; 1662afac34dSMatthew Dillon goto done2; 1672afac34dSMatthew Dillon } 168079b7badSJulian Elischer targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 169e84b7987SRobert Watson } 170e84b7987SRobert Watson 171f44d9e24SJohn Baldwin e = p_cansee(td, targetp); 172e631cff3SDavid Xu if (e == 0) { 173e631cff3SDavid Xu e = ksched_getparam(&td->td_retval[0], ksched, targettd, 174e631cff3SDavid Xu &sched_param); 175e631cff3SDavid Xu } 176e84b7987SRobert Watson PROC_UNLOCK(targetp); 1772afac34dSMatthew Dillon if (e == 0) 17864e55bf4SRobert Watson e = copyout(&sched_param, uap->param, sizeof(sched_param)); 1792afac34dSMatthew Dillon done2: 18064e55bf4SRobert Watson return (e); 1818a6472b7SPeter Dufault } 182b40ce416SJulian Elischer 1832afac34dSMatthew Dillon /* 1842afac34dSMatthew Dillon * MPSAFE 1852afac34dSMatthew Dillon */ 186b40ce416SJulian Elischer int sched_setscheduler(struct thread *td, 1878a6472b7SPeter Dufault struct sched_setscheduler_args *uap) 1888a6472b7SPeter Dufault { 1898a6472b7SPeter Dufault int e; 190aebde782SPeter Dufault struct sched_param sched_param; 191b40ce416SJulian Elischer struct thread *targettd; 1921af55356SRobert Watson struct proc *targetp; 1931af55356SRobert Watson 194e8f7a952SRobert Watson e = copyin(uap->param, &sched_param, sizeof(sched_param)); 195e8f7a952SRobert Watson if (e) 196e8f7a952SRobert Watson return (e); 197aebde782SPeter Dufault 198e84b7987SRobert Watson if (uap->pid == 0) { 199b40ce416SJulian Elischer targetp = td->td_proc; 200b40ce416SJulian Elischer targettd = td; 201e84b7987SRobert Watson PROC_LOCK(targetp); 202e84b7987SRobert Watson } else { 203e84b7987SRobert Watson targetp = pfind(uap->pid); 2042afac34dSMatthew Dillon if (targetp == NULL) { 2052afac34dSMatthew Dillon e = ESRCH; 2062afac34dSMatthew Dillon goto done2; 2072afac34dSMatthew Dillon } 208079b7badSJulian Elischer targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 209e84b7987SRobert Watson } 210e84b7987SRobert Watson 211f44d9e24SJohn Baldwin e = p_cansched(td, targetp); 2122afac34dSMatthew Dillon if (e == 0) { 213b40ce416SJulian Elischer e = ksched_setscheduler(&td->td_retval[0], ksched, targettd, 214b40ce416SJulian Elischer uap->policy, (const struct sched_param *)&sched_param); 2152afac34dSMatthew Dillon } 216e631cff3SDavid Xu PROC_UNLOCK(targetp); 2172afac34dSMatthew Dillon done2: 21864e55bf4SRobert Watson return (e); 2198a6472b7SPeter Dufault } 220b40ce416SJulian Elischer 2212afac34dSMatthew Dillon /* 2222afac34dSMatthew Dillon * MPSAFE 2232afac34dSMatthew Dillon */ 224b40ce416SJulian Elischer int sched_getscheduler(struct thread *td, 2258a6472b7SPeter Dufault struct sched_getscheduler_args *uap) 2268a6472b7SPeter Dufault { 2278a6472b7SPeter Dufault int e; 228b40ce416SJulian Elischer struct thread *targettd; 2291af55356SRobert Watson struct proc *targetp; 2308a6472b7SPeter Dufault 231e84b7987SRobert Watson if (uap->pid == 0) { 232b40ce416SJulian Elischer targetp = td->td_proc; 233b40ce416SJulian Elischer targettd = td; 234e84b7987SRobert Watson PROC_LOCK(targetp); 235e84b7987SRobert Watson } else { 236e84b7987SRobert Watson targetp = pfind(uap->pid); 2372afac34dSMatthew Dillon if (targetp == NULL) { 2382afac34dSMatthew Dillon e = ESRCH; 2392afac34dSMatthew Dillon goto done2; 2402afac34dSMatthew Dillon } 241079b7badSJulian Elischer targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 242e84b7987SRobert Watson } 243e84b7987SRobert Watson 244f44d9e24SJohn Baldwin e = p_cansee(td, targetp); 2452afac34dSMatthew Dillon if (e == 0) 246b40ce416SJulian Elischer e = ksched_getscheduler(&td->td_retval[0], ksched, targettd); 247e631cff3SDavid Xu PROC_UNLOCK(targetp); 24864e55bf4SRobert Watson 2492afac34dSMatthew Dillon done2: 25064e55bf4SRobert Watson return (e); 2518a6472b7SPeter Dufault } 252b40ce416SJulian Elischer 2532afac34dSMatthew Dillon /* 2542afac34dSMatthew Dillon * MPSAFE 2552afac34dSMatthew Dillon */ 256b40ce416SJulian Elischer int sched_yield(struct thread *td, 2578a6472b7SPeter Dufault struct sched_yield_args *uap) 2588a6472b7SPeter Dufault { 2592afac34dSMatthew Dillon int error; 2602afac34dSMatthew Dillon 261b40ce416SJulian Elischer error = ksched_yield(&td->td_retval[0], ksched); 2622afac34dSMatthew Dillon return (error); 2638a6472b7SPeter Dufault } 264b40ce416SJulian Elischer 2652afac34dSMatthew Dillon /* 2662afac34dSMatthew Dillon * MPSAFE 2672afac34dSMatthew Dillon */ 268b40ce416SJulian Elischer int sched_get_priority_max(struct thread *td, 2698a6472b7SPeter Dufault struct sched_get_priority_max_args *uap) 2708a6472b7SPeter Dufault { 2712afac34dSMatthew Dillon int error; 2722afac34dSMatthew Dillon 273b40ce416SJulian Elischer error = ksched_get_priority_max(&td->td_retval[0], ksched, uap->policy); 2742afac34dSMatthew Dillon return (error); 2758a6472b7SPeter Dufault } 276b40ce416SJulian Elischer 2772afac34dSMatthew Dillon /* 2782afac34dSMatthew Dillon * MPSAFE 2792afac34dSMatthew Dillon */ 280b40ce416SJulian Elischer int sched_get_priority_min(struct thread *td, 2818a6472b7SPeter Dufault struct sched_get_priority_min_args *uap) 2828a6472b7SPeter Dufault { 2832afac34dSMatthew Dillon int error; 284b40ce416SJulian Elischer 285b40ce416SJulian Elischer error = ksched_get_priority_min(&td->td_retval[0], ksched, uap->policy); 2862afac34dSMatthew Dillon return (error); 2878a6472b7SPeter Dufault } 288b40ce416SJulian Elischer 2892afac34dSMatthew Dillon /* 2902afac34dSMatthew Dillon * MPSAFE 2912afac34dSMatthew Dillon */ 292b40ce416SJulian Elischer int sched_rr_get_interval(struct thread *td, 2938a6472b7SPeter Dufault struct sched_rr_get_interval_args *uap) 2948a6472b7SPeter Dufault { 295fe24ab5fSJohn Baldwin struct timespec timespec; 296fe24ab5fSJohn Baldwin int error; 297fe24ab5fSJohn Baldwin 298fe24ab5fSJohn Baldwin error = kern_sched_rr_get_interval(td, uap->pid, ×pec); 299fe24ab5fSJohn Baldwin if (error == 0) 300fe24ab5fSJohn Baldwin error = copyout(×pec, uap->interval, sizeof(timespec)); 301fe24ab5fSJohn Baldwin return (error); 302fe24ab5fSJohn Baldwin } 303fe24ab5fSJohn Baldwin 304fe24ab5fSJohn Baldwin int kern_sched_rr_get_interval(struct thread *td, pid_t pid, 305fe24ab5fSJohn Baldwin struct timespec *ts) 306fe24ab5fSJohn Baldwin { 3078a6472b7SPeter Dufault int e; 308b40ce416SJulian Elischer struct thread *targettd; 3091af55356SRobert Watson struct proc *targetp; 3108a6472b7SPeter Dufault 311fe24ab5fSJohn Baldwin if (pid == 0) { 312b40ce416SJulian Elischer targettd = td; 313b40ce416SJulian Elischer targetp = td->td_proc; 314e84b7987SRobert Watson PROC_LOCK(targetp); 315e84b7987SRobert Watson } else { 316fe24ab5fSJohn Baldwin targetp = pfind(pid); 317e631cff3SDavid Xu if (targetp == NULL) 318fe24ab5fSJohn Baldwin return (ESRCH); 319e631cff3SDavid Xu 320079b7badSJulian Elischer targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 321e84b7987SRobert Watson } 322e84b7987SRobert Watson 323f44d9e24SJohn Baldwin e = p_cansee(td, targetp); 324a11acc6fSAlfred Perlstein if (e == 0) 325fe24ab5fSJohn Baldwin e = ksched_rr_get_interval(&td->td_retval[0], ksched, targettd, 326fe24ab5fSJohn Baldwin ts); 327fe24ab5fSJohn Baldwin PROC_UNLOCK(targetp); 32864e55bf4SRobert Watson return (e); 3298a6472b7SPeter Dufault } 3308a6472b7SPeter Dufault 3318a6472b7SPeter Dufault #endif 3328a6472b7SPeter Dufault 3338a6472b7SPeter Dufault static void p31binit(void *notused) 3348a6472b7SPeter Dufault { 3358a6472b7SPeter Dufault (void) sched_attach(); 336ce47711dSPeter Dufault p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE); 3378a6472b7SPeter Dufault } 3388a6472b7SPeter Dufault 3398a6472b7SPeter Dufault SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL); 340