1 /*- 2 * Copyright (c) 1996, 1997, 1998 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 /* p1003_1b: Real Time common code. 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/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/module.h> 46 #include <sys/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/syscallsubr.h> 49 #include <sys/sysctl.h> 50 #include <sys/sysent.h> 51 #include <sys/syslog.h> 52 #include <sys/sysproto.h> 53 54 #include <posix4/posix4.h> 55 56 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B"); 57 58 /* The system calls return ENOSYS if an entry is called that is 59 * not run-time supported. I am also logging since some programs 60 * start to use this when they shouldn't. That will be removed if annoying. 61 */ 62 int 63 syscall_not_present(struct thread *td, const char *s, struct nosys_args *uap) 64 { 65 log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n", 66 td->td_proc->p_comm, td->td_proc->p_pid, s); 67 68 /* a " return nosys(p, uap); " here causes a core dump. 69 */ 70 71 return ENOSYS; 72 } 73 74 #if !defined(_KPOSIX_PRIORITY_SCHEDULING) 75 76 /* Not configured but loadable via a module: 77 */ 78 79 static int 80 sched_attach(void) 81 { 82 return 0; 83 } 84 85 SYSCALL_NOT_PRESENT_GEN(sched_setparam) 86 SYSCALL_NOT_PRESENT_GEN(sched_getparam) 87 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler) 88 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler) 89 SYSCALL_NOT_PRESENT_GEN(sched_yield) 90 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max) 91 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min) 92 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval) 93 94 #else 95 96 /* Configured in kernel version: 97 */ 98 static struct ksched *ksched; 99 100 static int 101 sched_attach(void) 102 { 103 int ret = ksched_attach(&ksched); 104 105 if (ret == 0) 106 p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1); 107 108 return ret; 109 } 110 111 /* 112 * MPSAFE 113 */ 114 int 115 sched_setparam(struct thread *td, struct sched_setparam_args *uap) 116 { 117 struct thread *targettd; 118 struct proc *targetp; 119 int e; 120 struct sched_param sched_param; 121 122 e = copyin(uap->param, &sched_param, sizeof(sched_param)); 123 if (e) 124 return (e); 125 126 if (uap->pid == 0) { 127 targetp = td->td_proc; 128 targettd = td; 129 PROC_LOCK(targetp); 130 } else { 131 targetp = pfind(uap->pid); 132 if (targetp == NULL) { 133 e = ESRCH; 134 goto done2; 135 } 136 targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 137 } 138 139 e = p_cansched(td, targetp); 140 if (e == 0) { 141 e = ksched_setparam(&td->td_retval[0], ksched, targettd, 142 (const struct sched_param *)&sched_param); 143 } 144 PROC_UNLOCK(targetp); 145 done2: 146 return (e); 147 } 148 149 /* 150 * MPSAFE 151 */ 152 int 153 sched_getparam(struct thread *td, struct sched_getparam_args *uap) 154 { 155 int e; 156 struct sched_param sched_param; 157 struct thread *targettd; 158 struct proc *targetp; 159 160 if (uap->pid == 0) { 161 targetp = td->td_proc; 162 targettd = td; 163 PROC_LOCK(targetp); 164 } else { 165 targetp = pfind(uap->pid); 166 if (targetp == NULL) { 167 e = ESRCH; 168 goto done2; 169 } 170 targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 171 } 172 173 e = p_cansee(td, targetp); 174 if (e == 0) { 175 e = ksched_getparam(&td->td_retval[0], ksched, targettd, 176 &sched_param); 177 } 178 PROC_UNLOCK(targetp); 179 if (e == 0) 180 e = copyout(&sched_param, uap->param, sizeof(sched_param)); 181 done2: 182 return (e); 183 } 184 185 /* 186 * MPSAFE 187 */ 188 int 189 sched_setscheduler(struct thread *td, struct sched_setscheduler_args *uap) 190 { 191 int e; 192 struct sched_param sched_param; 193 struct thread *targettd; 194 struct proc *targetp; 195 196 /* Don't allow non root user to set a scheduler policy */ 197 if (suser(td) != 0) 198 return (EPERM); 199 200 e = copyin(uap->param, &sched_param, sizeof(sched_param)); 201 if (e) 202 return (e); 203 204 if (uap->pid == 0) { 205 targetp = td->td_proc; 206 targettd = td; 207 PROC_LOCK(targetp); 208 } else { 209 targetp = pfind(uap->pid); 210 if (targetp == NULL) { 211 e = ESRCH; 212 goto done2; 213 } 214 targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 215 } 216 217 e = p_cansched(td, targetp); 218 if (e == 0) { 219 e = ksched_setscheduler(&td->td_retval[0], ksched, targettd, 220 uap->policy, (const struct sched_param *)&sched_param); 221 } 222 PROC_UNLOCK(targetp); 223 done2: 224 return (e); 225 } 226 227 /* 228 * MPSAFE 229 */ 230 int 231 sched_getscheduler(struct thread *td, struct sched_getscheduler_args *uap) 232 { 233 int e; 234 struct thread *targettd; 235 struct proc *targetp; 236 237 if (uap->pid == 0) { 238 targetp = td->td_proc; 239 targettd = td; 240 PROC_LOCK(targetp); 241 } else { 242 targetp = pfind(uap->pid); 243 if (targetp == NULL) { 244 e = ESRCH; 245 goto done2; 246 } 247 targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 248 } 249 250 e = p_cansee(td, targetp); 251 if (e == 0) 252 e = ksched_getscheduler(&td->td_retval[0], ksched, targettd); 253 PROC_UNLOCK(targetp); 254 255 done2: 256 return (e); 257 } 258 259 /* 260 * MPSAFE 261 */ 262 int 263 sched_yield(struct thread *td, struct sched_yield_args *uap) 264 { 265 int error; 266 267 error = ksched_yield(&td->td_retval[0], ksched); 268 return (error); 269 } 270 271 /* 272 * MPSAFE 273 */ 274 int 275 sched_get_priority_max(struct thread *td, 276 struct sched_get_priority_max_args *uap) 277 { 278 int error; 279 280 error = ksched_get_priority_max(&td->td_retval[0], ksched, uap->policy); 281 return (error); 282 } 283 284 /* 285 * MPSAFE 286 */ 287 int 288 sched_get_priority_min(struct thread *td, 289 struct sched_get_priority_min_args *uap) 290 { 291 int error; 292 293 error = ksched_get_priority_min(&td->td_retval[0], ksched, uap->policy); 294 return (error); 295 } 296 297 /* 298 * MPSAFE 299 */ 300 int 301 sched_rr_get_interval(struct thread *td, 302 struct sched_rr_get_interval_args *uap) 303 { 304 struct timespec timespec; 305 int error; 306 307 error = kern_sched_rr_get_interval(td, uap->pid, ×pec); 308 if (error == 0) 309 error = copyout(×pec, uap->interval, sizeof(timespec)); 310 return (error); 311 } 312 313 int 314 kern_sched_rr_get_interval(struct thread *td, pid_t pid, 315 struct timespec *ts) 316 { 317 int e; 318 struct thread *targettd; 319 struct proc *targetp; 320 321 if (pid == 0) { 322 targettd = td; 323 targetp = td->td_proc; 324 PROC_LOCK(targetp); 325 } else { 326 targetp = pfind(pid); 327 if (targetp == NULL) 328 return (ESRCH); 329 330 targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */ 331 } 332 333 e = p_cansee(td, targetp); 334 if (e == 0) 335 e = ksched_rr_get_interval(&td->td_retval[0], ksched, targettd, 336 ts); 337 PROC_UNLOCK(targetp); 338 return (e); 339 } 340 341 #endif 342 343 static void 344 p31binit(void *notused) 345 { 346 (void) sched_attach(); 347 p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE); 348 } 349 350 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL); 351