summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/disp/fx.c
blob: 8260680a07aae280e0ebca74f2cf42becce51fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2015, Joyent, Inc.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/session.h>
#include <sys/strsubr.h>
#include <sys/user.h>
#include <sys/priocntl.h>
#include <sys/class.h>
#include <sys/disp.h>
#include <sys/procset.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/fx.h>
#include <sys/fxpriocntl.h>
#include <sys/cpuvar.h>
#include <sys/systm.h>
#include <sys/vtrace.h>
#include <sys/schedctl.h>
#include <sys/tnf_probe.h>
#include <sys/sunddi.h>
#include <sys/spl.h>
#include <sys/modctl.h>
#include <sys/policy.h>
#include <sys/sdt.h>
#include <sys/cpupart.h>
#include <sys/cpucaps.h>

static pri_t fx_init(id_t, int, classfuncs_t **);

static struct sclass csw = {
	"FX",
	fx_init,
	0
};

static struct modlsched modlsched = {
	&mod_schedops, "Fixed priority sched class", &csw
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlsched, NULL
};


#define	FX_MAX_UNPRIV_PRI	0	/* maximum unpriviledge priority */

/*
 * The fxproc_t structures that have a registered callback vector,
 * are also kept in an array of circular doubly linked lists. A hash on
 * the thread id (from ddi_get_kt_did()) is used to determine which list
 * each of such fxproc structures should be placed. Each list has a dummy
 * "head" which is never removed, so the list is never empty.
 */

#define	FX_CB_LISTS 16		/* number of lists, must be power of 2 */
#define	FX_CB_LIST_HASH(ktid)	((uint_t)ktid & (FX_CB_LISTS - 1))

/* Insert fxproc into callback list */
#define	FX_CB_LIST_INSERT(fxpp)						\
{									\
	int index = FX_CB_LIST_HASH(fxpp->fx_ktid);			\
	kmutex_t *lockp = &fx_cb_list_lock[index];			\
	fxproc_t *headp = &fx_cb_plisthead[index];			\
	mutex_enter(lockp);						\
	fxpp->fx_cb_next = headp->fx_cb_next;				\
	fxpp->fx_cb_prev = headp;					\
	headp->fx_cb_next->fx_cb_prev = fxpp;				\
	headp->fx_cb_next = fxpp;					\
	mutex_exit(lockp);						\
}

/*
 * Remove thread from callback list.
 */
#define	FX_CB_LIST_DELETE(fxpp)						\
{									\
	int index = FX_CB_LIST_HASH(fxpp->fx_ktid);			\
	kmutex_t *lockp = &fx_cb_list_lock[index];			\
	mutex_enter(lockp);						\
	fxpp->fx_cb_prev->fx_cb_next = fxpp->fx_cb_next;		\
	fxpp->fx_cb_next->fx_cb_prev = fxpp->fx_cb_prev;		\
	mutex_exit(lockp);						\
}

#define	FX_HAS_CB(fxpp)	(fxpp->fx_callback != NULL)

/* adjust x to be between 0 and fx_maxumdpri */

#define	FX_ADJUST_PRI(pri)						\
{									\
	if (pri < 0)							\
		pri = 0;  						\
	else if (pri > fx_maxumdpri) 					\
		pri = fx_maxumdpri;  					\
}

#define	FX_ADJUST_QUANTUM(q)						\
{									\
	if (q > INT_MAX)						\
		q = INT_MAX;						\
	else if (q <= 0)						\
		q = FX_TQINF;						\
}

#define	FX_ISVALID(pri, quantum) \
	(((pri >= 0) || (pri == FX_CB_NOCHANGE)) &&			\
	    ((quantum >= 0) || (quantum == FX_NOCHANGE) ||		\
		(quantum == FX_TQDEF) || (quantum == FX_TQINF)))


static id_t	fx_cid;		/* fixed priority class ID */
static fxdpent_t *fx_dptbl;	/* fixed priority disp parameter table */

static pri_t	fx_maxupri = FXMAXUPRI;
static pri_t	fx_maxumdpri;	/* max user mode fixed priority */

static pri_t	fx_maxglobpri;	/* maximum global priority used by fx class */
static kmutex_t	fx_dptblock;	/* protects fixed priority dispatch table */


static kmutex_t	fx_cb_list_lock[FX_CB_LISTS];	/* protects list of fxprocs */
						/* that have callbacks */
static fxproc_t	fx_cb_plisthead[FX_CB_LISTS];	/* dummy fxproc at head of */
						/* list of fxprocs with */
						/* callbacks */

static int	fx_admin(caddr_t, cred_t *);
static int	fx_getclinfo(void *);
static int	fx_parmsin(void *);
static int	fx_parmsout(void *, pc_vaparms_t *);
static int	fx_vaparmsin(void *, pc_vaparms_t *);
static int	fx_vaparmsout(void *, pc_vaparms_t *);
static int	fx_getclpri(pcpri_t *);
static int	fx_alloc(void **, int);
static void	fx_free(void *);
static int	fx_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
static void	fx_exitclass(void *);
static int	fx_canexit(kthread_t *, cred_t *);
static int	fx_fork(kthread_t *, kthread_t *, void *);
static void	fx_forkret(kthread_t *, kthread_t *);
static void	fx_parmsget(kthread_t *, void *);
static int	fx_parmsset(kthread_t *, void *, id_t, cred_t *);
static void	fx_stop(kthread_t *, int, int);
static void	fx_exit(kthread_t *);
static pri_t	fx_swapin(kthread_t *, int);
static pri_t	fx_swapout(kthread_t *, int);
static void	fx_trapret(kthread_t *);
static void	fx_preempt(kthread_t *);
static void	fx_setrun(kthread_t *);
static void	fx_sleep(kthread_t *);
static void	fx_tick(kthread_t *);
static void	fx_wakeup(kthread_t *);
static int	fx_donice(kthread_t *, cred_t *, int, int *);
static int	fx_doprio(kthread_t *, cred_t *, int, int *);
static pri_t	fx_globpri(kthread_t *);
static void	fx_yield(kthread_t *);
static void	fx_nullsys();

extern fxdpent_t *fx_getdptbl(void);

static void	fx_change_priority(kthread_t *, fxproc_t *);
static fxproc_t *fx_list_lookup(kt_did_t);
static void fx_list_release(fxproc_t *);


static struct classfuncs fx_classfuncs = {
	/* class functions */
	fx_admin,
	fx_getclinfo,
	fx_parmsin,
	fx_parmsout,
	fx_vaparmsin,
	fx_vaparmsout,
	fx_getclpri,
	fx_alloc,
	fx_free,

	/* thread functions */
	fx_enterclass,
	fx_exitclass,
	fx_canexit,
	fx_fork,
	fx_forkret,
	fx_parmsget,
	fx_parmsset,
	fx_stop,
	fx_exit,
	fx_nullsys,	/* active */
	fx_nullsys,	/* inactive */
	fx_swapin,
	fx_swapout,
	fx_trapret,
	fx_preempt,
	fx_setrun,
	fx_sleep,
	fx_tick,
	fx_wakeup,
	fx_donice,
	fx_globpri,
	fx_nullsys,	/* set_process_group */
	fx_yield,
	fx_doprio,
};


