xref: /openbmc/linux/arch/m68k/kernel/ptrace.c (revision e802ca75)
1 /*
2  *  linux/arch/m68k/kernel/ptrace.c
3  *
4  *  Copyright (C) 1994 by Hamish Macdonald
5  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
6  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
7  *
8  * This file is subject to the terms and conditions of the GNU General
9  * Public License.  See the file COPYING in the main directory of
10  * this archive for more details.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/mm.h>
17 #include <linux/smp.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/signal.h>
22 
23 #include <linux/uaccess.h>
24 #include <asm/page.h>
25 #include <asm/processor.h>
26 
27 /*
28  * does not yet catch signals sent when the child dies.
29  * in exit.c or in signal.c.
30  */
31 
32 /* determines which bits in the SR the user has access to. */
33 /* 1 = access 0 = no access */
34 #define SR_MASK 0x001f
35 
36 /* sets the trace bits. */
37 #define TRACE_BITS 0xC000
38 #define T1_BIT 0x8000
39 #define T0_BIT 0x4000
40 
41 /* Find the stack offset for a register, relative to thread.esp0. */
42 #define PT_REG(reg)	((long)&((struct pt_regs *)0)->reg)
43 #define SW_REG(reg)	((long)&((struct switch_stack *)0)->reg \
44 			 - sizeof(struct switch_stack))
45 /* Mapping from PT_xxx to the stack offset at which the register is
46    saved.  Notice that usp has no stack-slot and needs to be treated
47    specially (see get_reg/put_reg below). */
48 static const int regoff[] = {
49 	[0]	= PT_REG(d1),
50 	[1]	= PT_REG(d2),
51 	[2]	= PT_REG(d3),
52 	[3]	= PT_REG(d4),
53 	[4]	= PT_REG(d5),
54 	[5]	= SW_REG(d6),
55 	[6]	= SW_REG(d7),
56 	[7]	= PT_REG(a0),
57 	[8]	= PT_REG(a1),
58 	[9]	= PT_REG(a2),
59 	[10]	= SW_REG(a3),
60 	[11]	= SW_REG(a4),
61 	[12]	= SW_REG(a5),
62 	[13]	= SW_REG(a6),
63 	[14]	= PT_REG(d0),
64 	[15]	= -1,
65 	[16]	= PT_REG(orig_d0),
66 	[17]	= PT_REG(sr),
67 	[18]	= PT_REG(pc),
68 };
69 
70 /*
71  * Get contents of register REGNO in task TASK.
72  */
73 static inline long get_reg(struct task_struct *task, int regno)
74 {
75 	unsigned long *addr;
76 
77 	if (regno == PT_USP)
78 		addr = &task->thread.usp;
79 	else if (regno < ARRAY_SIZE(regoff))
80 		addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
81 	else
82 		return 0;
83 	/* Need to take stkadj into account. */
84 	if (regno == PT_SR || regno == PT_PC) {
85 		long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
86 		addr = (unsigned long *) ((unsigned long)addr + stkadj);
87 		/* The sr is actually a 16 bit register.  */
88 		if (regno == PT_SR)
89 			return *(unsigned short *)addr;
90 	}
91 	return *addr;
92 }
93 
94 /*
95  * Write contents of register REGNO in task TASK.
96  */
97 static inline int put_reg(struct task_struct *task, int regno,
98 			  unsigned long data)
99 {
100 	unsigned long *addr;
101 
102 	if (regno == PT_USP)
103 		addr = &task->thread.usp;
104 	else if (regno < ARRAY_SIZE(regoff))
105 		addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
106 	else
107 		return -1;
108 	/* Need to take stkadj into account. */
109 	if (regno == PT_SR || regno == PT_PC) {
110 		long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
111 		addr = (unsigned long *) ((unsigned long)addr + stkadj);
112 		/* The sr is actually a 16 bit register.  */
113 		if (regno == PT_SR) {
114 			*(unsigned short *)addr = data;
115 			return 0;
116 		}
117 	}
118 	*addr = data;
119 	return 0;
120 }
121 
122 /*
123  * Make sure the single step bit is not set.
124  */
125 static inline void singlestep_disable(struct task_struct *child)
126 {
127 	unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
128 	put_reg(child, PT_SR, tmp);
129 	clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
130 }
131 
132 /*
133  * Called by kernel/ptrace.c when detaching..
134  */
135 void ptrace_disable(struct task_struct *child)
136 {
137 	singlestep_disable(child);
138 }
139 
140 void user_enable_single_step(struct task_struct *child)
141 {
142 	unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
143 	put_reg(child, PT_SR, tmp | T1_BIT);
144 	set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
145 }
146 
147 #ifdef CONFIG_MMU
148 void user_enable_block_step(struct task_struct *child)
149 {
150 	unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
151 	put_reg(child, PT_SR, tmp | T0_BIT);
152 }
153 #endif
154 
155 void user_disable_single_step(struct task_struct *child)
156 {
157 	singlestep_disable(child);
158 }
159 
160 long arch_ptrace(struct task_struct *child, long request,
161 		 unsigned long addr, unsigned long data)
162 {
163 	unsigned long tmp;
164 	int i, ret = 0;
165 	int regno = addr >> 2; /* temporary hack. */
166 	unsigned long __user *datap = (unsigned long __user *) data;
167 
168 	switch (request) {
169 	/* read the word at location addr in the USER area. */
170 	case PTRACE_PEEKUSR:
171 		if (addr & 3)
172 			goto out_eio;
173 
174 		if (regno >= 0 && regno < 19) {
175 			tmp = get_reg(child, regno);
176 		} else if (regno >= 21 && regno < 49) {
177 			tmp = child->thread.fp[regno - 21];
178 			/* Convert internal fpu reg representation
179 			 * into long double format
180 			 */
181 			if (FPU_IS_EMU && (regno < 45) && !(regno % 3))
182 				tmp = ((tmp & 0xffff0000) << 15) |
183 				      ((tmp & 0x0000ffff) << 16);
184 #ifndef CONFIG_MMU
185 		} else if (regno == 49) {
186 			tmp = child->mm->start_code;
187 		} else if (regno == 50) {
188 			tmp = child->mm->start_data;
189 		} else if (regno == 51) {
190 			tmp = child->mm->end_code;
191 #endif
192 		} else
193 			goto out_eio;
194 		ret = put_user(tmp, datap);
195 		break;
196 
197 	case PTRACE_POKEUSR:
198 	/* write the word at location addr in the USER area */
199 		if (addr & 3)
200 			goto out_eio;
201 
202 		if (regno == PT_SR) {
203 			data &= SR_MASK;
204 			data |= get_reg(child, PT_SR) & ~SR_MASK;
205 		}
206 		if (regno >= 0 && regno < 19) {
207 			if (put_reg(child, regno, data))
208 				goto out_eio;
209 		} else if (regno >= 21 && regno < 48) {
210 			/* Convert long double format
211 			 * into internal fpu reg representation
212 			 */
213 			if (FPU_IS_EMU && (regno < 45) && !(regno % 3)) {
214 				data <<= 15;
215 				data = (data & 0xffff0000) |
216 				       ((data & 0x0000ffff) >> 1);
217 			}
218 			child->thread.fp[regno - 21] = data;
219 		} else
220 			goto out_eio;
221 		break;
222 
223 	case PTRACE_GETREGS:	/* Get all gp regs from the child. */
224 		for (i = 0; i < 19; i++) {
225 			tmp = get_reg(child, i);
226 			ret = put_user(tmp, datap);
227 			if (ret)
228 				break;
229 			datap++;
230 		}
231 		break;
232 
233 	case PTRACE_SETREGS:	/* Set all gp regs in the child. */
234 		for (i = 0; i < 19; i++) {
235 			ret = get_user(tmp, datap);
236 			if (ret)
237 				break;
238 			if (i == PT_SR) {
239 				tmp &= SR_MASK;
240 				tmp |= get_reg(child, PT_SR) & ~SR_MASK;
241 			}
242 			put_reg(child, i, tmp);
243 			datap++;
244 		}
245 		break;
246 
247 	case PTRACE_GETFPREGS:	/* Get the child FPU state. */
248 		if (copy_to_user(datap, &child->thread.fp,
249 				 sizeof(struct user_m68kfp_struct)))
250 			ret = -EFAULT;
251 		break;
252 
253 	case PTRACE_SETFPREGS:	/* Set the child FPU state. */
254 		if (copy_from_user(&child->thread.fp, datap,
255 				   sizeof(struct user_m68kfp_struct)))
256 			ret = -EFAULT;
257 		break;
258 
259 	case PTRACE_GET_THREAD_AREA:
260 		ret = put_user(task_thread_info(child)->tp_value, datap);
261 		break;
262 
263 	default:
264 		ret = ptrace_request(child, request, addr, data);
265 		break;
266 	}
267 
268 	return ret;
269 out_eio:
270 	return -EIO;
271 }
272 
273 asmlinkage void syscall_trace(void)
274 {
275 	ptrace_report_syscall(0);
276 }
277 
278 #if defined(CONFIG_COLDFIRE) || !defined(CONFIG_MMU)
279 asmlinkage int syscall_trace_enter(void)
280 {
281 	int ret = 0;
282 
283 	if (test_thread_flag(TIF_SYSCALL_TRACE))
284 		ret = ptrace_report_syscall_entry(task_pt_regs(current));
285 	return ret;
286 }
287 
288 asmlinkage void syscall_trace_leave(void)
289 {
290 	if (test_thread_flag(TIF_SYSCALL_TRACE))
291 		ptrace_report_syscall_exit(task_pt_regs(current), 0);
292 }
293 #endif /* CONFIG_COLDFIRE */
294