xref: /openbmc/linux/arch/powerpc/kernel/ptrace/ptrace-view.c (revision 8dc7f0229b7892ccb23e19c9f30511c68cc0fdcc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/regset.h>
4 #include <linux/elf.h>
5 #include <linux/nospec.h>
6 #include <linux/pkeys.h>
7 
8 #include "ptrace-decl.h"
9 
10 struct pt_regs_offset {
11 	const char *name;
12 	int offset;
13 };
14 
15 #define STR(s)	#s			/* convert to string */
16 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
17 #define GPR_OFFSET_NAME(num)	\
18 	{.name = STR(r##num), .offset = offsetof(struct pt_regs, gpr[num])}, \
19 	{.name = STR(gpr##num), .offset = offsetof(struct pt_regs, gpr[num])}
20 #define REG_OFFSET_END {.name = NULL, .offset = 0}
21 
22 static const struct pt_regs_offset regoffset_table[] = {
23 	GPR_OFFSET_NAME(0),
24 	GPR_OFFSET_NAME(1),
25 	GPR_OFFSET_NAME(2),
26 	GPR_OFFSET_NAME(3),
27 	GPR_OFFSET_NAME(4),
28 	GPR_OFFSET_NAME(5),
29 	GPR_OFFSET_NAME(6),
30 	GPR_OFFSET_NAME(7),
31 	GPR_OFFSET_NAME(8),
32 	GPR_OFFSET_NAME(9),
33 	GPR_OFFSET_NAME(10),
34 	GPR_OFFSET_NAME(11),
35 	GPR_OFFSET_NAME(12),
36 	GPR_OFFSET_NAME(13),
37 	GPR_OFFSET_NAME(14),
38 	GPR_OFFSET_NAME(15),
39 	GPR_OFFSET_NAME(16),
40 	GPR_OFFSET_NAME(17),
41 	GPR_OFFSET_NAME(18),
42 	GPR_OFFSET_NAME(19),
43 	GPR_OFFSET_NAME(20),
44 	GPR_OFFSET_NAME(21),
45 	GPR_OFFSET_NAME(22),
46 	GPR_OFFSET_NAME(23),
47 	GPR_OFFSET_NAME(24),
48 	GPR_OFFSET_NAME(25),
49 	GPR_OFFSET_NAME(26),
50 	GPR_OFFSET_NAME(27),
51 	GPR_OFFSET_NAME(28),
52 	GPR_OFFSET_NAME(29),
53 	GPR_OFFSET_NAME(30),
54 	GPR_OFFSET_NAME(31),
55 	REG_OFFSET_NAME(nip),
56 	REG_OFFSET_NAME(msr),
57 	REG_OFFSET_NAME(ctr),
58 	REG_OFFSET_NAME(link),
59 	REG_OFFSET_NAME(xer),
60 	REG_OFFSET_NAME(ccr),
61 #ifdef CONFIG_PPC64
62 	REG_OFFSET_NAME(softe),
63 #else
64 	REG_OFFSET_NAME(mq),
65 #endif
66 	REG_OFFSET_NAME(trap),
67 	REG_OFFSET_NAME(dar),
68 	REG_OFFSET_NAME(dsisr),
69 	REG_OFFSET_END,
70 };
71 
72 /**
73  * regs_query_register_offset() - query register offset from its name
74  * @name:	the name of a register
75  *
76  * regs_query_register_offset() returns the offset of a register in struct
77  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
78  */
79 int regs_query_register_offset(const char *name)
80 {
81 	const struct pt_regs_offset *roff;
82 	for (roff = regoffset_table; roff->name != NULL; roff++)
83 		if (!strcmp(roff->name, name))
84 			return roff->offset;
85 	return -EINVAL;
86 }
87 
88 /**
89  * regs_query_register_name() - query register name from its offset
90  * @offset:	the offset of a register in struct pt_regs.
91  *
92  * regs_query_register_name() returns the name of a register from its
93  * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
94  */
95 const char *regs_query_register_name(unsigned int offset)
96 {
97 	const struct pt_regs_offset *roff;
98 	for (roff = regoffset_table; roff->name != NULL; roff++)
99 		if (roff->offset == offset)
100 			return roff->name;
101 	return NULL;
102 }
103 
104 /*
105  * does not yet catch signals sent when the child dies.
106  * in exit.c or in signal.c.
107  */
108 
109 static unsigned long get_user_msr(struct task_struct *task)
110 {
111 	return task->thread.regs->msr | task->thread.fpexc_mode;
112 }
113 
114 static __always_inline int set_user_msr(struct task_struct *task, unsigned long msr)
115 {
116 	task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
117 	task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
118 	return 0;
119 }
120 
121 #ifdef CONFIG_PPC64
122 static int get_user_dscr(struct task_struct *task, unsigned long *data)
123 {
124 	*data = task->thread.dscr;
125 	return 0;
126 }
127 
128 static int set_user_dscr(struct task_struct *task, unsigned long dscr)
129 {
130 	task->thread.dscr = dscr;
131 	task->thread.dscr_inherit = 1;
132 	return 0;
133 }
134 #else
135 static int get_user_dscr(struct task_struct *task, unsigned long *data)
136 {
137 	return -EIO;
138 }
139 
140 static int set_user_dscr(struct task_struct *task, unsigned long dscr)
141 {
142 	return -EIO;
143 }
144 #endif
145 
146 /*
147  * We prevent mucking around with the reserved area of trap
148  * which are used internally by the kernel.
149  */
150 static __always_inline int set_user_trap(struct task_struct *task, unsigned long trap)
151 {
152 	set_trap(task->thread.regs, trap);
153 	return 0;
154 }
155 
156 /*
157  * Get contents of register REGNO in task TASK.
158  */
159 int ptrace_get_reg(struct task_struct *task, int regno, unsigned long *data)
160 {
161 	unsigned int regs_max;
162 
163 	if (task->thread.regs == NULL || !data)
164 		return -EIO;
165 
166 	if (regno == PT_MSR) {
167 		*data = get_user_msr(task);
168 		return 0;
169 	}
170 
171 	if (regno == PT_DSCR)
172 		return get_user_dscr(task, data);
173 
174 	/*
175 	 * softe copies paca->irq_soft_mask variable state. Since irq_soft_mask is
176 	 * no more used as a flag, lets force usr to alway see the softe value as 1
177 	 * which means interrupts are not soft disabled.
178 	 */
179 	if (IS_ENABLED(CONFIG_PPC64) && regno == PT_SOFTE) {
180 		*data = 1;
181 		return  0;
182 	}
183 
184 	regs_max = sizeof(struct user_pt_regs) / sizeof(unsigned long);
185 	if (regno < regs_max) {
186 		regno = array_index_nospec(regno, regs_max);
187 		*data = ((unsigned long *)task->thread.regs)[regno];
188 		return 0;
189 	}
190 
191 	return -EIO;
192 }
193 
194 /*
195  * Write contents of register REGNO in task TASK.
196  */
197 int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
198 {
199 	if (task->thread.regs == NULL)
200 		return -EIO;
201 
202 	if (regno == PT_MSR)
203 		return set_user_msr(task, data);
204 	if (regno == PT_TRAP)
205 		return set_user_trap(task, data);
206 	if (regno == PT_DSCR)
207 		return set_user_dscr(task, data);
208 
209 	if (regno <= PT_MAX_PUT_REG) {
210 		regno = array_index_nospec(regno, PT_MAX_PUT_REG + 1);
211 		((unsigned long *)task->thread.regs)[regno] = data;
212 		return 0;
213 	}
214 	return -EIO;
215 }
216 
217 static int gpr_get(struct task_struct *target, const struct user_regset *regset,
218 		   struct membuf to)
219 {
220 	struct membuf to_msr = membuf_at(&to, offsetof(struct pt_regs, msr));
221 #ifdef CONFIG_PPC64
222 	struct membuf to_softe = membuf_at(&to, offsetof(struct pt_regs, softe));
223 #endif
224 	if (target->thread.regs == NULL)
225 		return -EIO;
226 
227 	membuf_write(&to, target->thread.regs, sizeof(struct user_pt_regs));
228 
229 	membuf_store(&to_msr, get_user_msr(target));
230 #ifdef CONFIG_PPC64
231 	membuf_store(&to_softe, 0x1ul);
232 #endif
233 	return membuf_zero(&to, ELF_NGREG * sizeof(unsigned long) -
234 				 sizeof(struct user_pt_regs));
235 }
236 
237 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
238 		   unsigned int pos, unsigned int count, const void *kbuf,
239 		   const void __user *ubuf)
240 {
241 	unsigned long reg;
242 	int ret;
243 
244 	if (target->thread.regs == NULL)
245 		return -EIO;
246 
247 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
248 				 target->thread.regs,
249 				 0, PT_MSR * sizeof(reg));
250 
251 	if (!ret && count > 0) {
252 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
253 					 PT_MSR * sizeof(reg),
254 					 (PT_MSR + 1) * sizeof(reg));
255 		if (!ret)
256 			ret = set_user_msr(target, reg);
257 	}
258 
259 	BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
260 		     offsetof(struct pt_regs, msr) + sizeof(long));
261 
262 	if (!ret)
263 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
264 					 &target->thread.regs->orig_gpr3,
265 					 PT_ORIG_R3 * sizeof(reg),
266 					 (PT_MAX_PUT_REG + 1) * sizeof(reg));
267 
268 	if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
269 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
270 						(PT_MAX_PUT_REG + 1) * sizeof(reg),
271 						PT_TRAP * sizeof(reg));
272 
273 	if (!ret && count > 0) {
274 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &reg,
275 					 PT_TRAP * sizeof(reg),
276 					 (PT_TRAP + 1) * sizeof(reg));
277 		if (!ret)
278 			ret = set_user_trap(target, reg);
279 	}
280 
281 	if (!ret)
282 		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
283 						(PT_TRAP + 1) * sizeof(reg), -1);
284 
285 	return ret;
286 }
287 
288 #ifdef CONFIG_PPC64
289 static int ppr_get(struct task_struct *target, const struct user_regset *regset,
290 		   struct membuf to)
291 {
292 	return membuf_write(&to, &target->thread.regs->ppr, sizeof(u64));
293 }
294 
295 static int ppr_set(struct task_struct *target, const struct user_regset *regset,
296 		   unsigned int pos, unsigned int count, const void *kbuf,
297 		   const void __user *ubuf)
298 {
299 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
300 				  &target->thread.regs->ppr, 0, sizeof(u64));
301 }
302 
303 static int dscr_get(struct task_struct *target, const struct user_regset *regset,
304 		    struct membuf to)
305 {
306 	return membuf_write(&to, &target->thread.dscr, sizeof(u64));
307 }
308 static int dscr_set(struct task_struct *target, const struct user_regset *regset,
309 		    unsigned int pos, unsigned int count, const void *kbuf,
310 		    const void __user *ubuf)
311 {
312 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
313 				  &target->thread.dscr, 0, sizeof(u64));
314 }
315 #endif
316 #ifdef CONFIG_PPC_BOOK3S_64
317 static int tar_get(struct task_struct *target, const struct user_regset *regset,
318 		   struct membuf to)
319 {
320 	return membuf_write(&to, &target->thread.tar, sizeof(u64));
321 }
322 static int tar_set(struct task_struct *target, const struct user_regset *regset,
323 		   unsigned int pos, unsigned int count, const void *kbuf,
324 		   const void __user *ubuf)
325 {
326 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
327 				  &target->thread.tar, 0, sizeof(u64));
328 }
329 
330 static int ebb_active(struct task_struct *target, const struct user_regset *regset)
331 {
332 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
333 		return -ENODEV;
334 
335 	if (target->thread.used_ebb)
336 		return regset->n;
337 
338 	return 0;
339 }
340 
341 static int ebb_get(struct task_struct *target, const struct user_regset *regset,
342 		   struct membuf to)
343 {
344 	/* Build tests */
345 	BUILD_BUG_ON(TSO(ebbrr) + sizeof(unsigned long) != TSO(ebbhr));
346 	BUILD_BUG_ON(TSO(ebbhr) + sizeof(unsigned long) != TSO(bescr));
347 
348 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
349 		return -ENODEV;
350 
351 	if (!target->thread.used_ebb)
352 		return -ENODATA;
353 
354 	return membuf_write(&to, &target->thread.ebbrr, 3 * sizeof(unsigned long));
355 }
356 
357 static int ebb_set(struct task_struct *target, const struct user_regset *regset,
358 		   unsigned int pos, unsigned int count, const void *kbuf,
359 		   const void __user *ubuf)
360 {
361 	int ret = 0;
362 
363 	/* Build tests */
364 	BUILD_BUG_ON(TSO(ebbrr) + sizeof(unsigned long) != TSO(ebbhr));
365 	BUILD_BUG_ON(TSO(ebbhr) + sizeof(unsigned long) != TSO(bescr));
366 
367 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
368 		return -ENODEV;
369 
370 	if (target->thread.used_ebb)
371 		return -ENODATA;
372 
373 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &target->thread.ebbrr,
374 				 0, sizeof(unsigned long));
375 
376 	if (!ret)
377 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
378 					 &target->thread.ebbhr, sizeof(unsigned long),
379 					 2 * sizeof(unsigned long));
380 
381 	if (!ret)
382 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
383 					 &target->thread.bescr, 2 * sizeof(unsigned long),
384 					 3 * sizeof(unsigned long));
385 
386 	return ret;
387 }
388 static int pmu_active(struct task_struct *target, const struct user_regset *regset)
389 {
390 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
391 		return -ENODEV;
392 
393 	return regset->n;
394 }
395 
396 static int pmu_get(struct task_struct *target, const struct user_regset *regset,
397 		   struct membuf to)
398 {
399 	/* Build tests */
400 	BUILD_BUG_ON(TSO(siar) + sizeof(unsigned long) != TSO(sdar));
401 	BUILD_BUG_ON(TSO(sdar) + sizeof(unsigned long) != TSO(sier));
402 	BUILD_BUG_ON(TSO(sier) + sizeof(unsigned long) != TSO(mmcr2));
403 	BUILD_BUG_ON(TSO(mmcr2) + sizeof(unsigned long) != TSO(mmcr0));
404 
405 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
406 		return -ENODEV;
407 
408 	return membuf_write(&to, &target->thread.siar, 5 * sizeof(unsigned long));
409 }
410 
411 static int pmu_set(struct task_struct *target, const struct user_regset *regset,
412 		   unsigned int pos, unsigned int count, const void *kbuf,
413 		   const void __user *ubuf)
414 {
415 	int ret = 0;
416 
417 	/* Build tests */
418 	BUILD_BUG_ON(TSO(siar) + sizeof(unsigned long) != TSO(sdar));
419 	BUILD_BUG_ON(TSO(sdar) + sizeof(unsigned long) != TSO(sier));
420 	BUILD_BUG_ON(TSO(sier) + sizeof(unsigned long) != TSO(mmcr2));
421 	BUILD_BUG_ON(TSO(mmcr2) + sizeof(unsigned long) != TSO(mmcr0));
422 
423 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
424 		return -ENODEV;
425 
426 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &target->thread.siar,
427 				 0, sizeof(unsigned long));
428 
429 	if (!ret)
430 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
431 					 &target->thread.sdar, sizeof(unsigned long),
432 					 2 * sizeof(unsigned long));
433 
434 	if (!ret)
435 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
436 					 &target->thread.sier, 2 * sizeof(unsigned long),
437 					 3 * sizeof(unsigned long));
438 
439 	if (!ret)
440 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
441 					 &target->thread.mmcr2, 3 * sizeof(unsigned long),
442 					 4 * sizeof(unsigned long));
443 
444 	if (!ret)
445 		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
446 					 &target->thread.mmcr0, 4 * sizeof(unsigned long),
447 					 5 * sizeof(unsigned long));
448 	return ret;
449 }
450 #endif
451 
452 #ifdef CONFIG_PPC_MEM_KEYS
453 static int pkey_active(struct task_struct *target, const struct user_regset *regset)
454 {
455 	if (!arch_pkeys_enabled())
456 		return -ENODEV;
457 
458 	return regset->n;
459 }
460 
461 static int pkey_get(struct task_struct *target, const struct user_regset *regset,
462 		    struct membuf to)
463 {
464 
465 	if (!arch_pkeys_enabled())
466 		return -ENODEV;
467 
468 	membuf_store(&to, target->thread.regs->amr);
469 	membuf_store(&to, target->thread.regs->iamr);
470 	return membuf_store(&to, default_uamor);
471 }
472 
473 static int pkey_set(struct task_struct *target, const struct user_regset *regset,
474 		    unsigned int pos, unsigned int count, const void *kbuf,
475 		    const void __user *ubuf)
476 {
477 	u64 new_amr;
478 	int ret;
479 
480 	if (!arch_pkeys_enabled())
481 		return -ENODEV;
482 
483 	/* Only the AMR can be set from userspace */
484 	if (pos != 0 || count != sizeof(new_amr))
485 		return -EINVAL;
486 
487 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
488 				 &new_amr, 0, sizeof(new_amr));
489 	if (ret)
490 		return ret;
491 
492 	/*
493 	 * UAMOR determines which bits of the AMR can be set from userspace.
494 	 * UAMOR value 0b11 indicates that the AMR value can be modified
495 	 * from userspace. If the kernel is using a specific key, we avoid
496 	 * userspace modifying the AMR value for that key by masking them
497 	 * via UAMOR 0b00.
498 	 *
499 	 * Pick the AMR values for the keys that kernel is using. This
500 	 * will be indicated by the ~default_uamor bits.
501 	 */
502 	target->thread.regs->amr = (new_amr & default_uamor) |
503 		(target->thread.regs->amr & ~default_uamor);
504 
505 	return 0;
506 }
507 #endif /* CONFIG_PPC_MEM_KEYS */
508 
509 static const struct user_regset native_regsets[] = {
510 	[REGSET_GPR] = {
511 		.core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
512 		.size = sizeof(long), .align = sizeof(long),
513 		.regset_get = gpr_get, .set = gpr_set
514 	},
515 #ifdef CONFIG_PPC_FPU_REGS
516 	[REGSET_FPR] = {
517 		.core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
518 		.size = sizeof(double), .align = sizeof(double),
519 		.regset_get = fpr_get, .set = fpr_set
520 	},
521 #endif
522 #ifdef CONFIG_ALTIVEC
523 	[REGSET_VMX] = {
524 		.core_note_type = NT_PPC_VMX, .n = 34,
525 		.size = sizeof(vector128), .align = sizeof(vector128),
526 		.active = vr_active, .regset_get = vr_get, .set = vr_set
527 	},
528 #endif
529 #ifdef CONFIG_VSX
530 	[REGSET_VSX] = {
531 		.core_note_type = NT_PPC_VSX, .n = 32,
532 		.size = sizeof(double), .align = sizeof(double),
533 		.active = vsr_active, .regset_get = vsr_get, .set = vsr_set
534 	},
535 #endif
536 #ifdef CONFIG_SPE
537 	[REGSET_SPE] = {
538 		.core_note_type = NT_PPC_SPE, .n = 35,
539 		.size = sizeof(u32), .align = sizeof(u32),
540 		.active = evr_active, .regset_get = evr_get, .set = evr_set
541 	},
542 #endif
543 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
544 	[REGSET_TM_CGPR] = {
545 		.core_note_type = NT_PPC_TM_CGPR, .n = ELF_NGREG,
546 		.size = sizeof(long), .align = sizeof(long),
547 		.active = tm_cgpr_active, .regset_get = tm_cgpr_get, .set = tm_cgpr_set
548 	},
549 	[REGSET_TM_CFPR] = {
550 		.core_note_type = NT_PPC_TM_CFPR, .n = ELF_NFPREG,
551 		.size = sizeof(double), .align = sizeof(double),
552 		.active = tm_cfpr_active, .regset_get = tm_cfpr_get, .set = tm_cfpr_set
553 	},
554 	[REGSET_TM_CVMX] = {
555 		.core_note_type = NT_PPC_TM_CVMX, .n = ELF_NVMX,
556 		.size = sizeof(vector128), .align = sizeof(vector128),
557 		.active = tm_cvmx_active, .regset_get = tm_cvmx_get, .set = tm_cvmx_set
558 	},
559 	[REGSET_TM_CVSX] = {
560 		.core_note_type = NT_PPC_TM_CVSX, .n = ELF_NVSX,
561 		.size = sizeof(double), .align = sizeof(double),
562 		.active = tm_cvsx_active, .regset_get = tm_cvsx_get, .set = tm_cvsx_set
563 	},
564 	[REGSET_TM_SPR] = {
565 		.core_note_type = NT_PPC_TM_SPR, .n = ELF_NTMSPRREG,
566 		.size = sizeof(u64), .align = sizeof(u64),
567 		.active = tm_spr_active, .regset_get = tm_spr_get, .set = tm_spr_set
568 	},
569 	[REGSET_TM_CTAR] = {
570 		.core_note_type = NT_PPC_TM_CTAR, .n = 1,
571 		.size = sizeof(u64), .align = sizeof(u64),
572 		.active = tm_tar_active, .regset_get = tm_tar_get, .set = tm_tar_set
573 	},
574 	[REGSET_TM_CPPR] = {
575 		.core_note_type = NT_PPC_TM_CPPR, .n = 1,
576 		.size = sizeof(u64), .align = sizeof(u64),
577 		.active = tm_ppr_active, .regset_get = tm_ppr_get, .set = tm_ppr_set
578 	},
579 	[REGSET_TM_CDSCR] = {
580 		.core_note_type = NT_PPC_TM_CDSCR, .n = 1,
581 		.size = sizeof(u64), .align = sizeof(u64),
582 		.active = tm_dscr_active, .regset_get = tm_dscr_get, .set = tm_dscr_set
583 	},
584 #endif
585 #ifdef CONFIG_PPC64
586 	[REGSET_PPR] = {
587 		.core_note_type = NT_PPC_PPR, .n = 1,
588 		.size = sizeof(u64), .align = sizeof(u64),
589 		.regset_get = ppr_get, .set = ppr_set
590 	},
591 	[REGSET_DSCR] = {
592 		.core_note_type = NT_PPC_DSCR, .n = 1,
593 		.size = sizeof(u64), .align = sizeof(u64),
594 		.regset_get = dscr_get, .set = dscr_set
595 	},
596 #endif
597 #ifdef CONFIG_PPC_BOOK3S_64
598 	[REGSET_TAR] = {
599 		.core_note_type = NT_PPC_TAR, .n = 1,
600 		.size = sizeof(u64), .align = sizeof(u64),
601 		.regset_get = tar_get, .set = tar_set
602 	},
603 	[REGSET_EBB] = {
604 		.core_note_type = NT_PPC_EBB, .n = ELF_NEBB,
605 		.size = sizeof(u64), .align = sizeof(u64),
606 		.active = ebb_active, .regset_get = ebb_get, .set = ebb_set
607 	},
608 	[REGSET_PMR] = {
609 		.core_note_type = NT_PPC_PMU, .n = ELF_NPMU,
610 		.size = sizeof(u64), .align = sizeof(u64),
611 		.active = pmu_active, .regset_get = pmu_get, .set = pmu_set
612 	},
613 #endif
614 #ifdef CONFIG_PPC_MEM_KEYS
615 	[REGSET_PKEY] = {
616 		.core_note_type = NT_PPC_PKEY, .n = ELF_NPKEY,
617 		.size = sizeof(u64), .align = sizeof(u64),
618 		.active = pkey_active, .regset_get = pkey_get, .set = pkey_set
619 	},
620 #endif
621 };
622 
623 const struct user_regset_view user_ppc_native_view = {
624 	.name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
625 	.regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
626 };
627 
628 #include <linux/compat.h>
629 
630 int gpr32_get_common(struct task_struct *target,
631 		     const struct user_regset *regset,
632 		     struct membuf to, unsigned long *regs)
633 {
634 	int i;
635 
636 	for (i = 0; i < PT_MSR; i++)
637 		membuf_store(&to, (u32)regs[i]);
638 	membuf_store(&to, (u32)get_user_msr(target));
639 	for (i++ ; i < PT_REGS_COUNT; i++)
640 		membuf_store(&to, (u32)regs[i]);
641 	return membuf_zero(&to, (ELF_NGREG - PT_REGS_COUNT) * sizeof(u32));
642 }
643 
644 int gpr32_set_common(struct task_struct *target,
645 		     const struct user_regset *regset,
646 		     unsigned int pos, unsigned int count,
647 		     const void *kbuf, const void __user *ubuf,
648 		     unsigned long *regs)
649 {
650 	const compat_ulong_t *k = kbuf;
651 	const compat_ulong_t __user *u = ubuf;
652 	compat_ulong_t reg;
653 
654 	if (!kbuf && !user_read_access_begin(u, count))
655 		return -EFAULT;
656 
657 	pos /= sizeof(reg);
658 	count /= sizeof(reg);
659 
660 	if (kbuf)
661 		for (; count > 0 && pos < PT_MSR; --count)
662 			regs[pos++] = *k++;
663 	else
664 		for (; count > 0 && pos < PT_MSR; --count) {
665 			unsafe_get_user(reg, u++, Efault);
666 			regs[pos++] = reg;
667 		}
668 
669 
670 	if (count > 0 && pos == PT_MSR) {
671 		if (kbuf)
672 			reg = *k++;
673 		else
674 			unsafe_get_user(reg, u++, Efault);
675 		set_user_msr(target, reg);
676 		++pos;
677 		--count;
678 	}
679 
680 	if (kbuf) {
681 		for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
682 			regs[pos++] = *k++;
683 		for (; count > 0 && pos < PT_TRAP; --count, ++pos)
684 			++k;
685 	} else {
686 		for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
687 			unsafe_get_user(reg, u++, Efault);
688 			regs[pos++] = reg;
689 		}
690 		for (; count > 0 && pos < PT_TRAP; --count, ++pos)
691 			unsafe_get_user(reg, u++, Efault);
692 	}
693 
694 	if (count > 0 && pos == PT_TRAP) {
695 		if (kbuf)
696 			reg = *k++;
697 		else
698 			unsafe_get_user(reg, u++, Efault);
699 		set_user_trap(target, reg);
700 		++pos;
701 		--count;
702 	}
703 	if (!kbuf)
704 		user_read_access_end();
705 
706 	kbuf = k;
707 	ubuf = u;
708 	pos *= sizeof(reg);
709 	count *= sizeof(reg);
710 	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
711 					 (PT_TRAP + 1) * sizeof(reg), -1);
712 
713 Efault:
714 	user_read_access_end();
715 	return -EFAULT;
716 }
717 
718 static int gpr32_get(struct task_struct *target,
719 		     const struct user_regset *regset,
720 		     struct membuf to)
721 {
722 	if (target->thread.regs == NULL)
723 		return -EIO;
724 
725 	return gpr32_get_common(target, regset, to,
726 			&target->thread.regs->gpr[0]);
727 }
728 
729 static int gpr32_set(struct task_struct *target,
730 		     const struct user_regset *regset,
731 		     unsigned int pos, unsigned int count,
732 		     const void *kbuf, const void __user *ubuf)
733 {
734 	if (target->thread.regs == NULL)
735 		return -EIO;
736 
737 	return gpr32_set_common(target, regset, pos, count, kbuf, ubuf,
738 			&target->thread.regs->gpr[0]);
739 }
740 
741 /*
742  * These are the regset flavors matching the CONFIG_PPC32 native set.
743  */
744 static const struct user_regset compat_regsets[] = {
745 	[REGSET_GPR] = {
746 		.core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
747 		.size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
748 		.regset_get = gpr32_get, .set = gpr32_set
749 	},
750 	[REGSET_FPR] = {
751 		.core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
752 		.size = sizeof(double), .align = sizeof(double),
753 		.regset_get = fpr_get, .set = fpr_set
754 	},
755 #ifdef CONFIG_ALTIVEC
756 	[REGSET_VMX] = {
757 		.core_note_type = NT_PPC_VMX, .n = 34,
758 		.size = sizeof(vector128), .align = sizeof(vector128),
759 		.active = vr_active, .regset_get = vr_get, .set = vr_set
760 	},
761 #endif
762 #ifdef CONFIG_SPE
763 	[REGSET_SPE] = {
764 		.core_note_type = NT_PPC_SPE, .n = 35,
765 		.size = sizeof(u32), .align = sizeof(u32),
766 		.active = evr_active, .regset_get = evr_get, .set = evr_set
767 	},
768 #endif
769 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
770 	[REGSET_TM_CGPR] = {
771 		.core_note_type = NT_PPC_TM_CGPR, .n = ELF_NGREG,
772 		.size = sizeof(long), .align = sizeof(long),
773 		.active = tm_cgpr_active,
774 		.regset_get = tm_cgpr32_get, .set = tm_cgpr32_set
775 	},
776 	[REGSET_TM_CFPR] = {
777 		.core_note_type = NT_PPC_TM_CFPR, .n = ELF_NFPREG,
778 		.size = sizeof(double), .align = sizeof(double),
779 		.active = tm_cfpr_active, .regset_get = tm_cfpr_get, .set = tm_cfpr_set
780 	},
781 	[REGSET_TM_CVMX] = {
782 		.core_note_type = NT_PPC_TM_CVMX, .n = ELF_NVMX,
783 		.size = sizeof(vector128), .align = sizeof(vector128),
784 		.active = tm_cvmx_active, .regset_get = tm_cvmx_get, .set = tm_cvmx_set
785 	},
786 	[REGSET_TM_CVSX] = {
787 		.core_note_type = NT_PPC_TM_CVSX, .n = ELF_NVSX,
788 		.size = sizeof(double), .align = sizeof(double),
789 		.active = tm_cvsx_active, .regset_get = tm_cvsx_get, .set = tm_cvsx_set
790 	},
791 	[REGSET_TM_SPR] = {
792 		.core_note_type = NT_PPC_TM_SPR, .n = ELF_NTMSPRREG,
793 		.size = sizeof(u64), .align = sizeof(u64),
794 		.active = tm_spr_active, .regset_get = tm_spr_get, .set = tm_spr_set
795 	},
796 	[REGSET_TM_CTAR] = {
797 		.core_note_type = NT_PPC_TM_CTAR, .n = 1,
798 		.size = sizeof(u64), .align = sizeof(u64),
799 		.active = tm_tar_active, .regset_get = tm_tar_get, .set = tm_tar_set
800 	},
801 	[REGSET_TM_CPPR] = {
802 		.core_note_type = NT_PPC_TM_CPPR, .n = 1,
803 		.size = sizeof(u64), .align = sizeof(u64),
804 		.active = tm_ppr_active, .regset_get = tm_ppr_get, .set = tm_ppr_set
805 	},
806 	[REGSET_TM_CDSCR] = {
807 		.core_note_type = NT_PPC_TM_CDSCR, .n = 1,
808 		.size = sizeof(u64), .align = sizeof(u64),
809 		.active = tm_dscr_active, .regset_get = tm_dscr_get, .set = tm_dscr_set
810 	},
811 #endif
812 #ifdef CONFIG_PPC64
813 	[REGSET_PPR] = {
814 		.core_note_type = NT_PPC_PPR, .n = 1,
815 		.size = sizeof(u64), .align = sizeof(u64),
816 		.regset_get = ppr_get, .set = ppr_set
817 	},
818 	[REGSET_DSCR] = {
819 		.core_note_type = NT_PPC_DSCR, .n = 1,
820 		.size = sizeof(u64), .align = sizeof(u64),
821 		.regset_get = dscr_get, .set = dscr_set
822 	},
823 #endif
824 #ifdef CONFIG_PPC_BOOK3S_64
825 	[REGSET_TAR] = {
826 		.core_note_type = NT_PPC_TAR, .n = 1,
827 		.size = sizeof(u64), .align = sizeof(u64),
828 		.regset_get = tar_get, .set = tar_set
829 	},
830 	[REGSET_EBB] = {
831 		.core_note_type = NT_PPC_EBB, .n = ELF_NEBB,
832 		.size = sizeof(u64), .align = sizeof(u64),
833 		.active = ebb_active, .regset_get = ebb_get, .set = ebb_set
834 	},
835 #endif
836 };
837 
838 static const struct user_regset_view user_ppc_compat_view = {
839 	.name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
840 	.regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
841 };
842 
843 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
844 {
845 	if (IS_ENABLED(CONFIG_PPC64) && test_tsk_thread_flag(task, TIF_32BIT))
846 		return &user_ppc_compat_view;
847 	return &user_ppc_native_view;
848 }
849