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