summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/disp/priocntl.c
blob: 60e870ba28a9d08dbb5f8415e46b8eb29c3b45b1 (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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/signal.h>
#include <sys/pcb.h>
#include <sys/user.h>
#include <sys/systm.h>
#include <sys/sysinfo.h>
#include <sys/var.h>
#include <sys/errno.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/procset.h>
#include <sys/debug.h>
#include <sys/inline.h>
#include <sys/priocntl.h>
#include <sys/disp.h>
#include <sys/class.h>
#include <sys/modctl.h>
#include <sys/t_lock.h>
#include <sys/uadmin.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
#include <sys/schedctl.h>

/*
 * Structure used to pass arguments to the proccmp() function.
 * The arguments must be passed in a structure because proccmp()
 * is called indirectly through the dotoprocs() function which
 * will only pass through a single one word argument.
 */
struct pcmpargs {
	id_t	*pcmp_cidp;
	int	*pcmp_cntp;
	kthread_t **pcmp_retthreadp;
};

/*
 * Structure used to pass arguments to the setparms() function
 * which is called indirectly through dotoprocs().
 */
struct stprmargs {
	struct pcparms	*stp_parmsp;	/* pointer to parameters */
	int		stp_error;	/* some errors returned here */
};

#if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
/*
 * A vaparm_t is an int followed by a long long -- this packs differently
 * between the 64-bit kernel ABI and the 32-bit user ABI.
 */
static int
copyin_vaparms32(caddr_t arg, pc_vaparms_t *vap, uio_seg_t seg)
{
	pc_vaparms32_t vaparms32;
	pc_vaparm32_t *src;
	pc_vaparm_t *dst;
	uint_t cnt;

	ASSERT(get_udatamodel() == DATAMODEL_ILP32);

	if ((seg == UIO_USERSPACE ? copyin : kcopy)(arg, &vaparms32,
	    sizeof (vaparms32)))
		return (EFAULT);

	vap->pc_vaparmscnt = vaparms32.pc_vaparmscnt;
	if ((cnt = vaparms32.pc_vaparmscnt) > PC_VAPARMCNT)
		cnt = PC_VAPARMCNT;
	for (src = vaparms32.pc_parms, dst = vap->pc_parms;
	    cnt--; src++, dst++) {
		dst->pc_key = src->pc_key;
		dst->pc_parm = src->pc_parm;
	}
	return (0);
}

#define	COPYIN_VAPARMS(arg, vap, size, seg)	\
	(get_udatamodel() == DATAMODEL_NATIVE ?	\
	(*copyinfn)(arg, vap, size) : copyin_vaparms32(arg, vap, seg))

#else

#define	COPYIN_VAPARMS(arg, vap, size, seg)	(*copyinfn)(arg, vap, size)

#endif

int donice(procset_t *, pcnice_t *);
static int doprio(procset_t *, pcprio_t *);
static int proccmp(proc_t *, struct pcmpargs *);
static int setparms(proc_t *, struct stprmargs *);
extern int threadcmp(struct pcmpargs *, kthread_t *);

/*
 * The priocntl system call.
 */
long
priocntl_common(int pc_version, procset_t *psp, int cmd, caddr_t arg,
    caddr_t arg2, uio_seg_t seg)
{
	pcinfo_t		pcinfo;
	pcparms_t		pcparms;
	pcnice_t		pcnice;
	pcprio_t		pcprio;
	pcadmin_t		pcadmin;
	pcpri_t			pcpri;
	procset_t		procset;
	struct stprmargs	stprmargs;
	struct pcmpargs		pcmpargs;
	pc_vaparms_t		vaparms;
	char			clname[PC_CLNMSZ];
	char			*outstr;
	int			count;
	kthread_t		*retthreadp;
	proc_t			*initpp;
	int			clnullflag;
	int			error = 0;
	int			error1 = 0;
	int			rv = 0;
	pid_t			saved_pid;
	id_t			classid;
	int			size;
	int (*copyinfn)(const void *, void *, size_t);
	int (*copyoutfn)(const void *, void *, size_t);

	/*
	 * First just check the version number. Right now there is only
	 * one version we know about and support.  If we get some other
	 * version number from the application it may be that the
	 * application was built with some future version and is trying
	 * to run on an old release of the system (that's us).  In any
	 * case if we don't recognize the version number all we can do is
	 * return error.
	 */
	if (pc_version != PC_VERSION)
		return (set_errno(EINVAL));

	if (seg == UIO_USERSPACE) {
		copyinfn = copyin;
		copyoutfn = copyout;
	} else {
		copyinfn = kcopy;
		copyoutfn = kcopy;
	}

	switch (cmd) {
	case PC_GETCID:
		/*
		 * If the arg pointer is NULL, the user just wants to
		 * know the number of classes. If non-NULL, the pointer
		 * should point to a valid user pcinfo buffer.  In the
		 * dynamic world we need to return the number of loaded
		 * classes, not the max number of available classes that
		 * can be loaded.
		 */
		if (arg == NULL) {
			rv = loaded_classes;
			break;
		} else {
			if ((*copyinfn)(arg, &pcinfo, sizeof (pcinfo)))
				return (set_errno(EFAULT));
		}

		pcinfo.pc_clname[PC_CLNMSZ-1] = '\0';

		/*
		 * Get the class ID corresponding to user supplied name.
		 */
		error = getcid(pcinfo.pc_clname, &pcinfo.pc_cid);
		if (error)
			return (set_errno(error));

		/*
		 * Can't get info about the sys class.
		 */
		if (pcinfo.pc_cid == 0)
			return (set_errno(EINVAL));

		/*
		 * Get the class specific information.
		 * we MUST make sure that the class has not already
		 * been unloaded before we try the CL_GETCLINFO.
		 * If it has then we need to load it.
		 */
		error =
		    scheduler_load(pcinfo.pc_clname, &sclass[pcinfo.pc_cid]);
		if (error)
			return (set_errno(error));
		error = CL_GETCLINFO(&sclass[pcinfo.pc_cid], pcinfo.pc_clinfo);
		if (error)
			return (set_errno(error));

		if ((*copyoutfn)(&pcinfo, arg, sizeof (pcinfo)))
			return (set_errno(EFAULT));

		rv = loaded_classes;

		break;

	case PC_GETCLINFO:
		/*
		 * If the arg pointer is NULL, the user just wants to know
		 * the number of classes. If non-NULL, the pointer should
		 * point to a valid user pcinfo buffer.
		 */
		if (arg == NULL) {
			rv = loaded_classes;
			break;
		} else {
			if ((*copyinfn)(arg, &pcinfo, sizeof (pcinfo)))
				return (set_errno(EFAULT));
		}

		if (pcinfo.pc_cid >= loaded_classes || pcinfo.pc_cid < 1)
			return (set_errno(EINVAL));

		(void) strncpy(pcinfo.pc_clname, sclass[pcinfo.pc_cid].cl_name,
		    PC_CLNMSZ);

		/*
		 * Get the class specific information.  we MUST make sure
		 * that the class has not already been unloaded before we
		 * try the CL_GETCLINFO.  If it has then we need to load
		 * it.
		 */
		error =
		    scheduler_load(pcinfo.pc_clname, &sclass[pcinfo.pc_cid]);
		if (error)
			return (set_errno(error));
		error = CL_GETCLINFO(&sclass[pcinfo.pc_cid], pcinfo.pc_clinfo);
		if (error)
			return (set_errno(error));

		if ((*copyoutfn)(&pcinfo, arg, sizeof (pcinfo)))
			return (set_errno(EFAULT));

		rv = loaded_classes;
		break;

	case PC_SETPARMS:
	case PC_SETXPARMS:
		/*
		 * First check the validity of the parameters we got from
		 * the user.  We don't do any permissions checking here
		 * because it's done on a per thread basis by parmsset().
		 */
		if (cmd == PC_SETPARMS) {
			if ((*copyinfn)(arg, &pcparms, sizeof (pcparms)))
				return (set_errno(EFAULT));

			error = parmsin(&pcparms, NULL);
		} else {
			if ((*copyinfn)(arg, clname, PC_CLNMSZ) ||
			    COPYIN_VAPARMS(arg2, &vaparms, sizeof (vaparms),
			    seg))
				return (set_errno(EFAULT));
			clname[PC_CLNMSZ-1] = '\0';

			if (getcid(clname, &pcparms.pc_cid))
				return (set_errno(EINVAL));

			error = parmsin(&pcparms, &vaparms);
		}

		if (error)
			return (set_errno(error));

		/*
		 * Get the procset from the user.
		 */
		if ((*copyinfn)(psp, &procset, sizeof (procset)))
			return (set_errno(EFAULT));

		/*
		 * For performance we do a quick check here to catch
		 * common cases where the current thread is the only one
		 * in the set.  In such cases we can call parmsset()
		 * directly, avoiding the relatively lengthy path through
		 * dotoprocs().  The underlying classes expect pidlock to
		 * be held.
		 */
		if (cur_inset_only(&procset) == B_TRUE) {
			/* do a single LWP */
			if ((procset.p_lidtype == P_LWPID) ||
			    (procset.p_ridtype == P_LWPID)) {
				mutex_enter(&pidlock);
				mutex_enter(&curproc->p_lock);
				error = parmsset(&pcparms, curthread);
				mutex_exit(&curproc->p_lock);
				mutex_exit(&pidlock);
			} else {
				/* do the entire process otherwise */
				stprmargs.stp_parmsp = &pcparms;
				stprmargs.stp_error = 0;
				mutex_enter(&pidlock);
				error = setparms(curproc, &stprmargs);
				mutex_exit(&pidlock);
				if (error == 0 && stprmargs.stp_error != 0)
					error = stprmargs.stp_error;
			}
			if (error)
				return (set_errno(error));
		} else {
			stprmargs.stp_parmsp = &pcparms;
			stprmargs.stp_error = 0;

			error1 = error = ESRCH;

			/*
			 * The dotoprocs() call below will cause
			 * setparms() to be called for each thread in the
			 * specified procset. setparms() will in turn
			 * call parmsset() (which does the real work).
			 */
			if ((procset.p_lidtype != P_LWPID) ||
			    (procset.p_ridtype != P_LWPID)) {
				error1 = dotoprocs(&procset, setparms,
				    (char *)&stprmargs);
			}

			/*
			 * take care of the case when any of the
			 * operands happen to be LWP's
			 */

			if ((procset.p_lidtype == P_LWPID) ||
			    (procset.p_ridtype == P_LWPID)) {
				error = dotolwp(&procset, parmsset,
				    (char *)&pcparms);
				/*
				 * Dotolwp() returns with p_lock held.
				 * This is required for the GETPARMS case
				 * below. So, here we just release the
				 * p_lock.
				 */
				if (MUTEX_HELD(&curproc->p_lock))
					mutex_exit(&curproc->p_lock);
			}

			/*
			 * If setparms() encounters a permissions error
			 * for one or more of the threads it returns
			 * EPERM in stp_error so dotoprocs() will
			 * continue through the thread set.  If
			 * dotoprocs() returned an error above, it was
			 * more serious than permissions and dotoprocs
			 * quit when the error was encountered.  We
			 * return the more serious error if there was
			 * one, otherwise we return EPERM if we got that
			 * back.
			 */
			if (error1 != ESRCH)
				error = error1;
			if (error == 0 && stprmargs.stp_error != 0)
				error = stprmargs.stp_error;
		}
		break;

	case PC_GETPARMS:
	case PC_GETXPARMS:
		if (cmd == PC_GETPARMS) {
			if ((*copyinfn)(arg, &pcparms, sizeof (pcparms)))
				return (set_errno(EFAULT));
		} else {
			if (arg != NULL) {
				if ((*copyinfn)(arg, clname, PC_CLNMSZ))
					return (set_errno(EFAULT));

				clname[PC_CLNMSZ-1] = '\0';

				if (getcid(clname, &pcparms.pc_cid))
					return (set_errno(EINVAL));
			} else
				pcparms.pc_cid = PC_CLNULL;

			if (COPYIN_VAPARMS(arg2, &vaparms, sizeof (vaparms),
			    seg))
				return (set_errno(EFAULT));
		}

		if (pcparms.pc_cid >= loaded_classes ||
		    (pcparms.pc_cid < 1 && pcparms.pc_cid != PC_CLNULL))
			return (set_errno(EINVAL));

		if ((*copyinfn)(psp, &procset, sizeof (procset)))
			return (set_errno(EFAULT));

		/*
		 * Check to see if the current thread is the only one
		 * in the set. If not we must go through the whole set
		 * to select a thread.
		 */
		if (cur_inset_only(&procset) == B_TRUE) {
			/* do a single LWP */
			if ((procset.p_lidtype == P_LWPID) ||
			    (procset.p_ridtype == P_LWPID)) {
				if (pcparms.pc_cid != PC_CLNULL &&
				    pcparms.pc_cid != curthread->t_cid) {
					/*
					 * Specified thread not in
					 * specified class.
					 */
					return (set_errno(ESRCH));
				} else {
					mutex_enter(&curproc->p_lock);
					retthreadp = curthread;
				}
			} else {
				count = 0;
				retthreadp = NULL;
				pcmpargs.pcmp_cidp = &pcparms.pc_cid;
				pcmpargs.pcmp_cntp = &count;
				pcmpargs.pcmp_retthreadp = &retthreadp;
				/*
				 * Specified thread not in specified class.
				 */
				if (pcparms.pc_cid != PC_CLNULL &&
				    pcparms.pc_cid != curthread->t_cid)
					return (set_errno(ESRCH));
				error = proccmp(curproc, &pcmpargs);
				if (error) {
					if (retthreadp != NULL)
						mutex_exit(&(curproc->p_lock));
					return (set_errno(error));
				}
			}
		} else {
			/*
			 * get initpp early to avoid lock ordering problems
			 * (we cannot get pidlock while holding any p_lock).
			 */
			mutex_enter(&pidlock);
			initpp = prfind(P_INITPID);
			mutex_exit(&pidlock);

			/*
			 * Select the thread (from the set) whose
			 * parameters we are going to return.  First we
			 * set up some locations for return values, then
			 * we call proccmp() indirectly through
			 * dotoprocs().  proccmp() will call a class
			 * specific routine which actually does the
			 * selection.  To understand how this works take
			 * a careful look at the code below, the
			 * dotoprocs() function, the proccmp() function,
			 * and the class specific cl_proccmp() functions.
			 */
			if (pcparms.pc_cid == PC_CLNULL)
				clnullflag = 1;
			else
				clnullflag = 0;
			count = 0;
			retthreadp = NULL;
			pcmpargs.pcmp_cidp = &pcparms.pc_cid;
			pcmpargs.pcmp_cntp = &count;
			pcmpargs.pcmp_retthreadp = &retthreadp;
			error1 = error = ESRCH;

			if ((procset.p_lidtype != P_LWPID) ||
			    (procset.p_ridtype != P_LWPID)) {
				error1 = dotoprocs(&procset, proccmp,
				    (char *)&pcmpargs);
			}

			/*
			 * take care of combination of LWP and process
			 * set case in a procset
			 */
			if ((procset.p_lidtype == P_LWPID) ||
			    (procset.p_ridtype == P_LWPID)) {
				error = dotolwp(&procset, threadcmp,
				    (char *)&pcmpargs);
			}

			/*
			 * Both proccmp() and threadcmp() return with the
			 * p_lock held for the ttoproc(retthreadp). This
			 * is required to make sure that the process we
			 * chose as the winner doesn't go away
			 * i.e. retthreadp has to be a valid pointer.
			 *
			 * The case below can only happen if the thread
			 * with the highest priority was not in your
			 * process.  In that case, dotolwp will return
			 * holding p_lock for both your process as well
			 * as the process in which retthreadp is a
			 * thread.
			 */
			if ((retthreadp != NULL) &&
			    (ttoproc(retthreadp) != curproc) &&
			    MUTEX_HELD(&(curproc)->p_lock))
				mutex_exit(&(curproc)->p_lock);

			ASSERT(retthreadp == NULL ||
			    MUTEX_HELD(&(ttoproc(retthreadp)->p_lock)));
			if (error1 != ESRCH)
				error = error1;
			if (error) {
				if (retthreadp != NULL)
				    /* CSTYLED */
				    mutex_exit(&(ttoproc(retthreadp)->p_lock));
				ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
				return (set_errno(error));
			}
			/*
			 * dotoprocs() ignores the init process if it is
			 * in the set, unless it was the only process found.
			 * Since we are getting parameters here rather than
			 * setting them, we want to make sure init is not
			 * excluded if it is in the set.
			 */
			if (initpp != NULL && retthreadp != NULL &&
			    ttoproc(retthreadp) != initpp) {
				mutex_enter(&initpp->p_lock);
				if (procinset(initpp, &procset)) {
					mutex_exit(&initpp->p_lock);
					(void) proccmp(initpp, &pcmpargs);
				} else {
					mutex_exit(&initpp->p_lock);
				}
			}

			/*
			 * If dotoprocs returned success it found at least
			 * one thread in the set.  If proccmp() failed to
			 * select a thread it is because the user specified
			 * a class and none of the threads in the set
			 * belonged to that class, or because the process
			 * specified was in the middle of exiting and had
			 * cleared its thread list.
			 */
			if (retthreadp == NULL) {
				/*
				 * Might be here and still holding p_lock
				 * if we did a dotolwp on an lwp that
				 * existed but was in the wrong class.
				 */
				if (MUTEX_HELD(&(curproc)->p_lock))
					mutex_exit(&(curproc)->p_lock);
				return (set_errno(ESRCH));
			}

			/*
			 * User can only use PC_CLNULL with one thread in set.
			 */
			if (clnullflag && count > 1) {
				if (retthreadp != NULL)
					mutex_exit(
					    &(ttoproc(retthreadp)->p_lock));
				ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
				return (set_errno(EINVAL));
			}
		}

		ASSERT(retthreadp == NULL ||
		    MUTEX_HELD(&(ttoproc(retthreadp)->p_lock)));
		/*
		 * It is possible to have retthreadp == NULL. Proccmp()
		 * in the rare case (p_tlist == NULL) could return without
		 * setting a value for retthreadp.
		 */
		if (retthreadp == NULL) {
			ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
			return (set_errno(ESRCH));
		}
		/*
		 * We've selected a thread so now get the parameters.
		 */
		parmsget(retthreadp, &pcparms);

		/*
		 * Prepare to return parameters to the user
		 */
		error = parmsout(&pcparms,
		    (cmd == PC_GETPARMS ? NULL : &vaparms));

		/*
		 * Save pid of selected thread before dropping p_lock.
		 */
		saved_pid = ttoproc(retthreadp)->p_pid;
		mutex_exit(&(ttoproc(retthreadp)->p_lock));
		ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));

		if (error)
			return (set_errno(error));

		if (cmd == PC_GETPARMS) {
			if ((*copyoutfn)(&pcparms, arg, sizeof (pcparms)))
				return (set_errno(EFAULT));
		} else if ((error = vaparmsout(arg, &pcparms, &vaparms,
		    seg)) != 0)
			return (set_errno(error));

		/*
		 * And finally, return the pid of the selected thread.
		 */
		rv = saved_pid;
		break;

	case PC_ADMIN:
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if ((*copyinfn)(arg, &pcadmin, sizeof (pcadmin_t)))
				return (set_errno(EFAULT));
#ifdef _SYSCALL32_IMPL
		} else {
			/* pcadmin struct from ILP32 callers */
			pcadmin32_t pcadmin32;

			if ((*copyinfn)(arg, &pcadmin32, sizeof (pcadmin32_t)))
				return (set_errno(EFAULT));
			pcadmin.pc_cid = pcadmin32.pc_cid;
			pcadmin.pc_cladmin = (caddr_t)(uintptr_t)
			    pcadmin32.pc_cladmin;
#endif /* _SYSCALL32_IMPL */
		}

		if (pcadmin.pc_cid >= loaded_classes ||
		    pcadmin.pc_cid < 1)
			return (set_errno(EINVAL));

		/*
		 * Have the class do whatever the user is requesting.
		 */
		mutex_enter(&ualock);
		error = CL_ADMIN(&sclass[pcadmin.pc_cid], pcadmin.pc_cladmin,
		    CRED());
		mutex_exit(&ualock);
		break;

	case PC_GETPRIRANGE:
		if ((*copyinfn)(arg, &pcpri, sizeof (pcpri_t)))
			return (set_errno(EFAULT));

		if (pcpri.pc_cid >= loaded_classes || pcpri.pc_cid < 0)
			return (set_errno(EINVAL));

		error = CL_GETCLPRI(&sclass[pcpri.pc_cid], &pcpri);
		if (!error) {
			if ((*copyoutfn)(&pcpri, arg, sizeof (pcpri)))
				return (set_errno(EFAULT));
		}
		break;

	case PC_DONICE:
		/*
		 * Get pcnice and procset structures from the user.
		 */
		if ((*copyinfn)(arg, &pcnice, sizeof (pcnice)) ||
		    (*copyinfn)(psp, &procset, sizeof (procset)))
			return (set_errno(EFAULT));

		error = donice(&procset, &pcnice);

		if (!error && (pcnice.pc_op == PC_GETNICE)) {
			if ((*copyoutfn)(&pcnice, arg, sizeof (pcnice)))
				return (set_errno(EFAULT));
		}
		break;

	case PC_DOPRIO:
		/*
		 * Get pcprio and procset structures from the user.
		 */
		if ((*copyinfn)(arg, &pcprio, sizeof (pcprio)) ||
		    (*copyinfn)(psp, &procset, sizeof (procset)))
			return (set_errno(EFAULT));

		error = doprio(&procset, &pcprio);

		if (!error && (pcprio.pc_op == PC_GETPRIO)) {
			if ((*copyoutfn)(&pcprio, arg, sizeof (pcprio)))
				return (set_errno(EFAULT));
		}
		break;

	case PC_SETDFLCL:
		if (secpolicy_dispadm(CRED()) != 0)
			return (set_errno(EPERM));

		if (copyin(arg, (caddr_t)clname, PC_CLNMSZ) != 0)
			return (set_errno(EFAULT));
		clname[PC_CLNMSZ-1] = '\0';

		if (getcid(clname, &classid) != 0)
			return (set_errno(EINVAL));
		if (CLASS_KERNEL(classid))
			return (set_errno(EINVAL));
		defaultcid = classid;
		ASSERT(defaultcid > 0 && defaultcid < loaded_classes);
		break;

	case PC_GETDFLCL:
		mutex_enter(&class_lock);

		if (defaultcid >= loaded_classes)
			outstr = "";
		else
			outstr = sclass[defaultcid].cl_name;
		size = strlen(outstr) + 1;
		if (arg != NULL)
			if ((*copyoutfn)(outstr, arg, size) != 0)
				error = EFAULT;

		mutex_exit(&class_lock);
		break;

	default:
		error = EINVAL;
		break;
	}
	return (error ? (set_errno(error)) : rv);
}

