1106174d0SLey Foon Tan /*
2106174d0SLey Foon Tan * Copyright (C) 2014 Altera Corporation
3106174d0SLey Foon Tan * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
4106174d0SLey Foon Tan *
5106174d0SLey Foon Tan * This file is subject to the terms and conditions of the GNU General
6106174d0SLey Foon Tan * Public License. See the file COPYING in the main directory of this
7106174d0SLey Foon Tan * archive for more details.
8106174d0SLey Foon Tan */
9106174d0SLey Foon Tan
10106174d0SLey Foon Tan #include <linux/elf.h>
11106174d0SLey Foon Tan #include <linux/errno.h>
12106174d0SLey Foon Tan #include <linux/kernel.h>
13106174d0SLey Foon Tan #include <linux/mm.h>
14106174d0SLey Foon Tan #include <linux/ptrace.h>
15106174d0SLey Foon Tan #include <linux/regset.h>
16106174d0SLey Foon Tan #include <linux/sched.h>
1768db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
18106174d0SLey Foon Tan #include <linux/uaccess.h>
19106174d0SLey Foon Tan #include <linux/user.h>
20106174d0SLey Foon Tan
genregs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)21106174d0SLey Foon Tan static int genregs_get(struct task_struct *target,
22106174d0SLey Foon Tan const struct user_regset *regset,
236bda7688SAl Viro struct membuf to)
24106174d0SLey Foon Tan {
25106174d0SLey Foon Tan const struct pt_regs *regs = task_pt_regs(target);
26106174d0SLey Foon Tan const struct switch_stack *sw = (struct switch_stack *)regs - 1;
27106174d0SLey Foon Tan
286bda7688SAl Viro membuf_zero(&to, 4); // R0
296bda7688SAl Viro membuf_write(&to, ®s->r1, 7 * 4); // R1..R7
306bda7688SAl Viro membuf_write(&to, ®s->r8, 8 * 4); // R8..R15
316bda7688SAl Viro membuf_write(&to, sw, 8 * 4); // R16..R23
326bda7688SAl Viro membuf_zero(&to, 2 * 4); /* et and bt */
336bda7688SAl Viro membuf_store(&to, regs->gp);
346bda7688SAl Viro membuf_store(&to, regs->sp);
356bda7688SAl Viro membuf_store(&to, regs->fp);
366bda7688SAl Viro membuf_store(&to, regs->ea);
376bda7688SAl Viro membuf_zero(&to, 4); // PTR_BA
386bda7688SAl Viro membuf_store(&to, regs->ra);
396bda7688SAl Viro membuf_store(&to, regs->ea); /* use ea for PC */
406bda7688SAl Viro return membuf_zero(&to, (NUM_PTRACE_REG - PTR_PC) * 4);
41106174d0SLey Foon Tan }
42106174d0SLey Foon Tan
43106174d0SLey Foon Tan /*
44106174d0SLey Foon Tan * Set the thread state from a regset passed in via ptrace
45106174d0SLey Foon Tan */
genregs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)46106174d0SLey Foon Tan static int genregs_set(struct task_struct *target,
47106174d0SLey Foon Tan const struct user_regset *regset,
48106174d0SLey Foon Tan unsigned int pos, unsigned int count,
49106174d0SLey Foon Tan const void *kbuf, const void __user *ubuf)
50106174d0SLey Foon Tan {
51106174d0SLey Foon Tan struct pt_regs *regs = task_pt_regs(target);
52106174d0SLey Foon Tan const struct switch_stack *sw = (struct switch_stack *)regs - 1;
53106174d0SLey Foon Tan int ret = 0;
54106174d0SLey Foon Tan
55106174d0SLey Foon Tan #define REG_IGNORE_RANGE(START, END) \
56106174d0SLey Foon Tan if (!ret) \
57*ae27da72SSergey Shtylyov user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
58106174d0SLey Foon Tan START * 4, (END * 4) + 4);
59106174d0SLey Foon Tan
60106174d0SLey Foon Tan #define REG_IN_ONE(PTR, LOC) \
61106174d0SLey Foon Tan if (!ret) \
62106174d0SLey Foon Tan ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
63106174d0SLey Foon Tan (void *)(PTR), LOC * 4, (LOC * 4) + 4);
64106174d0SLey Foon Tan
65106174d0SLey Foon Tan #define REG_IN_RANGE(PTR, START, END) \
66106174d0SLey Foon Tan if (!ret) \
67106174d0SLey Foon Tan ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
68106174d0SLey Foon Tan (void *)(PTR), START * 4, (END * 4) + 4);
69106174d0SLey Foon Tan
70106174d0SLey Foon Tan REG_IGNORE_RANGE(PTR_R0, PTR_R0);
71106174d0SLey Foon Tan REG_IN_RANGE(®s->r1, PTR_R1, PTR_R7);
72106174d0SLey Foon Tan REG_IN_RANGE(®s->r8, PTR_R8, PTR_R15);
73106174d0SLey Foon Tan REG_IN_RANGE(sw, PTR_R16, PTR_R23);
74106174d0SLey Foon Tan REG_IGNORE_RANGE(PTR_R24, PTR_R25); /* et and bt */
75106174d0SLey Foon Tan REG_IN_ONE(®s->gp, PTR_GP);
76106174d0SLey Foon Tan REG_IN_ONE(®s->sp, PTR_SP);
77106174d0SLey Foon Tan REG_IN_ONE(®s->fp, PTR_FP);
78106174d0SLey Foon Tan REG_IN_ONE(®s->ea, PTR_EA);
79106174d0SLey Foon Tan REG_IGNORE_RANGE(PTR_BA, PTR_BA);
80106174d0SLey Foon Tan REG_IN_ONE(®s->ra, PTR_RA);
81106174d0SLey Foon Tan REG_IN_ONE(®s->ea, PTR_PC); /* use ea for PC */
82106174d0SLey Foon Tan if (!ret)
83*ae27da72SSergey Shtylyov user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
84106174d0SLey Foon Tan PTR_STATUS * 4, -1);
85106174d0SLey Foon Tan
86106174d0SLey Foon Tan return ret;
87106174d0SLey Foon Tan }
88106174d0SLey Foon Tan
89106174d0SLey Foon Tan /*
90106174d0SLey Foon Tan * Define the register sets available on Nios2 under Linux
91106174d0SLey Foon Tan */
92106174d0SLey Foon Tan enum nios2_regset {
93106174d0SLey Foon Tan REGSET_GENERAL,
94106174d0SLey Foon Tan };
95106174d0SLey Foon Tan
96106174d0SLey Foon Tan static const struct user_regset nios2_regsets[] = {
97106174d0SLey Foon Tan [REGSET_GENERAL] = {
98106174d0SLey Foon Tan .core_note_type = NT_PRSTATUS,
99106174d0SLey Foon Tan .n = NUM_PTRACE_REG,
100106174d0SLey Foon Tan .size = sizeof(unsigned long),
101106174d0SLey Foon Tan .align = sizeof(unsigned long),
1026bda7688SAl Viro .regset_get = genregs_get,
103106174d0SLey Foon Tan .set = genregs_set,
104106174d0SLey Foon Tan }
105106174d0SLey Foon Tan };
106106174d0SLey Foon Tan
107106174d0SLey Foon Tan static const struct user_regset_view nios2_user_view = {
108106174d0SLey Foon Tan .name = "nios2",
109106174d0SLey Foon Tan .e_machine = ELF_ARCH,
110106174d0SLey Foon Tan .ei_osabi = ELF_OSABI,
111106174d0SLey Foon Tan .regsets = nios2_regsets,
112106174d0SLey Foon Tan .n = ARRAY_SIZE(nios2_regsets)
113106174d0SLey Foon Tan };
114106174d0SLey Foon Tan
task_user_regset_view(struct task_struct * task)115106174d0SLey Foon Tan const struct user_regset_view *task_user_regset_view(struct task_struct *task)
116106174d0SLey Foon Tan {
117106174d0SLey Foon Tan return &nios2_user_view;
118106174d0SLey Foon Tan }
119106174d0SLey Foon Tan
ptrace_disable(struct task_struct * child)120106174d0SLey Foon Tan void ptrace_disable(struct task_struct *child)
121106174d0SLey Foon Tan {
122106174d0SLey Foon Tan
123106174d0SLey Foon Tan }
124106174d0SLey Foon Tan
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)125106174d0SLey Foon Tan long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
126106174d0SLey Foon Tan unsigned long data)
127106174d0SLey Foon Tan {
128106174d0SLey Foon Tan return ptrace_request(child, request, addr, data);
129106174d0SLey Foon Tan }
130106174d0SLey Foon Tan
do_syscall_trace_enter(void)131106174d0SLey Foon Tan asmlinkage int do_syscall_trace_enter(void)
132106174d0SLey Foon Tan {
133106174d0SLey Foon Tan int ret = 0;
134106174d0SLey Foon Tan
135106174d0SLey Foon Tan if (test_thread_flag(TIF_SYSCALL_TRACE))
136153474baSEric W. Biederman ret = ptrace_report_syscall_entry(task_pt_regs(current));
137106174d0SLey Foon Tan
138106174d0SLey Foon Tan return ret;
139106174d0SLey Foon Tan }
140106174d0SLey Foon Tan
do_syscall_trace_exit(void)141106174d0SLey Foon Tan asmlinkage void do_syscall_trace_exit(void)
142106174d0SLey Foon Tan {
143106174d0SLey Foon Tan if (test_thread_flag(TIF_SYSCALL_TRACE))
144153474baSEric W. Biederman ptrace_report_syscall_exit(task_pt_regs(current), 0);
145106174d0SLey Foon Tan }
146