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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/param.h> 357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 367c478bd9Sstevel@tonic-gate #include <sys/cred.h> 377c478bd9Sstevel@tonic-gate #include <sys/proc.h> 387c478bd9Sstevel@tonic-gate #include <sys/pcb.h> 397c478bd9Sstevel@tonic-gate #include <sys/signal.h> 407c478bd9Sstevel@tonic-gate #include <sys/user.h> 417c478bd9Sstevel@tonic-gate #include <sys/priocntl.h> 427c478bd9Sstevel@tonic-gate #include <sys/class.h> 437c478bd9Sstevel@tonic-gate #include <sys/disp.h> 447c478bd9Sstevel@tonic-gate #include <sys/procset.h> 457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 467c478bd9Sstevel@tonic-gate #include <sys/debug.h> 477c478bd9Sstevel@tonic-gate #include <sys/rt.h> 487c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h> 497c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 507c478bd9Sstevel@tonic-gate #include <sys/systm.h> 517c478bd9Sstevel@tonic-gate #include <sys/errno.h> 527c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 537c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h> 547c478bd9Sstevel@tonic-gate #include <sys/time.h> 557c478bd9Sstevel@tonic-gate #include <sys/policy.h> 567c478bd9Sstevel@tonic-gate #include <sys/sdt.h> 577c478bd9Sstevel@tonic-gate #include <sys/cpupart.h> 587c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate static pri_t rt_init(id_t, int, classfuncs_t **); 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate static struct sclass csw = { 637c478bd9Sstevel@tonic-gate "RT", 647c478bd9Sstevel@tonic-gate rt_init, 657c478bd9Sstevel@tonic-gate 0 667c478bd9Sstevel@tonic-gate }; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static struct modlsched modlsched = { 697c478bd9Sstevel@tonic-gate &mod_schedops, "realtime scheduling class", &csw 707c478bd9Sstevel@tonic-gate }; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 737c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlsched, NULL 747c478bd9Sstevel@tonic-gate }; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate int 777c478bd9Sstevel@tonic-gate _init() 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate int 837c478bd9Sstevel@tonic-gate _fini() 847c478bd9Sstevel@tonic-gate { 857c478bd9Sstevel@tonic-gate return (EBUSY); /* don't remove RT for now */ 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate int 897c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 907c478bd9Sstevel@tonic-gate { 917c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Class specific code for the real-time class 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * Extern declarations for variables defined in the rt master file 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate #define RTMAXPRI 59 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate pri_t rt_maxpri = RTMAXPRI; /* maximum real-time priority */ 1057c478bd9Sstevel@tonic-gate rtdpent_t *rt_dptbl; /* real-time dispatcher parameter table */ 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * control flags (kparms->rt_cflags). 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate #define RT_DOPRI 0x01 /* change priority */ 1117c478bd9Sstevel@tonic-gate #define RT_DOTQ 0x02 /* change RT time quantum */ 1127c478bd9Sstevel@tonic-gate #define RT_DOSIG 0x04 /* change RT time quantum signal */ 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate static int rt_admin(caddr_t, cred_t *); 1157c478bd9Sstevel@tonic-gate static int rt_enterclass(kthread_t *, id_t, void *, cred_t *, void *); 1167c478bd9Sstevel@tonic-gate static int rt_fork(kthread_t *, kthread_t *, void *); 1177c478bd9Sstevel@tonic-gate static int rt_getclinfo(void *); 1187c478bd9Sstevel@tonic-gate static int rt_getclpri(pcpri_t *); 1197c478bd9Sstevel@tonic-gate static int rt_parmsin(void *); 1207c478bd9Sstevel@tonic-gate static int rt_parmsout(void *, pc_vaparms_t *); 1217c478bd9Sstevel@tonic-gate static int rt_vaparmsin(void *, pc_vaparms_t *); 1227c478bd9Sstevel@tonic-gate static int rt_vaparmsout(void *, pc_vaparms_t *); 1237c478bd9Sstevel@tonic-gate static int rt_parmsset(kthread_t *, void *, id_t, cred_t *); 1247c478bd9Sstevel@tonic-gate static int rt_donice(kthread_t *, cred_t *, int, int *); 1257c478bd9Sstevel@tonic-gate static void rt_exitclass(void *); 1267c478bd9Sstevel@tonic-gate static int rt_canexit(kthread_t *, cred_t *); 1277c478bd9Sstevel@tonic-gate static void rt_forkret(kthread_t *, kthread_t *); 1287c478bd9Sstevel@tonic-gate static void rt_nullsys(); 1297c478bd9Sstevel@tonic-gate static void rt_parmsget(kthread_t *, void *); 1307c478bd9Sstevel@tonic-gate static void rt_preempt(kthread_t *); 1317c478bd9Sstevel@tonic-gate static void rt_setrun(kthread_t *); 1327c478bd9Sstevel@tonic-gate static void rt_tick(kthread_t *); 1337c478bd9Sstevel@tonic-gate static void rt_wakeup(kthread_t *); 1347c478bd9Sstevel@tonic-gate static pri_t rt_swapin(kthread_t *, int); 1357c478bd9Sstevel@tonic-gate static pri_t rt_swapout(kthread_t *, int); 1367c478bd9Sstevel@tonic-gate static pri_t rt_globpri(kthread_t *); 1377c478bd9Sstevel@tonic-gate static void rt_yield(kthread_t *); 1387c478bd9Sstevel@tonic-gate static int rt_alloc(void **, int); 1397c478bd9Sstevel@tonic-gate static void rt_free(void *); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate static void rt_change_priority(kthread_t *, rtproc_t *); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate static id_t rt_cid; /* real-time class ID */ 1447c478bd9Sstevel@tonic-gate static rtproc_t rt_plisthead; /* dummy rtproc at head of rtproc list */ 1457c478bd9Sstevel@tonic-gate static kmutex_t rt_dptblock; /* protects realtime dispatch table */ 1467c478bd9Sstevel@tonic-gate static kmutex_t rt_list_lock; /* protects RT thread list */ 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate extern rtdpent_t *rt_getdptbl(void); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate static struct classfuncs rt_classfuncs = { 1517c478bd9Sstevel@tonic-gate /* class ops */ 1527c478bd9Sstevel@tonic-gate rt_admin, 1537c478bd9Sstevel@tonic-gate rt_getclinfo, 1547c478bd9Sstevel@tonic-gate rt_parmsin, 1557c478bd9Sstevel@tonic-gate rt_parmsout, 1567c478bd9Sstevel@tonic-gate rt_vaparmsin, 1577c478bd9Sstevel@tonic-gate rt_vaparmsout, 1587c478bd9Sstevel@tonic-gate rt_getclpri, 1597c478bd9Sstevel@tonic-gate rt_alloc, 1607c478bd9Sstevel@tonic-gate rt_free, 1617c478bd9Sstevel@tonic-gate /* thread ops */ 1627c478bd9Sstevel@tonic-gate rt_enterclass, 1637c478bd9Sstevel@tonic-gate rt_exitclass, 1647c478bd9Sstevel@tonic-gate rt_canexit, 1657c478bd9Sstevel@tonic-gate rt_fork, 1667c478bd9Sstevel@tonic-gate rt_forkret, 1677c478bd9Sstevel@tonic-gate rt_parmsget, 1687c478bd9Sstevel@tonic-gate rt_parmsset, 1697c478bd9Sstevel@tonic-gate rt_nullsys, /* stop */ 1707c478bd9Sstevel@tonic-gate rt_nullsys, /* exit */ 1717c478bd9Sstevel@tonic-gate rt_nullsys, /* active */ 1727c478bd9Sstevel@tonic-gate rt_nullsys, /* inactive */ 1737c478bd9Sstevel@tonic-gate rt_swapin, 1747c478bd9Sstevel@tonic-gate rt_swapout, 1757c478bd9Sstevel@tonic-gate rt_nullsys, /* trapret */ 1767c478bd9Sstevel@tonic-gate rt_preempt, 1777c478bd9Sstevel@tonic-gate rt_setrun, 1787c478bd9Sstevel@tonic-gate rt_nullsys, /* sleep */ 1797c478bd9Sstevel@tonic-gate rt_tick, 1807c478bd9Sstevel@tonic-gate rt_wakeup, 1817c478bd9Sstevel@tonic-gate rt_donice, 1827c478bd9Sstevel@tonic-gate rt_globpri, 1837c478bd9Sstevel@tonic-gate rt_nullsys, /* set_process_group */ 1847c478bd9Sstevel@tonic-gate rt_yield, 1857c478bd9Sstevel@tonic-gate }; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* 1887c478bd9Sstevel@tonic-gate * Real-time class initialization. Called by dispinit() at boot time. 1897c478bd9Sstevel@tonic-gate * We can ignore the clparmsz argument since we know that the smallest 1907c478bd9Sstevel@tonic-gate * possible parameter buffer is big enough for us. 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1937c478bd9Sstevel@tonic-gate pri_t 1947c478bd9Sstevel@tonic-gate rt_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp) 1957c478bd9Sstevel@tonic-gate { 1967c478bd9Sstevel@tonic-gate rt_dptbl = rt_getdptbl(); 1977c478bd9Sstevel@tonic-gate rt_cid = cid; /* Record our class ID */ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* 2007c478bd9Sstevel@tonic-gate * Initialize the rtproc list. 2017c478bd9Sstevel@tonic-gate */ 2027c478bd9Sstevel@tonic-gate rt_plisthead.rt_next = rt_plisthead.rt_prev = &rt_plisthead; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* 2057c478bd9Sstevel@tonic-gate * We're required to return a pointer to our classfuncs 2067c478bd9Sstevel@tonic-gate * structure and the highest global priority value we use. 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate *clfuncspp = &rt_classfuncs; 2097c478bd9Sstevel@tonic-gate mutex_init(&rt_dptblock, NULL, MUTEX_DEFAULT, NULL); 2107c478bd9Sstevel@tonic-gate mutex_init(&rt_list_lock, NULL, MUTEX_DEFAULT, NULL); 2117c478bd9Sstevel@tonic-gate return (rt_dptbl[rt_maxpri].rt_globpri); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * Get or reset the rt_dptbl values per the user's request. 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2187c478bd9Sstevel@tonic-gate static int 2197c478bd9Sstevel@tonic-gate rt_admin(caddr_t uaddr, cred_t *reqpcredp) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate rtadmin_t rtadmin; 2227c478bd9Sstevel@tonic-gate rtdpent_t *tmpdpp; 2237c478bd9Sstevel@tonic-gate size_t userdpsz; 2247c478bd9Sstevel@tonic-gate size_t rtdpsz; 2257c478bd9Sstevel@tonic-gate int i; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 2287c478bd9Sstevel@tonic-gate if (copyin(uaddr, &rtadmin, sizeof (rtadmin_t))) 2297c478bd9Sstevel@tonic-gate return (EFAULT); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2327c478bd9Sstevel@tonic-gate else { 2337c478bd9Sstevel@tonic-gate /* rtadmin struct from ILP32 callers */ 2347c478bd9Sstevel@tonic-gate rtadmin32_t rtadmin32; 2357c478bd9Sstevel@tonic-gate if (copyin(uaddr, &rtadmin32, sizeof (rtadmin32_t))) 2367c478bd9Sstevel@tonic-gate return (EFAULT); 2377c478bd9Sstevel@tonic-gate rtadmin.rt_dpents = 2387c478bd9Sstevel@tonic-gate (struct rtdpent *)(uintptr_t)rtadmin32.rt_dpents; 2397c478bd9Sstevel@tonic-gate rtadmin.rt_ndpents = rtadmin32.rt_ndpents; 2407c478bd9Sstevel@tonic-gate rtadmin.rt_cmd = rtadmin32.rt_cmd; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate rtdpsz = (rt_maxpri + 1) * sizeof (rtdpent_t); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate switch (rtadmin.rt_cmd) { 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate case RT_GETDPSIZE: 2497c478bd9Sstevel@tonic-gate rtadmin.rt_ndpents = rt_maxpri + 1; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 2527c478bd9Sstevel@tonic-gate if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t))) 2537c478bd9Sstevel@tonic-gate return (EFAULT); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2567c478bd9Sstevel@tonic-gate else { 2577c478bd9Sstevel@tonic-gate /* return rtadmin struct to ILP32 callers */ 2587c478bd9Sstevel@tonic-gate rtadmin32_t rtadmin32; 2597c478bd9Sstevel@tonic-gate rtadmin32.rt_dpents = 2607c478bd9Sstevel@tonic-gate (caddr32_t)(uintptr_t)rtadmin.rt_dpents; 2617c478bd9Sstevel@tonic-gate rtadmin32.rt_ndpents = rtadmin.rt_ndpents; 2627c478bd9Sstevel@tonic-gate rtadmin32.rt_cmd = rtadmin.rt_cmd; 2637c478bd9Sstevel@tonic-gate if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t))) 2647c478bd9Sstevel@tonic-gate return (EFAULT); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate break; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate case RT_GETDPTBL: 2717c478bd9Sstevel@tonic-gate userdpsz = MIN(rtadmin.rt_ndpents * sizeof (rtdpent_t), 2727c478bd9Sstevel@tonic-gate rtdpsz); 2737c478bd9Sstevel@tonic-gate if (copyout(rt_dptbl, rtadmin.rt_dpents, userdpsz)) 2747c478bd9Sstevel@tonic-gate return (EFAULT); 2757c478bd9Sstevel@tonic-gate rtadmin.rt_ndpents = userdpsz / sizeof (rtdpent_t); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) { 2787c478bd9Sstevel@tonic-gate if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t))) 2797c478bd9Sstevel@tonic-gate return (EFAULT); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2827c478bd9Sstevel@tonic-gate else { 2837c478bd9Sstevel@tonic-gate /* return rtadmin struct to ILP32 callers */ 2847c478bd9Sstevel@tonic-gate rtadmin32_t rtadmin32; 2857c478bd9Sstevel@tonic-gate rtadmin32.rt_dpents = 2867c478bd9Sstevel@tonic-gate (caddr32_t)(uintptr_t)rtadmin.rt_dpents; 2877c478bd9Sstevel@tonic-gate rtadmin32.rt_ndpents = rtadmin.rt_ndpents; 2887c478bd9Sstevel@tonic-gate rtadmin32.rt_cmd = rtadmin.rt_cmd; 2897c478bd9Sstevel@tonic-gate if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t))) 2907c478bd9Sstevel@tonic-gate return (EFAULT); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 2937c478bd9Sstevel@tonic-gate break; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate case RT_SETDPTBL: 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * We require that the requesting process has sufficient 2987c478bd9Sstevel@tonic-gate * priveleges. We also require that the table supplied by 2997c478bd9Sstevel@tonic-gate * the user exactly match the current rt_dptbl in size. 3007c478bd9Sstevel@tonic-gate */ 3017c478bd9Sstevel@tonic-gate if (secpolicy_dispadm(reqpcredp) != 0) 3027c478bd9Sstevel@tonic-gate return (EPERM); 3037c478bd9Sstevel@tonic-gate if (rtadmin.rt_ndpents * sizeof (rtdpent_t) != rtdpsz) 3047c478bd9Sstevel@tonic-gate return (EINVAL); 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * We read the user supplied table into a temporary buffer 3087c478bd9Sstevel@tonic-gate * where the time quantum values are validated before 3097c478bd9Sstevel@tonic-gate * being copied to the rt_dptbl. 3107c478bd9Sstevel@tonic-gate */ 3117c478bd9Sstevel@tonic-gate tmpdpp = kmem_alloc(rtdpsz, KM_SLEEP); 3127c478bd9Sstevel@tonic-gate if (copyin(rtadmin.rt_dpents, tmpdpp, rtdpsz)) { 3137c478bd9Sstevel@tonic-gate kmem_free(tmpdpp, rtdpsz); 3147c478bd9Sstevel@tonic-gate return (EFAULT); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate for (i = 0; i < rtadmin.rt_ndpents; i++) { 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * Validate the user supplied time quantum values. 3207c478bd9Sstevel@tonic-gate */ 3217c478bd9Sstevel@tonic-gate if (tmpdpp[i].rt_quantum <= 0 && 3227c478bd9Sstevel@tonic-gate tmpdpp[i].rt_quantum != RT_TQINF) { 3237c478bd9Sstevel@tonic-gate kmem_free(tmpdpp, rtdpsz); 3247c478bd9Sstevel@tonic-gate return (EINVAL); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * Copy the user supplied values over the current rt_dptbl 3307c478bd9Sstevel@tonic-gate * values. The rt_globpri member is read-only so we don't 3317c478bd9Sstevel@tonic-gate * overwrite it. 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate mutex_enter(&rt_dptblock); 3347c478bd9Sstevel@tonic-gate for (i = 0; i < rtadmin.rt_ndpents; i++) 3357c478bd9Sstevel@tonic-gate rt_dptbl[i].rt_quantum = tmpdpp[i].rt_quantum; 3367c478bd9Sstevel@tonic-gate mutex_exit(&rt_dptblock); 3377c478bd9Sstevel@tonic-gate kmem_free(tmpdpp, rtdpsz); 3387c478bd9Sstevel@tonic-gate break; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate default: 3417c478bd9Sstevel@tonic-gate return (EINVAL); 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate return (0); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * Allocate a real-time class specific proc structure and 3497c478bd9Sstevel@tonic-gate * initialize it with the parameters supplied. Also move thread 3507c478bd9Sstevel@tonic-gate * to specified real-time priority. 3517c478bd9Sstevel@tonic-gate */ 3527c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3537c478bd9Sstevel@tonic-gate static int 3547c478bd9Sstevel@tonic-gate rt_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp, 3557c478bd9Sstevel@tonic-gate void *bufp) 3567c478bd9Sstevel@tonic-gate { 3577c478bd9Sstevel@tonic-gate rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp; 3587c478bd9Sstevel@tonic-gate rtproc_t *rtpp; 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate /* 3617c478bd9Sstevel@tonic-gate * For a thread to enter the real-time class the thread 3627c478bd9Sstevel@tonic-gate * which initiates the request must be privileged. 3637c478bd9Sstevel@tonic-gate * This may have been checked previously but if our 3647c478bd9Sstevel@tonic-gate * caller passed us a credential structure we assume it 3657c478bd9Sstevel@tonic-gate * hasn't and we check it here. 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0) 3687c478bd9Sstevel@tonic-gate return (EPERM); 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate rtpp = (rtproc_t *)bufp; 3717c478bd9Sstevel@tonic-gate ASSERT(rtpp != NULL); 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate /* 3747c478bd9Sstevel@tonic-gate * If this thread's lwp is swapped out, it will be brought in 3757c478bd9Sstevel@tonic-gate * when it is put onto the runqueue. 3767c478bd9Sstevel@tonic-gate * 3777c478bd9Sstevel@tonic-gate * Now, Initialize the rtproc structure. 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate if (rtkparmsp == NULL) { 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Use default values 3827c478bd9Sstevel@tonic-gate */ 3837c478bd9Sstevel@tonic-gate rtpp->rt_pri = 0; 3847c478bd9Sstevel@tonic-gate rtpp->rt_pquantum = rt_dptbl[0].rt_quantum; 3857c478bd9Sstevel@tonic-gate rtpp->rt_tqsignal = 0; 3867c478bd9Sstevel@tonic-gate } else { 3877c478bd9Sstevel@tonic-gate /* 3887c478bd9Sstevel@tonic-gate * Use supplied values 3897c478bd9Sstevel@tonic-gate */ 3907c478bd9Sstevel@tonic-gate if ((rtkparmsp->rt_cflags & RT_DOPRI) == 0) 3917c478bd9Sstevel@tonic-gate rtpp->rt_pri = 0; 3927c478bd9Sstevel@tonic-gate else 3937c478bd9Sstevel@tonic-gate rtpp->rt_pri = rtkparmsp->rt_pri; 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate if (rtkparmsp->rt_tqntm == RT_TQINF) 3967c478bd9Sstevel@tonic-gate rtpp->rt_pquantum = RT_TQINF; 3977c478bd9Sstevel@tonic-gate else if (rtkparmsp->rt_tqntm == RT_TQDEF || 3987c478bd9Sstevel@tonic-gate (rtkparmsp->rt_cflags & RT_DOTQ) == 0) 3997c478bd9Sstevel@tonic-gate rtpp->rt_pquantum = rt_dptbl[rtpp->rt_pri].rt_quantum; 4007c478bd9Sstevel@tonic-gate else 4017c478bd9Sstevel@tonic-gate rtpp->rt_pquantum = rtkparmsp->rt_tqntm; 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate if ((rtkparmsp->rt_cflags & RT_DOSIG) == 0) 4047c478bd9Sstevel@tonic-gate rtpp->rt_tqsignal = 0; 4057c478bd9Sstevel@tonic-gate else 4067c478bd9Sstevel@tonic-gate rtpp->rt_tqsignal = rtkparmsp->rt_tqsig; 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate rtpp->rt_flags = 0; 4097c478bd9Sstevel@tonic-gate rtpp->rt_tp = t; 4107c478bd9Sstevel@tonic-gate /* 4117c478bd9Sstevel@tonic-gate * Reset thread priority 4127c478bd9Sstevel@tonic-gate */ 4137c478bd9Sstevel@tonic-gate thread_lock(t); 4147c478bd9Sstevel@tonic-gate t->t_clfuncs = &(sclass[cid].cl_funcs->thread); 4157c478bd9Sstevel@tonic-gate t->t_cid = cid; 4167c478bd9Sstevel@tonic-gate t->t_cldata = (void *)rtpp; 4177c478bd9Sstevel@tonic-gate t->t_schedflag &= ~TS_RUNQMATCH; 4187c478bd9Sstevel@tonic-gate rt_change_priority(t, rtpp); 4197c478bd9Sstevel@tonic-gate thread_unlock(t); 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * Link new structure into rtproc list 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate mutex_enter(&rt_list_lock); 4247c478bd9Sstevel@tonic-gate rtpp->rt_next = rt_plisthead.rt_next; 4257c478bd9Sstevel@tonic-gate rtpp->rt_prev = &rt_plisthead; 4267c478bd9Sstevel@tonic-gate rt_plisthead.rt_next->rt_prev = rtpp; 4277c478bd9Sstevel@tonic-gate rt_plisthead.rt_next = rtpp; 4287c478bd9Sstevel@tonic-gate mutex_exit(&rt_list_lock); 4297c478bd9Sstevel@tonic-gate return (0); 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Free rtproc structure of thread. 4357c478bd9Sstevel@tonic-gate */ 4367c478bd9Sstevel@tonic-gate static void 4377c478bd9Sstevel@tonic-gate rt_exitclass(void *procp) 4387c478bd9Sstevel@tonic-gate { 4397c478bd9Sstevel@tonic-gate rtproc_t *rtprocp = (rtproc_t *)procp; 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate mutex_enter(&rt_list_lock); 4427c478bd9Sstevel@tonic-gate rtprocp->rt_prev->rt_next = rtprocp->rt_next; 4437c478bd9Sstevel@tonic-gate rtprocp->rt_next->rt_prev = rtprocp->rt_prev; 4447c478bd9Sstevel@tonic-gate mutex_exit(&rt_list_lock); 4457c478bd9Sstevel@tonic-gate kmem_free(rtprocp, sizeof (rtproc_t)); 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * Allocate and initialize real-time class specific 4517c478bd9Sstevel@tonic-gate * proc structure for child. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate /* ARGSUSED */ 4547c478bd9Sstevel@tonic-gate static int 4557c478bd9Sstevel@tonic-gate rt_fork(kthread_t *t, kthread_t *ct, void *bufp) 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate rtproc_t *prtpp; 4587c478bd9Sstevel@tonic-gate rtproc_t *crtpp; 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock)); 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate /* 4637c478bd9Sstevel@tonic-gate * Initialize child's rtproc structure 4647c478bd9Sstevel@tonic-gate */ 4657c478bd9Sstevel@tonic-gate crtpp = (rtproc_t *)bufp; 4667c478bd9Sstevel@tonic-gate ASSERT(crtpp != NULL); 4677c478bd9Sstevel@tonic-gate prtpp = (rtproc_t *)t->t_cldata; 4687c478bd9Sstevel@tonic-gate thread_lock(t); 4697c478bd9Sstevel@tonic-gate crtpp->rt_timeleft = crtpp->rt_pquantum = prtpp->rt_pquantum; 4707c478bd9Sstevel@tonic-gate crtpp->rt_pri = prtpp->rt_pri; 4717c478bd9Sstevel@tonic-gate crtpp->rt_flags = prtpp->rt_flags & ~RTBACKQ; 4727c478bd9Sstevel@tonic-gate crtpp->rt_tqsignal = prtpp->rt_tqsignal; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate crtpp->rt_tp = ct; 4757c478bd9Sstevel@tonic-gate thread_unlock(t); 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate /* 4787c478bd9Sstevel@tonic-gate * Link new structure into rtproc list 4797c478bd9Sstevel@tonic-gate */ 4807c478bd9Sstevel@tonic-gate ct->t_cldata = (void *)crtpp; 4817c478bd9Sstevel@tonic-gate mutex_enter(&rt_list_lock); 4827c478bd9Sstevel@tonic-gate crtpp->rt_next = rt_plisthead.rt_next; 4837c478bd9Sstevel@tonic-gate crtpp->rt_prev = &rt_plisthead; 4847c478bd9Sstevel@tonic-gate rt_plisthead.rt_next->rt_prev = crtpp; 4857c478bd9Sstevel@tonic-gate rt_plisthead.rt_next = crtpp; 4867c478bd9Sstevel@tonic-gate mutex_exit(&rt_list_lock); 4877c478bd9Sstevel@tonic-gate return (0); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate /* 4927c478bd9Sstevel@tonic-gate * The child goes to the back of its dispatcher queue while the 4937c478bd9Sstevel@tonic-gate * parent continues to run after a real time thread forks. 4947c478bd9Sstevel@tonic-gate */ 4957c478bd9Sstevel@tonic-gate /* ARGSUSED */ 4967c478bd9Sstevel@tonic-gate static void 4977c478bd9Sstevel@tonic-gate rt_forkret(kthread_t *t, kthread_t *ct) 4987c478bd9Sstevel@tonic-gate { 4997c478bd9Sstevel@tonic-gate proc_t *pp = ttoproc(t); 5007c478bd9Sstevel@tonic-gate proc_t *cp = ttoproc(ct); 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate ASSERT(t == curthread); 5037c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate /* 5067c478bd9Sstevel@tonic-gate * Grab the child's p_lock before dropping pidlock to ensure 5077c478bd9Sstevel@tonic-gate * the process does not disappear before we set it running. 5087c478bd9Sstevel@tonic-gate */ 5097c478bd9Sstevel@tonic-gate mutex_enter(&cp->p_lock); 5107c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 5117c478bd9Sstevel@tonic-gate continuelwps(cp); 5127c478bd9Sstevel@tonic-gate mutex_exit(&cp->p_lock); 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 5157c478bd9Sstevel@tonic-gate continuelwps(pp); 5167c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * Get information about the real-time class into the buffer 5227c478bd9Sstevel@tonic-gate * pointed to by rtinfop. The maximum configured real-time 5237c478bd9Sstevel@tonic-gate * priority is the only information we supply. We ignore the 5247c478bd9Sstevel@tonic-gate * class and credential arguments because anyone can have this 5257c478bd9Sstevel@tonic-gate * information. 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate /* ARGSUSED */ 5287c478bd9Sstevel@tonic-gate static int 5297c478bd9Sstevel@tonic-gate rt_getclinfo(void *infop) 5307c478bd9Sstevel@tonic-gate { 5317c478bd9Sstevel@tonic-gate rtinfo_t *rtinfop = (rtinfo_t *)infop; 5327c478bd9Sstevel@tonic-gate rtinfop->rt_maxpri = rt_maxpri; 5337c478bd9Sstevel@tonic-gate return (0); 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate /* 5377c478bd9Sstevel@tonic-gate * Return the global scheduling priority ranges of the realtime 5387c478bd9Sstevel@tonic-gate * class in pcpri_t structure. 5397c478bd9Sstevel@tonic-gate */ 5407c478bd9Sstevel@tonic-gate static int 5417c478bd9Sstevel@tonic-gate rt_getclpri(pcpri_t *pcprip) 5427c478bd9Sstevel@tonic-gate { 5437c478bd9Sstevel@tonic-gate pcprip->pc_clpmax = rt_dptbl[rt_maxpri].rt_globpri; 5447c478bd9Sstevel@tonic-gate pcprip->pc_clpmin = rt_dptbl[0].rt_globpri; 5457c478bd9Sstevel@tonic-gate return (0); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate static void 5487c478bd9Sstevel@tonic-gate rt_nullsys() 5497c478bd9Sstevel@tonic-gate { 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate /* ARGSUSED */ 5537c478bd9Sstevel@tonic-gate static int 5547c478bd9Sstevel@tonic-gate rt_canexit(kthread_t *t, cred_t *cred) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * Thread can always leave RT class 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate return (0); 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate /* 5637c478bd9Sstevel@tonic-gate * Get the real-time scheduling parameters of the thread pointed to by 5647c478bd9Sstevel@tonic-gate * rtprocp into the buffer pointed to by rtkparmsp. 5657c478bd9Sstevel@tonic-gate */ 5667c478bd9Sstevel@tonic-gate static void 5677c478bd9Sstevel@tonic-gate rt_parmsget(kthread_t *t, void *parmsp) 5687c478bd9Sstevel@tonic-gate { 5697c478bd9Sstevel@tonic-gate rtproc_t *rtprocp = (rtproc_t *)t->t_cldata; 5707c478bd9Sstevel@tonic-gate rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp; 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate rtkparmsp->rt_pri = rtprocp->rt_pri; 5737c478bd9Sstevel@tonic-gate rtkparmsp->rt_tqntm = rtprocp->rt_pquantum; 5747c478bd9Sstevel@tonic-gate rtkparmsp->rt_tqsig = rtprocp->rt_tqsignal; 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * Check the validity of the real-time parameters in the buffer 5817c478bd9Sstevel@tonic-gate * pointed to by rtprmsp. 5827c478bd9Sstevel@tonic-gate * We convert the rtparms buffer from the user supplied format to 5837c478bd9Sstevel@tonic-gate * our internal format (i.e. time quantum expressed in ticks). 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate static int 5867c478bd9Sstevel@tonic-gate rt_parmsin(void *prmsp) 5877c478bd9Sstevel@tonic-gate { 5887c478bd9Sstevel@tonic-gate rtparms_t *rtprmsp = (rtparms_t *)prmsp; 5897c478bd9Sstevel@tonic-gate longlong_t ticks; 5907c478bd9Sstevel@tonic-gate uint_t cflags; 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate /* 5937c478bd9Sstevel@tonic-gate * First check the validity of parameters and convert 5947c478bd9Sstevel@tonic-gate * the buffer to kernel format. 5957c478bd9Sstevel@tonic-gate */ 5967c478bd9Sstevel@tonic-gate if ((rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri) && 5977c478bd9Sstevel@tonic-gate rtprmsp->rt_pri != RT_NOCHANGE) 5987c478bd9Sstevel@tonic-gate return (EINVAL); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate cflags = (rtprmsp->rt_pri != RT_NOCHANGE ? RT_DOPRI : 0); 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate if ((rtprmsp->rt_tqsecs == 0 && rtprmsp->rt_tqnsecs == 0) || 6037c478bd9Sstevel@tonic-gate rtprmsp->rt_tqnsecs >= NANOSEC) 6047c478bd9Sstevel@tonic-gate return (EINVAL); 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate if (rtprmsp->rt_tqnsecs != RT_NOCHANGE) 6077c478bd9Sstevel@tonic-gate cflags |= RT_DOTQ; 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate if (rtprmsp->rt_tqnsecs >= 0) { 6107c478bd9Sstevel@tonic-gate if ((ticks = SEC_TO_TICK((longlong_t)rtprmsp->rt_tqsecs) + 6117c478bd9Sstevel@tonic-gate NSEC_TO_TICK_ROUNDUP(rtprmsp->rt_tqnsecs)) > INT_MAX) 6127c478bd9Sstevel@tonic-gate return (ERANGE); 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate ((rtkparms_t *)rtprmsp)->rt_tqntm = (int)ticks; 6157c478bd9Sstevel@tonic-gate } else { 6167c478bd9Sstevel@tonic-gate if (rtprmsp->rt_tqnsecs != RT_NOCHANGE && 6177c478bd9Sstevel@tonic-gate rtprmsp->rt_tqnsecs != RT_TQINF && 6187c478bd9Sstevel@tonic-gate rtprmsp->rt_tqnsecs != RT_TQDEF) 6197c478bd9Sstevel@tonic-gate return (EINVAL); 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate ((rtkparms_t *)rtprmsp)->rt_tqntm = rtprmsp->rt_tqnsecs; 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate ((rtkparms_t *)rtprmsp)->rt_cflags = cflags; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate return (0); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * Check the validity of the real-time parameters in the pc_vaparms_t 6317c478bd9Sstevel@tonic-gate * structure vaparmsp and put them in the buffer pointed to by rtprmsp. 6327c478bd9Sstevel@tonic-gate * pc_vaparms_t contains (key, value) pairs of parameter. 6337c478bd9Sstevel@tonic-gate * rt_vaparmsin() is the variable parameter version of rt_parmsin(). 6347c478bd9Sstevel@tonic-gate */ 6357c478bd9Sstevel@tonic-gate static int 6367c478bd9Sstevel@tonic-gate rt_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp) 6377c478bd9Sstevel@tonic-gate { 6387c478bd9Sstevel@tonic-gate uint_t secs = 0; 6397c478bd9Sstevel@tonic-gate uint_t cnt; 6407c478bd9Sstevel@tonic-gate int nsecs = 0; 6417c478bd9Sstevel@tonic-gate int priflag, secflag, nsecflag, sigflag; 6427c478bd9Sstevel@tonic-gate longlong_t ticks; 6437c478bd9Sstevel@tonic-gate rtkparms_t *rtprmsp = (rtkparms_t *)prmsp; 6447c478bd9Sstevel@tonic-gate pc_vaparm_t *vpp = &vaparmsp->pc_parms[0]; 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate /* 6487c478bd9Sstevel@tonic-gate * First check the validity of parameters and convert them 6497c478bd9Sstevel@tonic-gate * from the user supplied format to the internal format. 6507c478bd9Sstevel@tonic-gate */ 6517c478bd9Sstevel@tonic-gate priflag = secflag = nsecflag = sigflag = 0; 6527c478bd9Sstevel@tonic-gate rtprmsp->rt_cflags = 0; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT) 6557c478bd9Sstevel@tonic-gate return (EINVAL); 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) { 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate switch (vpp->pc_key) { 6607c478bd9Sstevel@tonic-gate case RT_KY_PRI: 6617c478bd9Sstevel@tonic-gate if (priflag++) 6627c478bd9Sstevel@tonic-gate return (EINVAL); 6637c478bd9Sstevel@tonic-gate rtprmsp->rt_cflags |= RT_DOPRI; 6647c478bd9Sstevel@tonic-gate rtprmsp->rt_pri = (pri_t)vpp->pc_parm; 6657c478bd9Sstevel@tonic-gate if (rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri) 6667c478bd9Sstevel@tonic-gate return (EINVAL); 6677c478bd9Sstevel@tonic-gate break; 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate case RT_KY_TQSECS: 6707c478bd9Sstevel@tonic-gate if (secflag++) 6717c478bd9Sstevel@tonic-gate return (EINVAL); 6727c478bd9Sstevel@tonic-gate rtprmsp->rt_cflags |= RT_DOTQ; 6737c478bd9Sstevel@tonic-gate secs = (uint_t)vpp->pc_parm; 6747c478bd9Sstevel@tonic-gate break; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate case RT_KY_TQNSECS: 6777c478bd9Sstevel@tonic-gate if (nsecflag++) 6787c478bd9Sstevel@tonic-gate return (EINVAL); 6797c478bd9Sstevel@tonic-gate rtprmsp->rt_cflags |= RT_DOTQ; 6807c478bd9Sstevel@tonic-gate nsecs = (int)vpp->pc_parm; 6817c478bd9Sstevel@tonic-gate break; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate case RT_KY_TQSIG: 6847c478bd9Sstevel@tonic-gate if (sigflag++) 6857c478bd9Sstevel@tonic-gate return (EINVAL); 6867c478bd9Sstevel@tonic-gate rtprmsp->rt_cflags |= RT_DOSIG; 6877c478bd9Sstevel@tonic-gate rtprmsp->rt_tqsig = (int)vpp->pc_parm; 6887c478bd9Sstevel@tonic-gate if (rtprmsp->rt_tqsig < 0 || rtprmsp->rt_tqsig >= NSIG) 6897c478bd9Sstevel@tonic-gate return (EINVAL); 6907c478bd9Sstevel@tonic-gate break; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate default: 6937c478bd9Sstevel@tonic-gate return (EINVAL); 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt == 0) { 6987c478bd9Sstevel@tonic-gate /* 6997c478bd9Sstevel@tonic-gate * Use default parameters. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate rtprmsp->rt_pri = 0; 7027c478bd9Sstevel@tonic-gate rtprmsp->rt_tqntm = RT_TQDEF; 7037c478bd9Sstevel@tonic-gate rtprmsp->rt_tqsig = 0; 7047c478bd9Sstevel@tonic-gate rtprmsp->rt_cflags = RT_DOPRI | RT_DOTQ | RT_DOSIG; 7057c478bd9Sstevel@tonic-gate } else if ((rtprmsp->rt_cflags & RT_DOTQ) != 0) { 7067c478bd9Sstevel@tonic-gate if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC) 7077c478bd9Sstevel@tonic-gate return (EINVAL); 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate if (nsecs >= 0) { 7107c478bd9Sstevel@tonic-gate if ((ticks = SEC_TO_TICK((longlong_t)secs) + 7117c478bd9Sstevel@tonic-gate NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX) 7127c478bd9Sstevel@tonic-gate return (ERANGE); 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate rtprmsp->rt_tqntm = (int)ticks; 7157c478bd9Sstevel@tonic-gate } else { 7167c478bd9Sstevel@tonic-gate if (nsecs != RT_TQINF && nsecs != RT_TQDEF) 7177c478bd9Sstevel@tonic-gate return (EINVAL); 7187c478bd9Sstevel@tonic-gate rtprmsp->rt_tqntm = nsecs; 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate return (0); 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate /* 7267c478bd9Sstevel@tonic-gate * Do required processing on the real-time parameter buffer 7277c478bd9Sstevel@tonic-gate * before it is copied out to the user. 7287c478bd9Sstevel@tonic-gate * All we have to do is convert the buffer from kernel to user format 7297c478bd9Sstevel@tonic-gate * (i.e. convert time quantum from ticks to seconds-nanoseconds). 7307c478bd9Sstevel@tonic-gate */ 7317c478bd9Sstevel@tonic-gate /* ARGSUSED */ 7327c478bd9Sstevel@tonic-gate static int 7337c478bd9Sstevel@tonic-gate rt_parmsout(void *prmsp, pc_vaparms_t *vaparmsp) 7347c478bd9Sstevel@tonic-gate { 7357c478bd9Sstevel@tonic-gate rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp; 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate if (vaparmsp != NULL) 7387c478bd9Sstevel@tonic-gate return (0); 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate if (rtkprmsp->rt_tqntm < 0) { 7417c478bd9Sstevel@tonic-gate /* 7427c478bd9Sstevel@tonic-gate * Quantum field set to special value (e.g. RT_TQINF) 7437c478bd9Sstevel@tonic-gate */ 7447c478bd9Sstevel@tonic-gate ((rtparms_t *)rtkprmsp)->rt_tqnsecs = rtkprmsp->rt_tqntm; 7457c478bd9Sstevel@tonic-gate ((rtparms_t *)rtkprmsp)->rt_tqsecs = 0; 7467c478bd9Sstevel@tonic-gate } else { 7477c478bd9Sstevel@tonic-gate /* Convert quantum from ticks to seconds-nanoseconds */ 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate timestruc_t ts; 7507c478bd9Sstevel@tonic-gate TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts); 7517c478bd9Sstevel@tonic-gate ((rtparms_t *)rtkprmsp)->rt_tqsecs = ts.tv_sec; 7527c478bd9Sstevel@tonic-gate ((rtparms_t *)rtkprmsp)->rt_tqnsecs = ts.tv_nsec; 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate return (0); 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate /* 7607c478bd9Sstevel@tonic-gate * Copy all selected real-time class parameters to the user. 7617c478bd9Sstevel@tonic-gate * The parameters are specified by a key. 7627c478bd9Sstevel@tonic-gate */ 7637c478bd9Sstevel@tonic-gate static int 7647c478bd9Sstevel@tonic-gate rt_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp) 7657c478bd9Sstevel@tonic-gate { 7667c478bd9Sstevel@tonic-gate rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp; 7677c478bd9Sstevel@tonic-gate timestruc_t ts; 7687c478bd9Sstevel@tonic-gate uint_t cnt; 7697c478bd9Sstevel@tonic-gate uint_t secs; 7707c478bd9Sstevel@tonic-gate int nsecs; 7717c478bd9Sstevel@tonic-gate int priflag, secflag, nsecflag, sigflag; 7727c478bd9Sstevel@tonic-gate pc_vaparm_t *vpp = &vaparmsp->pc_parms[0]; 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate priflag = secflag = nsecflag = sigflag = 0; 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT) 7797c478bd9Sstevel@tonic-gate return (EINVAL); 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate if (rtkprmsp->rt_tqntm < 0) { 7827c478bd9Sstevel@tonic-gate /* 7837c478bd9Sstevel@tonic-gate * Quantum field set to special value (e.g. RT_TQINF). 7847c478bd9Sstevel@tonic-gate */ 7857c478bd9Sstevel@tonic-gate secs = 0; 7867c478bd9Sstevel@tonic-gate nsecs = rtkprmsp->rt_tqntm; 7877c478bd9Sstevel@tonic-gate } else { 7887c478bd9Sstevel@tonic-gate /* 7897c478bd9Sstevel@tonic-gate * Convert quantum from ticks to seconds-nanoseconds. 7907c478bd9Sstevel@tonic-gate */ 7917c478bd9Sstevel@tonic-gate TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts); 7927c478bd9Sstevel@tonic-gate secs = ts.tv_sec; 7937c478bd9Sstevel@tonic-gate nsecs = ts.tv_nsec; 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) { 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate switch (vpp->pc_key) { 8007c478bd9Sstevel@tonic-gate case RT_KY_PRI: 8017c478bd9Sstevel@tonic-gate if (priflag++) 8027c478bd9Sstevel@tonic-gate return (EINVAL); 8037c478bd9Sstevel@tonic-gate if (copyout(&rtkprmsp->rt_pri, 8047c478bd9Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t))) 8057c478bd9Sstevel@tonic-gate return (EFAULT); 8067c478bd9Sstevel@tonic-gate break; 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate case RT_KY_TQSECS: 8097c478bd9Sstevel@tonic-gate if (secflag++) 8107c478bd9Sstevel@tonic-gate return (EINVAL); 8117c478bd9Sstevel@tonic-gate if (copyout(&secs, (caddr_t)(uintptr_t)vpp->pc_parm, 8127c478bd9Sstevel@tonic-gate sizeof (uint_t))) 8137c478bd9Sstevel@tonic-gate return (EFAULT); 8147c478bd9Sstevel@tonic-gate break; 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate case RT_KY_TQNSECS: 8177c478bd9Sstevel@tonic-gate if (nsecflag++) 8187c478bd9Sstevel@tonic-gate return (EINVAL); 8197c478bd9Sstevel@tonic-gate if (copyout(&nsecs, (caddr_t)(uintptr_t)vpp->pc_parm, 8207c478bd9Sstevel@tonic-gate sizeof (int))) 8217c478bd9Sstevel@tonic-gate return (EFAULT); 8227c478bd9Sstevel@tonic-gate break; 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate case RT_KY_TQSIG: 8257c478bd9Sstevel@tonic-gate if (sigflag++) 8267c478bd9Sstevel@tonic-gate return (EINVAL); 8277c478bd9Sstevel@tonic-gate if (copyout(&rtkprmsp->rt_tqsig, 8287c478bd9Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int))) 8297c478bd9Sstevel@tonic-gate return (EFAULT); 8307c478bd9Sstevel@tonic-gate break; 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate default: 8337c478bd9Sstevel@tonic-gate return (EINVAL); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate return (0); 8387c478bd9Sstevel@tonic-gate } 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate /* 8427c478bd9Sstevel@tonic-gate * Set the scheduling parameters of the thread pointed to by rtprocp 8437c478bd9Sstevel@tonic-gate * to those specified in the buffer pointed to by rtkprmsp. 8447c478bd9Sstevel@tonic-gate * Note that the parameters are expected to be in kernel format 8457c478bd9Sstevel@tonic-gate * (i.e. time quantm expressed in ticks). Real time parameters copied 8467c478bd9Sstevel@tonic-gate * in from the user should be processed by rt_parmsin() before they are 8477c478bd9Sstevel@tonic-gate * passed to this function. 8487c478bd9Sstevel@tonic-gate */ 8497c478bd9Sstevel@tonic-gate static int 8507c478bd9Sstevel@tonic-gate rt_parmsset(kthread_t *tx, void *prmsp, id_t reqpcid, cred_t *reqpcredp) 8517c478bd9Sstevel@tonic-gate { 8527c478bd9Sstevel@tonic-gate rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp; 8537c478bd9Sstevel@tonic-gate rtproc_t *rtpp = (rtproc_t *)tx->t_cldata; 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock)); 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate /* 8587c478bd9Sstevel@tonic-gate * Basic permissions enforced by generic kernel code 8597c478bd9Sstevel@tonic-gate * for all classes require that a thread attempting 8607c478bd9Sstevel@tonic-gate * to change the scheduling parameters of a target thread 8617c478bd9Sstevel@tonic-gate * be privileged or have a real or effective UID 8627c478bd9Sstevel@tonic-gate * matching that of the target thread. We are not 8637c478bd9Sstevel@tonic-gate * called unless these basic permission checks have 8647c478bd9Sstevel@tonic-gate * already passed. The real-time class requires in addition 8657c478bd9Sstevel@tonic-gate * that the requesting thread be real-time unless it is privileged. 8667c478bd9Sstevel@tonic-gate * This may also have been checked previously but if our caller 8677c478bd9Sstevel@tonic-gate * passes us a credential structure we assume it hasn't and 8687c478bd9Sstevel@tonic-gate * we check it here. 8697c478bd9Sstevel@tonic-gate */ 8707c478bd9Sstevel@tonic-gate if (reqpcredp != NULL && reqpcid != rt_cid && 8717c478bd9Sstevel@tonic-gate secpolicy_setpriority(reqpcredp) != 0) 8727c478bd9Sstevel@tonic-gate return (EPERM); 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate thread_lock(tx); 8757c478bd9Sstevel@tonic-gate if ((rtkprmsp->rt_cflags & RT_DOPRI) != 0) { 8767c478bd9Sstevel@tonic-gate rtpp->rt_pri = rtkprmsp->rt_pri; 8777c478bd9Sstevel@tonic-gate rt_change_priority(tx, rtpp); 8787c478bd9Sstevel@tonic-gate } 8797c478bd9Sstevel@tonic-gate if (rtkprmsp->rt_tqntm == RT_TQINF) 8807c478bd9Sstevel@tonic-gate rtpp->rt_pquantum = RT_TQINF; 8817c478bd9Sstevel@tonic-gate else if (rtkprmsp->rt_tqntm == RT_TQDEF) 8827c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum = 8837c478bd9Sstevel@tonic-gate rt_dptbl[rtpp->rt_pri].rt_quantum; 8847c478bd9Sstevel@tonic-gate else if ((rtkprmsp->rt_cflags & RT_DOTQ) != 0) 8857c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum = rtkprmsp->rt_tqntm; 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate if ((rtkprmsp->rt_cflags & RT_DOSIG) != 0) 8887c478bd9Sstevel@tonic-gate rtpp->rt_tqsignal = rtkprmsp->rt_tqsig; 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate thread_unlock(tx); 8917c478bd9Sstevel@tonic-gate return (0); 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * Arrange for thread to be placed in appropriate location 8977c478bd9Sstevel@tonic-gate * on dispatcher queue. Runs at splhi() since the clock 8987c478bd9Sstevel@tonic-gate * interrupt can cause RTBACKQ to be set. 8997c478bd9Sstevel@tonic-gate */ 9007c478bd9Sstevel@tonic-gate static void 9017c478bd9Sstevel@tonic-gate rt_preempt(kthread_t *t) 9027c478bd9Sstevel@tonic-gate { 9037c478bd9Sstevel@tonic-gate rtproc_t *rtpp = (rtproc_t *)(t->t_cldata); 9047c478bd9Sstevel@tonic-gate klwp_t *lwp; 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate /* 9097c478bd9Sstevel@tonic-gate * If the state is user I allow swapping because I know I won't 9107c478bd9Sstevel@tonic-gate * be holding any locks. 9117c478bd9Sstevel@tonic-gate */ 9127c478bd9Sstevel@tonic-gate if ((lwp = curthread->t_lwp) != NULL && lwp->lwp_state == LWP_USER) 9137c478bd9Sstevel@tonic-gate t->t_schedflag &= ~TS_DONT_SWAP; 9147c478bd9Sstevel@tonic-gate if ((rtpp->rt_flags & RTBACKQ) != 0) { 9157c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum; 9167c478bd9Sstevel@tonic-gate rtpp->rt_flags &= ~RTBACKQ; 9177c478bd9Sstevel@tonic-gate setbackdq(t); 9187c478bd9Sstevel@tonic-gate } else 9197c478bd9Sstevel@tonic-gate setfrontdq(t); 9207c478bd9Sstevel@tonic-gate 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate 9237c478bd9Sstevel@tonic-gate /* 9247c478bd9Sstevel@tonic-gate * Return the global priority associated with this rt_pri. 9257c478bd9Sstevel@tonic-gate */ 9267c478bd9Sstevel@tonic-gate static pri_t 9277c478bd9Sstevel@tonic-gate rt_globpri(kthread_t *t) 9287c478bd9Sstevel@tonic-gate { 9297c478bd9Sstevel@tonic-gate rtproc_t *rtprocp = (rtproc_t *)t->t_cldata; 9307c478bd9Sstevel@tonic-gate return (rt_dptbl[rtprocp->rt_pri].rt_globpri); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate static void 9347c478bd9Sstevel@tonic-gate rt_setrun(kthread_t *t) 9357c478bd9Sstevel@tonic-gate { 9367c478bd9Sstevel@tonic-gate rtproc_t *rtpp = (rtproc_t *)(t->t_cldata); 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum; 9417c478bd9Sstevel@tonic-gate rtpp->rt_flags &= ~RTBACKQ; 9427c478bd9Sstevel@tonic-gate setbackdq(t); 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate /* 9467c478bd9Sstevel@tonic-gate * Returns the priority of the thread, -1 if the thread is loaded or ineligible 9477c478bd9Sstevel@tonic-gate * for swapin. 9487c478bd9Sstevel@tonic-gate * 9497c478bd9Sstevel@tonic-gate * FX and RT threads are designed so that they don't swapout; however, it 9507c478bd9Sstevel@tonic-gate * is possible that while the thread is swapped out and in another class, it 9517c478bd9Sstevel@tonic-gate * can be changed to FX or RT. Since these threads should be swapped in as 9527c478bd9Sstevel@tonic-gate * soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin 9537c478bd9Sstevel@tonic-gate * returns SHRT_MAX - 1, so that it gives deference to any swapped out RT 9547c478bd9Sstevel@tonic-gate * threads. 9557c478bd9Sstevel@tonic-gate */ 9567c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9577c478bd9Sstevel@tonic-gate static pri_t 9587c478bd9Sstevel@tonic-gate rt_swapin(kthread_t *t, int flags) 9597c478bd9Sstevel@tonic-gate { 9607c478bd9Sstevel@tonic-gate pri_t tpri = -1; 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) { 9657c478bd9Sstevel@tonic-gate tpri = (pri_t)SHRT_MAX; 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate 9687c478bd9Sstevel@tonic-gate return (tpri); 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * Return an effective priority for swapout. 9737c478bd9Sstevel@tonic-gate */ 9747c478bd9Sstevel@tonic-gate /* ARGSUSED */ 9757c478bd9Sstevel@tonic-gate static pri_t 9767c478bd9Sstevel@tonic-gate rt_swapout(kthread_t *t, int flags) 9777c478bd9Sstevel@tonic-gate { 9787c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 9797c478bd9Sstevel@tonic-gate 9807c478bd9Sstevel@tonic-gate return (-1); 9817c478bd9Sstevel@tonic-gate } 9827c478bd9Sstevel@tonic-gate 9837c478bd9Sstevel@tonic-gate /* 9847c478bd9Sstevel@tonic-gate * Check for time slice expiration (unless thread has infinite time 9857c478bd9Sstevel@tonic-gate * slice). If time slice has expired arrange for thread to be preempted 9867c478bd9Sstevel@tonic-gate * and placed on back of queue. 9877c478bd9Sstevel@tonic-gate */ 9887c478bd9Sstevel@tonic-gate static void 9897c478bd9Sstevel@tonic-gate rt_tick(kthread_t *t) 9907c478bd9Sstevel@tonic-gate { 9917c478bd9Sstevel@tonic-gate rtproc_t *rtpp = (rtproc_t *)(t->t_cldata); 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock)); 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate thread_lock(t); 9967c478bd9Sstevel@tonic-gate if ((rtpp->rt_pquantum != RT_TQINF && --rtpp->rt_timeleft == 0) || 997*b3383343Smishra (t->t_state == TS_ONPROC && DISP_MUST_SURRENDER(t))) { 9987c478bd9Sstevel@tonic-gate if (rtpp->rt_timeleft == 0 && rtpp->rt_tqsignal) { 9997c478bd9Sstevel@tonic-gate thread_unlock(t); 10007c478bd9Sstevel@tonic-gate sigtoproc(ttoproc(t), t, rtpp->rt_tqsignal); 10017c478bd9Sstevel@tonic-gate thread_lock(t); 10027c478bd9Sstevel@tonic-gate } 10037c478bd9Sstevel@tonic-gate rtpp->rt_flags |= RTBACKQ; 10047c478bd9Sstevel@tonic-gate cpu_surrender(t); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate thread_unlock(t); 10077c478bd9Sstevel@tonic-gate } 10087c478bd9Sstevel@tonic-gate 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate /* 10117c478bd9Sstevel@tonic-gate * Place the thread waking up on the dispatcher queue. 10127c478bd9Sstevel@tonic-gate */ 10137c478bd9Sstevel@tonic-gate static void 10147c478bd9Sstevel@tonic-gate rt_wakeup(kthread_t *t) 10157c478bd9Sstevel@tonic-gate { 10167c478bd9Sstevel@tonic-gate rtproc_t *rtpp = (rtproc_t *)(t->t_cldata); 10177c478bd9Sstevel@tonic-gate 10187c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum; 10217c478bd9Sstevel@tonic-gate rtpp->rt_flags &= ~RTBACKQ; 10227c478bd9Sstevel@tonic-gate setbackdq(t); 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate 10257c478bd9Sstevel@tonic-gate static void 10267c478bd9Sstevel@tonic-gate rt_yield(kthread_t *t) 10277c478bd9Sstevel@tonic-gate { 10287c478bd9Sstevel@tonic-gate rtproc_t *rtpp = (rtproc_t *)(t->t_cldata); 10297c478bd9Sstevel@tonic-gate 10307c478bd9Sstevel@tonic-gate ASSERT(t == curthread); 10317c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate rtpp->rt_flags &= ~RTBACKQ; 10347c478bd9Sstevel@tonic-gate setbackdq(t); 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate /* ARGSUSED */ 10387c478bd9Sstevel@tonic-gate static int 10397c478bd9Sstevel@tonic-gate rt_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp) 10407c478bd9Sstevel@tonic-gate { 10417c478bd9Sstevel@tonic-gate return (EINVAL); 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate static int 10457c478bd9Sstevel@tonic-gate rt_alloc(void **p, int flag) 10467c478bd9Sstevel@tonic-gate { 10477c478bd9Sstevel@tonic-gate void *bufp; 10487c478bd9Sstevel@tonic-gate bufp = kmem_alloc(sizeof (rtproc_t), flag); 10497c478bd9Sstevel@tonic-gate if (bufp == NULL) { 10507c478bd9Sstevel@tonic-gate return (ENOMEM); 10517c478bd9Sstevel@tonic-gate } else { 10527c478bd9Sstevel@tonic-gate *p = bufp; 10537c478bd9Sstevel@tonic-gate return (0); 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate 10577c478bd9Sstevel@tonic-gate static void 10587c478bd9Sstevel@tonic-gate rt_free(void *bufp) 10597c478bd9Sstevel@tonic-gate { 10607c478bd9Sstevel@tonic-gate if (bufp) 10617c478bd9Sstevel@tonic-gate kmem_free(bufp, sizeof (rtproc_t)); 10627c478bd9Sstevel@tonic-gate } 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate static void 10657c478bd9Sstevel@tonic-gate rt_change_priority(kthread_t *t, rtproc_t *rtpp) 10667c478bd9Sstevel@tonic-gate { 10677c478bd9Sstevel@tonic-gate pri_t new_pri; 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate new_pri = rt_dptbl[rtpp->rt_pri].rt_globpri; 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate if (t == curthread || t->t_state == TS_ONPROC) { 10747c478bd9Sstevel@tonic-gate cpu_t *cp = t->t_disp_queue->disp_cpu; 10757c478bd9Sstevel@tonic-gate THREAD_CHANGE_PRI(t, new_pri); 10767c478bd9Sstevel@tonic-gate if (t == cp->cpu_dispthread) 10777c478bd9Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(t); 10787c478bd9Sstevel@tonic-gate if (DISP_MUST_SURRENDER(t)) { 10797c478bd9Sstevel@tonic-gate rtpp->rt_flags |= RTBACKQ; 10807c478bd9Sstevel@tonic-gate cpu_surrender(t); 10817c478bd9Sstevel@tonic-gate } else { 10827c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum; 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate } else { 10857c478bd9Sstevel@tonic-gate /* 10867c478bd9Sstevel@tonic-gate * When the priority of a thread is changed, 10877c478bd9Sstevel@tonic-gate * it may be necessary to adjust its position 10887c478bd9Sstevel@tonic-gate * on a sleep queue or dispatch queue. The 10897c478bd9Sstevel@tonic-gate * function thread_change_pri() accomplishes this. 10907c478bd9Sstevel@tonic-gate */ 10917c478bd9Sstevel@tonic-gate if (thread_change_pri(t, new_pri, 0)) { 10927c478bd9Sstevel@tonic-gate /* 10937c478bd9Sstevel@tonic-gate * The thread was on a run queue. 10947c478bd9Sstevel@tonic-gate * Reset its CPU timeleft. 10957c478bd9Sstevel@tonic-gate */ 10967c478bd9Sstevel@tonic-gate rtpp->rt_timeleft = rtpp->rt_pquantum; 10977c478bd9Sstevel@tonic-gate } else { 10987c478bd9Sstevel@tonic-gate rtpp->rt_flags |= RTBACKQ; 10997c478bd9Sstevel@tonic-gate } 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate } 1102