long
priocntlsys(int pc_version, procset_t *psp, int cmd, caddr_t arg, caddr_t arg2)
{
	return (priocntl_common(pc_version, psp, cmd, arg, arg2,
	    UIO_USERSPACE));
}

/*
 * The proccmp() function is part of the implementation of the
 * PC_GETPARMS command of the priocntl system call.  This function works
 * with the system call code and with the class specific cl_globpri()
 * function to select one thread from a specified procset based on class
 * specific criteria. proccmp() is called indirectly from the priocntl
 * code through the dotoprocs function.  Basic strategy is dotoprocs()
 * calls us once for each thread in the set.  We in turn call the class
 * specific function to compare the current thread from dotoprocs to the
 * "best" (according to the class criteria) found so far.  We keep the
 * "best" thread in *pcmp_retthreadp.
 */
static int
proccmp(proc_t *pp, struct pcmpargs *argp)
{
	kthread_t	*tx;
	kthread_t	*ty;
	int		last_pri = -1;
	int		tx_pri;
	int		found = 0;

	mutex_enter(&pp->p_lock);

	if (pp->p_tlist == NULL) {
		mutex_exit(&pp->p_lock);
		return (0);
	}
	(*argp->pcmp_cntp)++;	/* Increment count of procs in the set */

	if (*argp->pcmp_cidp == PC_CLNULL) {
		/*
		 * If no cid is specified, then lets just pick the first one.
		 * It doesn't matter because if the number of processes in the
		 * set are more than 1, then we return EINVAL in priocntlsys.
		 */
		*argp->pcmp_cidp = pp->p_tlist->t_cid;
	}
	ty = tx = pp->p_tlist;
	do {
		if (tx->t_cid == *argp->pcmp_cidp) {
			/*
			 * We found one which matches the required cid.
			 */
			found = 1;
			if ((tx_pri = CL_GLOBPRI(tx)) > last_pri) {
				last_pri = tx_pri;
				ty = tx;
			}
		}
	} while ((tx = tx->t_forw) != pp->p_tlist);
	if (found) {
		if (*argp->pcmp_retthreadp == NULL) {
			/*
			 * First time through for this set.
			 * keep the mutex held. It might be the one!
			 */
			*argp->pcmp_retthreadp = ty;
		} else {
			tx = *argp->pcmp_retthreadp;
			if (CL_GLOBPRI(ty) <= CL_GLOBPRI(tx)) {
				mutex_exit(&pp->p_lock);
			} else {
				mutex_exit(&(ttoproc(tx)->p_lock));
				*argp->pcmp_retthreadp = ty;
			}
		}
	} else {
		/*
		 * We actually didn't find anything of the same cid in
		 * this process.
		 */
		mutex_exit(&pp->p_lock);
	}
	return (0);
}


