Skip to content

Commit 106174d

Browse files
author
Ley Foon Tan
committed
nios2: ptrace support
Add ptrace support for nios2. Signed-off-by: Ley Foon Tan <lftan@altera.com>
1 parent 42381bf commit 106174d

3 files changed

Lines changed: 319 additions & 0 deletions

File tree

‎arch/nios2/include/asm/ptrace.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2013 Altera Corporation
3+
* Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
4+
* Copyright (C) 2004 Microtronix Datacom Ltd
5+
*
6+
* based on m68k asm/processor.h
7+
*
8+
* This file is subject to the terms and conditions of the GNU General Public
9+
* License. See the file "COPYING" in the main directory of this archive
10+
* for more details.
11+
*/
12+
13+
#ifndef _ASM_NIOS2_PTRACE_H
14+
#define _ASM_NIOS2_PTRACE_H
15+
16+
#include <uapi/asm/ptrace.h>
17+
18+
#ifndef __ASSEMBLY__
19+
#define user_mode(regs) (((regs)->estatus & ESTATUS_EU))
20+
21+
#define instruction_pointer(regs) ((regs)->ra)
22+
#define profile_pc(regs) instruction_pointer(regs)
23+
#define user_stack_pointer(regs) ((regs)->sp)
24+
extern void show_regs(struct pt_regs *);
25+
26+
#define current_pt_regs() \
27+
((struct pt_regs *)((unsigned long)current_thread_info() + THREAD_SIZE)\
28+
- 1)
29+
30+
int do_syscall_trace_enter(void);
31+
void do_syscall_trace_exit(void);
32+
#endif /* __ASSEMBLY__ */
33+
#endif /* _ASM_NIOS2_PTRACE_H */
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
3+
* Copyright (C) 2004 Microtronix Datacom Ltd
4+
*
5+
* based on m68k asm/processor.h
6+
*
7+
* This file is subject to the terms and conditions of the GNU General Public
8+
* License. See the file "COPYING" in the main directory of this archive
9+
* for more details.
10+
*/
11+
12+
#ifndef _UAPI_ASM_NIOS2_PTRACE_H
13+
#define _UAPI_ASM_NIOS2_PTRACE_H
14+
15+
#ifndef __ASSEMBLY__
16+
17+
/*
18+
* Register numbers used by 'ptrace' system call interface.
19+
*/
20+
21+
/* GP registers */
22+
#define PTR_R0 0
23+
#define PTR_R1 1
24+
#define PTR_R2 2
25+
#define PTR_R3 3
26+
#define PTR_R4 4
27+
#define PTR_R5 5
28+
#define PTR_R6 6
29+
#define PTR_R7 7
30+
#define PTR_R8 8
31+
#define PTR_R9 9
32+
#define PTR_R10 10
33+
#define PTR_R11 11
34+
#define PTR_R12 12
35+
#define PTR_R13 13
36+
#define PTR_R14 14
37+
#define PTR_R15 15
38+
#define PTR_R16 16
39+
#define PTR_R17 17
40+
#define PTR_R18 18
41+
#define PTR_R19 19
42+
#define PTR_R20 20
43+
#define PTR_R21 21
44+
#define PTR_R22 22
45+
#define PTR_R23 23
46+
#define PTR_R24 24
47+
#define PTR_R25 25
48+
#define PTR_GP 26
49+
#define PTR_SP 27
50+
#define PTR_FP 28
51+
#define PTR_EA 29
52+
#define PTR_BA 30
53+
#define PTR_RA 31
54+
/* Control registers */
55+
#define PTR_PC 32
56+
#define PTR_STATUS 33
57+
#define PTR_ESTATUS 34
58+
#define PTR_BSTATUS 35
59+
#define PTR_IENABLE 36
60+
#define PTR_IPENDING 37
61+
#define PTR_CPUID 38
62+
#define PTR_CTL6 39
63+
#define PTR_CTL7 40
64+
#define PTR_PTEADDR 41
65+
#define PTR_TLBACC 42
66+
#define PTR_TLBMISC 43
67+
68+
#define NUM_PTRACE_REG (PTR_TLBMISC + 1)
69+
70+
/* this struct defines the way the registers are stored on the
71+
stack during a system call.
72+
73+
There is a fake_regs in setup.c that has to match pt_regs.*/
74+
75+
struct pt_regs {
76+
unsigned long r8; /* r8-r15 Caller-saved GP registers */
77+
unsigned long r9;
78+
unsigned long r10;
79+
unsigned long r11;
80+
unsigned long r12;
81+
unsigned long r13;
82+
unsigned long r14;
83+
unsigned long r15;
84+
unsigned long r1; /* Assembler temporary */
85+
unsigned long r2; /* Retval LS 32bits */
86+
unsigned long r3; /* Retval MS 32bits */
87+
unsigned long r4; /* r4-r7 Register arguments */
88+
unsigned long r5;
89+
unsigned long r6;
90+
unsigned long r7;
91+
unsigned long orig_r2; /* Copy of r2 ?? */
92+
unsigned long ra; /* Return address */
93+
unsigned long fp; /* Frame pointer */
94+
unsigned long sp; /* Stack pointer */
95+
unsigned long gp; /* Global pointer */
96+
unsigned long estatus;
97+
unsigned long ea; /* Exception return address (pc) */
98+
unsigned long orig_r7;
99+
};
100+
101+
/*
102+
* This is the extended stack used by signal handlers and the context
103+
* switcher: it's pushed after the normal "struct pt_regs".
104+
*/
105+
struct switch_stack {
106+
unsigned long r16; /* r16-r23 Callee-saved GP registers */
107+
unsigned long r17;
108+
unsigned long r18;
109+
unsigned long r19;
110+
unsigned long r20;
111+
unsigned long r21;
112+
unsigned long r22;
113+
unsigned long r23;
114+
unsigned long fp;
115+
unsigned long gp;
116+
unsigned long ra;
117+
};
118+
119+
#endif /* __ASSEMBLY__ */
120+
#endif /* _UAPI_ASM_NIOS2_PTRACE_H */

