1
2
3
4
5
6#include <linux/kernel.h>
7#include <linux/sched.h>
8#include <linux/sched/task_stack.h>
9#include <linux/mm.h>
10#include <linux/smp.h>
11#include <linux/errno.h>
12#include <linux/ptrace.h>
13#include <linux/user.h>
14#include <linux/signal.h>
15#include <linux/security.h>
16
17#include <linux/uaccess.h>
18#include <asm/page.h>
19#include <asm/pgtable.h>
20#include <asm/processor.h>
21#include <arch/hwregs/supp_reg.h>
22
23
24
25
26
27#define CCS_MASK 0x00087c00
28
29#define SBIT_USER (1 << (S_CCS_BITNR + CCS_SHIFT))
30
31static int put_debugreg(long pid, unsigned int regno, long data);
32static long get_debugreg(long pid, unsigned int regno);
33static unsigned long get_pseudo_pc(struct task_struct *child);
34void deconfigure_bp(long pid);
35
36extern unsigned long cris_signal_return_page;
37
38
39
40
41long get_reg(struct task_struct *task, unsigned int regno)
42{
43
44
45
46 unsigned long ret;
47
48 if (regno <= PT_EDA)
49 ret = ((unsigned long *)task_pt_regs(task))[regno];
50 else if (regno == PT_USP)
51 ret = task->thread.usp;
52 else if (regno == PT_PPC)
53 ret = get_pseudo_pc(task);
54 else if (regno <= PT_MAX)
55 ret = get_debugreg(task->pid, regno);
56 else
57 ret = 0;
58
59 return ret;
60}
61
62
63
64
65int put_reg(struct task_struct *task, unsigned int regno, unsigned long data)
66{
67 if (regno <= PT_EDA)
68 ((unsigned long *)task_pt_regs(task))[regno] = data;
69 else if (regno == PT_USP)
70 task->thread.usp = data;
71 else if (regno == PT_PPC) {
72
73 if (data != get_pseudo_pc(task))
74 task_pt_regs(task)->erp = data;
75 } else if (regno <= PT_MAX)
76 return put_debugreg(task->pid, regno, data);
77 else
78 return -1;
79 return 0;
80}
81
82void user_enable_single_step(struct task_struct *child)
83{
84 unsigned long tmp;
85
86
87
88
89
90 if (!get_reg(child, PT_SPC)) {
91
92 tmp = get_reg(child, PT_ERP) & ~1;
93 put_reg(child, PT_SPC, tmp);
94 }
95 tmp = get_reg(child, PT_CCS) | SBIT_USER;
96 put_reg(child, PT_CCS, tmp);
97}
98
99void user_disable_single_step(struct task_struct *child)
100{
101 put_reg(child, PT_SPC, 0);
102
103 if (!get_debugreg(child->pid, PT_BP_CTRL)) {
104 unsigned long tmp;
105
106 tmp = get_reg(child, PT_CCS) & ~SBIT_USER;
107 put_reg(child, PT_CCS, tmp);
108 }
109}
110
111
112
113
114
115
116void
117ptrace_disable(struct task_struct *child)
118{
119
120 user_disable_single_step(child);
121 put_reg(child, PT_SPC, 0);
122
123
124 deconfigure_bp(child->pid);
125}
126
127
128long arch_ptrace(struct task_struct *child, long request,
129 unsigned long addr, unsigned long data)
130{
131 int ret;
132 unsigned int regno = addr >> 2;
133 unsigned long __user *datap = (unsigned long __user *)data;
134
135 switch (request) {
136
137 case PTRACE_PEEKTEXT:
138 case PTRACE_PEEKDATA: {
139 unsigned long tmp;
140 int copied;
141
142 ret = -EIO;
143
144
145
146
147
148 if ((addr & PAGE_MASK) == cris_signal_return_page) {
149
150 tmp = *(unsigned long*)addr;
151 } else {
152 copied = ptrace_access_vm(child, addr, &tmp, sizeof(tmp), FOLL_FORCE);
153
154 if (copied != sizeof(tmp))
155 break;
156 }
157
158 ret = put_user(tmp,datap);
159 break;
160 }
161
162
163 case PTRACE_PEEKUSR: {
164 unsigned long tmp;
165
166 ret = -EIO;
167 if ((addr & 3) || regno > PT_MAX)
168 break;
169
170 tmp = get_reg(child, regno);
171 ret = put_user(tmp, datap);
172 break;
173 }
174
175
176 case PTRACE_POKETEXT:
177 case PTRACE_POKEDATA:
178 ret = generic_ptrace_pokedata(child, addr, data);
179 break;
180
181
182 case PTRACE_POKEUSR:
183 ret = -EIO;
184 if ((addr & 3) || regno > PT_MAX)
185 break;
186
187 if (regno == PT_CCS) {
188
189
190
191 data &= CCS_MASK;
192 data |= get_reg(child, PT_CCS) & ~CCS_MASK;
193 }
194 if (put_reg(child, regno, data))
195 break;
196 ret = 0;
197 break;
198
199
200 case PTRACE_GETREGS: {
201 int i;
202 unsigned long tmp;
203
204 for (i = 0; i <= PT_MAX; i++) {
205 tmp = get_reg(child, i);
206
207 if (put_user(tmp, datap)) {
208 ret = -EFAULT;
209 goto out_tsk;
210 }
211
212 datap++;
213 }
214
215 ret = 0;
216 break;
217 }
218
219
220 case PTRACE_SETREGS: {
221 int i;
222 unsigned long tmp;
223
224 for (i = 0; i <= PT_MAX; i++) {
225 if (get_user(tmp, datap)) {
226 ret = -EFAULT;
227 goto out_tsk;
228 }
229
230 if (i == PT_CCS) {
231 tmp &= CCS_MASK;
232 tmp |= get_reg(child, PT_CCS) & ~CCS_MASK;
233 }
234
235 put_reg(child, i, tmp);
236 datap++;
237 }
238
239 ret = 0;
240 break;
241 }
242
243 default:
244 ret = ptrace_request(child, request, addr, data);
245 break;
246 }
247
248out_tsk:
249 return ret;
250}
251
252void do_syscall_trace(void)
253{
254 if (!test_thread_flag(TIF_SYSCALL_TRACE))
255 return;
256
257 if (!(current->ptrace & PT_PTRACED))
258 return;
259
260
261
262 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
263 ? 0x80 : 0));
264
265
266
267
268
269 if (current->exit_code) {
270 send_sig(current->exit_code, current, 1);
271 current->exit_code = 0;
272 }
273}
274
275
276
277static int insn_size(struct task_struct *child, unsigned long pc)
278{
279 unsigned long opcode;
280 int copied;
281 int opsize = 0;
282
283
284 copied = access_process_vm(child, pc, &opcode, sizeof(opcode), FOLL_FORCE);
285 if (copied != sizeof(opcode))
286 return 0;
287
288 switch ((opcode & 0x0f00) >> 8) {
289 case 0x0:
290 case 0x9:
291 case 0xb:
292 opsize = 2;
293 break;
294 case 0xe:
295 case 0xf:
296 opsize = 6;
297 break;
298 case 0xd:
299
300 if ((opcode & 0xff) == 0xff)
301 opsize = 4;
302 else
303 opsize = 6;
304 break;
305 default:
306 panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
307 opcode, pc);
308 }
309
310 return opsize;
311}
312
313static unsigned long get_pseudo_pc(struct task_struct *child)
314{
315
316 unsigned long pc = get_reg(child, PT_ERP);
317
318 if (pc & 0x1) {
319 unsigned long spc = get_reg(child, PT_SPC);
320
321
322 if (spc) {
323
324
325
326 pc = spc;
327 } else {
328
329
330 pc += insn_size(child, pc & ~1) - 1;
331 }
332 }
333 return pc;
334}
335
336static long bp_owner = 0;
337
338
339void deconfigure_bp(long pid)
340{
341 int bp;
342
343
344 if (bp_owner != pid)
345 return;
346
347 for (bp = 0; bp < 6; bp++) {
348 unsigned long tmp;
349
350 put_debugreg(pid, PT_BP + 3 + (bp * 2), 0);
351 put_debugreg(pid, PT_BP + 4 + (bp * 2), 0);
352
353
354 tmp = get_debugreg(pid, PT_BP_CTRL) & ~(3 << (2 + (bp * 4)));
355 put_debugreg(pid, PT_BP_CTRL, tmp);
356 }
357
358 bp_owner = 0;
359}
360
361static int put_debugreg(long pid, unsigned int regno, long data)
362{
363 int ret = 0;
364 register int old_srs;
365
366#ifdef CONFIG_ETRAX_KGDB
367
368
369 return (data == 0) ? ret : -1;
370#endif
371
372
373 if (!bp_owner)
374 bp_owner = pid;
375 else if (bp_owner != pid) {
376
377
378 return (data == 0) ? ret : -1;
379 }
380
381
382 SPEC_REG_RD(SPEC_REG_SRS, old_srs);
383
384 SUPP_BANK_SEL(BANK_BP);
385
386 switch (regno - PT_BP) {
387 case 0:
388 SUPP_REG_WR(0, data); break;
389 case 1:
390 case 2:
391 if (data)
392 ret = -1;
393 break;
394 case 3:
395 SUPP_REG_WR(3, data); break;
396 case 4:
397 SUPP_REG_WR(4, data); break;
398 case 5:
399 SUPP_REG_WR(5, data); break;
400 case 6:
401 SUPP_REG_WR(6, data); break;
402 case 7:
403 SUPP_REG_WR(7, data); break;
404 case 8:
405 SUPP_REG_WR(8, data); break;
406 case 9:
407 SUPP_REG_WR(9, data); break;
408 case 10:
409 SUPP_REG_WR(10, data); break;
410 case 11:
411 SUPP_REG_WR(11, data); break;
412 case 12:
413 SUPP_REG_WR(12, data); break;
414 case 13:
415 SUPP_REG_WR(13, data); break;
416 case 14:
417 SUPP_REG_WR(14, data); break;
418 default:
419 ret = -1;
420 break;
421 }
422
423
424 SPEC_REG_WR(SPEC_REG_SRS, old_srs);
425
426 NOP();
427 NOP();
428 NOP();
429
430 return ret;
431}
432
433static long get_debugreg(long pid, unsigned int regno)
434{
435 register int old_srs;
436 register long data;
437
438 if (pid != bp_owner) {
439 return 0;
440 }
441
442
443 SPEC_REG_RD(SPEC_REG_SRS, old_srs);
444
445 SUPP_BANK_SEL(BANK_BP);
446
447 switch (regno - PT_BP) {
448 case 0:
449 SUPP_REG_RD(0, data); break;
450 case 1:
451 case 2:
452
453 data = 0;
454 break;
455 case 3:
456 SUPP_REG_RD(3, data); break;
457 case 4:
458 SUPP_REG_RD(4, data); break;
459 case 5:
460 SUPP_REG_RD(5, data); break;
461 case 6:
462 SUPP_REG_RD(6, data); break;
463 case 7:
464 SUPP_REG_RD(7, data); break;
465 case 8:
466 SUPP_REG_RD(8, data); break;
467 case 9:
468 SUPP_REG_RD(9, data); break;
469 case 10:
470 SUPP_REG_RD(10, data); break;
471 case 11:
472 SUPP_REG_RD(11, data); break;
473 case 12:
474 SUPP_REG_RD(12, data); break;
475 case 13:
476 SUPP_REG_RD(13, data); break;
477 case 14:
478 SUPP_REG_RD(14, data); break;
479 default:
480
481 data = 0;
482 }
483
484
485 SPEC_REG_WR(SPEC_REG_SRS, old_srs);
486
487 NOP();
488 NOP();
489 NOP();
490
491 return data;
492}
493