Blackfin: simplify SYSCFG code a bit and ignore attempts to change it
[firefly-linux-kernel-4.4.55.git] / arch / blackfin / kernel / ptrace.c
1 /*
2  * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
3  * these modifications are Copyright 2004-2009 Analog Devices Inc.
4  *
5  * Licensed under the GPL-2
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/ptrace.h>
14 #include <linux/user.h>
15 #include <linux/signal.h>
16 #include <linux/uaccess.h>
17
18 #include <asm/page.h>
19 #include <asm/pgtable.h>
20 #include <asm/system.h>
21 #include <asm/processor.h>
22 #include <asm/asm-offsets.h>
23 #include <asm/dma.h>
24 #include <asm/fixed_code.h>
25 #include <asm/cacheflush.h>
26 #include <asm/mem_map.h>
27
28 #define TEXT_OFFSET 0
29 /*
30  * does not yet catch signals sent when the child dies.
31  * in exit.c or in signal.c.
32  */
33
34 /* Find the stack offset for a register, relative to thread.esp0. */
35 #define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
36
37 /*
38  * Get the address of the live pt_regs for the specified task.
39  * These are saved onto the top kernel stack when the process
40  * is not running.
41  *
42  * Note: if a user thread is execve'd from kernel space, the
43  * kernel stack will not be empty on entry to the kernel, so
44  * ptracing these tasks will fail.
45  */
46 static inline struct pt_regs *get_user_regs(struct task_struct *task)
47 {
48         return (struct pt_regs *)
49             ((unsigned long)task_stack_page(task) +
50              (THREAD_SIZE - sizeof(struct pt_regs)));
51 }
52
53 /*
54  * Get all user integer registers.
55  */
56 static inline int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
57 {
58         struct pt_regs regs;
59         memcpy(&regs, get_user_regs(tsk), sizeof(regs));
60         regs.usp = tsk->thread.usp;
61         return copy_to_user(uregs, &regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
62 }
63
64 /* Mapping from PT_xxx to the stack offset at which the register is
65  * saved.  Notice that usp has no stack-slot and needs to be treated
66  * specially (see get_reg/put_reg below).
67  */
68
69 /*
70  * Get contents of register REGNO in task TASK.
71  */
72 static inline long get_reg(struct task_struct *task, int regno)
73 {
74         unsigned char *reg_ptr;
75
76         struct pt_regs *regs =
77             (struct pt_regs *)((unsigned long)task_stack_page(task) +
78                                (THREAD_SIZE - sizeof(struct pt_regs)));
79         reg_ptr = (char *)regs;
80
81         switch (regno) {
82         case PT_USP:
83                 return task->thread.usp;
84         default:
85                 if (regno <= 216)
86                         return *(long *)(reg_ptr + regno);
87         }
88         /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
89
90         printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
91         return 0;
92 }
93
94 /*
95  * Write contents of register REGNO in task TASK.
96  */
97 static inline int
98 put_reg(struct task_struct *task, int regno, unsigned long data)
99 {
100         char *reg_ptr;
101
102         struct pt_regs *regs =
103             (struct pt_regs *)((unsigned long)task_stack_page(task) +
104                                (THREAD_SIZE - sizeof(struct pt_regs)));
105         reg_ptr = (char *)regs;
106
107         switch (regno) {
108         case PT_PC:
109                 /*********************************************************************/
110                 /* At this point the kernel is most likely in exception.             */
111                 /* The RETX register will be used to populate the pc of the process. */
112                 /*********************************************************************/
113                 regs->retx = data;
114                 regs->pc = data;
115                 break;
116         case PT_RETX:
117                 break;          /* regs->retx = data; break; */
118         case PT_USP:
119                 regs->usp = data;
120                 task->thread.usp = data;
121                 break;
122         default:
123                 if (regno <= 216)
124                         *(long *)(reg_ptr + regno) = data;
125         }
126         return 0;
127 }
128
129 /*
130  * check that an address falls within the bounds of the target process's memory mappings
131  */
132 static inline int is_user_addr_valid(struct task_struct *child,
133                                      unsigned long start, unsigned long len)
134 {
135         struct vm_area_struct *vma;
136         struct sram_list_struct *sraml;
137
138         /* overflow */
139         if (start + len < start)
140                 return -EIO;
141
142         vma = find_vma(child->mm, start);
143         if (vma && start >= vma->vm_start && start + len <= vma->vm_end)
144                         return 0;
145
146         for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
147                 if (start >= (unsigned long)sraml->addr
148                     && start + len < (unsigned long)sraml->addr + sraml->length)
149                         return 0;
150
151         if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
152                 return 0;
153
154         return -EIO;
155 }
156
157 void ptrace_enable(struct task_struct *child)
158 {
159         struct pt_regs *regs = task_pt_regs(child);
160         regs->syscfg |= SYSCFG_SSSTEP;
161 }
162
163 /*
164  * Called by kernel/ptrace.c when detaching..
165  *
166  * Make sure the single step bit is not set.
167  */
168 void ptrace_disable(struct task_struct *child)
169 {
170         struct pt_regs *regs = task_pt_regs(child);
171         regs->syscfg &= ~SYSCFG_SSSTEP;
172 }
173
174 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
175 {
176         int ret;
177         unsigned long __user *datap = (unsigned long __user *)data;
178         void *paddr = (void *)addr;
179
180         switch (request) {
181                 /* when I and D space are separate, these will need to be fixed. */
182         case PTRACE_PEEKDATA:
183                 pr_debug("ptrace: PEEKDATA\n");
184                 /* fall through */
185         case PTRACE_PEEKTEXT:   /* read word at location addr. */
186                 {
187                         unsigned long tmp = 0;
188                         int copied = 0, to_copy = sizeof(tmp);
189
190                         ret = -EIO;
191                         pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
192                         if (is_user_addr_valid(child, addr, to_copy) < 0)
193                                 break;
194                         pr_debug("ptrace: user address is valid\n");
195
196                         switch (bfin_mem_access_type(addr, to_copy)) {
197                         case BFIN_MEM_ACCESS_CORE:
198                         case BFIN_MEM_ACCESS_CORE_ONLY:
199                                 copied = access_process_vm(child, addr, &tmp,
200                                                            to_copy, 0);
201                                 if (copied)
202                                         break;
203
204                                 /* hrm, why didn't that work ... maybe no mapping */
205                                 if (addr >= FIXED_CODE_START &&
206                                     addr + to_copy <= FIXED_CODE_END) {
207                                         copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
208                                         copied = to_copy;
209                                 } else if (addr >= BOOT_ROM_START) {
210                                         memcpy(&tmp, paddr, to_copy);
211                                         copied = to_copy;
212                                 }
213
214                                 break;
215                         case BFIN_MEM_ACCESS_DMA:
216                                 if (safe_dma_memcpy(&tmp, paddr, to_copy))
217                                         copied = to_copy;
218                                 break;
219                         case BFIN_MEM_ACCESS_ITEST:
220                                 if (isram_memcpy(&tmp, paddr, to_copy))
221                                         copied = to_copy;
222                                 break;
223                         default:
224                                 copied = 0;
225                                 break;
226                         }
227
228                         pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
229                         if (copied == to_copy)
230                                 ret = put_user(tmp, datap);
231                         break;
232                 }
233
234                 /* read the word at location addr in the USER area. */
235         case PTRACE_PEEKUSR:
236                 {
237                         unsigned long tmp;
238                         ret = -EIO;
239                         tmp = 0;
240                         if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
241                                 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
242                                                     "0 - %x sizeof(pt_regs) is %lx\n",
243                                      (int)addr, sizeof(struct pt_regs));
244                                 break;
245                         }
246                         if (addr == sizeof(struct pt_regs)) {
247                                 /* PT_TEXT_ADDR */
248                                 tmp = child->mm->start_code + TEXT_OFFSET;
249                         } else if (addr == (sizeof(struct pt_regs) + 4)) {
250                                 /* PT_TEXT_END_ADDR */
251                                 tmp = child->mm->end_code;
252                         } else if (addr == (sizeof(struct pt_regs) + 8)) {
253                                 /* PT_DATA_ADDR */
254                                 tmp = child->mm->start_data;
255 #ifdef CONFIG_BINFMT_ELF_FDPIC
256                         } else if (addr == (sizeof(struct pt_regs) + 12)) {
257                                 goto case_PTRACE_GETFDPIC_EXEC;
258                         } else if (addr == (sizeof(struct pt_regs) + 16)) {
259                                 goto case_PTRACE_GETFDPIC_INTERP;
260 #endif
261                         } else {
262                                 tmp = get_reg(child, addr);
263                         }
264                         ret = put_user(tmp, datap);
265                         break;
266                 }
267
268 #ifdef CONFIG_BINFMT_ELF_FDPIC
269         case PTRACE_GETFDPIC: {
270                 unsigned long tmp = 0;
271
272                 switch (addr) {
273                 case_PTRACE_GETFDPIC_EXEC:
274                 case PTRACE_GETFDPIC_EXEC:
275                         tmp = child->mm->context.exec_fdpic_loadmap;
276                         break;
277                 case_PTRACE_GETFDPIC_INTERP:
278                 case PTRACE_GETFDPIC_INTERP:
279                         tmp = child->mm->context.interp_fdpic_loadmap;
280                         break;
281                 default:
282                         break;
283                 }
284
285                 ret = put_user(tmp, datap);
286                 break;
287         }
288 #endif
289
290                 /* when I and D space are separate, this will have to be fixed. */
291         case PTRACE_POKEDATA:
292                 pr_debug("ptrace: PTRACE_PEEKDATA\n");
293                 /* fall through */
294         case PTRACE_POKETEXT:   /* write the word at location addr. */
295                 {
296                         int copied = 0, to_copy = sizeof(data);
297
298                         ret = -EIO;
299                         pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
300                                  addr, to_copy, data);
301                         if (is_user_addr_valid(child, addr, to_copy) < 0)
302                                 break;
303                         pr_debug("ptrace: user address is valid\n");
304
305                         switch (bfin_mem_access_type(addr, to_copy)) {
306                         case BFIN_MEM_ACCESS_CORE:
307                         case BFIN_MEM_ACCESS_CORE_ONLY:
308                                 copied = access_process_vm(child, addr, &data,
309                                                            to_copy, 1);
310                                 break;
311                         case BFIN_MEM_ACCESS_DMA:
312                                 if (safe_dma_memcpy(paddr, &data, to_copy))
313                                         copied = to_copy;
314                                 break;
315                         case BFIN_MEM_ACCESS_ITEST:
316                                 if (isram_memcpy(paddr, &data, to_copy))
317                                         copied = to_copy;
318                                 break;
319                         default:
320                                 copied = 0;
321                                 break;
322                         }
323
324                         pr_debug("ptrace: copied size %d\n", copied);
325                         if (copied == to_copy)
326                                 ret = 0;
327                         break;
328                 }
329
330         case PTRACE_POKEUSR:    /* write the word at location addr in the USER area */
331                 ret = -EIO;
332                 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
333                         printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
334                         break;
335                 }
336
337                 /* Ignore writes to SYSCFG and other pseudo regs */
338                 if (addr >= PT_SYSCFG) {
339                         ret = 0;
340                         break;
341                 }
342                 ret = put_reg(child, addr, data);
343                 break;
344
345         case PTRACE_GETREGS:
346                 /* Get all gp regs from the child. */
347                 ret = ptrace_getregs(child, datap);
348                 break;
349
350         case PTRACE_SETREGS:
351                 printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
352                 /* Set all gp regs in the child. */
353                 ret = 0;
354                 break;
355
356         default:
357                 ret = ptrace_request(child, request, addr, data);
358                 break;
359         }
360
361         return ret;
362 }
363
364 asmlinkage void syscall_trace(void)
365 {
366         if (!test_thread_flag(TIF_SYSCALL_TRACE))
367                 return;
368
369         if (!(current->ptrace & PT_PTRACED))
370                 return;
371
372         /* the 0x80 provides a way for the tracing parent to distinguish
373          * between a syscall stop and SIGTRAP delivery
374          */
375         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
376                                  ? 0x80 : 0));
377
378         /*
379          * this isn't the same as continuing with a signal, but it will do
380          * for normal use.  strace only continues with a signal if the
381          * stopping signal is not SIGTRAP.  -brl
382          */
383         if (current->exit_code) {
384                 send_sig(current->exit_code, current, 1);
385                 current->exit_code = 0;
386         }
387 }