int
threadcmp(struct pcmpargs *argp, kthread_t *tp)
{
	kthread_t	*tx;
	proc_t		*pp;

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

	(*argp->pcmp_cntp)++;   /* Increment count of procs in the set */
	if (*argp->pcmp_cidp == PC_CLNULL) {
		/*
		 * If no cid is specified, then lets just pick the first one.
		 * It doesn't matter because if the number of threads in the
		 * set are more than 1, then we return EINVAL in priocntlsys.
		 */
		*argp->pcmp_cidp = tp->t_cid;
	}
	if (tp->t_cid == *argp->pcmp_cidp) {
		if (*argp->pcmp_retthreadp == NULL) {
			/*
			 * First time through for this set.
			 */
			*argp->pcmp_retthreadp = tp;
		} else {
			tx = *argp->pcmp_retthreadp;
			if (CL_GLOBPRI(tp) > CL_GLOBPRI(tx)) {
				/*
				 * Unlike proccmp(), we don't release the
				 * p_lock of the ttoproc(tp) if tp's global
				 * priority is less than tx's. We need to go
				 * through the entire list before we can do
				 * that. The p_lock is released by the caller
				 * of dotolwp().
				 */
				pp = ttoproc(tx);
				ASSERT(MUTEX_HELD(&pp->p_lock));
				if (pp != curproc) {
					mutex_exit(&pp->p_lock);
				}
				*argp->pcmp_retthreadp = tp;
			}
		}
	}
	return (0);
}


