xref: /freebsd/sys/kern/ksched.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1 /*-
2  * Copyright (c) 1996, 1997
3  *	HD Associates, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by HD Associates, Inc
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /* ksched: Soft real time scheduling based on "rtprio".
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_posix.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/posix4.h>
47 #include <sys/resource.h>
48 #include <sys/sched.h>
49 
50 /* ksched: Real-time extension to support POSIX priority scheduling.
51  */
52 
53 struct ksched {
54 	struct timespec rr_interval;
55 };
56 
57 int
58 ksched_attach(struct ksched **p)
59 {
60 	struct ksched *ksched= p31b_malloc(sizeof(*ksched));
61 
62 	ksched->rr_interval.tv_sec = 0;
63 	ksched->rr_interval.tv_nsec = 1000000000L / sched_rr_interval();
64 
65 	*p = ksched;
66 	return 0;
67 }
68 
69 int
70 ksched_detach(struct ksched *ks)
71 {
72 	p31b_free(ks);
73 
74 	return 0;
75 }
76 
77 /*
78  * XXX About priorities
79  *
80  *	POSIX 1003.1b requires that numerically higher priorities be of
81  *	higher priority.  It also permits sched_setparam to be
82  *	implementation defined for SCHED_OTHER.  I don't like
83  *	the notion of inverted priorites for normal processes when
84  *  you can use "setpriority" for that.
85  *
86  *	I'm rejecting sched_setparam for SCHED_OTHER with EINVAL.
87  */
88 
89 /* Macros to convert between the unix (lower numerically is higher priority)
90  * and POSIX 1003.1b (higher numerically is higher priority)
91  */
92 
93 #define p4prio_to_rtpprio(P) (RTP_PRIO_MAX - (P))
94 #define rtpprio_to_p4prio(P) (RTP_PRIO_MAX - (P))
95 
96 /* These improve readability a bit for me:
97  */
98 #define P1B_PRIO_MIN rtpprio_to_p4prio(RTP_PRIO_MAX)
99 #define P1B_PRIO_MAX rtpprio_to_p4prio(RTP_PRIO_MIN)
100 
101 static __inline int
102 getscheduler(struct ksched *ksched, struct thread *td, int *policy)
103 {
104 	struct rtprio rtp;
105 	int e = 0;
106 
107 	pri_to_rtp(td, &rtp);
108 	switch (rtp.type)
109 	{
110 		case RTP_PRIO_FIFO:
111 		*policy = SCHED_FIFO;
112 		break;
113 
114 		case RTP_PRIO_REALTIME:
115 		*policy = SCHED_RR;
116 		break;
117 
118 		default:
119 		*policy = SCHED_OTHER;
120 		break;
121 	}
122 
123 	return e;
124 }
125 
126 int
127 ksched_setparam(struct ksched *ksched,
128     struct thread *td, const struct sched_param *param)
129 {
130 	int policy;
131 	int e;
132 
133 	e = getscheduler(ksched, td, &policy);
134 
135 	if (e == 0)
136 	{
137 		if (policy == SCHED_OTHER)
138 			e = EINVAL;
139 		else
140 			e = ksched_setscheduler(ksched, td, policy, param);
141 	}
142 
143 	return e;
144 }
145 
146 int
147 ksched_getparam(struct ksched *ksched,
148     struct thread *td, struct sched_param *param)
149 {
150 	struct rtprio rtp;
151 
152 	pri_to_rtp(td, &rtp);
153 	if (RTP_PRIO_IS_REALTIME(rtp.type))
154 		param->sched_priority = rtpprio_to_p4prio(rtp.prio);
155 
156 	return 0;
157 }
158 
159 /*
160  * XXX The priority and scheduler modifications should
161  *     be moved into published interfaces in kern/kern_sync.
162  *
163  * The permissions to modify process p were checked in "p31b_proc()".
164  *
165  */
166 int
167 ksched_setscheduler(struct ksched *ksched,
168     struct thread *td, int policy, const struct sched_param *param)
169 {
170 	int e = 0;
171 	struct rtprio rtp;
172 
173 	switch(policy)
174 	{
175 		case SCHED_RR:
176 		case SCHED_FIFO:
177 
178 		if (param->sched_priority >= P1B_PRIO_MIN &&
179 		    param->sched_priority <= P1B_PRIO_MAX)
180 		{
181 			rtp.prio = p4prio_to_rtpprio(param->sched_priority);
182 			rtp.type = (policy == SCHED_FIFO)
183 				? RTP_PRIO_FIFO : RTP_PRIO_REALTIME;
184 
185 			rtp_to_pri(&rtp, td);
186 		}
187 		else
188 			e = EPERM;
189 
190 
191 		break;
192 
193 		case SCHED_OTHER:
194 		{
195 			rtp.type = RTP_PRIO_NORMAL;
196 			rtp.prio = p4prio_to_rtpprio(param->sched_priority);
197 			rtp_to_pri(&rtp, td);
198 		}
199 		break;
200 
201 		default:
202 			e = EINVAL;
203 			break;
204 	}
205 
206 	return e;
207 }
208 
209 int
210 ksched_getscheduler(struct ksched *ksched, struct thread *td, int *policy)
211 {
212 	return getscheduler(ksched, td, policy);
213 }
214 
215 /* ksched_yield: Yield the CPU.
216  */
217 int
218 ksched_yield(struct ksched *ksched)
219 {
220 	sched_relinquish(curthread);
221 	return 0;
222 }
223 
224 int
225 ksched_get_priority_max(struct ksched *ksched, int policy, int *prio)
226 {
227 	int e = 0;
228 
229 	switch (policy)
230 	{
231 		case SCHED_FIFO:
232 		case SCHED_RR:
233 		*prio = RTP_PRIO_MAX;
234 		break;
235 
236 		case SCHED_OTHER:
237 		*prio = PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
238 		break;
239 
240 		default:
241 		e = EINVAL;
242 	}
243 
244 	return e;
245 }
246 
247 int
248 ksched_get_priority_min(struct ksched *ksched, int policy, int *prio)
249 {
250 	int e = 0;
251 
252 	switch (policy)
253 	{
254 		case SCHED_FIFO:
255 		case SCHED_RR:
256 		*prio = P1B_PRIO_MIN;
257 		break;
258 
259 		case SCHED_OTHER:
260 		*prio = 0;
261 		break;
262 
263 		default:
264 		e = EINVAL;
265 	}
266 
267 	return e;
268 }
269 
270 int
271 ksched_rr_get_interval(struct ksched *ksched,
272    struct thread *td, struct timespec *timespec)
273 {
274 	*timespec = ksched->rr_interval;
275 
276 	return 0;
277 }
278