p1003_1b.c revision 293482
1102167Stjr/*-
2102167Stjr * Copyright (c) 1996, 1997, 1998
3102167Stjr *	HD Associates, Inc.  All rights reserved.
4102167Stjr *
5102167Stjr * Redistribution and use in source and binary forms, with or without
6102167Stjr * modification, are permitted provided that the following conditions
7102167Stjr * are met:
8102167Stjr * 1. Redistributions of source code must retain the above copyright
9102167Stjr *    notice, this list of conditions and the following disclaimer.
10102167Stjr * 2. Redistributions in binary form must reproduce the above copyright
11102167Stjr *    notice, this list of conditions and the following disclaimer in the
12102167Stjr *    documentation and/or other materials provided with the distribution.
13102167Stjr * 3. All advertising materials mentioning features or use of this software
14102167Stjr *    must display the following acknowledgement:
15102167Stjr *	This product includes software developed by HD Associates, Inc
16102167Stjr * 4. Neither the name of the author nor the names of any co-contributors
17102167Stjr *    may be used to endorse or promote products derived from this software
18102167Stjr *    without specific prior written permission.
19102167Stjr *
20102167Stjr * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
21102167Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22102167Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23102167Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
24102167Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25102167Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26107392Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27133914Strhodes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28102167Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29102167Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30102167Stjr * SUCH DAMAGE.
31102167Stjr */
32102167Stjr
33102167Stjr/* p1003_1b: Real Time common code.
34102167Stjr */
35102167Stjr
36102167Stjr#include <sys/cdefs.h>
37102167Stjr__FBSDID("$FreeBSD: stable/10/sys/kern/p1003_1b.c 293482 2016-01-09 14:40:38Z dchagin $");
38102167Stjr
39102167Stjr#include "opt_posix.h"
40102167Stjr
41102167Stjr#include <sys/param.h>
42102167Stjr#include <sys/systm.h>
43102167Stjr#include <sys/kernel.h>
44102167Stjr#include <sys/lock.h>
45102167Stjr#include <sys/module.h>
46102167Stjr#include <sys/mutex.h>
47102167Stjr#include <sys/priv.h>
48102167Stjr#include <sys/proc.h>
49102167Stjr#include <sys/posix4.h>
50102167Stjr#include <sys/syscallsubr.h>
51107392Sru#include <sys/sysctl.h>
52102167Stjr#include <sys/sysent.h>
53102167Stjr#include <sys/syslog.h>
54102167Stjr#include <sys/sysproto.h>
55102167Stjr
56104402StjrMALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
57104402Stjr
58104402Stjr/* The system calls return ENOSYS if an entry is called that is not run-time
59104402Stjr * supported.  I am also logging since some programs start to use this when
60104402Stjr * they shouldn't.  That will be removed if annoying.
61104402Stjr */
62104402Stjrint
63104402Stjrsyscall_not_present(struct thread *td, const char *s, struct nosys_args *uap)
64104402Stjr{
65104402Stjr	log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
66104402Stjr			td->td_name, td->td_proc->p_pid, s);
67104402Stjr
68133566Stjr	/* a " return nosys(p, uap); " here causes a core dump.
69133566Stjr	 */
70107392Sru
71133915Strhodes	return ENOSYS;
72104402Stjr}
73104402Stjr
74107392Sru#if !defined(_KPOSIX_PRIORITY_SCHEDULING)
75104402Stjr
76133566Stjr/* Not configured but loadable via a module:
77133566Stjr */
78104402Stjr
79104402Stjrstatic int
80102167Stjrsched_attach(void)
81102167Stjr{
82102167Stjr	return 0;
83102167Stjr}
84102167Stjr
85102167StjrSYSCALL_NOT_PRESENT_GEN(sched_setparam)
86102167StjrSYSCALL_NOT_PRESENT_GEN(sched_getparam)
87102167StjrSYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
88SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
89SYSCALL_NOT_PRESENT_GEN(sched_yield)
90SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
91SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
92SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
93#else
94
95/* Configured in kernel version:
96 */
97static struct ksched *ksched;
98
99static int
100sched_attach(void)
101{
102	int ret = ksched_attach(&ksched);
103
104	if (ret == 0)
105		p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 200112L);
106
107	return ret;
108}
109
110int
111sys_sched_setparam(struct thread *td, struct sched_setparam_args *uap)
112{
113	struct thread *targettd;
114	struct proc *targetp;
115	int e;
116	struct sched_param sched_param;
117
118	e = copyin(uap->param, &sched_param, sizeof(sched_param));
119	if (e)
120		return (e);
121
122	if (uap->pid == 0) {
123		targetp = td->td_proc;
124		targettd = td;
125		PROC_LOCK(targetp);
126	} else {
127		targetp = pfind(uap->pid);
128		if (targetp == NULL)
129			return (ESRCH);
130		targettd = FIRST_THREAD_IN_PROC(targetp);
131	}
132
133	e = p_cansched(td, targetp);
134	if (e == 0) {
135		e = ksched_setparam(ksched, targettd,
136			(const struct sched_param *)&sched_param);
137	}
138	PROC_UNLOCK(targetp);
139	return (e);
140}
141
142int
143sys_sched_getparam(struct thread *td, struct sched_getparam_args *uap)
144{
145	int e;
146	struct sched_param sched_param;
147	struct thread *targettd;
148	struct proc *targetp;
149
150	if (uap->pid == 0) {
151		targetp = td->td_proc;
152		targettd = td;
153		PROC_LOCK(targetp);
154	} else {
155		targetp = pfind(uap->pid);
156		if (targetp == NULL) {
157			return (ESRCH);
158		}
159		targettd = FIRST_THREAD_IN_PROC(targetp);
160	}
161
162	e = p_cansee(td, targetp);
163	if (e == 0) {
164		e = ksched_getparam(ksched, targettd, &sched_param);
165	}
166	PROC_UNLOCK(targetp);
167	if (e == 0)
168		e = copyout(&sched_param, uap->param, sizeof(sched_param));
169	return (e);
170}
171
172int
173sys_sched_setscheduler(struct thread *td, struct sched_setscheduler_args *uap)
174{
175	int e;
176	struct sched_param sched_param;
177	struct thread *targettd;
178	struct proc *targetp;
179
180	/* Don't allow non root user to set a scheduler policy. */
181	e = priv_check(td, PRIV_SCHED_SET);
182	if (e)
183		return (e);
184
185	e = copyin(uap->param, &sched_param, sizeof(sched_param));
186	if (e)
187		return (e);
188
189	if (uap->pid == 0) {
190		targetp = td->td_proc;
191		targettd = td;
192		PROC_LOCK(targetp);
193	} else {
194		targetp = pfind(uap->pid);
195		if (targetp == NULL)
196			return (ESRCH);
197		targettd = FIRST_THREAD_IN_PROC(targetp);
198	}
199
200	e = p_cansched(td, targetp);
201	if (e == 0) {
202		e = ksched_setscheduler(ksched, targettd,
203			uap->policy, (const struct sched_param *)&sched_param);
204	}
205	PROC_UNLOCK(targetp);
206	return (e);
207}
208
209int
210sys_sched_getscheduler(struct thread *td, struct sched_getscheduler_args *uap)
211{
212	int e, policy;
213	struct thread *targettd;
214	struct proc *targetp;
215
216	if (uap->pid == 0) {
217		targetp = td->td_proc;
218		targettd = td;
219		PROC_LOCK(targetp);
220	} else {
221		targetp = pfind(uap->pid);
222		if (targetp == NULL)
223			return (ESRCH);
224		targettd = FIRST_THREAD_IN_PROC(targetp);
225	}
226
227	e = p_cansee(td, targetp);
228	if (e == 0) {
229		e = ksched_getscheduler(ksched, targettd, &policy);
230		td->td_retval[0] = policy;
231	}
232	PROC_UNLOCK(targetp);
233
234	return (e);
235}
236
237int
238sys_sched_yield(struct thread *td, struct sched_yield_args *uap)
239{
240
241	sched_relinquish(curthread);
242	return 0;
243}
244
245int
246sys_sched_get_priority_max(struct thread *td,
247    struct sched_get_priority_max_args *uap)
248{
249	int error, prio;
250
251	error = ksched_get_priority_max(ksched, uap->policy, &prio);
252	td->td_retval[0] = prio;
253	return (error);
254}
255
256int
257sys_sched_get_priority_min(struct thread *td,
258    struct sched_get_priority_min_args *uap)
259{
260	int error, prio;
261
262	error = ksched_get_priority_min(ksched, uap->policy, &prio);
263	td->td_retval[0] = prio;
264	return (error);
265}
266
267int
268sys_sched_rr_get_interval(struct thread *td,
269    struct sched_rr_get_interval_args *uap)
270{
271	struct timespec timespec;
272	int error;
273
274	error = kern_sched_rr_get_interval(td, uap->pid, &timespec);
275	if (error == 0)
276		error = copyout(&timespec, uap->interval, sizeof(timespec));
277	return (error);
278}
279
280int
281kern_sched_rr_get_interval(struct thread *td, pid_t pid,
282    struct timespec *ts)
283{
284	int e;
285	struct thread *targettd;
286	struct proc *targetp;
287
288	if (pid == 0) {
289		targettd = td;
290		targetp = td->td_proc;
291		PROC_LOCK(targetp);
292	} else {
293		targetp = pfind(pid);
294		if (targetp == NULL)
295			return (ESRCH);
296		targettd = FIRST_THREAD_IN_PROC(targetp);
297	}
298
299	e = kern_sched_rr_get_interval_td(td, targettd, ts);
300	PROC_UNLOCK(targetp);
301	return (e);
302}
303
304int
305kern_sched_rr_get_interval_td(struct thread *td, struct thread *targettd,
306    struct timespec *ts)
307{
308	struct proc *p;
309	int error;
310
311	p = targettd->td_proc;
312	PROC_LOCK_ASSERT(p, MA_OWNED);
313
314	error = p_cansee(td, p);
315	if (error == 0)
316		error = ksched_rr_get_interval(ksched, targettd, ts);
317	return (error);
318}
319#endif
320
321static void
322p31binit(void *notused)
323{
324	(void) sched_attach();
325	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
326}
327
328SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
329