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 */
regs_query_register_offset(const char * name)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 */
regs_query_register_name(unsigned int offset)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
get_user_msr(struct task_struct * task)109 static unsigned long get_user_msr(struct task_struct *task)
110 {
111 return task->thread.regs->msr | task->thread.fpexc_mode;
112 }
113
set_user_msr(struct task_struct * task,unsigned long msr)114 static 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
get_user_dscr(struct task_struct * task,unsigned long * data)122 static int get_user_dscr(struct task_struct *task, unsigned long *data)
123 {
124 *data = task->thread.dscr;
125 return 0;
126 }
127
set_user_dscr(struct task_struct * task,unsigned long dscr)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
get_user_dscr(struct task_struct * task,unsigned long * data)135 static int get_user_dscr(struct task_struct *task, unsigned long *data)
136 {
137 return -EIO;
138 }
139
set_user_dscr(struct task_struct * task,unsigned long dscr)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 */
set_user_trap(struct task_struct * task,unsigned long trap)150 static 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 */
ptrace_get_reg(struct task_struct * task,int regno,unsigned long * data)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 */
ptrace_put_reg(struct task_struct * task,int regno,unsigned long data)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
gpr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)217 static int gpr_get(struct task_struct *target, const struct user_regset *regset,
218 struct membuf to)
219 {
220 int i;
221
222 if (target->thread.regs == NULL)
223 return -EIO;
224
225 if (!FULL_REGS(target->thread.regs)) {
226 /* We have a partial register set. Fill 14-31 with bogus values */
227 for (i = 14; i < 32; i++)
228 target->thread.regs->gpr[i] = NV_REG_POISON;
229 }
230
231 membuf_write(&to, target->thread.regs, offsetof(struct pt_regs, msr));
232 membuf_store(&to, get_user_msr(target));
233
234 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
235 offsetof(struct pt_regs, msr) + sizeof(long));
236
237 membuf_write(&to, &target->thread.regs->orig_gpr3,
238 sizeof(struct user_pt_regs) -
239 offsetof(struct pt_regs, orig_gpr3));
240 return membuf_zero(&to, ELF_NGREG * sizeof(unsigned long) -
241 sizeof(struct user_pt_regs));
242 }
243
gpr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)244 static int gpr_set(struct task_struct *target, const struct user_regset *regset,
245 unsigned int pos, unsigned int count, const void *kbuf,
246 const void __user *ubuf)
247 {
248 unsigned long reg;
249 int ret;
250
251 if (target->thread.regs == NULL)
252 return -EIO;
253
254 CHECK_FULL_REGS(target->thread.regs);
255
256 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
257 target->thread.regs,
258 0, PT_MSR * sizeof(reg));
259
260 if (!ret && count > 0) {
261 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®,
262 PT_MSR * sizeof(reg),
263 (PT_MSR + 1) * sizeof(reg));
264 if (!ret)
265 ret = set_user_msr(target, reg);
266 }
267
268 BUILD_BUG_ON(offsetof(struct pt_regs, orig_gpr3) !=
269 offsetof(struct pt_regs, msr) + sizeof(long));
270
271 if (!ret)
272 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
273 &target->thread.regs->orig_gpr3,
274 PT_ORIG_R3 * sizeof(reg),
275 (PT_MAX_PUT_REG + 1) * sizeof(reg));
276
277 if (PT_MAX_PUT_REG + 1 < PT_TRAP && !ret)
278 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
279 (PT_MAX_PUT_REG + 1) * sizeof(reg),
280 PT_TRAP * sizeof(reg));
281
282 if (!ret && count > 0) {
283 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®,
284 PT_TRAP * sizeof(reg),
285 (PT_TRAP + 1) * sizeof(reg));
286 if (!ret)
287 ret = set_user_trap(target, reg);
288 }
289
290 if (!ret)
291 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
292 (PT_TRAP + 1) * sizeof(reg), -1);
293
294 return ret;
295 }
296
297 #ifdef CONFIG_PPC64
ppr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)298 static int ppr_get(struct task_struct *target, const struct user_regset *regset,
299 struct membuf to)
300 {
301 return membuf_write(&to, &target->thread.regs->ppr, sizeof(u64));
302 }
303
ppr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)304 static int ppr_set(struct task_struct *target, const struct user_regset *regset,
305 unsigned int pos, unsigned int count, const void *kbuf,
306 const void __user *ubuf)
307 {
308 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
309 &target->thread.regs->ppr, 0, sizeof(u64));
310 }
311
dscr_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)312 static int dscr_get(struct task_struct *target, const struct user_regset *regset,
313 struct membuf to)
314 {
315 return membuf_write(&to, &target->thread.dscr, sizeof(u64));
316 }
dscr_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)317 static int dscr_set(struct task_struct *target, const struct user_regset *regset,
318 unsigned int pos, unsigned int count, const void *kbuf,
319 const void __user *ubuf)
320 {
321 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
322 &target->thread.dscr, 0, sizeof(u64));
323 }
324 #endif
325 #ifdef CONFIG_PPC_BOOK3S_64
tar_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)326 static int tar_get(struct task_struct *target, const struct user_regset *regset,
327 struct membuf to)
328 {
329 return membuf_write(&to, &target->thread.tar, sizeof(u64));
330 }
tar_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)331 static int tar_set(struct task_struct *target, const struct user_regset *regset,
332 unsigned int pos, unsigned int count, const void *kbuf,
333 const void __user *ubuf)
334 {
335 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
336 &target->thread.tar, 0, sizeof(u64));
337 }
338
ebb_active(struct task_struct * target,const struct user_regset * regset)339 static int ebb_active(struct task_struct *target, const struct user_regset *regset)
340 {
341 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
342 return -ENODEV;
343
344 if (target->thread.used_ebb)
345 return regset->n;
346
347 return 0;
348 }
349
ebb_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)350 static int ebb_get(struct task_struct *target, const struct user_regset *regset,
351 struct membuf to)
352 {
353 /* Build tests */
354 BUILD_BUG_ON(TSO(ebbrr) + sizeof(unsigned long) != TSO(ebbhr));
355 BUILD_BUG_ON(TSO(ebbhr) + sizeof(unsigned long) != TSO(bescr));
356
357 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
358 return -ENODEV;
359
360 if (!target->thread.used_ebb)
361 return -ENODATA;
362
363 return membuf_write(&to, &target->thread.ebbrr, 3 * sizeof(unsigned long));
364 }
365
ebb_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)366 static int ebb_set(struct task_struct *target, const struct user_regset *regset,
367 unsigned int pos, unsigned int count, const void *kbuf,
368 const void __user *ubuf)
369 {
370 int ret = 0;
371
372 /* Build tests */
373 BUILD_BUG_ON(TSO(ebbrr) + sizeof(unsigned long) != TSO(ebbhr));
374 BUILD_BUG_ON(TSO(ebbhr) + sizeof(unsigned long) != TSO(bescr));
375
376 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
377 return -ENODEV;
378
379 if (target->thread.used_ebb)
380 return -ENODATA;
381
382 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &target->thread.ebbrr,
383 0, sizeof(unsigned long));
384
385 if (!ret)
386 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
387 &target->thread.ebbhr, sizeof(unsigned long),
388 2 * sizeof(unsigned long));
389
390 if (!ret)
391 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
392 &target->thread.bescr, 2 * sizeof(unsigned long),
393 3 * sizeof(unsigned long));
394
395 return ret;
396 }
pmu_active(struct task_struct * target,const struct user_regset * regset)397 static int pmu_active(struct task_struct *target, const struct user_regset *regset)
398 {
399 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
400 return -ENODEV;
401
402 return regset->n;
403 }
404
pmu_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)405 static int pmu_get(struct task_struct *target, const struct user_regset *regset,
406 struct membuf to)
407 {
408 /* Build tests */
409 BUILD_BUG_ON(TSO(siar) + sizeof(unsigned long) != TSO(sdar));
410 BUILD_BUG_ON(TSO(sdar) + sizeof(unsigned long) != TSO(sier));
411 BUILD_BUG_ON(TSO(sier) + sizeof(unsigned long) != TSO(mmcr2));
412 BUILD_BUG_ON(TSO(mmcr2) + sizeof(unsigned long) != TSO(mmcr0));
413
414 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
415 return -ENODEV;
416
417 return membuf_write(&to, &target->thread.siar, 5 * sizeof(unsigned long));
418 }
419
pmu_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)420 static int pmu_set(struct task_struct *target, const struct user_regset *regset,
421 unsigned int pos, unsigned int count, const void *kbuf,
422 const void __user *ubuf)
423 {
424 int ret = 0;
425
426 /* Build tests */
427 BUILD_BUG_ON(TSO(siar) + sizeof(unsigned long) != TSO(sdar));
428 BUILD_BUG_ON(TSO(sdar) + sizeof(unsigned long) != TSO(sier));
429 BUILD_BUG_ON(TSO(sier) + sizeof(unsigned long) != TSO(mmcr2));
430 BUILD_BUG_ON(TSO(mmcr2) + sizeof(unsigned long) != TSO(mmcr0));
431
432 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
433 return -ENODEV;
434
435 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &target->thread.siar,
436 0, sizeof(unsigned long));
437
438 if (!ret)
439 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
440 &target->thread.sdar, sizeof(unsigned long),
441 2 * sizeof(unsigned long));
442
443 if (!ret)
444 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
445 &target->thread.sier, 2 * sizeof(unsigned long),
446 3 * sizeof(unsigned long));
447
448 if (!ret)
449 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
450 &target->thread.mmcr2, 3 * sizeof(unsigned long),
451 4 * sizeof(unsigned long));
452
453 if (!ret)
454 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
455 &target->thread.mmcr0, 4 * sizeof(unsigned long),
456 5 * sizeof(unsigned long));
457 return ret;
458 }
459 #endif
460
461 #ifdef CONFIG_PPC_MEM_KEYS
pkey_active(struct task_struct * target,const struct user_regset * regset)462 static int pkey_active(struct task_struct *target, const struct user_regset *regset)
463 {
464 if (!arch_pkeys_enabled())
465 return -ENODEV;
466
467 return regset->n;
468 }
469
pkey_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)470 static int pkey_get(struct task_struct *target, const struct user_regset *regset,
471 struct membuf to)
472 {
473 BUILD_BUG_ON(TSO(amr) + sizeof(unsigned long) != TSO(iamr));
474
475 if (!arch_pkeys_enabled())
476 return -ENODEV;
477
478 membuf_write(&to, &target->thread.amr, 2 * sizeof(unsigned long));
479 return membuf_store(&to, default_uamor);
480 }
481
pkey_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)482 static int pkey_set(struct task_struct *target, const struct user_regset *regset,
483 unsigned int pos, unsigned int count, const void *kbuf,
484 const void __user *ubuf)
485 {
486 u64 new_amr;
487 int ret;
488
489 if (!arch_pkeys_enabled())
490 return -ENODEV;
491
492 /* Only the AMR can be set from userspace */
493 if (pos != 0 || count != sizeof(new_amr))
494 return -EINVAL;
495
496 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
497 &new_amr, 0, sizeof(new_amr));
498 if (ret)
499 return ret;
500
501 /*
502 * UAMOR determines which bits of the AMR can be set from userspace.
503 * UAMOR value 0b11 indicates that the AMR value can be modified
504 * from userspace. If the kernel is using a specific key, we avoid
505 * userspace modifying the AMR value for that key by masking them
506 * via UAMOR 0b00.
507 *
508 * Pick the AMR values for the keys that kernel is using. This
509 * will be indicated by the ~default_uamor bits.
510 */
511 target->thread.amr = (new_amr & default_uamor) | (target->thread.amr & ~default_uamor);
512
513 return 0;
514 }
515 #endif /* CONFIG_PPC_MEM_KEYS */
516
517 static const struct user_regset native_regsets[] = {
518 [REGSET_GPR] = {
519 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
520 .size = sizeof(long), .align = sizeof(long),
521 .regset_get = gpr_get, .set = gpr_set
522 },
523 [REGSET_FPR] = {
524 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
525 .size = sizeof(double), .align = sizeof(double),
526 .regset_get = fpr_get, .set = fpr_set
527 },
528 #ifdef CONFIG_ALTIVEC
529 [REGSET_VMX] = {
530 .core_note_type = NT_PPC_VMX, .n = 34,
531 .size = sizeof(vector128), .align = sizeof(vector128),
532 .active = vr_active, .regset_get = vr_get, .set = vr_set
533 },
534 #endif
535 #ifdef CONFIG_VSX
536 [REGSET_VSX] = {
537 .core_note_type = NT_PPC_VSX, .n = 32,
538 .size = sizeof(double), .align = sizeof(double),
539 .active = vsr_active, .regset_get = vsr_get, .set = vsr_set
540 },
541 #endif
542 #ifdef CONFIG_SPE
543 [REGSET_SPE] = {
544 .core_note_type = NT_PPC_SPE, .n = 35,
545 .size = sizeof(u32), .align = sizeof(u32),
546 .active = evr_active, .regset_get = evr_get, .set = evr_set
547 },
548 #endif
549 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
550 [REGSET_TM_CGPR] = {
551 .core_note_type = NT_PPC_TM_CGPR, .n = ELF_NGREG,
552 .size = sizeof(long), .align = sizeof(long),
553 .active = tm_cgpr_active, .regset_get = tm_cgpr_get, .set = tm_cgpr_set
554 },
555 [REGSET_TM_CFPR] = {
556 .core_note_type = NT_PPC_TM_CFPR, .n = ELF_NFPREG,
557 .size = sizeof(double), .align = sizeof(double),
558 .active = tm_cfpr_active, .regset_get = tm_cfpr_get, .set = tm_cfpr_set
559 },
560 [REGSET_TM_CVMX] = {
561 .core_note_type = NT_PPC_TM_CVMX, .n = ELF_NVMX,
562 .size = sizeof(vector128), .align = sizeof(vector128),
563 .active = tm_cvmx_active, .regset_get = tm_cvmx_get, .set = tm_cvmx_set
564 },
565 [REGSET_TM_CVSX] = {
566 .core_note_type = NT_PPC_TM_CVSX, .n = ELF_NVSX,
567 .size = sizeof(double), .align = sizeof(double),
568 .active = tm_cvsx_active, .regset_get = tm_cvsx_get, .set = tm_cvsx_set
569 },
570 [REGSET_TM_SPR] = {
571 .core_note_type = NT_PPC_TM_SPR, .n = ELF_NTMSPRREG,
572 .size = sizeof(u64), .align = sizeof(u64),
573 .active = tm_spr_active, .regset_get = tm_spr_get, .set = tm_spr_set
574 },
575 [REGSET_TM_CTAR] = {
576 .core_note_type = NT_PPC_TM_CTAR, .n = 1,
577 .size = sizeof(u64), .align = sizeof(u64),
578 .active = tm_tar_active, .regset_get = tm_tar_get, .set = tm_tar_set
579 },
580 [REGSET_TM_CPPR] = {
581 .core_note_type = NT_PPC_TM_CPPR, .n = 1,
582 .size = sizeof(u64), .align = sizeof(u64),
583 .active = tm_ppr_active, .regset_get = tm_ppr_get, .set = tm_ppr_set
584 },
585 [REGSET_TM_CDSCR] = {
586 .core_note_type = NT_PPC_TM_CDSCR, .n = 1,
587 .size = sizeof(u64), .align = sizeof(u64),
588 .active = tm_dscr_active, .regset_get = tm_dscr_get, .set = tm_dscr_set
589 },
590 #endif
591 #ifdef CONFIG_PPC64
592 [REGSET_PPR] = {
593 .core_note_type = NT_PPC_PPR, .n = 1,
594 .size = sizeof(u64), .align = sizeof(u64),
595 .regset_get = ppr_get, .set = ppr_set
596 },
597 [REGSET_DSCR] = {
598 .core_note_type = NT_PPC_DSCR, .n = 1,
599 .size = sizeof(u64), .align = sizeof(u64),
600 .regset_get = dscr_get, .set = dscr_set
601 },
602 #endif
603 #ifdef CONFIG_PPC_BOOK3S_64
604 [REGSET_TAR] = {
605 .core_note_type = NT_PPC_TAR, .n = 1,
606 .size = sizeof(u64), .align = sizeof(u64),
607 .regset_get = tar_get, .set = tar_set
608 },
609 [REGSET_EBB] = {
610 .core_note_type = NT_PPC_EBB, .n = ELF_NEBB,
611 .size = sizeof(u64), .align = sizeof(u64),
612 .active = ebb_active, .regset_get = ebb_get, .set = ebb_set
613 },
614 [REGSET_PMR] = {
615 .core_note_type = NT_PPC_PMU, .n = ELF_NPMU,
616 .size = sizeof(u64), .align = sizeof(u64),
617 .active = pmu_active, .regset_get = pmu_get, .set = pmu_set
618 },
619 #endif
620 #ifdef CONFIG_PPC_MEM_KEYS
621 [REGSET_PKEY] = {
622 .core_note_type = NT_PPC_PKEY, .n = ELF_NPKEY,
623 .size = sizeof(u64), .align = sizeof(u64),
624 .active = pkey_active, .regset_get = pkey_get, .set = pkey_set
625 },
626 #endif
627 };
628
629 const struct user_regset_view user_ppc_native_view = {
630 .name = UTS_MACHINE, .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
631 .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets)
632 };
633
634 #include <linux/compat.h>
635
gpr32_get_common(struct task_struct * target,const struct user_regset * regset,struct membuf to,unsigned long * regs)636 int gpr32_get_common(struct task_struct *target,
637 const struct user_regset *regset,
638 struct membuf to, unsigned long *regs)
639 {
640 int i;
641
642 for (i = 0; i < PT_MSR; i++)
643 membuf_store(&to, (u32)regs[i]);
644 membuf_store(&to, (u32)get_user_msr(target));
645 for (i++ ; i < PT_REGS_COUNT; i++)
646 membuf_store(&to, (u32)regs[i]);
647 return membuf_zero(&to, (ELF_NGREG - PT_REGS_COUNT) * sizeof(u32));
648 }
649
gpr32_set_common(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf,unsigned long * regs)650 int gpr32_set_common(struct task_struct *target,
651 const struct user_regset *regset,
652 unsigned int pos, unsigned int count,
653 const void *kbuf, const void __user *ubuf,
654 unsigned long *regs)
655 {
656 const compat_ulong_t *k = kbuf;
657 const compat_ulong_t __user *u = ubuf;
658 compat_ulong_t reg;
659
660 pos /= sizeof(reg);
661 count /= sizeof(reg);
662
663 if (kbuf)
664 for (; count > 0 && pos < PT_MSR; --count)
665 regs[pos++] = *k++;
666 else
667 for (; count > 0 && pos < PT_MSR; --count) {
668 if (__get_user(reg, u++))
669 return -EFAULT;
670 regs[pos++] = reg;
671 }
672
673
674 if (count > 0 && pos == PT_MSR) {
675 if (kbuf)
676 reg = *k++;
677 else if (__get_user(reg, u++))
678 return -EFAULT;
679 set_user_msr(target, reg);
680 ++pos;
681 --count;
682 }
683
684 if (kbuf) {
685 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count)
686 regs[pos++] = *k++;
687 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
688 ++k;
689 } else {
690 for (; count > 0 && pos <= PT_MAX_PUT_REG; --count) {
691 if (__get_user(reg, u++))
692 return -EFAULT;
693 regs[pos++] = reg;
694 }
695 for (; count > 0 && pos < PT_TRAP; --count, ++pos)
696 if (__get_user(reg, u++))
697 return -EFAULT;
698 }
699
700 if (count > 0 && pos == PT_TRAP) {
701 if (kbuf)
702 reg = *k++;
703 else if (__get_user(reg, u++))
704 return -EFAULT;
705 set_user_trap(target, reg);
706 ++pos;
707 --count;
708 }
709
710 kbuf = k;
711 ubuf = u;
712 pos *= sizeof(reg);
713 count *= sizeof(reg);
714 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
715 (PT_TRAP + 1) * sizeof(reg), -1);
716 }
717
gpr32_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)718 static int gpr32_get(struct task_struct *target,
719 const struct user_regset *regset,
720 struct membuf to)
721 {
722 int i;
723
724 if (target->thread.regs == NULL)
725 return -EIO;
726
727 if (!FULL_REGS(target->thread.regs)) {
728 /*
729 * We have a partial register set.
730 * Fill 14-31 with bogus values.
731 */
732 for (i = 14; i < 32; i++)
733 target->thread.regs->gpr[i] = NV_REG_POISON;
734 }
735 return gpr32_get_common(target, regset, to,
736 &target->thread.regs->gpr[0]);
737 }
738
gpr32_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)739 static int gpr32_set(struct task_struct *target,
740 const struct user_regset *regset,
741 unsigned int pos, unsigned int count,
742 const void *kbuf, const void __user *ubuf)
743 {
744 if (target->thread.regs == NULL)
745 return -EIO;
746
747 CHECK_FULL_REGS(target->thread.regs);
748 return gpr32_set_common(target, regset, pos, count, kbuf, ubuf,
749 &target->thread.regs->gpr[0]);
750 }
751
752 /*
753 * These are the regset flavors matching the CONFIG_PPC32 native set.
754 */
755 static const struct user_regset compat_regsets[] = {
756 [REGSET_GPR] = {
757 .core_note_type = NT_PRSTATUS, .n = ELF_NGREG,
758 .size = sizeof(compat_long_t), .align = sizeof(compat_long_t),
759 .regset_get = gpr32_get, .set = gpr32_set
760 },
761 [REGSET_FPR] = {
762 .core_note_type = NT_PRFPREG, .n = ELF_NFPREG,
763 .size = sizeof(double), .align = sizeof(double),
764 .regset_get = fpr_get, .set = fpr_set
765 },
766 #ifdef CONFIG_ALTIVEC
767 [REGSET_VMX] = {
768 .core_note_type = NT_PPC_VMX, .n = 34,
769 .size = sizeof(vector128), .align = sizeof(vector128),
770 .active = vr_active, .regset_get = vr_get, .set = vr_set
771 },
772 #endif
773 #ifdef CONFIG_SPE
774 [REGSET_SPE] = {
775 .core_note_type = NT_PPC_SPE, .n = 35,
776 .size = sizeof(u32), .align = sizeof(u32),
777 .active = evr_active, .regset_get = evr_get, .set = evr_set
778 },
779 #endif
780 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
781 [REGSET_TM_CGPR] = {
782 .core_note_type = NT_PPC_TM_CGPR, .n = ELF_NGREG,
783 .size = sizeof(long), .align = sizeof(long),
784 .active = tm_cgpr_active,
785 .regset_get = tm_cgpr32_get, .set = tm_cgpr32_set
786 },
787 [REGSET_TM_CFPR] = {
788 .core_note_type = NT_PPC_TM_CFPR, .n = ELF_NFPREG,
789 .size = sizeof(double), .align = sizeof(double),
790 .active = tm_cfpr_active, .regset_get = tm_cfpr_get, .set = tm_cfpr_set
791 },
792 [REGSET_TM_CVMX] = {
793 .core_note_type = NT_PPC_TM_CVMX, .n = ELF_NVMX,
794 .size = sizeof(vector128), .align = sizeof(vector128),
795 .active = tm_cvmx_active, .regset_get = tm_cvmx_get, .set = tm_cvmx_set
796 },
797 [REGSET_TM_CVSX] = {
798 .core_note_type = NT_PPC_TM_CVSX, .n = ELF_NVSX,
799 .size = sizeof(double), .align = sizeof(double),
800 .active = tm_cvsx_active, .regset_get = tm_cvsx_get, .set = tm_cvsx_set
801 },
802 [REGSET_TM_SPR] = {
803 .core_note_type = NT_PPC_TM_SPR, .n = ELF_NTMSPRREG,
804 .size = sizeof(u64), .align = sizeof(u64),
805 .active = tm_spr_active, .regset_get = tm_spr_get, .set = tm_spr_set
806 },
807 [REGSET_TM_CTAR] = {
808 .core_note_type = NT_PPC_TM_CTAR, .n = 1,
809 .size = sizeof(u64), .align = sizeof(u64),
810 .active = tm_tar_active, .regset_get = tm_tar_get, .set = tm_tar_set
811 },
812 [REGSET_TM_CPPR] = {
813 .core_note_type = NT_PPC_TM_CPPR, .n = 1,
814 .size = sizeof(u64), .align = sizeof(u64),
815 .active = tm_ppr_active, .regset_get = tm_ppr_get, .set = tm_ppr_set
816 },
817 [REGSET_TM_CDSCR] = {
818 .core_note_type = NT_PPC_TM_CDSCR, .n = 1,
819 .size = sizeof(u64), .align = sizeof(u64),
820 .active = tm_dscr_active, .regset_get = tm_dscr_get, .set = tm_dscr_set
821 },
822 #endif
823 #ifdef CONFIG_PPC64
824 [REGSET_PPR] = {
825 .core_note_type = NT_PPC_PPR, .n = 1,
826 .size = sizeof(u64), .align = sizeof(u64),
827 .regset_get = ppr_get, .set = ppr_set
828 },
829 [REGSET_DSCR] = {
830 .core_note_type = NT_PPC_DSCR, .n = 1,
831 .size = sizeof(u64), .align = sizeof(u64),
832 .regset_get = dscr_get, .set = dscr_set
833 },
834 #endif
835 #ifdef CONFIG_PPC_BOOK3S_64
836 [REGSET_TAR] = {
837 .core_note_type = NT_PPC_TAR, .n = 1,
838 .size = sizeof(u64), .align = sizeof(u64),
839 .regset_get = tar_get, .set = tar_set
840 },
841 [REGSET_EBB] = {
842 .core_note_type = NT_PPC_EBB, .n = ELF_NEBB,
843 .size = sizeof(u64), .align = sizeof(u64),
844 .active = ebb_active, .regset_get = ebb_get, .set = ebb_set
845 },
846 #endif
847 };
848
849 static const struct user_regset_view user_ppc_compat_view = {
850 .name = "ppc", .e_machine = EM_PPC, .ei_osabi = ELF_OSABI,
851 .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets)
852 };
853
task_user_regset_view(struct task_struct * task)854 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
855 {
856 if (IS_ENABLED(CONFIG_PPC64) && test_tsk_thread_flag(task, TIF_32BIT))
857 return &user_ppc_compat_view;
858 return &user_ppc_native_view;
859 }
860