xref: /freebsd/sys/kern/p1003_1b.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
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/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47 #include <sys/syslog.h>
48 #include <sys/sysproto.h>
49 
50 #include <posix4/posix4.h>
51 
52 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
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 with credential cr1 do "write flavor" operations to credential
72  * cr2.  This check needs to use generalized checks.
73  */
74 #define CAN_AFFECT(cr1, cr2) \
75 	(!suser_xxx(cr1, NULL, PRISON_ROOT) || \
76 	    (c1)->cr_ruid == (cr2)->cr_ruid || \
77 	    (c1)->cr_uid == (cr2)->cr_ruid || \
78 	    (c1)->cr_ruid == (cr2)->cr_uid || \
79 	    (c1)->cr_uid == (cr2)->cr_uid)
80 #else
81 #define CAN_AFFECT(cr1, cr2) (!suser_xxx(cr1, NULL, PRISON_ROOT))
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  */
88 int 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 		PROC_LOCK(p);
96 	} else
97 		other_proc = pfind(pid);
98 
99 	if (other_proc)
100 	{
101 		/* Enforce permission policy.
102 		 */
103 		if (CAN_AFFECT(p->p_ucred, other_proc->p_ucred))
104 			*pp = other_proc;
105 		else
106 			ret = EPERM;
107 		PROC_UNLOCK(other_proc);
108 	}
109 	else
110 		ret = ESRCH;
111 
112 	return ret;
113 }
114 
115 /* The system calls return ENOSYS if an entry is called that is
116  * not run-time supported.  I am also logging since some programs
117  * start to use this when they shouldn't.  That will be removed if annoying.
118  */
119 int
120 syscall_not_present(struct proc *p, const char *s, struct nosys_args *uap)
121 {
122 	log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
123 			p->p_comm, p->p_pid, s);
124 
125 	/* a " return nosys(p, uap); " here causes a core dump.
126 	 */
127 
128 	return ENOSYS;
129 }
130 
131 #if !defined(_KPOSIX_PRIORITY_SCHEDULING)
132 
133 /* Not configured but loadable via a module:
134  */
135 
136 static int sched_attach(void)
137 {
138 	return 0;
139 }
140 
141 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
142 SYSCALL_NOT_PRESENT_GEN(sched_getparam)
143 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
144 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
145 SYSCALL_NOT_PRESENT_GEN(sched_yield)
146 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
147 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
148 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
149 
150 #else
151 
152 /* Configured in kernel version:
153  */
154 static struct ksched *ksched;
155 
156 static int sched_attach(void)
157 {
158 	int ret = ksched_attach(&ksched);
159 
160 	if (ret == 0)
161 		p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
162 
163 	return ret;
164 }
165 
166 int sched_setparam(struct proc *p,
167 	struct sched_setparam_args *uap)
168 {
169 	int e;
170 
171 	struct sched_param sched_param;
172 	copyin(uap->param, &sched_param, sizeof(sched_param));
173 
174 	(void) (0
175 	|| (e = p31b_proc(p, uap->pid, &p))
176 	|| (e = ksched_setparam(&p->p_retval[0], ksched, p,
177 		(const struct sched_param *)&sched_param))
178 	);
179 
180 	return e;
181 }
182 
183 int sched_getparam(struct proc *p,
184 	struct sched_getparam_args *uap)
185 {
186 	int e;
187 	struct sched_param sched_param;
188 
189 	(void) (0
190 	|| (e = p31b_proc(p, uap->pid, &p))
191 	|| (e = ksched_getparam(&p->p_retval[0], ksched, p, &sched_param))
192 	);
193 
194 	if (!e)
195 		copyout(&sched_param, uap->param, sizeof(sched_param));
196 
197 	return e;
198 }
199 int sched_setscheduler(struct proc *p,
200 	struct sched_setscheduler_args *uap)
201 {
202 	int e;
203 
204 	struct sched_param sched_param;
205 	copyin(uap->param, &sched_param, sizeof(sched_param));
206 
207 	(void) (0
208 	|| (e = p31b_proc(p, uap->pid, &p))
209 	|| (e = ksched_setscheduler(&p->p_retval[0],
210 	ksched, p, uap->policy,
211 		(const struct sched_param *)&sched_param))
212 	);
213 
214 	return e;
215 }
216 int sched_getscheduler(struct proc *p,
217 	struct sched_getscheduler_args *uap)
218 {
219 	int e;
220 	(void) (0
221 	|| (e = p31b_proc(p, uap->pid, &p))
222 	|| (e = ksched_getscheduler(&p->p_retval[0], ksched, p))
223 	);
224 
225 	return e;
226 }
227 int sched_yield(struct proc *p,
228 	struct sched_yield_args *uap)
229 {
230 	return ksched_yield(&p->p_retval[0], ksched);
231 }
232 int sched_get_priority_max(struct proc *p,
233 	struct sched_get_priority_max_args *uap)
234 {
235 	return ksched_get_priority_max(&p->p_retval[0],
236 	ksched, uap->policy);
237 }
238 int sched_get_priority_min(struct proc *p,
239 	struct sched_get_priority_min_args *uap)
240 {
241 	return ksched_get_priority_min(&p->p_retval[0],
242 	ksched, uap->policy);
243 }
244 int sched_rr_get_interval(struct proc *p,
245 	struct sched_rr_get_interval_args *uap)
246 {
247 	int e;
248 
249 	(void) (0
250 	|| (e = p31b_proc(p, uap->pid, &p))
251 	|| (e = ksched_rr_get_interval(&p->p_retval[0], ksched,
252 	p, uap->interval))
253 	);
254 
255 	return e;
256 }
257 
258 #endif
259 
260 static void p31binit(void *notused)
261 {
262 	(void) sched_attach();
263 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
264 }
265 
266 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
267