‎arch/nios2/kernel/ptrace.c‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (C) 2014 Altera Corporation
3+
* Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
4+
*
5+
* This file is subject to the terms and conditions of the GNU General
6+
* Public License. See the file COPYING in the main directory of this
7+
* archive for more details.
8+
*/
9+
10+
#include <linux/elf.h>
11+
#include <linux/errno.h>
12+
#include <linux/kernel.h>
13+
#include <linux/mm.h>
14+
#include <linux/ptrace.h>
15+
#include <linux/regset.h>
16+
#include <linux/sched.h>
17+
#include <linux/tracehook.h>
18+
#include <linux/uaccess.h>
19+
#include <linux/user.h>
20+
21+
static int genregs_get(struct task_struct *target,
22+
const struct user_regset *regset,
23+
unsigned int pos, unsigned int count,
24+
void *kbuf, void __user *ubuf)
25+
{
26+
const struct pt_regs *regs = task_pt_regs(target);
27+
const struct switch_stack *sw = (struct switch_stack *)regs - 1;
28+
int ret = 0;
29+
30+
#define REG_O_ZERO_RANGE(START, END) \
31+
if (!ret) \
32+
ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, \
33+
START * 4, (END * 4) + 4);
34+
35+
#define REG_O_ONE(PTR, LOC) \
36+
if (!ret) \
37+
ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, PTR, \
38+
LOC * 4, (LOC * 4) + 4);
39+
40+
#define REG_O_RANGE(PTR, START, END) \
41+
if (!ret) \
42+
ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, PTR, \
43+
START * 4, (END * 4) + 4);
44+
45+
REG_O_ZERO_RANGE(PTR_R0, PTR_R0);
46+
REG_O_RANGE(&regs->r1, PTR_R1, PTR_R7);
47+
REG_O_RANGE(&regs->r8, PTR_R8, PTR_R15);
48+
REG_O_RANGE(sw, PTR_R16, PTR_R23);
49+
REG_O_ZERO_RANGE(PTR_R24, PTR_R25); /* et and bt */
50+
REG_O_ONE(&regs->gp, PTR_GP);
51+
REG_O_ONE(&regs->sp, PTR_SP);
52+
REG_O_ONE(&regs->fp, PTR_FP);
53+
REG_O_ONE(&regs->ea, PTR_EA);
54+
REG_O_ZERO_RANGE(PTR_BA, PTR_BA);
55+
REG_O_ONE(&regs->ra, PTR_RA);
56+
REG_O_ONE(&regs->ea, PTR_PC); /* use ea for PC */
57+
if (!ret)
58+
ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
59+
PTR_STATUS * 4, -1);
60+
61+
return ret;
62+
}
63+
64+
/*
65+
* Set the thread state from a regset passed in via ptrace
66+
*/
67+
static int genregs_set(struct task_struct *target,
68+
const struct user_regset *regset,
69+
unsigned int pos, unsigned int count,
70+
const void *kbuf, const void __user *ubuf)
71+
{
72+
struct pt_regs *regs = task_pt_regs(target);
73+
const struct switch_stack *sw = (struct switch_stack *)regs - 1;
74+
int ret = 0;
75+
76+
#define REG_IGNORE_RANGE(START, END) \
77+
if (!ret) \
78+
ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
79+
START * 4, (END * 4) + 4);
80+
81+
#define REG_IN_ONE(PTR, LOC) \
82+
if (!ret) \
83+
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
84+
(void *)(PTR), LOC * 4, (LOC * 4) + 4);
85+
86+
#define REG_IN_RANGE(PTR, START, END) \
87+
if (!ret) \
88+
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
89+
(void *)(PTR), START * 4, (END * 4) + 4);
90+
91+
REG_IGNORE_RANGE(PTR_R0, PTR_R0);
92+
REG_IN_RANGE(&regs->r1, PTR_R1, PTR_R7);
93+
REG_IN_RANGE(&regs->r8, PTR_R8, PTR_R15);
94+
REG_IN_RANGE(sw, PTR_R16, PTR_R23);
95+
REG_IGNORE_RANGE(PTR_R24, PTR_R25); /* et and bt */
96+
REG_IN_ONE(&regs->gp, PTR_GP);
97+
REG_IN_ONE(&regs->sp, PTR_SP);
98+
REG_IN_ONE(&regs->fp, PTR_FP);
99+
REG_IN_ONE(&regs->ea, PTR_EA);
100+
REG_IGNORE_RANGE(PTR_BA, PTR_BA);
101+
REG_IN_ONE(&regs->ra, PTR_RA);
102+
REG_IN_ONE(&regs->ea, PTR_PC); /* use ea for PC */
103+
if (!ret)
104+
ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
105+
PTR_STATUS * 4, -1);
106+
107+
return ret;
108+
}
109+
110+
/*
111+
* Define the register sets available on Nios2 under Linux
112+
*/
113+
enum nios2_regset {
114+
REGSET_GENERAL,
115+
};
116+
117+
static const struct user_regset nios2_regsets[] = {
118+
[REGSET_GENERAL] = {
119+
.core_note_type = NT_PRSTATUS,
120+
.n = NUM_PTRACE_REG,
121+
.size = sizeof(unsigned long),
122+
.align = sizeof(unsigned long),
123+
.get = genregs_get,
124+
.set = genregs_set,
125+
}
126+
};
127+
128+
static const struct user_regset_view nios2_user_view = {
129+
.name = "nios2",
130+
.e_machine = ELF_ARCH,
131+
.ei_osabi = ELF_OSABI,
132+
.regsets = nios2_regsets,
133+
.n = ARRAY_SIZE(nios2_regsets)
134+
};
135+
136+
const struct user_regset_view *task_user_regset_view(struct task_struct *task)
137+
{
138+
return &nios2_user_view;
139+
}
140+
141+
void ptrace_disable(struct task_struct *child)
142+
{
143+
144+
}
145+
146+
long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
147+
unsigned long data)
148+
{
149+
return ptrace_request(child, request, addr, data);
150+
}
151+
152+
asmlinkage int do_syscall_trace_enter(void)
153+
{
154+
int ret = 0;
155+
156+
if (test_thread_flag(TIF_SYSCALL_TRACE))
157+
ret = tracehook_report_syscall_entry(task_pt_regs(current));
158+
159+
return ret;
160+
}
161+
162+
asmlinkage void do_syscall_trace_exit(void)
163+
{
164+
if (test_thread_flag(TIF_SYSCALL_TRACE))
165+
tracehook_report_syscall_exit(task_pt_regs(current), 0);
166+
}

0 commit comments

Comments
 (0)