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 * $FreeBSD: src/sys/posix4/p1003_1b.c,v 1.5.2.2 2003/03/25 06:13:35 rwatson Exp $ 33 */ 34 35 /* p1003_1b: Real Time common code. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/sysent.h> 42 #include <sys/posix4.h> 43 #include <sys/proc.h> 44 #include <sys/syslog.h> 45 #include <sys/module.h> 46 #include <sys/sysproto.h> 47 #include <sys/sysctl.h> 48 #include <sys/unistd.h> 49 50 #include <sys/mplock2.h> 51 52 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B"); 53 54 /* p31b_proc: Return a proc struct corresponding to a pid to operate on. 55 * 56 * Enforce permission policy. 57 * 58 * The policy is the same as for sending signals except there 59 * is no notion of process groups. 60 * 61 * pid == 0 means my process. 62 * 63 * This is disabled until I've got a permission gate in again: 64 * only root can do this. 65 */ 66 67 #if 0 68 /* 69 * This is stolen from CANSIGNAL in kern_sig: 70 * 71 * Can process p, with pcred pc, do "write flavor" operations to process q? 72 */ 73 #define CAN_AFFECT(p, cr, q) \ 74 ((cr)->cr_uid == 0 || \ 75 (cr)->cr_ruid == (q)->p_ucred->cr_ruid || \ 76 (cr)->cr_uid == (q)->p_ucred->cr_ruid || \ 77 (cr)->cr_ruid == (q)->p_ucred->cr_uid || \ 78 (cr)->cr_uid == (q)->p_ucred->cr_uid) 79 #else 80 #define CAN_AFFECT(p, cr, q) ((cr)->cr_uid == 0) 81 #endif 82 83 /* 84 * p31b_proc: Look up a proc from a PID. If proc is 0 it is 85 * my own proc. 86 */ 87 int p31b_proc(struct proc *p, pid_t pid, struct proc **pp) 88 { 89 int ret = 0; 90 struct proc *other_proc = NULL; 91 92 if (pid == 0) { 93 other_proc = p; 94 if (other_proc) 95 PHOLD(other_proc); 96 } else { 97 other_proc = pfind(pid); 98 /* ref from pfind() */ 99 } 100 101 if (other_proc) { 102 /* Enforce permission policy. 103 */ 104 if (CAN_AFFECT(p, p->p_ucred, other_proc)) 105 *pp = other_proc; 106 else 107 ret = EPERM; 108 PRELE(other_proc); 109 } else { 110 ret = ESRCH; 111 } 112 113 return ret; 114 } 115 116 117 #if !defined(_KPOSIX_PRIORITY_SCHEDULING) 118 119 int syscall_not_present(const char *s); 120 121 /* The system calls return ENOSYS if an entry is called that is 122 * not run-time supported. I am also logging since some programs 123 * start to use this when they shouldn't. That will be removed if annoying. 124 */ 125 int syscall_not_present(const char *s) 126 { 127 struct proc *p = curproc; 128 log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n", 129 p->p_comm, p->p_pid, s); 130 131 /* a " return nosys(p, uap); " here causes a core dump. 132 */ 133 134 return ENOSYS; 135 } 136 137 /* Not configured but loadable via a module: 138 */ 139 140 static int sched_attach(void) 141 { 142 return 0; 143 } 144 145 #define SYSCALL_NOT_PRESENT_GEN(SC) \ 146 int sys_##SC (struct SC##_args *uap) \ 147 { \ 148 return syscall_not_present(#SC); \ 149 } 150 151 SYSCALL_NOT_PRESENT_GEN(sched_setparam) 152 SYSCALL_NOT_PRESENT_GEN(sched_getparam) 153 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler) 154 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler) 155 SYSCALL_NOT_PRESENT_GEN(sched_yield) 156 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max) 157 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min) 158 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval) 159 160 #else 161 162 /* Configured in kernel version: 163 */ 164 static struct ksched *ksched; 165 166 static int sched_attach(void) 167 { 168 int ret = ksched_attach(&ksched); 169 170 if (ret == 0) 171 p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1); 172 173 return ret; 174 } 175 176 /* 177 * MPALMOSTSAFE 178 */ 179 int 180 sys_sched_setparam(struct sched_setparam_args *uap) 181 { 182 struct proc *p = curproc; 183 struct lwp *lp; 184 int e; 185 struct sched_param sched_param; 186 187 copyin(uap->param, &sched_param, sizeof(sched_param)); 188 189 get_mplock(); 190 if ((e = p31b_proc(p, uap->pid, &p)) == 0) { 191 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */ 192 e = ksched_setparam(&uap->sysmsg_reg, ksched, lp, 193 (const struct sched_param *)&sched_param); 194 } 195 rel_mplock(); 196 return e; 197 } 198 199 /* 200 * MPALMOSTSAFE 201 */ 202 int 203 sys_sched_getparam(struct sched_getparam_args *uap) 204 { 205 struct proc *p = curproc; 206 struct proc *targetp; 207 struct lwp *lp; 208 struct sched_param sched_param; 209 int e; 210 211 get_mplock(); 212 if (uap->pid != 0 && uap->pid != p->p_pid) { 213 e = p31b_proc(p, uap->pid, &targetp); 214 if (e) 215 goto done; 216 } else { 217 targetp = p; 218 } 219 220 lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */ 221 e = ksched_getparam(&uap->sysmsg_reg, ksched, lp, &sched_param); 222 done: 223 rel_mplock(); 224 if (e == 0) 225 copyout(&sched_param, uap->param, sizeof(sched_param)); 226 return e; 227 } 228 229 /* 230 * MPALMOSTSAFE 231 */ 232 int 233 sys_sched_setscheduler(struct sched_setscheduler_args *uap) 234 { 235 struct proc *p = curproc; 236 struct lwp *lp; 237 int e; 238 struct sched_param sched_param; 239 240 copyin(uap->param, &sched_param, sizeof(sched_param)); 241 242 get_mplock(); 243 if ((e = p31b_proc(p, uap->pid, &p)) == 0) { 244 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */ 245 e = ksched_setscheduler(&uap->sysmsg_reg, ksched, lp, 246 uap->policy, 247 (const struct sched_param *)&sched_param); 248 } 249 rel_mplock(); 250 return e; 251 } 252 253 /* 254 * MPALMOSTSAFE 255 */ 256 int 257 sys_sched_getscheduler(struct sched_getscheduler_args *uap) 258 { 259 struct proc *p = curproc; 260 struct proc *targetp; 261 struct lwp *lp; 262 int e; 263 264 get_mplock(); 265 if (uap->pid != 0 && uap->pid != p->p_pid) { 266 e = p31b_proc(p, uap->pid, &targetp); 267 if (e) 268 goto done; 269 } else { 270 targetp = p; 271 } 272 273 lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */ 274 e = ksched_getscheduler(&uap->sysmsg_reg, ksched, lp); 275 done: 276 rel_mplock(); 277 return e; 278 } 279 280 /* 281 * MPSAFE 282 */ 283 int 284 sys_sched_yield(struct sched_yield_args *uap) 285 { 286 return ksched_yield(&uap->sysmsg_reg, ksched); 287 } 288 289 /* 290 * MPSAFE 291 */ 292 int 293 sys_sched_get_priority_max(struct sched_get_priority_max_args *uap) 294 { 295 return ksched_get_priority_max(&uap->sysmsg_reg, ksched, uap->policy); 296 } 297 298 /* 299 * MPSAFE 300 */ 301 int 302 sys_sched_get_priority_min(struct sched_get_priority_min_args *uap) 303 { 304 return ksched_get_priority_min(&uap->sysmsg_reg, ksched, uap->policy); 305 } 306 307 /* 308 * MPALMOSTSAFE 309 */ 310 int 311 sys_sched_rr_get_interval(struct sched_rr_get_interval_args *uap) 312 { 313 int e; 314 struct proc *p = curproc; 315 struct lwp *lp = curthread->td_lwp; 316 struct timespec ts; 317 318 get_mplock(); 319 if ((e = p31b_proc(p, uap->pid, &p)) == 0) { 320 e = ksched_rr_get_interval(&uap->sysmsg_reg, ksched, lp, &ts); 321 if (e == 0) 322 e = copyout(&ts, uap->interval, sizeof(ts)); 323 } 324 rel_mplock(); 325 return e; 326 } 327 328 #endif 329 330 static void 331 p31binit(void *notused) 332 { 333 (void) sched_attach(); 334 p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE); 335 p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, -1); 336 p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING); 337 } 338 339 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL); 340