1 /*
2  *  linux/arch/h8300/kernel/ptrace.c
3  *
4  *  Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
5  *
6  * This file is subject to the terms and conditions of the GNU General
7  * Public License.  See the file COPYING in the main directory of
8  * this archive for more details.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/ptrace.h>
14 #include <linux/audit.h>
15 #include <linux/tracehook.h>
16 #include <linux/regset.h>
17 #include <linux/elf.h>
18 
19 #define CCR_MASK 0x6f    /* mode/imask not set */
20 #define EXR_MASK 0x80    /* modify only T */
21 
22 #define PT_REG(r) offsetof(struct pt_regs, r)
23 
24 extern void user_disable_single_step(struct task_struct *child);
25 
26 /* Mapping from PT_xxx to the stack offset at which the register is
27    saved.  Notice that usp has no stack-slot and needs to be treated
28    specially (see get_reg/put_reg below). */
29 static const int register_offset[] = {
30 	PT_REG(er1), PT_REG(er2), PT_REG(er3), PT_REG(er4),
31 	PT_REG(er5), PT_REG(er6), PT_REG(er0), -1,
32 	PT_REG(orig_er0), PT_REG(ccr), PT_REG(pc),
33 #if defined(CONFIG_CPU_H8S)
34 	PT_REG(exr),
35 #endif
36 };
37 
38 /* read register */
h8300_get_reg(struct task_struct * task,int regno)39 long h8300_get_reg(struct task_struct *task, int regno)
40 {
41 	switch (regno) {
42 	case PT_USP:
43 		return task->thread.usp + sizeof(long)*2;
44 	case PT_CCR:
45 	case PT_EXR:
46 	    return *(unsigned short *)(task->thread.esp0 +
47 				       register_offset[regno]);
48 	default:
49 	    return *(unsigned long *)(task->thread.esp0 +
50 				      register_offset[regno]);
51 	}
52 }
53 
h8300_put_reg(struct task_struct * task,int regno,unsigned long data)54 int h8300_put_reg(struct task_struct *task, int regno, unsigned long data)
55 {
56 	unsigned short oldccr;
57 	unsigned short oldexr;
58 
59 	switch (regno) {
60 	case PT_USP:
61 		task->thread.usp = data - sizeof(long)*2;
62 	case PT_CCR:
63 		oldccr = *(unsigned short *)(task->thread.esp0 +
64 					     register_offset[regno]);
65 		oldccr &= ~CCR_MASK;
66 		data &= CCR_MASK;
67 		data |= oldccr;
68 		*(unsigned short *)(task->thread.esp0 +
69 				    register_offset[regno]) = data;
70 		break;
71 	case PT_EXR:
72 		oldexr = *(unsigned short *)(task->thread.esp0 +
73 					     register_offset[regno]);
74 		oldccr &= ~EXR_MASK;
75 		data &= EXR_MASK;
76 		data |= oldexr;
77 		*(unsigned short *)(task->thread.esp0 +
78 				    register_offset[regno]) = data;
79 		break;
80 	default:
81 		*(unsigned long *)(task->thread.esp0 +
82 				   register_offset[regno]) = data;
83 		break;
84 	}
85 	return 0;
86 }
87 
regs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)88 static int regs_get(struct task_struct *target,
89 		    const struct user_regset *regset,
90 		    struct membuf to)
91 {
92 	int r;
93 
94 	BUILD_BUG_ON(sizeof(struct user_regs_struct) % sizeof(long) != 0);
95 	for (r = 0; r < ELF_NGREG; r++)
96 		membuf_store(&to, h8300_get_reg(target, r));
97 
98 	return 0;
99 }
100 
regs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)101 static int regs_set(struct task_struct *target,
102 		    const struct user_regset *regset,
103 		    unsigned int pos, unsigned int count,
104 		    const void *kbuf, const void __user *ubuf)
105 {
106 	int r;
107 	int ret;
108 	struct user_regs_struct regs;
109 	long *reg;
110 
111 	/* build user regs in buffer */
112 	BUILD_BUG_ON(sizeof(regs) % sizeof(long) != 0);
113 	for (reg = (long *)&regs, r = 0; r < sizeof(regs) / sizeof(long); r++)
114 		*reg++ = h8300_get_reg(target, r);
115 
116 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
117 				 &regs, 0, sizeof(regs));
118 	if (ret)
119 		return ret;
120 
121 	/* write back to pt_regs */
122 	for (reg = (long *)&regs, r = 0; r < sizeof(regs) / sizeof(long); r++)
123 		h8300_put_reg(target, r, *reg++);
124 	return 0;
125 }
126 
127 enum h8300_regset {
128 	REGSET_GENERAL,
129 };
130 
131 static const struct user_regset h8300_regsets[] = {
132 	[REGSET_GENERAL] = {
133 		.core_note_type	= NT_PRSTATUS,
134 		.n		= ELF_NGREG,
135 		.size		= sizeof(long),
136 		.align		= sizeof(long),
137 		.regset_get		= regs_get,
138 		.set		= regs_set,
139 	},
140 };
141 
142 static const struct user_regset_view user_h8300_native_view = {
143 	.name = "h8300",
144 	.e_machine = EM_H8_300,
145 	.regsets = h8300_regsets,
146 	.n = ARRAY_SIZE(h8300_regsets),
147 };
148 
task_user_regset_view(struct task_struct * task)149 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
150 {
151 	return &user_h8300_native_view;
152 }
153 
ptrace_disable(struct task_struct * child)154 void ptrace_disable(struct task_struct *child)
155 {
156 	user_disable_single_step(child);
157 }
158 
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)159 long arch_ptrace(struct task_struct *child, long request,
160 		 unsigned long addr, unsigned long data)
161 {
162 	int ret;
163 
164 	switch (request) {
165 	default:
166 		ret = ptrace_request(child, request, addr, data);
167 		break;
168 	}
169 	return ret;
170 }
171 
do_syscall_trace_enter(struct pt_regs * regs)172 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
173 {
174 	long ret = 0;
175 
176 	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
177 	    tracehook_report_syscall_entry(regs))
178 		/*
179 		 * Tracing decided this syscall should not happen.
180 		 * We'll return a bogus call number to get an ENOSYS
181 		 * error, but leave the original number in regs->regs[0].
182 		 */
183 		ret = -1L;
184 
185 	audit_syscall_entry(regs->er1, regs->er2, regs->er3,
186 			    regs->er4, regs->er5);
187 
188 	return ret ?: regs->er0;
189 }
190 
do_syscall_trace_leave(struct pt_regs * regs)191 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
192 {
193 	int step;
194 
195 	audit_syscall_exit(regs);
196 
197 	step = test_thread_flag(TIF_SINGLESTEP);
198 	if (step || test_thread_flag(TIF_SYSCALL_TRACE))
199 		tracehook_report_syscall_exit(regs, step);
200 }
201