int
_init()
{
	return (mod_install(&modlinkage));
}

int
_fini()
{
	return (EBUSY);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

/*
 * Fixed priority class initialization. Called by dispinit() at boot time.
 * We can ignore the clparmsz argument since we know that the smallest
 * possible parameter buffer is big enough for us.
 */
/* ARGSUSED */
static pri_t
fx_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
{
	int i;
	extern pri_t fx_getmaxumdpri(void);

	fx_dptbl = fx_getdptbl();
	fx_maxumdpri = fx_getmaxumdpri();
	fx_maxglobpri = fx_dptbl[fx_maxumdpri].fx_globpri;

	fx_cid = cid;		/* Record our class ID */

	/*
	 * Initialize the hash table for fxprocs with callbacks
	 */
	for (i = 0; i < FX_CB_LISTS; i++) {
		fx_cb_plisthead[i].fx_cb_next = fx_cb_plisthead[i].fx_cb_prev =
		    &fx_cb_plisthead[i];
	}

	/*
	 * We're required to return a pointer to our classfuncs
	 * structure and the highest global priority value we use.
	 */
	*clfuncspp = &fx_classfuncs;
	return (fx_maxglobpri);
}

/*
 * Get or reset the fx_dptbl values per the user's request.
 */
static int
fx_admin(caddr_t uaddr, cred_t *reqpcredp)
{
	fxadmin_t	fxadmin;
	fxdpent_t	*tmpdpp;
	int		userdpsz;
	int		i;
	size_t		fxdpsz;

	if (get_udatamodel() == DATAMODEL_NATIVE) {
		if (copyin(uaddr, &fxadmin, sizeof (fxadmin_t)))
			return (EFAULT);
	}
#ifdef _SYSCALL32_IMPL
	else {
		/* get fxadmin struct from ILP32 caller */
		fxadmin32_t fxadmin32;
		if (copyin(uaddr, &fxadmin32, sizeof (fxadmin32_t)))
			return (EFAULT);
		fxadmin.fx_dpents =
		    (struct fxdpent *)(uintptr_t)fxadmin32.fx_dpents;
		fxadmin.fx_ndpents = fxadmin32.fx_ndpents;
		fxadmin.fx_cmd = fxadmin32.fx_cmd;
	}
#endif /* _SYSCALL32_IMPL */

	fxdpsz = (fx_maxumdpri + 1) * sizeof (fxdpent_t);

	switch (fxadmin.fx_cmd) {
	case FX_GETDPSIZE:
		fxadmin.fx_ndpents = fx_maxumdpri + 1;

		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyout(&fxadmin, uaddr, sizeof (fxadmin_t)))
				return (EFAULT);
		}
#ifdef _SYSCALL32_IMPL
		else {
			/* return fxadmin struct to ILP32 caller */
			fxadmin32_t fxadmin32;
			fxadmin32.fx_dpents =
			    (caddr32_t)(uintptr_t)fxadmin.fx_dpents;
			fxadmin32.fx_ndpents = fxadmin.fx_ndpents;
			fxadmin32.fx_cmd = fxadmin.fx_cmd;
			if (copyout(&fxadmin32, uaddr, sizeof (fxadmin32_t)))
				return (EFAULT);
		}
#endif /* _SYSCALL32_IMPL */
		break;

	case FX_GETDPTBL:
		userdpsz = MIN(fxadmin.fx_ndpents * sizeof (fxdpent_t),
		    fxdpsz);
		if (copyout(fx_dptbl, fxadmin.fx_dpents, userdpsz))
			return (EFAULT);

		fxadmin.fx_ndpents = userdpsz / sizeof (fxdpent_t);

		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyout(&fxadmin, uaddr, sizeof (fxadmin_t)))
				return (EFAULT);
		}
#ifdef _SYSCALL32_IMPL
		else {
			/* return fxadmin struct to ILP32 callers */
			fxadmin32_t fxadmin32;
			fxadmin32.fx_dpents =
			    (caddr32_t)(uintptr_t)fxadmin.fx_dpents;
			fxadmin32.fx_ndpents = fxadmin.fx_ndpents;
			fxadmin32.fx_cmd = fxadmin.fx_cmd;
			if (copyout(&fxadmin32, uaddr, sizeof (fxadmin32_t)))
				return (EFAULT);
		}
#endif /* _SYSCALL32_IMPL */
		break;

	case FX_SETDPTBL:
		/*
		 * We require that the requesting process has sufficient
		 * privileges. We also require that the table supplied by
		 * the user exactly match the current fx_dptbl in size.
		 */
		if (secpolicy_dispadm(reqpcredp) != 0) {
			return (EPERM);
		}
		if (fxadmin.fx_ndpents * sizeof (fxdpent_t) != fxdpsz) {
			return (EINVAL);
		}

		/*
		 * We read the user supplied table into a temporary buffer
		 * where it is validated before being copied over the
		 * fx_dptbl.
		 */
		tmpdpp = kmem_alloc(fxdpsz, KM_SLEEP);
		if (copyin(fxadmin.fx_dpents, tmpdpp, fxdpsz)) {
			kmem_free(tmpdpp, fxdpsz);
			return (EFAULT);
		}
		for (i = 0; i < fxadmin.fx_ndpents; i++) {

			/*
			 * Validate the user supplied values. All we are doing
			 * here is verifying that the values are within their
			 * allowable ranges and will not panic the system. We
			 * make no attempt to ensure that the resulting
			 * configuration makes sense or results in reasonable
			 * performance.
			 */
			if (tmpdpp[i].fx_quantum <= 0 &&
			    tmpdpp[i].fx_quantum != FX_TQINF) {
				kmem_free(tmpdpp, fxdpsz);
				return (EINVAL);
			}
		}

		/*
		 * Copy the user supplied values over the current fx_dptbl
		 * values. The fx_globpri member is read-only so we don't
		 * overwrite it.
		 */
		mutex_enter(&fx_dptblock);
		for (i = 0; i < fxadmin.fx_ndpents; i++) {
			fx_dptbl[i].fx_quantum = tmpdpp[i].fx_quantum;
		}
		mutex_exit(&fx_dptblock);
		kmem_free(tmpdpp, fxdpsz);
		break;

	default:
		return (EINVAL);
	}
	return (0);
}

/*
 * Allocate a fixed priority class specific thread structure and
 * initialize it with the parameters supplied. Also move the thread
 * to specified priority.
 */