/*
 * The setparms() function is called indirectly by priocntlsys()
 * through the dotoprocs() function.  setparms() acts as an
 * intermediary between dotoprocs() and the parmsset() function,
 * calling parmsset() for each thread in the set and handling
 * the error returns on their way back up to dotoprocs().
 */
static int
setparms(proc_t *targpp, struct stprmargs *stprmp)
{
	int error = 0;
	kthread_t *t;
	int err;

	mutex_enter(&targpp->p_lock);
	if ((t = targpp->p_tlist) == NULL) {
		mutex_exit(&targpp->p_lock);
		return (0);
	}
	do {
		err = parmsset(stprmp->stp_parmsp, t);
		if (error == 0)
			error = err;
	} while ((t = t->t_forw) != targpp->p_tlist);
	mutex_exit(&targpp->p_lock);
	if (error) {
		if (error == EPERM) {
			stprmp->stp_error = EPERM;
			return (0);
		} else {
			return (error);
		}
	} else
		return (0);
}

int
setthreadnice(pcnice_t *pcnice, kthread_t *tp)
{
	int error;
	int nice;
	int inc;
	id_t rtcid;

	ASSERT(MUTEX_HELD(&pidlock));
	ASSERT(MUTEX_HELD(&(ttoproc(tp)->p_lock)));

	/*
	 * The XPG5 standard requires that any realtime process or thread
	 * must be unaffected by a call to setpriority().
	 */
	error = getcidbyname("RT", &rtcid);
	if (error == 0 && tp->t_cid == rtcid) {
		if (pcnice->pc_op == PC_SETNICE)
			return (0);
	}

	if ((error = CL_DONICE(tp, CRED(), 0, &nice)) != 0)
		return (error);

	if (pcnice->pc_op == PC_GETNICE) {
		/*
		 * If there is no change to priority, we should return the
		 * highest priority (lowest numerical value) pertaining to
		 * any of the specified threads.
		 */
		if (nice < pcnice->pc_val)
			pcnice->pc_val = nice;
	} else {
		ASSERT(pcnice->pc_op == PC_SETNICE);
		/*
		 * Try to change the nice value of the thread.
		 */
		inc = pcnice->pc_val - nice;

		error = CL_DONICE(tp, CRED(), inc, &inc);
		schedctl_set_cidpri(tp);
	}

	return (error);
}

