Debian Codegraph @

runtime/linux/itrace.c

raw
1 /*
2 * user space instruction tracing
3 * Copyright (C) 2005, 2006, 2007, 2008, 2009 IBM Corp.
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * This file is part of systemtap, and is free software. You can
7 * redistribute it and/or modify it under the terms of the GNU General
8 * Public License (GPL); either version 2, or (at your option) any
9 * later version.
10 */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/spinlock.h>
17 #include <linux/err.h>
18 #include <linux/sched.h>
19 #include <linux/rcupdate.h>
20 #include <linux/utrace.h>
21 #include "ptrace_compatibility.h"
22
23 /* PR10171: To avoid ia64 lockups, disable itrace on ia64. */
24 #if defined(__ia64__)
25 #error "Unsupported itrace architecture"
26 #endif
27
28 /* PR9974: Adapt to struct renaming. */
29 #ifdef UTRACE_API_VERSION
30 #define utrace_attached_engine utrace_engine
31 #endif
32
33 #include <asm/string.h>
34 #include "uprobes/uprobes.h"
35 #include "utrace_compatibility.h"
36
37 #ifndef put_task_struct
38 #define put_task_struct(t) \
39 BUG_ON(atomic_dec_and_test(&tsk->usage))
40 #endif
41
42 #ifdef CONFIG_PPC
43 struct bpt_info {
44 unsigned long addr;
45 unsigned int instr;
46 };
47
48 struct atomic_ss_info {
49 int step_over_atomic;
50 struct bpt_info end_bpt;
51 struct bpt_info br_bpt;
52 };
53
54 static int handle_ppc_atomic_seq(struct task_struct *tsk, struct pt_regs *regs,
55 struct atomic_ss_info *ss_info);
56 static void remove_atomic_ss_breakpoint (struct task_struct *tsk,
57 struct bpt_info *bpt);
58 #endif
59
60 struct itrace_info {
61 u32 step_flag;
62 struct stap_itrace_probe *itrace_probe;
63 #ifdef CONFIG_PPC
64 struct atomic_ss_info ppc_atomic_ss;
65 #endif
66 struct task_struct *tsk;
67 struct utrace_attached_engine *engine;
68 struct list_head link;
69 };
70
71 static u32 debug = 0 /* 1 */;
72
73 static LIST_HEAD(usr_itrace_info);
74 static spinlock_t itrace_lock;
75 static struct itrace_info *create_itrace_info(
76 struct task_struct *tsk, u32 step_flag,
77 struct stap_itrace_probe *itrace_probe);
78
79
80 /* Note: __access_process_vm moved to access_process_vm.h */
81
82 #ifdef UTRACE_ORIG_VERSION
83 static u32 usr_itrace_report_quiesce(struct utrace_attached_engine *engine,
84 struct task_struct *tsk)
85 #else
86 #if defined(UTRACE_API_VERSION) && (UTRACE_API_VERSION >= 20091216)
87 static u32 usr_itrace_report_quiesce(u32 action,
88 struct utrace_attached_engine *engine,
89 unsigned long event)
90 #else
91 static u32 usr_itrace_report_quiesce(enum utrace_resume_action action,
92 struct utrace_attached_engine *engine,
93 struct task_struct *tsk,
94 unsigned long event)
95 #endif
96 #endif
97 {
98 int status;
99 struct itrace_info *ui;
100
101 rcu_read_lock();
102 ui = rcu_dereference(engine->data);
103 rcu_read_unlock();
104 WARN_ON(!ui);
105
106 #ifdef UTRACE_ORIG_VERSION
107 return (ui->step_flag | UTRACE_ACTION_NEWSTATE);
108 #else
109 return (event == 0 ? ui->step_flag : UTRACE_RESUME);
110 #endif
111 }
112
113
114 #ifdef UTRACE_ORIG_VERSION
115 static u32 usr_itrace_report_signal(
116 struct utrace_attached_engine *engine,
117 struct task_struct *tsk,
118 struct pt_regs *regs,
119 u32 action, siginfo_t *info,
120 const struct k_sigaction *orig_ka,
121 struct k_sigaction *return_ka)
122 #else
123 #if defined(UTRACE_API_VERSION) && (UTRACE_API_VERSION >= 20091216)
124 static u32 usr_itrace_report_signal(u32 action,
125 struct utrace_attached_engine *engine,
126 struct pt_regs *regs,
127 siginfo_t *info,
128 const struct k_sigaction *orig_ka,
129 struct k_sigaction *return_ka)
130 #else
131 static u32 usr_itrace_report_signal(u32 action,
132 struct utrace_attached_engine *engine,
133 struct task_struct *tsk,
134 struct pt_regs *regs,
135 siginfo_t *info,
136 const struct k_sigaction *orig_ka,
137 struct k_sigaction *return_ka)
138 #endif
139 #endif
140 {
141 #if defined(UTRACE_API_VERSION) && (UTRACE_API_VERSION >= 20091216)
142 struct task_struct *tsk = current;
143 #endif
144 struct itrace_info *ui;
145 u32 return_flags;
146 unsigned long data = 0;
147 #ifdef CONFIG_PPC
148 data = mfspr(SPRN_SDAR);
149 #endif
150
151 rcu_read_lock();
152 ui = rcu_dereference(engine->data);
153 rcu_read_unlock();
154 WARN_ON(!ui);
155
156 #if defined(UTRACE_ORIG_VERSION)
157 if (info->si_signo != SIGTRAP || !ui)
158 return UTRACE_RESUME;
159 #else
160 if (utrace_signal_action(action) == UTRACE_SIGNAL_HANDLER ||
161 utrace_signal_action(action) == UTRACE_SIGNAL_REPORT ||
162 info->si_signo != SIGTRAP || !ui)
163 return UTRACE_RESUME | utrace_signal_action(action);
164 #endif
165
166 #if defined(UTRACE_ORIG_VERSION) && defined(CONFIG_PPC)
167 /* Because of a ppc utrace bug, we need to stop the task here.
168 usr_itrace_report_quiesce() will continue stepping the task. */
169 return_flags = UTRACE_SIGNAL_IGN | UTRACE_STOP | UTRACE_ACTION_NEWSTATE;
170 #else
171 /* normal case: continue stepping */
172 return_flags = ui->step_flag | UTRACE_SIGNAL_IGN;
173 #endif
174 #ifdef CONFIG_PPC
175 if (ui->ppc_atomic_ss.step_over_atomic) {
176 remove_atomic_ss_breakpoint(tsk, &ui->ppc_atomic_ss.end_bpt);
177 if (ui->ppc_atomic_ss.br_bpt.addr)
178 remove_atomic_ss_breakpoint(tsk,
179 &ui->ppc_atomic_ss.br_bpt);
180 ui->ppc_atomic_ss.step_over_atomic = 0;
181 }
182
183 if (handle_ppc_atomic_seq(tsk, regs, &ui->ppc_atomic_ss))
184 return_flags = UTRACE_RESUME | UTRACE_SIGNAL_IGN;
185 #endif
186
187 enter_itrace_probe(ui->itrace_probe, regs, (void *)&data);
188
189 return return_flags;
190 }
191
192
193
194 #ifdef UTRACE_ORIG_VERSION
195 static u32 usr_itrace_report_clone(
196 struct utrace_attached_engine *engine,
197 struct task_struct *parent,
198 unsigned long clone_flags,
199 struct task_struct *child)
200 #else
201 #if defined(UTRACE_API_VERSION) && (UTRACE_API_VERSION >= 20091216)
202 static u32 usr_itrace_report_clone(u32 action,
203 struct utrace_attached_engine *engine,
204 unsigned long clone_flags,
205 struct task_struct *child)
206 #else
207 static u32 usr_itrace_report_clone(enum utrace_resume_action action,
208 struct utrace_attached_engine *engine,
209 struct task_struct *parent, unsigned long clone_flags,
210 struct task_struct *child)
211 #endif
212 #endif
213 {
214 return UTRACE_RESUME;
215 }
216
217 #ifdef UTRACE_ORIG_VERSION
218 static u32 usr_itrace_report_death(struct utrace_attached_engine *e,
219 struct task_struct *tsk)
220 #else
221 #if defined(UTRACE_API_VERSION) && (UTRACE_API_VERSION >= 20091216)
222 static u32 usr_itrace_report_death(struct utrace_attached_engine *e,
223 bool group_dead, int signal)
224 #else
225 static u32 usr_itrace_report_death(struct utrace_attached_engine *e,
226 struct task_struct *tsk, bool group_dead, int signal)
227 #endif
228 #endif
229 {
230 struct itrace_info *ui;
231 rcu_read_lock();
232 ui = rcu_dereference(e->data);
233 rcu_read_unlock();
234 WARN_ON(!ui);
235
236 return (UTRACE_DETACH);
237 }
238
239 static const struct utrace_engine_ops utrace_ops =
240 {
241 .report_quiesce = usr_itrace_report_quiesce,
242 .report_signal = usr_itrace_report_signal,
243 .report_clone = usr_itrace_report_clone,
244 .report_death = usr_itrace_report_death
245 };
246
247
248 static struct itrace_info *create_itrace_info(
249 struct task_struct *tsk, u32 step_flag,
250 struct stap_itrace_probe *itrace_probe)
251 {
252 struct itrace_info *ui;
253 int status;
254
255 if (debug)
256 printk(KERN_INFO "create_itrace_info: tid=%d\n", tsk->pid);
257 /* initialize ui */
258 ui = _stp_kzalloc(sizeof(struct itrace_info));
259 if (ui == NULL) {
260 printk(KERN_ERR "%s:%d: Unable to allocate memory\n",
261 __FUNCTION__, __LINE__);
262 return NULL;
263 }
264 ui->tsk = tsk;
265 ui->step_flag = step_flag;
266 ui->itrace_probe = itrace_probe;
267 #ifdef CONFIG_PPC
268 ui->ppc_atomic_ss.step_over_atomic = 0;
269 #endif
270 INIT_LIST_HEAD(&ui->link);
271
272 /* push ui onto usr_itrace_info */
273 spin_lock(&itrace_lock);
274 list_add(&ui->link, &usr_itrace_info);
275 spin_unlock(&itrace_lock);
276
277 /* attach a single stepping engine. create_itrace_info is called
278 under rcu read lock, so needs atomic. */
279 ui->engine = utrace_attach_task(ui->tsk,
280 (UTRACE_ATTACH_CREATE
281 | UTRACE_ATTACH_ATOMIC),
282 &utrace_ops, ui);
283 if (IS_ERR(ui->engine)) {
284 printk(KERN_ERR "utrace_attach returns %ld\n",
285 PTR_ERR(ui->engine));
286 return NULL;
287 }
288 status = utrace_set_events(tsk, ui->engine, ui->engine->flags |
289 UTRACE_EVENT(QUIESCE) |
290 UTRACE_EVENT(CLONE) | UTRACE_EVENT_SIGNAL_ALL |
291 UTRACE_EVENT(DEATH));
292 if (status < 0) {
293 printk(KERN_ERR "utrace_attach returns %d\n", status);
294 utrace_engine_put(ui->engine);
295 return NULL;
296 }
297
298 status = utrace_control(tsk, ui->engine, UTRACE_STOP);
299 if (status == 0) {
300 status = utrace_control(tsk, ui->engine, step_flag);
301 if (status < 0) {
302 printk(KERN_ERR "utrace_control(%d) returns %d\n",
303 step_flag, status);
304 utrace_engine_put(ui->engine);
305 return NULL;
306 }
307 }
308
309 utrace_engine_put(ui->engine);
310 return ui;
311 }
312
313 static struct itrace_info *find_itrace_info(struct task_struct *tsk)
314 {
315 struct itrace_info *ui = NULL;
316
317 spin_lock(&itrace_lock);
318 list_for_each_entry(ui, &usr_itrace_info, link) {
319 if (ui->tsk == tsk)
320 goto done;
321 }
322 ui = NULL;
323 done:
324 spin_unlock(&itrace_lock);
325 return ui;
326 }
327
328
329 static int usr_itrace_init(int single_step, struct task_struct *tsk, struct stap_itrace_probe *p)
330 {
331 struct itrace_info *ui;
332
333 spin_lock_init(&itrace_lock);
334 rcu_read_lock();
335 if (tsk == NULL) {
336 printk(KERN_ERR "usr_itrace_init: Invalid task\n");
337 rcu_read_unlock();
338 return 1;
339 }
340
341 get_task_struct(tsk);
342 ui = create_itrace_info(tsk,
343 (single_step ?
344 UTRACE_SINGLESTEP : UTRACE_BLOCKSTEP), p);
345 if (!ui)
346 return 1;
347
348 put_task_struct(tsk);
349 rcu_read_unlock();
350
351 if (debug)
352 printk(KERN_INFO "usr_itrace_init: completed for tid = %d\n",
353 tsk->pid);
354
355 return 0;
356 }
357
358 void static remove_usr_itrace_info(struct itrace_info *ui)
359 {
360 struct itrace_info *tmp;
361 int status;
362
363 if (!ui)
364 return;
365
366 if (debug)
367 printk(KERN_INFO "remove_usr_itrace_info: tid=%d\n",
368 (ui->tsk ? ui->tsk->pid : -1));
369
370 if (ui->tsk && ui->engine) {
371 status = utrace_control(ui->tsk, ui->engine, UTRACE_DETACH);
372 if (status == -EINPROGRESS) {
373 do {
374 status = utrace_barrier(ui->tsk, ui->engine);
375 } while (status == -ERESTARTSYS);
376 }
377 if (status < 0 && status != -ESRCH && status != -EALREADY)
378 printk(KERN_ERR
379 "utrace_control(UTRACE_DETACH) returns %d\n",
380 status);
381 }
382 spin_lock(&itrace_lock);
383 list_del(&ui->link);
384 spin_unlock(&itrace_lock);
385 _stp_kfree(ui);
386 }
387
388 void static cleanup_usr_itrace(void)
389 {
390 struct itrace_info *tmp;
391 struct itrace_info *ui;
392
393 if (debug)
394 printk(KERN_INFO "cleanup_usr_itrace called\n");
395
396 list_for_each_entry_safe(ui, tmp, &usr_itrace_info, link) {
397 remove_usr_itrace_info(ui);
398 }
399 }
400
401
402 #ifdef CONFIG_PPC
403 #define PPC_INSTR_SIZE 4
404 #define TEXT_SEGMENT_BASE 1
405
406 /* Instruction masks used during single-stepping of atomic sequences. */
407 #define LWARX_MASK 0xfc0007fe
408 #define LWARX_INSTR 0x7c000028
409 #define LDARX_INSTR 0x7c0000A8
410 #define STWCX_MASK 0xfc0007ff
411 #define STWCX_INSTR 0x7c00012d
412 #define STDCX_INSTR 0x7c0001ad
413 #define BC_MASK 0xfc000000
414 #define BC_INSTR 0x40000000
415 #define ATOMIC_SEQ_LENGTH 16
416 #define BPT_TRAP 0x7fe00008
417 #define INSTR_SZ sizeof(int)
418
419 static int get_instr(unsigned long addr, char *msg)
420 {
421 unsigned int instr;
422
423 if (copy_from_user(&instr, (const void __user *) addr,
424 sizeof(instr))) {
425 printk(KERN_ERR "get_instr failed: %s\n", msg);
426 WARN_ON(1);
427 }
428 return instr;
429
430 }
431
432 static void insert_atomic_ss_breakpoint (struct task_struct *tsk,
433 struct bpt_info *bpt)
434 {
435 unsigned int bp_instr = BPT_TRAP;
436 unsigned int cur_instr;
437
438 cur_instr = get_instr(bpt->addr, "insert_atomic_ss_breakpoint");
439 if (cur_instr != BPT_TRAP) {
440 bpt->instr = cur_instr;
441 WARN_ON(__access_process_vm(tsk, bpt->addr, &bp_instr, INSTR_SZ, 1) !=
442 INSTR_SZ);
443 }
444 }
445
446 static void remove_atomic_ss_breakpoint (struct task_struct *tsk,
447 struct bpt_info *bpt)
448 {
449 WARN_ON(__access_process_vm(tsk, bpt->addr, &bpt->instr, INSTR_SZ, 1) !=
450 INSTR_SZ);
451 }
452
453 /* locate the branch destination. Return -1 if not a branch. */
454 static unsigned long
455 branch_dest (int opcode, int instr, struct pt_regs *regs, unsigned long pc)
456 {
457 unsigned long dest;
458 int immediate;
459 int absolute;
460 int ext_op;
461
462 absolute = (int) ((instr >> 1) & 1);
463
464 switch (opcode) {
465 case 18:
466 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
467 if (absolute)
468 dest = immediate;
469 else
470 dest = pc + immediate;
471 break;
472
473 case 16:
474 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
475 if (absolute)
476 dest = immediate;
477 else
478 dest = pc + immediate;
479 break;
480
481 case 19:
482 ext_op = (instr >> 1) & 0x3ff;
483
484 if (ext_op == 16) {
485 /* br conditional register */
486 dest = regs->link & ~3;
487 /* FIX: we might be in a signal handler */
488 WARN_ON(dest > 0);
489 } else if (ext_op == 528) {
490 /* br cond to ctr reg */
491 dest = regs->ctr & ~3;
492
493 /* for system call dest < TEXT_SEGMENT_BASE */
494 if (dest < TEXT_SEGMENT_BASE)
495 dest = regs->link & ~3;
496 } else
497 return -1;
498 break;
499
500 default:
501 return -1;
502 }
503 return dest;
504 }
505
506 /* Checks for an atomic sequence of instructions beginning with a LWARX/LDARX
507 instruction and ending with a STWCX/STDCX instruction. If such a sequence
508 is found, attempt to step through it. A breakpoint is placed at the end of
509 the sequence. */
510
511 static int handle_ppc_atomic_seq(struct task_struct *tsk, struct pt_regs *regs,
512 struct atomic_ss_info *ss_info)
513 {
514 unsigned long ip = regs->nip;
515 unsigned long start_addr;
516 unsigned int instr;
517 int got_stx = 0;
518 int i;
519 int ret;
520
521 unsigned long br_dest; /* bpt at branch instr's destination */
522 int bc_instr_count = 0; /* conditional branch instr count */
523
524 instr = get_instr(regs->nip, "handle_ppc_atomic_seq:1");
525 /* Beginning of atomic sequence starts with lwarx/ldarx instr */
526 if ((instr & LWARX_MASK) != LWARX_INSTR
527 && (instr & LWARX_MASK) != LDARX_INSTR)
528 return 0;
529
530 start_addr = regs->nip;
531 for (i = 0; i < ATOMIC_SEQ_LENGTH; ++i) {
532 ip += INSTR_SZ;
533 instr = get_instr(ip, "handle_ppc_atomic_seq:2");
534
535 /* look for at most one conditional branch in the sequence
536 * and put a bpt at it's destination address
537 */
538 if ((instr & BC_MASK) == BC_INSTR) {
539 if (bc_instr_count >= 1)
540 return 0; /* only handle a single branch */
541
542 br_dest = branch_dest (BC_INSTR >> 26, instr, regs, ip);
543
544 if (br_dest != -1 &&
545 br_dest >= TEXT_SEGMENT_BASE) {
546 ss_info->br_bpt.addr = br_dest;
547 bc_instr_count++;
548 }
549 }
550
551 if ((instr & STWCX_MASK) == STWCX_INSTR
552 || (instr & STWCX_MASK) == STDCX_INSTR) {
553 got_stx = 1;
554 break;
555 }
556 }
557
558 /* Atomic sequence ends with a stwcx/stdcx instr */
559 if (!got_stx)
560 return 0;
561
562 ip += INSTR_SZ;
563 instr = get_instr(ip, "handle_ppc_atomic_seq:3");
564 if ((instr & BC_MASK) == BC_INSTR) {
565 ip += INSTR_SZ;
566 instr = get_instr(ip, "handle_ppc_atomic_seq:4");
567 }
568
569 /* Insert a breakpoint right after the end of the atomic sequence. */
570 ss_info->end_bpt.addr = ip;
571
572 /* Check for duplicate bpts */
573 if (bc_instr_count && (ss_info->br_bpt.addr >= start_addr &&
574 ss_info->br_bpt.addr <= ss_info->end_bpt.addr))
575 ss_info->br_bpt.addr = 0;
576
577 insert_atomic_ss_breakpoint (tsk, &ss_info->end_bpt);
578 if (ss_info->br_bpt.addr)
579 insert_atomic_ss_breakpoint (tsk, &ss_info->br_bpt);
580
581 ss_info->step_over_atomic = 1;
582 return 1;
583 }
584 #endif