static int
fx_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
    void *bufp)
{
	fxkparms_t	*fxkparmsp = (fxkparms_t *)parmsp;
	fxproc_t	*fxpp;
	pri_t		reqfxupri;
	pri_t		reqfxuprilim;

	fxpp = (fxproc_t *)bufp;
	ASSERT(fxpp != NULL);

	/*
	 * Initialize the fxproc structure.
	 */
	fxpp->fx_flags = 0;
	fxpp->fx_callback = NULL;
	fxpp->fx_cookie = NULL;

	if (fxkparmsp == NULL) {
		/*
		 * Use default values.
		 */
		fxpp->fx_pri = fxpp->fx_uprilim = 0;
		fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
		fxpp->fx_nice =  NZERO;
	} else {
		/*
		 * Use supplied values.
		 */

		if ((fxkparmsp->fx_cflags & FX_DOUPRILIM) == 0) {
			reqfxuprilim = 0;
		} else {
			if (fxkparmsp->fx_uprilim > FX_MAX_UNPRIV_PRI &&
			    secpolicy_setpriority(reqpcredp) != 0)
				return (EPERM);
			reqfxuprilim = fxkparmsp->fx_uprilim;
			FX_ADJUST_PRI(reqfxuprilim);
		}

		if ((fxkparmsp->fx_cflags & FX_DOUPRI) == 0) {
			reqfxupri = reqfxuprilim;
		} else {
			if (fxkparmsp->fx_upri > FX_MAX_UNPRIV_PRI &&
			    secpolicy_setpriority(reqpcredp) != 0)
				return (EPERM);
			/*
			 * Set the user priority to the requested value
			 * or the upri limit, whichever is lower.
			 */
			reqfxupri = fxkparmsp->fx_upri;
			FX_ADJUST_PRI(reqfxupri);

			if (reqfxupri > reqfxuprilim)
				reqfxupri = reqfxuprilim;
		}


		fxpp->fx_uprilim = reqfxuprilim;
		fxpp->fx_pri = reqfxupri;

		fxpp->fx_nice = NZERO - (NZERO * reqfxupri) / fx_maxupri;

		if (((fxkparmsp->fx_cflags & FX_DOTQ) == 0) ||
		    (fxkparmsp->fx_tqntm == FX_TQDEF)) {
			fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
		} else {
			if (secpolicy_setpriority(reqpcredp) != 0)
				return (EPERM);

			if (fxkparmsp->fx_tqntm == FX_TQINF)
				fxpp->fx_pquantum = FX_TQINF;
			else {
				fxpp->fx_pquantum = fxkparmsp->fx_tqntm;
			}
		}

	}

	fxpp->fx_timeleft = fxpp->fx_pquantum;
	cpucaps_sc_init(&fxpp->fx_caps);
	fxpp->fx_tp = t;

	thread_lock(t);			/* get dispatcher lock on thread */
	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
	t->t_cid = cid;
	t->t_cldata = (void *)fxpp;
	t->t_schedflag &= ~TS_RUNQMATCH;
	fx_change_priority(t, fxpp);
	thread_unlock(t);

	return (0);
}

/*
 * The thread is exiting.
 */
static void
fx_exit(kthread_t *t)
{
	fxproc_t *fxpp;

	thread_lock(t);
	fxpp = (fxproc_t *)(t->t_cldata);

	/*
	 * A thread could be exiting in between clock ticks, so we need to
	 * calculate how much CPU time it used since it was charged last time.
	 *
	 * CPU caps are not enforced on exiting processes - it is usually
	 * desirable to exit as soon as possible to free resources.
	 */
	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ONLY);

	if (FX_HAS_CB(fxpp)) {
		FX_CB_EXIT(FX_CALLB(fxpp), fxpp->fx_cookie);
		fxpp->fx_callback = NULL;
		fxpp->fx_cookie = NULL;
		thread_unlock(t);
		FX_CB_LIST_DELETE(fxpp);
		return;
	}

	thread_unlock(t);
}

/*
 * Exiting the class. Free fxproc structure of thread.
 */
static void
fx_exitclass(void *procp)
{
	fxproc_t *fxpp = (fxproc_t *)procp;

	thread_lock(fxpp->fx_tp);
	if (FX_HAS_CB(fxpp)) {

		FX_CB_EXIT(FX_CALLB(fxpp), fxpp->fx_cookie);

		fxpp->fx_callback = NULL;
		fxpp->fx_cookie = NULL;
		thread_unlock(fxpp->fx_tp);
		FX_CB_LIST_DELETE(fxpp);
	} else
		thread_unlock(fxpp->fx_tp);

	kmem_free(fxpp, sizeof (fxproc_t));
}

/* ARGSUSED */
static int
fx_canexit(kthread_t *t, cred_t *cred)
{
	/*
	 * A thread can always leave the FX class
	 */
	return (0);
}

/*
 * Initialize fixed-priority class specific proc structure for a child.
 * callbacks are not inherited upon fork.
 */
static int
fx_fork(kthread_t *t, kthread_t *ct, void *bufp)
{
	fxproc_t	*pfxpp;		/* ptr to parent's fxproc structure */
	fxproc_t	*cfxpp;		/* ptr to child's fxproc structure */

	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

	cfxpp = (fxproc_t *)bufp;
	ASSERT(cfxpp != NULL);
	thread_lock(t);
	pfxpp = (fxproc_t *)t->t_cldata;
	/*
	 * Initialize child's fxproc structure.
	 */
	cfxpp->fx_timeleft = cfxpp->fx_pquantum = pfxpp->fx_pquantum;
	cfxpp->fx_pri = pfxpp->fx_pri;
	cfxpp->fx_uprilim = pfxpp->fx_uprilim;
	cfxpp->fx_nice = pfxpp->fx_nice;
	cfxpp->fx_callback = NULL;
	cfxpp->fx_cookie = NULL;
	cfxpp->fx_flags = pfxpp->fx_flags & ~(FXBACKQ);
	cpucaps_sc_init(&cfxpp->fx_caps);

	cfxpp->fx_tp = ct;
	ct->t_cldata = (void *)cfxpp;
	thread_unlock(t);

	/*
	 * Link new structure into fxproc list.
	 */
	return (0);
}


/*
 * Child is placed at back of dispatcher queue and parent gives
 * up processor so that the child runs first after the fork.
 * This allows the child immediately execing to break the multiple
 * use of copy on write pages with no disk home. The parent will
 * get to steal them back rather than uselessly copying them.
 */
static void
fx_forkret(kthread_t *t, kthread_t *ct)
{
	proc_t	*pp = ttoproc(t);
	proc_t	*cp = ttoproc(ct);
	fxproc_t *fxpp;

	ASSERT(t == curthread);
	ASSERT(MUTEX_HELD(&pidlock));

	/*
	 * Grab the child's p_lock before dropping pidlock to ensure
	 * the process does not disappear before we set it running.
	 */
	mutex_enter(&cp->p_lock);
	continuelwps(cp);
	mutex_exit(&cp->p_lock);

	mutex_enter(&pp->p_lock);
	mutex_exit(&pidlock);
	continuelwps(pp);

	thread_lock(t);
	fxpp = (fxproc_t *)(t->t_cldata);
	t->t_pri = fx_dptbl[fxpp->fx_pri].fx_globpri;
	ASSERT(t->t_pri >= 0 && t->t_pri <= fx_maxglobpri);
	THREAD_TRANSITION(t);
	fx_setrun(t);
	thread_unlock(t);
	/*
	 * Safe to drop p_lock now since it is safe to change
	 * the scheduling class after this point.
	 */
	mutex_exit(&pp->p_lock);

	swtch();
}


