Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / arch / hexagon / kernel / ptrace.c
1 /*
2  * Ptrace support for Hexagon
3  *
4  * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20
21 #include <generated/compile.h>
22
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/mm.h>
26 #include <linux/smp.h>
27 #include <linux/errno.h>
28 #include <linux/ptrace.h>
29 #include <linux/regset.h>
30 #include <linux/user.h>
31
32 #include <asm/user.h>
33
34 static int genregs_get(struct task_struct *target,
35                    const struct user_regset *regset,
36                    unsigned int pos, unsigned int count,
37                    void *kbuf, void __user *ubuf)
38 {
39         int ret;
40         unsigned int dummy;
41         struct pt_regs *regs = task_pt_regs(target);
42
43
44         if (!regs)
45                 return -EIO;
46
47         /* The general idea here is that the copyout must happen in
48          * exactly the same order in which the userspace expects these
49          * regs. Now, the sequence in userspace does not match the
50          * sequence in the kernel, so everything past the 32 gprs
51          * happens one at a time.
52          */
53         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
54                                   &regs->r00, 0, 32*sizeof(unsigned long));
55
56 #define ONEXT(KPT_REG, USR_REG) \
57         if (!ret) \
58                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, \
59                         KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
60                         offsetof(struct user_regs_struct, USR_REG) + \
61                                  sizeof(unsigned long));
62
63         /* Must be exactly same sequence as struct user_regs_struct */
64         ONEXT(&regs->sa0, sa0);
65         ONEXT(&regs->lc0, lc0);
66         ONEXT(&regs->sa1, sa1);
67         ONEXT(&regs->lc1, lc1);
68         ONEXT(&regs->m0, m0);
69         ONEXT(&regs->m1, m1);
70         ONEXT(&regs->usr, usr);
71         ONEXT(&regs->preds, p3_0);
72         ONEXT(&regs->gp, gp);
73         ONEXT(&regs->ugp, ugp);
74         ONEXT(&pt_elr(regs), pc);
75         dummy = pt_cause(regs);
76         ONEXT(&dummy, cause);
77         ONEXT(&pt_badva(regs), badva);
78
79         /* Pad the rest with zeros, if needed */
80         if (!ret)
81                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
82                                         offsetof(struct user_regs_struct, pad1), -1);
83         return ret;
84 }
85
86 static int genregs_set(struct task_struct *target,
87                    const struct user_regset *regset,
88                    unsigned int pos, unsigned int count,
89                    const void *kbuf, const void __user *ubuf)
90 {
91         int ret;
92         unsigned long bucket;
93         struct pt_regs *regs = task_pt_regs(target);
94
95         if (!regs)
96                 return -EIO;
97
98         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
99                                  &regs->r00, 0, 32*sizeof(unsigned long));
100
101 #define INEXT(KPT_REG, USR_REG) \
102         if (!ret) \
103                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
104                         KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
105                         offsetof(struct user_regs_struct, USR_REG) + \
106                                 sizeof(unsigned long));
107
108         /* Must be exactly same sequence as struct user_regs_struct */
109         INEXT(&regs->sa0, sa0);
110         INEXT(&regs->lc0, lc0);
111         INEXT(&regs->sa1, sa1);
112         INEXT(&regs->lc1, lc1);
113         INEXT(&regs->m0, m0);
114         INEXT(&regs->m1, m1);
115         INEXT(&regs->usr, usr);
116         INEXT(&regs->preds, p3_0);
117         INEXT(&regs->gp, gp);
118         INEXT(&regs->ugp, ugp);
119         INEXT(&pt_elr(regs), pc);
120
121         /* CAUSE and BADVA aren't writeable. */
122         INEXT(&bucket, cause);
123         INEXT(&bucket, badva);
124
125         /* Ignore the rest, if needed */
126         if (!ret)
127                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
128                                         offsetof(struct user_regs_struct, pad1), -1);
129
130         if (ret)
131                 return ret;
132
133         /*
134          * This is special; SP is actually restored by the VM via the
135          * special event record which is set by the special trap.
136          */
137         regs->hvmer.vmpsp = regs->r29;
138         return 0;
139 }
140
141 enum hexagon_regset {
142         REGSET_GENERAL,
143 };
144
145 static const struct user_regset hexagon_regsets[] = {
146         [REGSET_GENERAL] = {
147                 .core_note_type = NT_PRSTATUS,
148                 .n = ELF_NGREG,
149                 .size = sizeof(unsigned long),
150                 .align = sizeof(unsigned long),
151                 .get = genregs_get,
152                 .set = genregs_set,
153         },
154 };
155
156 static const struct user_regset_view hexagon_user_view = {
157         .name = UTS_MACHINE,
158         .e_machine = ELF_ARCH,
159         .ei_osabi = ELF_OSABI,
160         .regsets = hexagon_regsets,
161         .n = ARRAY_SIZE(hexagon_regsets)
162 };
163
164 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
165 {
166         return &hexagon_user_view;
167 }
168
169 void ptrace_disable(struct task_struct *child)
170 {
171         /* Boilerplate - resolves to null inline if no HW single-step */
172         user_disable_single_step(child);
173 }
174
175 long arch_ptrace(struct task_struct *child, long request,
176                  unsigned long addr, unsigned long data)
177 {
178         return ptrace_request(child, request, addr, data);
179 }