]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/systemcalls.c
973d35899420d9aaed1a1fc6fa358d33916b4daf
[apple/xnu.git] / bsd / dev / ppc / systemcalls.c
1 /*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
32 * Version 2.0.
33 */
34
35 #include <mach/mach_traps.h>
36
37 #include <kern/task.h>
38 #include <kern/thread.h>
39 #include <kern/assert.h>
40 #include <kern/clock.h>
41 #include <kern/locks.h>
42 #include <kern/sched_prim.h>
43 #include <mach/machine/thread_status.h>
44 #include <mach/thread_act.h>
45 #include <ppc/savearea.h>
46
47 #include <sys/kernel.h>
48 #include <sys/vm.h>
49 #include <sys/proc_internal.h>
50 #include <sys/syscall.h>
51 #include <sys/systm.h>
52 #include <sys/user.h>
53 #include <sys/errno.h>
54 #include <sys/kdebug.h>
55 #include <sys/sysent.h>
56 #include <sys/sysproto.h>
57 #include <sys/kauth.h>
58
59 #include <security/audit/audit.h>
60
61 #if CONFIG_DTRACE
62 extern int32_t dtrace_systrace_syscall(struct proc *, void *, int *);
63 extern void dtrace_systrace_syscall_return(unsigned short, int, int *);
64 #endif
65
66 extern void
67 unix_syscall(struct savearea *regs);
68
69 extern struct savearea *
70 find_user_regs(
71 thread_t act);
72
73 extern lck_spin_t * tz_slock;
74
75 /*
76 * Function: unix_syscall
77 *
78 * Inputs: regs - pointer to Process Control Block
79 *
80 * Outputs: none
81 */
82 void
83 unix_syscall(struct savearea *regs)
84 {
85 thread_t thread_act;
86 struct uthread *uthread;
87 struct proc *proc;
88 struct sysent *callp;
89 int error;
90 unsigned int code;
91 boolean_t flavor;
92
93 flavor = (((unsigned int)regs->save_r0) == 0)? 1: 0;
94
95 if (flavor)
96 code = regs->save_r3;
97 else
98 code = regs->save_r0;
99
100 if (kdebug_enable && (code != 180)) {
101 if (flavor)
102 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
103 regs->save_r4, regs->save_r5, regs->save_r6, regs->save_r7, 0);
104 else
105 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_START,
106 regs->save_r3, regs->save_r4, regs->save_r5, regs->save_r6, 0);
107 }
108 thread_act = current_thread();
109 uthread = get_bsdthread_info(thread_act);
110
111 if (!(uthread->uu_flag & UT_VFORK))
112 proc = (struct proc *)get_bsdtask_info(current_task());
113 else
114 proc = current_proc();
115
116 /* Make sure there is a process associated with this task */
117 if (proc == NULL) {
118 regs->save_r3 = (long long)EPERM;
119 /* set the "pc" to execute cerror routine */
120 regs->save_srr0 -= 4;
121 task_terminate_internal(current_task());
122 thread_exception_return();
123 /* NOTREACHED */
124 }
125
126 /*
127 * Delayed binding of thread credential to process credential, if we
128 * are not running with an explicitly set thread credential.
129 */
130 kauth_cred_uthread_update(uthread, proc);
131
132 callp = (code >= NUM_SYSENT) ? &sysent[63] : &sysent[code];
133
134 if (callp->sy_narg != 0) {
135 void *regsp;
136 sy_munge_t *mungerp;
137
138 if (IS_64BIT_PROCESS(proc)) {
139 /* XXX Turn 64 bit unsafe calls into nosys() */
140 if (callp->sy_flags & UNSAFE_64BIT) {
141 callp = &sysent[63];
142 goto unsafe;
143 }
144 mungerp = callp->sy_arg_munge64;
145 }
146 else {
147 mungerp = callp->sy_arg_munge32;
148 }
149 if ( !flavor) {
150 regsp = (void *) &regs->save_r3;
151 } else {
152 /* indirect system call consumes an argument so only 7 are supported */
153 if (callp->sy_narg > 7) {
154 callp = &sysent[63];
155 goto unsafe;
156 }
157 regsp = (void *) &regs->save_r4;
158 }
159 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
160 (*mungerp)(regsp, (void *) &uthread->uu_arg[0]);
161 }
162
163 unsafe:
164
165 uthread->uu_flag |= UT_NOTCANCELPT;
166
167 uthread->uu_rval[0] = 0;
168
169 /*
170 * r4 is volatile, if we set it to regs->save_r4 here the child
171 * will have parents r4 after execve
172 */
173 uthread->uu_rval[1] = 0;
174
175 error = 0;
176
177 /*
178 * PPC runtime calls cerror after every unix system call, so
179 * assume no error and adjust the "pc" to skip this call.
180 * It will be set back to the cerror call if an error is detected.
181 */
182 regs->save_srr0 += 4;
183
184 #ifdef JOE_DEBUG
185 uthread->uu_iocount = 0;
186 uthread->uu_vpindex = 0;
187 #endif
188 AUDIT_SYSCALL_ENTER(code, proc, uthread);
189 error = (*(callp->sy_call))(proc, (void *)uthread->uu_arg, &(uthread->uu_rval[0]));
190 AUDIT_SYSCALL_EXIT(code, proc, uthread, error);
191 #if CONFIG_MACF
192 mac_thread_userret(code, error, thread_act);
193 #endif
194
195
196 #ifdef JOE_DEBUG
197 if (uthread->uu_iocount)
198 printf("system call returned with uu_iocount != 0\n");
199 #endif
200 #if CONFIG_DTRACE
201 uthread->t_dtrace_errno = error;
202 #endif /* CONFIG_DTRACE */
203
204 regs = find_user_regs(thread_act);
205
206 if (error == ERESTART) {
207 regs->save_srr0 -= 8;
208 } else if (error != EJUSTRETURN) {
209 if (error) {
210 regs->save_r3 = (long long)error;
211 /* set the "pc" to execute cerror routine */
212 regs->save_srr0 -= 4;
213 } else { /* (not error) */
214 switch (callp->sy_return_type) {
215 case _SYSCALL_RET_INT_T:
216 regs->save_r3 = uthread->uu_rval[0];
217 regs->save_r4 = uthread->uu_rval[1];
218 break;
219 case _SYSCALL_RET_UINT_T:
220 regs->save_r3 = ((u_int)uthread->uu_rval[0]);
221 regs->save_r4 = ((u_int)uthread->uu_rval[1]);
222 break;
223 case _SYSCALL_RET_OFF_T:
224 /* off_t returns 64 bits split across two registers for 32 bit */
225 /* process and in one register for 64 bit process */
226 if (IS_64BIT_PROCESS(proc)) {
227 u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
228 regs->save_r3 = *retp;
229 regs->save_r4 = 0;
230 }
231 else {
232 regs->save_r3 = uthread->uu_rval[0];
233 regs->save_r4 = uthread->uu_rval[1];
234 }
235 break;
236 case _SYSCALL_RET_ADDR_T:
237 case _SYSCALL_RET_SIZE_T:
238 case _SYSCALL_RET_SSIZE_T:
239 /* the variable length return types (user_addr_t, user_ssize_t,
240 * and user_size_t) are always the largest possible size in the
241 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
242 */
243 {
244 user_addr_t *retp = (user_addr_t *)&uthread->uu_rval[0];
245 regs->save_r3 = *retp;
246 regs->save_r4 = 0;
247 }
248 break;
249 case _SYSCALL_RET_NONE:
250 break;
251 default:
252 panic("unix_syscall: unknown return type");
253 break;
254 }
255 }
256 }
257 /* else (error == EJUSTRETURN) { nothing } */
258
259
260 uthread->uu_flag &= ~UT_NOTCANCELPT;
261
262 /* panic if funnel is held */
263 syscall_exit_funnelcheck();
264
265 if (uthread->uu_lowpri_window) {
266 /*
267 * task is marked as a low priority I/O type
268 * and the I/O we issued while in this system call
269 * collided with normal I/O operations... we'll
270 * delay in order to mitigate the impact of this
271 * task on the normal operation of the system
272 */
273 throttle_lowpri_io(TRUE);
274 }
275 if (kdebug_enable && (code != 180)) {
276
277 if (callp->sy_return_type == _SYSCALL_RET_SSIZE_T)
278 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
279 error, uthread->uu_rval[1], 0, proc->p_pid, 0);
280 else
281 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
282 error, uthread->uu_rval[0], uthread->uu_rval[1], proc->p_pid, 0);
283 }
284
285 thread_exception_return();
286 /* NOTREACHED */
287 }
288
289 void
290 unix_syscall_return(int error)
291 {
292 thread_t thread_act;
293 struct uthread *uthread;
294 struct proc *proc;
295 struct savearea *regs;
296 unsigned int code;
297 struct sysent *callp;
298
299 thread_act = current_thread();
300 proc = current_proc();
301 uthread = get_bsdthread_info(thread_act);
302
303 regs = find_user_regs(thread_act);
304
305 if (regs->save_r0 != 0)
306 code = regs->save_r0;
307 else
308 code = regs->save_r3;
309
310 callp = (code >= NUM_SYSENT) ? &sysent[63] : &sysent[code];
311
312 #if CONFIG_DTRACE
313 if (callp->sy_call == dtrace_systrace_syscall)
314 dtrace_systrace_syscall_return( code, error, uthread->uu_rval );
315 #endif /* CONFIG_DTRACE */
316 AUDIT_SYSCALL_EXIT(code, proc, uthread, error);
317
318 /*
319 * Get index into sysent table
320 */
321 if (error == ERESTART) {
322 regs->save_srr0 -= 8;
323 } else if (error != EJUSTRETURN) {
324 if (error) {
325 regs->save_r3 = (long long)error;
326 /* set the "pc" to execute cerror routine */
327 regs->save_srr0 -= 4;
328 } else { /* (not error) */
329 switch (callp->sy_return_type) {
330 case _SYSCALL_RET_INT_T:
331 regs->save_r3 = uthread->uu_rval[0];
332 regs->save_r4 = uthread->uu_rval[1];
333 break;
334 case _SYSCALL_RET_UINT_T:
335 regs->save_r3 = ((u_int)uthread->uu_rval[0]);
336 regs->save_r4 = ((u_int)uthread->uu_rval[1]);
337 break;
338 case _SYSCALL_RET_OFF_T:
339 /* off_t returns 64 bits split across two registers for 32 bit */
340 /* process and in one register for 64 bit process */
341 if (IS_64BIT_PROCESS(proc)) {
342 u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
343 regs->save_r3 = *retp;
344 }
345 else {
346 regs->save_r3 = uthread->uu_rval[0];
347 regs->save_r4 = uthread->uu_rval[1];
348 }
349 break;
350 case _SYSCALL_RET_ADDR_T:
351 case _SYSCALL_RET_SIZE_T:
352 case _SYSCALL_RET_SSIZE_T:
353 /* the variable length return types (user_addr_t, user_ssize_t,
354 * and user_size_t) are always the largest possible size in the
355 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
356 */
357 {
358 u_int64_t *retp = (u_int64_t *)&uthread->uu_rval[0];
359 regs->save_r3 = *retp;
360 }
361 break;
362 case _SYSCALL_RET_NONE:
363 break;
364 default:
365 panic("unix_syscall: unknown return type");
366 break;
367 }
368 }
369 }
370 /* else (error == EJUSTRETURN) { nothing } */
371
372
373 uthread->uu_flag &= ~UT_NOTCANCELPT;
374
375 /* panic if funnel is held */
376 syscall_exit_funnelcheck();
377
378 if (uthread->uu_lowpri_window) {
379 /*
380 * task is marked as a low priority I/O type
381 * and the I/O we issued while in this system call
382 * collided with normal I/O operations... we'll
383 * delay in order to mitigate the impact of this
384 * task on the normal operation of the system
385 */
386 throttle_lowpri_io(TRUE);
387 }
388 if (kdebug_enable && (code != 180)) {
389 if (callp->sy_return_type == _SYSCALL_RET_SSIZE_T)
390 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
391 error, uthread->uu_rval[1], 0, proc->p_pid, 0);
392 else
393 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC, code) | DBG_FUNC_END,
394 error, uthread->uu_rval[0], uthread->uu_rval[1], proc->p_pid, 0);
395 }
396
397 thread_exception_return();
398 /* NOTREACHED */
399 }
400
401 void
402 munge_lwww(
403 const void *in32,
404 void *out64)
405 {
406 const uint32_t *arg32;
407 uint64_t *arg64;
408
409 arg32 = (const uint32_t *) in32;
410 arg64 = (uint64_t *) out64;
411
412 arg64[3] = arg32[9]; /* lwwW */
413 arg64[2] = arg32[7]; /* lwWw */
414 arg64[1] = arg32[5]; /* lWww */
415 arg64[0] = ((uint64_t) arg32[1]) << 32; /* Lwww (hi) */
416 arg64[0] |= (uint64_t) arg32[3]; /* Lwww (lo) */
417 }
418
419 void
420 munge_lw(
421 const void *in32,
422 void *out64)
423 {
424 const uint32_t *arg32;
425 uint64_t *arg64;
426
427 arg32 = (const uint32_t *) in32;
428 arg64 = (uint64_t *) out64;
429
430 arg64[1] = arg32[5]; /* lW */
431 arg64[0] = ((uint64_t) arg32[1]) << 32; /* Lw (hi) */
432 arg64[0] |= (uint64_t) arg32[3]; /* Lw (lo) */
433 }