/*
 * Get information about the fixed-priority class into the buffer
 * pointed to by fxinfop. The maximum configured user priority
 * is the only information we supply.
 */
static int
fx_getclinfo(void *infop)
{
	fxinfo_t *fxinfop = (fxinfo_t *)infop;
	fxinfop->fx_maxupri = fx_maxupri;
	return (0);
}



/*
 * Return the user mode scheduling priority range.
 */
static int
fx_getclpri(pcpri_t *pcprip)
{
	pcprip->pc_clpmax = fx_maxupri;
	pcprip->pc_clpmin = 0;
	return (0);
}


static void
fx_nullsys()
{}


/*
 * Get the fixed-priority parameters of the thread pointed to by
 * fxprocp into the buffer pointed to by fxparmsp.
 */
static void
fx_parmsget(kthread_t *t, void *parmsp)
{
	fxproc_t *fxpp = (fxproc_t *)t->t_cldata;
	fxkparms_t *fxkparmsp = (fxkparms_t *)parmsp;

	fxkparmsp->fx_upri = fxpp->fx_pri;
	fxkparmsp->fx_uprilim = fxpp->fx_uprilim;
	fxkparmsp->fx_tqntm = fxpp->fx_pquantum;
}



/*
 * Check the validity of the fixed-priority parameters in the buffer
 * pointed to by fxparmsp.
 */
static int
fx_parmsin(void *parmsp)
{
	fxparms_t	*fxparmsp = (fxparms_t *)parmsp;
	uint_t		cflags;
	longlong_t	ticks;
	/*
	 * Check validity of parameters.
	 */

	if ((fxparmsp->fx_uprilim > fx_maxupri ||
	    fxparmsp->fx_uprilim < 0) &&
	    fxparmsp->fx_uprilim != FX_NOCHANGE)
		return (EINVAL);

	if ((fxparmsp->fx_upri > fx_maxupri ||
	    fxparmsp->fx_upri < 0) &&
	    fxparmsp->fx_upri != FX_NOCHANGE)
		return (EINVAL);

	if ((fxparmsp->fx_tqsecs == 0 && fxparmsp->fx_tqnsecs == 0) ||
	    fxparmsp->fx_tqnsecs >= NANOSEC)
		return (EINVAL);

	cflags = (fxparmsp->fx_upri != FX_NOCHANGE ? FX_DOUPRI : 0);

	if (fxparmsp->fx_uprilim != FX_NOCHANGE) {
		cflags |= FX_DOUPRILIM;
	}

	if (fxparmsp->fx_tqnsecs != FX_NOCHANGE)
		cflags |= FX_DOTQ;

	/*
	 * convert the buffer to kernel format.
	 */

	if (fxparmsp->fx_tqnsecs >= 0) {
		if ((ticks = SEC_TO_TICK((longlong_t)fxparmsp->fx_tqsecs) +
		    NSEC_TO_TICK_ROUNDUP(fxparmsp->fx_tqnsecs)) > INT_MAX)
			return (ERANGE);

		((fxkparms_t *)fxparmsp)->fx_tqntm = (int)ticks;
	} else {
		if ((fxparmsp->fx_tqnsecs != FX_NOCHANGE) &&
		    (fxparmsp->fx_tqnsecs != FX_TQINF) &&
		    (fxparmsp->fx_tqnsecs != FX_TQDEF))
			return (EINVAL);
		((fxkparms_t *)fxparmsp)->fx_tqntm = fxparmsp->fx_tqnsecs;
	}

	((fxkparms_t *)fxparmsp)->fx_cflags = cflags;

	return (0);
}


/*
 * Check the validity of the fixed-priority parameters in the pc_vaparms_t
 * structure vaparmsp and put them in the buffer pointed to by fxprmsp.
 * pc_vaparms_t contains (key, value) pairs of parameter.
 */
static int
fx_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp)
{
	uint_t		secs = 0;
	uint_t		cnt;
	int		nsecs = 0;
	int		priflag, secflag, nsecflag, limflag;
	longlong_t	ticks;
	fxkparms_t	*fxprmsp = (fxkparms_t *)prmsp;
	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];


	/*
	 * First check the validity of parameters and convert them
	 * from the user supplied format to the internal format.
	 */
	priflag = secflag = nsecflag = limflag = 0;

	fxprmsp->fx_cflags = 0;

	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
		return (EINVAL);

	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {

		switch (vpp->pc_key) {
		case FX_KY_UPRILIM:
			if (limflag++)
				return (EINVAL);
			fxprmsp->fx_cflags |= FX_DOUPRILIM;
			fxprmsp->fx_uprilim = (pri_t)vpp->pc_parm;
			if (fxprmsp->fx_uprilim > fx_maxupri ||
			    fxprmsp->fx_uprilim < 0)
				return (EINVAL);
			break;

		case FX_KY_UPRI:
			if (priflag++)
				return (EINVAL);
			fxprmsp->fx_cflags |= FX_DOUPRI;
			fxprmsp->fx_upri = (pri_t)vpp->pc_parm;
			if (fxprmsp->fx_upri > fx_maxupri ||
			    fxprmsp->fx_upri < 0)
				return (EINVAL);
			break;

		case FX_KY_TQSECS:
			if (secflag++)
				return (EINVAL);
			fxprmsp->fx_cflags |= FX_DOTQ;
			secs = (uint_t)vpp->pc_parm;
			break;

		case FX_KY_TQNSECS:
			if (nsecflag++)
				return (EINVAL);
			fxprmsp->fx_cflags |= FX_DOTQ;
			nsecs = (int)vpp->pc_parm;
			break;

		default:
			return (EINVAL);
		}
	}

	if (vaparmsp->pc_vaparmscnt == 0) {
		/*
		 * Use default parameters.
		 */
		fxprmsp->fx_upri = 0;
		fxprmsp->fx_uprilim = 0;
		fxprmsp->fx_tqntm = FX_TQDEF;
		fxprmsp->fx_cflags = FX_DOUPRI | FX_DOUPRILIM | FX_DOTQ;
	} else if ((fxprmsp->fx_cflags & FX_DOTQ) != 0) {
		if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC)
			return (EINVAL);

		if (nsecs >= 0) {
			if ((ticks = SEC_TO_TICK((longlong_t)secs) +
			    NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX)
				return (ERANGE);

			fxprmsp->fx_tqntm = (int)ticks;
		} else {
			if (nsecs != FX_TQINF && nsecs != FX_TQDEF)
				return (EINVAL);
			fxprmsp->fx_tqntm = nsecs;
		}
	}

	return (0);
}


/*
 * Nothing to do here but return success.
 */
