xref: /openbmc/linux/arch/arc/kernel/ptrace.c (revision a93fbb00)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4  */
5 
6 #include <linux/ptrace.h>
7 #include <linux/sched/task_stack.h>
8 #include <linux/regset.h>
9 #include <linux/unistd.h>
10 #include <linux/elf.h>
11 
12 static struct callee_regs *task_callee_regs(struct task_struct *tsk)
13 {
14 	struct callee_regs *tmp = (struct callee_regs *)tsk->thread.callee_reg;
15 	return tmp;
16 }
17 
18 static int genregs_get(struct task_struct *target,
19 		       const struct user_regset *regset,
20 		       struct membuf to)
21 {
22 	const struct pt_regs *ptregs = task_pt_regs(target);
23 	const struct callee_regs *cregs = task_callee_regs(target);
24 	unsigned int stop_pc_val;
25 
26 	membuf_zero(&to, 4);	// pad
27 	membuf_store(&to, ptregs->bta);
28 	membuf_store(&to, ptregs->lp_start);
29 	membuf_store(&to, ptregs->lp_end);
30 	membuf_store(&to, ptregs->lp_count);
31 	membuf_store(&to, ptregs->status32);
32 	membuf_store(&to, ptregs->ret);
33 	membuf_store(&to, ptregs->blink);
34 	membuf_store(&to, ptregs->fp);
35 	membuf_store(&to, ptregs->r26);	// gp
36 	membuf_store(&to, ptregs->r12);
37 	membuf_store(&to, ptregs->r11);
38 	membuf_store(&to, ptregs->r10);
39 	membuf_store(&to, ptregs->r9);
40 	membuf_store(&to, ptregs->r8);
41 	membuf_store(&to, ptregs->r7);
42 	membuf_store(&to, ptregs->r6);
43 	membuf_store(&to, ptregs->r5);
44 	membuf_store(&to, ptregs->r4);
45 	membuf_store(&to, ptregs->r3);
46 	membuf_store(&to, ptregs->r2);
47 	membuf_store(&to, ptregs->r1);
48 	membuf_store(&to, ptregs->r0);
49 	membuf_store(&to, ptregs->sp);
50 	membuf_zero(&to, 4);	// pad2
51 	membuf_store(&to, cregs->r25);
52 	membuf_store(&to, cregs->r24);
53 	membuf_store(&to, cregs->r23);
54 	membuf_store(&to, cregs->r22);
55 	membuf_store(&to, cregs->r21);
56 	membuf_store(&to, cregs->r20);
57 	membuf_store(&to, cregs->r19);
58 	membuf_store(&to, cregs->r18);
59 	membuf_store(&to, cregs->r17);
60 	membuf_store(&to, cregs->r16);
61 	membuf_store(&to, cregs->r15);
62 	membuf_store(&to, cregs->r14);
63 	membuf_store(&to, cregs->r13);
64 	membuf_store(&to, target->thread.fault_address); // efa
65 
66 	if (in_brkpt_trap(ptregs)) {
67 		stop_pc_val = target->thread.fault_address;
68 		pr_debug("\t\tstop_pc (brk-pt)\n");
69 	} else {
70 		stop_pc_val = ptregs->ret;
71 		pr_debug("\t\tstop_pc (others)\n");
72 	}
73 
74 	return membuf_store(&to, stop_pc_val); // stop_pc
75 }
76 
77 static int genregs_set(struct task_struct *target,
78 		       const struct user_regset *regset,
79 		       unsigned int pos, unsigned int count,
80 		       const void *kbuf, const void __user *ubuf)
81 {
82 	const struct pt_regs *ptregs = task_pt_regs(target);
83 	const struct callee_regs *cregs = task_callee_regs(target);
84 	int ret = 0;
85 
86 #define REG_IN_CHUNK(FIRST, NEXT, PTR)	\
87 	if (!ret)			\
88 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
89 			(void *)(PTR), \
90 			offsetof(struct user_regs_struct, FIRST), \
91 			offsetof(struct user_regs_struct, NEXT));
92 
93 #define REG_IN_ONE(LOC, PTR)		\
94 	if (!ret)			\
95 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, \
96 			(void *)(PTR), \
97 			offsetof(struct user_regs_struct, LOC), \
98 			offsetof(struct user_regs_struct, LOC) + 4);
99 
100 #define REG_IGNORE_ONE(LOC)		\
101 	if (!ret)			\
102 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, \
103 			offsetof(struct user_regs_struct, LOC), \
104 			offsetof(struct user_regs_struct, LOC) + 4);
105 
106 	REG_IGNORE_ONE(pad);
107 
108 	REG_IN_ONE(scratch.bta, &ptregs->bta);
109 	REG_IN_ONE(scratch.lp_start, &ptregs->lp_start);
110 	REG_IN_ONE(scratch.lp_end, &ptregs->lp_end);
111 	REG_IN_ONE(scratch.lp_count, &ptregs->lp_count);
112 
113 	REG_IGNORE_ONE(scratch.status32);
114 
115 	REG_IN_ONE(scratch.ret, &ptregs->ret);
116 	REG_IN_ONE(scratch.blink, &ptregs->blink);
117 	REG_IN_ONE(scratch.fp, &ptregs->fp);
118 	REG_IN_ONE(scratch.gp, &ptregs->r26);
119 	REG_IN_ONE(scratch.r12, &ptregs->r12);
120 	REG_IN_ONE(scratch.r11, &ptregs->r11);
121 	REG_IN_ONE(scratch.r10, &ptregs->r10);
122 	REG_IN_ONE(scratch.r9, &ptregs->r9);
123 	REG_IN_ONE(scratch.r8, &ptregs->r8);
124 	REG_IN_ONE(scratch.r7, &ptregs->r7);
125 	REG_IN_ONE(scratch.r6, &ptregs->r6);
126 	REG_IN_ONE(scratch.r5, &ptregs->r5);
127 	REG_IN_ONE(scratch.r4, &ptregs->r4);
128 	REG_IN_ONE(scratch.r3, &ptregs->r3);
129 	REG_IN_ONE(scratch.r2, &ptregs->r2);
130 	REG_IN_ONE(scratch.r1, &ptregs->r1);
131 	REG_IN_ONE(scratch.r0, &ptregs->r0);
132 	REG_IN_ONE(scratch.sp, &ptregs->sp);
133 
134 	REG_IGNORE_ONE(pad2);
135 
136 	REG_IN_ONE(callee.r25, &cregs->r25);
137 	REG_IN_ONE(callee.r24, &cregs->r24);
138 	REG_IN_ONE(callee.r23, &cregs->r23);
139 	REG_IN_ONE(callee.r22, &cregs->r22);
140 	REG_IN_ONE(callee.r21, &cregs->r21);
141 	REG_IN_ONE(callee.r20, &cregs->r20);
142 	REG_IN_ONE(callee.r19, &cregs->r19);
143 	REG_IN_ONE(callee.r18, &cregs->r18);
144 	REG_IN_ONE(callee.r17, &cregs->r17);
145 	REG_IN_ONE(callee.r16, &cregs->r16);
146 	REG_IN_ONE(callee.r15, &cregs->r15);
147 	REG_IN_ONE(callee.r14, &cregs->r14);
148 	REG_IN_ONE(callee.r13, &cregs->r13);
149 
150 	REG_IGNORE_ONE(efa);			/* efa update invalid */
151 	REG_IGNORE_ONE(stop_pc);		/* PC updated via @ret */
152 
153 	return ret;
154 }
155 
156 #ifdef CONFIG_ISA_ARCV2
157 static int arcv2regs_get(struct task_struct *target,
158 		       const struct user_regset *regset,
159 		       struct membuf to)
160 {
161 	const struct pt_regs *regs = task_pt_regs(target);
162 
163 	if (IS_ENABLED(CONFIG_ARC_HAS_ACCL_REGS))
164 		/*
165 		 * itemized copy not needed like above as layout of regs (r30,r58,r59)
166 		 * is exactly same in kernel (pt_regs) and userspace (user_regs_arcv2)
167 		 */
168 		return membuf_write(&to, &regs->r30, sizeof(struct user_regs_arcv2));
169 
170 
171 	membuf_write(&to, &regs->r30, 4); /* r30 only */
172 	return membuf_zero(&to, sizeof(struct user_regs_arcv2) - 4);
173 }
174 
175 static int arcv2regs_set(struct task_struct *target,
176 		       const struct user_regset *regset,
177 		       unsigned int pos, unsigned int count,
178 		       const void *kbuf, const void __user *ubuf)
179 {
180 	const struct pt_regs *regs = task_pt_regs(target);
181 	int ret, copy_sz;
182 
183 	if (IS_ENABLED(CONFIG_ARC_HAS_ACCL_REGS))
184 		copy_sz = sizeof(struct user_regs_arcv2);
185 	else
186 		copy_sz = 4;	/* r30 only */
187 
188 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, (void *)&regs->r30,
189 				  0, copy_sz);
190 
191 	return ret;
192 }
193 
194 #endif
195 
196 enum arc_getset {
197 	REGSET_CMN,
198 	REGSET_ARCV2,
199 };
200 
201 static const struct user_regset arc_regsets[] = {
202 	[REGSET_CMN] = {
203 	       .core_note_type = NT_PRSTATUS,
204 	       .n = ELF_NGREG,
205 	       .size = sizeof(unsigned long),
206 	       .align = sizeof(unsigned long),
207 	       .regset_get = genregs_get,
208 	       .set = genregs_set,
209 	},
210 #ifdef CONFIG_ISA_ARCV2
211 	[REGSET_ARCV2] = {
212 	       .core_note_type = NT_ARC_V2,
213 	       .n = ELF_ARCV2REG,
214 	       .size = sizeof(unsigned long),
215 	       .align = sizeof(unsigned long),
216 	       .regset_get = arcv2regs_get,
217 	       .set = arcv2regs_set,
218 	},
219 #endif
220 };
221 
222 static const struct user_regset_view user_arc_view = {
223 	.name		= "arc",
224 	.e_machine	= EM_ARC_INUSE,
225 	.regsets	= arc_regsets,
226 	.n		= ARRAY_SIZE(arc_regsets)
227 };
228 
229 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
230 {
231 	return &user_arc_view;
232 }
233 
234 void ptrace_disable(struct task_struct *child)
235 {
236 }
237 
238 long arch_ptrace(struct task_struct *child, long request,
239 		 unsigned long addr, unsigned long data)
240 {
241 	int ret = -EIO;
242 
243 	pr_debug("REQ=%ld: ADDR =0x%lx, DATA=0x%lx)\n", request, addr, data);
244 
245 	switch (request) {
246 	case PTRACE_GET_THREAD_AREA:
247 		ret = put_user(task_thread_info(child)->thr_ptr,
248 			       (unsigned long __user *)data);
249 		break;
250 	default:
251 		ret = ptrace_request(child, request, addr, data);
252 		break;
253 	}
254 
255 	return ret;
256 }
257 
258 asmlinkage int syscall_trace_entry(struct pt_regs *regs)
259 {
260 	if (ptrace_report_syscall_entry(regs))
261 		return ULONG_MAX;
262 
263 	return regs->r8;
264 }
265 
266 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
267 {
268 	ptrace_report_syscall_exit(regs, 0);
269 }
270