2 * Copyright (c) 2007 Apple Inc. All rights reserved.
7 * The contents of this file are subject to the terms of the
8 * Common Development and Distribution License, Version 1.0 only
9 * (the "License"). You may not use this file except in compliance
12 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
13 * or http://www.opensolaris.org/os/licensing.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
17 * When distributing Covered Code, include this CDDL HEADER in each
18 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
19 * If applicable, add the following below this CDDL HEADER, with the
20 * fields enclosed by brackets "[]" replaced with your own identifying
21 * information: Portions Copyright [yyyy] [name of copyright owner]
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #include <sys/fasttrap_isa.h>
31 #include <sys/fasttrap_impl.h>
32 #include <sys/dtrace.h>
33 #include <sys/dtrace_impl.h>
34 #include <kern/task.h>
36 #include <vm/vm_map.h>
37 #include <mach/mach_vm.h>
38 #include <arm/proc_reg.h>
39 #include <arm/caches_internal.h>
41 #include <sys/dtrace_ptss.h>
42 #include <kern/debug.h>
44 #include <pexpert/pexpert.h>
46 extern dtrace_id_t dtrace_probeid_error
;
48 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
49 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
51 extern int dtrace_decode_arm(uint32_t instr
);
52 extern int dtrace_decode_thumb(uint32_t instr
);
55 * Lossless User-Land Tracing on ARM
56 * ---------------------------------
58 * The details here will be fleshed out as more of this is implemented. The
59 * basic design will be the same way as tracing works in x86.
61 * Some ARM specific issues:
63 * We need to patch differently for ARM instructions and Thumb instructions.
64 * When we hit a probe, we check to see if the mode we're currently in is the
65 * same as the mode we're patching for. If not, we remove the tracepoint and
66 * abort. This ARM/Thumb information is pulled in from the arch specific
67 * information in the fasttrap probe.
69 * On ARM, any instruction that uses registers can also use the pc as a
70 * register. This presents problems during emulation because we have copied
71 * the instruction and thus the pc can be different. Currently we've emulated
72 * any instructions that use the pc if they can be used in a return probe.
73 * Eventually we will want to support all instructions that use the pc, but
74 * to do so requires disassembling the instruction and reconstituting it by
75 * substituting a different register.
79 #define THUMB_INSTR(x) (*(uint16_t*) &(x))
81 #define SIGNEXTEND(x, v) ((((int) (x)) << (32-(v))) >> (32-(v)))
82 #define ALIGNADDR(x, v) (((x) >> (v)) << (v))
83 #define GETITSTATE(x) ((((x) >> 8) & 0xFC) | (((x) >> 25) & 0x3))
84 #define ISLASTINIT(x) (((x) & 0xF) == 8)
86 #define SET16(x, w) *((uint16_t*) (x)) = (w)
87 #define SET32(x, w) *((uint32_t*) (x)) = (w)
89 #define IS_ARM_NOP(x) ((x) == 0xE1A00000)
90 /* Marker for is-enabled probes */
91 #define IS_ARM_IS_ENABLED(x) ((x) == 0xE0200000)
93 #define IS_THUMB_NOP(x) ((x) == 0x46C0)
94 /* Marker for is-enabled probes */
95 #define IS_THUMB_IS_ENABLED(x) ((x) == 0x4040)
97 #define ARM_LDM_UF (1 << 23)
98 #define ARM_LDM_PF (1 << 24)
99 #define ARM_LDM_WF (1 << 21)
101 #define ARM_LDR_UF (1 << 23)
102 #define ARM_LDR_BF (1 << 22)
104 extern int dtrace_arm_condition_true(int cond
, int cpsr
);
107 fasttrap_tracepoint_init(proc_t
*p
, fasttrap_tracepoint_t
*tp
,
108 user_addr_t pc
, fasttrap_probe_type_t type
)
114 * Read the instruction at the given address out of the process's
115 * address space. We don't have to worry about a debugger
116 * changing this instruction before we overwrite it with our trap
117 * instruction since P_PR_LOCK is set. Since instructions can span
118 * pages, we potentially read the instruction in two parts. If the
119 * second part fails, we just zero out that part of the instruction.
122 * APPLE NOTE: Of course, we do not have a P_PR_LOCK, so this is racey...
125 if (uread(p
, &instr
, 4, pc
) != 0) {
129 /* We want &instr to always point to the saved instruction, so just copy the
130 * whole thing When cast to a pointer to a uint16_t, that will give us a
131 * pointer to the first two bytes, which is the thumb instruction.
133 tp
->ftt_instr
= instr
;
135 if (tp
->ftt_fntype
!= FASTTRAP_FN_DONE_INIT
) {
136 switch (tp
->ftt_fntype
) {
137 case FASTTRAP_FN_UNKNOWN
:
138 /* Can't instrument without any information. We can add some heuristics later if necessary. */
141 case FASTTRAP_FN_USDT
:
142 if (IS_ARM_NOP(instr
) || IS_ARM_IS_ENABLED(instr
)) {
144 } else if (IS_THUMB_NOP(THUMB_INSTR(instr
)) || IS_THUMB_IS_ENABLED(THUMB_INSTR(instr
))) {
147 /* Shouldn't reach here - this means we don't recognize
148 * the instruction at one of the USDT probe locations
152 tp
->ftt_fntype
= FASTTRAP_FN_DONE_INIT
;
155 case FASTTRAP_FN_ARM
:
157 tp
->ftt_fntype
= FASTTRAP_FN_DONE_INIT
;
160 case FASTTRAP_FN_THUMB
:
162 tp
->ftt_fntype
= FASTTRAP_FN_DONE_INIT
;
171 tp
->ftt_type
= dtrace_decode_thumb(instr
);
173 tp
->ftt_type
= dtrace_decode_arm(instr
);
176 if (tp
->ftt_type
== FASTTRAP_T_INV
) {
177 /* This is an instruction we either don't recognize or can't instrument */
178 printf("dtrace: fasttrap: Unrecognized instruction: %08x at %08x\n",
179 (tp
->ftt_thumb
&& dtrace_instr_size(tp
->ftt_instr
, tp
->ftt_thumb
) == 2) ? tp
->ftt_instr1
: instr
, pc
);
187 fasttrap_tracepoint_install(proc_t
*p
, fasttrap_tracepoint_t
*tp
)
189 /* The thumb patch is a 2 byte instruction regardless of the size of the original instruction */
191 int size
= tp
->ftt_thumb
? 2 : 4;
194 *((uint16_t*) &instr
) = FASTTRAP_THUMB_INSTR
;
196 instr
= FASTTRAP_ARM_INSTR
;
199 if (uwrite(p
, &instr
, size
, tp
->ftt_pc
) != 0) {
203 tp
->ftt_installed
= 1;
209 fasttrap_tracepoint_remove(proc_t
*p
, fasttrap_tracepoint_t
*tp
)
211 /* The thumb patch is a 2 byte instruction regardless of the size of the original instruction */
213 int size
= tp
->ftt_thumb
? 2 : 4;
216 * Distinguish between read or write failures and a changed
219 if (uread(p
, &instr
, size
, tp
->ftt_pc
) != 0) {
223 if (*((uint16_t*) &instr
) != FASTTRAP_THUMB_INSTR
) {
227 if (instr
!= FASTTRAP_ARM_INSTR
) {
231 if (uwrite(p
, &tp
->ftt_instr
, size
, tp
->ftt_pc
) != 0) {
236 tp
->ftt_installed
= 0;
242 fasttrap_return_common(proc_t
*p
, arm_saved_state_t
*regs
, user_addr_t pc
, user_addr_t new_pc
)
244 pid_t pid
= p
->p_pid
;
245 fasttrap_tracepoint_t
*tp
;
246 fasttrap_bucket_t
*bucket
;
251 pid_mtx
= &cpu_core
[CPU
->cpu_id
].cpuc_pid_lock
;
252 lck_mtx_lock(pid_mtx
);
253 bucket
= &fasttrap_tpoints
.fth_table
[FASTTRAP_TPOINTS_INDEX(pid
, pc
)];
255 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
256 if (pid
== tp
->ftt_pid
&& pc
== tp
->ftt_pc
&&
257 tp
->ftt_proc
->ftpc_acount
!= 0) {
263 * Don't sweat it if we can't find the tracepoint again; unlike
264 * when we're in fasttrap_pid_probe(), finding the tracepoint here
265 * is not essential to the correct execution of the process.
268 lck_mtx_unlock(pid_mtx
);
272 for (id
= tp
->ftt_retids
; id
!= NULL
; id
= id
->fti_next
) {
273 fasttrap_probe_t
*probe
= id
->fti_probe
;
275 * If there's a branch that could act as a return site, we
276 * need to trace it, and check here if the program counter is
277 * external to the function.
279 if (tp
->ftt_type
!= FASTTRAP_T_LDM_PC
&&
280 tp
->ftt_type
!= FASTTRAP_T_POP_PC
&&
281 new_pc
- probe
->ftp_faddr
< probe
->ftp_fsize
) {
285 if (probe
->ftp_prov
->ftp_provider_type
== DTFTP_PROVIDER_ONESHOT
) {
286 if (os_atomic_xchg(&probe
->ftp_triggered
, 1, relaxed
)) {
287 /* already triggered */
292 * If we have at least one probe associated that
293 * is not a oneshot probe, don't remove the
299 #if defined(XNU_TARGET_OS_OSX)
300 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
)) {
301 dtrace_probe(dtrace_probeid_error
, 0 /* state */, id
->fti_probe
->ftp_id
,
302 1 /* ndx */, -1 /* offset */, DTRACEFLT_UPRIV
);
305 #endif /* defined(XNU_TARGET_OS_OSX) */
307 dtrace_probe(id
->fti_probe
->ftp_id
,
308 pc
- id
->fti_probe
->ftp_faddr
,
309 regs
->r
[0], 0, 0, 0);
313 fasttrap_tracepoint_retire(p
, tp
);
316 lck_mtx_unlock(pid_mtx
);
323 fasttrap_sigsegv(proc_t
*p
, uthread_t t
, user_addr_t addr
, arm_saved_state_t
*regs
)
325 /* TODO: This function isn't implemented yet. In debug mode, panic the system to
326 * find out why we're hitting this point. In other modes, kill the process.
329 #pragma unused(p,t,addr,arm_saved_state)
330 panic("fasttrap: sigsegv not yet implemented");
332 #pragma unused(p,t,addr)
333 /* Kill the process */
340 /* Set fault address and mark signal */
342 t
->uu_siglist
|= sigmask(SIGSEGV
);
345 * XXX These two line may be redundant; if not, then we need
346 * XXX to potentially set the data address in the machine
347 * XXX specific thread state structure to indicate the address.
349 t
->uu_exception
= KERN_INVALID_ADDRESS
; /* SIGSEGV */
350 t
->uu_subcode
= 0; /* XXX pad */
355 signal_setast(t
->uu_context
.vc_thread
);
360 fasttrap_usdt_args(fasttrap_probe_t
*probe
, arm_saved_state_t
*regs
, int argc
,
363 int i
, x
, cap
= MIN(argc
, probe
->ftp_nargs
);
365 for (i
= 0; i
< cap
; i
++) {
366 x
= probe
->ftp_argmap
[i
];
369 argv
[i
] = regs
->r
[x
];
371 fasttrap_fuword32_noerr(regs
->sp
+ (x
- 4) * sizeof(uint32_t), &argv
[i
]);
375 for (; i
< argc
; i
++) {
381 set_thumb_flag(arm_saved_state_t
*regs
, user_addr_t pc
)
384 regs
->cpsr
|= PSR_TF
;
386 regs
->cpsr
&= ~PSR_TF
;
391 fasttrap_pid_probe(arm_saved_state_t
*regs
)
393 proc_t
*p
= current_proc();
394 user_addr_t new_pc
= 0;
395 fasttrap_bucket_t
*bucket
;
397 fasttrap_tracepoint_t
*tp
, tp_local
;
399 dtrace_icookie_t cookie
;
400 uint_t is_enabled
= 0;
402 int was_simulated
= 1, retire_tp
= 1;
404 user_addr_t pc
= regs
->pc
;
406 uthread_t uthread
= (uthread_t
) get_bsdthread_info(current_thread());
409 * It's possible that a user (in a veritable orgy of bad planning)
410 * could redirect this thread's flow of control before it reached the
411 * return probe fasttrap. In this case we need to kill the process
412 * since it's in a unrecoverable state.
414 if (uthread
->t_dtrace_step
) {
415 ASSERT(uthread
->t_dtrace_on
);
416 fasttrap_sigtrap(p
, uthread
, pc
);
421 * Clear all user tracing flags.
423 uthread
->t_dtrace_ft
= 0;
424 uthread
->t_dtrace_pc
= 0;
425 uthread
->t_dtrace_npc
= 0;
426 uthread
->t_dtrace_scrpc
= 0;
427 uthread
->t_dtrace_astpc
= 0;
430 * Treat a child created by a call to vfork(2) as if it were its
431 * parent. We know that there's only one thread of control in such a
434 if (p
->p_lflag
& P_LINVFORK
) {
436 while (p
->p_lflag
& P_LINVFORK
) {
443 pid_mtx
= &cpu_core
[CPU
->cpu_id
].cpuc_pid_lock
;
444 lck_mtx_lock(pid_mtx
);
445 bucket
= &fasttrap_tpoints
.fth_table
[FASTTRAP_TPOINTS_INDEX(pid
, pc
)];
448 * Lookup the tracepoint that the process just hit.
450 for (tp
= bucket
->ftb_data
; tp
!= NULL
; tp
= tp
->ftt_next
) {
451 if (pid
== tp
->ftt_pid
&& pc
== tp
->ftt_pc
&&
452 tp
->ftt_proc
->ftpc_acount
!= 0) {
458 * If we couldn't find a matching tracepoint, either a tracepoint has
459 * been inserted without using the pid<pid> ioctl interface (see
460 * fasttrap_ioctl), or somehow we have mislaid this tracepoint.
463 lck_mtx_unlock(pid_mtx
);
467 /* Default to always execute */
468 int condition_code
= 0xE;
470 uint32_t itstate
= GETITSTATE(regs
->cpsr
);
472 /* In IT block, make sure it's the last statement in the block */
473 if (ISLASTINIT(itstate
)) {
474 condition_code
= itstate
>> 4;
476 printf("dtrace: fasttrap: Tried to trace instruction %08x at %08x but not at end of IT block\n",
477 (tp
->ftt_thumb
&& dtrace_instr_size(tp
->ftt_instr
, tp
->ftt_thumb
) == 2) ? tp
->ftt_instr1
: tp
->ftt_instr
, pc
);
479 fasttrap_tracepoint_remove(p
, tp
);
480 lck_mtx_unlock(pid_mtx
);
485 condition_code
= ARM_CONDCODE(tp
->ftt_instr
);
488 if (!tp
->ftt_thumb
!= !(regs
->cpsr
& PSR_TF
)) {
489 /* The ARM/Thumb mode does not match what we expected for this probe.
490 * Remove this probe and bail.
492 fasttrap_tracepoint_remove(p
, tp
);
493 lck_mtx_unlock(pid_mtx
);
497 if (tp
->ftt_ids
!= NULL
) {
501 uint32_t *stack
= (uint32_t *)regs
->sp
;
503 /* First four parameters are passed in registers */
504 fasttrap_fuword32_noerr((user_addr_t
)(uint32_t)stack
, &s4
);
506 for (id
= tp
->ftt_ids
; id
!= NULL
; id
= id
->fti_next
) {
507 fasttrap_probe_t
*probe
= id
->fti_probe
;
509 #if defined(XNU_TARGET_OS_OSX)
510 if (ISSET(current_proc()->p_lflag
, P_LNOATTACH
)) {
511 dtrace_probe(dtrace_probeid_error
, 0 /* state */, probe
->ftp_id
,
512 1 /* ndx */, -1 /* offset */, DTRACEFLT_UPRIV
);
515 #endif /* defined(XNU_TARGET_OS_OSX) */
517 if (probe
->ftp_prov
->ftp_provider_type
== DTFTP_PROVIDER_ONESHOT
) {
518 if (os_atomic_xchg(&probe
->ftp_triggered
, 1, relaxed
)) {
519 /* already triggered */
524 * If we have at least probe associated that
525 * is not a oneshot probe, don't remove the
531 if (id
->fti_ptype
== DTFTP_ENTRY
) {
533 * We note that this was an entry
534 * probe to help ustack() find the
537 cookie
= dtrace_interrupt_disable();
538 DTRACE_CPUFLAG_SET(CPU_DTRACE_ENTRY
);
539 dtrace_probe(probe
->ftp_id
, regs
->r
[0], regs
->r
[1], regs
->r
[2], regs
->r
[3], s4
);
540 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_ENTRY
);
541 dtrace_interrupt_enable(cookie
);
542 } else if (id
->fti_ptype
== DTFTP_IS_ENABLED
) {
544 * Note that in this case, we don't
545 * call dtrace_probe() since it's only
546 * an artificial probe meant to change
547 * the flow of control so that it
548 * encounters the true probe.
551 } else if (probe
->ftp_argmap
== NULL
) {
552 dtrace_probe(probe
->ftp_id
, regs
->r
[0], regs
->r
[1], regs
->r
[2], regs
->r
[3], s4
);
556 fasttrap_usdt_args(probe
, regs
, 5, t
);
557 dtrace_probe(probe
->ftp_id
, t
[0], t
[1], t
[2], t
[3], t
[4]);
562 fasttrap_tracepoint_retire(p
, tp
);
566 * We're about to do a bunch of work so we cache a local copy of
567 * the tracepoint to emulate the instruction, and then find the
568 * tracepoint again later if we need to light up any return probes.
571 lck_mtx_unlock(pid_mtx
);
575 * If there's an is-enabled probe connected to this tracepoint it
576 * means that there was a 'eor r0,r0,r0'
577 * instruction that was placed there by DTrace when the binary was
578 * linked. As this probe is, in fact, enabled, we need to stuff 1
579 * into R0. Accordingly, we can bypass all the instruction
580 * emulation logic since we know the inevitable result. It's possible
581 * that a user could construct a scenario where the 'is-enabled'
582 * probe was on some other instruction, but that would be a rather
583 * exotic way to shoot oneself in the foot.
588 new_pc
= regs
->pc
+ (tp
->ftt_thumb
? 2 : 4);
592 /* For USDT probes, bypass all the emulation logic for the nop instruction */
593 if ((tp
->ftt_thumb
&& IS_THUMB_NOP(THUMB_INSTR(tp
->ftt_instr
))) ||
594 (!tp
->ftt_thumb
&& IS_ARM_NOP(tp
->ftt_instr
))) {
595 new_pc
= regs
->pc
+ (tp
->ftt_thumb
? 2 : 4);
599 instr_size
= dtrace_instr_size(tp
->ftt_instr
, tp
->ftt_thumb
);
601 switch (tp
->ftt_type
) {
602 case FASTTRAP_T_MOV_PC_REG
:
603 case FASTTRAP_T_CPY_PC
:
605 if (!dtrace_arm_condition_true(condition_code
, regs
->cpsr
)) {
606 new_pc
= pc
+ instr_size
;
612 rm
= THUMB16_HRM(tp
->ftt_instr1
);
614 rm
= tp
->ftt_instr
& 0xF;
616 new_pc
= regs
->r
[rm
];
618 /* This instruction does not change the Thumb state */
623 case FASTTRAP_T_STM_LR
:
624 case FASTTRAP_T_PUSH_LR
:
627 * This is a very common case, so we want to emulate this instruction if
628 * possible. However, on a push, it is possible that we might reach the end
629 * of a page and have to allocate a new page. Most of the time this will not
630 * happen, and we know that the push instruction can store at most 16 words,
631 * so check to see if we are far from the boundary, and if so, emulate. This
632 * can be made more aggressive by checking the actual number of words being
633 * pushed, but we won't do that for now.
635 * Some of the same issues that apply to POP_PC probably apply here also.
642 if (!dtrace_arm_condition_true(condition_code
, regs
->cpsr
)) {
643 new_pc
= pc
+ instr_size
;
647 base
= (uintptr_t*) regs
->sp
;
648 if (((((uintptr_t) base
) - 16 * 4) >> PAGE_SHIFT
) != (((uintptr_t) base
) >> PAGE_SHIFT
)) {
649 /* Crosses the page boundary, go to emulation */
654 if (instr_size
== 4) {
655 /* We know we have to push lr, never push sp or pc */
656 reglist
= tp
->ftt_instr2
& 0x1FFF;
658 reglist
= tp
->ftt_instr1
& 0xFF;
661 /* We know we have to push lr, never push sp or pc */
662 reglist
= tp
->ftt_instr
& 0x1FFF;
665 /* Push the link register */
667 ret
= fasttrap_suword32((uint32_t) base
, regs
->lr
);
669 fasttrap_sigsegv(p
, uthread
, (user_addr_t
) base
, regs
);
674 /* Start pushing from $r12 */
675 int regmask
= 1 << 12;
679 if (reglist
& regmask
) {
681 ret
= fasttrap_suword32((uint32_t) base
, regs
->r
[regnum
]);
683 fasttrap_sigsegv(p
, uthread
, (user_addr_t
) base
, regs
);
692 regs
->sp
= (uintptr_t) base
;
694 new_pc
= pc
+ instr_size
;
700 case FASTTRAP_T_LDM_PC
:
701 case FASTTRAP_T_POP_PC
:
703 /* TODO Two issues that will eventually need to be resolved:
705 * 1. Understand what the hardware does if we have to segfault (data abort) in
706 * the middle of a load multiple. We currently don't have a working segfault
707 * handler anyway, and with no swapfile we should never segfault on this load.
708 * If we do, we'll just kill the process by setting the pc to 0.
710 * 2. The emulation is no longer atomic. We currently only emulate pop for
711 * function epilogues, and so we should never have a race here because one
712 * thread should never be trying to manipulate another thread's stack frames.
713 * That is almost certainly a bug in the program.
715 * This will need to be fixed if we ever:
716 * a. Ship dtrace externally, as this could be a potential attack vector
717 * b. Support instruction level tracing, as we might then pop/ldm non epilogues.
721 /* Assume ldmia! sp/pop ... pc */
723 int regnum
= 0, reglist
;
727 if (!dtrace_arm_condition_true(condition_code
, regs
->cpsr
)) {
728 new_pc
= pc
+ instr_size
;
733 if (instr_size
== 4) {
734 /* We know we have to load the pc, don't do it twice */
735 reglist
= tp
->ftt_instr2
& 0x7FFF;
737 reglist
= tp
->ftt_instr1
& 0xFF;
740 /* We know we have to load the pc, don't do it twice */
741 reglist
= tp
->ftt_instr
& 0x7FFF;
744 base
= (uintptr_t*) regs
->sp
;
747 ret
= fasttrap_fuword32((uint32_t) base
, ®s
->r
[regnum
]);
749 fasttrap_sigsegv(p
, uthread
, (user_addr_t
) base
, regs
);
759 ret
= fasttrap_fuword32((uint32_t) base
, &new_pc
);
761 fasttrap_sigsegv(p
, uthread
, (user_addr_t
) base
, regs
);
767 regs
->sp
= (uintptr_t) base
;
769 set_thumb_flag(regs
, new_pc
);
774 case FASTTRAP_T_CB_N_Z
:
776 /* Thumb mode instruction, and not permitted in IT block, so skip the condition code check */
777 int rn
= tp
->ftt_instr1
& 0x7;
778 int offset
= (((tp
->ftt_instr1
& 0x00F8) >> 2) | ((tp
->ftt_instr1
& 0x0200) >> 3)) + 4;
779 int nonzero
= tp
->ftt_instr1
& 0x0800;
780 if (!nonzero
!= !(regs
->r
[rn
] == 0)) {
781 new_pc
= pc
+ offset
;
783 new_pc
= pc
+ instr_size
;
788 case FASTTRAP_T_B_COND
:
790 /* Use the condition code in the instruction and ignore the ITSTATE */
794 if (instr_size
== 4) {
795 code
= (tp
->ftt_instr1
>> 6) & 0xF;
796 if (code
== 14 || code
== 15) {
797 panic("fasttrap: Emulation of invalid branch");
799 int S
= (tp
->ftt_instr1
>> 10) & 1,
800 J1
= (tp
->ftt_instr2
>> 13) & 1,
801 J2
= (tp
->ftt_instr2
>> 11) & 1;
802 offset
= 4 + SIGNEXTEND(
803 (S
<< 20) | (J2
<< 19) | (J1
<< 18) |
804 ((tp
->ftt_instr1
& 0x003F) << 12) |
805 ((tp
->ftt_instr2
& 0x07FF) << 1),
808 code
= (tp
->ftt_instr1
>> 8) & 0xF;
809 if (code
== 14 || code
== 15) {
810 panic("fasttrap: Emulation of invalid branch");
812 offset
= 4 + (SIGNEXTEND(tp
->ftt_instr1
& 0xFF, 8) << 1);
815 code
= ARM_CONDCODE(tp
->ftt_instr
);
817 panic("fasttrap: Emulation of invalid branch");
819 offset
= 8 + (SIGNEXTEND(tp
->ftt_instr
& 0x00FFFFFF, 24) << 2);
822 if (dtrace_arm_condition_true(code
, regs
->cpsr
)) {
823 new_pc
= pc
+ offset
;
825 new_pc
= pc
+ instr_size
;
831 case FASTTRAP_T_B_UNCOND
:
835 /* Unconditional branches can only be taken from Thumb mode */
836 /* (This is different from an ARM branch with condition code "always") */
837 ASSERT(tp
->ftt_thumb
== 1);
839 if (!dtrace_arm_condition_true(condition_code
, regs
->cpsr
)) {
840 new_pc
= pc
+ instr_size
;
844 if (instr_size
== 4) {
845 int S
= (tp
->ftt_instr1
>> 10) & 1,
846 J1
= (tp
->ftt_instr2
>> 13) & 1,
847 J2
= (tp
->ftt_instr2
>> 11) & 1;
848 int I1
= (J1
!= S
) ? 0 : 1, I2
= (J2
!= S
) ? 0 : 1;
849 offset
= 4 + SIGNEXTEND(
850 (S
<< 24) | (I1
<< 23) | (I2
<< 22) |
851 ((tp
->ftt_instr1
& 0x03FF) << 12) |
852 ((tp
->ftt_instr2
& 0x07FF) << 1),
855 uint32_t instr1
= tp
->ftt_instr1
;
856 offset
= 4 + (SIGNEXTEND(instr1
& 0x7FF, 11) << 1);
859 new_pc
= pc
+ offset
;
864 case FASTTRAP_T_BX_REG
:
868 if (!dtrace_arm_condition_true(condition_code
, regs
->cpsr
)) {
869 new_pc
= pc
+ instr_size
;
874 reg
= THUMB16_HRM(tp
->ftt_instr1
);
876 reg
= ARM_RM(tp
->ftt_instr
);
878 new_pc
= regs
->r
[reg
];
879 set_thumb_flag(regs
, new_pc
);
884 case FASTTRAP_T_LDR_PC_IMMED
:
885 case FASTTRAP_T_VLDR_PC_IMMED
:
886 /* Handle these instructions by replacing the PC in the instruction with another
887 * register. They are common, so we'd like to support them, and this way we do so
888 * without any risk of having to simulate a segfault.
894 case FASTTRAP_T_COMMON
:
899 fasttrap_instr_t emul_instr
;
900 emul_instr
.instr32
= tp
->ftt_instr
;
904 * Unfortunately sometimes when we emulate the instruction and have to replace the
905 * PC, there is no longer a thumb mode equivalent. We end up having to run the
906 * modified instruction in ARM mode. We use this variable to keep track of which
907 * mode we should emulate in. We still use the original variable to determine
908 * what mode to return to.
910 uint8_t emul_thumb
= tp
->ftt_thumb
;
912 uint32_t save_val
= 0;
915 * Dealing with condition codes and emulation:
916 * We can't just uniformly do a condition code check here because not all instructions
917 * have condition codes. We currently do not support an instruction by instruction trace,
918 * so we can assume that either: 1. We are executing a Thumb instruction, in which case
919 * we either are not in an IT block and should execute always, or we are last in an IT
920 * block. Either way, the traced instruction will run correctly, and we won't have any
921 * problems when we return to the original code, because we will no longer be in the IT
922 * block. 2. We are executing an ARM instruction, in which case we are ok as long as
923 * we don't attempt to change the condition code.
925 if (tp
->ftt_type
== FASTTRAP_T_LDR_PC_IMMED
) {
926 /* We know we always have a free register (the one we plan to write the
927 * result value to!). So we'll replace the pc with that one.
931 /* Check to see if thumb or thumb2 */
932 if (instr_size
== 2) {
934 * Sadness. We need to emulate this instruction in ARM mode
935 * because it has an 8 bit immediate offset. Instead of having
936 * to deal with condition codes in the ARM instruction, we'll
937 * just check the condition and abort if the condition is false.
939 if (!dtrace_arm_condition_true(condition_code
, regs
->cpsr
)) {
940 new_pc
= pc
+ instr_size
;
944 new_reg
= (tp
->ftt_instr1
>> 8) & 0x7;
945 regs
->r
[new_reg
] = ALIGNADDR(regs
->pc
+ 4, 2);
947 emul_instr
.instr32
= 0xE5900000 | (new_reg
<< 16) | (new_reg
<< 12) | ((tp
->ftt_instr1
& 0xFF) << 2);
949 /* Thumb2. Just replace the register. */
950 new_reg
= (tp
->ftt_instr2
>> 12) & 0xF;
951 regs
->r
[new_reg
] = ALIGNADDR(regs
->pc
+ 4, 2);
952 emul_instr
.instr16
.instr1
&= ~0x000F;
953 emul_instr
.instr16
.instr1
|= new_reg
;
956 /* ARM. Just replace the register. */
957 new_reg
= (tp
->ftt_instr
>> 12) & 0xF;
958 regs
->r
[new_reg
] = ALIGNADDR(regs
->pc
+ 8, 2);
959 emul_instr
.instr32
&= ~0x000F0000;
960 emul_instr
.instr32
|= new_reg
<< 16;
962 } else if (tp
->ftt_type
== FASTTRAP_T_VLDR_PC_IMMED
) {
963 /* This instruction only uses one register, and if we're here, we know
964 * it must be the pc. So we'll just replace it with R0.
967 save_val
= regs
->r
[0];
968 regs
->r
[save_reg
] = ALIGNADDR(regs
->pc
+ (tp
->ftt_thumb
? 4 : 8), 2);
970 emul_instr
.instr16
.instr1
&= ~0x000F;
972 emul_instr
.instr32
&= ~0x000F0000;
976 emul_instr_size
= dtrace_instr_size(emul_instr
.instr32
, emul_thumb
);
980 * tp->ftt_thumb = thumb mode of original instruction
981 * emul_thumb = thumb mode for emulation
982 * emul_instr = instruction we are using to emulate original instruction
983 * emul_instr_size = size of emulating instruction
986 addr
= uthread
->t_dtrace_scratch
->addr
;
989 fasttrap_sigtrap(p
, uthread
, pc
); // Should be killing target proc
994 uthread
->t_dtrace_scrpc
= addr
;
997 * No way to do an unconditional branch in Thumb mode, shove the address
998 * onto the user stack and go to the next location with a pop. This can
999 * segfault if this push happens to cross a stack page, but that's ok, since
1000 * we are running in userland, and the kernel knows how to handle userland
1001 * stack expansions correctly.
1003 * Layout of scratch space for Thumb mode:
1004 * Emulated instruction
1005 * ldr save_reg, [pc, #16] (if necessary, restore any register we clobbered)
1010 * Location we should return to in original program
1011 * Saved value of clobbered register (if necessary)
1014 bcopy(&emul_instr
, &scratch
[i
], emul_instr_size
); i
+= emul_instr_size
;
1016 if (save_reg
!= -1) {
1017 uint16_t restore_inst
= 0x4803;
1018 restore_inst
|= (save_reg
& 0x7) << 8;
1019 SET16(scratch
+ i
, restore_inst
); i
+= 2; // ldr reg, [pc , #16]
1022 SET16(scratch
+ i
, 0xB403); i
+= 2; // push { r0, r1 }
1023 SET16(scratch
+ i
, 0x4801); i
+= 2; // ldr r0, [pc, #4]
1024 SET16(scratch
+ i
, 0x9001); i
+= 2; // str r0, [sp, #4]
1025 SET16(scratch
+ i
, 0xBD01); i
+= 2; // pop { r0, pc }
1028 SET16(scratch
+ i
, 0); i
+= 2; // padding - saved 32 bit words must be aligned
1030 SET32(scratch
+ i
, pc
+ instr_size
+ (tp
->ftt_thumb
? 1 : 0)); i
+= 4; // Return address
1031 if (save_reg
!= -1) {
1032 SET32(scratch
+ i
, save_val
); i
+= 4; // saved value of clobbered register
1035 uthread
->t_dtrace_astpc
= addr
+ i
;
1036 bcopy(&emul_instr
, &scratch
[i
], emul_instr_size
); i
+= emul_instr_size
;
1037 SET16(scratch
+ i
, FASTTRAP_THUMB_RET_INSTR
); i
+= 2;
1040 * Layout of scratch space for ARM mode:
1041 * Emulated instruction
1042 * ldr save_reg, [pc, #12] (if necessary, restore any register we clobbered)
1044 * Location we should return to in original program
1045 * Saved value of clobbered register (if necessary)
1048 bcopy(&emul_instr
, &scratch
[i
], emul_instr_size
); i
+= emul_instr_size
;
1050 if (save_reg
!= -1) {
1051 uint32_t restore_inst
= 0xE59F0004;
1052 restore_inst
|= save_reg
<< 12;
1053 SET32(scratch
+ i
, restore_inst
); i
+= 4; // ldr reg, [pc, #12]
1055 SET32(scratch
+ i
, 0xE51FF004); i
+= 4; // ldr pc, [pc, #4]
1057 SET32(scratch
+ i
, pc
+ instr_size
+ (tp
->ftt_thumb
? 1 : 0)); i
+= 4; // Return address
1058 if (save_reg
!= -1) {
1059 SET32(scratch
+ i
, save_val
); i
+= 4; // Saved value of clobbered register
1062 uthread
->t_dtrace_astpc
= addr
+ i
;
1063 bcopy(&emul_instr
, &scratch
[i
], emul_instr_size
); i
+= emul_instr_size
;
1064 SET32(scratch
+ i
, FASTTRAP_ARM_RET_INSTR
); i
+= 4;
1067 if (uwrite(p
, scratch
, i
, uthread
->t_dtrace_scratch
->write_addr
) != KERN_SUCCESS
) {
1068 fasttrap_sigtrap(p
, uthread
, pc
);
1073 if (tp
->ftt_retids
!= NULL
) {
1074 uthread
->t_dtrace_step
= 1;
1075 uthread
->t_dtrace_ret
= 1;
1076 new_pc
= uthread
->t_dtrace_astpc
+ (emul_thumb
? 1 : 0);
1078 new_pc
= uthread
->t_dtrace_scrpc
+ (emul_thumb
? 1 : 0);
1081 uthread
->t_dtrace_pc
= pc
;
1082 uthread
->t_dtrace_npc
= pc
+ instr_size
;
1083 uthread
->t_dtrace_on
= 1;
1085 set_thumb_flag(regs
, new_pc
);
1090 panic("fasttrap: mishandled an instruction");
1097 * We're setting this earlier than Solaris does, to get a "correct"
1098 * ustack() output. In the Sun code, a() -> b() -> c() -> d() is
1099 * reported at: d, b, a. The new way gives c, b, a, which is closer
1100 * to correct, as the return instruction has already exectued.
1105 * If there were no return probes when we first found the tracepoint,
1106 * we should feel no obligation to honor any return probes that were
1107 * subsequently enabled -- they'll just have to wait until the next
1110 if (tp
->ftt_retids
!= NULL
) {
1112 * We need to wait until the results of the instruction are
1113 * apparent before invoking any return probes. If this
1114 * instruction was emulated we can just call
1115 * fasttrap_return_common(); if it needs to be executed, we
1116 * need to wait until the user thread returns to the kernel.
1119 * It used to be that only common instructions were simulated.
1120 * For performance reasons, we now simulate some instructions
1121 * when safe and go back to userland otherwise. The was_simulated
1122 * flag means we don't need to go back to userland.
1124 if (was_simulated
) {
1125 fasttrap_return_common(p
, regs
, pc
, new_pc
);
1127 ASSERT(uthread
->t_dtrace_ret
!= 0);
1128 ASSERT(uthread
->t_dtrace_pc
== pc
);
1129 ASSERT(uthread
->t_dtrace_scrpc
!= 0);
1130 ASSERT(new_pc
== uthread
->t_dtrace_astpc
);
1138 fasttrap_return_probe(arm_saved_state_t
*regs
)
1140 proc_t
*p
= current_proc();
1141 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1142 user_addr_t pc
= uthread
->t_dtrace_pc
;
1143 user_addr_t npc
= uthread
->t_dtrace_npc
;
1145 uthread
->t_dtrace_pc
= 0;
1146 uthread
->t_dtrace_npc
= 0;
1147 uthread
->t_dtrace_scrpc
= 0;
1148 uthread
->t_dtrace_astpc
= 0;
1151 * Treat a child created by a call to vfork(2) as if it were its
1152 * parent. We know that there's only one thread of control in such a
1153 * process: this one.
1155 if (p
->p_lflag
& P_LINVFORK
) {
1157 while (p
->p_lflag
& P_LINVFORK
) {
1164 * We set rp->r_pc to the address of the traced instruction so
1165 * that it appears to dtrace_probe() that we're on the original
1166 * instruction, and so that the user can't easily detect our
1167 * complex web of lies. dtrace_return_probe() (our caller)
1168 * will correctly set %pc after we return.
1172 fasttrap_return_common(p
, regs
, pc
, npc
);
1178 fasttrap_pid_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
,
1181 #pragma unused(arg, id, parg, aframes)
1182 arm_saved_state_t
* regs
= find_user_regs(current_thread());
1184 /* First four arguments are in registers */
1186 return regs
->r
[argno
];
1189 /* Look on the stack for the rest */
1191 uint32_t* sp
= (uint32_t*) regs
->sp
;
1192 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1193 value
= dtrace_fuword32((user_addr_t
) (sp
+ argno
- 4));
1194 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
| CPU_DTRACE_BADADDR
);
1200 fasttrap_usdt_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
1202 #pragma unused(arg, id, parg, argno, aframes)
1204 return fasttrap_anarg(ttolwp(curthread
)->lwp_regs
, 0, argno
);