• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ptrace.h>
11 #include <asm/syscall.h>
12 #include <asm/thread_info.h>
13 #include <asm/switch_to.h>
14 #include <linux/audit.h>
15 #include <linux/ptrace.h>
16 #include <linux/elf.h>
17 #include <linux/regset.h>
18 #include <linux/sched.h>
19 #include <linux/sched/task_stack.h>
20 #include <linux/tracehook.h>
21 
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/syscalls.h>
24 
25 enum riscv_regset {
26 	REGSET_X,
27 #ifdef CONFIG_FPU
28 	REGSET_F,
29 #endif
30 };
31 
riscv_gpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)32 static int riscv_gpr_get(struct task_struct *target,
33 			 const struct user_regset *regset,
34 			 struct membuf to)
35 {
36 	return membuf_write(&to, task_pt_regs(target),
37 			    sizeof(struct user_regs_struct));
38 }
39 
riscv_gpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)40 static int riscv_gpr_set(struct task_struct *target,
41 			 const struct user_regset *regset,
42 			 unsigned int pos, unsigned int count,
43 			 const void *kbuf, const void __user *ubuf)
44 {
45 	int ret;
46 	struct pt_regs *regs;
47 
48 	regs = task_pt_regs(target);
49 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
50 	return ret;
51 }
52 
53 #ifdef CONFIG_FPU
riscv_fpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)54 static int riscv_fpr_get(struct task_struct *target,
55 			 const struct user_regset *regset,
56 			 struct membuf to)
57 {
58 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
59 
60 	if (target == current)
61 		fstate_save(current, task_pt_regs(current));
62 
63 	membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
64 	membuf_store(&to, fstate->fcsr);
65 	return membuf_zero(&to, 4);	// explicitly pad
66 }
67 
riscv_fpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)68 static int riscv_fpr_set(struct task_struct *target,
69 			 const struct user_regset *regset,
70 			 unsigned int pos, unsigned int count,
71 			 const void *kbuf, const void __user *ubuf)
72 {
73 	int ret;
74 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
75 
76 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
77 				 offsetof(struct __riscv_d_ext_state, fcsr));
78 	if (!ret) {
79 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
80 					 offsetof(struct __riscv_d_ext_state, fcsr) +
81 					 sizeof(fstate->fcsr));
82 	}
83 
84 	return ret;
85 }
86 #endif
87 
88 static const struct user_regset riscv_user_regset[] = {
89 	[REGSET_X] = {
90 		.core_note_type = NT_PRSTATUS,
91 		.n = ELF_NGREG,
92 		.size = sizeof(elf_greg_t),
93 		.align = sizeof(elf_greg_t),
94 		.regset_get = riscv_gpr_get,
95 		.set = riscv_gpr_set,
96 	},
97 #ifdef CONFIG_FPU
98 	[REGSET_F] = {
99 		.core_note_type = NT_PRFPREG,
100 		.n = ELF_NFPREG,
101 		.size = sizeof(elf_fpreg_t),
102 		.align = sizeof(elf_fpreg_t),
103 		.regset_get = riscv_fpr_get,
104 		.set = riscv_fpr_set,
105 	},
106 #endif
107 };
108 
109 static const struct user_regset_view riscv_user_native_view = {
110 	.name = "riscv",
111 	.e_machine = EM_RISCV,
112 	.regsets = riscv_user_regset,
113 	.n = ARRAY_SIZE(riscv_user_regset),
114 };
115 
task_user_regset_view(struct task_struct * task)116 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
117 {
118 	return &riscv_user_native_view;
119 }
120 
ptrace_disable(struct task_struct * child)121 void ptrace_disable(struct task_struct *child)
122 {
123 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
124 }
125 
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)126 long arch_ptrace(struct task_struct *child, long request,
127 		 unsigned long addr, unsigned long data)
128 {
129 	long ret = -EIO;
130 
131 	switch (request) {
132 	default:
133 		ret = ptrace_request(child, request, addr, data);
134 		break;
135 	}
136 
137 	return ret;
138 }
139 
140 /*
141  * Allows PTRACE_SYSCALL to work.  These are called from entry.S in
142  * {handle,ret_from}_syscall.
143  */
do_syscall_trace_enter(struct pt_regs * regs)144 __visible int do_syscall_trace_enter(struct pt_regs *regs)
145 {
146 	if (test_thread_flag(TIF_SYSCALL_TRACE))
147 		if (tracehook_report_syscall_entry(regs))
148 			return -1;
149 
150 	/*
151 	 * Do the secure computing after ptrace; failures should be fast.
152 	 * If this fails we might have return value in a0 from seccomp
153 	 * (via SECCOMP_RET_ERRNO/TRACE).
154 	 */
155 	if (secure_computing() == -1)
156 		return -1;
157 
158 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
159 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
160 		trace_sys_enter(regs, syscall_get_nr(current, regs));
161 #endif
162 
163 	audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
164 	return 0;
165 }
166 
do_syscall_trace_exit(struct pt_regs * regs)167 __visible void do_syscall_trace_exit(struct pt_regs *regs)
168 {
169 	audit_syscall_exit(regs);
170 
171 	if (test_thread_flag(TIF_SYSCALL_TRACE))
172 		tracehook_report_syscall_exit(regs, 0);
173 
174 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
175 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
176 		trace_sys_exit(regs, regs_return_value(regs));
177 #endif
178 }
179