int
setprocnice(proc_t *pp, pcnice_t *pcnice)
{
	kthread_t *tp;
	int retval = 0;
	int error;

	ASSERT(MUTEX_HELD(&pidlock));
	mutex_enter(&pp->p_lock);

	if ((tp = pp->p_tlist) == NULL) {
		mutex_exit(&pp->p_lock);
		return (ESRCH);
	}

	/*
	 * Check permissions before changing the nice value.
	 */
	if (pcnice->pc_op == PC_SETNICE) {
		if (!prochasprocperm(pp, curproc, CRED())) {
			mutex_exit(&pp->p_lock);
			return (EPERM);
		}
	}

	do {
		error = setthreadnice(pcnice, tp);
		if (error)
			retval = error;
	} while ((tp = tp->t_forw) != pp->p_tlist);

	mutex_exit(&pp->p_lock);
	return (retval);
}

/*
 * Update the nice value of the specified LWP or set of processes.
 */
int
donice(procset_t *procset, pcnice_t *pcnice)
{
	int err_proc = 0;
	int err_thread = 0;
	int err = 0;

	/*
	 * Sanity check.
	 */
	if (pcnice->pc_op != PC_GETNICE && pcnice->pc_op != PC_SETNICE)
		return (EINVAL);

	/*
	 * If it is PC_GETNICE operation then set pc_val to the largest
	 * possible nice value to help us find the lowest nice value
	 * pertaining to any of the specified processes.
	 */
	if (pcnice->pc_op == PC_GETNICE)
		pcnice->pc_val = NZERO;

	if (procset->p_lidtype != P_LWPID ||
	    procset->p_ridtype != P_LWPID)
		err_proc = dotoprocs(procset, setprocnice, (char *)pcnice);

	if (procset->p_lidtype == P_LWPID || procset->p_ridtype == P_LWPID) {
		err_thread = dotolwp(procset, setthreadnice, (char *)pcnice);
		/*
		 * dotolwp() can return with p_lock held.  This is required
		 * for the priocntl GETPARMS case.  So, here we just release
		 * the p_lock.
		 */
		if (MUTEX_HELD(&curproc->p_lock))
			mutex_exit(&curproc->p_lock);

		/*
		 * If we were called for a single LWP, then ignore ESRCH
		 * returned by the previous dotoprocs() call.
		 */
		if (err_proc == ESRCH)
			err_proc = 0;
	}

	/*
	 * dotoprocs() ignores the init process if it is in the set, unless
	 * it was the only process found. We want to make sure init is not
	 * excluded if we're going PC_GETNICE operation.
	 */
	if (pcnice->pc_op == PC_GETNICE) {
		proc_t *initpp;

		mutex_enter(&pidlock);
		if ((initpp = prfind(P_INITPID)) != NULL) {
			mutex_enter(&initpp->p_lock);
			if (procinset(initpp, procset)) {
				mutex_exit(&initpp->p_lock);
				err = setprocnice(initpp, pcnice);
			} else {
				mutex_exit(&initpp->p_lock);
			}
		}
		mutex_exit(&pidlock);
	}

	/*
	 * We're returning the latest error here that we've got back from
	 * the setthreadnice() or setprocnice(). That is, err_thread and/or
	 * err_proc can be replaced by err.
	 */
	if (!err)
		err = err_thread ? err_thread : err_proc;

	return (err);
}

