xref: /freebsd/sys/kern/ksched.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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  * $FreeBSD$
33  */
34 
35 /* ksched: Soft real time scheduling based on "rtprio".
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/resource.h>
44 
45 #include <posix4/posix4.h>
46 
47 /* ksched: Real-time extension to support POSIX priority scheduling.
48  */
49 
50 struct ksched {
51 	struct timespec rr_interval;
52 };
53 
54 int ksched_attach(struct ksched **p)
55 {
56 	struct ksched *ksched= p31b_malloc(sizeof(*ksched));
57 
58 	ksched->rr_interval.tv_sec = 0;
59 	ksched->rr_interval.tv_nsec = 1000000000L / roundrobin_interval();
60 
61 	*p = ksched;
62 	return 0;
63 }
64 
65 int ksched_detach(struct ksched *ks)
66 {
67 	p31b_free(ks);
68 
69 	return 0;
70 }
71 
72 /*
73  * XXX About priorities
74  *
75  *	POSIX 1003.1b requires that numerically higher priorities be of
76  *	higher priority.  It also permits sched_setparam to be
77  *	implementation defined for SCHED_OTHER.  I don't like
78  *	the notion of inverted priorites for normal processes when
79  *  you can use "setpriority" for that.
80  *
81  *	I'm rejecting sched_setparam for SCHED_OTHER with EINVAL.
82  */
83 
84 /* Macros to convert between the unix (lower numerically is higher priority)
85  * and POSIX 1003.1b (higher numerically is higher priority)
86  */
87 
88 #define p4prio_to_rtpprio(P) (RTP_PRIO_MAX - (P))
89 #define rtpprio_to_p4prio(P) (RTP_PRIO_MAX - (P))
90 
91 /* These improve readability a bit for me:
92  */
93 #define P1B_PRIO_MIN rtpprio_to_p4prio(RTP_PRIO_MAX)
94 #define P1B_PRIO_MAX rtpprio_to_p4prio(RTP_PRIO_MIN)
95 
96 static __inline int
97 getscheduler(register_t *ret, struct ksched *ksched, struct thread *td)
98 {
99 	struct rtprio rtp;
100 	int e = 0;
101 
102 	mtx_lock_spin(&sched_lock);
103 	pri_to_rtp(td->td_ksegrp, &rtp);
104 	mtx_unlock_spin(&sched_lock);
105 	switch (rtp.type)
106 	{
107 		case RTP_PRIO_FIFO:
108 		*ret = SCHED_FIFO;
109 		break;
110 
111 		case RTP_PRIO_REALTIME:
112 		*ret = SCHED_RR;
113 		break;
114 
115 		default:
116 		*ret = SCHED_OTHER;
117 		break;
118 	}
119 
120 	return e;
121 }
122 
123 int ksched_setparam(register_t *ret, struct ksched *ksched,
124 	struct thread *td, const struct sched_param *param)
125 {
126 	register_t policy;
127 	int e;
128 
129 	e = getscheduler(&policy, ksched, td);
130 
131 	if (e == 0)
132 	{
133 		if (policy == SCHED_OTHER)
134 			e = EINVAL;
135 		else
136 			e = ksched_setscheduler(ret, ksched, td, policy, param);
137 	}
138 
139 	return e;
140 }
141 
142 int ksched_getparam(register_t *ret, struct ksched *ksched,
143 	struct thread *td, struct sched_param *param)
144 {
145 	struct rtprio rtp;
146 
147 	mtx_lock_spin(&sched_lock);
148 	pri_to_rtp(td->td_ksegrp, &rtp);
149 	mtx_unlock_spin(&sched_lock);
150 	if (RTP_PRIO_IS_REALTIME(rtp.type))
151 		param->sched_priority = rtpprio_to_p4prio(rtp.prio);
152 
153 	return 0;
154 }
155 
156 /*
157  * XXX The priority and scheduler modifications should
158  *     be moved into published interfaces in kern/kern_sync.
159  *
160  * The permissions to modify process p were checked in "p31b_proc()".
161  *
162  */
163 int ksched_setscheduler(register_t *ret, struct ksched *ksched,
164 	struct thread *td, int policy, const struct sched_param *param)
165 {
166 	int e = 0;
167 	struct rtprio rtp;
168 	struct ksegrp *kg = td->td_ksegrp;
169 
170 	switch(policy)
171 	{
172 		case SCHED_RR:
173 		case SCHED_FIFO:
174 
175 		if (param->sched_priority >= P1B_PRIO_MIN &&
176 		param->sched_priority <= P1B_PRIO_MAX)
177 		{
178 			rtp.prio = p4prio_to_rtpprio(param->sched_priority);
179 			rtp.type = (policy == SCHED_FIFO)
180 				? RTP_PRIO_FIFO : RTP_PRIO_REALTIME;
181 
182 			mtx_lock_spin(&sched_lock);
183 			rtp_to_pri(&rtp, kg);
184 			FOREACH_THREAD_IN_GROUP(kg, td) { /* XXXKSE */
185 				if (td->td_state == TDS_RUNNING) {
186 					td->td_kse->ke_flags |= KEF_NEEDRESCHED;
187 				} else if (td->td_state == TDS_RUNQ) {
188 					if (td->td_priority > kg->kg_user_pri) {
189 						remrunqueue(td);
190 						td->td_priority =
191 						    kg->kg_user_pri;
192 						setrunqueue(td);
193 					}
194 				}
195 			}
196 			mtx_unlock_spin(&sched_lock);
197 		}
198 		else
199 			e = EPERM;
200 
201 
202 		break;
203 
204 		case SCHED_OTHER:
205 		{
206 			rtp.type = RTP_PRIO_NORMAL;
207 			rtp.prio = p4prio_to_rtpprio(param->sched_priority);
208 			mtx_lock_spin(&sched_lock);
209 			rtp_to_pri(&rtp, kg);
210 
211 			/* XXX Simply revert to whatever we had for last
212 			 *     normal scheduler priorities.
213 			 *     This puts a requirement
214 			 *     on the scheduling code: You must leave the
215 			 *     scheduling info alone.
216 			 */
217 			FOREACH_THREAD_IN_GROUP(kg, td) {
218 				if (td->td_state == TDS_RUNNING) {
219 					td->td_kse->ke_flags |= KEF_NEEDRESCHED;
220 				} else if (td->td_state == TDS_RUNQ) {
221 					if (td->td_priority > kg->kg_user_pri) {
222 						remrunqueue(td);
223 						td->td_priority =
224 						    kg->kg_user_pri;
225 						setrunqueue(td);
226 					}
227 				}
228 
229 			}
230 			mtx_unlock_spin(&sched_lock);
231 		}
232 		break;
233 	}
234 
235 	return e;
236 }
237 
238 int ksched_getscheduler(register_t *ret, struct ksched *ksched, struct thread *td)
239 {
240 	return getscheduler(ret, ksched, td);
241 }
242 
243 /* ksched_yield: Yield the CPU.
244  */
245 int ksched_yield(register_t *ret, struct ksched *ksched)
246 {
247 	mtx_lock_spin(&sched_lock);
248 	curthread->td_kse->ke_flags |= KEF_NEEDRESCHED;
249 	mtx_unlock_spin(&sched_lock);
250 	return 0;
251 }
252 
253 int ksched_get_priority_max(register_t*ret, struct ksched *ksched, int policy)
254 {
255 	int e = 0;
256 
257 	switch (policy)
258 	{
259 		case SCHED_FIFO:
260 		case SCHED_RR:
261 		*ret = RTP_PRIO_MAX;
262 		break;
263 
264 		case SCHED_OTHER:
265 		*ret =  PRIO_MAX;
266 		break;
267 
268 		default:
269 		e = EINVAL;
270 	}
271 
272 	return e;
273 }
274 
275 int ksched_get_priority_min(register_t *ret, struct ksched *ksched, int policy)
276 {
277 	int e = 0;
278 
279 	switch (policy)
280 	{
281 		case SCHED_FIFO:
282 		case SCHED_RR:
283 		*ret = P1B_PRIO_MIN;
284 		break;
285 
286 		case SCHED_OTHER:
287 		*ret =  PRIO_MIN;
288 		break;
289 
290 		default:
291 		e = EINVAL;
292 	}
293 
294 	return e;
295 }
296 
297 int ksched_rr_get_interval(register_t *ret, struct ksched *ksched,
298 	struct thread *td, struct timespec *timespec)
299 {
300 	*timespec = ksched->rr_interval;
301 
302 	return 0;
303 }
304