Merge tag 'openrisc-for-linus' of git://github.com/openrisc/linux
[sfrench/cifs-2.6.git] / arch / openrisc / kernel / ptrace.c
1 /*
2  * OpenRISC ptrace.c
3  *
4  * Linux architectural port borrowing liberally from similar works of
5  * others.  All original copyrights apply as per the original source
6  * declaration.
7  *
8  * Modifications for the OpenRISC architecture:
9  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10  * Copyright (C) 2005 Gyorgy Jeney <nog@bsemi.com>
11  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12  *
13  *      This program is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU General Public License
15  *      as published by the Free Software Foundation; either version
16  *      2 of the License, or (at your option) any later version.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22
23 #include <linux/mm.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/audit.h>
27 #include <linux/regset.h>
28 #include <linux/tracehook.h>
29 #include <linux/elf.h>
30
31 #include <asm/thread_info.h>
32 #include <asm/segment.h>
33 #include <asm/page.h>
34 #include <asm/pgtable.h>
35
36 /*
37  * Copy the thread state to a regset that can be interpreted by userspace.
38  *
39  * It doesn't matter what our internal pt_regs structure looks like.  The
40  * important thing is that we export a consistent view of the thread state
41  * to userspace.  As such, we need to make sure that the regset remains
42  * ABI compatible as defined by the struct user_regs_struct:
43  *
44  * (Each item is a 32-bit word)
45  * r0 = 0 (exported for clarity)
46  * 31 GPRS r1-r31
47  * PC (Program counter)
48  * SR (Supervision register)
49  */
50 static int genregs_get(struct task_struct *target,
51                        const struct user_regset *regset,
52                        unsigned int pos, unsigned int count,
53                        void *kbuf, void __user * ubuf)
54 {
55         const struct pt_regs *regs = task_pt_regs(target);
56         int ret;
57
58         /* r0 */
59         ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, 0, 4);
60
61         if (!ret)
62                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
63                                           regs->gpr+1, 4, 4*32);
64         if (!ret)
65                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
66                                   &regs->pc, 4*32, 4*33);
67         if (!ret)
68                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
69                                           &regs->sr, 4*33, 4*34);
70         if (!ret)
71                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
72                                                4*34, -1);
73
74         return ret;
75 }
76
77 /*
78  * Set the thread state from a regset passed in via ptrace
79  */
80 static int genregs_set(struct task_struct *target,
81                        const struct user_regset *regset,
82                        unsigned int pos, unsigned int count,
83                        const void *kbuf, const void __user * ubuf)
84 {
85         struct pt_regs *regs = task_pt_regs(target);
86         int ret;
87
88         /* ignore r0 */
89         ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, 4);
90         /* r1 - r31 */
91         if (!ret)
92                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
93                                          regs->gpr+1, 4, 4*32);
94         /* PC */
95         if (!ret)
96                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
97                                  &regs->pc, 4*32, 4*33);
98         /*
99          * Skip SR and padding... userspace isn't allowed to changes bits in
100          * the Supervision register
101          */
102         if (!ret)
103                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
104                                                 4*33, -1);
105
106         return ret;
107 }
108
109 /*
110  * Define the register sets available on OpenRISC under Linux
111  */
112 enum or1k_regset {
113         REGSET_GENERAL,
114 };
115
116 static const struct user_regset or1k_regsets[] = {
117         [REGSET_GENERAL] = {
118                             .core_note_type = NT_PRSTATUS,
119                             .n = ELF_NGREG,
120                             .size = sizeof(long),
121                             .align = sizeof(long),
122                             .get = genregs_get,
123                             .set = genregs_set,
124                             },
125 };
126
127 static const struct user_regset_view user_or1k_native_view = {
128         .name = "or1k",
129         .e_machine = EM_OPENRISC,
130         .regsets = or1k_regsets,
131         .n = ARRAY_SIZE(or1k_regsets),
132 };
133
134 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
135 {
136         return &user_or1k_native_view;
137 }
138
139 /*
140  * does not yet catch signals sent when the child dies.
141  * in exit.c or in signal.c.
142  */
143
144
145 /*
146  * Called by kernel/ptrace.c when detaching..
147  *
148  * Make sure the single step bit is not set.
149  */
150 void ptrace_disable(struct task_struct *child)
151 {
152         pr_debug("ptrace_disable(): TODO\n");
153
154         user_disable_single_step(child);
155         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
156 }
157
158 long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
159                  unsigned long data)
160 {
161         int ret;
162
163         switch (request) {
164         default:
165                 ret = ptrace_request(child, request, addr, data);
166                 break;
167         }
168
169         return ret;
170 }
171
172 /*
173  * Notification of system call entry/exit
174  * - triggered by current->work.syscall_trace
175  */
176 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
177 {
178         long ret = 0;
179
180         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
181             tracehook_report_syscall_entry(regs))
182                 /*
183                  * Tracing decided this syscall should not happen.
184                  * We'll return a bogus call number to get an ENOSYS
185                  * error, but leave the original number in <something>.
186                  */
187                 ret = -1L;
188
189         audit_syscall_entry(regs->gpr[11], regs->gpr[3], regs->gpr[4],
190                             regs->gpr[5], regs->gpr[6]);
191
192         return ret ? : regs->gpr[11];
193 }
194
195 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
196 {
197         int step;
198
199         audit_syscall_exit(regs);
200
201         step = test_thread_flag(TIF_SINGLESTEP);
202         if (step || test_thread_flag(TIF_SYSCALL_TRACE))
203                 tracehook_report_syscall_exit(regs, step);
204 }