GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / score / kernel / ptrace.c
1 /*
2  * arch/score/kernel/ptrace.c
3  *
4  * Score Processor version.
5  *
6  * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
7  *  Chen Liqin <liqin.chen@sunplusct.com>
8  *  Lennox Wu <lennox.wu@sunplusct.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see the file COPYING, or write
22  * to the Free Software Foundation, Inc.,
23  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25
26 #include <linux/elf.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/ptrace.h>
30 #include <linux/regset.h>
31 #include <linux/sched/task_stack.h>
32
33 #include <linux/uaccess.h>
34
35 /*
36  * retrieve the contents of SCORE userspace general registers
37  */
38 static int genregs_get(struct task_struct *target,
39                        const struct user_regset *regset,
40                        unsigned int pos, unsigned int count,
41                        void *kbuf, void __user *ubuf)
42 {
43         const struct pt_regs *regs = task_pt_regs(target);
44         int ret;
45
46         /* skip 9 * sizeof(unsigned long) not use for pt_regs */
47         ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
48                                         0, offsetof(struct pt_regs, regs));
49
50         /* r0 - r31, cel, ceh, sr0, sr1, sr2, epc, ema, psr, ecr, condition */
51         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
52                                   regs->regs,
53                                   offsetof(struct pt_regs, regs),
54                                   offsetof(struct pt_regs, cp0_condition));
55
56         if (!ret)
57                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
58                                                 sizeof(struct pt_regs), -1);
59
60         return ret;
61 }
62
63 /*
64  * update the contents of the SCORE userspace general registers
65  */
66 static int genregs_set(struct task_struct *target,
67                        const struct user_regset *regset,
68                        unsigned int pos, unsigned int count,
69                        const void *kbuf, const void __user *ubuf)
70 {
71         struct pt_regs *regs = task_pt_regs(target);
72         int ret;
73
74         /* skip 9 * sizeof(unsigned long) */
75         ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
76                                         0, offsetof(struct pt_regs, regs));
77
78         /* r0 - r31, cel, ceh, sr0, sr1, sr2, epc, ema, psr, ecr, condition */
79         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
80                                   regs->regs,
81                                   offsetof(struct pt_regs, regs),
82                                   offsetof(struct pt_regs, cp0_condition));
83
84         if (!ret)
85                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
86                                                 sizeof(struct pt_regs), -1);
87
88         return ret;
89 }
90
91 /*
92  * Define the register sets available on the score7 under Linux
93  */
94 enum score7_regset {
95         REGSET_GENERAL,
96 };
97
98 static const struct user_regset score7_regsets[] = {
99         [REGSET_GENERAL] = {
100                 .core_note_type = NT_PRSTATUS,
101                 .n              = ELF_NGREG,
102                 .size           = sizeof(long),
103                 .align          = sizeof(long),
104                 .get            = genregs_get,
105                 .set            = genregs_set,
106         },
107 };
108
109 static const struct user_regset_view user_score_native_view = {
110         .name           = "score7",
111         .e_machine      = EM_SCORE7,
112         .regsets        = score7_regsets,
113         .n              = ARRAY_SIZE(score7_regsets),
114 };
115
116 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
117 {
118         return &user_score_native_view;
119 }
120
121 static int is_16bitinsn(unsigned long insn)
122 {
123         if ((insn & INSN32_MASK) == INSN32_MASK)
124                 return 0;
125         else
126                 return 1;
127 }
128
129 int
130 read_tsk_long(struct task_struct *child,
131                 unsigned long addr, unsigned long *res)
132 {
133         int copied;
134
135         copied = access_process_vm(child, addr, res, sizeof(*res), FOLL_FORCE);
136
137         return copied != sizeof(*res) ? -EIO : 0;
138 }
139
140 int
141 read_tsk_short(struct task_struct *child,
142                 unsigned long addr, unsigned short *res)
143 {
144         int copied;
145
146         copied = access_process_vm(child, addr, res, sizeof(*res), FOLL_FORCE);
147
148         return copied != sizeof(*res) ? -EIO : 0;
149 }
150
151 static int
152 write_tsk_short(struct task_struct *child,
153                 unsigned long addr, unsigned short val)
154 {
155         int copied;
156
157         copied = access_process_vm(child, addr, &val, sizeof(val),
158                         FOLL_FORCE | FOLL_WRITE);
159
160         return copied != sizeof(val) ? -EIO : 0;
161 }
162
163 static int
164 write_tsk_long(struct task_struct *child,
165                 unsigned long addr, unsigned long val)
166 {
167         int copied;
168
169         copied = access_process_vm(child, addr, &val, sizeof(val),
170                         FOLL_FORCE | FOLL_WRITE);
171
172         return copied != sizeof(val) ? -EIO : 0;
173 }
174
175 void user_enable_single_step(struct task_struct *child)
176 {
177         /* far_epc is the target of branch */
178         unsigned int epc, far_epc = 0;
179         unsigned long epc_insn, far_epc_insn;
180         int ninsn_type;                 /* next insn type 0=16b, 1=32b */
181         unsigned int tmp, tmp2;
182         struct pt_regs *regs = task_pt_regs(child);
183         child->thread.single_step = 1;
184         child->thread.ss_nextcnt = 1;
185         epc = regs->cp0_epc;
186
187         read_tsk_long(child, epc, &epc_insn);
188
189         if (is_16bitinsn(epc_insn)) {
190                 if ((epc_insn & J16M) == J16) {
191                         tmp = epc_insn & 0xFFE;
192                         epc = (epc & 0xFFFFF000) | tmp;
193                 } else if ((epc_insn & B16M) == B16) {
194                         child->thread.ss_nextcnt = 2;
195                         tmp = (epc_insn & 0xFF) << 1;
196                         tmp = tmp << 23;
197                         tmp = (unsigned int)((int) tmp >> 23);
198                         far_epc = epc + tmp;
199                         epc += 2;
200                 } else if ((epc_insn & BR16M) == BR16) {
201                         child->thread.ss_nextcnt = 2;
202                         tmp = (epc_insn >> 4) & 0xF;
203                         far_epc = regs->regs[tmp];
204                         epc += 2;
205                 } else
206                         epc += 2;
207         } else {
208                 if ((epc_insn & J32M) == J32) {
209                         tmp = epc_insn & 0x03FFFFFE;
210                         tmp2 = tmp & 0x7FFF;
211                         tmp = (((tmp >> 16) & 0x3FF) << 15) | tmp2;
212                         epc = (epc & 0xFFC00000) | tmp;
213                 } else if ((epc_insn & B32M) == B32) {
214                         child->thread.ss_nextcnt = 2;
215                         tmp = epc_insn & 0x03FFFFFE;    /* discard LK bit */
216                         tmp2 = tmp & 0x3FF;
217                         tmp = (((tmp >> 16) & 0x3FF) << 10) | tmp2; /* 20bit */
218                         tmp = tmp << 12;
219                         tmp = (unsigned int)((int) tmp >> 12);
220                         far_epc = epc + tmp;
221                         epc += 4;
222                 } else if ((epc_insn & BR32M) == BR32) {
223                         child->thread.ss_nextcnt = 2;
224                         tmp = (epc_insn >> 16) & 0x1F;
225                         far_epc = regs->regs[tmp];
226                         epc += 4;
227                 } else
228                         epc += 4;
229         }
230
231         if (child->thread.ss_nextcnt == 1) {
232                 read_tsk_long(child, epc, &epc_insn);
233
234                 if (is_16bitinsn(epc_insn)) {
235                         write_tsk_short(child, epc, SINGLESTEP16_INSN);
236                         ninsn_type = 0;
237                 } else {
238                         write_tsk_long(child, epc, SINGLESTEP32_INSN);
239                         ninsn_type = 1;
240                 }
241
242                 if (ninsn_type == 0) {  /* 16bits */
243                         child->thread.insn1_type = 0;
244                         child->thread.addr1 = epc;
245                          /* the insn may have 32bit data */
246                         child->thread.insn1 = (short)epc_insn;
247                 } else {
248                         child->thread.insn1_type = 1;
249                         child->thread.addr1 = epc;
250                         child->thread.insn1 = epc_insn;
251                 }
252         } else {
253                 /* branch! have two target child->thread.ss_nextcnt=2 */
254                 read_tsk_long(child, epc, &epc_insn);
255                 read_tsk_long(child, far_epc, &far_epc_insn);
256                 if (is_16bitinsn(epc_insn)) {
257                         write_tsk_short(child, epc, SINGLESTEP16_INSN);
258                         ninsn_type = 0;
259                 } else {
260                         write_tsk_long(child, epc, SINGLESTEP32_INSN);
261                         ninsn_type = 1;
262                 }
263
264                 if (ninsn_type == 0) {  /* 16bits */
265                         child->thread.insn1_type = 0;
266                         child->thread.addr1 = epc;
267                          /* the insn may have 32bit data */
268                         child->thread.insn1 = (short)epc_insn;
269                 } else {
270                         child->thread.insn1_type = 1;
271                         child->thread.addr1 = epc;
272                         child->thread.insn1 = epc_insn;
273                 }
274
275                 if (is_16bitinsn(far_epc_insn)) {
276                         write_tsk_short(child, far_epc, SINGLESTEP16_INSN);
277                         ninsn_type = 0;
278                 } else {
279                         write_tsk_long(child, far_epc, SINGLESTEP32_INSN);
280                         ninsn_type = 1;
281                 }
282
283                 if (ninsn_type == 0) {  /* 16bits */
284                         child->thread.insn2_type = 0;
285                         child->thread.addr2 = far_epc;
286                          /* the insn may have 32bit data */
287                         child->thread.insn2 = (short)far_epc_insn;
288                 } else {
289                         child->thread.insn2_type = 1;
290                         child->thread.addr2 = far_epc;
291                         child->thread.insn2 = far_epc_insn;
292                 }
293         }
294 }
295
296 void user_disable_single_step(struct task_struct *child)
297 {
298         if (child->thread.insn1_type == 0)
299                 write_tsk_short(child, child->thread.addr1,
300                                 child->thread.insn1);
301
302         if (child->thread.insn1_type == 1)
303                 write_tsk_long(child, child->thread.addr1,
304                                 child->thread.insn1);
305
306         if (child->thread.ss_nextcnt == 2) {    /* branch */
307                 if (child->thread.insn1_type == 0)
308                         write_tsk_short(child, child->thread.addr1,
309                                         child->thread.insn1);
310                 if (child->thread.insn1_type == 1)
311                         write_tsk_long(child, child->thread.addr1,
312                                         child->thread.insn1);
313                 if (child->thread.insn2_type == 0)
314                         write_tsk_short(child, child->thread.addr2,
315                                         child->thread.insn2);
316                 if (child->thread.insn2_type == 1)
317                         write_tsk_long(child, child->thread.addr2,
318                                         child->thread.insn2);
319         }
320
321         child->thread.single_step = 0;
322         child->thread.ss_nextcnt = 0;
323 }
324
325 void ptrace_disable(struct task_struct *child)
326 {
327         user_disable_single_step(child);
328 }
329
330 long
331 arch_ptrace(struct task_struct *child, long request,
332             unsigned long addr, unsigned long data)
333 {
334         int ret;
335         unsigned long __user *datap = (void __user *)data;
336
337         switch (request) {
338         case PTRACE_GETREGS:
339                 ret = copy_regset_to_user(child, &user_score_native_view,
340                                                 REGSET_GENERAL,
341                                                 0, sizeof(struct pt_regs),
342                                                 datap);
343                 break;
344
345         case PTRACE_SETREGS:
346                 ret = copy_regset_from_user(child, &user_score_native_view,
347                                                 REGSET_GENERAL,
348                                                 0, sizeof(struct pt_regs),
349                                                 datap);
350                 break;
351
352         default:
353                 ret = ptrace_request(child, request, addr, data);
354                 break;
355         }
356
357         return ret;
358 }
359
360 /*
361  * Notification of system call entry/exit
362  * - triggered by current->work.syscall_trace
363  */
364 asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
365 {
366         if (!(current->ptrace & PT_PTRACED))
367                 return;
368
369         if (!test_thread_flag(TIF_SYSCALL_TRACE))
370                 return;
371
372         /* The 0x80 provides a way for the tracing parent to distinguish
373            between a syscall stop and SIGTRAP delivery. */
374         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
375                         0x80 : 0));
376
377         /*
378          * this isn't the same as continuing with a signal, but it will do
379          * for normal use.  strace only continues with a signal if the
380          * stopping signal is not SIGTRAP.  -brl
381          */
382         if (current->exit_code) {
383                 send_sig(current->exit_code, current, 1);
384                 current->exit_code = 0;
385         }
386 }