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/proc.h> 41 #include <sys/kernel.h> 42 #include <sys/resource.h> 43 #include <machine/cpu.h> /* For need_resched */ 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 *p) 66 { 67 p31b_free(p); 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 proc *p) 98 { 99 int e = 0; 100 101 switch (p->p_rtprio.type) 102 { 103 case RTP_PRIO_FIFO: 104 *ret = SCHED_FIFO; 105 break; 106 107 case RTP_PRIO_REALTIME: 108 *ret = SCHED_RR; 109 break; 110 111 default: 112 *ret = SCHED_OTHER; 113 break; 114 } 115 116 return e; 117 } 118 119 int ksched_setparam(register_t *ret, struct ksched *ksched, 120 struct proc *p, const struct sched_param *param) 121 { 122 register_t policy; 123 int e; 124 125 e = getscheduler(&policy, ksched, p); 126 127 if (e == 0) 128 { 129 if (policy == SCHED_OTHER) 130 e = EINVAL; 131 else 132 e = ksched_setscheduler(ret, ksched, p, policy, param); 133 } 134 135 return e; 136 } 137 138 int ksched_getparam(register_t *ret, struct ksched *ksched, 139 struct proc *p, struct sched_param *param) 140 { 141 if (RTP_PRIO_IS_REALTIME(p->p_rtprio.type)) 142 param->sched_priority = rtpprio_to_p4prio(p->p_rtprio.prio); 143 144 return 0; 145 } 146 147 /* 148 * XXX The priority and scheduler modifications should 149 * be moved into published interfaces in kern/kern_sync. 150 * 151 * The permissions to modify process p were checked in "p31b_proc()". 152 * 153 */ 154 int ksched_setscheduler(register_t *ret, struct ksched *ksched, 155 struct proc *p, int policy, const struct sched_param *param) 156 { 157 int e = 0; 158 struct rtprio rtp; 159 160 switch(policy) 161 { 162 case SCHED_RR: 163 case SCHED_FIFO: 164 165 if (param->sched_priority >= P1B_PRIO_MIN && 166 param->sched_priority <= P1B_PRIO_MAX) 167 { 168 rtp.prio = p4prio_to_rtpprio(param->sched_priority); 169 rtp.type = (policy == SCHED_FIFO) 170 ? RTP_PRIO_FIFO : RTP_PRIO_REALTIME; 171 172 p->p_rtprio = rtp; 173 need_resched(); 174 } 175 else 176 e = EPERM; 177 178 179 break; 180 181 case SCHED_OTHER: 182 { 183 rtp.type = RTP_PRIO_NORMAL; 184 rtp.prio = p4prio_to_rtpprio(param->sched_priority); 185 p->p_rtprio = rtp; 186 187 /* XXX Simply revert to whatever we had for last 188 * normal scheduler priorities. 189 * This puts a requirement 190 * on the scheduling code: You must leave the 191 * scheduling info alone. 192 */ 193 need_resched(); 194 } 195 break; 196 } 197 198 return e; 199 } 200 201 int ksched_getscheduler(register_t *ret, struct ksched *ksched, struct proc *p) 202 { 203 return getscheduler(ret, ksched, p); 204 } 205 206 /* ksched_yield: Yield the CPU. 207 */ 208 int ksched_yield(register_t *ret, struct ksched *ksched) 209 { 210 need_resched(); 211 return 0; 212 } 213 214 int ksched_get_priority_max(register_t*ret, struct ksched *ksched, int policy) 215 { 216 int e = 0; 217 218 switch (policy) 219 { 220 case SCHED_FIFO: 221 case SCHED_RR: 222 *ret = RTP_PRIO_MAX; 223 break; 224 225 case SCHED_OTHER: 226 *ret = PRIO_MAX; 227 break; 228 229 default: 230 e = EINVAL; 231 } 232 233 return e; 234 } 235 236 int ksched_get_priority_min(register_t *ret, struct ksched *ksched, int policy) 237 { 238 int e = 0; 239 240 switch (policy) 241 { 242 case SCHED_FIFO: 243 case SCHED_RR: 244 *ret = P1B_PRIO_MIN; 245 break; 246 247 case SCHED_OTHER: 248 *ret = PRIO_MIN; 249 break; 250 251 default: 252 e = EINVAL; 253 } 254 255 return e; 256 } 257 258 int ksched_rr_get_interval(register_t *ret, struct ksched *ksched, 259 struct proc *p, struct timespec *timespec) 260 { 261 *timespec = ksched->rr_interval; 262 263 return 0; 264 } 265