]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/arm64/dtrace_subr_arm.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / bsd / dev / arm64 / dtrace_subr_arm.c
CommitLineData
5ba3f43e 1/*
cb323159 2 * Copyright (c) 2007-2018 Apple Inc. All rights reserved.
5ba3f43e
A
3 */
4/*
5 * CDDL HEADER START
6 *
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
10 * with the License.
11 *
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.
16 *
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]
22 *
23 * CDDL HEADER END
24 */
25/*
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
5ba3f43e
A
30#include <sys/dtrace.h>
31#include <sys/dtrace_glue.h>
32#include <sys/dtrace_impl.h>
33#include <sys/fasttrap.h>
34#include <sys/vm.h>
35#include <sys/user.h>
36#include <sys/kauth.h>
37#include <kern/debug.h>
38#include <arm/proc_reg.h>
39
0a7de745 40int (*dtrace_pid_probe_ptr)(arm_saved_state_t *);
5ba3f43e
A
41int (*dtrace_return_probe_ptr) (arm_saved_state_t *);
42
43kern_return_t
44dtrace_user_probe(arm_saved_state_t *);
45
46kern_return_t
47dtrace_user_probe(arm_saved_state_t *regs)
48{
49 /*
50 * FIXME
51 *
52 * The only call path into this method is always a user trap.
53 * We don't need to test for user trap, but should assert it.
54 */
55
56 lck_rw_t *rwp;
57 struct proc *p = current_proc();
58 int is_fasttrap = 0;
59
60 uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread());
61
62 kauth_cred_uthread_update(uthread, p);
63
cb323159
A
64 uint32_t pc;
65 if (copyin((user_addr_t)saved_state64(regs)->pc, &pc, sizeof(uint32_t))) {
66 return KERN_FAILURE;
5ba3f43e 67 }
cb323159 68 is_fasttrap = (pc == FASTTRAP_ARM64_RET_INSTR);
5ba3f43e
A
69
70 if (is_fasttrap) {
71 uint8_t step = uthread->t_dtrace_step;
72 uint8_t ret = uthread->t_dtrace_ret;
73 user_addr_t npc = uthread->t_dtrace_npc;
74
75 if (uthread->t_dtrace_ast) {
76 printf("dtrace_user_probe() should be calling aston()\n");
77 // aston(thread);
78 // uthread->t_sig_check = 1;
79 }
80
81 /*
82 * Clear all user tracing flags.
83 */
84 uthread->t_dtrace_ft = 0;
85
86 /*
87 * If we weren't expecting a quick return to the kernel, just kill
88 * the process as though it had just executed an unassigned
89 * trap instruction.
90 */
91 if (step == 0) {
92 /*
0a7de745 93 * APPLE NOTE: We're returning KERN_FAILURE, which causes
5ba3f43e
A
94 * the generic signal handling code to take over, which will effectively
95 * deliver a EXC_BAD_INSTRUCTION to the user process.
96 */
97 return KERN_FAILURE;
0a7de745 98 }
5ba3f43e
A
99
100 /*
101 * If we hit this trap unrelated to a return probe, we're
102 * here to either:
103 *
104 * 1. Reset the AST flag, since we deferred a signal
105 * until after we logically single-stepped the instruction we
106 * copied out.
107 *
108 * 2. Just return to normal execution (required for U64).
109 */
110 if (ret == 0) {
111 set_saved_state_pc(regs, npc);
112 return KERN_SUCCESS;
113 }
114
115 /*
116 * We need to wait until after we've called the
117 * dtrace_return_probe_ptr function pointer to step the pc.
118 */
119 rwp = &CPU->cpu_ft_lock;
120 lck_rw_lock_shared(rwp);
121
0a7de745 122 if (dtrace_return_probe_ptr != NULL) {
5ba3f43e 123 (void) (*dtrace_return_probe_ptr)(regs);
0a7de745 124 }
5ba3f43e
A
125 lck_rw_unlock_shared(rwp);
126
127 set_saved_state_pc(regs, npc);
128
129 return KERN_SUCCESS;
130 } else {
131 rwp = &CPU->cpu_ft_lock;
132
133 /*
134 * The DTrace fasttrap provider uses a trap,
135 * FASTTRAP_{ARM,THUMB}_INSTR. We let
136 * DTrace take the first crack at handling
137 * this trap; if it's not a probe that DTrace knows about,
138 * we call into the trap() routine to handle it like a
139 * breakpoint placed by a conventional debugger.
140 */
141
142 /*
143 * APPLE NOTE: I believe the purpose of the reader/writers lock
144 * is thus: There are times which dtrace needs to prevent calling
145 * dtrace_pid_probe_ptr(). Sun's original impl grabbed a plain
146 * mutex here. However, that serialized all probe calls, and
147 * destroyed MP behavior. So now they use a RW lock, with probes
148 * as readers, and the top level synchronization as a writer.
149 */
150 lck_rw_lock_shared(rwp);
151 if (dtrace_pid_probe_ptr != NULL &&
152 (*dtrace_pid_probe_ptr)(regs) == 0) {
153 lck_rw_unlock_shared(rwp);
154 return KERN_SUCCESS;
155 }
156 lck_rw_unlock_shared(rwp);
157
158 /*
159 * If the instruction that caused the breakpoint trap doesn't
160 * look like our trap anymore, it may be that this tracepoint
161 * was removed just after the user thread executed it. In
162 * that case, return to user land to retry the instuction.
163 *
164 * Note that the PC points to the instruction that caused the fault.
165 */
cb323159
A
166 uint32_t instr;
167 if (fuword32(saved_state64(regs)->pc, &instr) == 0 && instr != FASTTRAP_ARM64_INSTR) {
168 return KERN_SUCCESS;
5ba3f43e
A
169 }
170 }
171
172 return KERN_FAILURE;
173}