4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2013 Joyent, Inc. All rights reserved.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/sysmacros.h>
37 #include <sys/signal.h>
39 #include <sys/priocntl.h>
40 #include <sys/class.h>
42 #include <sys/procset.h>
43 #include <sys/cmn_err.h>
44 #include <sys/debug.h>
46 #include <sys/rtpriocntl.h>
48 #include <sys/systm.h>
49 #include <sys/schedctl.h>
50 #include <sys/errno.h>
51 #include <sys/cpuvar.h>
52 #include <sys/vmsystm.h>
54 #include <sys/policy.h>
56 #include <sys/cpupart.h>
57 #include <sys/modctl.h>
59 static pri_t
rt_init(id_t
, int, classfuncs_t
**);
61 static struct sclass csw
= {
67 static struct modlsched modlsched
= {
68 &mod_schedops
, "realtime scheduling class", &csw
71 static struct modlinkage modlinkage
= {
72 MODREV_1
, (void *)&modlsched
, NULL
78 return (mod_install(&modlinkage
));
84 return (EBUSY
); /* don't remove RT for now */
88 _info(struct modinfo
*modinfop
)
90 return (mod_info(&modlinkage
, modinfop
));
95 * Class specific code for the real-time class
99 * Extern declarations for variables defined in the rt master file
103 pri_t rt_maxpri
= RTMAXPRI
; /* maximum real-time priority */
104 rtdpent_t
*rt_dptbl
; /* real-time dispatcher parameter table */
107 * control flags (kparms->rt_cflags).
109 #define RT_DOPRI 0x01 /* change priority */
110 #define RT_DOTQ 0x02 /* change RT time quantum */
111 #define RT_DOSIG 0x04 /* change RT time quantum signal */
113 static int rt_admin(caddr_t
, cred_t
*);
114 static int rt_enterclass(kthread_t
*, id_t
, void *, cred_t
*, void *);
115 static int rt_fork(kthread_t
*, kthread_t
*, void *);
116 static int rt_getclinfo(void *);
117 static int rt_getclpri(pcpri_t
*);
118 static int rt_parmsin(void *);
119 static int rt_parmsout(void *, pc_vaparms_t
*);
120 static int rt_vaparmsin(void *, pc_vaparms_t
*);
121 static int rt_vaparmsout(void *, pc_vaparms_t
*);
122 static int rt_parmsset(kthread_t
*, void *, id_t
, cred_t
*);
123 static int rt_donice(kthread_t
*, cred_t
*, int, int *);
124 static int rt_doprio(kthread_t
*, cred_t
*, int, int *);
125 static void rt_exitclass(void *);
126 static int rt_canexit(kthread_t
*, cred_t
*);
127 static void rt_forkret(kthread_t
*, kthread_t
*);
128 static void rt_nullsys();
129 static void rt_parmsget(kthread_t
*, void *);
130 static void rt_preempt(kthread_t
*);
131 static void rt_setrun(kthread_t
*);
132 static void rt_tick(kthread_t
*);
133 static void rt_wakeup(kthread_t
*);
134 static pri_t
rt_swapin(kthread_t
*, int);
135 static pri_t
rt_swapout(kthread_t
*, int);
136 static pri_t
rt_globpri(kthread_t
*);
137 static void rt_yield(kthread_t
*);
138 static int rt_alloc(void **, int);
139 static void rt_free(void *);
141 static void rt_change_priority(kthread_t
*, rtproc_t
*);
143 static id_t rt_cid
; /* real-time class ID */
144 static rtproc_t rt_plisthead
; /* dummy rtproc at head of rtproc list */
145 static kmutex_t rt_dptblock
; /* protects realtime dispatch table */
146 static kmutex_t rt_list_lock
; /* protects RT thread list */
148 extern rtdpent_t
*rt_getdptbl(void);
150 static struct classfuncs rt_classfuncs
= {
169 rt_nullsys
, /* stop */
170 rt_nullsys
, /* exit */
171 rt_nullsys
, /* active */
172 rt_nullsys
, /* inactive */
175 rt_nullsys
, /* trapret */
178 rt_nullsys
, /* sleep */
183 rt_nullsys
, /* set_process_group */
189 * Real-time class initialization. Called by dispinit() at boot time.
190 * We can ignore the clparmsz argument since we know that the smallest
191 * possible parameter buffer is big enough for us.
195 rt_init(id_t cid
, int clparmsz
, classfuncs_t
**clfuncspp
)
197 rt_dptbl
= rt_getdptbl();
198 rt_cid
= cid
; /* Record our class ID */
201 * Initialize the rtproc list.
203 rt_plisthead
.rt_next
= rt_plisthead
.rt_prev
= &rt_plisthead
;
206 * We're required to return a pointer to our classfuncs
207 * structure and the highest global priority value we use.
209 *clfuncspp
= &rt_classfuncs
;
210 mutex_init(&rt_dptblock
, NULL
, MUTEX_DEFAULT
, NULL
);
211 mutex_init(&rt_list_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
212 return (rt_dptbl
[rt_maxpri
].rt_globpri
);
216 * Get or reset the rt_dptbl values per the user's request.
220 rt_admin(caddr_t uaddr
, cred_t
*reqpcredp
)
228 if (get_udatamodel() == DATAMODEL_NATIVE
) {
229 if (copyin(uaddr
, &rtadmin
, sizeof (rtadmin_t
)))
232 #ifdef _SYSCALL32_IMPL
234 /* rtadmin struct from ILP32 callers */
235 rtadmin32_t rtadmin32
;
236 if (copyin(uaddr
, &rtadmin32
, sizeof (rtadmin32_t
)))
239 (struct rtdpent
*)(uintptr_t)rtadmin32
.rt_dpents
;
240 rtadmin
.rt_ndpents
= rtadmin32
.rt_ndpents
;
241 rtadmin
.rt_cmd
= rtadmin32
.rt_cmd
;
243 #endif /* _SYSCALL32_IMPL */
245 rtdpsz
= (rt_maxpri
+ 1) * sizeof (rtdpent_t
);
247 switch (rtadmin
.rt_cmd
) {
250 rtadmin
.rt_ndpents
= rt_maxpri
+ 1;
252 if (get_udatamodel() == DATAMODEL_NATIVE
) {
253 if (copyout(&rtadmin
, uaddr
, sizeof (rtadmin_t
)))
256 #ifdef _SYSCALL32_IMPL
258 /* return rtadmin struct to ILP32 callers */
259 rtadmin32_t rtadmin32
;
260 rtadmin32
.rt_dpents
=
261 (caddr32_t
)(uintptr_t)rtadmin
.rt_dpents
;
262 rtadmin32
.rt_ndpents
= rtadmin
.rt_ndpents
;
263 rtadmin32
.rt_cmd
= rtadmin
.rt_cmd
;
264 if (copyout(&rtadmin32
, uaddr
, sizeof (rtadmin32_t
)))
267 #endif /* _SYSCALL32_IMPL */
272 userdpsz
= MIN(rtadmin
.rt_ndpents
* sizeof (rtdpent_t
),
274 if (copyout(rt_dptbl
, rtadmin
.rt_dpents
, userdpsz
))
276 rtadmin
.rt_ndpents
= userdpsz
/ sizeof (rtdpent_t
);
278 if (get_udatamodel() == DATAMODEL_NATIVE
) {
279 if (copyout(&rtadmin
, uaddr
, sizeof (rtadmin_t
)))
282 #ifdef _SYSCALL32_IMPL
284 /* return rtadmin struct to ILP32 callers */
285 rtadmin32_t rtadmin32
;
286 rtadmin32
.rt_dpents
=
287 (caddr32_t
)(uintptr_t)rtadmin
.rt_dpents
;
288 rtadmin32
.rt_ndpents
= rtadmin
.rt_ndpents
;
289 rtadmin32
.rt_cmd
= rtadmin
.rt_cmd
;
290 if (copyout(&rtadmin32
, uaddr
, sizeof (rtadmin32_t
)))
293 #endif /* _SYSCALL32_IMPL */
298 * We require that the requesting process has sufficient
299 * priveleges. We also require that the table supplied by
300 * the user exactly match the current rt_dptbl in size.
302 if (secpolicy_dispadm(reqpcredp
) != 0)
304 if (rtadmin
.rt_ndpents
* sizeof (rtdpent_t
) != rtdpsz
)
308 * We read the user supplied table into a temporary buffer
309 * where the time quantum values are validated before
310 * being copied to the rt_dptbl.
312 tmpdpp
= kmem_alloc(rtdpsz
, KM_SLEEP
);
313 if (copyin(rtadmin
.rt_dpents
, tmpdpp
, rtdpsz
)) {
314 kmem_free(tmpdpp
, rtdpsz
);
317 for (i
= 0; i
< rtadmin
.rt_ndpents
; i
++) {
320 * Validate the user supplied time quantum values.
322 if (tmpdpp
[i
].rt_quantum
<= 0 &&
323 tmpdpp
[i
].rt_quantum
!= RT_TQINF
) {
324 kmem_free(tmpdpp
, rtdpsz
);
330 * Copy the user supplied values over the current rt_dptbl
331 * values. The rt_globpri member is read-only so we don't
334 mutex_enter(&rt_dptblock
);
335 for (i
= 0; i
< rtadmin
.rt_ndpents
; i
++)
336 rt_dptbl
[i
].rt_quantum
= tmpdpp
[i
].rt_quantum
;
337 mutex_exit(&rt_dptblock
);
338 kmem_free(tmpdpp
, rtdpsz
);
349 * Allocate a real-time class specific proc structure and
350 * initialize it with the parameters supplied. Also move thread
351 * to specified real-time priority.
355 rt_enterclass(kthread_t
*t
, id_t cid
, void *parmsp
, cred_t
*reqpcredp
,
358 rtkparms_t
*rtkparmsp
= (rtkparms_t
*)parmsp
;
362 * For a thread to enter the real-time class the thread
363 * which initiates the request must be privileged.
364 * This may have been checked previously but if our
365 * caller passed us a credential structure we assume it
366 * hasn't and we check it here.
368 if (reqpcredp
!= NULL
&& secpolicy_setpriority(reqpcredp
) != 0)
371 rtpp
= (rtproc_t
*)bufp
;
372 ASSERT(rtpp
!= NULL
);
375 * If this thread's lwp is swapped out, it will be brought in
376 * when it is put onto the runqueue.
378 * Now, Initialize the rtproc structure.
380 if (rtkparmsp
== NULL
) {
385 rtpp
->rt_pquantum
= rt_dptbl
[0].rt_quantum
;
386 rtpp
->rt_tqsignal
= 0;
389 * Use supplied values
391 if ((rtkparmsp
->rt_cflags
& RT_DOPRI
) == 0)
394 rtpp
->rt_pri
= rtkparmsp
->rt_pri
;
396 if (rtkparmsp
->rt_tqntm
== RT_TQINF
)
397 rtpp
->rt_pquantum
= RT_TQINF
;
398 else if (rtkparmsp
->rt_tqntm
== RT_TQDEF
||
399 (rtkparmsp
->rt_cflags
& RT_DOTQ
) == 0)
400 rtpp
->rt_pquantum
= rt_dptbl
[rtpp
->rt_pri
].rt_quantum
;
402 rtpp
->rt_pquantum
= rtkparmsp
->rt_tqntm
;
404 if ((rtkparmsp
->rt_cflags
& RT_DOSIG
) == 0)
405 rtpp
->rt_tqsignal
= 0;
407 rtpp
->rt_tqsignal
= rtkparmsp
->rt_tqsig
;
412 * Reset thread priority
415 t
->t_clfuncs
= &(sclass
[cid
].cl_funcs
->thread
);
417 t
->t_cldata
= (void *)rtpp
;
418 t
->t_schedflag
&= ~TS_RUNQMATCH
;
419 rt_change_priority(t
, rtpp
);
422 * Link new structure into rtproc list
424 mutex_enter(&rt_list_lock
);
425 rtpp
->rt_next
= rt_plisthead
.rt_next
;
426 rtpp
->rt_prev
= &rt_plisthead
;
427 rt_plisthead
.rt_next
->rt_prev
= rtpp
;
428 rt_plisthead
.rt_next
= rtpp
;
429 mutex_exit(&rt_list_lock
);
435 * Free rtproc structure of thread.
438 rt_exitclass(void *procp
)
440 rtproc_t
*rtprocp
= (rtproc_t
*)procp
;
442 mutex_enter(&rt_list_lock
);
443 rtprocp
->rt_prev
->rt_next
= rtprocp
->rt_next
;
444 rtprocp
->rt_next
->rt_prev
= rtprocp
->rt_prev
;
445 mutex_exit(&rt_list_lock
);
446 kmem_free(rtprocp
, sizeof (rtproc_t
));
451 * Allocate and initialize real-time class specific
452 * proc structure for child.
456 rt_fork(kthread_t
*t
, kthread_t
*ct
, void *bufp
)
461 ASSERT(MUTEX_HELD(&ttoproc(t
)->p_lock
));
464 * Initialize child's rtproc structure
466 crtpp
= (rtproc_t
*)bufp
;
467 ASSERT(crtpp
!= NULL
);
468 prtpp
= (rtproc_t
*)t
->t_cldata
;
470 crtpp
->rt_timeleft
= crtpp
->rt_pquantum
= prtpp
->rt_pquantum
;
471 crtpp
->rt_pri
= prtpp
->rt_pri
;
472 crtpp
->rt_flags
= prtpp
->rt_flags
& ~RTBACKQ
;
473 crtpp
->rt_tqsignal
= prtpp
->rt_tqsignal
;
479 * Link new structure into rtproc list
481 ct
->t_cldata
= (void *)crtpp
;
482 mutex_enter(&rt_list_lock
);
483 crtpp
->rt_next
= rt_plisthead
.rt_next
;
484 crtpp
->rt_prev
= &rt_plisthead
;
485 rt_plisthead
.rt_next
->rt_prev
= crtpp
;
486 rt_plisthead
.rt_next
= crtpp
;
487 mutex_exit(&rt_list_lock
);
493 * The child goes to the back of its dispatcher queue while the
494 * parent continues to run after a real time thread forks.
498 rt_forkret(kthread_t
*t
, kthread_t
*ct
)
500 proc_t
*pp
= ttoproc(t
);
501 proc_t
*cp
= ttoproc(ct
);
503 ASSERT(t
== curthread
);
504 ASSERT(MUTEX_HELD(&pidlock
));
507 * Grab the child's p_lock before dropping pidlock to ensure
508 * the process does not disappear before we set it running.
510 mutex_enter(&cp
->p_lock
);
511 mutex_exit(&pidlock
);
513 mutex_exit(&cp
->p_lock
);
515 mutex_enter(&pp
->p_lock
);
517 mutex_exit(&pp
->p_lock
);
522 * Get information about the real-time class into the buffer
523 * pointed to by rtinfop. The maximum configured real-time
524 * priority is the only information we supply. We ignore the
525 * class and credential arguments because anyone can have this
530 rt_getclinfo(void *infop
)
532 rtinfo_t
*rtinfop
= (rtinfo_t
*)infop
;
533 rtinfop
->rt_maxpri
= rt_maxpri
;
538 * Return the user mode scheduling priority range.
541 rt_getclpri(pcpri_t
*pcprip
)
543 pcprip
->pc_clpmax
= rt_maxpri
;
544 pcprip
->pc_clpmin
= 0;
555 rt_canexit(kthread_t
*t
, cred_t
*cred
)
558 * Thread can always leave RT class
564 * Get the real-time scheduling parameters of the thread pointed to by
565 * rtprocp into the buffer pointed to by rtkparmsp.
568 rt_parmsget(kthread_t
*t
, void *parmsp
)
570 rtproc_t
*rtprocp
= (rtproc_t
*)t
->t_cldata
;
571 rtkparms_t
*rtkparmsp
= (rtkparms_t
*)parmsp
;
573 rtkparmsp
->rt_pri
= rtprocp
->rt_pri
;
574 rtkparmsp
->rt_tqntm
= rtprocp
->rt_pquantum
;
575 rtkparmsp
->rt_tqsig
= rtprocp
->rt_tqsignal
;
581 * Check the validity of the real-time parameters in the buffer
582 * pointed to by rtprmsp.
583 * We convert the rtparms buffer from the user supplied format to
584 * our internal format (i.e. time quantum expressed in ticks).
587 rt_parmsin(void *prmsp
)
589 rtparms_t
*rtprmsp
= (rtparms_t
*)prmsp
;
594 * First check the validity of parameters and convert
595 * the buffer to kernel format.
597 if ((rtprmsp
->rt_pri
< 0 || rtprmsp
->rt_pri
> rt_maxpri
) &&
598 rtprmsp
->rt_pri
!= RT_NOCHANGE
)
601 cflags
= (rtprmsp
->rt_pri
!= RT_NOCHANGE
? RT_DOPRI
: 0);
603 if ((rtprmsp
->rt_tqsecs
== 0 && rtprmsp
->rt_tqnsecs
== 0) ||
604 rtprmsp
->rt_tqnsecs
>= NANOSEC
)
607 if (rtprmsp
->rt_tqnsecs
!= RT_NOCHANGE
)
610 if (rtprmsp
->rt_tqnsecs
>= 0) {
611 if ((ticks
= SEC_TO_TICK((longlong_t
)rtprmsp
->rt_tqsecs
) +
612 NSEC_TO_TICK_ROUNDUP(rtprmsp
->rt_tqnsecs
)) > INT_MAX
)
615 ((rtkparms_t
*)rtprmsp
)->rt_tqntm
= (int)ticks
;
617 if (rtprmsp
->rt_tqnsecs
!= RT_NOCHANGE
&&
618 rtprmsp
->rt_tqnsecs
!= RT_TQINF
&&
619 rtprmsp
->rt_tqnsecs
!= RT_TQDEF
)
622 ((rtkparms_t
*)rtprmsp
)->rt_tqntm
= rtprmsp
->rt_tqnsecs
;
624 ((rtkparms_t
*)rtprmsp
)->rt_cflags
= cflags
;
631 * Check the validity of the real-time parameters in the pc_vaparms_t
632 * structure vaparmsp and put them in the buffer pointed to by rtprmsp.
633 * pc_vaparms_t contains (key, value) pairs of parameter.
634 * rt_vaparmsin() is the variable parameter version of rt_parmsin().
637 rt_vaparmsin(void *prmsp
, pc_vaparms_t
*vaparmsp
)
642 int priflag
, secflag
, nsecflag
, sigflag
;
644 rtkparms_t
*rtprmsp
= (rtkparms_t
*)prmsp
;
645 pc_vaparm_t
*vpp
= &vaparmsp
->pc_parms
[0];
649 * First check the validity of parameters and convert them
650 * from the user supplied format to the internal format.
652 priflag
= secflag
= nsecflag
= sigflag
= 0;
653 rtprmsp
->rt_cflags
= 0;
655 if (vaparmsp
->pc_vaparmscnt
> PC_VAPARMCNT
)
658 for (cnt
= 0; cnt
< vaparmsp
->pc_vaparmscnt
; cnt
++, vpp
++) {
660 switch (vpp
->pc_key
) {
664 rtprmsp
->rt_cflags
|= RT_DOPRI
;
665 rtprmsp
->rt_pri
= (pri_t
)vpp
->pc_parm
;
666 if (rtprmsp
->rt_pri
< 0 || rtprmsp
->rt_pri
> rt_maxpri
)
673 rtprmsp
->rt_cflags
|= RT_DOTQ
;
674 secs
= (uint_t
)vpp
->pc_parm
;
680 rtprmsp
->rt_cflags
|= RT_DOTQ
;
681 nsecs
= (int)vpp
->pc_parm
;
687 rtprmsp
->rt_cflags
|= RT_DOSIG
;
688 rtprmsp
->rt_tqsig
= (int)vpp
->pc_parm
;
689 if (rtprmsp
->rt_tqsig
< 0 || rtprmsp
->rt_tqsig
>= NSIG
)
698 if (vaparmsp
->pc_vaparmscnt
== 0) {
700 * Use default parameters.
703 rtprmsp
->rt_tqntm
= RT_TQDEF
;
704 rtprmsp
->rt_tqsig
= 0;
705 rtprmsp
->rt_cflags
= RT_DOPRI
| RT_DOTQ
| RT_DOSIG
;
706 } else if ((rtprmsp
->rt_cflags
& RT_DOTQ
) != 0) {
707 if ((secs
== 0 && nsecs
== 0) || nsecs
>= NANOSEC
)
711 if ((ticks
= SEC_TO_TICK((longlong_t
)secs
) +
712 NSEC_TO_TICK_ROUNDUP(nsecs
)) > INT_MAX
)
715 rtprmsp
->rt_tqntm
= (int)ticks
;
717 if (nsecs
!= RT_TQINF
&& nsecs
!= RT_TQDEF
)
719 rtprmsp
->rt_tqntm
= nsecs
;
727 * Do required processing on the real-time parameter buffer
728 * before it is copied out to the user.
729 * All we have to do is convert the buffer from kernel to user format
730 * (i.e. convert time quantum from ticks to seconds-nanoseconds).
734 rt_parmsout(void *prmsp
, pc_vaparms_t
*vaparmsp
)
736 rtkparms_t
*rtkprmsp
= (rtkparms_t
*)prmsp
;
738 if (vaparmsp
!= NULL
)
741 if (rtkprmsp
->rt_tqntm
< 0) {
743 * Quantum field set to special value (e.g. RT_TQINF)
745 ((rtparms_t
*)rtkprmsp
)->rt_tqnsecs
= rtkprmsp
->rt_tqntm
;
746 ((rtparms_t
*)rtkprmsp
)->rt_tqsecs
= 0;
748 /* Convert quantum from ticks to seconds-nanoseconds */
751 TICK_TO_TIMESTRUC(rtkprmsp
->rt_tqntm
, &ts
);
752 ((rtparms_t
*)rtkprmsp
)->rt_tqsecs
= ts
.tv_sec
;
753 ((rtparms_t
*)rtkprmsp
)->rt_tqnsecs
= ts
.tv_nsec
;
761 * Copy all selected real-time class parameters to the user.
762 * The parameters are specified by a key.
765 rt_vaparmsout(void *prmsp
, pc_vaparms_t
*vaparmsp
)
767 rtkparms_t
*rtkprmsp
= (rtkparms_t
*)prmsp
;
772 int priflag
, secflag
, nsecflag
, sigflag
;
773 pc_vaparm_t
*vpp
= &vaparmsp
->pc_parms
[0];
775 ASSERT(MUTEX_NOT_HELD(&curproc
->p_lock
));
777 priflag
= secflag
= nsecflag
= sigflag
= 0;
779 if (vaparmsp
->pc_vaparmscnt
> PC_VAPARMCNT
)
782 if (rtkprmsp
->rt_tqntm
< 0) {
784 * Quantum field set to special value (e.g. RT_TQINF).
787 nsecs
= rtkprmsp
->rt_tqntm
;
790 * Convert quantum from ticks to seconds-nanoseconds.
792 TICK_TO_TIMESTRUC(rtkprmsp
->rt_tqntm
, &ts
);
798 for (cnt
= 0; cnt
< vaparmsp
->pc_vaparmscnt
; cnt
++, vpp
++) {
800 switch (vpp
->pc_key
) {
804 if (copyout(&rtkprmsp
->rt_pri
,
805 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (pri_t
)))
812 if (copyout(&secs
, (caddr_t
)(uintptr_t)vpp
->pc_parm
,
820 if (copyout(&nsecs
, (caddr_t
)(uintptr_t)vpp
->pc_parm
,
828 if (copyout(&rtkprmsp
->rt_tqsig
,
829 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (int)))
843 * Set the scheduling parameters of the thread pointed to by rtprocp
844 * to those specified in the buffer pointed to by rtkprmsp.
845 * Note that the parameters are expected to be in kernel format
846 * (i.e. time quantm expressed in ticks). Real time parameters copied
847 * in from the user should be processed by rt_parmsin() before they are
848 * passed to this function.
851 rt_parmsset(kthread_t
*tx
, void *prmsp
, id_t reqpcid
, cred_t
*reqpcredp
)
853 rtkparms_t
*rtkprmsp
= (rtkparms_t
*)prmsp
;
854 rtproc_t
*rtpp
= (rtproc_t
*)tx
->t_cldata
;
856 ASSERT(MUTEX_HELD(&(ttoproc(tx
))->p_lock
));
859 * Basic permissions enforced by generic kernel code
860 * for all classes require that a thread attempting
861 * to change the scheduling parameters of a target thread
862 * be privileged or have a real or effective UID
863 * matching that of the target thread. We are not
864 * called unless these basic permission checks have
865 * already passed. The real-time class requires in addition
866 * that the requesting thread be real-time unless it is privileged.
867 * This may also have been checked previously but if our caller
868 * passes us a credential structure we assume it hasn't and
871 if (reqpcredp
!= NULL
&& reqpcid
!= rt_cid
&&
872 secpolicy_raisepriority(reqpcredp
) != 0)
876 if ((rtkprmsp
->rt_cflags
& RT_DOPRI
) != 0) {
877 rtpp
->rt_pri
= rtkprmsp
->rt_pri
;
878 rt_change_priority(tx
, rtpp
);
880 if (rtkprmsp
->rt_tqntm
== RT_TQINF
)
881 rtpp
->rt_pquantum
= RT_TQINF
;
882 else if (rtkprmsp
->rt_tqntm
== RT_TQDEF
)
883 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
=
884 rt_dptbl
[rtpp
->rt_pri
].rt_quantum
;
885 else if ((rtkprmsp
->rt_cflags
& RT_DOTQ
) != 0)
886 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
= rtkprmsp
->rt_tqntm
;
888 if ((rtkprmsp
->rt_cflags
& RT_DOSIG
) != 0)
889 rtpp
->rt_tqsignal
= rtkprmsp
->rt_tqsig
;
897 * Arrange for thread to be placed in appropriate location
898 * on dispatcher queue. Runs at splhi() since the clock
899 * interrupt can cause RTBACKQ to be set.
902 rt_preempt(kthread_t
*t
)
904 rtproc_t
*rtpp
= (rtproc_t
*)(t
->t_cldata
);
907 ASSERT(THREAD_LOCK_HELD(t
));
910 * If the state is user I allow swapping because I know I won't
911 * be holding any locks.
913 if ((lwp
= curthread
->t_lwp
) != NULL
&& lwp
->lwp_state
== LWP_USER
)
914 t
->t_schedflag
&= ~TS_DONT_SWAP
;
915 if ((rtpp
->rt_flags
& RTBACKQ
) != 0) {
916 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
;
917 rtpp
->rt_flags
&= ~RTBACKQ
;
925 * Return the global priority associated with this rt_pri.
928 rt_globpri(kthread_t
*t
)
930 rtproc_t
*rtprocp
= (rtproc_t
*)t
->t_cldata
;
931 return (rt_dptbl
[rtprocp
->rt_pri
].rt_globpri
);
935 rt_setrun(kthread_t
*t
)
937 rtproc_t
*rtpp
= (rtproc_t
*)(t
->t_cldata
);
939 ASSERT(THREAD_LOCK_HELD(t
));
941 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
;
942 rtpp
->rt_flags
&= ~RTBACKQ
;
947 * Returns the priority of the thread, -1 if the thread is loaded or ineligible
950 * FX and RT threads are designed so that they don't swapout; however, it
951 * is possible that while the thread is swapped out and in another class, it
952 * can be changed to FX or RT. Since these threads should be swapped in as
953 * soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
954 * returns SHRT_MAX - 1, so that it gives deference to any swapped out RT
959 rt_swapin(kthread_t
*t
, int flags
)
963 ASSERT(THREAD_LOCK_HELD(t
));
965 if (t
->t_state
== TS_RUN
&& (t
->t_schedflag
& TS_LOAD
) == 0) {
966 tpri
= (pri_t
)SHRT_MAX
;
973 * Return an effective priority for swapout.
977 rt_swapout(kthread_t
*t
, int flags
)
979 ASSERT(THREAD_LOCK_HELD(t
));
985 * Check for time slice expiration (unless thread has infinite time
986 * slice). If time slice has expired arrange for thread to be preempted
987 * and placed on back of queue.
990 rt_tick(kthread_t
*t
)
992 rtproc_t
*rtpp
= (rtproc_t
*)(t
->t_cldata
);
994 ASSERT(MUTEX_HELD(&(ttoproc(t
))->p_lock
));
997 if ((rtpp
->rt_pquantum
!= RT_TQINF
&& --rtpp
->rt_timeleft
== 0) ||
998 (t
->t_state
== TS_ONPROC
&& DISP_MUST_SURRENDER(t
))) {
999 if (rtpp
->rt_timeleft
== 0 && rtpp
->rt_tqsignal
) {
1001 sigtoproc(ttoproc(t
), t
, rtpp
->rt_tqsignal
);
1004 rtpp
->rt_flags
|= RTBACKQ
;
1012 * Place the thread waking up on the dispatcher queue.
1015 rt_wakeup(kthread_t
*t
)
1017 rtproc_t
*rtpp
= (rtproc_t
*)(t
->t_cldata
);
1019 ASSERT(THREAD_LOCK_HELD(t
));
1021 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
;
1022 rtpp
->rt_flags
&= ~RTBACKQ
;
1027 rt_yield(kthread_t
*t
)
1029 rtproc_t
*rtpp
= (rtproc_t
*)(t
->t_cldata
);
1031 ASSERT(t
== curthread
);
1032 ASSERT(THREAD_LOCK_HELD(t
));
1034 rtpp
->rt_flags
&= ~RTBACKQ
;
1040 rt_donice(kthread_t
*t
, cred_t
*cr
, int incr
, int *retvalp
)
1046 * Increment the priority of the specified thread by incr and
1047 * return the new value in *retvalp.
1050 rt_doprio(kthread_t
*t
, cred_t
*cr
, int incr
, int *retvalp
)
1053 rtproc_t
*rtpp
= (rtproc_t
*)(t
->t_cldata
);
1054 rtkparms_t rtkparms
;
1056 /* If there's no change to the priority, just return current setting */
1058 *retvalp
= rtpp
->rt_pri
;
1062 newpri
= rtpp
->rt_pri
+ incr
;
1063 if (newpri
> rt_maxpri
|| newpri
< 0)
1067 rtkparms
.rt_pri
= newpri
;
1068 rtkparms
.rt_tqntm
= RT_NOCHANGE
;
1069 rtkparms
.rt_tqsig
= 0;
1070 rtkparms
.rt_cflags
= RT_DOPRI
;
1071 return (rt_parmsset(t
, &rtkparms
, rt_cid
, cr
));
1075 rt_alloc(void **p
, int flag
)
1078 bufp
= kmem_alloc(sizeof (rtproc_t
), flag
);
1091 kmem_free(bufp
, sizeof (rtproc_t
));
1095 rt_change_priority(kthread_t
*t
, rtproc_t
*rtpp
)
1099 ASSERT(THREAD_LOCK_HELD(t
));
1101 new_pri
= rt_dptbl
[rtpp
->rt_pri
].rt_globpri
;
1103 t
->t_cpri
= rtpp
->rt_pri
;
1104 if (t
== curthread
|| t
->t_state
== TS_ONPROC
) {
1105 cpu_t
*cp
= t
->t_disp_queue
->disp_cpu
;
1106 THREAD_CHANGE_PRI(t
, new_pri
);
1107 if (t
== cp
->cpu_dispthread
)
1108 cp
->cpu_dispatch_pri
= DISP_PRIO(t
);
1109 if (DISP_MUST_SURRENDER(t
)) {
1110 rtpp
->rt_flags
|= RTBACKQ
;
1113 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
;
1117 * When the priority of a thread is changed,
1118 * it may be necessary to adjust its position
1119 * on a sleep queue or dispatch queue. The
1120 * function thread_change_pri() accomplishes this.
1122 if (thread_change_pri(t
, new_pri
, 0)) {
1124 * The thread was on a run queue.
1125 * Reset its CPU timeleft.
1127 rtpp
->rt_timeleft
= rtpp
->rt_pquantum
;
1129 rtpp
->rt_flags
|= RTBACKQ
;