Debian Codegraph @

tapset/riscv/registers.stp

raw
1 /* Dwarfless register access for RISC-V */
2
3 global _reg_offsets, _stp_regs_registered
4
5 function _stp_register_regs() {
6 offset_acx = 0
7 /* Same order as struct pt_regs */
8 _reg_offsets["epc"] = 0
9 _reg_offsets["ra"] = 8
10 _reg_offsets["sp"] = 16
11 _reg_offsets["gp"] = 24
12 _reg_offsets["tp"] = 32
13 _reg_offsets["t0"] = 40
14 _reg_offsets["t1"] = 48
15 _reg_offsets["t2"] = 56
16 _reg_offsets["s0"] = 64
17 _reg_offsets["s1"] = 72
18 _reg_offsets["a0"] = 80
19 _reg_offsets["a1"] = 88
20 _reg_offsets["a2"] = 96
21 _reg_offsets["a3"] = 104
22 _reg_offsets["a4"] = 112
23 _reg_offsets["a5"] = 120
24 _reg_offsets["a6"] = 128
25 _reg_offsets["a7"] = 136
26 _reg_offsets["s2"] = 144
27 _reg_offsets["s3"] = 152
28 _reg_offsets["s4"] = 160
29 _reg_offsets["s5"] = 168
30 _reg_offsets["s6"] = 176
31 _reg_offsets["s7"] = 184
32 _reg_offsets["s8"] = 192
33 _reg_offsets["s9"] = 200
34 _reg_offsets["s10"] = 208
35 _reg_offsets["s11"] = 216
36 _reg_offsets["t3"] = 224
37 _reg_offsets["t4"] = 232
38 _reg_offsets["t5"] = 240
39 _reg_offsets["t6"] = 248
40
41 _reg_offsets["status"] = 256
42 _reg_offsets["badaddr"] = 264
43 _reg_offsets["cause"] = 272
44 _reg_offsets["orig_a0"] = 280
45
46 _stp_regs_registered = 1
47 }
48
49 function _stp_get_register_by_offset:long (offset:long) %{ /* pure */
50 long value;
51 struct pt_regs *regs;
52 regs = (CONTEXT->user_mode_p ? CONTEXT->uregs : CONTEXT->kregs);
53
54 if (!regs) {
55 CONTEXT->last_error = "No registers available in this context";
56 return;
57 }
58 if (STAP_ARG_offset < 0 || STAP_ARG_offset > sizeof(struct pt_regs) - sizeof(long)) {
59 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
60 "Bad register offset: %lld", STAP_ARG_offset);
61 CONTEXT->last_error = CONTEXT->error_buffer;
62 return;
63 }
64 memcpy(&value, ((char *)regs) + STAP_ARG_offset, sizeof(value));
65 STAP_RETVALUE = value;
66 %}
67
68 function _stp_sign_extend32:long (value:long) {
69 if (value & 0x80000000)
70 value |= (0xffffffff << 32)
71 return value
72 }
73
74 function _stp_register:long (name:string, sign_extend:long) {
75 if (!registers_valid()) {
76 error("cannot access CPU registers in this context")
77 return 0
78 }
79 if (!_stp_regs_registered)
80 _stp_register_regs()
81 offset = _reg_offsets[name]
82 if (offset == 0 && !(name in _reg_offsets)) {
83 error("Unknown register: " . name)
84 return 0
85 }
86 value = _stp_get_register_by_offset(offset)
87 return value
88 }
89
90 /* Return the named register value as a signed value. */
91 function register:long (name:string) {
92 return _stp_register(name, 1)
93 }
94
95 /*
96 * Return the named register value as an unsigned value. Specifically,
97 * don't sign-extend the register value when promoting it to 64 bits.
98 */
99 function u_register:long (name:string) {
100 return _stp_register(name, 0)
101 }
102
103
104 /* Dwarfless register access for mips */
105
106 %{
107 // These functions are largely lifted from arch/riscv/include/asm/ptrace.h.
108
109 static inline unsigned long _stp_kernel_stack_pointer(struct pt_regs *regs)
110 {
111 return regs->sp;
112 }
113
114 /**
115 * _stp_regs_within_kernel_stack() - check the address in the stack
116 * @regs: pt_regs which contains kernel stack pointer.
117 * @addr: address which is checked.
118 *
119 * _stp_regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
120 * If @addr is within the kernel stack, it returns true. If not, returns false.
121 */
122 static inline int _stp_regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
123 {
124 return ((addr & ~(THREAD_SIZE - 1)) ==
125 (_stp_kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
126 }
127
128 /*
129 * _stp_regs_get_kernel_stack_nth_addr() - get address of the Nth entry of the stack
130 * @regs: pt_regs which contains kernel stack pointer.
131 * @n: stack entry number.
132 *
133 * _stp_regs_get_kernel_stack_nth_addr() returns the address of the @n
134 * th entry of the kernel stack which is specified by @regs. If the @n
135 * th entry is NOT in the kernel stack, this returns 0.
136 */
137 static long *
138 _stp_regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n)
139 {
140 long *addr = (unsigned long *)_stp_kernel_stack_pointer(regs);
141 addr += n;
142 if (_stp_regs_within_kernel_stack(regs, (unsigned long)addr))
143 return addr;
144 else
145 return 0;
146 }
147
148 %}
149
150 function _stp_get_stack_nth:long (n:long)
151 %{ /* pure */
152 __label__ deref_fault;
153 unsigned int n = (unsigned int)STAP_ARG_n;
154 struct pt_regs *regs;
155 long *addr;
156
157 STAP_RETVALUE = 0;
158 if (CONTEXT->user_mode_p) {
159 // This function only handles kernel arguments off the stack.
160 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
161 "cannot access function args in this context");
162 CONTEXT->last_error = CONTEXT->error_buffer;
163 return;
164 }
165 regs = CONTEXT->kregs;
166 if (!regs) {
167 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
168 "cannot access function args in this context");
169 CONTEXT->last_error = CONTEXT->error_buffer;
170 return;
171 }
172
173 /* Get the address of the nth item on the stack. */
174 addr = _stp_regs_get_kernel_stack_nth_addr(regs, n);
175 if (addr == NULL) {
176 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
177 "cannot access stack arg(%d)", n);
178 CONTEXT->last_error = CONTEXT->error_buffer;
179 return;
180 }
181 STAP_RETVALUE = kread(addr);
182 return;
183
184 deref_fault: /* branched to from kread() */
185 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
186 "kernel fault at %#lx accessing stack arg(%d)",
187 (unsigned long)addr, n);
188 CONTEXT->last_error = CONTEXT->error_buffer;
189 %}
190
191 /*
192 * Return the value of function arg #argnum (1=first arg).
193 * If truncate=1, mask off the top 32 bits.
194 * If sign_extend=1 and (truncate=1 or the probepoint we've hit is in a
195 * 32-bit app), sign-extend the 32-bit value.
196 */
197 function _stp_arg:long (argnum:long, sign_extend:long, truncate:long) {
198 val = 0
199 if (argnum < 1 || argnum > 8) {
200 error(sprintf("Cannot access arg(%d)", argnum))
201 return 0
202 }
203
204 if (argnum == 1)
205 val = u_register("a0")
206 else if (argnum == 2)
207 val = u_register("a1")
208 else if (argnum == 3)
209 val = u_register("a2")
210 else if (argnum == 4)
211 val = u_register("a3")
212 else if (argnum == 5)
213 val = u_register("a4")
214 else if (argnum == 6)
215 val = u_register("a5")
216 else if (argnum == 7)
217 val = u_register("a6")
218 else if (argnum == 8)
219 val = u_register("a7")
220 else
221 val = _stp_get_stack_nth(argnum - 5)
222
223 if (truncate) {
224 if (sign_extend)
225 val = _stp_sign_extend32(val)
226 else
227 /* High bits may be garbage. */
228 val = (val & 0xffffffff);
229 }
230 return val;
231 }
232
233 function arch_bytes:long() %{ /* pure */
234 STAP_RETVALUE = sizeof(long);
235 %}
236
237 function uarch_bytes:long() {
238 assert(user_mode(), "requires user mode")
239 return probing_32bit_app() ? 4 : 8
240 }
241
242 /* Return the value of function arg #argnum (1=first arg) as a signed int. */
243 function int_arg:long (argnum:long) {
244 return _stp_arg(argnum, 1, 1)
245 }
246
247 /* Return the value of function arg #argnum (1=first arg) as an unsigned int. */
248 function uint_arg:long (argnum:long) {
249 return _stp_arg(argnum, 0, 1)
250 }
251
252 function long_arg:long (argnum:long) {
253 return _stp_arg(argnum, 1, 0)
254 }
255
256 function ulong_arg:long (argnum:long) {
257 return _stp_arg(argnum, 0, 0)
258 }
259
260 function longlong_arg:long (argnum:long) {
261 return _stp_arg(argnum, 0, 0)
262 }
263
264 function ulonglong_arg:long (argnum:long) {
265 return longlong_arg(argnum)
266 }
267
268 function pointer_arg:long (argnum:long) {
269 return _stp_arg(argnum, 0, 0)
270 }
271
272 function s32_arg:long (argnum:long) {
273 return int_arg(argnum)
274 }
275
276 function u32_arg:long (argnum:long) {
277 return uint_arg(argnum)
278 }
279
280 function s64_arg:long (argnum:long) {
281 return longlong_arg(argnum)
282 }
283
284 function u64_arg:long (argnum:long) {
285 return ulonglong_arg(argnum)
286 }
287
288 function asmlinkage() %{ /* pure */ %}
289
290 function fastcall() %{ /* pure */ %}
291
292 function regparm() %{
293 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
294 "regparm is invalid on mips.");
295 CONTEXT->last_error = CONTEXT->error_buffer;
296 %}