/* ARGSUSED */
static int
fx_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
{
	register fxkparms_t	*fxkprmsp = (fxkparms_t *)parmsp;

	if (vaparmsp != NULL)
		return (0);

	if (fxkprmsp->fx_tqntm < 0) {
		/*
		 * Quantum field set to special value (e.g. FX_TQINF)
		 */
		((fxparms_t *)fxkprmsp)->fx_tqnsecs = fxkprmsp->fx_tqntm;
		((fxparms_t *)fxkprmsp)->fx_tqsecs = 0;

	} else {
		/* Convert quantum from ticks to seconds-nanoseconds */

		timestruc_t ts;
		TICK_TO_TIMESTRUC(fxkprmsp->fx_tqntm, &ts);
		((fxparms_t *)fxkprmsp)->fx_tqsecs = ts.tv_sec;
		((fxparms_t *)fxkprmsp)->fx_tqnsecs = ts.tv_nsec;
	}

	return (0);
}


/*
 * Copy all selected fixed-priority class parameters to the user.
 * The parameters are specified by a key.
 */
static int
fx_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
{
	fxkparms_t	*fxkprmsp = (fxkparms_t *)prmsp;
	timestruc_t	ts;
	uint_t		cnt;
	uint_t		secs;
	int		nsecs;
	int		priflag, secflag, nsecflag, limflag;
	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];

	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));

	priflag = secflag = nsecflag = limflag = 0;

	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
		return (EINVAL);

	if (fxkprmsp->fx_tqntm < 0) {
		/*
		 * Quantum field set to special value (e.g. FX_TQINF).
		 */
		secs = 0;
		nsecs = fxkprmsp->fx_tqntm;
	} else {
		/*
		 * Convert quantum from ticks to seconds-nanoseconds.
		 */
		TICK_TO_TIMESTRUC(fxkprmsp->fx_tqntm, &ts);
		secs = ts.tv_sec;
		nsecs = ts.tv_nsec;
	}


	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {

		switch (vpp->pc_key) {
		case FX_KY_UPRILIM:
			if (limflag++)
				return (EINVAL);
			if (copyout(&fxkprmsp->fx_uprilim,
			    (void *)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
				return (EFAULT);
			break;

		case FX_KY_UPRI:
			if (priflag++)
				return (EINVAL);
			if (copyout(&fxkprmsp->fx_upri,
			    (void *)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
				return (EFAULT);
			break;

		case FX_KY_TQSECS:
			if (secflag++)
				return (EINVAL);
			if (copyout(&secs,
			    (void *)(uintptr_t)vpp->pc_parm, sizeof (uint_t)))
				return (EFAULT);
			break;

		case FX_KY_TQNSECS:
			if (nsecflag++)
				return (EINVAL);
			if (copyout(&nsecs,
			    (void *)(uintptr_t)vpp->pc_parm, sizeof (int)))
				return (EFAULT);
			break;

		default:
			return (EINVAL);
		}
	}

	return (0);
}

/*
 * Set the scheduling parameters of the thread pointed to by fxprocp
 * to those specified in the buffer pointed to by fxparmsp.
 */
/* ARGSUSED */
static int
fx_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
{
	char		nice;
	pri_t		reqfxuprilim;
	pri_t		reqfxupri;
	fxkparms_t	*fxkparmsp = (fxkparms_t *)parmsp;
	fxproc_t	*fxpp;


	ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));

	thread_lock(tx);
	fxpp = (fxproc_t *)tx->t_cldata;

	if ((fxkparmsp->fx_cflags & FX_DOUPRILIM) == 0)
		reqfxuprilim = fxpp->fx_uprilim;
	else
		reqfxuprilim = fxkparmsp->fx_uprilim;

	/*
	 * Basic permissions enforced by generic kernel code
	 * for all classes require that a thread attempting
	 * to change the scheduling parameters of a target
	 * thread be privileged or have a real or effective
	 * UID matching that of the target thread. We are not
	 * called unless these basic permission checks have
	 * already passed. The fixed priority class requires in
	 * addition that the calling thread be privileged if it
	 * is attempting to raise the pri above its current
	 * value This may have been checked previously but if our
	 * caller passed us a non-NULL credential pointer we assume
	 * it hasn't and we check it here.
	 */

	if ((reqpcredp != NULL) &&
	    (reqfxuprilim > fxpp->fx_uprilim ||
	    ((fxkparmsp->fx_cflags & FX_DOTQ) != 0)) &&
	    secpolicy_raisepriority(reqpcredp) != 0) {
		thread_unlock(tx);
		return (EPERM);
	}

	FX_ADJUST_PRI(reqfxuprilim);

	if ((fxkparmsp->fx_cflags & FX_DOUPRI) == 0)
		reqfxupri = fxpp->fx_pri;
	else
		reqfxupri = fxkparmsp->fx_upri;


	/*
	 * Make sure the user priority doesn't exceed the upri limit.
	 */
	if (reqfxupri > reqfxuprilim)
		reqfxupri = reqfxuprilim;

	/*
	 * Set fx_nice to the nice value corresponding to the user
	 * priority we are setting.  Note that setting the nice field
	 * of the parameter struct won't affect upri or nice.
	 */

	nice = NZERO - (reqfxupri * NZERO) / fx_maxupri;

	if (nice > NZERO)
		nice = NZERO;

	fxpp->fx_uprilim = reqfxuprilim;
	fxpp->fx_pri = reqfxupri;

	if (fxkparmsp->fx_tqntm == FX_TQINF)
		fxpp->fx_pquantum = FX_TQINF;
	else if (fxkparmsp->fx_tqntm == FX_TQDEF)
		fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
	else if ((fxkparmsp->fx_cflags & FX_DOTQ) != 0)
		fxpp->fx_pquantum = fxkparmsp->fx_tqntm;

	fxpp->fx_nice = nice;

	fx_change_priority(tx, fxpp);
	thread_unlock(tx);
	return (0);
}


/*
 * Return the global scheduling priority that would be assigned
 * to a thread entering the fixed-priority class with the fx_upri.
 */
static pri_t
fx_globpri(kthread_t *t)
{
	fxproc_t *fxpp;

	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

	fxpp = (fxproc_t *)t->t_cldata;
	return (fx_dptbl[fxpp->fx_pri].fx_globpri);

}

/*
 * Arrange for thread to be placed in appropriate location
 * on dispatcher queue.
 *
 * This is called with the current thread in TS_ONPROC and locked.
 */
static void
fx_preempt(kthread_t *t)
{
	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);

	ASSERT(t == curthread);
	ASSERT(THREAD_LOCK_HELD(curthread));

	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ENFORCE);

	/*
	 * Check to see if we're doing "preemption control" here.  If
	 * we are, and if the user has requested that this thread not
	 * be preempted, and if preemptions haven't been put off for
	 * too long, let the preemption happen here but try to make
	 * sure the thread is rescheduled as soon as possible.  We do
	 * this by putting it on the front of the highest priority run
	 * queue in the FX class.  If the preemption has been put off
	 * for too long, clear the "nopreempt" bit and let the thread
	 * be preempted.
	 */
	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
		if (fxpp->fx_pquantum == FX_TQINF ||
		    fxpp->fx_timeleft > -SC_MAX_TICKS) {
			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
			schedctl_set_yield(t, 1);
			setfrontdq(t);
			return;
		} else {
			schedctl_set_nopreempt(t, 0);
			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
			TNF_PROBE_2(schedctl_preempt, "schedctl FX fx_preempt",
			    /* CSTYLED */, tnf_pid, pid, ttoproc(t)->p_pid,
			    tnf_lwpid, lwpid, t->t_tid);
			/*
			 * Fall through and be preempted below.
			 */
		}
	}

	if (FX_HAS_CB(fxpp)) {
		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
		pri_t	newpri = fxpp->fx_pri;
		FX_CB_PREEMPT(FX_CALLB(fxpp), fxpp->fx_cookie,
		    &new_quantum, &newpri);
		FX_ADJUST_QUANTUM(new_quantum);
		if ((int)new_quantum != fxpp->fx_pquantum) {
			fxpp->fx_pquantum = (int)new_quantum;
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		}
		FX_ADJUST_PRI(newpri);
		fxpp->fx_pri = newpri;
		THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
	}

	/*
	 * This thread may be placed on wait queue by CPU Caps. In this case we
	 * do not need to do anything until it is removed from the wait queue.
	 */
	if (CPUCAPS_ENFORCE(t)) {
		return;
	}

	if ((fxpp->fx_flags & (FXBACKQ)) == FXBACKQ) {
		fxpp->fx_timeleft = fxpp->fx_pquantum;
		fxpp->fx_flags &= ~FXBACKQ;
		setbackdq(t);
	} else {
		setfrontdq(t);
	}
}

