]>
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
;
74 * Function: unix_syscall
76 * Inputs: regs - pointer to Process Control Block
81 unix_syscall(struct savearea
*regs
)
84 struct uthread
*uthread
;
91 flavor
= (((unsigned int)regs
->save_r0
) == 0)? 1: 0;
98 if (kdebug_enable
&& (code
!= 180)) {
100 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
101 regs
->save_r4
, regs
->save_r5
, regs
->save_r6
, regs
->save_r7
, 0);
103 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_START
,
104 regs
->save_r3
, regs
->save_r4
, regs
->save_r5
, regs
->save_r6
, 0);
106 thread_act
= current_thread();
107 uthread
= get_bsdthread_info(thread_act
);
109 if (!(uthread
->uu_flag
& UT_VFORK
))
110 proc
= (struct proc
*)get_bsdtask_info(current_task());
112 proc
= current_proc();
114 /* Make sure there is a process associated with this task */
116 regs
->save_r3
= (long long)EPERM
;
117 /* set the "pc" to execute cerror routine */
118 regs
->save_srr0
-= 4;
119 task_terminate_internal(current_task());
120 thread_exception_return();
125 * Delayed binding of thread credential to process credential, if we
126 * are not running with an explicitly set thread credential.
128 kauth_cred_uthread_update(uthread
, proc
);
130 callp
= (code
>= NUM_SYSENT
) ? &sysent
[63] : &sysent
[code
];
132 if (callp
->sy_narg
!= 0) {
136 if (IS_64BIT_PROCESS(proc
)) {
137 /* XXX Turn 64 bit unsafe calls into nosys() */
138 if (callp
->sy_flags
& UNSAFE_64BIT
) {
142 mungerp
= callp
->sy_arg_munge64
;
145 mungerp
= callp
->sy_arg_munge32
;
148 regsp
= (void *) ®s
->save_r3
;
150 /* indirect system call consumes an argument so only 7 are supported */
151 if (callp
->sy_narg
> 7) {
155 regsp
= (void *) ®s
->save_r4
;
157 /* call syscall argument munger to copy in arguments (see xnu/bsd/dev/ppc/munge.s) */
158 (*mungerp
)(regsp
, (void *) &uthread
->uu_arg
[0]);
163 uthread
->uu_flag
|= UT_NOTCANCELPT
;
165 uthread
->uu_rval
[0] = 0;
168 * r4 is volatile, if we set it to regs->save_r4 here the child
169 * will have parents r4 after execve
171 uthread
->uu_rval
[1] = 0;
176 * PPC runtime calls cerror after every unix system call, so
177 * assume no error and adjust the "pc" to skip this call.
178 * It will be set back to the cerror call if an error is detected.
180 regs
->save_srr0
+= 4;
183 uthread
->uu_iocount
= 0;
184 uthread
->uu_vpindex
= 0;
186 AUDIT_SYSCALL_ENTER(code
, proc
, uthread
);
187 error
= (*(callp
->sy_call
))(proc
, (void *)uthread
->uu_arg
, &(uthread
->uu_rval
[0]));
188 AUDIT_SYSCALL_EXIT(code
, proc
, uthread
, error
);
190 mac_thread_userret(code
, error
, thread_act
);
195 if (uthread
->uu_iocount
)
196 joe_debug("system call returned with uu_iocount != 0");
199 uthread
->t_dtrace_errno
= error
;
200 #endif /* CONFIG_DTRACE */
202 regs
= find_user_regs(thread_act
);
204 if (error
== ERESTART
) {
205 regs
->save_srr0
-= 8;
206 } else if (error
!= EJUSTRETURN
) {
208 regs
->save_r3
= (long long)error
;
209 /* set the "pc" to execute cerror routine */
210 regs
->save_srr0
-= 4;
211 } else { /* (not error) */
212 switch (callp
->sy_return_type
) {
213 case _SYSCALL_RET_INT_T
:
214 regs
->save_r3
= uthread
->uu_rval
[0];
215 regs
->save_r4
= uthread
->uu_rval
[1];
217 case _SYSCALL_RET_UINT_T
:
218 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
219 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
221 case _SYSCALL_RET_OFF_T
:
222 /* off_t returns 64 bits split across two registers for 32 bit */
223 /* process and in one register for 64 bit process */
224 if (IS_64BIT_PROCESS(proc
)) {
225 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
226 regs
->save_r3
= *retp
;
230 regs
->save_r3
= uthread
->uu_rval
[0];
231 regs
->save_r4
= uthread
->uu_rval
[1];
234 case _SYSCALL_RET_ADDR_T
:
235 case _SYSCALL_RET_SIZE_T
:
236 case _SYSCALL_RET_SSIZE_T
:
237 /* the variable length return types (user_addr_t, user_ssize_t,
238 * and user_size_t) are always the largest possible size in the
239 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
242 user_addr_t
*retp
= (user_addr_t
*)&uthread
->uu_rval
[0];
243 regs
->save_r3
= *retp
;
247 case _SYSCALL_RET_NONE
:
250 panic("unix_syscall: unknown return type");
255 /* else (error == EJUSTRETURN) { nothing } */
258 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
260 /* panic if funnel is held */
261 syscall_exit_funnelcheck();
263 if (uthread
->uu_lowpri_window
) {
265 * task is marked as a low priority I/O type
266 * and the I/O we issued while in this system call
267 * collided with normal I/O operations... we'll
268 * delay in order to mitigate the impact of this
269 * task on the normal operation of the system
271 throttle_lowpri_io(TRUE
);
273 if (kdebug_enable
&& (code
!= 180)) {
275 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
276 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
277 error
, uthread
->uu_rval
[1], 0, 0, 0);
279 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
280 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
283 thread_exception_return();
288 unix_syscall_return(int error
)
291 struct uthread
*uthread
;
293 struct savearea
*regs
;
295 struct sysent
*callp
;
297 thread_act
= current_thread();
298 proc
= current_proc();
299 uthread
= get_bsdthread_info(thread_act
);
301 regs
= find_user_regs(thread_act
);
303 if (regs
->save_r0
!= 0)
304 code
= regs
->save_r0
;
306 code
= regs
->save_r3
;
308 callp
= (code
>= NUM_SYSENT
) ? &sysent
[63] : &sysent
[code
];
311 if (callp
->sy_call
== dtrace_systrace_syscall
)
312 dtrace_systrace_syscall_return( code
, error
, uthread
->uu_rval
);
313 #endif /* CONFIG_DTRACE */
316 * Get index into sysent table
318 if (error
== ERESTART
) {
319 regs
->save_srr0
-= 8;
320 } else if (error
!= EJUSTRETURN
) {
322 regs
->save_r3
= (long long)error
;
323 /* set the "pc" to execute cerror routine */
324 regs
->save_srr0
-= 4;
325 } else { /* (not error) */
326 switch (callp
->sy_return_type
) {
327 case _SYSCALL_RET_INT_T
:
328 regs
->save_r3
= uthread
->uu_rval
[0];
329 regs
->save_r4
= uthread
->uu_rval
[1];
331 case _SYSCALL_RET_UINT_T
:
332 regs
->save_r3
= ((u_int
)uthread
->uu_rval
[0]);
333 regs
->save_r4
= ((u_int
)uthread
->uu_rval
[1]);
335 case _SYSCALL_RET_OFF_T
:
336 /* off_t returns 64 bits split across two registers for 32 bit */
337 /* process and in one register for 64 bit process */
338 if (IS_64BIT_PROCESS(proc
)) {
339 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
340 regs
->save_r3
= *retp
;
343 regs
->save_r3
= uthread
->uu_rval
[0];
344 regs
->save_r4
= uthread
->uu_rval
[1];
347 case _SYSCALL_RET_ADDR_T
:
348 case _SYSCALL_RET_SIZE_T
:
349 case _SYSCALL_RET_SSIZE_T
:
350 /* the variable length return types (user_addr_t, user_ssize_t,
351 * and user_size_t) are always the largest possible size in the
352 * kernel (we use uu_rval[0] and [1] as one 64 bit value).
355 u_int64_t
*retp
= (u_int64_t
*)&uthread
->uu_rval
[0];
356 regs
->save_r3
= *retp
;
359 case _SYSCALL_RET_NONE
:
362 panic("unix_syscall: unknown return type");
367 /* else (error == EJUSTRETURN) { nothing } */
370 uthread
->uu_flag
&= ~UT_NOTCANCELPT
;
372 /* panic if funnel is held */
373 syscall_exit_funnelcheck();
375 if (uthread
->uu_lowpri_window
) {
377 * task is marked as a low priority I/O type
378 * and the I/O we issued while in this system call
379 * collided with normal I/O operations... we'll
380 * delay in order to mitigate the impact of this
381 * task on the normal operation of the system
383 throttle_lowpri_io(TRUE
);
385 if (kdebug_enable
&& (code
!= 180)) {
386 if (callp
->sy_return_type
== _SYSCALL_RET_SSIZE_T
)
387 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
388 error
, uthread
->uu_rval
[1], 0, 0, 0);
390 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_EXCP_SC
, code
) | DBG_FUNC_END
,
391 error
, uthread
->uu_rval
[0], uthread
->uu_rval
[1], 0, 0);
394 thread_exception_return();