1 | /* |
2 | * Copyright (c) 2007 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 | #ifdef MACH_BSD |
30 | #include <mach_debug.h> |
31 | #include <mach_ldebug.h> |
32 | |
33 | #include <mach/kern_return.h> |
34 | #include <mach/mach_traps.h> |
35 | #include <mach/vm_param.h> |
36 | |
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> |
46 | #include <kern/spl.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> |
51 | #include <vm/pmap.h> |
52 | |
53 | #include <sys/kdebug.h> |
54 | |
55 | #include <sys/syscall.h> |
56 | |
57 | extern void throttle_lowpri_io(int); |
58 | void mach_syscall(struct arm_saved_state*); |
59 | typedef kern_return_t (*mach_call_t)(void *); |
60 | |
61 | struct mach_call_args { |
62 | syscall_arg_t arg1; |
63 | syscall_arg_t arg2; |
64 | syscall_arg_t arg3; |
65 | syscall_arg_t arg4; |
66 | syscall_arg_t arg5; |
67 | syscall_arg_t arg6; |
68 | syscall_arg_t arg7; |
69 | syscall_arg_t arg8; |
70 | syscall_arg_t arg9; |
71 | }; |
72 | |
73 | static void |
74 | arm_set_mach_syscall_ret(struct arm_saved_state *state, int retval) |
75 | { |
76 | if (is_saved_state32(state)) { |
77 | saved_state32(state)->r[0] = retval; |
78 | } else { |
79 | saved_state64(state)->x[0] = retval; |
80 | } |
81 | } |
82 | |
83 | static kern_return_t |
84 | arm_get_mach_syscall_args(struct arm_saved_state *state, struct mach_call_args *dest, const mach_trap_t *trapp) |
85 | { |
86 | uint32_t reg_count; |
87 | |
88 | if (is_saved_state32(state)) { |
89 | /* The trap table entry defines the number of 32-bit words to be copied in from userspace. */ |
90 | reg_count = trapp->mach_trap_u32_words; |
91 | |
92 | /* |
93 | * We get 7 contiguous words; r0-r6, hop over r7 |
94 | * (frame pointer), optionally r8 |
95 | */ |
96 | if (reg_count <= 7) { |
97 | bcopy((char*)saved_state32(state), (char*)dest, sizeof(uint32_t) * reg_count); |
98 | } else if (reg_count <= 9) { |
99 | bcopy((char*)saved_state32(state), (char*)dest, sizeof(uint32_t) * 7); |
100 | bcopy((char*)&saved_state32(state)->r[8], ((char*)dest) + sizeof(uint32_t) * 7, |
101 | reg_count - 7); |
102 | } else { |
103 | panic("Trap with %d words of args? We only support 9." , reg_count); |
104 | } |
105 | |
106 | #if CONFIG_REQUIRES_U32_MUNGING |
107 | trapp->mach_trap_arg_munge32(dest); |
108 | #else |
109 | #error U32 mach traps on ARM64 kernel requires munging |
110 | #endif |
111 | } else { |
112 | assert(is_saved_state64(state)); |
113 | bcopy((char*)saved_state64(state), (char*)dest, trapp->mach_trap_arg_count * sizeof(uint64_t)); |
114 | } |
115 | |
116 | return KERN_SUCCESS; |
117 | } |
118 | |
119 | kern_return_t |
120 | thread_setsinglestep(__unused thread_t thread, __unused int on) |
121 | { |
122 | return (KERN_FAILURE); /* XXX TODO */ |
123 | } |
124 | |
125 | #if CONFIG_DTRACE |
126 | |
127 | vm_offset_t dtrace_get_cpu_int_stack_top(void); |
128 | |
129 | vm_offset_t |
130 | dtrace_get_cpu_int_stack_top(void) |
131 | { |
132 | return getCpuDatap()->intstack_top; |
133 | } |
134 | #endif /* CONFIG_DTRACE */ |
135 | extern const char *mach_syscall_name_table[]; |
136 | |
137 | /* ARM64_TODO: remove this. still TODO?*/ |
138 | extern struct proc* current_proc(void); |
139 | extern int proc_pid(struct proc*); |
140 | |
141 | void |
142 | mach_syscall(struct arm_saved_state *state) |
143 | { |
144 | kern_return_t retval; |
145 | mach_call_t mach_call; |
146 | struct mach_call_args args = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
147 | int call_number = get_saved_state_svc_number(state); |
148 | int64_t exc_code; |
149 | int argc; |
150 | |
151 | struct uthread *ut = get_bsdthread_info(current_thread()); |
152 | uthread_reset_proc_refcount(ut); |
153 | |
154 | assert(call_number < 0); /* Otherwise it would be a Unix syscall */ |
155 | call_number = -call_number; |
156 | |
157 | if (call_number >= MACH_TRAP_TABLE_COUNT) { |
158 | goto bad; |
159 | } |
160 | |
161 | DEBUG_KPRINT_SYSCALL_MACH( |
162 | "mach_syscall: code=%d(%s) (pid %d, tid %lld)\n" , |
163 | call_number, mach_syscall_name_table[call_number], |
164 | proc_pid(current_proc()), thread_tid(current_thread())); |
165 | |
166 | #if DEBUG_TRACE |
167 | kprintf("mach_syscall(0x%08x) code=%d\n" , state, call_number); |
168 | #endif |
169 | |
170 | mach_call = (mach_call_t)mach_trap_table[call_number].mach_trap_function; |
171 | |
172 | if (mach_call == (mach_call_t)kern_invalid) { |
173 | DEBUG_KPRINT_SYSCALL_MACH( |
174 | "mach_syscall: kern_invalid 0x%x\n" , call_number); |
175 | goto bad; |
176 | } |
177 | |
178 | argc = mach_trap_table[call_number].mach_trap_arg_count; |
179 | if (argc) { |
180 | retval = arm_get_mach_syscall_args(state, &args, &mach_trap_table[call_number]); |
181 | if (retval != KERN_SUCCESS) { |
182 | arm_set_mach_syscall_ret(state, retval); |
183 | |
184 | DEBUG_KPRINT_SYSCALL_MACH( |
185 | "mach_syscall: retval=0x%x\n" , retval); |
186 | return; |
187 | } |
188 | } |
189 | |
190 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
191 | MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) | DBG_FUNC_START, |
192 | args.arg1, args.arg2, args.arg3, args.arg4, 0); |
193 | |
194 | retval = mach_call(&args); |
195 | |
196 | DEBUG_KPRINT_SYSCALL_MACH("mach_syscall: retval=0x%x (pid %d, tid %lld)\n" , retval, |
197 | proc_pid(current_proc()), thread_tid(current_thread())); |
198 | |
199 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
200 | MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number)) | DBG_FUNC_END, |
201 | retval, 0, 0, 0, 0); |
202 | |
203 | arm_set_mach_syscall_ret(state, retval); |
204 | |
205 | throttle_lowpri_io(1); |
206 | |
207 | #if DEBUG || DEVELOPMENT |
208 | kern_allocation_name_t |
209 | prior __assert_only = thread_get_kernel_state(current_thread())->allocation_name; |
210 | assertf(prior == NULL, "thread_set_allocation_name(\"%s\") not cleared" , kern_allocation_get_name(prior)); |
211 | #endif /* DEBUG || DEVELOPMENT */ |
212 | |
213 | #if PROC_REF_DEBUG |
214 | if (__improbable(uthread_get_proc_refcount(ut) != 0)) { |
215 | panic("system call returned with uu_proc_refcount != 0" ); |
216 | } |
217 | #endif |
218 | |
219 | return; |
220 | |
221 | bad: |
222 | exc_code = call_number; |
223 | exception_triage(EXC_SYSCALL, &exc_code, 1); |
224 | /* NOTREACHED */ |
225 | panic("Returned from exception_triage()?\n" ); |
226 | } |
227 | #endif /* MACH_BSD */ |
228 | |