• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 2005-2012 Imagination Technologies Ltd.
3  *
4  * This file is subject to the terms and conditions of the GNU General
5  * Public License.  See the file COPYING in the main directory of
6  * this archive for more details.
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/mm.h>
11 #include <linux/errno.h>
12 #include <linux/ptrace.h>
13 #include <linux/user.h>
14 #include <linux/regset.h>
15 #include <linux/tracehook.h>
16 #include <linux/elf.h>
17 #include <linux/uaccess.h>
18 #include <trace/syscall.h>
19 
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/syscalls.h>
22 
23 /*
24  * user_regset definitions.
25  */
26 
user_txstatus(const struct pt_regs * regs)27 static unsigned long user_txstatus(const struct pt_regs *regs)
28 {
29 	unsigned long data = (unsigned long)regs->ctx.Flags;
30 
31 	if (regs->ctx.SaveMask & TBICTX_CBUF_BIT)
32 		data |= USER_GP_REGS_STATUS_CATCH_BIT;
33 
34 	return data;
35 }
36 
metag_gp_regs_copyout(const struct pt_regs * regs,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)37 int metag_gp_regs_copyout(const struct pt_regs *regs,
38 			  unsigned int pos, unsigned int count,
39 			  void *kbuf, void __user *ubuf)
40 {
41 	const void *ptr;
42 	unsigned long data;
43 	int ret;
44 
45 	/* D{0-1}.{0-7} */
46 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
47 				  regs->ctx.DX, 0, 4*16);
48 	if (ret)
49 		goto out;
50 	/* A{0-1}.{0-1} */
51 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
52 				  regs->ctx.AX, 4*16, 4*20);
53 	if (ret)
54 		goto out;
55 	/* A{0-1}.2 */
56 	if (regs->ctx.SaveMask & TBICTX_XEXT_BIT)
57 		ptr = regs->ctx.Ext.Ctx.pExt;
58 	else
59 		ptr = &regs->ctx.Ext.AX2;
60 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
61 				  ptr, 4*20, 4*22);
62 	if (ret)
63 		goto out;
64 	/* A{0-1}.3 */
65 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
66 				  &regs->ctx.AX3, 4*22, 4*24);
67 	if (ret)
68 		goto out;
69 	/* PC */
70 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
71 				  &regs->ctx.CurrPC, 4*24, 4*25);
72 	if (ret)
73 		goto out;
74 	/* TXSTATUS */
75 	data = user_txstatus(regs);
76 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
77 				  &data, 4*25, 4*26);
78 	if (ret)
79 		goto out;
80 	/* TXRPT, TXBPOBITS, TXMODE */
81 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
82 				  &regs->ctx.CurrRPT, 4*26, 4*29);
83 	if (ret)
84 		goto out;
85 	/* Padding */
86 	ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
87 				       4*29, 4*30);
88 out:
89 	return ret;
90 }
91 
metag_gp_regs_copyin(struct pt_regs * regs,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)92 int metag_gp_regs_copyin(struct pt_regs *regs,
93 			 unsigned int pos, unsigned int count,
94 			 const void *kbuf, const void __user *ubuf)
95 {
96 	void *ptr;
97 	unsigned long data;
98 	int ret;
99 
100 	/* D{0-1}.{0-7} */
101 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
102 				 regs->ctx.DX, 0, 4*16);
103 	if (ret)
104 		goto out;
105 	/* A{0-1}.{0-1} */
106 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
107 				 regs->ctx.AX, 4*16, 4*20);
108 	if (ret)
109 		goto out;
110 	/* A{0-1}.2 */
111 	if (regs->ctx.SaveMask & TBICTX_XEXT_BIT)
112 		ptr = regs->ctx.Ext.Ctx.pExt;
113 	else
114 		ptr = &regs->ctx.Ext.AX2;
115 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
116 				 ptr, 4*20, 4*22);
117 	if (ret)
118 		goto out;
119 	/* A{0-1}.3 */
120 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
121 				 &regs->ctx.AX3, 4*22, 4*24);
122 	if (ret)
123 		goto out;
124 	/* PC */
125 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
126 				 &regs->ctx.CurrPC, 4*24, 4*25);
127 	if (ret)
128 		goto out;
129 	/* TXSTATUS */
130 	data = user_txstatus(regs);
131 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
132 				 &data, 4*25, 4*26);
133 	if (ret)
134 		goto out;
135 	regs->ctx.Flags = data & 0xffff;
136 	if (data & USER_GP_REGS_STATUS_CATCH_BIT)
137 		regs->ctx.SaveMask |= TBICTX_XCBF_BIT | TBICTX_CBUF_BIT;
138 	else
139 		regs->ctx.SaveMask &= ~TBICTX_CBUF_BIT;
140 	/* TXRPT, TXBPOBITS, TXMODE */
141 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
142 				 &regs->ctx.CurrRPT, 4*26, 4*29);
143 out:
144 	return ret;
145 }
146 
metag_gp_regs_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)147 static int metag_gp_regs_get(struct task_struct *target,
148 			     const struct user_regset *regset,
149 			     unsigned int pos, unsigned int count,
150 			     void *kbuf, void __user *ubuf)
151 {
152 	const struct pt_regs *regs = task_pt_regs(target);
153 	return metag_gp_regs_copyout(regs, pos, count, kbuf, ubuf);
154 }
155 
metag_gp_regs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)156 static int metag_gp_regs_set(struct task_struct *target,
157 			     const struct user_regset *regset,
158 			     unsigned int pos, unsigned int count,
159 			     const void *kbuf, const void __user *ubuf)
160 {
161 	struct pt_regs *regs = task_pt_regs(target);
162 	return metag_gp_regs_copyin(regs, pos, count, kbuf, ubuf);
163 }
164 
metag_cb_regs_copyout(const struct pt_regs * regs,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)165 int metag_cb_regs_copyout(const struct pt_regs *regs,
166 			  unsigned int pos, unsigned int count,
167 			  void *kbuf, void __user *ubuf)
168 {
169 	int ret;
170 
171 	/* TXCATCH{0-3} */
172 	if (regs->ctx.SaveMask & TBICTX_XCBF_BIT)
173 		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
174 					  regs->extcb0, 0, 4*4);
175 	else
176 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
177 					       0, 4*4);
178 	return ret;
179 }
180 
metag_cb_regs_copyin(struct pt_regs * regs,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)181 int metag_cb_regs_copyin(struct pt_regs *regs,
182 			 unsigned int pos, unsigned int count,
183 			 const void *kbuf, const void __user *ubuf)
184 {
185 	int ret;
186 
187 	/* TXCATCH{0-3} */
188 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
189 				 regs->extcb0, 0, 4*4);
190 	return ret;
191 }
192 
metag_cb_regs_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)193 static int metag_cb_regs_get(struct task_struct *target,
194 			     const struct user_regset *regset,
195 			     unsigned int pos, unsigned int count,
196 			     void *kbuf, void __user *ubuf)
197 {
198 	const struct pt_regs *regs = task_pt_regs(target);
199 	return metag_cb_regs_copyout(regs, pos, count, kbuf, ubuf);
200 }
201 
metag_cb_regs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)202 static int metag_cb_regs_set(struct task_struct *target,
203 			     const struct user_regset *regset,
204 			     unsigned int pos, unsigned int count,
205 			     const void *kbuf, const void __user *ubuf)
206 {
207 	struct pt_regs *regs = task_pt_regs(target);
208 	return metag_cb_regs_copyin(regs, pos, count, kbuf, ubuf);
209 }
210 
metag_rp_state_copyout(const struct pt_regs * regs,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)211 int metag_rp_state_copyout(const struct pt_regs *regs,
212 			   unsigned int pos, unsigned int count,
213 			   void *kbuf, void __user *ubuf)
214 {
215 	unsigned long mask;
216 	u64 *ptr;
217 	int ret, i;
218 
219 	/* Empty read pipeline */
220 	if (!(regs->ctx.SaveMask & TBICTX_CBRP_BIT)) {
221 		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
222 					       0, 4*13);
223 		goto out;
224 	}
225 
226 	mask = (regs->ctx.CurrDIVTIME & TXDIVTIME_RPMASK_BITS) >>
227 		TXDIVTIME_RPMASK_S;
228 
229 	/* Read pipeline entries */
230 	ptr = (void *)&regs->extcb0[1];
231 	for (i = 0; i < 6; ++i, ++ptr) {
232 		if (mask & (1 << i))
233 			ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
234 						  ptr, 8*i, 8*(i + 1));
235 		else
236 			ret = user_regset_copyout_zero(&pos, &count, &kbuf,
237 						       &ubuf, 8*i, 8*(i + 1));
238 		if (ret)
239 			goto out;
240 	}
241 	/* Mask of entries */
242 	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
243 				  &mask, 4*12, 4*13);
244 out:
245 	return ret;
246 }
247 
metag_rp_state_copyin(struct pt_regs * regs,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)248 int metag_rp_state_copyin(struct pt_regs *regs,
249 			  unsigned int pos, unsigned int count,
250 			  const void *kbuf, const void __user *ubuf)
251 {
252 	struct user_rp_state rp;
253 	unsigned long long *ptr;
254 	int ret, i;
255 
256 	if (count < 4*13)
257 		return -EINVAL;
258 	/* Read the entire pipeline before making any changes */
259 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
260 				 &rp, 0, 4*13);
261 	if (ret)
262 		goto out;
263 
264 	/* Write pipeline entries */
265 	ptr = (void *)&regs->extcb0[1];
266 	for (i = 0; i < 6; ++i, ++ptr)
267 		if (rp.mask & (1 << i))
268 			*ptr = rp.entries[i];
269 
270 	/* Update RPMask in TXDIVTIME */
271 	regs->ctx.CurrDIVTIME &= ~TXDIVTIME_RPMASK_BITS;
272 	regs->ctx.CurrDIVTIME |= (rp.mask << TXDIVTIME_RPMASK_S)
273 				 & TXDIVTIME_RPMASK_BITS;
274 
275 	/* Set/clear flags to indicate catch/read pipeline state */
276 	if (rp.mask)
277 		regs->ctx.SaveMask |= TBICTX_XCBF_BIT | TBICTX_CBRP_BIT;
278 	else
279 		regs->ctx.SaveMask &= ~TBICTX_CBRP_BIT;
280 out:
281 	return ret;
282 }
283 
metag_rp_state_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)284 static int metag_rp_state_get(struct task_struct *target,
285 			      const struct user_regset *regset,
286 			      unsigned int pos, unsigned int count,
287 			      void *kbuf, void __user *ubuf)
288 {
289 	const struct pt_regs *regs = task_pt_regs(target);
290 	return metag_rp_state_copyout(regs, pos, count, kbuf, ubuf);
291 }
292 
metag_rp_state_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)293 static int metag_rp_state_set(struct task_struct *target,
294 			      const struct user_regset *regset,
295 			      unsigned int pos, unsigned int count,
296 			      const void *kbuf, const void __user *ubuf)
297 {
298 	struct pt_regs *regs = task_pt_regs(target);
299 	return metag_rp_state_copyin(regs, pos, count, kbuf, ubuf);
300 }
301 
metag_tls_get(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,void * kbuf,void __user * ubuf)302 static int metag_tls_get(struct task_struct *target,
303 			const struct user_regset *regset,
304 			unsigned int pos, unsigned int count,
305 			void *kbuf, void __user *ubuf)
306 {
307 	void __user *tls = target->thread.tls_ptr;
308 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
309 }
310 
metag_tls_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)311 static int metag_tls_set(struct task_struct *target,
312 			const struct user_regset *regset,
313 			unsigned int pos, unsigned int count,
314 			const void *kbuf, const void __user *ubuf)
315 {
316 	int ret;
317 	void __user *tls = target->thread.tls_ptr;
318 
319 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
320 	if (ret)
321 		return ret;
322 
323 	target->thread.tls_ptr = tls;
324 	return ret;
325 }
326 
327 enum metag_regset {
328 	REGSET_GENERAL,
329 	REGSET_CBUF,
330 	REGSET_READPIPE,
331 	REGSET_TLS,
332 };
333 
334 static const struct user_regset metag_regsets[] = {
335 	[REGSET_GENERAL] = {
336 		.core_note_type = NT_PRSTATUS,
337 		.n = ELF_NGREG,
338 		.size = sizeof(long),
339 		.align = sizeof(long long),
340 		.get = metag_gp_regs_get,
341 		.set = metag_gp_regs_set,
342 	},
343 	[REGSET_CBUF] = {
344 		.core_note_type = NT_METAG_CBUF,
345 		.n = sizeof(struct user_cb_regs) / sizeof(long),
346 		.size = sizeof(long),
347 		.align = sizeof(long long),
348 		.get = metag_cb_regs_get,
349 		.set = metag_cb_regs_set,
350 	},
351 	[REGSET_READPIPE] = {
352 		.core_note_type = NT_METAG_RPIPE,
353 		.n = sizeof(struct user_rp_state) / sizeof(long),
354 		.size = sizeof(long),
355 		.align = sizeof(long long),
356 		.get = metag_rp_state_get,
357 		.set = metag_rp_state_set,
358 	},
359 	[REGSET_TLS] = {
360 		.core_note_type = NT_METAG_TLS,
361 		.n = 1,
362 		.size = sizeof(void *),
363 		.align = sizeof(void *),
364 		.get = metag_tls_get,
365 		.set = metag_tls_set,
366 	},
367 };
368 
369 static const struct user_regset_view user_metag_view = {
370 	.name = "metag",
371 	.e_machine = EM_METAG,
372 	.regsets = metag_regsets,
373 	.n = ARRAY_SIZE(metag_regsets)
374 };
375 
task_user_regset_view(struct task_struct * task)376 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
377 {
378 	return &user_metag_view;
379 }
380 
381 /*
382  * Called by kernel/ptrace.c when detaching..
383  *
384  * Make sure single step bits etc are not set.
385  */
ptrace_disable(struct task_struct * child)386 void ptrace_disable(struct task_struct *child)
387 {
388 	/* nothing to do.. */
389 }
390 
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)391 long arch_ptrace(struct task_struct *child, long request, unsigned long addr,
392 		 unsigned long data)
393 {
394 	int ret;
395 
396 	switch (request) {
397 	default:
398 		ret = ptrace_request(child, request, addr, data);
399 		break;
400 	}
401 
402 	return ret;
403 }
404 
syscall_trace_enter(struct pt_regs * regs)405 int syscall_trace_enter(struct pt_regs *regs)
406 {
407 	int ret = 0;
408 
409 	if (test_thread_flag(TIF_SYSCALL_TRACE))
410 		ret = tracehook_report_syscall_entry(regs);
411 
412 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
413 		trace_sys_enter(regs, regs->ctx.DX[0].U1);
414 
415 	return ret ? -1 : regs->ctx.DX[0].U1;
416 }
417 
syscall_trace_leave(struct pt_regs * regs)418 void syscall_trace_leave(struct pt_regs *regs)
419 {
420 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
421 		trace_sys_exit(regs, regs->ctx.DX[0].U1);
422 
423 	if (test_thread_flag(TIF_SYSCALL_TRACE))
424 		tracehook_report_syscall_exit(regs, 0);
425 }
426