11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* ptrace.c */
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* By Ross Biro 1/23/92 */
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* edited by Linus Torvalds */
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* mangled further by Bob Manson (manson@santafe.edu) */
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* more mutilation by David Mosberger (davidm@azstarnet.com) */
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sched.h>
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/mm.h>
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/smp.h>
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h>
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/ptrace.h>
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/user.h>
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/security.h>
157ed20e1ad521b5f5df61bf6559ae60738e393741Jesper Juhl#include <linux/signal.h>
1612f79be93d94698778ff2b3f921073fc5f6780d6Al Viro#include <linux/tracehook.h>
17a9302e8439445710552886e7b623dbcfa943a1f2蔡正龙#include <linux/audit.h>
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/pgtable.h>
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/fpu.h>
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "proto.h"
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DEBUG	DBG_MEM
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef DEBUG
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef DEBUG
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsenum {
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DBG_MEM		= (1<<0),
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DBG_BPT		= (1<<1),
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DBG_MEM_ALL	= (1<<2)
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DBG(fac,args)	{if ((fac) & DEBUG) printk args;}
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#else
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DBG(fac,args)
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define BREAKINST	0x00000080	/* call_pal bpt */
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * does not yet catch signals sent when the child dies.
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in exit.c or in signal.c.
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Processes always block with the following stack-layout:
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  +================================+ <---- task + 2*PAGE_SIZE
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  | PALcode saved frame (ps, pc,   | ^
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  | gp, a0, a1, a2)		     | |
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  +================================+ | struct pt_regs
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  |	        		     | |
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  | frame generated by SAVE_ALL    | |
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  |	        		     | v
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  +================================+
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  |	        		     | ^
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  | frame saved by do_switch_stack | | struct switch_stack
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  |	        		     | v
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  +================================+
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The following table maps a register index into the stack offset at
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * which the register is saved.  Register indices are 0-31 for integer
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * regs, 32-63 for fp regs, and 64 for the pc.  Notice that sp and
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * zero have no stack-slot and need to be treated specially (see
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * get_reg/put_reg below).
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsenum {
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	REG_R0 = 0, REG_F0 = 32, REG_FPCR = 63, REG_PC = 64
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
74e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton#define PT_REG(reg) \
75e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton  (PAGE_SIZE*2 - sizeof(struct pt_regs) + offsetof(struct pt_regs, reg))
76e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton
77e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton#define SW_REG(reg) \
78e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton (PAGE_SIZE*2 - sizeof(struct pt_regs) - sizeof(struct switch_stack) \
79e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton  + offsetof(struct switch_stack, reg))
80e52f4ca2a7cfea3495cabae23d48f1538f09ccf2Andrew Morton
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int regoff[] = {
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	   r0), PT_REG(	   r1), PT_REG(	   r2), PT_REG(	  r3),
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	   r4), PT_REG(	   r5), PT_REG(	   r6), PT_REG(	  r7),
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	   r8), SW_REG(	   r9), SW_REG(	  r10), SW_REG(	 r11),
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(	  r12), SW_REG(	  r13), SW_REG(	  r14), SW_REG(	 r15),
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	  r16), PT_REG(	  r17), PT_REG(	  r18), PT_REG(	 r19),
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	  r20), PT_REG(	  r21), PT_REG(	  r22), PT_REG(	 r23),
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	  r24), PT_REG(	  r25), PT_REG(	  r26), PT_REG(	 r27),
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	  r28), PT_REG(	   gp),		   -1,		   -1,
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[ 0]), SW_REG(fp[ 1]), SW_REG(fp[ 2]), SW_REG(fp[ 3]),
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[ 4]), SW_REG(fp[ 5]), SW_REG(fp[ 6]), SW_REG(fp[ 7]),
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[ 8]), SW_REG(fp[ 9]), SW_REG(fp[10]), SW_REG(fp[11]),
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[12]), SW_REG(fp[13]), SW_REG(fp[14]), SW_REG(fp[15]),
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[16]), SW_REG(fp[17]), SW_REG(fp[18]), SW_REG(fp[19]),
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[20]), SW_REG(fp[21]), SW_REG(fp[22]), SW_REG(fp[23]),
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[24]), SW_REG(fp[25]), SW_REG(fp[26]), SW_REG(fp[27]),
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	SW_REG(fp[28]), SW_REG(fp[29]), SW_REG(fp[30]), SW_REG(fp[31]),
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	PT_REG(	   pc)
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned long zero;
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Get address of register REGNO in task TASK.
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned long *
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_reg_addr(struct task_struct * task, unsigned long regno)
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long *addr;
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (regno == 30) {
11237bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		addr = &task_thread_info(task)->pcb.usp;
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (regno == 65) {
11437bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		addr = &task_thread_info(task)->pcb.unique;
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (regno == 31 || regno > 65) {
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		zero = 0;
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr = &zero;
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
11927f451304aa6f2bccf34ef3c2b049c01d05d6fffAl Viro		addr = task_stack_page(task) + regoff[regno];
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return addr;
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Get contents of register REGNO in task TASK.
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned long
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsget_reg(struct task_struct * task, unsigned long regno)
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Special hack for fpcr -- combine hardware and software bits.  */
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (regno == 63) {
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long fpcr = *get_reg_addr(task, regno);
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long swcr
13437bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		  = task_thread_info(task)->ieee_state & IEEE_SW_MASK;
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		swcr = swcr_update_status(swcr, fpcr);
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return fpcr | swcr;
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return *get_reg_addr(task, regno);
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Write contents of register REGNO in task TASK.
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsput_reg(struct task_struct *task, unsigned long regno, unsigned long data)
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (regno == 63) {
14837bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		task_thread_info(task)->ieee_state
14937bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		  = ((task_thread_info(task)->ieee_state & ~IEEE_SW_MASK)
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     | (data & IEEE_SW_MASK));
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data = (data & FPCR_DYN_MASK) | ieee_swcr_to_fpcr(data);
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*get_reg_addr(task, regno) = data;
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsread_int(struct task_struct *task, unsigned long addr, int * data)
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int copied = access_process_vm(task, addr, data, sizeof(int), 0);
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (copied == sizeof(int)) ? 0 : -EIO;
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldswrite_int(struct task_struct *task, unsigned long addr, int data)
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int copied = access_process_vm(task, addr, &data, sizeof(int), 1);
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (copied == sizeof(int)) ? 0 : -EIO;
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Set breakpoint.
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsptrace_set_bpt(struct task_struct * child)
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int displ, i, res, reg_b, nsaved = 0;
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int insn, op_code;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long pc;
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pc  = get_reg(child, REG_PC);
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	res = read_int(child, pc, (int *) &insn);
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (res < 0)
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return res;
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	op_code = insn >> 26;
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (op_code >= 0x30) {
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * It's a branch: instead of trying to figure out
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * whether the branch will be taken or not, we'll put
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * a breakpoint at either location.  This is simpler,
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * more reliable, and probably not a whole lot slower
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * than the alternative approach of emulating the
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * branch (emulation can be tricky for fp branches).
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		displ = ((s32)(insn << 11)) >> 9;
19737bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (displ)		/* guard against unoptimized code */
19937bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro			task_thread_info(child)->bpt_addr[nsaved++]
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  = pc + 4 + displ;
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		DBG(DBG_BPT, ("execing branch\n"));
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (op_code == 0x1a) {
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		reg_b = (insn >> 16) & 0x1f;
20437bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		task_thread_info(child)->bpt_addr[nsaved++] = get_reg(child, reg_b);
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		DBG(DBG_BPT, ("execing jump\n"));
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
20737bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		task_thread_info(child)->bpt_addr[nsaved++] = pc + 4;
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		DBG(DBG_BPT, ("execing normal insn\n"));
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* install breakpoints: */
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < nsaved; ++i) {
21337bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		res = read_int(child, task_thread_info(child)->bpt_addr[i],
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       (int *) &insn);
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (res < 0)
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return res;
21737bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		task_thread_info(child)->bpt_insn[i] = insn;
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		DBG(DBG_BPT, ("    -> next_pc=%lx\n",
21937bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro			      task_thread_info(child)->bpt_addr[i]));
22037bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		res = write_int(child, task_thread_info(child)->bpt_addr[i],
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				BREAKINST);
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (res < 0)
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return res;
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
22537bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro	task_thread_info(child)->bpt_nsaved = nsaved;
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Ensure no single-step breakpoint is pending.  Returns non-zero
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * value if child was being single-stepped.
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsptrace_cancel_bpt(struct task_struct * child)
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
23637bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro	int i, nsaved = task_thread_info(child)->bpt_nsaved;
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23837bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro	task_thread_info(child)->bpt_nsaved = 0;
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (nsaved > 2) {
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nsaved = 2;
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < nsaved; ++i) {
24637bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro		write_int(child, task_thread_info(child)->bpt_addr[i],
24737bfbaf995d2c1f8196ee04c9d6f68258d5ec3e8Al Viro			  task_thread_info(child)->bpt_insn[i]);
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (nsaved != 0);
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
252fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwigvoid user_enable_single_step(struct task_struct *child)
253fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig{
254fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig	/* Mark single stepping.  */
255fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig	task_thread_info(child)->bpt_nsaved = -1;
256fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig}
257fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig
258fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwigvoid user_disable_single_step(struct task_struct *child)
259fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig{
260fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig	ptrace_cancel_bpt(child);
261fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig}
262fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Called by kernel/ptrace.c when detaching..
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Make sure the single step bit is not set.
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid ptrace_disable(struct task_struct *child)
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
270fd341abba65e3d93fc24c132e4c0278d18b6d2fcChristoph Hellwig	user_disable_single_step(child);
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2739b05a69e0534ec70bc94921936ffa05b330507cbNamhyung Kimlong arch_ptrace(struct task_struct *child, long request,
2749b05a69e0534ec70bc94921936ffa05b330507cbNamhyung Kim		 unsigned long addr, unsigned long data)
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long tmp;
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t copied;
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	long ret;
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (request) {
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* When I and D space are separate, these will need to be fixed.  */
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PTRACE_PEEKTEXT: /* read word at location addr. */
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PTRACE_PEEKDATA:
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = -EIO;
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (copied != sizeof(tmp))
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
289a5f833f3c1961488733bf645b011b6350ee5c7beChristoph Hellwig		force_successful_syscall_return();
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = tmp;
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Read register number ADDR. */
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PTRACE_PEEKUSR:
295a5f833f3c1961488733bf645b011b6350ee5c7beChristoph Hellwig		force_successful_syscall_return();
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = get_reg(child, addr);
2979b05a69e0534ec70bc94921936ffa05b330507cbNamhyung Kim		DBG(DBG_MEM, ("peek $%lu->%#lx\n", addr, ret));
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* When I and D space are separate, this will have to be fixed.  */
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PTRACE_POKETEXT: /* write the word at location addr. */
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PTRACE_POKEDATA:
303f284ce7269031947326bac6bb19a977705276222Alexey Dobriyan		ret = generic_ptrace_pokedata(child, addr, data);
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PTRACE_POKEUSR: /* write the specified register */
3079b05a69e0534ec70bc94921936ffa05b330507cbNamhyung Kim		DBG(DBG_MEM, ("poke $%lu<-%#lx\n", addr, data));
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = put_reg(child, addr, data);
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = ptrace_request(child, request, addr, data);
312a5f833f3c1961488733bf645b011b6350ee5c7beChristoph Hellwig		break;
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31712f79be93d94698778ff2b3f921073fc5f6780d6Al Viroasmlinkage unsigned long syscall_trace_enter(void)
31812f79be93d94698778ff2b3f921073fc5f6780d6Al Viro{
31912f79be93d94698778ff2b3f921073fc5f6780d6Al Viro	unsigned long ret = 0;
320a9302e8439445710552886e7b623dbcfa943a1f2蔡正龙	struct pt_regs *regs = current_pt_regs();
32112f79be93d94698778ff2b3f921073fc5f6780d6Al Viro	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
32212f79be93d94698778ff2b3f921073fc5f6780d6Al Viro	    tracehook_report_syscall_entry(current_pt_regs()))
32312f79be93d94698778ff2b3f921073fc5f6780d6Al Viro		ret = -1UL;
32491397401bb5072f71e8ce8744ad0bdec3e875a91Eric Paris	audit_syscall_entry(regs->r0, regs->r16, regs->r17, regs->r18, regs->r19);
32512f79be93d94698778ff2b3f921073fc5f6780d6Al Viro	return ret ?: current_pt_regs()->r0;
32612f79be93d94698778ff2b3f921073fc5f6780d6Al Viro}
32712f79be93d94698778ff2b3f921073fc5f6780d6Al Viro
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsasmlinkage void
32912f79be93d94698778ff2b3f921073fc5f6780d6Al Virosyscall_trace_leave(void)
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
331a9302e8439445710552886e7b623dbcfa943a1f2蔡正龙	audit_syscall_exit(current_pt_regs());
33212f79be93d94698778ff2b3f921073fc5f6780d6Al Viro	if (test_thread_flag(TIF_SYSCALL_TRACE))
33312f79be93d94698778ff2b3f921073fc5f6780d6Al Viro		tracehook_report_syscall_exit(current_pt_regs(), 0);
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
335