xref: /illumos-gate/usr/src/uts/common/disp/fx.c (revision 634e26ec)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/sysmacros.h>
30 #include <sys/cred.h>
31 #include <sys/proc.h>
32 #include <sys/session.h>
33 #include <sys/strsubr.h>
34 #include <sys/user.h>
35 #include <sys/priocntl.h>
36 #include <sys/class.h>
37 #include <sys/disp.h>
38 #include <sys/procset.h>
39 #include <sys/debug.h>
40 #include <sys/kmem.h>
41 #include <sys/errno.h>
42 #include <sys/fx.h>
43 #include <sys/fxpriocntl.h>
44 #include <sys/cpuvar.h>
45 #include <sys/systm.h>
46 #include <sys/vtrace.h>
47 #include <sys/schedctl.h>
48 #include <sys/tnf_probe.h>
49 #include <sys/sunddi.h>
50 #include <sys/spl.h>
51 #include <sys/modctl.h>
52 #include <sys/policy.h>
53 #include <sys/sdt.h>
54 #include <sys/cpupart.h>
55 #include <sys/cpucaps.h>
56 
57 static pri_t fx_init(id_t, int, classfuncs_t **);
58 
59 static struct sclass csw = {
60 	"FX",
61 	fx_init,
62 	0
63 };
64 
65 static struct modlsched modlsched = {
66 	&mod_schedops, "Fixed priority sched class", &csw
67 };
68 
69 static struct modlinkage modlinkage = {
70 	MODREV_1, (void *)&modlsched, NULL
71 };
72 
73 
74 /*
75  * control flags (kparms->fx_cflags).
76  */
77 #define	FX_DOUPRILIM	0x01    /* change user priority limit */
78 #define	FX_DOUPRI	0x02    /* change user priority */
79 #define	FX_DOTQ		0x04    /* change FX time quantum */
80 
81 
82 #define	FXMAXUPRI 60		/* maximum user priority setting */
83 
84 #define	FX_MAX_UNPRIV_PRI	0	/* maximum unpriviledge priority */
85 
86 /*
87  * The fxproc_t structures that have a registered callback vector,
88  * are also kept in an array of circular doubly linked lists. A hash on
89  * the thread id (from ddi_get_kt_did()) is used to determine which list
90  * each of such fxproc structures should be placed. Each list has a dummy
91  * "head" which is never removed, so the list is never empty.
92  */
93 
94 #define	FX_CB_LISTS 16		/* number of lists, must be power of 2 */
95 #define	FX_CB_LIST_HASH(ktid)	((uint_t)ktid & (FX_CB_LISTS - 1))
96 
97 /* Insert fxproc into callback list */
98 #define	FX_CB_LIST_INSERT(fxpp)						\
99 {									\
100 	int index = FX_CB_LIST_HASH(fxpp->fx_ktid);			\
101 	kmutex_t *lockp = &fx_cb_list_lock[index];			\
102 	fxproc_t *headp = &fx_cb_plisthead[index];			\
103 	mutex_enter(lockp);						\
104 	fxpp->fx_cb_next = headp->fx_cb_next;				\
105 	fxpp->fx_cb_prev = headp;					\
106 	headp->fx_cb_next->fx_cb_prev = fxpp;				\
107 	headp->fx_cb_next = fxpp;					\
108 	mutex_exit(lockp);						\
109 }
110 
111 /*
112  * Remove thread from callback list.
113  */
114 #define	FX_CB_LIST_DELETE(fxpp)						\
115 {									\
116 	int index = FX_CB_LIST_HASH(fxpp->fx_ktid);			\
117 	kmutex_t *lockp = &fx_cb_list_lock[index];			\
118 	mutex_enter(lockp);						\
119 	fxpp->fx_cb_prev->fx_cb_next = fxpp->fx_cb_next;		\
120 	fxpp->fx_cb_next->fx_cb_prev = fxpp->fx_cb_prev;		\
121 	mutex_exit(lockp);						\
122 }
123 
124 #define	FX_HAS_CB(fxpp)	(fxpp->fx_callback != NULL)
125 
126 /* adjust x to be between 0 and fx_maxumdpri */
127 
128 #define	FX_ADJUST_PRI(pri)						\
129 {									\
130 	if (pri < 0)							\
131 		pri = 0;  						\
132 	else if (pri > fx_maxumdpri) 					\
133 		pri = fx_maxumdpri;  					\
134 }
135 
136 #define	FX_ADJUST_QUANTUM(q)						\
137 {									\
138 	if (q > INT_MAX)						\
139 		q = INT_MAX;						\
140 	else if (q <= 0)						\
141 		q = FX_TQINF;						\
142 }
143 
144 #define	FX_ISVALID(pri, quantum) \
145 	(((pri >= 0) || (pri == FX_CB_NOCHANGE)) &&			\
146 	    ((quantum >= 0) || (quantum == FX_NOCHANGE) ||		\
147 		(quantum == FX_TQDEF) || (quantum == FX_TQINF)))
148 
149 
150 static id_t	fx_cid;		/* fixed priority class ID */
151 static fxdpent_t *fx_dptbl;	/* fixed priority disp parameter table */
152 
153 static pri_t	fx_maxupri = FXMAXUPRI;
154 static pri_t	fx_maxumdpri;	/* max user mode fixed priority */
155 
156 static pri_t	fx_maxglobpri;	/* maximum global priority used by fx class */
157 static kmutex_t	fx_dptblock;	/* protects fixed priority dispatch table */
158 
159 
160 static kmutex_t	fx_cb_list_lock[FX_CB_LISTS];	/* protects list of fxprocs */
161 						/* that have callbacks */
162 static fxproc_t	fx_cb_plisthead[FX_CB_LISTS];	/* dummy fxproc at head of */
163 						/* list of fxprocs with */
164 						/* callbacks */
165 
166 static int	fx_admin(caddr_t, cred_t *);
167 static int	fx_getclinfo(void *);
168 static int	fx_parmsin(void *);
169 static int	fx_parmsout(void *, pc_vaparms_t *);
170 static int	fx_vaparmsin(void *, pc_vaparms_t *);
171 static int	fx_vaparmsout(void *, pc_vaparms_t *);
172 static int	fx_getclpri(pcpri_t *);
173 static int	fx_alloc(void **, int);
174 static void	fx_free(void *);
175 static int	fx_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
176 static void	fx_exitclass(void *);
177 static int	fx_canexit(kthread_t *, cred_t *);
178 static int	fx_fork(kthread_t *, kthread_t *, void *);
179 static void	fx_forkret(kthread_t *, kthread_t *);
180 static void	fx_parmsget(kthread_t *, void *);
181 static int	fx_parmsset(kthread_t *, void *, id_t, cred_t *);
182 static void	fx_stop(kthread_t *, int, int);
183 static void	fx_exit(kthread_t *);
184 static pri_t	fx_swapin(kthread_t *, int);
185 static pri_t	fx_swapout(kthread_t *, int);
186 static void	fx_trapret(kthread_t *);
187 static void	fx_preempt(kthread_t *);
188 static void	fx_setrun(kthread_t *);
189 static void	fx_sleep(kthread_t *);
190 static void	fx_tick(kthread_t *);
191 static void	fx_wakeup(kthread_t *);
192 static int	fx_donice(kthread_t *, cred_t *, int, int *);
193 static int	fx_doprio(kthread_t *, cred_t *, int, int *);
194 static pri_t	fx_globpri(kthread_t *);
195 static void	fx_yield(kthread_t *);
196 static void	fx_nullsys();
197 
198 extern fxdpent_t *fx_getdptbl(void);
199 
200 static void	fx_change_priority(kthread_t *, fxproc_t *);
201 static fxproc_t *fx_list_lookup(kt_did_t);
202 static void fx_list_release(fxproc_t *);
203 
204 
205 static struct classfuncs fx_classfuncs = {
206 	/* class functions */
207 	fx_admin,
208 	fx_getclinfo,
209 	fx_parmsin,
210 	fx_parmsout,
211 	fx_vaparmsin,
212 	fx_vaparmsout,
213 	fx_getclpri,
214 	fx_alloc,
215 	fx_free,
216 
217 	/* thread functions */
218 	fx_enterclass,
219 	fx_exitclass,
220 	fx_canexit,
221 	fx_fork,
222 	fx_forkret,
223 	fx_parmsget,
224 	fx_parmsset,
225 	fx_stop,
226 	fx_exit,
227 	fx_nullsys,	/* active */
228 	fx_nullsys,	/* inactive */
229 	fx_swapin,
230 	fx_swapout,
231 	fx_trapret,
232 	fx_preempt,
233 	fx_setrun,
234 	fx_sleep,
235 	fx_tick,
236 	fx_wakeup,
237 	fx_donice,
238 	fx_globpri,
239 	fx_nullsys,	/* set_process_group */
240 	fx_yield,
241 	fx_doprio,
242 };
243 
244 
245 int
246 _init()
247 {
248 	return (mod_install(&modlinkage));
249 }
250 
251 int
252 _fini()
253 {
254 	return (EBUSY);
255 }
256 
257 int
258 _info(struct modinfo *modinfop)
259 {
260 	return (mod_info(&modlinkage, modinfop));
261 }
262 
263 /*
264  * Fixed priority class initialization. Called by dispinit() at boot time.
265  * We can ignore the clparmsz argument since we know that the smallest
266  * possible parameter buffer is big enough for us.
267  */
268 /* ARGSUSED */
269 static pri_t
270 fx_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
271 {
272 	int i;
273 	extern pri_t fx_getmaxumdpri(void);
274 
275 	fx_dptbl = fx_getdptbl();
276 	fx_maxumdpri = fx_getmaxumdpri();
277 	fx_maxglobpri = fx_dptbl[fx_maxumdpri].fx_globpri;
278 
279 	fx_cid = cid;		/* Record our class ID */
280 
281 	/*
282 	 * Initialize the hash table for fxprocs with callbacks
283 	 */
284 	for (i = 0; i < FX_CB_LISTS; i++) {
285 		fx_cb_plisthead[i].fx_cb_next = fx_cb_plisthead[i].fx_cb_prev =
286 		    &fx_cb_plisthead[i];
287 	}
288 
289 	/*
290 	 * We're required to return a pointer to our classfuncs
291 	 * structure and the highest global priority value we use.
292 	 */
293 	*clfuncspp = &fx_classfuncs;
294 	return (fx_maxglobpri);
295 }
296 
297 /*
298  * Get or reset the fx_dptbl values per the user's request.
299  */
300 static int
301 fx_admin(caddr_t uaddr, cred_t *reqpcredp)
302 {
303 	fxadmin_t	fxadmin;
304 	fxdpent_t	*tmpdpp;
305 	int		userdpsz;
306 	int		i;
307 	size_t		fxdpsz;
308 
309 	if (get_udatamodel() == DATAMODEL_NATIVE) {
310 		if (copyin(uaddr, &fxadmin, sizeof (fxadmin_t)))
311 			return (EFAULT);
312 	}
313 #ifdef _SYSCALL32_IMPL
314 	else {
315 		/* get fxadmin struct from ILP32 caller */
316 		fxadmin32_t fxadmin32;
317 		if (copyin(uaddr, &fxadmin32, sizeof (fxadmin32_t)))
318 			return (EFAULT);
319 		fxadmin.fx_dpents =
320 		    (struct fxdpent *)(uintptr_t)fxadmin32.fx_dpents;
321 		fxadmin.fx_ndpents = fxadmin32.fx_ndpents;
322 		fxadmin.fx_cmd = fxadmin32.fx_cmd;
323 	}
324 #endif /* _SYSCALL32_IMPL */
325 
326 	fxdpsz = (fx_maxumdpri + 1) * sizeof (fxdpent_t);
327 
328 	switch (fxadmin.fx_cmd) {
329 	case FX_GETDPSIZE:
330 		fxadmin.fx_ndpents = fx_maxumdpri + 1;
331 
332 		if (get_udatamodel() == DATAMODEL_NATIVE) {
333 			if (copyout(&fxadmin, uaddr, sizeof (fxadmin_t)))
334 				return (EFAULT);
335 		}
336 #ifdef _SYSCALL32_IMPL
337 		else {
338 			/* return fxadmin struct to ILP32 caller */
339 			fxadmin32_t fxadmin32;
340 			fxadmin32.fx_dpents =
341 			    (caddr32_t)(uintptr_t)fxadmin.fx_dpents;
342 			fxadmin32.fx_ndpents = fxadmin.fx_ndpents;
343 			fxadmin32.fx_cmd = fxadmin.fx_cmd;
344 			if (copyout(&fxadmin32, uaddr, sizeof (fxadmin32_t)))
345 				return (EFAULT);
346 		}
347 #endif /* _SYSCALL32_IMPL */
348 		break;
349 
350 	case FX_GETDPTBL:
351 		userdpsz = MIN(fxadmin.fx_ndpents * sizeof (fxdpent_t),
352 		    fxdpsz);
353 		if (copyout(fx_dptbl, fxadmin.fx_dpents, userdpsz))
354 			return (EFAULT);
355 
356 		fxadmin.fx_ndpents = userdpsz / sizeof (fxdpent_t);
357 
358 		if (get_udatamodel() == DATAMODEL_NATIVE) {
359 			if (copyout(&fxadmin, uaddr, sizeof (fxadmin_t)))
360 				return (EFAULT);
361 		}
362 #ifdef _SYSCALL32_IMPL
363 		else {
364 			/* return fxadmin struct to ILP32 callers */
365 			fxadmin32_t fxadmin32;
366 			fxadmin32.fx_dpents =
367 			    (caddr32_t)(uintptr_t)fxadmin.fx_dpents;
368 			fxadmin32.fx_ndpents = fxadmin.fx_ndpents;
369 			fxadmin32.fx_cmd = fxadmin.fx_cmd;
370 			if (copyout(&fxadmin32, uaddr, sizeof (fxadmin32_t)))
371 				return (EFAULT);
372 		}
373 #endif /* _SYSCALL32_IMPL */
374 		break;
375 
376 	case FX_SETDPTBL:
377 		/*
378 		 * We require that the requesting process has sufficient
379 		 * privileges. We also require that the table supplied by
380 		 * the user exactly match the current fx_dptbl in size.
381 		 */
382 		if (secpolicy_dispadm(reqpcredp) != 0) {
383 			return (EPERM);
384 		}
385 		if (fxadmin.fx_ndpents * sizeof (fxdpent_t) != fxdpsz) {
386 			return (EINVAL);
387 		}
388 
389 		/*
390 		 * We read the user supplied table into a temporary buffer
391 		 * where it is validated before being copied over the
392 		 * fx_dptbl.
393 		 */
394 		tmpdpp = kmem_alloc(fxdpsz, KM_SLEEP);
395 		if (copyin(fxadmin.fx_dpents, tmpdpp, fxdpsz)) {
396 			kmem_free(tmpdpp, fxdpsz);
397 			return (EFAULT);
398 		}
399 		for (i = 0; i < fxadmin.fx_ndpents; i++) {
400 
401 			/*
402 			 * Validate the user supplied values. All we are doing
403 			 * here is verifying that the values are within their
404 			 * allowable ranges and will not panic the system. We
405 			 * make no attempt to ensure that the resulting
406 			 * configuration makes sense or results in reasonable
407 			 * performance.
408 			 */
409 			if (tmpdpp[i].fx_quantum <= 0 &&
410 			    tmpdpp[i].fx_quantum != FX_TQINF) {
411 				kmem_free(tmpdpp, fxdpsz);
412 				return (EINVAL);
413 			}
414 		}
415 
416 		/*
417 		 * Copy the user supplied values over the current fx_dptbl
418 		 * values. The fx_globpri member is read-only so we don't
419 		 * overwrite it.
420 		 */
421 		mutex_enter(&fx_dptblock);
422 		for (i = 0; i < fxadmin.fx_ndpents; i++) {
423 			fx_dptbl[i].fx_quantum = tmpdpp[i].fx_quantum;
424 		}
425 		mutex_exit(&fx_dptblock);
426 		kmem_free(tmpdpp, fxdpsz);
427 		break;
428 
429 	default:
430 		return (EINVAL);
431 	}
432 	return (0);
433 }
434 
435 /*
436  * Allocate a fixed priority class specific thread structure and
437  * initialize it with the parameters supplied. Also move the thread
438  * to specified priority.
439  */
440 static int
441 fx_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
442     void *bufp)
443 {
444 	fxkparms_t	*fxkparmsp = (fxkparms_t *)parmsp;
445 	fxproc_t	*fxpp;
446 	pri_t		reqfxupri;
447 	pri_t		reqfxuprilim;
448 
449 	fxpp = (fxproc_t *)bufp;
450 	ASSERT(fxpp != NULL);
451 
452 	/*
453 	 * Initialize the fxproc structure.
454 	 */
455 	fxpp->fx_flags = 0;
456 	fxpp->fx_callback = NULL;
457 	fxpp->fx_cookie = NULL;
458 
459 	if (fxkparmsp == NULL) {
460 		/*
461 		 * Use default values.
462 		 */
463 		fxpp->fx_pri = fxpp->fx_uprilim = 0;
464 		fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
465 		fxpp->fx_nice =  NZERO;
466 	} else {
467 		/*
468 		 * Use supplied values.
469 		 */
470 
471 		if ((fxkparmsp->fx_cflags & FX_DOUPRILIM) == 0) {
472 			reqfxuprilim = 0;
473 		} else {
474 			if (fxkparmsp->fx_uprilim > FX_MAX_UNPRIV_PRI &&
475 			    secpolicy_setpriority(reqpcredp) != 0)
476 				return (EPERM);
477 			reqfxuprilim = fxkparmsp->fx_uprilim;
478 			FX_ADJUST_PRI(reqfxuprilim);
479 		}
480 
481 		if ((fxkparmsp->fx_cflags & FX_DOUPRI) == 0) {
482 			reqfxupri = reqfxuprilim;
483 		} else {
484 			if (fxkparmsp->fx_upri > FX_MAX_UNPRIV_PRI &&
485 			    secpolicy_setpriority(reqpcredp) != 0)
486 				return (EPERM);
487 			/*
488 			 * Set the user priority to the requested value
489 			 * or the upri limit, whichever is lower.
490 			 */
491 			reqfxupri = fxkparmsp->fx_upri;
492 			FX_ADJUST_PRI(reqfxupri);
493 
494 			if (reqfxupri > reqfxuprilim)
495 				reqfxupri = reqfxuprilim;
496 		}
497 
498 
499 		fxpp->fx_uprilim = reqfxuprilim;
500 		fxpp->fx_pri = reqfxupri;
501 
502 		fxpp->fx_nice = NZERO - (NZERO * reqfxupri) / fx_maxupri;
503 
504 		if (((fxkparmsp->fx_cflags & FX_DOTQ) == 0) ||
505 		    (fxkparmsp->fx_tqntm == FX_TQDEF)) {
506 			fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
507 		} else {
508 			if (secpolicy_setpriority(reqpcredp) != 0)
509 				return (EPERM);
510 
511 			if (fxkparmsp->fx_tqntm == FX_TQINF)
512 				fxpp->fx_pquantum = FX_TQINF;
513 			else {
514 				fxpp->fx_pquantum = fxkparmsp->fx_tqntm;
515 			}
516 		}
517 
518 	}
519 
520 	fxpp->fx_timeleft = fxpp->fx_pquantum;
521 	cpucaps_sc_init(&fxpp->fx_caps);
522 	fxpp->fx_tp = t;
523 
524 	thread_lock(t);			/* get dispatcher lock on thread */
525 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
526 	t->t_cid = cid;
527 	t->t_cldata = (void *)fxpp;
528 	t->t_schedflag &= ~TS_RUNQMATCH;
529 	fx_change_priority(t, fxpp);
530 	thread_unlock(t);
531 
532 	return (0);
533 }
534 
535 /*
536  * The thread is exiting.
537  */
538 static void
539 fx_exit(kthread_t *t)
540 {
541 	fxproc_t *fxpp;
542 
543 	thread_lock(t);
544 	fxpp = (fxproc_t *)(t->t_cldata);
545 
546 	/*
547 	 * A thread could be exiting in between clock ticks, so we need to
548 	 * calculate how much CPU time it used since it was charged last time.
549 	 *
550 	 * CPU caps are not enforced on exiting processes - it is usually
551 	 * desirable to exit as soon as possible to free resources.
552 	 */
553 	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ONLY);
554 
555 	if (FX_HAS_CB(fxpp)) {
556 		FX_CB_EXIT(FX_CALLB(fxpp), fxpp->fx_cookie);
557 		fxpp->fx_callback = NULL;
558 		fxpp->fx_cookie = NULL;
559 		thread_unlock(t);
560 		FX_CB_LIST_DELETE(fxpp);
561 		return;
562 	}
563 
564 	thread_unlock(t);
565 }
566 
567 /*
568  * Exiting the class. Free fxproc structure of thread.
569  */
570 static void
571 fx_exitclass(void *procp)
572 {
573 	fxproc_t *fxpp = (fxproc_t *)procp;
574 
575 	thread_lock(fxpp->fx_tp);
576 	if (FX_HAS_CB(fxpp)) {
577 
578 		FX_CB_EXIT(FX_CALLB(fxpp), fxpp->fx_cookie);
579 
580 		fxpp->fx_callback = NULL;
581 		fxpp->fx_cookie = NULL;
582 		thread_unlock(fxpp->fx_tp);
583 		FX_CB_LIST_DELETE(fxpp);
584 	} else
585 		thread_unlock(fxpp->fx_tp);
586 
587 	kmem_free(fxpp, sizeof (fxproc_t));
588 }
589 
590 /* ARGSUSED */
591 static int
592 fx_canexit(kthread_t *t, cred_t *cred)
593 {
594 	/*
595 	 * A thread can always leave the FX class
596 	 */
597 	return (0);
598 }
599 
600 /*
601  * Initialize fixed-priority class specific proc structure for a child.
602  * callbacks are not inherited upon fork.
603  */
604 static int
605 fx_fork(kthread_t *t, kthread_t *ct, void *bufp)
606 {
607 	fxproc_t	*pfxpp;		/* ptr to parent's fxproc structure */
608 	fxproc_t	*cfxpp;		/* ptr to child's fxproc structure */
609 
610 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
611 
612 	cfxpp = (fxproc_t *)bufp;
613 	ASSERT(cfxpp != NULL);
614 	thread_lock(t);
615 	pfxpp = (fxproc_t *)t->t_cldata;
616 	/*
617 	 * Initialize child's fxproc structure.
618 	 */
619 	cfxpp->fx_timeleft = cfxpp->fx_pquantum = pfxpp->fx_pquantum;
620 	cfxpp->fx_pri = pfxpp->fx_pri;
621 	cfxpp->fx_uprilim = pfxpp->fx_uprilim;
622 	cfxpp->fx_nice = pfxpp->fx_nice;
623 	cfxpp->fx_callback = NULL;
624 	cfxpp->fx_cookie = NULL;
625 	cfxpp->fx_flags = pfxpp->fx_flags & ~(FXBACKQ);
626 	cpucaps_sc_init(&cfxpp->fx_caps);
627 
628 	cfxpp->fx_tp = ct;
629 	ct->t_cldata = (void *)cfxpp;
630 	thread_unlock(t);
631 
632 	/*
633 	 * Link new structure into fxproc list.
634 	 */
635 	return (0);
636 }
637 
638 
639 /*
640  * Child is placed at back of dispatcher queue and parent gives
641  * up processor so that the child runs first after the fork.
642  * This allows the child immediately execing to break the multiple
643  * use of copy on write pages with no disk home. The parent will
644  * get to steal them back rather than uselessly copying them.
645  */
646 static void
647 fx_forkret(kthread_t *t, kthread_t *ct)
648 {
649 	proc_t	*pp = ttoproc(t);
650 	proc_t	*cp = ttoproc(ct);
651 	fxproc_t *fxpp;
652 
653 	ASSERT(t == curthread);
654 	ASSERT(MUTEX_HELD(&pidlock));
655 
656 	/*
657 	 * Grab the child's p_lock before dropping pidlock to ensure
658 	 * the process does not disappear before we set it running.
659 	 */
660 	mutex_enter(&cp->p_lock);
661 	mutex_exit(&pidlock);
662 	continuelwps(cp);
663 	mutex_exit(&cp->p_lock);
664 
665 	mutex_enter(&pp->p_lock);
666 	continuelwps(pp);
667 	mutex_exit(&pp->p_lock);
668 
669 	thread_lock(t);
670 	fxpp = (fxproc_t *)(t->t_cldata);
671 	t->t_pri = fx_dptbl[fxpp->fx_pri].fx_globpri;
672 	ASSERT(t->t_pri >= 0 && t->t_pri <= fx_maxglobpri);
673 	THREAD_TRANSITION(t);
674 	fx_setrun(t);
675 	thread_unlock(t);
676 
677 	swtch();
678 }
679 
680 
681 /*
682  * Get information about the fixed-priority class into the buffer
683  * pointed to by fxinfop. The maximum configured user priority
684  * is the only information we supply.
685  */
686 static int
687 fx_getclinfo(void *infop)
688 {
689 	fxinfo_t *fxinfop = (fxinfo_t *)infop;
690 	fxinfop->fx_maxupri = fx_maxupri;
691 	return (0);
692 }
693 
694 
695 
696 /*
697  * Return the user mode scheduling priority range.
698  */
699 static int
700 fx_getclpri(pcpri_t *pcprip)
701 {
702 	pcprip->pc_clpmax = fx_maxupri;
703 	pcprip->pc_clpmin = 0;
704 	return (0);
705 }
706 
707 
708 static void
709 fx_nullsys()
710 {}
711 
712 
713 /*
714  * Get the fixed-priority parameters of the thread pointed to by
715  * fxprocp into the buffer pointed to by fxparmsp.
716  */
717 static void
718 fx_parmsget(kthread_t *t, void *parmsp)
719 {
720 	fxproc_t *fxpp = (fxproc_t *)t->t_cldata;
721 	fxkparms_t *fxkparmsp = (fxkparms_t *)parmsp;
722 
723 	fxkparmsp->fx_upri = fxpp->fx_pri;
724 	fxkparmsp->fx_uprilim = fxpp->fx_uprilim;
725 	fxkparmsp->fx_tqntm = fxpp->fx_pquantum;
726 }
727 
728 
729 
730 /*
731  * Check the validity of the fixed-priority parameters in the buffer
732  * pointed to by fxparmsp.
733  */
734 static int
735 fx_parmsin(void *parmsp)
736 {
737 	fxparms_t	*fxparmsp = (fxparms_t *)parmsp;
738 	uint_t		cflags;
739 	longlong_t	ticks;
740 	/*
741 	 * Check validity of parameters.
742 	 */
743 
744 	if ((fxparmsp->fx_uprilim > fx_maxupri ||
745 	    fxparmsp->fx_uprilim < 0) &&
746 	    fxparmsp->fx_uprilim != FX_NOCHANGE)
747 		return (EINVAL);
748 
749 	if ((fxparmsp->fx_upri > fx_maxupri ||
750 	    fxparmsp->fx_upri < 0) &&
751 	    fxparmsp->fx_upri != FX_NOCHANGE)
752 		return (EINVAL);
753 
754 	if ((fxparmsp->fx_tqsecs == 0 && fxparmsp->fx_tqnsecs == 0) ||
755 	    fxparmsp->fx_tqnsecs >= NANOSEC)
756 		return (EINVAL);
757 
758 	cflags = (fxparmsp->fx_upri != FX_NOCHANGE ? FX_DOUPRI : 0);
759 
760 	if (fxparmsp->fx_uprilim != FX_NOCHANGE) {
761 		cflags |= FX_DOUPRILIM;
762 	}
763 
764 	if (fxparmsp->fx_tqnsecs != FX_NOCHANGE)
765 		cflags |= FX_DOTQ;
766 
767 	/*
768 	 * convert the buffer to kernel format.
769 	 */
770 
771 	if (fxparmsp->fx_tqnsecs >= 0) {
772 		if ((ticks = SEC_TO_TICK((longlong_t)fxparmsp->fx_tqsecs) +
773 		    NSEC_TO_TICK_ROUNDUP(fxparmsp->fx_tqnsecs)) > INT_MAX)
774 			return (ERANGE);
775 
776 		((fxkparms_t *)fxparmsp)->fx_tqntm = (int)ticks;
777 	} else {
778 		if ((fxparmsp->fx_tqnsecs != FX_NOCHANGE) &&
779 		    (fxparmsp->fx_tqnsecs != FX_TQINF) &&
780 		    (fxparmsp->fx_tqnsecs != FX_TQDEF))
781 			return (EINVAL);
782 		((fxkparms_t *)fxparmsp)->fx_tqntm = fxparmsp->fx_tqnsecs;
783 	}
784 
785 	((fxkparms_t *)fxparmsp)->fx_cflags = cflags;
786 
787 	return (0);
788 }
789 
790 
791 /*
792  * Check the validity of the fixed-priority parameters in the pc_vaparms_t
793  * structure vaparmsp and put them in the buffer pointed to by fxprmsp.
794  * pc_vaparms_t contains (key, value) pairs of parameter.
795  */
796 static int
797 fx_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp)
798 {
799 	uint_t		secs = 0;
800 	uint_t		cnt;
801 	int		nsecs = 0;
802 	int		priflag, secflag, nsecflag, limflag;
803 	longlong_t	ticks;
804 	fxkparms_t	*fxprmsp = (fxkparms_t *)prmsp;
805 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
806 
807 
808 	/*
809 	 * First check the validity of parameters and convert them
810 	 * from the user supplied format to the internal format.
811 	 */
812 	priflag = secflag = nsecflag = limflag = 0;
813 
814 	fxprmsp->fx_cflags = 0;
815 
816 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
817 		return (EINVAL);
818 
819 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
820 
821 		switch (vpp->pc_key) {
822 		case FX_KY_UPRILIM:
823 			if (limflag++)
824 				return (EINVAL);
825 			fxprmsp->fx_cflags |= FX_DOUPRILIM;
826 			fxprmsp->fx_uprilim = (pri_t)vpp->pc_parm;
827 			if (fxprmsp->fx_uprilim > fx_maxupri ||
828 			    fxprmsp->fx_uprilim < 0)
829 				return (EINVAL);
830 			break;
831 
832 		case FX_KY_UPRI:
833 			if (priflag++)
834 				return (EINVAL);
835 			fxprmsp->fx_cflags |= FX_DOUPRI;
836 			fxprmsp->fx_upri = (pri_t)vpp->pc_parm;
837 			if (fxprmsp->fx_upri > fx_maxupri ||
838 			    fxprmsp->fx_upri < 0)
839 				return (EINVAL);
840 			break;
841 
842 		case FX_KY_TQSECS:
843 			if (secflag++)
844 				return (EINVAL);
845 			fxprmsp->fx_cflags |= FX_DOTQ;
846 			secs = (uint_t)vpp->pc_parm;
847 			break;
848 
849 		case FX_KY_TQNSECS:
850 			if (nsecflag++)
851 				return (EINVAL);
852 			fxprmsp->fx_cflags |= FX_DOTQ;
853 			nsecs = (int)vpp->pc_parm;
854 			break;
855 
856 		default:
857 			return (EINVAL);
858 		}
859 	}
860 
861 	if (vaparmsp->pc_vaparmscnt == 0) {
862 		/*
863 		 * Use default parameters.
864 		 */
865 		fxprmsp->fx_upri = 0;
866 		fxprmsp->fx_uprilim = 0;
867 		fxprmsp->fx_tqntm = FX_TQDEF;
868 		fxprmsp->fx_cflags = FX_DOUPRI | FX_DOUPRILIM | FX_DOTQ;
869 	} else if ((fxprmsp->fx_cflags & FX_DOTQ) != 0) {
870 		if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC)
871 			return (EINVAL);
872 
873 		if (nsecs >= 0) {
874 			if ((ticks = SEC_TO_TICK((longlong_t)secs) +
875 			    NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX)
876 				return (ERANGE);
877 
878 			fxprmsp->fx_tqntm = (int)ticks;
879 		} else {
880 			if (nsecs != FX_TQINF && nsecs != FX_TQDEF)
881 				return (EINVAL);
882 			fxprmsp->fx_tqntm = nsecs;
883 		}
884 	}
885 
886 	return (0);
887 }
888 
889 
890 /*
891  * Nothing to do here but return success.
892  */
893 /* ARGSUSED */
894 static int
895 fx_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
896 {
897 	register fxkparms_t	*fxkprmsp = (fxkparms_t *)parmsp;
898 
899 	if (vaparmsp != NULL)
900 		return (0);
901 
902 	if (fxkprmsp->fx_tqntm < 0) {
903 		/*
904 		 * Quantum field set to special value (e.g. FX_TQINF)
905 		 */
906 		((fxparms_t *)fxkprmsp)->fx_tqnsecs = fxkprmsp->fx_tqntm;
907 		((fxparms_t *)fxkprmsp)->fx_tqsecs = 0;
908 
909 	} else {
910 		/* Convert quantum from ticks to seconds-nanoseconds */
911 
912 		timestruc_t ts;
913 		TICK_TO_TIMESTRUC(fxkprmsp->fx_tqntm, &ts);
914 		((fxparms_t *)fxkprmsp)->fx_tqsecs = ts.tv_sec;
915 		((fxparms_t *)fxkprmsp)->fx_tqnsecs = ts.tv_nsec;
916 	}
917 
918 	return (0);
919 }
920 
921 
922 /*
923  * Copy all selected fixed-priority class parameters to the user.
924  * The parameters are specified by a key.
925  */
926 static int
927 fx_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
928 {
929 	fxkparms_t	*fxkprmsp = (fxkparms_t *)prmsp;
930 	timestruc_t	ts;
931 	uint_t		cnt;
932 	uint_t		secs;
933 	int		nsecs;
934 	int		priflag, secflag, nsecflag, limflag;
935 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
936 
937 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
938 
939 	priflag = secflag = nsecflag = limflag = 0;
940 
941 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
942 		return (EINVAL);
943 
944 	if (fxkprmsp->fx_tqntm < 0) {
945 		/*
946 		 * Quantum field set to special value (e.g. FX_TQINF).
947 		 */
948 		secs = 0;
949 		nsecs = fxkprmsp->fx_tqntm;
950 	} else {
951 		/*
952 		 * Convert quantum from ticks to seconds-nanoseconds.
953 		 */
954 		TICK_TO_TIMESTRUC(fxkprmsp->fx_tqntm, &ts);
955 		secs = ts.tv_sec;
956 		nsecs = ts.tv_nsec;
957 	}
958 
959 
960 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
961 
962 		switch (vpp->pc_key) {
963 		case FX_KY_UPRILIM:
964 			if (limflag++)
965 				return (EINVAL);
966 			if (copyout(&fxkprmsp->fx_uprilim,
967 			    (void *)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
968 				return (EFAULT);
969 			break;
970 
971 		case FX_KY_UPRI:
972 			if (priflag++)
973 				return (EINVAL);
974 			if (copyout(&fxkprmsp->fx_upri,
975 			    (void *)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
976 				return (EFAULT);
977 			break;
978 
979 		case FX_KY_TQSECS:
980 			if (secflag++)
981 				return (EINVAL);
982 			if (copyout(&secs,
983 			    (void *)(uintptr_t)vpp->pc_parm, sizeof (uint_t)))
984 				return (EFAULT);
985 			break;
986 
987 		case FX_KY_TQNSECS:
988 			if (nsecflag++)
989 				return (EINVAL);
990 			if (copyout(&nsecs,
991 			    (void *)(uintptr_t)vpp->pc_parm, sizeof (int)))
992 				return (EFAULT);
993 			break;
994 
995 		default:
996 			return (EINVAL);
997 		}
998 	}
999 
1000 	return (0);
1001 }
1002 
1003 /*
1004  * Set the scheduling parameters of the thread pointed to by fxprocp
1005  * to those specified in the buffer pointed to by fxparmsp.
1006  */
1007 /* ARGSUSED */
1008 static int
1009 fx_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1010 {
1011 	char		nice;
1012 	pri_t		reqfxuprilim;
1013 	pri_t		reqfxupri;
1014 	fxkparms_t	*fxkparmsp = (fxkparms_t *)parmsp;
1015 	fxproc_t	*fxpp;
1016 
1017 
1018 	ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
1019 
1020 	thread_lock(tx);
1021 	fxpp = (fxproc_t *)tx->t_cldata;
1022 
1023 	if ((fxkparmsp->fx_cflags & FX_DOUPRILIM) == 0)
1024 		reqfxuprilim = fxpp->fx_uprilim;
1025 	else
1026 		reqfxuprilim = fxkparmsp->fx_uprilim;
1027 
1028 	/*
1029 	 * Basic permissions enforced by generic kernel code
1030 	 * for all classes require that a thread attempting
1031 	 * to change the scheduling parameters of a target
1032 	 * thread be privileged or have a real or effective
1033 	 * UID matching that of the target thread. We are not
1034 	 * called unless these basic permission checks have
1035 	 * already passed. The fixed priority class requires in
1036 	 * addition that the calling thread be privileged if it
1037 	 * is attempting to raise the pri above its current
1038 	 * value This may have been checked previously but if our
1039 	 * caller passed us a non-NULL credential pointer we assume
1040 	 * it hasn't and we check it here.
1041 	 */
1042 
1043 	if ((reqpcredp != NULL) &&
1044 	    (reqfxuprilim > fxpp->fx_uprilim ||
1045 	    ((fxkparmsp->fx_cflags & FX_DOTQ) != 0)) &&
1046 	    secpolicy_setpriority(reqpcredp) != 0) {
1047 		thread_unlock(tx);
1048 		return (EPERM);
1049 	}
1050 
1051 	FX_ADJUST_PRI(reqfxuprilim);
1052 
1053 	if ((fxkparmsp->fx_cflags & FX_DOUPRI) == 0)
1054 		reqfxupri = fxpp->fx_pri;
1055 	else
1056 		reqfxupri = fxkparmsp->fx_upri;
1057 
1058 
1059 	/*
1060 	 * Make sure the user priority doesn't exceed the upri limit.
1061 	 */
1062 	if (reqfxupri > reqfxuprilim)
1063 		reqfxupri = reqfxuprilim;
1064 
1065 	/*
1066 	 * Set fx_nice to the nice value corresponding to the user
1067 	 * priority we are setting.  Note that setting the nice field
1068 	 * of the parameter struct won't affect upri or nice.
1069 	 */
1070 
1071 	nice = NZERO - (reqfxupri * NZERO) / fx_maxupri;
1072 
1073 	if (nice > NZERO)
1074 		nice = NZERO;
1075 
1076 	fxpp->fx_uprilim = reqfxuprilim;
1077 	fxpp->fx_pri = reqfxupri;
1078 
1079 	if (fxkparmsp->fx_tqntm == FX_TQINF)
1080 		fxpp->fx_pquantum = FX_TQINF;
1081 	else if (fxkparmsp->fx_tqntm == FX_TQDEF)
1082 		fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
1083 	else if ((fxkparmsp->fx_cflags & FX_DOTQ) != 0)
1084 		fxpp->fx_pquantum = fxkparmsp->fx_tqntm;
1085 
1086 	fxpp->fx_nice = nice;
1087 
1088 	fx_change_priority(tx, fxpp);
1089 	thread_unlock(tx);
1090 	return (0);
1091 }
1092 
1093 
1094 /*
1095  * Return the global scheduling priority that would be assigned
1096  * to a thread entering the fixed-priority class with the fx_upri.
1097  */
1098 static pri_t
1099 fx_globpri(kthread_t *t)
1100 {
1101 	fxproc_t *fxpp;
1102 
1103 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1104 
1105 	fxpp = (fxproc_t *)t->t_cldata;
1106 	return (fx_dptbl[fxpp->fx_pri].fx_globpri);
1107 
1108 }
1109 
1110 /*
1111  * Arrange for thread to be placed in appropriate location
1112  * on dispatcher queue.
1113  *
1114  * This is called with the current thread in TS_ONPROC and locked.
1115  */
1116 static void
1117 fx_preempt(kthread_t *t)
1118 {
1119 	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
1120 
1121 	ASSERT(t == curthread);
1122 	ASSERT(THREAD_LOCK_HELD(curthread));
1123 
1124 	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ENFORCE);
1125 
1126 	/*
1127 	 * Check to see if we're doing "preemption control" here.  If
1128 	 * we are, and if the user has requested that this thread not
1129 	 * be preempted, and if preemptions haven't been put off for
1130 	 * too long, let the preemption happen here but try to make
1131 	 * sure the thread is rescheduled as soon as possible.  We do
1132 	 * this by putting it on the front of the highest priority run
1133 	 * queue in the FX class.  If the preemption has been put off
1134 	 * for too long, clear the "nopreempt" bit and let the thread
1135 	 * be preempted.
1136 	 */
1137 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1138 		if (fxpp->fx_pquantum == FX_TQINF ||
1139 		    fxpp->fx_timeleft > -SC_MAX_TICKS) {
1140 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
1141 			schedctl_set_yield(t, 1);
1142 			setfrontdq(t);
1143 			return;
1144 		} else {
1145 			schedctl_set_nopreempt(t, 0);
1146 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
1147 			TNF_PROBE_2(schedctl_preempt, "schedctl FX fx_preempt",
1148 			    /* CSTYLED */, tnf_pid, pid, ttoproc(t)->p_pid,
1149 			    tnf_lwpid, lwpid, t->t_tid);
1150 			/*
1151 			 * Fall through and be preempted below.
1152 			 */
1153 		}
1154 	}
1155 
1156 	if (FX_HAS_CB(fxpp)) {
1157 		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
1158 		pri_t	newpri = fxpp->fx_pri;
1159 		FX_CB_PREEMPT(FX_CALLB(fxpp), fxpp->fx_cookie,
1160 		    &new_quantum, &newpri);
1161 		FX_ADJUST_QUANTUM(new_quantum);
1162 		if ((int)new_quantum != fxpp->fx_pquantum) {
1163 			fxpp->fx_pquantum = (int)new_quantum;
1164 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1165 		}
1166 		FX_ADJUST_PRI(newpri);
1167 		fxpp->fx_pri = newpri;
1168 		THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
1169 	}
1170 
1171 	/*
1172 	 * This thread may be placed on wait queue by CPU Caps. In this case we
1173 	 * do not need to do anything until it is removed from the wait queue.
1174 	 */
1175 	if (CPUCAPS_ENFORCE(t)) {
1176 		return;
1177 	}
1178 
1179 	if ((fxpp->fx_flags & (FXBACKQ)) == FXBACKQ) {
1180 		fxpp->fx_timeleft = fxpp->fx_pquantum;
1181 		fxpp->fx_flags &= ~FXBACKQ;
1182 		setbackdq(t);
1183 	} else {
1184 		setfrontdq(t);
1185 	}
1186 }
1187 
1188 static void
1189 fx_setrun(kthread_t *t)
1190 {
1191 	fxproc_t *fxpp = (fxproc_t *)(t->t_cldata);
1192 
1193 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
1194 	fxpp->fx_flags &= ~FXBACKQ;
1195 
1196 	if (t->t_disp_time != ddi_get_lbolt())
1197 		setbackdq(t);
1198 	else
1199 		setfrontdq(t);
1200 }
1201 
1202 
1203 /*
1204  * Prepare thread for sleep. We reset the thread priority so it will
1205  * run at the kernel priority level when it wakes up.
1206  */
1207 static void
1208 fx_sleep(kthread_t *t)
1209 {
1210 	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
1211 
1212 	ASSERT(t == curthread);
1213 	ASSERT(THREAD_LOCK_HELD(t));
1214 
1215 	/*
1216 	 * Account for time spent on CPU before going to sleep.
1217 	 */
1218 	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ENFORCE);
1219 
1220 	if (FX_HAS_CB(fxpp)) {
1221 		FX_CB_SLEEP(FX_CALLB(fxpp), fxpp->fx_cookie);
1222 	}
1223 	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
1224 }
1225 
1226 
1227 /*
1228  * Return Values:
1229  *
1230  *	-1 if the thread is loaded or is not eligible to be swapped in.
1231  *
1232  * FX and RT threads are designed so that they don't swapout; however,
1233  * it is possible that while the thread is swapped out and in another class, it
1234  * can be changed to FX or RT.  Since these threads should be swapped in
1235  * as soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
1236  * returns SHRT_MAX - 1, so that it gives deference to any swapped out
1237  * RT threads.
1238  */
1239 /* ARGSUSED */
1240 static pri_t
1241 fx_swapin(kthread_t *t, int flags)
1242 {
1243 	pri_t	tpri = -1;
1244 
1245 	ASSERT(THREAD_LOCK_HELD(t));
1246 
1247 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
1248 		tpri = (pri_t)SHRT_MAX - 1;
1249 	}
1250 
1251 	return (tpri);
1252 }
1253 
1254 /*
1255  * Return Values
1256  *	-1 if the thread isn't loaded or is not eligible to be swapped out.
1257  */
1258 /* ARGSUSED */
1259 static pri_t
1260 fx_swapout(kthread_t *t, int flags)
1261 {
1262 	ASSERT(THREAD_LOCK_HELD(t));
1263 
1264 	return (-1);
1265 
1266 }
1267 
1268 /* ARGSUSED */
1269 static void
1270 fx_stop(kthread_t *t, int why, int what)
1271 {
1272 	fxproc_t *fxpp = (fxproc_t *)(t->t_cldata);
1273 
1274 	ASSERT(THREAD_LOCK_HELD(t));
1275 
1276 	if (FX_HAS_CB(fxpp)) {
1277 		FX_CB_STOP(FX_CALLB(fxpp), fxpp->fx_cookie);
1278 	}
1279 }
1280 
1281 /*
1282  * Check for time slice expiration.  If time slice has expired
1283  * set runrun to cause preemption.
1284  */
1285 static void
1286 fx_tick(kthread_t *t)
1287 {
1288 	boolean_t call_cpu_surrender = B_FALSE;
1289 	fxproc_t *fxpp;
1290 
1291 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1292 
1293 	thread_lock(t);
1294 
1295 	fxpp = (fxproc_t *)(t->t_cldata);
1296 
1297 	if (FX_HAS_CB(fxpp)) {
1298 		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
1299 		pri_t	newpri = fxpp->fx_pri;
1300 		FX_CB_TICK(FX_CALLB(fxpp), fxpp->fx_cookie,
1301 		    &new_quantum, &newpri);
1302 		FX_ADJUST_QUANTUM(new_quantum);
1303 		if ((int)new_quantum != fxpp->fx_pquantum) {
1304 			fxpp->fx_pquantum = (int)new_quantum;
1305 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1306 		}
1307 		FX_ADJUST_PRI(newpri);
1308 		if (newpri != fxpp->fx_pri) {
1309 			fxpp->fx_pri = newpri;
1310 			fx_change_priority(t, fxpp);
1311 		}
1312 	}
1313 
1314 	/*
1315 	 * Keep track of thread's project CPU usage.  Note that projects
1316 	 * get charged even when threads are running in the kernel.
1317 	 */
1318 	call_cpu_surrender =  CPUCAPS_CHARGE(t, &fxpp->fx_caps,
1319 	    CPUCAPS_CHARGE_ENFORCE);
1320 
1321 	if ((fxpp->fx_pquantum != FX_TQINF) &&
1322 	    (--fxpp->fx_timeleft <= 0)) {
1323 		pri_t	new_pri;
1324 
1325 		/*
1326 		 * If we're doing preemption control and trying to
1327 		 * avoid preempting this thread, just note that
1328 		 * the thread should yield soon and let it keep
1329 		 * running (unless it's been a while).
1330 		 */
1331 		if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1332 			if (fxpp->fx_timeleft > -SC_MAX_TICKS) {
1333 				DTRACE_SCHED1(schedctl__nopreempt,
1334 				    kthread_t *, t);
1335 				schedctl_set_yield(t, 1);
1336 				thread_unlock_nopreempt(t);
1337 				return;
1338 			}
1339 			TNF_PROBE_2(schedctl_failsafe,
1340 			    "schedctl FX fx_tick", /* CSTYLED */,
1341 			    tnf_pid, pid, ttoproc(t)->p_pid,
1342 			    tnf_lwpid, lwpid, t->t_tid);
1343 		}
1344 		new_pri = fx_dptbl[fxpp->fx_pri].fx_globpri;
1345 		ASSERT(new_pri >= 0 && new_pri <= fx_maxglobpri);
1346 		/*
1347 		 * When the priority of a thread is changed,
1348 		 * it may be necessary to adjust its position
1349 		 * on a sleep queue or dispatch queue. Even
1350 		 * when the priority is not changed, we need
1351 		 * to preserve round robin on dispatch queue.
1352 		 * The function thread_change_pri accomplishes
1353 		 * this.
1354 		 */
1355 		if (thread_change_pri(t, new_pri, 0)) {
1356 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1357 		} else {
1358 			call_cpu_surrender = B_TRUE;
1359 		}
1360 	} else if (t->t_state == TS_ONPROC &&
1361 	    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
1362 		call_cpu_surrender = B_TRUE;
1363 	}
1364 
1365 	if (call_cpu_surrender) {
1366 		fxpp->fx_flags |= FXBACKQ;
1367 		cpu_surrender(t);
1368 	}
1369 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
1370 }
1371 
1372 
1373 static void
1374 fx_trapret(kthread_t *t)
1375 {
1376 	cpu_t		*cp = CPU;
1377 
1378 	ASSERT(THREAD_LOCK_HELD(t));
1379 	ASSERT(t == curthread);
1380 	ASSERT(cp->cpu_dispthread == t);
1381 	ASSERT(t->t_state == TS_ONPROC);
1382 }
1383 
1384 
1385 /*
1386  * Processes waking up go to the back of their queue.
1387  */
1388 static void
1389 fx_wakeup(kthread_t *t)
1390 {
1391 	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
1392 
1393 	ASSERT(THREAD_LOCK_HELD(t));
1394 
1395 	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
1396 	if (FX_HAS_CB(fxpp)) {
1397 		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
1398 		pri_t	newpri = fxpp->fx_pri;
1399 		FX_CB_WAKEUP(FX_CALLB(fxpp), fxpp->fx_cookie,
1400 		    &new_quantum, &newpri);
1401 		FX_ADJUST_QUANTUM(new_quantum);
1402 		if ((int)new_quantum != fxpp->fx_pquantum) {
1403 			fxpp->fx_pquantum = (int)new_quantum;
1404 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1405 		}
1406 
1407 		FX_ADJUST_PRI(newpri);
1408 		if (newpri != fxpp->fx_pri) {
1409 			fxpp->fx_pri = newpri;
1410 			THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
1411 		}
1412 	}
1413 
1414 	fxpp->fx_flags &= ~FXBACKQ;
1415 
1416 	if (t->t_disp_time != ddi_get_lbolt())
1417 		setbackdq(t);
1418 	else
1419 		setfrontdq(t);
1420 }
1421 
1422 
1423 /*
1424  * When a thread yields, put it on the back of the run queue.
1425  */
1426 static void
1427 fx_yield(kthread_t *t)
1428 {
1429 	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
1430 
1431 	ASSERT(t == curthread);
1432 	ASSERT(THREAD_LOCK_HELD(t));
1433 
1434 	/*
1435 	 * Collect CPU usage spent before yielding CPU.
1436 	 */
1437 	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ENFORCE);
1438 
1439 	if (FX_HAS_CB(fxpp))  {
1440 		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
1441 		pri_t	newpri = fxpp->fx_pri;
1442 		FX_CB_PREEMPT(FX_CALLB(fxpp), fxpp->fx_cookie,
1443 		    &new_quantum, &newpri);
1444 		FX_ADJUST_QUANTUM(new_quantum);
1445 		if ((int)new_quantum != fxpp->fx_pquantum) {
1446 			fxpp->fx_pquantum = (int)new_quantum;
1447 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1448 		}
1449 		FX_ADJUST_PRI(newpri);
1450 		fxpp->fx_pri = newpri;
1451 		THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
1452 	}
1453 
1454 	/*
1455 	 * Clear the preemption control "yield" bit since the user is
1456 	 * doing a yield.
1457 	 */
1458 	if (t->t_schedctl)
1459 		schedctl_set_yield(t, 0);
1460 
1461 	if (fxpp->fx_timeleft <= 0) {
1462 		/*
1463 		 * Time slice was artificially extended to avoid
1464 		 * preemption, so pretend we're preempting it now.
1465 		 */
1466 		DTRACE_SCHED1(schedctl__yield, int, -fxpp->fx_timeleft);
1467 		fxpp->fx_timeleft = fxpp->fx_pquantum;
1468 		THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
1469 		ASSERT(t->t_pri >= 0 && t->t_pri <= fx_maxglobpri);
1470 	}
1471 
1472 	fxpp->fx_flags &= ~FXBACKQ;
1473 	setbackdq(t);
1474 }
1475 
1476 /*
1477  * Increment the nice value of the specified thread by incr and
1478  * return the new value in *retvalp.
1479  */
1480 static int
1481 fx_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1482 {
1483 	int		newnice;
1484 	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
1485 	fxkparms_t	fxkparms;
1486 
1487 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1488 
1489 	/* If there's no change to priority, just return current setting */
1490 	if (incr == 0) {
1491 		if (retvalp) {
1492 			*retvalp = fxpp->fx_nice - NZERO;
1493 		}
1494 		return (0);
1495 	}
1496 
1497 	if ((incr < 0 || incr > 2 * NZERO) &&
1498 	    secpolicy_setpriority(cr) != 0)
1499 		return (EPERM);
1500 
1501 	/*
1502 	 * Specifying a nice increment greater than the upper limit of
1503 	 * 2 * NZERO - 1 will result in the thread's nice value being
1504 	 * set to the upper limit.  We check for this before computing
1505 	 * the new value because otherwise we could get overflow
1506 	 * if a privileged user specified some ridiculous increment.
1507 	 */
1508 	if (incr > 2 * NZERO - 1)
1509 		incr = 2 * NZERO - 1;
1510 
1511 	newnice = fxpp->fx_nice + incr;
1512 	if (newnice > NZERO)
1513 		newnice = NZERO;
1514 	else if (newnice < 0)
1515 		newnice = 0;
1516 
1517 	fxkparms.fx_uprilim = fxkparms.fx_upri =
1518 	    -((newnice - NZERO) * fx_maxupri) / NZERO;
1519 
1520 	fxkparms.fx_cflags = FX_DOUPRILIM | FX_DOUPRI;
1521 
1522 	fxkparms.fx_tqntm = FX_TQDEF;
1523 
1524 	/*
1525 	 * Reset the uprilim and upri values of the thread. Adjust
1526 	 * time quantum accordingly.
1527 	 */
1528 
1529 	(void) fx_parmsset(t, (void *)&fxkparms, (id_t)0, (cred_t *)NULL);
1530 
1531 	/*
1532 	 * Although fx_parmsset already reset fx_nice it may
1533 	 * not have been set to precisely the value calculated above
1534 	 * because fx_parmsset determines the nice value from the
1535 	 * user priority and we may have truncated during the integer
1536 	 * conversion from nice value to user priority and back.
1537 	 * We reset fx_nice to the value we calculated above.
1538 	 */
1539 	fxpp->fx_nice = (char)newnice;
1540 
1541 	if (retvalp)
1542 		*retvalp = newnice - NZERO;
1543 
1544 	return (0);
1545 }
1546 
1547 /*
1548  * Increment the priority of the specified thread by incr and
1549  * return the new value in *retvalp.
1550  */
1551 static int
1552 fx_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1553 {
1554 	int		newpri;
1555 	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
1556 	fxkparms_t	fxkparms;
1557 
1558 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1559 
1560 	/* If there's no change to priority, just return current setting */
1561 	if (incr == 0) {
1562 		*retvalp = fxpp->fx_pri;
1563 		return (0);
1564 	}
1565 
1566 	newpri = fxpp->fx_pri + incr;
1567 	if (newpri > fx_maxupri || newpri < 0)
1568 		return (EINVAL);
1569 
1570 	*retvalp = newpri;
1571 	fxkparms.fx_uprilim = fxkparms.fx_upri = newpri;
1572 	fxkparms.fx_tqntm = FX_NOCHANGE;
1573 	fxkparms.fx_cflags = FX_DOUPRILIM | FX_DOUPRI;
1574 
1575 	/*
1576 	 * Reset the uprilim and upri values of the thread.
1577 	 */
1578 	return (fx_parmsset(t, (void *)&fxkparms, (id_t)0, cr));
1579 }
1580 
1581 static void
1582 fx_change_priority(kthread_t *t, fxproc_t *fxpp)
1583 {
1584 	pri_t	new_pri;
1585 
1586 	ASSERT(THREAD_LOCK_HELD(t));
1587 	new_pri = fx_dptbl[fxpp->fx_pri].fx_globpri;
1588 	ASSERT(new_pri >= 0 && new_pri <= fx_maxglobpri);
1589 	t->t_cpri = fxpp->fx_pri;
1590 	if (t == curthread || t->t_state == TS_ONPROC) {
1591 		/* curthread is always onproc */
1592 		cpu_t	*cp = t->t_disp_queue->disp_cpu;
1593 		THREAD_CHANGE_PRI(t, new_pri);
1594 		if (t == cp->cpu_dispthread)
1595 			cp->cpu_dispatch_pri = DISP_PRIO(t);
1596 		if (DISP_MUST_SURRENDER(t)) {
1597 			fxpp->fx_flags |= FXBACKQ;
1598 			cpu_surrender(t);
1599 		} else {
1600 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1601 		}
1602 	} else {
1603 		/*
1604 		 * When the priority of a thread is changed,
1605 		 * it may be necessary to adjust its position
1606 		 * on a sleep queue or dispatch queue.
1607 		 * The function thread_change_pri accomplishes
1608 		 * this.
1609 		 */
1610 		if (thread_change_pri(t, new_pri, 0)) {
1611 			/*
1612 			 * The thread was on a run queue. Reset
1613 			 * its CPU timeleft from the quantum
1614 			 * associated with the new priority.
1615 			 */
1616 			fxpp->fx_timeleft = fxpp->fx_pquantum;
1617 		} else {
1618 			fxpp->fx_flags |= FXBACKQ;
1619 		}
1620 	}
1621 }
1622 
1623 static int
1624 fx_alloc(void **p, int flag)
1625 {
1626 	void *bufp;
1627 
1628 	bufp = kmem_alloc(sizeof (fxproc_t), flag);
1629 	if (bufp == NULL) {
1630 		return (ENOMEM);
1631 	} else {
1632 		*p = bufp;
1633 		return (0);
1634 	}
1635 }
1636 
1637 static void
1638 fx_free(void *bufp)
1639 {
1640 	if (bufp)
1641 		kmem_free(bufp, sizeof (fxproc_t));
1642 }
1643 
1644 /*
1645  * Release the callback list mutex after successful lookup
1646  */
1647 void
1648 fx_list_release(fxproc_t *fxpp)
1649 {
1650 	int index = FX_CB_LIST_HASH(fxpp->fx_ktid);
1651 	kmutex_t *lockp = &fx_cb_list_lock[index];
1652 	mutex_exit(lockp);
1653 }
1654 
1655 fxproc_t *
1656 fx_list_lookup(kt_did_t ktid)
1657 {
1658 	int index = FX_CB_LIST_HASH(ktid);
1659 	kmutex_t *lockp = &fx_cb_list_lock[index];
1660 	fxproc_t *fxpp;
1661 
1662 	mutex_enter(lockp);
1663 
1664 	for (fxpp = fx_cb_plisthead[index].fx_cb_next;
1665 	    fxpp != &fx_cb_plisthead[index]; fxpp = fxpp->fx_cb_next) {
1666 		if (fxpp->fx_tp->t_cid == fx_cid && fxpp->fx_ktid == ktid &&
1667 		    fxpp->fx_callback != NULL) {
1668 			/*
1669 			 * The caller is responsible for calling
1670 			 * fx_list_release to drop the lock upon
1671 			 * successful lookup
1672 			 */
1673 			return (fxpp);
1674 		}
1675 	}
1676 	mutex_exit(lockp);
1677 	return ((fxproc_t *)NULL);
1678 }
1679 
1680 
1681 /*
1682  * register a callback set of routines for current thread
1683  * thread should already be in FX class
1684  */
1685 int
1686 fx_register_callbacks(fx_callbacks_t *fx_callback, fx_cookie_t cookie,
1687 	pri_t pri, clock_t quantum)
1688 {
1689 
1690 	fxproc_t	*fxpp;
1691 
1692 	if (fx_callback == NULL)
1693 		return (EINVAL);
1694 
1695 	if (secpolicy_dispadm(CRED()) != 0)
1696 		return (EPERM);
1697 
1698 	if (FX_CB_VERSION(fx_callback) != FX_CALLB_REV)
1699 		return (EINVAL);
1700 
1701 	if (!FX_ISVALID(pri, quantum))
1702 		return (EINVAL);
1703 
1704 	thread_lock(curthread);		/* get dispatcher lock on thread */
1705 
1706 	if (curthread->t_cid != fx_cid) {
1707 		thread_unlock(curthread);
1708 		return (EINVAL);
1709 	}
1710 
1711 	fxpp = (fxproc_t *)(curthread->t_cldata);
1712 	ASSERT(fxpp != NULL);
1713 	if (FX_HAS_CB(fxpp)) {
1714 		thread_unlock(curthread);
1715 		return (EINVAL);
1716 	}
1717 
1718 	fxpp->fx_callback = fx_callback;
1719 	fxpp->fx_cookie = cookie;
1720 
1721 	if (pri != FX_CB_NOCHANGE) {
1722 		fxpp->fx_pri = pri;
1723 		FX_ADJUST_PRI(fxpp->fx_pri);
1724 		if (quantum == FX_TQDEF) {
1725 			fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
1726 		} else if (quantum == FX_TQINF) {
1727 			fxpp->fx_pquantum = FX_TQINF;
1728 		} else if (quantum != FX_NOCHANGE) {
1729 			FX_ADJUST_QUANTUM(quantum);
1730 			fxpp->fx_pquantum = quantum;
1731 		}
1732 	} else if (quantum != FX_NOCHANGE && quantum != FX_TQDEF) {
1733 		if (quantum == FX_TQINF)
1734 			fxpp->fx_pquantum = FX_TQINF;
1735 		else {
1736 			FX_ADJUST_QUANTUM(quantum);
1737 			fxpp->fx_pquantum = quantum;
1738 		}
1739 	}
1740 
1741 	fxpp->fx_ktid = ddi_get_kt_did();
1742 
1743 	fx_change_priority(curthread, fxpp);
1744 
1745 	thread_unlock(curthread);
1746 
1747 	/*
1748 	 * Link new structure into fxproc list.
1749 	 */
1750 	FX_CB_LIST_INSERT(fxpp);
1751 	return (0);
1752 }
1753 
1754 /* unregister a callback set of routines for current thread */
1755 int
1756 fx_unregister_callbacks()
1757 {
1758 	fxproc_t	*fxpp;
1759 
1760 	if ((fxpp = fx_list_lookup(ddi_get_kt_did())) == NULL) {
1761 		/*
1762 		 * did not have a registered callback;
1763 		 */
1764 		return (EINVAL);
1765 	}
1766 
1767 	thread_lock(fxpp->fx_tp);
1768 	fxpp->fx_callback = NULL;
1769 	fxpp->fx_cookie = NULL;
1770 	thread_unlock(fxpp->fx_tp);
1771 	fx_list_release(fxpp);
1772 
1773 	FX_CB_LIST_DELETE(fxpp);
1774 	return (0);
1775 }
1776 
1777 /*
1778  * modify priority and/or quantum value of a thread with callback
1779  */
1780 int
1781 fx_modify_priority(kt_did_t ktid, clock_t quantum, pri_t pri)
1782 {
1783 	fxproc_t	*fxpp;
1784 
1785 	if (!FX_ISVALID(pri, quantum))
1786 		return (EINVAL);
1787 
1788 	if ((fxpp = fx_list_lookup(ktid)) == NULL) {
1789 		/*
1790 		 * either thread had exited or did not have a registered
1791 		 * callback;
1792 		 */
1793 		return (ESRCH);
1794 	}
1795 
1796 	thread_lock(fxpp->fx_tp);
1797 
1798 	if (pri != FX_CB_NOCHANGE) {
1799 		fxpp->fx_pri = pri;
1800 		FX_ADJUST_PRI(fxpp->fx_pri);
1801 		if (quantum == FX_TQDEF) {
1802 			fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
1803 		} else if (quantum == FX_TQINF) {
1804 			fxpp->fx_pquantum = FX_TQINF;
1805 		} else if (quantum != FX_NOCHANGE) {
1806 			FX_ADJUST_QUANTUM(quantum);
1807 			fxpp->fx_pquantum = quantum;
1808 		}
1809 	} else if (quantum != FX_NOCHANGE && quantum != FX_TQDEF) {
1810 		if (quantum == FX_TQINF) {
1811 			fxpp->fx_pquantum = FX_TQINF;
1812 		} else {
1813 			FX_ADJUST_QUANTUM(quantum);
1814 			fxpp->fx_pquantum = quantum;
1815 		}
1816 	}
1817 
1818 	fx_change_priority(fxpp->fx_tp, fxpp);
1819 
1820 	thread_unlock(fxpp->fx_tp);
1821 	fx_list_release(fxpp);
1822 	return (0);
1823 }
1824 
1825 
1826 /*
1827  * return an iblock cookie for mutex initialization to be used in callbacks
1828  */
1829 void *
1830 fx_get_mutex_cookie()
1831 {
1832 	return ((void *)(uintptr_t)__ipltospl(DISP_LEVEL));
1833 }
1834 
1835 /*
1836  * return maximum relative priority
1837  */
1838 pri_t
1839 fx_get_maxpri()
1840 {
1841 	return (fx_maxumdpri);
1842 }
1843