]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2010 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | #ifdef MACH_BSD | |
1c79356b A |
29 | #include <mach_rt.h> |
30 | #include <mach_debug.h> | |
31 | #include <mach_ldebug.h> | |
32 | ||
33 | #include <mach/kern_return.h> | |
91447636 | 34 | #include <mach/mach_traps.h> |
1c79356b A |
35 | #include <mach/thread_status.h> |
36 | #include <mach/vm_param.h> | |
1c79356b A |
37 | |
38 | #include <kern/counters.h> | |
39 | #include <kern/cpu_data.h> | |
40 | #include <kern/mach_param.h> | |
41 | #include <kern/task.h> | |
42 | #include <kern/thread.h> | |
1c79356b A |
43 | #include <kern/sched_prim.h> |
44 | #include <kern/misc_protos.h> | |
45 | #include <kern/assert.h> | |
b0d623f7 | 46 | #include <kern/debug.h> |
1c79356b | 47 | #include <kern/spl.h> |
55e303ae | 48 | #include <kern/syscall_sw.h> |
1c79356b A |
49 | #include <ipc/ipc_port.h> |
50 | #include <vm/vm_kern.h> | |
51 | #include <vm/pmap.h> | |
52 | ||
91447636 | 53 | #include <i386/cpu_number.h> |
1c79356b A |
54 | #include <i386/eflags.h> |
55 | #include <i386/proc_reg.h> | |
1c79356b A |
56 | #include <i386/tss.h> |
57 | #include <i386/user_ldt.h> | |
58 | #include <i386/fpu.h> | |
1c79356b | 59 | #include <i386/machdep_call.h> |
91447636 | 60 | #include <i386/vmparam.h> |
b0d623f7 A |
61 | #include <i386/mp_desc.h> |
62 | #include <i386/misc_protos.h> | |
63 | #include <i386/thread.h> | |
0c530ab8 | 64 | #include <i386/trap.h> |
b0d623f7 | 65 | #include <i386/seg.h> |
0c530ab8 | 66 | #include <mach/i386/syscall_sw.h> |
9bccf70c | 67 | #include <sys/syscall.h> |
91447636 | 68 | #include <sys/kdebug.h> |
0c530ab8 | 69 | #include <sys/errno.h> |
91447636 A |
70 | #include <../bsd/sys/sysent.h> |
71 | ||
b0d623f7 A |
72 | #ifdef MACH_BSD |
73 | extern void mach_kauth_cred_uthread_update(void); | |
39236c6e | 74 | extern void throttle_lowpri_io(int); |
b0d623f7 A |
75 | #endif |
76 | ||
0c530ab8 A |
77 | void * find_user_regs(thread_t); |
78 | ||
1c79356b A |
79 | unsigned int get_msr_exportmask(void); |
80 | ||
81 | unsigned int get_msr_nbits(void); | |
82 | ||
83 | unsigned int get_msr_rbits(void); | |
84 | ||
85 | /* | |
86 | * thread_userstack: | |
87 | * | |
88 | * Return the user stack pointer from the machine | |
89 | * dependent thread state info. | |
90 | */ | |
91 | kern_return_t | |
92 | thread_userstack( | |
2d21ac55 A |
93 | __unused thread_t thread, |
94 | int flavor, | |
95 | thread_state_t tstate, | |
96 | __unused unsigned int count, | |
6d2010ae | 97 | mach_vm_offset_t *user_stack, |
2d21ac55 A |
98 | int *customstack |
99 | ) | |
1c79356b | 100 | { |
2d21ac55 A |
101 | if (customstack) |
102 | *customstack = 0; | |
4452a7af | 103 | |
2d21ac55 A |
104 | switch (flavor) { |
105 | case x86_THREAD_STATE32: | |
106 | { | |
107 | x86_thread_state32_t *state25; | |
108 | ||
109 | state25 = (x86_thread_state32_t *) tstate; | |
110 | ||
6d2010ae | 111 | if (state25->esp) { |
2d21ac55 | 112 | *user_stack = state25->esp; |
6d2010ae A |
113 | if (customstack) |
114 | *customstack = 1; | |
115 | } else { | |
2d21ac55 | 116 | *user_stack = VM_USRSTACK32; |
6d2010ae A |
117 | if (customstack) |
118 | *customstack = 0; | |
119 | } | |
2d21ac55 A |
120 | break; |
121 | } | |
0c530ab8 | 122 | |
2d21ac55 A |
123 | case x86_THREAD_STATE64: |
124 | { | |
125 | x86_thread_state64_t *state25; | |
126 | ||
127 | state25 = (x86_thread_state64_t *) tstate; | |
128 | ||
6d2010ae | 129 | if (state25->rsp) { |
2d21ac55 | 130 | *user_stack = state25->rsp; |
6d2010ae A |
131 | if (customstack) |
132 | *customstack = 1; | |
133 | } else { | |
2d21ac55 | 134 | *user_stack = VM_USRSTACK64; |
6d2010ae A |
135 | if (customstack) |
136 | *customstack = 0; | |
137 | } | |
2d21ac55 A |
138 | break; |
139 | } | |
1c79356b | 140 | |
2d21ac55 A |
141 | default: |
142 | return (KERN_INVALID_ARGUMENT); | |
143 | } | |
0c530ab8 | 144 | |
2d21ac55 A |
145 | return (KERN_SUCCESS); |
146 | } | |
1c79356b | 147 | |
316670eb A |
148 | /* |
149 | * thread_userstackdefault: | |
150 | * | |
151 | * Return the default stack location for the | |
152 | * thread, if otherwise unknown. | |
153 | */ | |
154 | kern_return_t | |
155 | thread_userstackdefault( | |
156 | thread_t thread, | |
157 | mach_vm_offset_t *default_user_stack) | |
158 | { | |
159 | if (thread_is_64bit(thread)) { | |
160 | *default_user_stack = VM_USRSTACK64; | |
161 | } else { | |
162 | *default_user_stack = VM_USRSTACK32; | |
163 | } | |
164 | return (KERN_SUCCESS); | |
165 | } | |
0c530ab8 | 166 | |
1c79356b A |
167 | kern_return_t |
168 | thread_entrypoint( | |
2d21ac55 A |
169 | __unused thread_t thread, |
170 | int flavor, | |
171 | thread_state_t tstate, | |
172 | __unused unsigned int count, | |
173 | mach_vm_offset_t *entry_point | |
174 | ) | |
1c79356b | 175 | { |
2d21ac55 | 176 | /* |
0c530ab8 A |
177 | * Set a default. |
178 | */ | |
2d21ac55 A |
179 | if (*entry_point == 0) |
180 | *entry_point = VM_MIN_ADDRESS; | |
181 | ||
0c530ab8 | 182 | switch (flavor) { |
2d21ac55 A |
183 | case x86_THREAD_STATE32: |
184 | { | |
185 | x86_thread_state32_t *state25; | |
0c530ab8 | 186 | |
2d21ac55 A |
187 | state25 = (i386_thread_state_t *) tstate; |
188 | *entry_point = state25->eip ? state25->eip: VM_MIN_ADDRESS; | |
189 | break; | |
190 | } | |
6601e61a | 191 | |
2d21ac55 A |
192 | case x86_THREAD_STATE64: |
193 | { | |
194 | x86_thread_state64_t *state25; | |
0c530ab8 | 195 | |
2d21ac55 A |
196 | state25 = (x86_thread_state64_t *) tstate; |
197 | *entry_point = state25->rip ? state25->rip: VM_MIN_ADDRESS64; | |
198 | break; | |
199 | } | |
200 | } | |
201 | return (KERN_SUCCESS); | |
202 | } | |
1c79356b | 203 | |
1c79356b A |
204 | /* |
205 | * FIXME - thread_set_child | |
206 | */ | |
207 | ||
91447636 | 208 | void thread_set_child(thread_t child, int pid); |
1c79356b | 209 | void |
91447636 | 210 | thread_set_child(thread_t child, int pid) |
1c79356b | 211 | { |
6d2010ae | 212 | pal_register_cache_state(child, DIRTY); |
b0d623f7 | 213 | |
2d21ac55 A |
214 | if (thread_is_64bit(child)) { |
215 | x86_saved_state64_t *iss64; | |
0c530ab8 | 216 | |
0c530ab8 A |
217 | iss64 = USER_REGS64(child); |
218 | ||
219 | iss64->rax = pid; | |
220 | iss64->rdx = 1; | |
221 | iss64->isf.rflags &= ~EFL_CF; | |
222 | } else { | |
2d21ac55 A |
223 | x86_saved_state32_t *iss32; |
224 | ||
0c530ab8 A |
225 | iss32 = USER_REGS32(child); |
226 | ||
227 | iss32->eax = pid; | |
228 | iss32->edx = 1; | |
229 | iss32->efl &= ~EFL_CF; | |
230 | } | |
1c79356b | 231 | } |
0c530ab8 A |
232 | |
233 | ||
1c79356b | 234 | |
1c79356b A |
235 | /* |
236 | * System Call handling code | |
237 | */ | |
238 | ||
91447636 A |
239 | extern long fuword(vm_offset_t); |
240 | ||
1c79356b | 241 | |
1c79356b A |
242 | |
243 | void | |
0c530ab8 | 244 | machdep_syscall(x86_saved_state_t *state) |
8f6c56a5 | 245 | { |
0c530ab8 | 246 | int args[machdep_call_count]; |
2d21ac55 | 247 | int trapno; |
0c530ab8 | 248 | int nargs; |
316670eb | 249 | const machdep_call_t *entry; |
0c530ab8 A |
250 | x86_saved_state32_t *regs; |
251 | ||
252 | assert(is_saved_state32(state)); | |
253 | regs = saved_state32(state); | |
1c79356b | 254 | |
0c530ab8 A |
255 | trapno = regs->eax; |
256 | #if DEBUG_TRACE | |
257 | kprintf("machdep_syscall(0x%08x) code=%d\n", regs, trapno); | |
258 | #endif | |
1c79356b | 259 | |
b0d623f7 A |
260 | DEBUG_KPRINT_SYSCALL_MDEP( |
261 | "machdep_syscall: trapno=%d\n", trapno); | |
262 | ||
0c530ab8 | 263 | if (trapno < 0 || trapno >= machdep_call_count) { |
2d21ac55 | 264 | regs->eax = (unsigned int)kern_invalid(NULL); |
1c79356b | 265 | |
91447636 A |
266 | thread_exception_return(); |
267 | /* NOTREACHED */ | |
268 | } | |
0c530ab8 A |
269 | entry = &machdep_call_table[trapno]; |
270 | nargs = entry->nargs; | |
91447636 | 271 | |
0c530ab8 | 272 | if (nargs != 0) { |
2d21ac55 A |
273 | if (copyin((user_addr_t) regs->uesp + sizeof (int), |
274 | (char *) args, (nargs * sizeof (int)))) { | |
275 | regs->eax = KERN_INVALID_ADDRESS; | |
6601e61a | 276 | |
0c530ab8 A |
277 | thread_exception_return(); |
278 | /* NOTREACHED */ | |
279 | } | |
1c79356b | 280 | } |
0c530ab8 | 281 | switch (nargs) { |
2d21ac55 A |
282 | case 0: |
283 | regs->eax = (*entry->routine.args_0)(); | |
0c530ab8 | 284 | break; |
2d21ac55 | 285 | case 1: |
0c530ab8 A |
286 | regs->eax = (*entry->routine.args_1)(args[0]); |
287 | break; | |
2d21ac55 A |
288 | case 2: |
289 | regs->eax = (*entry->routine.args_2)(args[0],args[1]); | |
0c530ab8 | 290 | break; |
2d21ac55 A |
291 | case 3: |
292 | if (!entry->bsd_style) | |
293 | regs->eax = (*entry->routine.args_3)(args[0],args[1],args[2]); | |
0c530ab8 | 294 | else { |
2d21ac55 A |
295 | int error; |
296 | uint32_t rval; | |
0c530ab8 | 297 | |
2d21ac55 | 298 | error = (*entry->routine.args_bsd_3)(&rval, args[0], args[1], args[2]); |
0c530ab8 | 299 | if (error) { |
2d21ac55 | 300 | regs->eax = error; |
0c530ab8 A |
301 | regs->efl |= EFL_CF; /* carry bit */ |
302 | } else { | |
2d21ac55 A |
303 | regs->eax = rval; |
304 | regs->efl &= ~EFL_CF; | |
0c530ab8 A |
305 | } |
306 | } | |
307 | break; | |
2d21ac55 | 308 | case 4: |
0c530ab8 A |
309 | regs->eax = (*entry->routine.args_4)(args[0], args[1], args[2], args[3]); |
310 | break; | |
1c79356b | 311 | |
2d21ac55 A |
312 | default: |
313 | panic("machdep_syscall: too many args"); | |
6601e61a | 314 | } |
6601e61a | 315 | |
b0d623f7 A |
316 | DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%u\n", regs->eax); |
317 | ||
39236c6e | 318 | throttle_lowpri_io(1); |
593a1d5f | 319 | |
0c530ab8 A |
320 | thread_exception_return(); |
321 | /* NOTREACHED */ | |
1c79356b A |
322 | } |
323 | ||
324 | ||
325 | void | |
0c530ab8 | 326 | machdep_syscall64(x86_saved_state_t *state) |
1c79356b | 327 | { |
2d21ac55 | 328 | int trapno; |
316670eb | 329 | const machdep_call_t *entry; |
0c530ab8 | 330 | x86_saved_state64_t *regs; |
1c79356b | 331 | |
0c530ab8 A |
332 | assert(is_saved_state64(state)); |
333 | regs = saved_state64(state); | |
1c79356b | 334 | |
b0d623f7 A |
335 | trapno = (int)(regs->rax & SYSCALL_NUMBER_MASK); |
336 | ||
337 | DEBUG_KPRINT_SYSCALL_MDEP( | |
338 | "machdep_syscall64: trapno=%d\n", trapno); | |
1c79356b | 339 | |
0c530ab8 | 340 | if (trapno < 0 || trapno >= machdep_call_count) { |
2d21ac55 | 341 | regs->rax = (unsigned int)kern_invalid(NULL); |
1c79356b | 342 | |
0c530ab8 A |
343 | thread_exception_return(); |
344 | /* NOTREACHED */ | |
1c79356b | 345 | } |
0c530ab8 | 346 | entry = &machdep_call_table64[trapno]; |
1c79356b | 347 | |
0c530ab8 | 348 | switch (entry->nargs) { |
2d21ac55 A |
349 | case 0: |
350 | regs->rax = (*entry->routine.args_0)(); | |
6601e61a | 351 | break; |
2d21ac55 | 352 | case 1: |
0c530ab8 | 353 | regs->rax = (*entry->routine.args64_1)(regs->rdi); |
55e303ae | 354 | break; |
fe8ab488 A |
355 | case 2: |
356 | regs->rax = (*entry->routine.args64_2)(regs->rdi, regs->rsi); | |
357 | break; | |
2d21ac55 A |
358 | default: |
359 | panic("machdep_syscall64: too many args"); | |
55e303ae | 360 | } |
6601e61a | 361 | |
b0d623f7 A |
362 | DEBUG_KPRINT_SYSCALL_MDEP("machdep_syscall: retval=%llu\n", regs->rax); |
363 | ||
39236c6e | 364 | throttle_lowpri_io(1); |
593a1d5f | 365 | |
0c530ab8 A |
366 | thread_exception_return(); |
367 | /* NOTREACHED */ | |
1c79356b A |
368 | } |
369 | ||
0c530ab8 | 370 | #endif /* MACH_BSD */ |
4452a7af | 371 | |
21362eb3 | 372 | |
0c530ab8 | 373 | typedef kern_return_t (*mach_call_t)(void *); |
4452a7af | 374 | |
0c530ab8 A |
375 | struct mach_call_args { |
376 | syscall_arg_t arg1; | |
377 | syscall_arg_t arg2; | |
378 | syscall_arg_t arg3; | |
379 | syscall_arg_t arg4; | |
380 | syscall_arg_t arg5; | |
381 | syscall_arg_t arg6; | |
382 | syscall_arg_t arg7; | |
383 | syscall_arg_t arg8; | |
384 | syscall_arg_t arg9; | |
385 | }; | |
4452a7af | 386 | |
0c530ab8 | 387 | static kern_return_t |
39236c6e | 388 | mach_call_arg_munger32(uint32_t sp, struct mach_call_args *args, const mach_trap_t *trapp); |
4452a7af | 389 | |
6601e61a | 390 | |
0c530ab8 | 391 | static kern_return_t |
39236c6e | 392 | mach_call_arg_munger32(uint32_t sp, struct mach_call_args *args, const mach_trap_t *trapp) |
0c530ab8 | 393 | { |
39236c6e | 394 | if (copyin((user_addr_t)(sp + sizeof(int)), (char *)args, trapp->mach_trap_u32_words * sizeof (int))) |
2d21ac55 | 395 | return KERN_INVALID_ARGUMENT; |
fe8ab488 A |
396 | #if CONFIG_REQUIRES_U32_MUNGING |
397 | trapp->mach_trap_arg_munge32(args); | |
398 | #else | |
399 | #error U32 mach traps on x86_64 kernel requires munging | |
400 | #endif | |
0c530ab8 | 401 | return KERN_SUCCESS; |
6601e61a | 402 | } |
4452a7af A |
403 | |
404 | ||
2d21ac55 | 405 | __private_extern__ void mach_call_munger(x86_saved_state_t *state); |
0c530ab8 | 406 | |
b0d623f7 A |
407 | extern const char *mach_syscall_name_table[]; |
408 | ||
0c530ab8 A |
409 | void |
410 | mach_call_munger(x86_saved_state_t *state) | |
4452a7af | 411 | { |
4452a7af | 412 | int argc; |
0c530ab8 | 413 | int call_number; |
4452a7af | 414 | mach_call_t mach_call; |
6601e61a A |
415 | kern_return_t retval; |
416 | struct mach_call_args args = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
0c530ab8 A |
417 | x86_saved_state32_t *regs; |
418 | ||
419 | assert(is_saved_state32(state)); | |
420 | regs = saved_state32(state); | |
421 | ||
422 | call_number = -(regs->eax); | |
b0d623f7 A |
423 | |
424 | DEBUG_KPRINT_SYSCALL_MACH( | |
425 | "mach_call_munger: code=%d(%s)\n", | |
426 | call_number, mach_syscall_name_table[call_number]); | |
0c530ab8 A |
427 | #if DEBUG_TRACE |
428 | kprintf("mach_call_munger(0x%08x) code=%d\n", regs, call_number); | |
429 | #endif | |
430 | ||
431 | if (call_number < 0 || call_number >= mach_trap_count) { | |
2d21ac55 | 432 | i386_exception(EXC_SYSCALL, call_number, 1); |
0c530ab8 A |
433 | /* NOTREACHED */ |
434 | } | |
435 | mach_call = (mach_call_t)mach_trap_table[call_number].mach_trap_function; | |
2d21ac55 | 436 | |
0c530ab8 | 437 | if (mach_call == (mach_call_t)kern_invalid) { |
b0d623f7 A |
438 | DEBUG_KPRINT_SYSCALL_MACH( |
439 | "mach_call_munger: kern_invalid 0x%x\n", regs->eax); | |
2d21ac55 | 440 | i386_exception(EXC_SYSCALL, call_number, 1); |
0c530ab8 A |
441 | /* NOTREACHED */ |
442 | } | |
0c530ab8 | 443 | |
2d21ac55 | 444 | argc = mach_trap_table[call_number].mach_trap_arg_count; |
0c530ab8 | 445 | if (argc) { |
39236c6e | 446 | retval = mach_call_arg_munger32(regs->uesp, &args, &mach_trap_table[call_number]); |
0c530ab8 | 447 | if (retval != KERN_SUCCESS) { |
2d21ac55 A |
448 | regs->eax = retval; |
449 | ||
b0d623f7 A |
450 | DEBUG_KPRINT_SYSCALL_MACH( |
451 | "mach_call_munger: retval=0x%x\n", retval); | |
452 | ||
0c530ab8 A |
453 | thread_exception_return(); |
454 | /* NOTREACHED */ | |
455 | } | |
4452a7af | 456 | } |
b0d623f7 A |
457 | |
458 | #ifdef MACH_BSD | |
459 | mach_kauth_cred_uthread_update(); | |
460 | #endif | |
316670eb A |
461 | |
462 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, | |
463 | MACHDBG_CODE(DBG_MACH_EXCP_SC, (call_number)) | DBG_FUNC_START, | |
464 | args.arg1, args.arg2, args.arg3, args.arg4, 0); | |
2d21ac55 | 465 | |
0c530ab8 A |
466 | retval = mach_call(&args); |
467 | ||
b0d623f7 A |
468 | DEBUG_KPRINT_SYSCALL_MACH("mach_call_munger: retval=0x%x\n", retval); |
469 | ||
316670eb A |
470 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
471 | MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number)) | DBG_FUNC_END, | |
472 | retval, 0, 0, 0, 0); | |
473 | ||
0c530ab8 | 474 | regs->eax = retval; |
2d21ac55 | 475 | |
39236c6e | 476 | throttle_lowpri_io(1); |
593a1d5f | 477 | |
0c530ab8 A |
478 | thread_exception_return(); |
479 | /* NOTREACHED */ | |
480 | } | |
481 | ||
482 | ||
2d21ac55 | 483 | __private_extern__ void mach_call_munger64(x86_saved_state_t *regs); |
0c530ab8 | 484 | |
0c530ab8 A |
485 | void |
486 | mach_call_munger64(x86_saved_state_t *state) | |
487 | { | |
488 | int call_number; | |
489 | int argc; | |
490 | mach_call_t mach_call; | |
fe8ab488 | 491 | struct mach_call_args args = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
0c530ab8 A |
492 | x86_saved_state64_t *regs; |
493 | ||
494 | assert(is_saved_state64(state)); | |
495 | regs = saved_state64(state); | |
496 | ||
b0d623f7 A |
497 | call_number = (int)(regs->rax & SYSCALL_NUMBER_MASK); |
498 | ||
499 | DEBUG_KPRINT_SYSCALL_MACH( | |
500 | "mach_call_munger64: code=%d(%s)\n", | |
501 | call_number, mach_syscall_name_table[call_number]); | |
0c530ab8 | 502 | |
316670eb A |
503 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
504 | MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number)) | DBG_FUNC_START, | |
505 | regs->rdi, regs->rsi, regs->rdx, regs->r10, 0); | |
6601e61a | 506 | |
0c530ab8 A |
507 | if (call_number < 0 || call_number >= mach_trap_count) { |
508 | i386_exception(EXC_SYSCALL, regs->rax, 1); | |
509 | /* NOTREACHED */ | |
510 | } | |
6601e61a | 511 | mach_call = (mach_call_t)mach_trap_table[call_number].mach_trap_function; |
6601e61a | 512 | |
0c530ab8 A |
513 | if (mach_call == (mach_call_t)kern_invalid) { |
514 | i386_exception(EXC_SYSCALL, regs->rax, 1); | |
515 | /* NOTREACHED */ | |
516 | } | |
517 | argc = mach_trap_table[call_number].mach_trap_arg_count; | |
fe8ab488 A |
518 | if (argc) { |
519 | int args_in_regs = MIN(6, argc); | |
0c530ab8 | 520 | |
fe8ab488 A |
521 | memcpy(&args.arg1, ®s->rdi, args_in_regs * sizeof(syscall_arg_t)); |
522 | ||
523 | if (argc > 6) { | |
0c530ab8 A |
524 | int copyin_count; |
525 | ||
fe8ab488 A |
526 | assert(argc <= 9); |
527 | copyin_count = (argc - 6) * (int)sizeof(syscall_arg_t); | |
0c530ab8 | 528 | |
fe8ab488 | 529 | if (copyin((user_addr_t)(regs->isf.rsp + sizeof(user_addr_t)), (char *)&args.arg7, copyin_count)) { |
0c530ab8 A |
530 | regs->rax = KERN_INVALID_ARGUMENT; |
531 | ||
fe8ab488 A |
532 | thread_exception_return(); |
533 | /* NOTREACHED */ | |
534 | } | |
0c530ab8 A |
535 | } |
536 | } | |
b0d623f7 A |
537 | |
538 | #ifdef MACH_BSD | |
539 | mach_kauth_cred_uthread_update(); | |
540 | #endif | |
541 | ||
fe8ab488 | 542 | regs->rax = (uint64_t)mach_call((void *)&args); |
0c530ab8 | 543 | |
b0d623f7 A |
544 | DEBUG_KPRINT_SYSCALL_MACH( "mach_call_munger64: retval=0x%llx\n", regs->rax); |
545 | ||
316670eb A |
546 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
547 | MACHDBG_CODE(DBG_MACH_EXCP_SC,(call_number)) | DBG_FUNC_END, | |
548 | regs->rax, 0, 0, 0, 0); | |
91447636 | 549 | |
39236c6e | 550 | throttle_lowpri_io(1); |
593a1d5f | 551 | |
0c530ab8 A |
552 | thread_exception_return(); |
553 | /* NOTREACHED */ | |
91447636 A |
554 | } |
555 | ||
0c530ab8 | 556 | |
91447636 A |
557 | /* |
558 | * thread_setuserstack: | |
559 | * | |
560 | * Sets the user stack pointer into the machine | |
561 | * dependent thread state info. | |
562 | */ | |
563 | void | |
564 | thread_setuserstack( | |
565 | thread_t thread, | |
566 | mach_vm_address_t user_stack) | |
567 | { | |
6d2010ae | 568 | pal_register_cache_state(thread, DIRTY); |
2d21ac55 A |
569 | if (thread_is_64bit(thread)) { |
570 | x86_saved_state64_t *iss64; | |
0c530ab8 A |
571 | |
572 | iss64 = USER_REGS64(thread); | |
5d5c5d0d | 573 | |
0c530ab8 A |
574 | iss64->isf.rsp = (uint64_t)user_stack; |
575 | } else { | |
2d21ac55 A |
576 | x86_saved_state32_t *iss32; |
577 | ||
0c530ab8 A |
578 | iss32 = USER_REGS32(thread); |
579 | ||
b0d623f7 | 580 | iss32->uesp = CAST_DOWN_EXPLICIT(unsigned int, user_stack); |
0c530ab8 | 581 | } |
91447636 A |
582 | } |
583 | ||
584 | /* | |
585 | * thread_adjuserstack: | |
586 | * | |
587 | * Returns the adjusted user stack pointer from the machine | |
588 | * dependent thread state info. Used for small (<2G) deltas. | |
589 | */ | |
590 | uint64_t | |
591 | thread_adjuserstack( | |
592 | thread_t thread, | |
593 | int adjust) | |
594 | { | |
6d2010ae | 595 | pal_register_cache_state(thread, DIRTY); |
2d21ac55 A |
596 | if (thread_is_64bit(thread)) { |
597 | x86_saved_state64_t *iss64; | |
5d5c5d0d | 598 | |
0c530ab8 A |
599 | iss64 = USER_REGS64(thread); |
600 | ||
601 | iss64->isf.rsp += adjust; | |
602 | ||
603 | return iss64->isf.rsp; | |
604 | } else { | |
2d21ac55 A |
605 | x86_saved_state32_t *iss32; |
606 | ||
0c530ab8 A |
607 | iss32 = USER_REGS32(thread); |
608 | ||
609 | iss32->uesp += adjust; | |
610 | ||
611 | return CAST_USER_ADDR_T(iss32->uesp); | |
612 | } | |
91447636 A |
613 | } |
614 | ||
615 | /* | |
616 | * thread_setentrypoint: | |
617 | * | |
618 | * Sets the user PC into the machine | |
619 | * dependent thread state info. | |
620 | */ | |
621 | void | |
0c530ab8 | 622 | thread_setentrypoint(thread_t thread, mach_vm_address_t entry) |
4452a7af | 623 | { |
6d2010ae | 624 | pal_register_cache_state(thread, DIRTY); |
2d21ac55 A |
625 | if (thread_is_64bit(thread)) { |
626 | x86_saved_state64_t *iss64; | |
4452a7af | 627 | |
0c530ab8 A |
628 | iss64 = USER_REGS64(thread); |
629 | ||
630 | iss64->isf.rip = (uint64_t)entry; | |
631 | } else { | |
2d21ac55 A |
632 | x86_saved_state32_t *iss32; |
633 | ||
0c530ab8 A |
634 | iss32 = USER_REGS32(thread); |
635 | ||
b0d623f7 | 636 | iss32->eip = CAST_DOWN_EXPLICIT(unsigned int, entry); |
0c530ab8 A |
637 | } |
638 | } | |
639 | ||
640 | ||
2d21ac55 | 641 | kern_return_t |
0c530ab8 A |
642 | thread_setsinglestep(thread_t thread, int on) |
643 | { | |
6d2010ae | 644 | pal_register_cache_state(thread, DIRTY); |
2d21ac55 A |
645 | if (thread_is_64bit(thread)) { |
646 | x86_saved_state64_t *iss64; | |
0c530ab8 A |
647 | |
648 | iss64 = USER_REGS64(thread); | |
649 | ||
650 | if (on) | |
2d21ac55 | 651 | iss64->isf.rflags |= EFL_TF; |
0c530ab8 | 652 | else |
2d21ac55 | 653 | iss64->isf.rflags &= ~EFL_TF; |
0c530ab8 | 654 | } else { |
2d21ac55 A |
655 | x86_saved_state32_t *iss32; |
656 | ||
0c530ab8 A |
657 | iss32 = USER_REGS32(thread); |
658 | ||
b0d623f7 | 659 | if (on) { |
2d21ac55 | 660 | iss32->efl |= EFL_TF; |
b0d623f7 A |
661 | /* Ensure IRET */ |
662 | if (iss32->cs == SYSENTER_CS) | |
663 | iss32->cs = SYSENTER_TF_CS; | |
664 | } | |
0c530ab8 | 665 | else |
2d21ac55 | 666 | iss32->efl &= ~EFL_TF; |
0c530ab8 | 667 | } |
2d21ac55 A |
668 | |
669 | return (KERN_SUCCESS); | |
0c530ab8 A |
670 | } |
671 | ||
672 | ||
673 | ||
674 | /* XXX this should be a struct savearea so that CHUD will work better on x86 */ | |
675 | void * | |
2d21ac55 | 676 | find_user_regs(thread_t thread) |
0c530ab8 | 677 | { |
6d2010ae | 678 | pal_register_cache_state(thread, DIRTY); |
0c530ab8 A |
679 | return USER_STATE(thread); |
680 | } | |
91447636 | 681 | |
2d21ac55 A |
682 | void * |
683 | get_user_regs(thread_t th) | |
684 | { | |
6d2010ae A |
685 | pal_register_cache_state(th, DIRTY); |
686 | return(USER_STATE(th)); | |
2d21ac55 A |
687 | } |
688 | ||
689 | #if CONFIG_DTRACE | |
690 | /* | |
691 | * DTrace would like to have a peek at the kernel interrupt state, if available. | |
692 | * Based on osfmk/chud/i386/chud_thread_i386.c:chudxnu_thread_get_state(), which see. | |
693 | */ | |
b0d623f7 | 694 | x86_saved_state_t *find_kern_regs(thread_t); |
2d21ac55 | 695 | |
b0d623f7 | 696 | x86_saved_state_t * |
2d21ac55 A |
697 | find_kern_regs(thread_t thread) |
698 | { | |
699 | if (thread == current_thread() && | |
700 | NULL != current_cpu_datap()->cpu_int_state && | |
701 | !(USER_STATE(thread) == current_cpu_datap()->cpu_int_state && | |
702 | current_cpu_datap()->cpu_interrupt_level == 1)) { | |
703 | ||
b0d623f7 | 704 | return current_cpu_datap()->cpu_int_state; |
2d21ac55 A |
705 | } else { |
706 | return NULL; | |
707 | } | |
708 | } | |
709 | ||
710 | vm_offset_t dtrace_get_cpu_int_stack_top(void); | |
711 | ||
712 | vm_offset_t | |
713 | dtrace_get_cpu_int_stack_top(void) | |
714 | { | |
715 | return current_cpu_datap()->cpu_int_stack_top; | |
716 | } | |
717 | #endif |