Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | /* * Copyright (c) 2019 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #ifdef MACH_BSD #include <mach_ldebug.h> #include <mach/kern_return.h> #include <mach/mach_traps.h> #include <mach/vm_param.h> #include <kern/bits.h> #include <kern/cpu_data.h> #include <arm/cpu_data_internal.h> #include <kern/mach_param.h> #include <kern/task.h> #include <kern/thread.h> #include <kern/sched_prim.h> #include <kern/misc_protos.h> #include <kern/assert.h> #include <kern/spl.h> #include <kern/syscall_sw.h> #include <ipc/ipc_port.h> #include <vm/vm_kern.h> #include <mach/thread_status.h> #include <vm/pmap.h> #include <sys/kdebug.h> #include <sys/syscall.h> #if CONFIG_MACF #include <security/mac_mach_internal.h> #endif extern void throttle_lowpri_io(int); extern arm_debug_state64_t *find_or_allocate_debug_state64(thread_t thread); void mach_syscall(struct arm_saved_state*); typedef kern_return_t (*mach_call_t)(void *); struct mach_call_args { syscall_arg_t arg1; syscall_arg_t arg2; syscall_arg_t arg3; syscall_arg_t arg4; syscall_arg_t arg5; syscall_arg_t arg6; syscall_arg_t arg7; syscall_arg_t arg8; syscall_arg_t arg9; }; static void arm_set_mach_syscall_ret(struct arm_saved_state *state, int retval) { if (is_saved_state32(state)) { saved_state32(state)->r[0] = retval; } else { saved_state64(state)->x[0] = retval; } } static kern_return_t arm_get_mach_syscall_args(struct arm_saved_state *state, struct mach_call_args *dest, const mach_trap_t *trapp) { uint32_t reg_count; if (is_saved_state32(state)) { /* The trap table entry defines the number of 32-bit words to be copied in from userspace. */ reg_count = trapp->mach_trap_u32_words; /* * We get 7 contiguous words; r0-r6, hop over r7 * (frame pointer), optionally r8 */ if (reg_count <= 7) { bcopy((char*)saved_state32(state), (char*)dest, sizeof(uint32_t) * reg_count); } else if (reg_count <= 9) { bcopy((char*)saved_state32(state), (char*)dest, sizeof(uint32_t) * 7); bcopy((char*)&saved_state32(state)->r[8], ((char*)dest) + sizeof(uint32_t) * 7, reg_count - 7); } else { panic("Trap with %d words of args? We only support 9.", reg_count); } #if CONFIG_REQUIRES_U32_MUNGING trapp->mach_trap_arg_munge32(dest); #else #error U32 mach traps on ARM64 kernel requires munging #endif } else { assert(is_saved_state64(state)); bcopy((char*)saved_state64(state), (char*)dest, trapp->mach_trap_arg_count * sizeof(uint64_t)); } return KERN_SUCCESS; } /** * Marks or unmarks the given thread to be single stepped such * that it executes exactly one instruction and then takes an exception to * prevent further execution. * * @param thread 64 bit thread to be single stepped * @param on boolean value representing whether the thread should be * single stepped (on is true) or not (on is false) * * @returns KERN_SUCCESS if the status is successfully set or KERN_FAILURE if * it fails for any reason. */ kern_return_t thread_setsinglestep(thread_t thread, int on) { arm_debug_state64_t *thread_state = find_or_allocate_debug_state64(thread); if (thread_state == NULL) { return KERN_FAILURE; } if (on) { thread_state->mdscr_el1 |= MDSCR_SS; } else { thread_state->mdscr_el1 &= ~MDSCR_SS; } if (thread == current_thread()) { arm_debug_set64(thread->machine.DebugData); } return KERN_SUCCESS; } #if CONFIG_DTRACE vm_offset_t dtrace_get_cpu_int_stack_top(void); vm_offset_t dtrace_get_cpu_int_stack_top(void) { return getCpuDatap()->intstack_top; } #endif /* CONFIG_DTRACE */ /* ARM64_TODO: remove this. still TODO?*/ extern struct proc* current_proc(void); extern int proc_pid(struct proc*); #if CONFIG_DEBUG_SYSCALL_REJECTION extern int debug_syscall_rejection_mode; extern bool debug_syscall_rejection_handle(int syscall_mach_trap_number); #endif /* CONFIG_DEBUG_SYSCALL_REJECTION */ void mach_syscall(struct arm_saved_state *state) { kern_return_t retval; mach_call_t mach_call; struct mach_call_args args = { .arg1 = 0, .arg2 = 0, .arg3 = 0, .arg4 = 0, .arg5 = 0, .arg6 = 0, .arg7 = 0, .arg8 = 0, .arg9 = 0 }; int call_number = get_saved_state_svc_number(state); int64_t exc_code; int argc; struct uthread *ut = get_bsdthread_info(current_thread()); uthread_reset_proc_refcount(ut); assert(call_number < 0); /* Otherwise it would be a Unix syscall */ call_number = -call_number; if (call_number >= MACH_TRAP_TABLE_COUNT) { goto bad; } DEBUG_KPRINT_SYSCALL_MACH( "mach_syscall: code=%d(%s) (pid %d, tid %lld)\n", call_number, mach_syscall_name_table[call_number], proc_pid(current_proc()), thread_tid(current_thread())); #if DEBUG_TRACE kprintf("mach_syscall(0x%08x) code=%d\n", state, call_number); #endif mach_call = (mach_call_t)mach_trap_table[call_number].mach_trap_function; if (mach_call == (mach_call_t)kern_invalid) { DEBUG_KPRINT_SYSCALL_MACH( "mach_syscall: kern_invalid 0x%x\n", call_number); goto bad; } argc = mach_trap_table[call_number].mach_trap_arg_count; if (argc) { retval = arm_get_mach_syscall_args(state, &args, &mach_trap_table[call_number]); if (retval != KERN_SUCCESS) { arm_set_mach_syscall_ret(state, retval); DEBUG_KPRINT_SYSCALL_MACH( "mach_syscall: retval=0x%x\n", retval); return; } } KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) | DBG_FUNC_START, args.arg1, args.arg2, args.arg3, args.arg4, 0); #if CONFIG_MACF /* * Check syscall filter mask, if exists. * * Not all mach traps are filtered. e.g., mach_absolute_time() and * mach_continuous_time(). See handle_svc(). */ thread_ro_t tro = current_thread_ro(); task_t task = tro->tro_task; struct proc *proc = tro->tro_proc; uint8_t *filter_mask = task_get_mach_trap_filter_mask(task); if (__improbable(filter_mask != NULL && !bitstr_test(filter_mask, call_number) && mac_task_mach_trap_evaluate != NULL)) { retval = mac_task_mach_trap_evaluate(proc, call_number); if (retval != KERN_SUCCESS) { if (mach_trap_table[call_number].mach_trap_returns_port) { retval = MACH_PORT_NULL; } goto skip_machcall; } } #endif /* CONFIG_MACF */ #if CONFIG_DEBUG_SYSCALL_REJECTION bitmap_t const *rejection_mask = uthread_get_syscall_rejection_mask(ut); if (__improbable(rejection_mask != NULL && uthread_syscall_rejection_is_enabled(ut)) && !bitmap_test(rejection_mask, call_number)) { if (debug_syscall_rejection_handle(-call_number)) { if (mach_trap_table[call_number].mach_trap_returns_port) { retval = MACH_PORT_NULL; } else { retval = KERN_DENIED; } goto skip_machcall; } } #endif /* CONFIG_DEBUG_SYSCALL_REJECTION */ retval = mach_call(&args); skip_machcall: DEBUG_KPRINT_SYSCALL_MACH("mach_syscall: retval=0x%x (pid %d, tid %lld)\n", retval, proc_pid(current_proc()), thread_tid(current_thread())); KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) | DBG_FUNC_END, retval, 0, 0, 0, 0); arm_set_mach_syscall_ret(state, retval); throttle_lowpri_io(1); #if DEBUG || DEVELOPMENT kern_allocation_name_t prior __assert_only = thread_get_kernel_state(current_thread())->allocation_name; assertf(prior == NULL, "thread_set_allocation_name(\"%s\") not cleared", kern_allocation_get_name(prior)); #endif /* DEBUG || DEVELOPMENT */ uthread_assert_zero_proc_refcount(ut); return; bad: exc_code = call_number; exception_triage(EXC_SYSCALL, &exc_code, 1); /* NOTREACHED */ panic("Returned from exception_triage()?"); } #endif /* MACH_BSD */ |