Debian Codegraph @

runtime/linux/regs.c

raw
1 /* -*- linux-c -*-
2 * Functions to access the members of pt_regs struct
3 * Copyright (C) 2005, 2007 Red Hat Inc.
4 * Copyright (C) 2005 Intel Corporation.
5 * Copyright (C) 2007 Quentin Barnes.
6 *
7 * This file is part of systemtap, and is free software. You can
8 * redistribute it and/or modify it under the terms of the GNU General
9 * Public License (GPL); either version 2, or (at your option) any
10 * later version.
11 */
12
13 #ifndef _LINUX_REGS_C_
14 #define _LINUX_REGS_C_
15
16 #include "compatdefs.h"
17
18 /** Get the current return address for a return probe.
19 * Call from kprobe return probe.
20 * @param ri Pointer to the struct kretprobe_instance.
21 * @return The return address
22 */
23 // PR29028, ret_addr field may be located in a another struct
24 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
25 #define _stp_ret_addr_r(ri) ((ri)->node.ret_addr)
26 #else
27 #define _stp_ret_addr_r(ri) ((ri)->ret_addr)
28 #endif
29
30 /** Get the probe address for a kprobe.
31 * Call from a kprobe. This will return the
32 * address of the function that is being probed.
33 * @param kp Pointer to the struct kprobe.
34 * @return The function's address
35 */
36 #define _stp_probe_addr(kp) (kp->addr)
37
38 /** Get the probe address for a return probe.
39 * Call from kprobe return probe. This will return the
40 * address of the function that is being probed.
41 * @param ri Pointer to the struct kretprobe_instance.
42 * @return The function's address
43 */
44 #define _stp_probe_addr_r(ri) (get_kretprobe(ri)->kp.addr)
45
46 #if defined (__x86_64__)
47
48 #ifdef STAPCONF_X86_UNIREGS
49 #define EREG(nm, regs) ((regs)->nm)
50 #define RREG(nm, regs) ((regs)->nm)
51 #else
52 #define EREG(nm, regs) ((regs)->e##nm)
53 #define RREG(nm, regs) ((regs)->r##nm)
54 #endif
55
56 static void _stp_print_regs(struct pt_regs * regs)
57 {
58 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
59 unsigned int fsindex, gsindex;
60 unsigned int ds, cs, es;
61
62 _stp_printf("RIP: %016lx\nRSP: %016lx EFLAGS: %08lx\n",
63 RREG(ip, regs), RREG(sp, regs), EREG(flags, regs));
64 _stp_printf("RAX: %016lx RBX: %016lx RCX: %016lx\n",
65 RREG(ax, regs), RREG(bx, regs), RREG(cx, regs));
66 _stp_printf("RDX: %016lx RSI: %016lx RDI: %016lx\n",
67 RREG(dx, regs), RREG(si, regs), RREG(di, regs));
68 _stp_printf("RBP: %016lx R08: %016lx R09: %016lx\n",
69 RREG(bp, regs), regs->r8, regs->r9);
70 _stp_printf("R10: %016lx R11: %016lx R12: %016lx\n",
71 regs->r10, regs->r11, regs->r12);
72 _stp_printf("R13: %016lx R14: %016lx R15: %016lx\n",
73 regs->r13, regs->r14, regs->r15);
74
75 asm("movl %%ds,%0" : "=r" (ds));
76 asm("movl %%cs,%0" : "=r" (cs));
77 asm("movl %%es,%0" : "=r" (es));
78 asm("movl %%fs,%0" : "=r" (fsindex));
79 asm("movl %%gs,%0" : "=r" (gsindex));
80
81 rdmsrl(MSR_FS_BASE, fs);
82 rdmsrl(MSR_GS_BASE, gs);
83 rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
84
85 asm("movq %%cr0, %0": "=r" (cr0));
86 asm("movq %%cr2, %0": "=r" (cr2));
87 asm("movq %%cr3, %0": "=r" (cr3));
88 asm("movq %%cr4, %0": "=r" (cr4));
89
90 _stp_printf("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
91 fs, fsindex, gs, gsindex, shadowgs);
92 _stp_printf("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0);
93 _stp_printf("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4);
94 }
95
96 #elif defined (__i386__)
97
98 #ifdef STAPCONF_X86_UNIREGS
99 #define EREG(nm, regs) ((regs)->nm)
100 #define XREG(nm, regs) ((regs)->nm)
101 #else
102 #define EREG(nm, regs) ((regs)->e##nm)
103 #define XREG(nm, regs) ((regs)->x##nm)
104 #endif
105
106 /** Write the registers to a string.
107 * @param regs The pt_regs saved by the kprobe.
108 * @note i386 and x86_64 only so far.
109 */
110 static void _stp_print_regs(struct pt_regs * regs)
111 {
112 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
113
114 _stp_printf ("EIP: %08lx\n", EREG(ip, regs));
115 _stp_printf ("ESP: %08lx\n", EREG(sp, regs));
116 _stp_printf ("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
117 EREG(ax, regs), EREG(bx, regs), EREG(cx, regs), EREG(dx, regs));
118 _stp_printf ("ESI: %08lx EDI: %08lx EBP: %08lx",
119 EREG(si, regs), EREG(di, regs), EREG(bp, regs));
120 _stp_printf (" DS: %04x ES: %04x\n",
121 0xffff & XREG(ds, regs), 0xffff & XREG(es, regs));
122
123 __asm__("movl %%cr0, %0": "=r" (cr0));
124 __asm__("movl %%cr2, %0": "=r" (cr2));
125 __asm__("movl %%cr3, %0": "=r" (cr3));
126 /* This could fault if %cr4 does not exist */
127 __asm__("1: movl %%cr4, %0 \n"
128 "2: \n"
129 ".section __ex_table,\"a\" \n"
130 ".long 1b,2b \n"
131 ".previous \n"
132 : "=r" (cr4): "0" (0));
133 _stp_printf ("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4);
134 }
135
136 #elif defined (__ia64__)
137 static void _stp_print_regs(struct pt_regs * regs)
138 {
139 unsigned long ip = regs->cr_iip + ia64_psr(regs)->ri;
140
141 _stp_printf("\nPid: %d, CPU %d, comm: %20s\n", current->pid,
142 smp_processor_id(), current->comm);
143 _stp_printf("psr : %016lx ifs : %016lx ip : [<%016lx>] \n",
144 regs->cr_ipsr, regs->cr_ifs, ip);
145 _stp_printf("unat: %016lx pfs : %016lx rsc : %016lx\n",
146 regs->ar_unat, regs->ar_pfs, regs->ar_rsc);
147 _stp_printf("rnat: %016lx bsps: %016lx pr : %016lx\n",
148 regs->ar_rnat, regs->ar_bspstore, regs->pr);
149 _stp_printf("ldrs: %016lx ccv : %016lx fpsr: %016lx\n",
150 regs->loadrs, regs->ar_ccv, regs->ar_fpsr);
151 _stp_printf("csd : %016lx ssd : %016lx\n",
152 regs->ar_csd, regs->ar_ssd);
153 _stp_printf("b0 : %016lx b6 : %016lx b7 : %016lx\n",
154 regs->b0, regs->b6, regs->b7);
155 _stp_printf("f6 : %05lx%016lx f7 : %05lx%016lx\n",
156 regs->f6.u.bits[1], regs->f6.u.bits[0],
157 regs->f7.u.bits[1], regs->f7.u.bits[0]);
158 _stp_printf("f8 : %05lx%016lx f9 : %05lx%016lx\n",
159 regs->f8.u.bits[1], regs->f8.u.bits[0],
160 regs->f9.u.bits[1], regs->f9.u.bits[0]);
161 _stp_printf("f10 : %05lx%016lx f11 : %05lx%016lx\n",
162 regs->f10.u.bits[1], regs->f10.u.bits[0],
163 regs->f11.u.bits[1], regs->f11.u.bits[0]);
164 }
165
166 #elif defined (__powerpc64__)
167
168 static void _stp_print_regs(struct pt_regs * regs)
169 {
170 int i;
171
172 _stp_printf("NIP: %016lX XER: %08X LR: %016lX CTR: %016lX\n",
173 regs->nip, (unsigned int)regs->xer, regs->link, regs->ctr);
174 _stp_printf("REGS: %016lx TRAP: %04lx\n", (long)regs, regs->trap);
175 _stp_printf("MSR: %016lx CR: %08X\n",
176 regs->msr, (unsigned int)regs->ccr);
177 _stp_printf("DAR: %016lx DSISR: %016lx\n",
178 regs->dar, regs->dsisr);
179
180 #ifdef CONFIG_SMP
181 _stp_printf(" CPU: %d", smp_processor_id());
182 #endif /* CONFIG_SMP */
183
184 for (i = 0; i < 32; i++) {
185 if ((i % 4) == 0) {
186 _stp_printf("\n GPR%02d: ", i);
187 }
188
189 _stp_printf("%016lX ", regs->gpr[i]);
190 /* since kernel commit 8dc7f0229 */
191 #ifndef FULL_REGS
192 #define FULL_REGS(r) true
193 #endif
194 if (i == 13 && !FULL_REGS(regs))
195 break;
196 }
197 _stp_printf("\nNIP [%016lx] ", regs->nip);
198 _stp_printf("LR [%016lx]\n", regs->link);
199 }
200
201 #elif defined (__aarch64__)
202 static void _stp_print_regs(struct pt_regs * regs)
203 {
204 int i, top_reg;
205 u64 lr, sp;
206
207 if (compat_user_mode(regs)) {
208 lr = regs->compat_lr;
209 sp = regs->compat_sp;
210 top_reg = 12;
211 } else {
212 lr = regs->regs[30];
213 sp = regs->sp;
214 top_reg = 29;
215 }
216
217 _stp_printf("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
218 regs->pc, lr, regs->pstate);
219 _stp_printf("sp : %016llx\n", sp);
220 for (i = top_reg; i >= 0; i--) {
221 _stp_printf("x%-2d: %016llx ", i, regs->regs[i]);
222 if (i % 2 == 0)
223 _stp_printf("\n");
224 }
225 _stp_printf("\n");
226 }
227
228 #elif defined (__arm__)
229
230 static const char *processor_modes[]=
231 { "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
232 "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
233 "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" ,
234 "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
235 };
236
237
238 #ifdef STAPCONF_SET_FS
239 /* get_ds() removed in 736706bee3298208343a76096370e4f6a5c55915 */
240 #ifndef get_ds
241 #define get_ds() (KERNEL_DS)
242 #endif
243 #endif
244
245 static void _stp_print_regs(struct pt_regs * regs)
246 {
247 unsigned long flags = regs->ARM_cpsr;
248
249 #ifdef CONFIG_SMP
250 _stp_printf(" CPU: %d", smp_processor_id());
251 #endif /* CONFIG_SMP */
252
253 _stp_printf("pc : [<%08lx>] lr : [<%08lx>]\n"
254 "sp : %08lx ip : %08lx fp : %08lx\n",
255 instruction_pointer(regs),
256 regs->ARM_lr, regs->ARM_sp,
257 regs->ARM_ip, regs->ARM_fp);
258 _stp_printf("r10: %08lx r9 : %08lx r8 : %08lx\n",
259 regs->ARM_r10, regs->ARM_r9,
260 regs->ARM_r8);
261 _stp_printf("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
262 regs->ARM_r7, regs->ARM_r6,
263 regs->ARM_r5, regs->ARM_r4);
264 _stp_printf("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
265 regs->ARM_r3, regs->ARM_r2,
266 regs->ARM_r1, regs->ARM_r0);
267 _stp_printf("Flags: %c%c%c%c",
268 flags & PSR_N_BIT ? 'N' : 'n',
269 flags & PSR_Z_BIT ? 'Z' : 'z',
270 flags & PSR_C_BIT ? 'C' : 'c',
271 flags & PSR_V_BIT ? 'V' : 'v');
272 #ifdef STAPCONF_SET_FS
273 _stp_printf(" IRQs o%s FIQs o%s Mode %s%s Segment %s\n",
274 #else
275 _stp_printf(" IRQs o%s FIQs o%s Mode %s%s\n",
276 #endif
277 interrupts_enabled(regs) ? "n" : "ff",
278 fast_interrupts_enabled(regs) ? "n" : "ff",
279 processor_modes[processor_mode(regs)],
280 #ifdef STAPCONF_SET_FS
281 thumb_mode(regs) ? " (T)" : "",
282 get_fs() == get_ds() ? "kernel" : "user");
283 #else
284 thumb_mode(regs) ? " (T)" : "");
285 #endif
286 #ifdef CONFIG_CPU_CP15
287 {
288 unsigned int ctrl;
289 __asm__ (
290 " mrc p15, 0, %0, c1, c0\n"
291 : "=r" (ctrl));
292 _stp_printf("Control: %04X\n", ctrl);
293 }
294 #ifdef CONFIG_CPU_CP15_MMU
295 {
296 unsigned int transbase, dac;
297 __asm__ (
298 " mrc p15, 0, %0, c2, c0\n"
299 " mrc p15, 0, %1, c3, c0\n"
300 : "=r" (transbase), "=r" (dac));
301 _stp_printf("Table: %08X DAC: %08X\n",
302 transbase, dac);
303 }
304 #endif
305 #endif
306 }
307
308 #elif defined (__s390x__) || defined (__s390__)
309
310 #ifdef __s390x__
311 #define GPRSIZE "%016lX "
312 #else /* s390 */
313 #define GPRSIZE "%08lX "
314 #endif
315
316 static void _stp_print_regs(struct pt_regs * regs)
317 {
318 char *mode;
319 int i;
320
321 mode = (regs->psw.mask & PSW_MASK_PSTATE) ? "User" : "Krnl";
322 _stp_printf("%s PSW : ["GPRSIZE"] ["GPRSIZE"]",
323 mode, (void *) regs->psw.mask,
324 (void *) regs->psw.addr);
325
326 #ifdef CONFIG_SMP
327 _stp_printf(" CPU: %d", smp_processor_id());
328 #endif /* CONFIG_SMP */
329
330 for (i = 0; i < 16; i++) {
331 if ((i % 4) == 0) {
332 _stp_printf("\n GPRS%02d: ", i);
333 }
334 _stp_printf(GPRSIZE, regs->gprs[i]);
335 }
336 _stp_printf("\n");
337 }
338
339 #elif defined (__mips__)
340 /*
341 * MIPS_CPU_ISA_I was removed from the kernel (3.11) by
342 * Commit: 1990e5429c21 ("MIPS: Get rid of MIPS I flag and test macros.")
343 */
344 #ifndef MIPS_CPU_ISA_I
345 #define IS_CPU_ISA_MIPS_I(x) (!((x) & (MIPS_CPU_ISA_32BIT | MIPS_CPU_ISA_64BIT)))
346 #else
347 #define IS_CPU_ISA_MIPS_I(x) ((x) == MIPS_CPU_ISA_I)
348 #endif
349 /*
350 * Only o32 application has 32bit registers. N32 and N64 both
351 * should use 64bit registers regardless of pointer size
352 */
353 static int _stp_probing_app_with_32bit_regs(struct pt_regs *regs)
354 {
355 if (!regs)
356 return 0;
357 return (user_mode(regs) &&
358 _stp_is_compat_task());
359 }
360
361 void _stp_print_regs(struct pt_regs * regs)
362 {
363 #ifdef MIPS_PRINT_REGS_SHOW_SYMBOLS
364 /* it is too much stack to do symbol translation by default */
365 char symbol_name[KSYM_SYMBOL_LEN];
366 #endif /* MIPS_PRINT_REGS_SHOW_SYMBOLS */
367 const int field = 2 * sizeof(unsigned long);
368 unsigned int cause = regs->cp0_cause;
369 int i;
370
371 #ifdef CONFIG_SMP
372 _stp_printf("Cpu %d\n", smp_processor_id());
373 #endif
374
375 /*
376 * Saved main processor registers
377 */
378 for (i = 0; i < 32;) {
379 if ((i % 4) == 0)
380 _stp_printf("$%2d :", i);
381 if (i == 0)
382 _stp_printf(" %0*lx", field, 0UL);
383 else if (i == 26 || i == 27)
384 _stp_printf(" %*s", field, "");
385 else
386 _stp_printf(" %0*lx", field, regs->regs[i]);
387
388 i++;
389 if ((i % 4) == 0)
390 _stp_printf("\n");
391 }
392
393 /* Hi and Lo don't exist for R6 */
394 if (current_cpu_data.isa_level != MIPS_CPU_ISA_M32R6
395 && current_cpu_data.isa_level != MIPS_CPU_ISA_M64R6) {
396 _stp_printf("Hi : %0*lx\n", field, regs->hi);
397 _stp_printf("Lo : %0*lx\n", field, regs->lo);
398 }
399
400 /*
401 * Saved cp0 registers
402 */
403 _stp_printf("epc : %0*lx ", field, regs->cp0_epc);
404 #ifdef MIPS_PRINT_REGS_SHOW_SYMBOLSx
405 sprint_symbol(symbol_name, regs->cp0_epc);
406 _stp_printf("%s ", symbol_name);
407 #endif /* MIPS_PRINT_REGS_SHOW_SYMBOLS */
408
409 _stp_printf("ra : %0*lx ", field, regs->regs[31]);
410 #ifdef MIPS_PRINT_REGS_SHOW_SYMBOLS
411 sprint_symbol(symbol_name, regs->regs[31]);
412 _stp_printf("%s", symbol_name);
413 #endif /* MIPS_PRINT_REGS_SHOW_SYMBOLS */
414 _stp_printf("\n");
415
416 _stp_printf("Status: %08x ", (uint32_t) regs->cp0_status);
417
418 if (IS_CPU_ISA_MIPS_I(current_cpu_data.isa_level)) {
419 if (regs->cp0_status & ST0_KUO)
420 _stp_printf("KUo ");
421 if (regs->cp0_status & ST0_IEO)
422 _stp_printf("IEo ");
423 if (regs->cp0_status & ST0_KUP)
424 _stp_printf("KUp ");
425 if (regs->cp0_status & ST0_IEP)
426 _stp_printf("IEp ");
427 if (regs->cp0_status & ST0_KUC)
428 _stp_printf("KUc ");
429 if (regs->cp0_status & ST0_IEC)
430 _stp_printf("IEc ");
431 } else {
432 if (regs->cp0_status & ST0_KX)
433 _stp_printf("KX ");
434 if (regs->cp0_status & ST0_SX)
435 _stp_printf("SX ");
436 if (regs->cp0_status & ST0_UX)
437 _stp_printf("UX ");
438 switch (regs->cp0_status & ST0_KSU) {
439 case KSU_USER:
440 _stp_printf("USER ");
441 break;
442 case KSU_SUPERVISOR:
443 _stp_printf("SUPERVISOR ");
444 break;
445 case KSU_KERNEL:
446 _stp_printf("KERNEL ");
447 break;
448 default:
449 _stp_printf("BAD_MODE ");
450 break;
451 }
452 if (regs->cp0_status & ST0_ERL)
453 _stp_printf("ERL ");
454 if (regs->cp0_status & ST0_EXL)
455 _stp_printf("EXL ");
456 if (regs->cp0_status & ST0_IE)
457 _stp_printf("IE ");
458 }
459
460 _stp_printf("\n");
461
462 _stp_printf("Cause : %08x\n", cause);
463
464 cause = (((cause) & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE);
465 if (1 <= cause && cause <= 5)
466 _stp_printf("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
467
468 _stp_printf("PrId : %08x\n", read_c0_prid());
469 }
470 #endif
471
472
473 #endif /* _LINUX_REGS_C_ */