xref: /linux/arch/microblaze/kernel/ptrace.c (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
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/sched/task_stack.h>
31 #include <linux/ptrace.h>
32 #include <linux/signal.h>
33 #include <linux/elf.h>
34 #include <linux/audit.h>
35 #include <linux/seccomp.h>
36 
37 #include <linux/errno.h>
38 #include <asm/processor.h>
39 #include <linux/uaccess.h>
40 #include <asm/asm-offsets.h>
41 #include <asm/cacheflush.h>
42 #include <asm/syscall.h>
43 #include <linux/io.h>
44 
45 /* Returns the address where the register at REG_OFFS in P is stashed away. */
46 static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
47 					struct task_struct *t)
48 {
49 	struct pt_regs *regs;
50 
51 	/*
52 	 * Three basic cases:
53 	 *
54 	 * (1)	A register normally saved before calling the scheduler, is
55 	 *	available in the kernel entry pt_regs structure at the top
56 	 *	of the kernel stack. The kernel trap/irq exit path takes
57 	 *	care to save/restore almost all registers for ptrace'd
58 	 *	processes.
59 	 *
60 	 * (2)	A call-clobbered register, where the process P entered the
61 	 *	kernel via [syscall] trap, is not stored anywhere; that's
62 	 *	OK, because such registers are not expected to be preserved
63 	 *	when the trap returns anyway (so we don't actually bother to
64 	 *	test for this case).
65 	 *
66 	 * (3)	A few registers not used at all by the kernel, and so
67 	 *	normally never saved except by context-switches, are in the
68 	 *	context switch state.
69 	 */
70 
71 	/* Register saved during kernel entry (or not available). */
72 	regs = task_pt_regs(t);
73 
74 	return (microblaze_reg_t *)((char *)regs + reg_offs);
75 }
76 
77 long arch_ptrace(struct task_struct *child, long request,
78 		 unsigned long addr, unsigned long data)
79 {
80 	int rval;
81 	unsigned long val = 0;
82 
83 	switch (request) {
84 	/* Read/write the word at location ADDR in the registers. */
85 	case PTRACE_PEEKUSR:
86 	case PTRACE_POKEUSR:
87 		pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
88 		rval = 0;
89 		if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
90 			/*
91 			 * Special requests that don't actually correspond
92 			 * to offsets in struct pt_regs.
93 			 */
94 			if (addr == PT_TEXT_ADDR) {
95 				val = child->mm->start_code;
96 			} else if (addr == PT_DATA_ADDR) {
97 				val = child->mm->start_data;
98 			} else if (addr == PT_TEXT_LEN) {
99 				val = child->mm->end_code
100 					- child->mm->start_code;
101 			} else {
102 				rval = -EIO;
103 			}
104 		} else if (request == PTRACE_POKEUSR &&
105 			   (addr == PT_MSR || addr == PT_MODE)) {
106 			rval = -EIO;
107 		} else if (addr < PT_SIZE && (addr & 0x3) == 0) {
108 			microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
109 			if (request == PTRACE_PEEKUSR)
110 				val = *reg_addr;
111 			else {
112 #if 1
113 				*reg_addr = data;
114 #else
115 				/* MS potential problem on WB system
116 				 * Be aware that reg_addr is virtual address
117 				 * virt_to_phys conversion is necessary.
118 				 * This could be sensible solution.
119 				 */
120 				u32 paddr = virt_to_phys((u32)reg_addr);
121 				invalidate_icache_range(paddr, paddr + 4);
122 				*reg_addr = data;
123 				flush_dcache_range(paddr, paddr + 4);
124 #endif
125 			}
126 		} else
127 			rval = -EIO;
128 
129 		if (rval == 0 && request == PTRACE_PEEKUSR)
130 			rval = put_user(val, (unsigned long __user *)data);
131 		break;
132 	default:
133 		rval = ptrace_request(child, request, addr, data);
134 	}
135 	return rval;
136 }
137 
138 asmlinkage unsigned long do_syscall_trace_enter(struct pt_regs *regs)
139 {
140 	unsigned long ret = 0;
141 
142 	secure_computing_strict(regs->r12);
143 
144 	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
145 	    !ptrace_report_syscall_permit_entry(regs))
146 		/*
147 		 * Tracing decided this syscall should not happen.
148 		 * We'll return a bogus call number to get an ENOSYS
149 		 * error, but leave the original number in regs->regs[0].
150 		 */
151 		ret = -1L;
152 
153 	audit_syscall_entry(regs->r12, regs->r5, regs->r6, regs->r7, regs->r8);
154 
155 	return ret ?: regs->r12;
156 }
157 
158 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
159 {
160 	int step;
161 
162 	audit_syscall_exit(regs);
163 
164 	step = test_thread_flag(TIF_SINGLESTEP);
165 	if (step || test_thread_flag(TIF_SYSCALL_TRACE))
166 		ptrace_report_syscall_exit(regs, step);
167 }
168 
169 void ptrace_disable(struct task_struct *child)
170 {
171 	/* nothing to do */
172 }
173