static void
fx_setrun(kthread_t *t)
{
	fxproc_t *fxpp = (fxproc_t *)(t->t_cldata);

	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
	fxpp->fx_flags &= ~FXBACKQ;

	if (t->t_disp_time != ddi_get_lbolt())
		setbackdq(t);
	else
		setfrontdq(t);
}


/*
 * Prepare thread for sleep. We reset the thread priority so it will
 * run at the kernel priority level when it wakes up.
 */
static void
fx_sleep(kthread_t *t)
{
	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);

	ASSERT(t == curthread);
	ASSERT(THREAD_LOCK_HELD(t));

	/*
	 * Account for time spent on CPU before going to sleep.
	 */
	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ENFORCE);

	if (FX_HAS_CB(fxpp)) {
		FX_CB_SLEEP(FX_CALLB(fxpp), fxpp->fx_cookie);
	}
	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
}


/*
 * Return Values:
 *
 *	-1 if the thread is loaded or is not eligible to be swapped in.
 *
 * FX and RT threads are designed so that they don't swapout; however,
 * it is possible that while the thread is swapped out and in another class, it
 * can be changed to FX or RT.  Since these threads should be swapped in
 * as soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
 * returns SHRT_MAX - 1, so that it gives deference to any swapped out
 * RT threads.
 */
/* ARGSUSED */
static pri_t
fx_swapin(kthread_t *t, int flags)
{
	pri_t	tpri = -1;

	ASSERT(THREAD_LOCK_HELD(t));

	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
		tpri = (pri_t)SHRT_MAX - 1;
	}

	return (tpri);
}

/*
 * Return Values
 *	-1 if the thread isn't loaded or is not eligible to be swapped out.
 */
/* ARGSUSED */
static pri_t
fx_swapout(kthread_t *t, int flags)
{
	ASSERT(THREAD_LOCK_HELD(t));

	return (-1);

}

/* ARGSUSED */
static void
fx_stop(kthread_t *t, int why, int what)
{
	fxproc_t *fxpp = (fxproc_t *)(t->t_cldata);

	ASSERT(THREAD_LOCK_HELD(t));

	if (FX_HAS_CB(fxpp)) {
		FX_CB_STOP(FX_CALLB(fxpp), fxpp->fx_cookie);
	}
}

/*
 * Check for time slice expiration.  If time slice has expired
 * set runrun to cause preemption.
 */
static void
fx_tick(kthread_t *t)
{
	boolean_t call_cpu_surrender = B_FALSE;
	fxproc_t *fxpp;

	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));

	thread_lock(t);

	fxpp = (fxproc_t *)(t->t_cldata);

	if (FX_HAS_CB(fxpp)) {
		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
		pri_t	newpri = fxpp->fx_pri;
		FX_CB_TICK(FX_CALLB(fxpp), fxpp->fx_cookie,
		    &new_quantum, &newpri);
		FX_ADJUST_QUANTUM(new_quantum);
		if ((int)new_quantum != fxpp->fx_pquantum) {
			fxpp->fx_pquantum = (int)new_quantum;
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		}
		FX_ADJUST_PRI(newpri);
		if (newpri != fxpp->fx_pri) {
			fxpp->fx_pri = newpri;
			fx_change_priority(t, fxpp);
		}
	}

	/*
	 * Keep track of thread's project CPU usage.  Note that projects
	 * get charged even when threads are running in the kernel.
	 */
	call_cpu_surrender =  CPUCAPS_CHARGE(t, &fxpp->fx_caps,
	    CPUCAPS_CHARGE_ENFORCE);

	if ((fxpp->fx_pquantum != FX_TQINF) &&
	    (--fxpp->fx_timeleft <= 0)) {
		pri_t	new_pri;

		/*
		 * If we're doing preemption control and trying to
		 * avoid preempting this thread, just note that
		 * the thread should yield soon and let it keep
		 * running (unless it's been a while).
		 */
		if (t->t_schedctl && schedctl_get_nopreempt(t)) {
			if (fxpp->fx_timeleft > -SC_MAX_TICKS) {
				DTRACE_SCHED1(schedctl__nopreempt,
				    kthread_t *, t);
				schedctl_set_yield(t, 1);
				thread_unlock_nopreempt(t);
				return;
			}
			TNF_PROBE_2(schedctl_failsafe,
			    "schedctl FX fx_tick", /* CSTYLED */,
			    tnf_pid, pid, ttoproc(t)->p_pid,
			    tnf_lwpid, lwpid, t->t_tid);
		}
		new_pri = fx_dptbl[fxpp->fx_pri].fx_globpri;
		ASSERT(new_pri >= 0 && new_pri <= fx_maxglobpri);
		/*
		 * When the priority of a thread is changed,
		 * it may be necessary to adjust its position
		 * on a sleep queue or dispatch queue. Even
		 * when the priority is not changed, we need
		 * to preserve round robin on dispatch queue.
		 * The function thread_change_pri accomplishes
		 * this.
		 */
		if (thread_change_pri(t, new_pri, 0)) {
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		} else {
			call_cpu_surrender = B_TRUE;
		}
	} else if (t->t_state == TS_ONPROC &&
	    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
		call_cpu_surrender = B_TRUE;
	}

	if (call_cpu_surrender) {
		fxpp->fx_flags |= FXBACKQ;
		cpu_surrender(t);
	}
	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
}


