2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1996, 1997
5 * HD Associates, Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by HD Associates, Inc
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 /* ksched: Soft real time scheduling based on "rtprio". */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_posix.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <sys/kernel.h>
47 #include <sys/mutex.h>
49 #include <sys/posix4.h>
50 #include <sys/resource.h>
51 #include <sys/sched.h>
53 FEATURE(kposix_priority_scheduling, "POSIX P1003.1B realtime extensions");
55 /* ksched: Real-time extension to support POSIX priority scheduling. */
58 struct timespec rr_interval;
62 ksched_attach(struct ksched **p)
64 struct ksched *ksched;
66 ksched = malloc(sizeof(*ksched), M_P31B, M_WAITOK);
67 ksched->rr_interval.tv_sec = 0;
68 ksched->rr_interval.tv_nsec = 1000000000L / hz * sched_rr_interval();
74 ksched_detach(struct ksched *ks)
82 * XXX About priorities
84 * POSIX 1003.1b requires that numerically higher priorities be of
85 * higher priority. It also permits sched_setparam to be
86 * implementation defined for SCHED_OTHER. I don't like
87 * the notion of inverted priorites for normal processes when
88 * you can use "setpriority" for that.
92 /* Macros to convert between the unix (lower numerically is higher priority)
93 * and POSIX 1003.1b (higher numerically is higher priority)
96 #define p4prio_to_rtpprio(P) (RTP_PRIO_MAX - (P))
97 #define rtpprio_to_p4prio(P) (RTP_PRIO_MAX - (P))
99 #define p4prio_to_tsprio(P) ((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) - (P))
100 #define tsprio_to_p4prio(P) ((PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE) - (P))
102 /* These improve readability a bit for me:
104 #define P1B_PRIO_MIN rtpprio_to_p4prio(RTP_PRIO_MAX)
105 #define P1B_PRIO_MAX rtpprio_to_p4prio(RTP_PRIO_MIN)
108 getscheduler(struct ksched *ksched, struct thread *td, int *policy)
114 pri_to_rtp(td, &rtp);
117 *policy = SCHED_FIFO;
119 case RTP_PRIO_REALTIME:
123 *policy = SCHED_OTHER;
130 ksched_setparam(struct ksched *ksched,
131 struct thread *td, const struct sched_param *param)
135 e = getscheduler(ksched, td, &policy);
137 e = ksched_setscheduler(ksched, td, policy, param);
142 ksched_getparam(struct ksched *ksched, struct thread *td,
143 struct sched_param *param)
147 pri_to_rtp(td, &rtp);
148 if (RTP_PRIO_IS_REALTIME(rtp.type))
149 param->sched_priority = rtpprio_to_p4prio(rtp.prio);
151 if (PRI_MIN_TIMESHARE < rtp.prio)
153 * The interactive score has it to min realtime
154 * so we must show max (64 most likely).
156 param->sched_priority = PRI_MAX_TIMESHARE -
159 param->sched_priority = tsprio_to_p4prio(rtp.prio);
165 * XXX The priority and scheduler modifications should
166 * be moved into published interfaces in kern/kern_sync.
168 * The permissions to modify process p were checked in "p31b_proc()".
172 ksched_setscheduler(struct ksched *ksched, struct thread *td, int policy,
173 const struct sched_param *param)
182 if (param->sched_priority >= P1B_PRIO_MIN &&
183 param->sched_priority <= P1B_PRIO_MAX) {
184 rtp.prio = p4prio_to_rtpprio(param->sched_priority);
185 rtp.type = (policy == SCHED_FIFO) ? RTP_PRIO_FIFO :
187 rtp_to_pri(&rtp, td);
193 if (param->sched_priority >= 0 && param->sched_priority <=
194 (PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE)) {
195 rtp.type = RTP_PRIO_NORMAL;
196 rtp.prio = p4prio_to_tsprio(param->sched_priority);
197 rtp_to_pri(&rtp, td);
210 ksched_getscheduler(struct ksched *ksched, struct thread *td, int *policy)
213 return (getscheduler(ksched, td, policy));
216 /* ksched_yield: Yield the CPU. */
218 ksched_yield(struct ksched *ksched)
221 sched_relinquish(curthread);
226 ksched_get_priority_max(struct ksched *ksched, int policy, int *prio)
234 *prio = P1B_PRIO_MAX;
237 *prio = PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
247 ksched_get_priority_min(struct ksched *ksched, int policy, int *prio)
255 *prio = P1B_PRIO_MIN;
268 ksched_rr_get_interval(struct ksched *ksched, struct thread *td,
269 struct timespec *timespec)
272 *timespec = ksched->rr_interval;