]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/systemcalls.c
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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,
35 #include <kern/task.h>
36 #include <kern/thread.h>
37 #include <kern/assert.h>
38 #include <kern/clock.h>
39 #include <kern/locks.h>
40 #include <kern/sched_prim.h>
41 #include <mach/machine/thread_status.h>
42 #include <mach/thread_act.h>
43 #include <ppc/savearea.h>
45 #include <sys/kernel.h>
47 #include <sys/proc_internal.h>
48 #include <sys/syscall.h>
49 #include <sys/systm.h>
51 #include <sys/errno.h>
52 #include <sys/kdebug.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/kauth.h>
57 #include <bsm/audit_kernel.h>
60 extern int32_t dtrace_systrace_syscall(struct proc
*, void *, int *);
61 extern void dtrace_systrace_syscall_return(unsigned short, int, int *);
65 unix_syscall(struct savearea
*regs
);
67 extern struct savearea
*
71 extern lck_spin_t
* tz_slock
;
72 extern void throttle_lowpri_io(int *lowpri_window
, mount_t v_mount
);
75 * Function: unix_syscall
77 * Inputs: regs - pointer to Process Control Block
82 unix_syscall(struct savearea
*regs
)
85 struct uthread
*uthread
;
92 flavor
= (((unsigned int)regs
->save_r0
) == 0)? 1: 0;
99 if (kdebug_enable
&& (code
!= 180)) {
101 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
102 regs
->save_r4
, regs
->save_r5
, regs
->save_r6
, regs
->save_r7
, 0);
104 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
105 regs
->save_r3
, regs
->save_r4
, regs
->save_r5
, regs
->save_r6
, 0);
107 thread_act
= current_thread();
108 uthread
= get_bsdthread_info(thread_act
);
110 if (!(uthread
->uu_flag
& UT_VFORK
))
111 proc
= (struct proc
*)get_bsdtask_info(current_task());
113 proc
= current_proc();
115 /* Make sure there is a process associated with this task */
117 regs
->save_r3
= (long long)EPERM
;
118 /* set the "pc" to execute cerror routine */
119 regs
->save_srr0
-= 4;
120 task_terminate_internal(current_task());
121 thread_exception_return();
126 * Delayed binding of thread credential to process credential, if we
127 * are not running with an explicitly set thread credential.
129 kauth_cred_uthread_update(uthread
, proc
);
131 callp
= (code
>= NUM_SYSENT
) ? &sysent
[63] : &sysent
[code
];
133 if (callp
->sy_narg
!= 0) {
137 if (IS_64BIT_PROCESS(proc
)) {
138 /* XXX Turn 64 bit unsafe calls into nosys() */
139 if (callp
->sy_flags
& UNSAFE_64BIT
) {
143 mungerp
= callp
->sy_arg_munge64
;
146 mungerp
= callp
->sy_arg_munge32
;
149 regsp
= (void *) ®s
->save_r3
;
151 /* indirect system call consumes an argument so only 7 are supported */
152 if (callp
->sy_narg
> 7) {
156 regsp
= (void *) ®s
->save_r4
;
158 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
159 (*mungerp
)(regsp
, (void *) &uthread
->uu_arg
[0]);
164 uthread
->uu_flag
|= UT_NOTCANCELPT
;
166 uthread
->uu_rval
[0] = 0;
169 * r4 is volatile, if we set it to regs->save_r4 here the child
170 * will have parents r4 after execve
172 uthread
->uu_rval
[1] = 0;
177 * PPC runtime calls cerror after every unix system call, so
178 * assume no error and adjust the "pc" to skip this call.
179 * It will be set back to the cerror call if an error is detected.
181 regs
->save_srr0
+= 4;
184 uthread
->uu_iocount
= 0;
185 uthread
->uu_vpindex
= 0;
187 AUDIT_SYSCALL_ENTER(code
, proc
, uthread
);
188 error
= (*(callp
->sy_call
))(proc
, (void *)uthread
->uu_arg
, &(uthread
->uu_rval
[0]));
189 AUDIT_SYSCALL_EXIT(code
, proc
, uthread
, error
);
191 mac_thread_userret(code
, error
, thread_act
);
196 if (uthread
->uu_iocount
)
197 joe_debug("system call returned with uu_iocount != 0");
200 uthread
->t_dtrace_errno
= error
;
201 #endif /* CONFIG_DTRACE */
203 regs
= find_user_regs(thread_act
);
205 if (error
== ERESTART
) {
206 regs
->save_srr0
-= 8;
207 } else if (error
!= EJUSTRETURN
) {
209 regs
->save_r3
= (long long)error
;
210 /* set the "pc" to execute cerror routine */
211 regs
->save_srr0
-= 4;
212 } else { /* (not error) */
213 switch (callp
->sy_return_type
) {
214 case _SYSCALL_RET_INT_T
:
215 regs
->save_r3
= uthread
->uu_rval
[0];
216 regs
->save_r4
= uthread
->uu_rval
[1];
218 case _SYSCALL_RET_UINT_T
:
219 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
220 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
222 case _SYSCALL_RET_OFF_T
:
223 /* off_t returns 64 bits split across two registers for 32 bit */
224 /* process and in one register for 64 bit process */
225 if (IS_64BIT_PROCESS(proc
)) {
226 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
227 regs
->save_r3
= *retp
;
231 regs
->save_r3
= uthread
->uu_rval
[0];
232 regs
->save_r4
= uthread
->uu_rval
[1];
235 case _SYSCALL_RET_ADDR_T
:
236 case _SYSCALL_RET_SIZE_T
:
237 case _SYSCALL_RET_SSIZE_T
:
238 /* the variable length return types (user_addr_t, user_ssize_t,
239 * and user_size_t) are always the largest possible size in the
240 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
243 user_addr_t
*retp
= (user_addr_t
*)&uthread
->uu_rval
[0];
244 regs
->save_r3
= *retp
;
248 case _SYSCALL_RET_NONE
:
251 panic("unix_syscall: unknown return type");
256 /* else (error == EJUSTRETURN) { nothing } */
259 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
261 /* panic if funnel is held */
262 syscall_exit_funnelcheck();
264 if (uthread
->uu_lowpri_window
&& uthread
->v_mount
) {
266 * task is marked as a low priority I/O type
267 * and the I/O we issued while in this system call
268 * collided with normal I/O operations... we'll
269 * delay in order to mitigate the impact of this
270 * task on the normal operation of the system
272 throttle_lowpri_io(&uthread
->uu_lowpri_window
,uthread
->v_mount
);
274 if (kdebug_enable
&& (code
!= 180)) {
276 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
277 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
278 error
, uthread
->uu_rval
[1], 0, 0, 0);
280 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
281 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
284 thread_exception_return();
289 unix_syscall_return(int error
)
292 struct uthread
*uthread
;
294 struct savearea
*regs
;
296 struct sysent
*callp
;
298 thread_act
= current_thread();
299 proc
= current_proc();
300 uthread
= get_bsdthread_info(thread_act
);
302 regs
= find_user_regs(thread_act
);
304 if (regs
->save_r0
!= 0)
305 code
= regs
->save_r0
;
307 code
= regs
->save_r3
;
309 callp
= (code
>= NUM_SYSENT
) ? &sysent
[63] : &sysent
[code
];
312 if (callp
->sy_call
== dtrace_systrace_syscall
)
313 dtrace_systrace_syscall_return( code
, error
, uthread
->uu_rval
);
314 #endif /* CONFIG_DTRACE */
317 * Get index into sysent table
319 if (error
== ERESTART
) {
320 regs
->save_srr0
-= 8;
321 } else if (error
!= EJUSTRETURN
) {
323 regs
->save_r3
= (long long)error
;
324 /* set the "pc" to execute cerror routine */
325 regs
->save_srr0
-= 4;
326 } else { /* (not error) */
327 switch (callp
->sy_return_type
) {
328 case _SYSCALL_RET_INT_T
:
329 regs
->save_r3
= uthread
->uu_rval
[0];
330 regs
->save_r4
= uthread
->uu_rval
[1];
332 case _SYSCALL_RET_UINT_T
:
333 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
334 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
336 case _SYSCALL_RET_OFF_T
:
337 /* off_t returns 64 bits split across two registers for 32 bit */
338 /* process and in one register for 64 bit process */
339 if (IS_64BIT_PROCESS(proc
)) {
340 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
341 regs
->save_r3
= *retp
;
344 regs
->save_r3
= uthread
->uu_rval
[0];
345 regs
->save_r4
= uthread
->uu_rval
[1];
348 case _SYSCALL_RET_ADDR_T
:
349 case _SYSCALL_RET_SIZE_T
:
350 case _SYSCALL_RET_SSIZE_T
:
351 /* the variable length return types (user_addr_t, user_ssize_t,
352 * and user_size_t) are always the largest possible size in the
353 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
356 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
357 regs
->save_r3
= *retp
;
360 case _SYSCALL_RET_NONE
:
363 panic("unix_syscall: unknown return type");
368 /* else (error == EJUSTRETURN) { nothing } */
371 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
373 /* panic if funnel is held */
374 syscall_exit_funnelcheck();
376 if (uthread
->uu_lowpri_window
&& uthread
->v_mount
) {
378 * task is marked as a low priority I/O type
379 * and the I/O we issued while in this system call
380 * collided with normal I/O operations... we'll
381 * delay in order to mitigate the impact of this
382 * task on the normal operation of the system
384 throttle_lowpri_io(&uthread
->uu_lowpri_window
,uthread
->v_mount
);
386 if (kdebug_enable
&& (code
!= 180)) {
387 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
388 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
389 error
, uthread
->uu_rval
[1], 0, 0, 0);
391 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
392 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
395 thread_exception_return();