static void
fx_trapret(kthread_t *t)
{
	cpu_t		*cp = CPU;

	ASSERT(THREAD_LOCK_HELD(t));
	ASSERT(t == curthread);
	ASSERT(cp->cpu_dispthread == t);
	ASSERT(t->t_state == TS_ONPROC);
}


/*
 * Processes waking up go to the back of their queue.
 */
static void
fx_wakeup(kthread_t *t)
{
	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);

	ASSERT(THREAD_LOCK_HELD(t));

	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
	if (FX_HAS_CB(fxpp)) {
		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
		pri_t	newpri = fxpp->fx_pri;
		FX_CB_WAKEUP(FX_CALLB(fxpp), fxpp->fx_cookie,
		    &new_quantum, &newpri);
		FX_ADJUST_QUANTUM(new_quantum);
		if ((int)new_quantum != fxpp->fx_pquantum) {
			fxpp->fx_pquantum = (int)new_quantum;
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		}

		FX_ADJUST_PRI(newpri);
		if (newpri != fxpp->fx_pri) {
			fxpp->fx_pri = newpri;
			THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
		}
	}

	fxpp->fx_flags &= ~FXBACKQ;

	if (t->t_disp_time != ddi_get_lbolt())
		setbackdq(t);
	else
		setfrontdq(t);
}


/*
 * When a thread yields, put it on the back of the run queue.
 */
static void
fx_yield(kthread_t *t)
{
	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);

	ASSERT(t == curthread);
	ASSERT(THREAD_LOCK_HELD(t));

	/*
	 * Collect CPU usage spent before yielding CPU.
	 */
	(void) CPUCAPS_CHARGE(t, &fxpp->fx_caps, CPUCAPS_CHARGE_ENFORCE);

	if (FX_HAS_CB(fxpp))  {
		clock_t new_quantum =  (clock_t)fxpp->fx_pquantum;
		pri_t	newpri = fxpp->fx_pri;
		FX_CB_PREEMPT(FX_CALLB(fxpp), fxpp->fx_cookie,
		    &new_quantum, &newpri);
		FX_ADJUST_QUANTUM(new_quantum);
		if ((int)new_quantum != fxpp->fx_pquantum) {
			fxpp->fx_pquantum = (int)new_quantum;
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		}
		FX_ADJUST_PRI(newpri);
		fxpp->fx_pri = newpri;
		THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
	}

	/*
	 * Clear the preemption control "yield" bit since the user is
	 * doing a yield.
	 */
	if (t->t_schedctl)
		schedctl_set_yield(t, 0);

	if (fxpp->fx_timeleft <= 0) {
		/*
		 * Time slice was artificially extended to avoid
		 * preemption, so pretend we're preempting it now.
		 */
		DTRACE_SCHED1(schedctl__yield, int, -fxpp->fx_timeleft);
		fxpp->fx_timeleft = fxpp->fx_pquantum;
		THREAD_CHANGE_PRI(t, fx_dptbl[fxpp->fx_pri].fx_globpri);
		ASSERT(t->t_pri >= 0 && t->t_pri <= fx_maxglobpri);
	}

	fxpp->fx_flags &= ~FXBACKQ;
	setbackdq(t);
}

/*
 * Increment the nice value of the specified thread by incr and
 * return the new value in *retvalp.
 */
static int
fx_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
{
	int		newnice;
	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
	fxkparms_t	fxkparms;

	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));

	/* If there's no change to priority, just return current setting */
	if (incr == 0) {
		if (retvalp) {
			*retvalp = fxpp->fx_nice - NZERO;
		}
		return (0);
	}

	if ((incr < 0 || incr > 2 * NZERO) &&
	    secpolicy_raisepriority(cr) != 0)
		return (EPERM);

	/*
	 * Specifying a nice increment greater than the upper limit of
	 * 2 * NZERO - 1 will result in the thread's nice value being
	 * set to the upper limit.  We check for this before computing
	 * the new value because otherwise we could get overflow
	 * if a privileged user specified some ridiculous increment.
	 */
	if (incr > 2 * NZERO - 1)
		incr = 2 * NZERO - 1;

	newnice = fxpp->fx_nice + incr;
	if (newnice > NZERO)
		newnice = NZERO;
	else if (newnice < 0)
		newnice = 0;

	fxkparms.fx_uprilim = fxkparms.fx_upri =
	    -((newnice - NZERO) * fx_maxupri) / NZERO;

	fxkparms.fx_cflags = FX_DOUPRILIM | FX_DOUPRI;

	fxkparms.fx_tqntm = FX_TQDEF;

	/*
	 * Reset the uprilim and upri values of the thread. Adjust
	 * time quantum accordingly.
	 */

	(void) fx_parmsset(t, (void *)&fxkparms, (id_t)0, (cred_t *)NULL);

	/*
	 * Although fx_parmsset already reset fx_nice it may
	 * not have been set to precisely the value calculated above
	 * because fx_parmsset determines the nice value from the
	 * user priority and we may have truncated during the integer
	 * conversion from nice value to user priority and back.
	 * We reset fx_nice to the value we calculated above.
	 */
	fxpp->fx_nice = (char)newnice;

	if (retvalp)
		*retvalp = newnice - NZERO;

	return (0);
}

/*
 * Increment the priority of the specified thread by incr and
 * return the new value in *retvalp.
 */
static int
fx_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
{
	int		newpri;
	fxproc_t	*fxpp = (fxproc_t *)(t->t_cldata);
	fxkparms_t	fxkparms;

	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));

	/* If there's no change to priority, just return current setting */
	if (incr == 0) {
		*retvalp = fxpp->fx_pri;
		return (0);
	}

	newpri = fxpp->fx_pri + incr;
	if (newpri > fx_maxupri || newpri < 0)
		return (EINVAL);

	*retvalp = newpri;
	fxkparms.fx_uprilim = fxkparms.fx_upri = newpri;
	fxkparms.fx_tqntm = FX_NOCHANGE;
	fxkparms.fx_cflags = FX_DOUPRILIM | FX_DOUPRI;

	/*
	 * Reset the uprilim and upri values of the thread.
	 */
	return (fx_parmsset(t, (void *)&fxkparms, (id_t)0, cr));
}

