nrelease - fix/improve livecd
[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  */
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/malloc.h>
43 #include <sys/posix4.h>
44 #include <sys/proc.h>
45 #include <sys/syslog.h>
46 #include <sys/module.h>
47 #include <sys/sysmsg.h>
48 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50
51 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
52
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 #if !defined(_KPOSIX_PRIORITY_SCHEDULING)
84
85 int syscall_not_present(const char *s);
86
87 /* The system calls return ENOSYS if an entry is called that is
88  * not run-time supported.  I am also logging since some programs
89  * start to use this when they shouldn't.  That will be removed if annoying.
90  */
91 int syscall_not_present(const char *s)
92 {
93         struct proc *p = curproc;
94         log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
95                         p->p_comm, p->p_pid, s);
96
97         /* a " return nosys(p, uap); " here causes a core dump.
98          */
99
100         return ENOSYS;
101 }
102
103 /* Not configured but loadable via a module:
104  */
105
106 static int sched_attach(void)
107 {
108         return 0;
109 }
110
111 #define SYSCALL_NOT_PRESENT_GEN(SC) \
112 int sys_##SC (struct sysmsg *sysmsg, const struct SC##_args *uap) \
113 { \
114         return syscall_not_present(#SC); \
115 }
116
117 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
118 SYSCALL_NOT_PRESENT_GEN(sched_getparam)
119 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
120 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
121 SYSCALL_NOT_PRESENT_GEN(sched_yield)
122 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
123 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
124 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
125
126 #else
127
128 /*
129  * p31b_proc: Look up a proc from a PID.  If proc is 0 it is
130  * my own proc.
131  *
132  * Returns a held process in *pp.
133  */
134 static
135 int
136 p31b_proc(pid_t pid, struct proc **pp)
137 {
138         int ret = 0;
139         struct proc *p = curproc;
140         struct proc *other_proc;
141
142         if (pid == 0) {
143                 other_proc = p;
144                 if (other_proc)
145                         PHOLD(other_proc);
146         } else {
147                 other_proc = pfind(pid);
148                 /* ref from pfind() */
149         }
150
151         if (other_proc) {
152                 /* Enforce permission policy.
153                  */
154                 if (CAN_AFFECT(p, p->p_ucred, other_proc)) {
155                         *pp = other_proc;
156                         lwkt_gettoken(&other_proc->p_token);
157                 } else {
158                         *pp = NULL;
159                         ret = EPERM;
160                         PRELE(other_proc);
161                 }
162         } else {
163                 *pp = NULL;
164                 ret = ESRCH;
165         }
166         return ret;
167 }
168
169 static
170 void
171 p31b_proc_done(struct proc *other_proc)
172 {
173         if (other_proc) {
174                 lwkt_reltoken(&other_proc->p_token);
175                 PRELE(other_proc);
176         }
177 }
178
179 /* Configured in kernel version:
180  */
181 static struct ksched *ksched;
182
183 static int sched_attach(void)
184 {
185         int ret = ksched_attach(&ksched);
186
187         if (ret == 0)
188                 p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
189
190         return ret;
191 }
192
193 int
194 sys_sched_setparam(struct sysmsg *sysmsg,
195                    const struct sched_setparam_args *uap)
196 {
197         struct proc *p;
198         struct lwp *lp;
199         struct sched_param sched_param;
200         int e;
201
202         copyin(uap->param, &sched_param, sizeof(sched_param));
203
204         if ((e = p31b_proc(uap->pid, &p)) == 0) {
205                 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
206                 if (lp) {
207                         LWPHOLD(lp);
208                         lwkt_gettoken(&lp->lwp_token);
209                         e = ksched_setparam(&sysmsg->sysmsg_reg, ksched, lp,
210                                     (const struct sched_param *)&sched_param);
211                         lwkt_reltoken(&lp->lwp_token);
212                         LWPRELE(lp);
213                 } else {
214                         e = ESRCH;
215                 }
216                 p31b_proc_done(p);
217         }
218         return e;
219 }
220
221 int
222 sys_sched_getparam(struct sysmsg *sysmsg,
223                    const struct sched_getparam_args *uap)
224 {
225         struct proc *targetp;
226         struct lwp *lp;
227         struct sched_param sched_param;
228         int e;
229
230         if ((e = p31b_proc(uap->pid, &targetp)) == 0) {
231                 lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */
232                 if (lp) {
233                         LWPHOLD(lp);
234                         lwkt_gettoken(&lp->lwp_token);
235                         e = ksched_getparam(&sysmsg->sysmsg_reg, ksched,
236                                             lp, &sched_param);
237                         lwkt_reltoken(&lp->lwp_token);
238                         LWPRELE(lp);
239                 } else {
240                         e = ESRCH;
241                 }
242                 p31b_proc_done(targetp);
243         }
244         if (e == 0)
245                 copyout(&sched_param, uap->param, sizeof(sched_param));
246         return e;
247 }
248
249 int
250 sys_sched_setscheduler(struct sysmsg *sysmsg,
251                        const struct sched_setscheduler_args *uap)
252 {
253         struct proc *p;
254         struct lwp *lp;
255         int e;
256         struct sched_param sched_param;
257
258         copyin(uap->param, &sched_param, sizeof(sched_param));
259
260         if ((e = p31b_proc(uap->pid, &p)) == 0) {
261                 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
262                 if (lp) {
263                         LWPHOLD(lp);
264                         lwkt_gettoken(&lp->lwp_token);
265                         e = ksched_setscheduler(&sysmsg->sysmsg_reg, ksched,
266                                                 lp, uap->policy,
267                                     (const struct sched_param *)&sched_param);
268                         lwkt_reltoken(&lp->lwp_token);
269                         LWPRELE(lp);
270                 } else {
271                         e = ESRCH;
272                 }
273                 p31b_proc_done(p);
274         }
275         return e;
276 }
277
278 int
279 sys_sched_getscheduler(struct sysmsg *sysmsg,
280                        const struct sched_getscheduler_args *uap)
281 {
282         struct proc *targetp;
283         struct lwp *lp;
284         int e;
285
286         if ((e = p31b_proc(uap->pid, &targetp)) == 0) {
287                 lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */
288                 if (lp) {
289                         LWPHOLD(lp);
290                         lwkt_gettoken(&lp->lwp_token);
291                         e = ksched_getscheduler(&sysmsg->sysmsg_reg, ksched, lp);
292                         lwkt_reltoken(&lp->lwp_token);
293                         LWPRELE(lp);
294                 } else {
295                         e = ESRCH;
296                 }
297                 p31b_proc_done(targetp);
298         }
299         return e;
300 }
301
302 /*
303  * MPSAFE
304  */
305 int
306 sys_sched_yield(struct sysmsg *sysmsg,
307                 const struct sched_yield_args *uap)
308 {
309         return ksched_yield(&sysmsg->sysmsg_reg, ksched);
310 }
311
312 /*
313  * MPSAFE
314  */
315 int
316 sys_sched_get_priority_max(struct sysmsg *sysmsg,
317                            const struct sched_get_priority_max_args *uap)
318 {
319         return ksched_get_priority_max(&sysmsg->sysmsg_reg, ksched, uap->policy);
320 }
321
322 /*
323  * MPSAFE
324  */
325 int
326 sys_sched_get_priority_min(struct sysmsg *sysmsg,
327                            const struct sched_get_priority_min_args *uap)
328 {
329         return ksched_get_priority_min(&sysmsg->sysmsg_reg, ksched, uap->policy);
330 }
331
332 int
333 sys_sched_rr_get_interval(struct sysmsg *sysmsg,
334                           const struct sched_rr_get_interval_args *uap)
335 {
336         int e;
337         struct proc *p;
338         struct lwp *lp;
339         struct timespec ts;
340
341         if ((e = p31b_proc(uap->pid, &p)) == 0) {
342                 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
343                 if (lp) {
344                         LWPHOLD(lp);
345                         lwkt_gettoken(&lp->lwp_token);
346                         e = ksched_rr_get_interval(&sysmsg->sysmsg_reg, ksched,
347                                                    lp, &ts);
348                         if (e == 0)
349                                 e = copyout(&ts, uap->interval, sizeof(ts));
350                         lwkt_reltoken(&lp->lwp_token);
351                         LWPRELE(lp);
352                 } else {
353                         e = ESRCH;
354                 }
355                 p31b_proc_done(p);
356         }
357         return e;
358 }
359
360 #endif
361
362 static void
363 p31binit(void *notused)
364 {
365         sched_attach();
366         p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, -1);
367         p31b_setcfg(CTL_P1003_1B_FSYNC, _POSIX_FSYNC);
368         p31b_setcfg(CTL_P1003_1B_MAPPED_FILES, _POSIX_MAPPED_FILES);
369         p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
370         p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
371         p31b_setcfg(CTL_P1003_1B_SEMAPHORES, _POSIX_SEMAPHORES);
372         p31b_setcfg(CTL_P1003_1B_SEM_NSEMS_MAX, 256);
373         p31b_setcfg(CTL_P1003_1B_SHARED_MEMORY_OBJECTS,
374             _POSIX_SHARED_MEMORY_OBJECTS);
375
376 #if 0 /* XXX missing */
377         p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
378         p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
379         p31b_setcfg(CTL_P1003_1B_AIO_MAX, 0);
380         p31b_setcfg(CTL_P1003_1B_AIO_LISTIO_MAX, 0);
381         p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, 0);
382         p31b_setcfg(CTL_P1003_1B_MEMLOCK, 0);
383         p31b_setcfg(CTL_P1003_1B_MEMLOCK_RANGE, 0);
384         p31b_setcfg(CTL_P1003_1B_MEMORY_PROTECTION, 0);
385         p31b_setcfg(CTL_P1003_1B_PRIORITIZED_IO, _POSIX_PRIORITIZED_IO);
386         p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
387         p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
388         p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, 0);
389         p31b_setcfg(CTL_P1003_1B_SYNCHRONIZED_IO, _POSIX_SYNCHRONIZED_IO);
390         p31b_setcfg(CTL_P1003_1B_TIMERS, _POSIX_TIMERS);
391         p31b_setcfg(CTL_P1003_1B_TIMER_MAX, 0);
392 #endif
393 }
394
395 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);