__P(); not anymore in this directory
[dragonfly.git] / sys / kern / kern_p1003_1b.c
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  * $DragonFly: src/sys/kern/kern_p1003_1b.c,v 1.6 2003/08/07 21:17:19 dillon Exp $
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>
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
49 #include "posix4.h"
50
51 MALLOC_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  */
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)
78 #else
79 #define CAN_AFFECT(p, cr, q) ((cr)->cr_uid == 0)
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  */
86 int 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                  */
100                 if (CAN_AFFECT(p, p->p_ucred, other_proc))
101                         *pp = other_proc;
102                 else
103                         ret = EPERM;
104         }
105         else
106                 ret = ESRCH;
107
108         return ret;
109 }
110
111 /* The system calls return ENOSYS if an entry is called that is
112  * not run-time supported.  I am also logging since some programs
113  * start to use this when they shouldn't.  That will be removed if annoying.
114  */
115 int
116 syscall_not_present(struct proc *p, const char *s, struct nosys_args *uap)
117 {
118         log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
119                         p->p_comm, p->p_pid, s);
120
121         /* a " return nosys(p, uap); " here causes a core dump.
122          */
123
124         return ENOSYS;
125 }
126
127 #if !defined(_KPOSIX_PRIORITY_SCHEDULING)
128
129 /* Not configured but loadable via a module:
130  */
131
132 static int sched_attach(void)
133 {
134         return 0;
135 }
136
137 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
138 SYSCALL_NOT_PRESENT_GEN(sched_getparam)
139 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
140 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
141 SYSCALL_NOT_PRESENT_GEN(sched_yield)
142 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
143 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
144 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
145
146 #else
147
148 /* Configured in kernel version:
149  */
150 static struct ksched *ksched;
151
152 static int sched_attach(void)
153 {
154         int ret = ksched_attach(&ksched);
155
156         if (ret == 0)
157                 p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
158
159         return ret;
160 }
161
162 int
163 sched_setparam(struct sched_setparam_args *uap)
164 {
165         struct proc *p = curproc;
166         int e;
167
168         struct sched_param sched_param;
169         copyin(uap->param, &sched_param, sizeof(sched_param));
170
171         if ((e = p31b_proc(p, uap->pid, &p)) == 0) {
172                 e = ksched_setparam(&uap->sysmsg_result, ksched, p,
173                     (const struct sched_param *)&sched_param);
174         }
175         return e;
176 }
177
178 int
179 sched_getparam(struct sched_getparam_args *uap)
180 {
181         struct proc *p = curproc;
182         struct proc *targetp;
183         struct sched_param sched_param;
184         int e;
185  
186         if (uap->pid != 0 && uap->pid != p->p_pid) {
187                 e = p31b_proc(p, uap->pid, &targetp);
188                 if (e)
189                         return e;
190         } else {
191                 targetp = p;
192         }
193  
194         e = ksched_getparam(&uap->sysmsg_result, ksched, targetp, &sched_param);
195
196         if (!e)
197                 copyout(&sched_param, uap->param, sizeof(sched_param));
198
199         return e;
200 }
201
202 int
203 sched_setscheduler(struct sched_setscheduler_args *uap)
204 {
205         struct proc *p = curproc;
206         int e;
207
208         struct sched_param sched_param;
209         copyin(uap->param, &sched_param, sizeof(sched_param));
210
211         if ((e = p31b_proc(p, uap->pid, &p)) == 0) {
212                 e = ksched_setscheduler(&uap->sysmsg_result, ksched, p,
213                     uap->policy, (const struct sched_param *)&sched_param);
214         }
215         return e;
216 }
217
218 int
219 sched_getscheduler(struct sched_getscheduler_args *uap)
220 {
221         struct proc *p = curproc;
222         struct proc *targetp;
223         int e;
224  
225         if (uap->pid != 0 && uap->pid != p->p_pid) {
226                 e = p31b_proc(p, uap->pid, &targetp);
227                 if (e)
228                         return e;
229         } else {
230                 targetp = p;
231         }
232  
233         e = ksched_getscheduler(&uap->sysmsg_result, ksched, targetp);
234
235         return e;
236 }
237
238 int
239 sched_yield(struct sched_yield_args *uap)
240 {
241         return ksched_yield(&uap->sysmsg_result, ksched);
242 }
243
244 int
245 sched_get_priority_max(struct sched_get_priority_max_args *uap)
246 {
247         return ksched_get_priority_max(&uap->sysmsg_result, ksched, uap->policy);
248 }
249
250 int
251 sched_get_priority_min(struct sched_get_priority_min_args *uap)
252 {
253         return ksched_get_priority_min(&uap->sysmsg_result, ksched, uap->policy);
254 }
255
256 int
257 sched_rr_get_interval(struct sched_rr_get_interval_args *uap)
258 {
259         int e;
260         struct proc *p = curproc;
261
262         if ((e = p31b_proc(p, uap->pid, &p)) == 0) {
263             e = ksched_rr_get_interval(&uap->sysmsg_result, ksched,
264                     p, uap->interval);
265         }
266         return e;
267 }
268
269 #endif
270
271 static void p31binit(void *notused)
272 {
273         (void) sched_attach();
274         p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
275 }
276
277 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);