int
setthreadprio(pcprio_t *pcprio, kthread_t *tp)
{
	int prio = 0;
	int incr;
	int error;

	ASSERT(MUTEX_HELD(&pidlock));
	ASSERT(MUTEX_HELD(&(ttoproc(tp)->p_lock)));

	if (pcprio->pc_op == PC_SETPRIO && pcprio->pc_cid != tp->t_cid) {
		/*
		 * Target thread must change to new class.
		 * See comments in parmsset(), from where this code was copied.
		 */
		void *bufp = NULL;
		caddr_t clprocp = (caddr_t)tp->t_cldata;
		id_t oldcid = tp->t_cid;

		error = CL_CANEXIT(tp, NULL);
		if (error)
			return (error);
		if (CL_ALLOC(&bufp, pcprio->pc_cid, KM_NOSLEEP) != 0)
			return (ENOMEM);
		error = CL_ENTERCLASS(tp, pcprio->pc_cid, NULL, CRED(), bufp);
		if (error) {
			CL_FREE(pcprio->pc_cid, bufp);
			return (error);
		}
		CL_EXITCLASS(oldcid, clprocp);
		schedctl_set_cidpri(tp);
	}

	if ((error = CL_DOPRIO(tp, CRED(), 0, &prio)) != 0)
		return (error);

	if (pcprio->pc_op == PC_GETPRIO) {
		/*
		 * If we are not setting the priority, we should return the
		 * highest priority pertaining to any of the specified threads.
		 */
		if (prio > pcprio->pc_val) {
			pcprio->pc_cid = tp->t_cid;
			pcprio->pc_val = prio;
		}
	} else if (prio != pcprio->pc_val) {
		/*
		 * Try to change the priority of the thread.
		 */
		incr = pcprio->pc_val - prio;
		error = CL_DOPRIO(tp, CRED(), incr, &prio);
		schedctl_set_cidpri(tp);
	}

	return (error);
}

