xref: /openbmc/linux/arch/microblaze/kernel/ptrace.c (revision fd589a8f)
1 /*
2  * `ptrace' system call
3  *
4  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5  * Copyright (C) 2007-2009 PetaLogix
6  * Copyright (C) 2004-2007 John Williams <john.williams@petalogix.com>
7  *
8  * derived from arch/v850/kernel/ptrace.c
9  *
10  * Copyright (C) 2002,03 NEC Electronics Corporation
11  * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
12  *
13  * Derived from arch/mips/kernel/ptrace.c:
14  *
15  * Copyright (C) 1992 Ross Biro
16  * Copyright (C) Linus Torvalds
17  * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
18  * Copyright (C) 1996 David S. Miller
19  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
20  * Copyright (C) 1999 MIPS Technologies, Inc.
21  *
22  * This file is subject to the terms and conditions of the GNU General
23  * Public License. See the file COPYING in the main directory of this
24  * archive for more details.
25  */
26 
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/sched.h>
30 #include <linux/ptrace.h>
31 #include <linux/signal.h>
32 
33 #include <linux/errno.h>
34 #include <asm/processor.h>
35 #include <linux/uaccess.h>
36 #include <asm/asm-offsets.h>
37 
38 /* Returns the address where the register at REG_OFFS in P is stashed away. */
39 static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
40 					struct task_struct *t)
41 {
42 	struct pt_regs *regs;
43 
44 	/*
45 	 * Three basic cases:
46 	 *
47 	 * (1)	A register normally saved before calling the scheduler, is
48 	 *	available in the kernel entry pt_regs structure at the top
49 	 *	of the kernel stack. The kernel trap/irq exit path takes
50 	 *	care to save/restore almost all registers for ptrace'd
51 	 *	processes.
52 	 *
53 	 * (2)	A call-clobbered register, where the process P entered the
54 	 *	kernel via [syscall] trap, is not stored anywhere; that's
55 	 *	OK, because such registers are not expected to be preserved
56 	 *	when the trap returns anyway (so we don't actually bother to
57 	 *	test for this case).
58 	 *
59 	 * (3)	A few registers not used at all by the kernel, and so
60 	 *	normally never saved except by context-switches, are in the
61 	 *	context switch state.
62 	 */
63 
64 	/* Register saved during kernel entry (or not available). */
65 	regs = task_pt_regs(t);
66 
67 	return (microblaze_reg_t *)((char *)regs + reg_offs);
68 }
69 
70 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
71 {
72 	int rval;
73 	unsigned long val = 0;
74 	unsigned long copied;
75 
76 	switch (request) {
77 	case PTRACE_PEEKTEXT: /* read word at location addr. */
78 	case PTRACE_PEEKDATA:
79 		pr_debug("PEEKTEXT/PEEKDATA at %08lX\n", addr);
80 		copied = access_process_vm(child, addr, &val, sizeof(val), 0);
81 		rval = -EIO;
82 		if (copied != sizeof(val))
83 			break;
84 		rval = put_user(val, (unsigned long *)data);
85 		break;
86 
87 	case PTRACE_POKETEXT: /* write the word at location addr. */
88 	case PTRACE_POKEDATA:
89 		pr_debug("POKETEXT/POKEDATA to %08lX\n", addr);
90 		rval = 0;
91 		if (access_process_vm(child, addr, &data, sizeof(data), 1)
92 		    == sizeof(data))
93 			break;
94 		rval = -EIO;
95 		break;
96 
97 	/* Read/write the word at location ADDR in the registers. */
98 	case PTRACE_PEEKUSR:
99 	case PTRACE_POKEUSR:
100 		pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
101 		rval = 0;
102 		if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
103 			/*
104 			 * Special requests that don't actually correspond
105 			 * to offsets in struct pt_regs.
106 			 */
107 			if (addr == PT_TEXT_ADDR) {
108 				val = child->mm->start_code;
109 			} else if (addr == PT_DATA_ADDR) {
110 				val = child->mm->start_data;
111 			} else if (addr == PT_TEXT_LEN) {
112 				val = child->mm->end_code
113 					- child->mm->start_code;
114 			} else {
115 				rval = -EIO;
116 			}
117 		} else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) {
118 			microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
119 			if (request == PTRACE_PEEKUSR)
120 				val = *reg_addr;
121 			else
122 				*reg_addr = data;
123 		} else
124 			rval = -EIO;
125 
126 		if (rval == 0 && request == PTRACE_PEEKUSR)
127 			rval = put_user(val, (unsigned long *)data);
128 		break;
129 	/* Continue and stop at next (return from) syscall */
130 	case PTRACE_SYSCALL:
131 		pr_debug("PTRACE_SYSCALL\n");
132 	case PTRACE_SINGLESTEP:
133 		pr_debug("PTRACE_SINGLESTEP\n");
134 	/* Restart after a signal.  */
135 	case PTRACE_CONT:
136 		pr_debug("PTRACE_CONT\n");
137 		rval = -EIO;
138 		if (!valid_signal(data))
139 			break;
140 
141 		if (request == PTRACE_SYSCALL)
142 			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
143 		else
144 			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
145 
146 		child->exit_code = data;
147 		pr_debug("wakeup_process\n");
148 		wake_up_process(child);
149 		rval = 0;
150 		break;
151 
152 	/*
153 	 * make the child exit.  Best I can do is send it a sigkill.
154 	 * perhaps it should be put in the status that it wants to
155 	 * exit.
156 	 */
157 	case PTRACE_KILL:
158 		pr_debug("PTRACE_KILL\n");
159 		rval = 0;
160 		if (child->exit_state == EXIT_ZOMBIE)	/* already dead */
161 			break;
162 		child->exit_code = SIGKILL;
163 		wake_up_process(child);
164 		break;
165 
166 	case PTRACE_DETACH: /* detach a process that was attached. */
167 		pr_debug("PTRACE_DETACH\n");
168 		rval = ptrace_detach(child, data);
169 		break;
170 	default:
171 		/* rval = ptrace_request(child, request, addr, data); noMMU */
172 		rval = -EIO;
173 	}
174 	return rval;
175 }
176 
177 void ptrace_disable(struct task_struct *child)
178 {
179 	/* nothing to do */
180 }
181