2 * Copyright (c) 2019 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@
30 #include <mach_debug.h>
31 #include <mach_ldebug.h>
33 #include <mach/kern_return.h>
34 #include <mach/mach_traps.h>
35 #include <mach/vm_param.h>
37 #include <kern/counters.h>
38 #include <kern/cpu_data.h>
39 #include <arm/cpu_data_internal.h>
40 #include <kern/mach_param.h>
41 #include <kern/task.h>
42 #include <kern/thread.h>
43 #include <kern/sched_prim.h>
44 #include <kern/misc_protos.h>
45 #include <kern/assert.h>
47 #include <kern/syscall_sw.h>
48 #include <ipc/ipc_port.h>
49 #include <vm/vm_kern.h>
50 #include <mach/thread_status.h>
53 #include <sys/kdebug.h>
55 #include <sys/syscall.h>
58 #include <security/mac_mach_internal.h>
61 extern void throttle_lowpri_io(int);
62 extern arm_debug_state64_t
*find_or_allocate_debug_state64(thread_t thread
);
63 void mach_syscall(struct arm_saved_state
*);
64 typedef kern_return_t (*mach_call_t
)(void *);
66 struct mach_call_args
{
79 arm_set_mach_syscall_ret(struct arm_saved_state
*state
, int retval
)
81 if (is_saved_state32(state
)) {
82 saved_state32(state
)->r
[0] = retval
;
84 saved_state64(state
)->x
[0] = retval
;
89 arm_get_mach_syscall_args(struct arm_saved_state
*state
, struct mach_call_args
*dest
, const mach_trap_t
*trapp
)
93 if (is_saved_state32(state
)) {
94 /* The trap table entry defines the number of 32-bit words to be copied in from userspace. */
95 reg_count
= trapp
->mach_trap_u32_words
;
98 * We get 7 contiguous words; r0-r6, hop over r7
99 * (frame pointer), optionally r8
101 if (reg_count
<= 7) {
102 bcopy((char*)saved_state32(state
), (char*)dest
, sizeof(uint32_t) * reg_count
);
103 } else if (reg_count
<= 9) {
104 bcopy((char*)saved_state32(state
), (char*)dest
, sizeof(uint32_t) * 7);
105 bcopy((char*)&saved_state32(state
)->r
[8], ((char*)dest
) + sizeof(uint32_t) * 7,
108 panic("Trap with %d words of args? We only support 9.", reg_count
);
111 #if CONFIG_REQUIRES_U32_MUNGING
112 trapp
->mach_trap_arg_munge32(dest
);
114 #error U32 mach traps on ARM64 kernel requires munging
117 assert(is_saved_state64(state
));
118 bcopy((char*)saved_state64(state
), (char*)dest
, trapp
->mach_trap_arg_count
* sizeof(uint64_t));
125 * Marks or unmarks the given thread to be single stepped such
126 * that it executes exactly one instruction and then takes an exception to
127 * prevent further execution.
129 * @param thread 64 bit thread to be single stepped
130 * @param on boolean value representing whether the thread should be
131 * single stepped (on is true) or not (on is false)
133 * @returns KERN_SUCCESS if the status is successfully set or KERN_FAILURE if
134 * it fails for any reason.
137 thread_setsinglestep(thread_t thread
, int on
)
139 arm_debug_state64_t
*thread_state
= find_or_allocate_debug_state64(thread
);
141 if (thread_state
== NULL
) {
146 thread_state
->mdscr_el1
|= MDSCR_SS
;
148 thread_state
->mdscr_el1
&= ~MDSCR_SS
;
151 if (thread
== current_thread()) {
152 arm_debug_set64(thread
->machine
.DebugData
);
159 vm_offset_t
dtrace_get_cpu_int_stack_top(void);
162 dtrace_get_cpu_int_stack_top(void)
164 return getCpuDatap()->intstack_top
;
166 #endif /* CONFIG_DTRACE */
167 extern const char *mach_syscall_name_table
[];
169 /* ARM64_TODO: remove this. still TODO?*/
170 extern struct proc
* current_proc(void);
171 extern int proc_pid(struct proc
*);
174 mach_syscall(struct arm_saved_state
*state
)
176 kern_return_t retval
;
177 mach_call_t mach_call
;
178 struct mach_call_args args
= {
189 int call_number
= get_saved_state_svc_number(state
);
193 struct uthread
*ut
= get_bsdthread_info(current_thread());
194 uthread_reset_proc_refcount(ut
);
196 assert(call_number
< 0); /* Otherwise it would be a Unix syscall */
197 call_number
= -call_number
;
199 if (call_number
>= MACH_TRAP_TABLE_COUNT
) {
203 DEBUG_KPRINT_SYSCALL_MACH(
204 "mach_syscall: code=%d(%s) (pid %d, tid %lld)\n",
205 call_number
, mach_syscall_name_table
[call_number
],
206 proc_pid(current_proc()), thread_tid(current_thread()));
209 kprintf("mach_syscall(0x%08x) code=%d\n", state
, call_number
);
212 mach_call
= (mach_call_t
)mach_trap_table
[call_number
].mach_trap_function
;
214 if (mach_call
== (mach_call_t
)kern_invalid
) {
215 DEBUG_KPRINT_SYSCALL_MACH(
216 "mach_syscall: kern_invalid 0x%x\n", call_number
);
220 argc
= mach_trap_table
[call_number
].mach_trap_arg_count
;
222 retval
= arm_get_mach_syscall_args(state
, &args
, &mach_trap_table
[call_number
]);
223 if (retval
!= KERN_SUCCESS
) {
224 arm_set_mach_syscall_ret(state
, retval
);
226 DEBUG_KPRINT_SYSCALL_MACH(
227 "mach_syscall: retval=0x%x\n", retval
);
232 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
233 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_START
,
234 args
.arg1
, args
.arg2
, args
.arg3
, args
.arg4
, 0);
238 * Check syscall filter mask, if exists.
240 * Not all mach traps are filtered. e.g., mach_absolute_time() and
241 * mach_continuous_time(). See handle_svc().
243 task_t task
= current_task();
244 uint8_t *filter_mask
= task
->mach_trap_filter_mask
;
246 if (__improbable(filter_mask
!= NULL
&&
247 !bitstr_test(filter_mask
, call_number
))) {
248 if (mac_task_mach_trap_evaluate
!= NULL
) {
249 retval
= mac_task_mach_trap_evaluate(get_bsdtask_info(task
),
256 #endif /* CONFIG_MACF */
258 retval
= mach_call(&args
);
264 DEBUG_KPRINT_SYSCALL_MACH("mach_syscall: retval=0x%x (pid %d, tid %lld)\n", retval
,
265 proc_pid(current_proc()), thread_tid(current_thread()));
267 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
268 MACHDBG_CODE(DBG_MACH_EXCP_SC
, (call_number
)) | DBG_FUNC_END
,
271 arm_set_mach_syscall_ret(state
, retval
);
273 throttle_lowpri_io(1);
275 #if DEBUG || DEVELOPMENT
276 kern_allocation_name_t
277 prior __assert_only
= thread_get_kernel_state(current_thread())->allocation_name
;
278 assertf(prior
== NULL
, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior
));
279 #endif /* DEBUG || DEVELOPMENT */
282 if (__improbable(uthread_get_proc_refcount(ut
) != 0)) {
283 panic("system call returned with uu_proc_refcount != 0");
290 exc_code
= call_number
;
291 exception_triage(EXC_SYSCALL
, &exc_code
, 1);
293 panic("Returned from exception_triage()?\n");
295 #endif /* MACH_BSD */