int
setprocprio(proc_t *pp, pcprio_t *pcprio)
{
	kthread_t *tp;
	int retval = 0;
	int error;

	ASSERT(MUTEX_HELD(&pidlock));
	mutex_enter(&pp->p_lock);

	if ((tp = pp->p_tlist) == NULL) {
		mutex_exit(&pp->p_lock);
		return (ESRCH);
	}

	/*
	 * Check permissions before changing the prio value.
	 */
	if (pcprio->pc_op == PC_SETPRIO) {
		if (!prochasprocperm(pp, curproc, CRED())) {
			mutex_exit(&pp->p_lock);
			return (EPERM);
		}
	}

	do {
		error = setthreadprio(pcprio, tp);
		if (error)
			retval = error;
	} while ((tp = tp->t_forw) != pp->p_tlist);

	mutex_exit(&pp->p_lock);
	return (retval);
}

/*
 * Set the class and priority of the specified LWP or set of processes.
 */
static int
doprio(procset_t *procset, pcprio_t *pcprio)
{
	int err_proc = 0;
	int err_thread = 0;
	int err = 0;

	/*
	 * Sanity check.
	 */
	if (pcprio->pc_op != PC_GETPRIO && pcprio->pc_op != PC_SETPRIO)
		return (EINVAL);
	if (pcprio->pc_op == PC_SETPRIO &&
	    (pcprio->pc_cid >= loaded_classes || pcprio->pc_cid < 1))
		return (EINVAL);

	/*
	 * If it is a PC_GETPRIO operation then set pc_val to the smallest
	 * possible prio value to help us find the highest priority
	 * pertaining to any of the specified processes.
	 */
	if (pcprio->pc_op == PC_GETPRIO)
		pcprio->pc_val = SHRT_MIN;

	if (procset->p_lidtype != P_LWPID ||
	    procset->p_ridtype != P_LWPID)
		err_proc = dotoprocs(procset, setprocprio, (char *)pcprio);

	if (procset->p_lidtype == P_LWPID || procset->p_ridtype == P_LWPID) {
		err_thread = dotolwp(procset, setthreadprio, (char *)pcprio);
		/*
		 * dotolwp() can return with p_lock held.  This is required
		 * for the priocntl GETPARMS case.  So, here we just release
		 * the p_lock.
		 */
		if (MUTEX_HELD(&curproc->p_lock))
			mutex_exit(&curproc->p_lock);

		/*
		 * If we were called for a single LWP, then ignore ESRCH
		 * returned by the previous dotoprocs() call.
		 */
		if (err_proc == ESRCH)
			err_proc = 0;
	}

	/*
	 * dotoprocs() ignores the init process if it is in the set, unless
	 * it was the only process found. We want to make sure init is not
	 * excluded if we're going PC_GETPRIO operation.
	 */
	if (pcprio->pc_op == PC_GETPRIO) {
		proc_t *initpp;

		mutex_enter(&pidlock);
		if ((initpp = prfind(P_INITPID)) != NULL) {
			mutex_enter(&initpp->p_lock);
			if (procinset(initpp, procset)) {
				mutex_exit(&initpp->p_lock);
				err = setprocprio(initpp, pcprio);
			} else {
				mutex_exit(&initpp->p_lock);
			}
		}
		mutex_exit(&pidlock);
	}

	/*
	 * We're returning the latest error here that we've got back from
	 * the setthreadprio() or setprocprio(). That is, err_thread and/or
	 * err_proc can be replaced by err.
	 */
	if (!err)
		err = err_thread ? err_thread : err_proc;

	return (err);
}