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