blob: badd286882ae6865c069f9ae6107e4e7708f45e4 [file] [log] [blame]
Michal Simek2b438452009-03-27 14:25:27 +01001/*
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>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010030#include <linux/sched/task_stack.h>
Michal Simek2b438452009-03-27 14:25:27 +010031#include <linux/ptrace.h>
32#include <linux/signal.h>
Michal Simek23575482009-08-24 13:26:04 +020033#include <linux/elf.h>
34#include <linux/audit.h>
35#include <linux/seccomp.h>
36#include <linux/tracehook.h>
Michal Simek2b438452009-03-27 14:25:27 +010037
38#include <linux/errno.h>
Michal Simek2b438452009-03-27 14:25:27 +010039#include <asm/processor.h>
40#include <linux/uaccess.h>
41#include <asm/asm-offsets.h>
Michal Simek6847ba92010-05-24 12:13:24 +020042#include <asm/cacheflush.h>
Michal Simekc1df53b2011-02-07 11:38:57 +010043#include <asm/syscall.h>
Michal Simek6bd55f02012-12-27 10:40:38 +010044#include <linux/io.h>
Michal Simek2b438452009-03-27 14:25:27 +010045
46/* Returns the address where the register at REG_OFFS in P is stashed away. */
47static microblaze_reg_t *reg_save_addr(unsigned reg_offs,
48 struct task_struct *t)
49{
50 struct pt_regs *regs;
51
52 /*
53 * Three basic cases:
54 *
55 * (1) A register normally saved before calling the scheduler, is
56 * available in the kernel entry pt_regs structure at the top
57 * of the kernel stack. The kernel trap/irq exit path takes
58 * care to save/restore almost all registers for ptrace'd
59 * processes.
60 *
61 * (2) A call-clobbered register, where the process P entered the
62 * kernel via [syscall] trap, is not stored anywhere; that's
63 * OK, because such registers are not expected to be preserved
64 * when the trap returns anyway (so we don't actually bother to
65 * test for this case).
66 *
67 * (3) A few registers not used at all by the kernel, and so
68 * normally never saved except by context-switches, are in the
69 * context switch state.
70 */
71
72 /* Register saved during kernel entry (or not available). */
73 regs = task_pt_regs(t);
74
75 return (microblaze_reg_t *)((char *)regs + reg_offs);
76}
77
Namhyung Kim9b05a692010-10-27 15:33:47 -070078long arch_ptrace(struct task_struct *child, long request,
79 unsigned long addr, unsigned long data)
Michal Simek2b438452009-03-27 14:25:27 +010080{
81 int rval;
82 unsigned long val = 0;
Michal Simek2b438452009-03-27 14:25:27 +010083
84 switch (request) {
Michal Simek2b438452009-03-27 14:25:27 +010085 /* Read/write the word at location ADDR in the registers. */
86 case PTRACE_PEEKUSR:
87 case PTRACE_POKEUSR:
88 pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr);
89 rval = 0;
90 if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
91 /*
92 * Special requests that don't actually correspond
93 * to offsets in struct pt_regs.
94 */
95 if (addr == PT_TEXT_ADDR) {
96 val = child->mm->start_code;
97 } else if (addr == PT_DATA_ADDR) {
98 val = child->mm->start_data;
99 } else if (addr == PT_TEXT_LEN) {
100 val = child->mm->end_code
101 - child->mm->start_code;
102 } else {
103 rval = -EIO;
104 }
Namhyung Kimcfd866f2010-10-27 15:33:57 -0700105 } else if (addr < PT_SIZE && (addr & 0x3) == 0) {
Michal Simek2b438452009-03-27 14:25:27 +0100106 microblaze_reg_t *reg_addr = reg_save_addr(addr, child);
107 if (request == PTRACE_PEEKUSR)
108 val = *reg_addr;
Michal Simek6847ba92010-05-24 12:13:24 +0200109 else {
110#if 1
Michal Simek2b438452009-03-27 14:25:27 +0100111 *reg_addr = data;
Michal Simek6847ba92010-05-24 12:13:24 +0200112#else
113 /* MS potential problem on WB system
114 * Be aware that reg_addr is virtual address
115 * virt_to_phys conversion is necessary.
116 * This could be sensible solution.
117 */
118 u32 paddr = virt_to_phys((u32)reg_addr);
119 invalidate_icache_range(paddr, paddr + 4);
120 *reg_addr = data;
121 flush_dcache_range(paddr, paddr + 4);
122#endif
123 }
Michal Simek2b438452009-03-27 14:25:27 +0100124 } else
125 rval = -EIO;
126
127 if (rval == 0 && request == PTRACE_PEEKUSR)
Michal Simekc1df53b2011-02-07 11:38:57 +0100128 rval = put_user(val, (unsigned long __user *)data);
Michal Simek2b438452009-03-27 14:25:27 +0100129 break;
Michal Simek2b438452009-03-27 14:25:27 +0100130 default:
Christoph Hellwigb3c1e012010-03-10 15:22:44 -0800131 rval = ptrace_request(child, request, addr, data);
Michal Simek2b438452009-03-27 14:25:27 +0100132 }
133 return rval;
134}
135
Michal Simek8543e6c2014-12-18 15:41:13 +0100136asmlinkage unsigned long do_syscall_trace_enter(struct pt_regs *regs)
Michal Simek23575482009-08-24 13:26:04 +0200137{
Michal Simek8543e6c2014-12-18 15:41:13 +0100138 unsigned long ret = 0;
Michal Simek23575482009-08-24 13:26:04 +0200139
Will Drewrye4da89d2012-04-17 14:48:57 -0500140 secure_computing_strict(regs->r12);
Michal Simek23575482009-08-24 13:26:04 +0200141
142 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
143 tracehook_report_syscall_entry(regs))
144 /*
145 * Tracing decided this syscall should not happen.
146 * We'll return a bogus call number to get an ENOSYS
147 * error, but leave the original number in regs->regs[0].
148 */
149 ret = -1L;
150
Eric Paris91397402014-03-11 13:29:28 -0400151 audit_syscall_entry(regs->r12, regs->r5, regs->r6, regs->r7, regs->r8);
Michal Simek23575482009-08-24 13:26:04 +0200152
153 return ret ?: regs->r12;
154}
155
156asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
157{
158 int step;
159
Eric Parisd7e75282012-01-03 14:23:06 -0500160 audit_syscall_exit(regs);
Michal Simek23575482009-08-24 13:26:04 +0200161
162 step = test_thread_flag(TIF_SINGLESTEP);
163 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
164 tracehook_report_syscall_exit(regs, step);
165}
166
Michal Simek2b438452009-03-27 14:25:27 +0100167void ptrace_disable(struct task_struct *child)
168{
169 /* nothing to do */
170}