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