static void
fx_change_priority(kthread_t *t, fxproc_t *fxpp)
{
	pri_t	new_pri;

	ASSERT(THREAD_LOCK_HELD(t));
	new_pri = fx_dptbl[fxpp->fx_pri].fx_globpri;
	ASSERT(new_pri >= 0 && new_pri <= fx_maxglobpri);
	t->t_cpri = fxpp->fx_pri;
	if (t == curthread || t->t_state == TS_ONPROC) {
		/* curthread is always onproc */
		cpu_t	*cp = t->t_disp_queue->disp_cpu;
		THREAD_CHANGE_PRI(t, new_pri);
		if (t == cp->cpu_dispthread)
			cp->cpu_dispatch_pri = DISP_PRIO(t);
		if (DISP_MUST_SURRENDER(t)) {
			fxpp->fx_flags |= FXBACKQ;
			cpu_surrender(t);
		} else {
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		}
	} else {
		/*
		 * When the priority of a thread is changed,
		 * it may be necessary to adjust its position
		 * on a sleep queue or dispatch queue.
		 * The function thread_change_pri accomplishes
		 * this.
		 */
		if (thread_change_pri(t, new_pri, 0)) {
			/*
			 * The thread was on a run queue. Reset
			 * its CPU timeleft from the quantum
			 * associated with the new priority.
			 */
			fxpp->fx_timeleft = fxpp->fx_pquantum;
		} else {
			fxpp->fx_flags |= FXBACKQ;
		}
	}
}

static int
fx_alloc(void **p, int flag)
{
	void *bufp;

	bufp = kmem_alloc(sizeof (fxproc_t), flag);
	if (bufp == NULL) {
		return (ENOMEM);
	} else {
		*p = bufp;
		return (0);
	}
}

static void
fx_free(void *bufp)
{
	if (bufp)
		kmem_free(bufp, sizeof (fxproc_t));
}

/*
 * Release the callback list mutex after successful lookup
 */
void
fx_list_release(fxproc_t *fxpp)
{
	int index = FX_CB_LIST_HASH(fxpp->fx_ktid);
	kmutex_t *lockp = &fx_cb_list_lock[index];
	mutex_exit(lockp);
}

fxproc_t *
fx_list_lookup(kt_did_t ktid)
{
	int index = FX_CB_LIST_HASH(ktid);
	kmutex_t *lockp = &fx_cb_list_lock[index];
	fxproc_t *fxpp;

	mutex_enter(lockp);

	for (fxpp = fx_cb_plisthead[index].fx_cb_next;
	    fxpp != &fx_cb_plisthead[index]; fxpp = fxpp->fx_cb_next) {
		if (fxpp->fx_tp->t_cid == fx_cid && fxpp->fx_ktid == ktid &&
		    fxpp->fx_callback != NULL) {
			/*
			 * The caller is responsible for calling
			 * fx_list_release to drop the lock upon
			 * successful lookup
			 */
			return (fxpp);
		}
	}
	mutex_exit(lockp);
	return ((fxproc_t *)NULL);
}


/*
 * register a callback set of routines for current thread
 * thread should already be in FX class
 */
int
fx_register_callbacks(fx_callbacks_t *fx_callback, fx_cookie_t cookie,
	pri_t pri, clock_t quantum)
{

	fxproc_t	*fxpp;

	if (fx_callback == NULL)
		return (EINVAL);

	if (secpolicy_dispadm(CRED()) != 0)
		return (EPERM);

	if (FX_CB_VERSION(fx_callback) != FX_CALLB_REV)
		return (EINVAL);

	if (!FX_ISVALID(pri, quantum))
		return (EINVAL);

	thread_lock(curthread);		/* get dispatcher lock on thread */

	if (curthread->t_cid != fx_cid) {
		thread_unlock(curthread);
		return (EINVAL);
	}

	fxpp = (fxproc_t *)(curthread->t_cldata);
	ASSERT(fxpp != NULL);
	if (FX_HAS_CB(fxpp)) {
		thread_unlock(curthread);
		return (EINVAL);
	}

	fxpp->fx_callback = fx_callback;
	fxpp->fx_cookie = cookie;

	if (pri != FX_CB_NOCHANGE) {
		fxpp->fx_pri = pri;
		FX_ADJUST_PRI(fxpp->fx_pri);
		if (quantum == FX_TQDEF) {
			fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
		} else if (quantum == FX_TQINF) {
			fxpp->fx_pquantum = FX_TQINF;
		} else if (quantum != FX_NOCHANGE) {
			FX_ADJUST_QUANTUM(quantum);
			fxpp->fx_pquantum = quantum;
		}
	} else if (quantum != FX_NOCHANGE && quantum != FX_TQDEF) {
		if (quantum == FX_TQINF)
			fxpp->fx_pquantum = FX_TQINF;
		else {
			FX_ADJUST_QUANTUM(quantum);
			fxpp->fx_pquantum = quantum;
		}
	}

	fxpp->fx_ktid = ddi_get_kt_did();

	fx_change_priority(curthread, fxpp);

	thread_unlock(curthread);

	/*
	 * Link new structure into fxproc list.
	 */
	FX_CB_LIST_INSERT(fxpp);
	return (0);
}

/* unregister a callback set of routines for current thread */
int
fx_unregister_callbacks()
{
	fxproc_t	*fxpp;

	if ((fxpp = fx_list_lookup(ddi_get_kt_did())) == NULL) {
		/*
		 * did not have a registered callback;
		 */
		return (EINVAL);
	}

	thread_lock(fxpp->fx_tp);
	fxpp->fx_callback = NULL;
	fxpp->fx_cookie = NULL;
	thread_unlock(fxpp->fx_tp);
	fx_list_release(fxpp);

	FX_CB_LIST_DELETE(fxpp);
	return (0);
}

/*
 * modify priority and/or quantum value of a thread with callback
 */
int
fx_modify_priority(kt_did_t ktid, clock_t quantum, pri_t pri)
{
	fxproc_t	*fxpp;

	if (!FX_ISVALID(pri, quantum))
		return (EINVAL);

	if ((fxpp = fx_list_lookup(ktid)) == NULL) {
		/*
		 * either thread had exited or did not have a registered
		 * callback;
		 */
		return (ESRCH);
	}

	thread_lock(fxpp->fx_tp);

	if (pri != FX_CB_NOCHANGE) {
		fxpp->fx_pri = pri;
		FX_ADJUST_PRI(fxpp->fx_pri);
		if (quantum == FX_TQDEF) {
			fxpp->fx_pquantum = fx_dptbl[fxpp->fx_pri].fx_quantum;
		} else if (quantum == FX_TQINF) {
			fxpp->fx_pquantum = FX_TQINF;
		} else if (quantum != FX_NOCHANGE) {
			FX_ADJUST_QUANTUM(quantum);
			fxpp->fx_pquantum = quantum;
		}
	} else if (quantum != FX_NOCHANGE && quantum != FX_TQDEF) {
		if (quantum == FX_TQINF) {
			fxpp->fx_pquantum = FX_TQINF;
		} else {
			FX_ADJUST_QUANTUM(quantum);
			fxpp->fx_pquantum = quantum;
		}
	}

	fx_change_priority(fxpp->fx_tp, fxpp);

	thread_unlock(fxpp->fx_tp);
	fx_list_release(fxpp);
	return (0);
}


/*
 * return an iblock cookie for mutex initialization to be used in callbacks
 */
void *
fx_get_mutex_cookie()
{
	return ((void *)(uintptr_t)__ipltospl(DISP_LEVEL));
}

/*
 * return maximum relative priority
 */
pri_t
fx_get_maxpri()
{
	return (fx_maxumdpri);
}