xref: /linux/arch/riscv/kernel/ptrace.c (revision d30c1683aaecb93d2ab95685dc4300a33d3cea7a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2010 Tilera Corporation. All Rights Reserved.
4  * Copyright 2015 Regents of the University of California
5  * Copyright 2017 SiFive
6  *
7  * Copied from arch/tile/kernel/ptrace.c
8  */
9 
10 #include <asm/vector.h>
11 #include <asm/ptrace.h>
12 #include <asm/syscall.h>
13 #include <asm/thread_info.h>
14 #include <asm/switch_to.h>
15 #include <linux/audit.h>
16 #include <linux/compat.h>
17 #include <linux/ptrace.h>
18 #include <linux/elf.h>
19 #include <linux/regset.h>
20 #include <linux/sched.h>
21 #include <linux/sched/task_stack.h>
22 #include <asm/usercfi.h>
23 
24 enum riscv_regset {
25 	REGSET_X,
26 #ifdef CONFIG_FPU
27 	REGSET_F,
28 #endif
29 #ifdef CONFIG_RISCV_ISA_V
30 	REGSET_V,
31 #endif
32 #ifdef CONFIG_RISCV_ISA_SUPM
33 	REGSET_TAGGED_ADDR_CTRL,
34 #endif
35 #ifdef CONFIG_RISCV_USER_CFI
36 	REGSET_CFI,
37 #endif
38 };
39 
40 static int riscv_gpr_get(struct task_struct *target,
41 			 const struct user_regset *regset,
42 			 struct membuf to)
43 {
44 	return membuf_write(&to, task_pt_regs(target),
45 			    sizeof(struct user_regs_struct));
46 }
47 
48 static int riscv_gpr_set(struct task_struct *target,
49 			 const struct user_regset *regset,
50 			 unsigned int pos, unsigned int count,
51 			 const void *kbuf, const void __user *ubuf)
52 {
53 	struct pt_regs *regs;
54 
55 	regs = task_pt_regs(target);
56 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
57 }
58 
59 #ifdef CONFIG_FPU
60 static int riscv_fpr_get(struct task_struct *target,
61 			 const struct user_regset *regset,
62 			 struct membuf to)
63 {
64 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
65 
66 	if (target == current)
67 		fstate_save(current, task_pt_regs(current));
68 
69 	membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
70 	membuf_store(&to, fstate->fcsr);
71 	return membuf_zero(&to, 4);	// explicitly pad
72 }
73 
74 static int riscv_fpr_set(struct task_struct *target,
75 			 const struct user_regset *regset,
76 			 unsigned int pos, unsigned int count,
77 			 const void *kbuf, const void __user *ubuf)
78 {
79 	int ret;
80 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
81 
82 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
83 				 offsetof(struct __riscv_d_ext_state, fcsr));
84 	if (!ret) {
85 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
86 					 offsetof(struct __riscv_d_ext_state, fcsr) +
87 					 sizeof(fstate->fcsr));
88 	}
89 
90 	return ret;
91 }
92 #endif
93 
94 #ifdef CONFIG_RISCV_ISA_V
95 static int riscv_vr_get(struct task_struct *target,
96 			const struct user_regset *regset,
97 			struct membuf to)
98 {
99 	struct __riscv_v_ext_state *vstate = &target->thread.vstate;
100 	struct __riscv_v_regset_state ptrace_vstate;
101 
102 	if (!riscv_v_vstate_query(task_pt_regs(target)))
103 		return -EINVAL;
104 
105 	/*
106 	 * Ensure the vector registers have been saved to the memory before
107 	 * copying them to membuf.
108 	 */
109 	if (target == current) {
110 		get_cpu_vector_context();
111 		riscv_v_vstate_save(&current->thread.vstate, task_pt_regs(current));
112 		put_cpu_vector_context();
113 	}
114 
115 	ptrace_vstate.vstart = vstate->vstart;
116 	ptrace_vstate.vl = vstate->vl;
117 	ptrace_vstate.vtype = vstate->vtype;
118 	ptrace_vstate.vcsr = vstate->vcsr;
119 	ptrace_vstate.vlenb = vstate->vlenb;
120 
121 	/* Copy vector header from vstate. */
122 	membuf_write(&to, &ptrace_vstate, sizeof(struct __riscv_v_regset_state));
123 
124 	/* Copy all the vector registers from vstate. */
125 	return membuf_write(&to, vstate->datap, riscv_v_vsize);
126 }
127 
128 static int riscv_vr_set(struct task_struct *target,
129 			const struct user_regset *regset,
130 			unsigned int pos, unsigned int count,
131 			const void *kbuf, const void __user *ubuf)
132 {
133 	int ret;
134 	struct __riscv_v_ext_state *vstate = &target->thread.vstate;
135 	struct __riscv_v_regset_state ptrace_vstate;
136 
137 	if (!riscv_v_vstate_query(task_pt_regs(target)))
138 		return -EINVAL;
139 
140 	/* Copy rest of the vstate except datap */
141 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ptrace_vstate, 0,
142 				 sizeof(struct __riscv_v_regset_state));
143 	if (unlikely(ret))
144 		return ret;
145 
146 	if (vstate->vlenb != ptrace_vstate.vlenb)
147 		return -EINVAL;
148 
149 	vstate->vstart = ptrace_vstate.vstart;
150 	vstate->vl = ptrace_vstate.vl;
151 	vstate->vtype = ptrace_vstate.vtype;
152 	vstate->vcsr = ptrace_vstate.vcsr;
153 
154 	/* Copy all the vector registers. */
155 	pos = 0;
156 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap,
157 				 0, riscv_v_vsize);
158 	return ret;
159 }
160 
161 static int riscv_vr_active(struct task_struct *target, const struct user_regset *regset)
162 {
163 	if (!(has_vector() || has_xtheadvector()))
164 		return -ENODEV;
165 
166 	if (!riscv_v_vstate_query(task_pt_regs(target)))
167 		return 0;
168 
169 	return regset->n;
170 }
171 #endif
172 
173 #ifdef CONFIG_RISCV_ISA_SUPM
174 static int tagged_addr_ctrl_get(struct task_struct *target,
175 				const struct user_regset *regset,
176 				struct membuf to)
177 {
178 	long ctrl = get_tagged_addr_ctrl(target);
179 
180 	if (IS_ERR_VALUE(ctrl))
181 		return ctrl;
182 
183 	return membuf_write(&to, &ctrl, sizeof(ctrl));
184 }
185 
186 static int tagged_addr_ctrl_set(struct task_struct *target,
187 				const struct user_regset *regset,
188 				unsigned int pos, unsigned int count,
189 				const void *kbuf, const void __user *ubuf)
190 {
191 	int ret;
192 	long ctrl;
193 
194 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1);
195 	if (ret)
196 		return ret;
197 
198 	return set_tagged_addr_ctrl(target, ctrl);
199 }
200 #endif
201 
202 #ifdef CONFIG_RISCV_USER_CFI
203 static int riscv_cfi_get(struct task_struct *target,
204 			 const struct user_regset *regset,
205 			 struct membuf to)
206 {
207 	struct user_cfi_state user_cfi;
208 	struct pt_regs *regs;
209 
210 	memset(&user_cfi, 0, sizeof(user_cfi));
211 	regs = task_pt_regs(target);
212 
213 	if (is_indir_lp_enabled(target)) {
214 		user_cfi.cfi_status.cfi_state |= PTRACE_CFI_LP_EN_STATE;
215 		user_cfi.cfi_status.cfi_state |= is_indir_lp_locked(target) ?
216 						 PTRACE_CFI_LP_LOCK_STATE : 0;
217 		user_cfi.cfi_status.cfi_state |= (regs->status & SR_ELP) ?
218 						PTRACE_CFI_ELP_STATE : 0;
219 	}
220 
221 	if (is_shstk_enabled(target)) {
222 		user_cfi.cfi_status.cfi_state |= (PTRACE_CFI_SS_EN_STATE |
223 						  PTRACE_CFI_SS_PTR_STATE);
224 		user_cfi.cfi_status.cfi_state |= is_shstk_locked(target) ?
225 						 PTRACE_CFI_SS_LOCK_STATE : 0;
226 		user_cfi.shstk_ptr = get_active_shstk(target);
227 	}
228 
229 	return membuf_write(&to, &user_cfi, sizeof(user_cfi));
230 }
231 
232 /*
233  * Does it make sense to allow enable / disable of cfi via ptrace?
234  * We don't allow enable / disable / locking control via ptrace for now.
235  * Setting the shadow stack pointer is allowed. GDB might use it to unwind or
236  * some other fixup. Similarly gdb might want to suppress elp and may want
237  * to reset elp state.
238  */
239 static int riscv_cfi_set(struct task_struct *target,
240 			 const struct user_regset *regset,
241 			 unsigned int pos, unsigned int count,
242 			 const void *kbuf, const void __user *ubuf)
243 {
244 	int ret;
245 	struct user_cfi_state user_cfi;
246 	struct pt_regs *regs;
247 
248 	regs = task_pt_regs(target);
249 
250 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1);
251 	if (ret)
252 		return ret;
253 
254 	/*
255 	 * Not allowing enabling or locking shadow stack or landing pad
256 	 * There is no disabling of shadow stack or landing pad via ptrace
257 	 * rsvd field should be set to zero so that if those fields are needed in future
258 	 */
259 	if ((user_cfi.cfi_status.cfi_state &
260 	     (PTRACE_CFI_LP_EN_STATE | PTRACE_CFI_LP_LOCK_STATE |
261 	      PTRACE_CFI_SS_EN_STATE | PTRACE_CFI_SS_LOCK_STATE)) ||
262 	     (user_cfi.cfi_status.cfi_state & PRACE_CFI_STATE_INVALID_MASK))
263 		return -EINVAL;
264 
265 	/* If lpad is enabled on target and ptrace requests to set / clear elp, do that */
266 	if (is_indir_lp_enabled(target)) {
267 		if (user_cfi.cfi_status.cfi_state &
268 		    PTRACE_CFI_ELP_STATE) /* set elp state */
269 			regs->status |= SR_ELP;
270 		else
271 			regs->status &= ~SR_ELP; /* clear elp state */
272 	}
273 
274 	/* If shadow stack enabled on target, set new shadow stack pointer */
275 	if (is_shstk_enabled(target) &&
276 	    (user_cfi.cfi_status.cfi_state & PTRACE_CFI_SS_PTR_STATE))
277 		set_active_shstk(target, user_cfi.shstk_ptr);
278 
279 	return 0;
280 }
281 #endif
282 
283 static struct user_regset riscv_user_regset[] __ro_after_init = {
284 	[REGSET_X] = {
285 		USER_REGSET_NOTE_TYPE(PRSTATUS),
286 		.n = ELF_NGREG,
287 		.size = sizeof(elf_greg_t),
288 		.align = sizeof(elf_greg_t),
289 		.regset_get = riscv_gpr_get,
290 		.set = riscv_gpr_set,
291 	},
292 #ifdef CONFIG_FPU
293 	[REGSET_F] = {
294 		USER_REGSET_NOTE_TYPE(PRFPREG),
295 		.n = ELF_NFPREG,
296 		.size = sizeof(elf_fpreg_t),
297 		.align = sizeof(elf_fpreg_t),
298 		.regset_get = riscv_fpr_get,
299 		.set = riscv_fpr_set,
300 	},
301 #endif
302 #ifdef CONFIG_RISCV_ISA_V
303 	[REGSET_V] = {
304 		USER_REGSET_NOTE_TYPE(RISCV_VECTOR),
305 		.align = 16,
306 		.size = sizeof(__u32),
307 		.regset_get = riscv_vr_get,
308 		.set = riscv_vr_set,
309 		.active = riscv_vr_active,
310 	},
311 #endif
312 #ifdef CONFIG_RISCV_ISA_SUPM
313 	[REGSET_TAGGED_ADDR_CTRL] = {
314 		USER_REGSET_NOTE_TYPE(RISCV_TAGGED_ADDR_CTRL),
315 		.n = 1,
316 		.size = sizeof(long),
317 		.align = sizeof(long),
318 		.regset_get = tagged_addr_ctrl_get,
319 		.set = tagged_addr_ctrl_set,
320 	},
321 #endif
322 #ifdef CONFIG_RISCV_USER_CFI
323 	[REGSET_CFI] = {
324 		.core_note_type = NT_RISCV_USER_CFI,
325 		.align = sizeof(__u64),
326 		.n = sizeof(struct user_cfi_state) / sizeof(__u64),
327 		.size = sizeof(__u64),
328 		.regset_get = riscv_cfi_get,
329 		.set = riscv_cfi_set,
330 	},
331 #endif
332 };
333 
334 static const struct user_regset_view riscv_user_native_view = {
335 	.name = "riscv",
336 	.e_machine = EM_RISCV,
337 	.regsets = riscv_user_regset,
338 	.n = ARRAY_SIZE(riscv_user_regset),
339 };
340 
341 #ifdef CONFIG_RISCV_ISA_V
342 void __init update_regset_vector_info(unsigned long size)
343 {
344 	riscv_user_regset[REGSET_V].n = (size + sizeof(struct __riscv_v_regset_state)) /
345 					sizeof(__u32);
346 }
347 #endif
348 
349 struct pt_regs_offset {
350 	const char *name;
351 	int offset;
352 };
353 
354 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
355 #define REG_OFFSET_END {.name = NULL, .offset = 0}
356 
357 static const struct pt_regs_offset regoffset_table[] = {
358 	REG_OFFSET_NAME(epc),
359 	REG_OFFSET_NAME(ra),
360 	REG_OFFSET_NAME(sp),
361 	REG_OFFSET_NAME(gp),
362 	REG_OFFSET_NAME(tp),
363 	REG_OFFSET_NAME(t0),
364 	REG_OFFSET_NAME(t1),
365 	REG_OFFSET_NAME(t2),
366 	REG_OFFSET_NAME(s0),
367 	REG_OFFSET_NAME(s1),
368 	REG_OFFSET_NAME(a0),
369 	REG_OFFSET_NAME(a1),
370 	REG_OFFSET_NAME(a2),
371 	REG_OFFSET_NAME(a3),
372 	REG_OFFSET_NAME(a4),
373 	REG_OFFSET_NAME(a5),
374 	REG_OFFSET_NAME(a6),
375 	REG_OFFSET_NAME(a7),
376 	REG_OFFSET_NAME(s2),
377 	REG_OFFSET_NAME(s3),
378 	REG_OFFSET_NAME(s4),
379 	REG_OFFSET_NAME(s5),
380 	REG_OFFSET_NAME(s6),
381 	REG_OFFSET_NAME(s7),
382 	REG_OFFSET_NAME(s8),
383 	REG_OFFSET_NAME(s9),
384 	REG_OFFSET_NAME(s10),
385 	REG_OFFSET_NAME(s11),
386 	REG_OFFSET_NAME(t3),
387 	REG_OFFSET_NAME(t4),
388 	REG_OFFSET_NAME(t5),
389 	REG_OFFSET_NAME(t6),
390 	REG_OFFSET_NAME(status),
391 	REG_OFFSET_NAME(badaddr),
392 	REG_OFFSET_NAME(cause),
393 	REG_OFFSET_NAME(orig_a0),
394 	REG_OFFSET_END,
395 };
396 
397 /**
398  * regs_query_register_offset() - query register offset from its name
399  * @name:	the name of a register
400  *
401  * regs_query_register_offset() returns the offset of a register in struct
402  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
403  */
404 int regs_query_register_offset(const char *name)
405 {
406 	const struct pt_regs_offset *roff;
407 
408 	for (roff = regoffset_table; roff->name != NULL; roff++)
409 		if (!strcmp(roff->name, name))
410 			return roff->offset;
411 	return -EINVAL;
412 }
413 
414 /**
415  * regs_within_kernel_stack() - check the address in the stack
416  * @regs:      pt_regs which contains kernel stack pointer.
417  * @addr:      address which is checked.
418  *
419  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
420  * If @addr is within the kernel stack, it returns true. If not, returns false.
421  */
422 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
423 {
424 	return (addr & ~(THREAD_SIZE - 1))  ==
425 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
426 }
427 
428 /**
429  * regs_get_kernel_stack_nth() - get Nth entry of the stack
430  * @regs:	pt_regs which contains kernel stack pointer.
431  * @n:		stack entry number.
432  *
433  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
434  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
435  * this returns 0.
436  */
437 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
438 {
439 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
440 
441 	addr += n;
442 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
443 		return *addr;
444 	else
445 		return 0;
446 }
447 
448 void ptrace_disable(struct task_struct *child)
449 {
450 }
451 
452 long arch_ptrace(struct task_struct *child, long request,
453 		 unsigned long addr, unsigned long data)
454 {
455 	long ret = -EIO;
456 
457 	switch (request) {
458 	default:
459 		ret = ptrace_request(child, request, addr, data);
460 		break;
461 	}
462 
463 	return ret;
464 }
465 
466 #ifdef CONFIG_COMPAT
467 static int compat_riscv_gpr_get(struct task_struct *target,
468 				const struct user_regset *regset,
469 				struct membuf to)
470 {
471 	struct compat_user_regs_struct cregs;
472 
473 	regs_to_cregs(&cregs, task_pt_regs(target));
474 
475 	return membuf_write(&to, &cregs,
476 			    sizeof(struct compat_user_regs_struct));
477 }
478 
479 static int compat_riscv_gpr_set(struct task_struct *target,
480 				const struct user_regset *regset,
481 				unsigned int pos, unsigned int count,
482 				const void *kbuf, const void __user *ubuf)
483 {
484 	int ret;
485 	struct compat_user_regs_struct cregs;
486 
487 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
488 
489 	cregs_to_regs(&cregs, task_pt_regs(target));
490 
491 	return ret;
492 }
493 
494 static const struct user_regset compat_riscv_user_regset[] = {
495 	[REGSET_X] = {
496 		USER_REGSET_NOTE_TYPE(PRSTATUS),
497 		.n = ELF_NGREG,
498 		.size = sizeof(compat_elf_greg_t),
499 		.align = sizeof(compat_elf_greg_t),
500 		.regset_get = compat_riscv_gpr_get,
501 		.set = compat_riscv_gpr_set,
502 	},
503 #ifdef CONFIG_FPU
504 	[REGSET_F] = {
505 		USER_REGSET_NOTE_TYPE(PRFPREG),
506 		.n = ELF_NFPREG,
507 		.size = sizeof(elf_fpreg_t),
508 		.align = sizeof(elf_fpreg_t),
509 		.regset_get = riscv_fpr_get,
510 		.set = riscv_fpr_set,
511 	},
512 #endif
513 };
514 
515 static const struct user_regset_view compat_riscv_user_native_view = {
516 	.name = "riscv",
517 	.e_machine = EM_RISCV,
518 	.regsets = compat_riscv_user_regset,
519 	.n = ARRAY_SIZE(compat_riscv_user_regset),
520 };
521 
522 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
523 			compat_ulong_t caddr, compat_ulong_t cdata)
524 {
525 	long ret = -EIO;
526 
527 	switch (request) {
528 	default:
529 		ret = compat_ptrace_request(child, request, caddr, cdata);
530 		break;
531 	}
532 
533 	return ret;
534 }
535 #else
536 static const struct user_regset_view compat_riscv_user_native_view = {};
537 #endif /* CONFIG_COMPAT */
538 
539 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
540 {
541 	if (is_compat_thread(&task->thread_info))
542 		return &compat_riscv_user_native_view;
543 	else
544 		return &riscv_user_native_view;
545 }
546