AMD64 - Adjust _pmap_allocpte() to handle races.
[dragonfly.git] / sys / kern / kern_p1003_1b.c
CommitLineData
984263bc
MD
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 $
3eb2971d 33 * $DragonFly: src/sys/kern/kern_p1003_1b.c,v 1.10 2007/06/26 19:31:08 dillon Exp $
984263bc
MD
34 */
35
36/* p1003_1b: Real Time common code.
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/sysent.h>
3eb2971d 43#include <sys/posix4.h>
984263bc
MD
44#include <sys/proc.h>
45#include <sys/syslog.h>
46#include <sys/module.h>
47#include <sys/sysproto.h>
48#include <sys/sysctl.h>
49
984263bc
MD
50MALLOC_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 */
41c20dac
MD
71#define CAN_AFFECT(p, cr, q) \
72 ((cr)->cr_uid == 0 || \
73 (cr)->cr_ruid == (q)->p_ucred->cr_ruid || \
74 (cr)->cr_uid == (q)->p_ucred->cr_ruid || \
75 (cr)->cr_ruid == (q)->p_ucred->cr_uid || \
76 (cr)->cr_uid == (q)->p_ucred->cr_uid)
984263bc 77#else
41c20dac 78#define CAN_AFFECT(p, cr, q) ((cr)->cr_uid == 0)
984263bc
MD
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 */
85int 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 */
41c20dac 99 if (CAN_AFFECT(p, p->p_ucred, other_proc))
984263bc
MD
100 *pp = other_proc;
101 else
102 ret = EPERM;
103 }
104 else
105 ret = ESRCH;
106
107 return ret;
108}
109
1acbf33c
MD
110
111#if !defined(_KPOSIX_PRIORITY_SCHEDULING)
112
113int syscall_not_present(const char *s);
114
984263bc
MD
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 */
1acbf33c 119int syscall_not_present(const char *s)
984263bc 120{
1acbf33c 121 struct proc *p = curproc;
984263bc
MD
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
984263bc
MD
131/* Not configured but loadable via a module:
132 */
133
134static int sched_attach(void)
135{
136 return 0;
137}
138
1acbf33c
MD
139#define SYSCALL_NOT_PRESENT_GEN(SC) \
140int SC (struct SC##_args *uap) \
141{ \
142 return syscall_not_present(#SC); \
143}
144
984263bc
MD
145SYSCALL_NOT_PRESENT_GEN(sched_setparam)
146SYSCALL_NOT_PRESENT_GEN(sched_getparam)
147SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
148SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
149SYSCALL_NOT_PRESENT_GEN(sched_yield)
150SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
151SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
152SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
153
154#else
155
156/* Configured in kernel version:
157 */
158static struct ksched *ksched;
159
160static int sched_attach(void)
161{
162 int ret = ksched_attach(&ksched);
163
164 if (ret == 0)
165 p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
166
167 return ret;
168}
169
41c20dac 170int
753fd850 171sys_sched_setparam(struct sched_setparam_args *uap)
984263bc 172{
41c20dac 173 struct proc *p = curproc;
08f2f1bb 174 struct lwp *lp;
984263bc
MD
175 int e;
176
177 struct sched_param sched_param;
178 copyin(uap->param, &sched_param, sizeof(sched_param));
179
41c20dac 180 if ((e = p31b_proc(p, uap->pid, &p)) == 0) {
08f2f1bb
SS
181 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
182 e = ksched_setparam(&uap->sysmsg_result, ksched, lp,
41c20dac
MD
183 (const struct sched_param *)&sched_param);
184 }
984263bc
MD
185 return e;
186}
187
41c20dac 188int
753fd850 189sys_sched_getparam(struct sched_getparam_args *uap)
984263bc 190{
41c20dac 191 struct proc *p = curproc;
984263bc 192 struct proc *targetp;
08f2f1bb 193 struct lwp *lp;
984263bc
MD
194 struct sched_param sched_param;
195 int e;
196
197 if (uap->pid != 0 && uap->pid != p->p_pid) {
198 e = p31b_proc(p, uap->pid, &targetp);
199 if (e)
200 return e;
41c20dac 201 } else {
984263bc 202 targetp = p;
41c20dac 203 }
984263bc 204
08f2f1bb
SS
205 lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */
206 e = ksched_getparam(&uap->sysmsg_result, ksched, lp, &sched_param);
984263bc
MD
207
208 if (!e)
209 copyout(&sched_param, uap->param, sizeof(sched_param));
210
211 return e;
212}
41c20dac
MD
213
214int
753fd850 215sys_sched_setscheduler(struct sched_setscheduler_args *uap)
984263bc 216{
41c20dac 217 struct proc *p = curproc;
08f2f1bb 218 struct lwp *lp;
984263bc
MD
219 int e;
220
221 struct sched_param sched_param;
222 copyin(uap->param, &sched_param, sizeof(sched_param));
223
41c20dac 224 if ((e = p31b_proc(p, uap->pid, &p)) == 0) {
08f2f1bb
SS
225 lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
226 e = ksched_setscheduler(&uap->sysmsg_result, ksched, lp,
41c20dac
MD
227 uap->policy, (const struct sched_param *)&sched_param);
228 }
984263bc
MD
229 return e;
230}
41c20dac
MD
231
232int
753fd850 233sys_sched_getscheduler(struct sched_getscheduler_args *uap)
984263bc 234{
41c20dac 235 struct proc *p = curproc;
984263bc 236 struct proc *targetp;
08f2f1bb 237 struct lwp *lp;
984263bc
MD
238 int e;
239
240 if (uap->pid != 0 && uap->pid != p->p_pid) {
241 e = p31b_proc(p, uap->pid, &targetp);
242 if (e)
243 return e;
41c20dac 244 } else {
984263bc 245 targetp = p;
41c20dac 246 }
984263bc 247
08f2f1bb
SS
248 lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */
249 e = ksched_getscheduler(&uap->sysmsg_result, ksched, lp);
984263bc
MD
250
251 return e;
252}
41c20dac
MD
253
254int
753fd850 255sys_sched_yield(struct sched_yield_args *uap)
984263bc 256{
c7114eea 257 return ksched_yield(&uap->sysmsg_result, ksched);
984263bc 258}
41c20dac
MD
259
260int
753fd850 261sys_sched_get_priority_max(struct sched_get_priority_max_args *uap)
984263bc 262{
c7114eea 263 return ksched_get_priority_max(&uap->sysmsg_result, ksched, uap->policy);
984263bc 264}
41c20dac
MD
265
266int
753fd850 267sys_sched_get_priority_min(struct sched_get_priority_min_args *uap)
984263bc 268{
c7114eea 269 return ksched_get_priority_min(&uap->sysmsg_result, ksched, uap->policy);
984263bc 270}
41c20dac
MD
271
272int
753fd850 273sys_sched_rr_get_interval(struct sched_rr_get_interval_args *uap)
984263bc
MD
274{
275 int e;
41c20dac 276 struct proc *p = curproc;
08f2f1bb 277 struct lwp *lp = curthread->td_lwp;
984263bc 278
41c20dac 279 if ((e = p31b_proc(p, uap->pid, &p)) == 0) {
c7114eea 280 e = ksched_rr_get_interval(&uap->sysmsg_result, ksched,
08f2f1bb 281 lp, uap->interval);
41c20dac 282 }
984263bc
MD
283 return e;
284}
285
286#endif
287
288static void p31binit(void *notused)
289{
290 (void) sched_attach();
291 p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
292}
293
294SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);