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