sys/kern/kern_p1003_1b.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | /* * Copyright (c) 1996, 1997, 1998 * HD Associates, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by HD Associates, Inc * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/posix4/p1003_1b.c,v 1.5.2.2 2003/03/25 06:13:35 rwatson Exp $ */ /* p1003_1b: Real Time common code. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/sysent.h> #include <sys/malloc.h> #include <sys/posix4.h> #include <sys/proc.h> #include <sys/syslog.h> #include <sys/module.h> #include <sys/sysmsg.h> #include <sys/sysctl.h> #include <sys/unistd.h> MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B"); /* * p31b_proc: Return a proc struct corresponding to a pid to operate on. * * Enforce permission policy. * * The policy is the same as for sending signals except there * is no notion of process groups. * * pid == 0 means my process. * * This is disabled until I've got a permission gate in again: * only root can do this. */ #if 0 /* * This is stolen from CANSIGNAL in kern_sig: * * Can process p, with pcred pc, do "write flavor" operations to process q? */ #define CAN_AFFECT(p, cr, q) \ ((cr)->cr_uid == 0 || \ (cr)->cr_ruid == (q)->p_ucred->cr_ruid || \ (cr)->cr_uid == (q)->p_ucred->cr_ruid || \ (cr)->cr_ruid == (q)->p_ucred->cr_uid || \ (cr)->cr_uid == (q)->p_ucred->cr_uid) #else #define CAN_AFFECT(p, cr, q) ((cr)->cr_uid == 0) #endif #if !defined(_KPOSIX_PRIORITY_SCHEDULING) int syscall_not_present(const char *s); /* The system calls return ENOSYS if an entry is called that is * not run-time supported. I am also logging since some programs * start to use this when they shouldn't. That will be removed if annoying. */ int syscall_not_present(const char *s) { struct proc *p = curproc; log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n", p->p_comm, p->p_pid, s); /* a " return nosys(p, uap); " here causes a core dump. */ return ENOSYS; } /* Not configured but loadable via a module: */ static int sched_attach(void) { return 0; } #define SYSCALL_NOT_PRESENT_GEN(SC) \ int sys_##SC (struct sysmsg *sysmsg, const struct SC##_args *uap) \ { \ return syscall_not_present(#SC); \ } SYSCALL_NOT_PRESENT_GEN(sched_setparam) SYSCALL_NOT_PRESENT_GEN(sched_getparam) SYSCALL_NOT_PRESENT_GEN(sched_setscheduler) SYSCALL_NOT_PRESENT_GEN(sched_getscheduler) SYSCALL_NOT_PRESENT_GEN(sched_yield) SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max) SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min) SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval) #else /* * p31b_proc: Look up a proc from a PID. If proc is 0 it is * my own proc. * * Returns a held process in *pp. */ static int p31b_proc(pid_t pid, struct proc **pp) { int ret = 0; struct proc *p = curproc; struct proc *other_proc; if (pid == 0) { other_proc = p; if (other_proc) PHOLD(other_proc); } else { other_proc = pfind(pid); /* ref from pfind() */ } if (other_proc) { /* Enforce permission policy. */ if (CAN_AFFECT(p, p->p_ucred, other_proc)) { *pp = other_proc; lwkt_gettoken(&other_proc->p_token); } else { *pp = NULL; ret = EPERM; PRELE(other_proc); } } else { *pp = NULL; ret = ESRCH; } return ret; } static void p31b_proc_done(struct proc *other_proc) { if (other_proc) { lwkt_reltoken(&other_proc->p_token); PRELE(other_proc); } } /* Configured in kernel version: */ static struct ksched *ksched; static int sched_attach(void) { int ret = ksched_attach(&ksched); if (ret == 0) p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1); return ret; } int sys_sched_setparam(struct sysmsg *sysmsg, const struct sched_setparam_args *uap) { struct proc *p; struct lwp *lp; struct sched_param sched_param; int e; copyin(uap->param, &sched_param, sizeof(sched_param)); if ((e = p31b_proc(uap->pid, &p)) == 0) { lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */ if (lp) { LWPHOLD(lp); lwkt_gettoken(&lp->lwp_token); e = ksched_setparam(&sysmsg->sysmsg_reg, ksched, lp, (const struct sched_param *)&sched_param); lwkt_reltoken(&lp->lwp_token); LWPRELE(lp); } else { e = ESRCH; } p31b_proc_done(p); } return e; } int sys_sched_getparam(struct sysmsg *sysmsg, const struct sched_getparam_args *uap) { struct proc *targetp; struct lwp *lp; struct sched_param sched_param; int e; if ((e = p31b_proc(uap->pid, &targetp)) == 0) { lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */ if (lp) { LWPHOLD(lp); lwkt_gettoken(&lp->lwp_token); e = ksched_getparam(&sysmsg->sysmsg_reg, ksched, lp, &sched_param); lwkt_reltoken(&lp->lwp_token); LWPRELE(lp); } else { e = ESRCH; } p31b_proc_done(targetp); } if (e == 0) copyout(&sched_param, uap->param, sizeof(sched_param)); return e; } int sys_sched_setscheduler(struct sysmsg *sysmsg, const struct sched_setscheduler_args *uap) { struct proc *p; struct lwp *lp; int e; struct sched_param sched_param; copyin(uap->param, &sched_param, sizeof(sched_param)); if ((e = p31b_proc(uap->pid, &p)) == 0) { lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */ if (lp) { LWPHOLD(lp); lwkt_gettoken(&lp->lwp_token); e = ksched_setscheduler(&sysmsg->sysmsg_reg, ksched, lp, uap->policy, (const struct sched_param *)&sched_param); lwkt_reltoken(&lp->lwp_token); LWPRELE(lp); } else { e = ESRCH; } p31b_proc_done(p); } return e; } int sys_sched_getscheduler(struct sysmsg *sysmsg, const struct sched_getscheduler_args *uap) { struct proc *targetp; struct lwp *lp; int e; if ((e = p31b_proc(uap->pid, &targetp)) == 0) { lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */ if (lp) { LWPHOLD(lp); lwkt_gettoken(&lp->lwp_token); e = ksched_getscheduler(&sysmsg->sysmsg_reg, ksched, lp); lwkt_reltoken(&lp->lwp_token); LWPRELE(lp); } else { e = ESRCH; } p31b_proc_done(targetp); } return e; } /* * MPSAFE */ int sys_sched_yield(struct sysmsg *sysmsg, const struct sched_yield_args *uap) { return ksched_yield(&sysmsg->sysmsg_reg, ksched); } /* * MPSAFE */ int sys_sched_get_priority_max(struct sysmsg *sysmsg, const struct sched_get_priority_max_args *uap) { return ksched_get_priority_max(&sysmsg->sysmsg_reg, ksched, uap->policy); } /* * MPSAFE */ int sys_sched_get_priority_min(struct sysmsg *sysmsg, const struct sched_get_priority_min_args *uap) { return ksched_get_priority_min(&sysmsg->sysmsg_reg, ksched, uap->policy); } int sys_sched_rr_get_interval(struct sysmsg *sysmsg, const struct sched_rr_get_interval_args *uap) { int e; struct proc *p; struct lwp *lp; struct timespec ts; if ((e = p31b_proc(uap->pid, &p)) == 0) { lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */ if (lp) { LWPHOLD(lp); lwkt_gettoken(&lp->lwp_token); e = ksched_rr_get_interval(&sysmsg->sysmsg_reg, ksched, lp, &ts); if (e == 0) e = copyout(&ts, uap->interval, sizeof(ts)); lwkt_reltoken(&lp->lwp_token); LWPRELE(lp); } else { e = ESRCH; } p31b_proc_done(p); } return e; } #endif static void p31binit(void *notused) { sched_attach(); p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, -1); p31b_setcfg(CTL_P1003_1B_FSYNC, _POSIX_FSYNC); p31b_setcfg(CTL_P1003_1B_MAPPED_FILES, _POSIX_MAPPED_FILES); p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING); p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE); p31b_setcfg(CTL_P1003_1B_SEMAPHORES, _POSIX_SEMAPHORES); p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, 256); p31b_setcfg(CTL_P1003_1B_SHARED_MEMORY_OBJECTS, _POSIX_SHARED_MEMORY_OBJECTS); #if 0 /* XXX missing */ p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO); p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0); p31b_setcfg(CTL_P1003_1B_AIO_MAX, 0); p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, 0); p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, 0); p31b_setcfg(CTL_P1003_1B_MEMLOCK, 0); p31b_setcfg(CTL_P1003_1B_MEMLOCK_RANGE, 0); p31b_setcfg(CTL_P1003_1B_MEMORY_PROTECTION, 0); p31b_setcfg(CTL_P1003_1B_PRIORITIZED_IO, _POSIX_PRIORITIZED_IO); p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS); p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1); p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, 0); p31b_setcfg(CTL_P1003_1B_SYNCHRONIZED_IO, _POSIX_SYNCHRONIZED_IO); p31b_setcfg(CTL_P1003_1B_TIMERS, _POSIX_TIMERS); p31b_setcfg(CTL_P1003_1B_TIMER_MAX, 0); #endif } SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL); |