1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/compat.h>
22#include <linux/signal.h>
23#include <linux/syscalls.h>
24#include <linux/ratelimit.h>
25
26#include <asm/esr.h>
27#include <asm/fpsimd.h>
28#include <asm/signal32.h>
29#include <asm/traps.h>
30#include <linux/uaccess.h>
31#include <asm/unistd.h>
32
33struct compat_sigcontext {
34
35 compat_ulong_t trap_no;
36 compat_ulong_t error_code;
37
38 compat_ulong_t oldmask;
39 compat_ulong_t arm_r0;
40 compat_ulong_t arm_r1;
41 compat_ulong_t arm_r2;
42 compat_ulong_t arm_r3;
43 compat_ulong_t arm_r4;
44 compat_ulong_t arm_r5;
45 compat_ulong_t arm_r6;
46 compat_ulong_t arm_r7;
47 compat_ulong_t arm_r8;
48 compat_ulong_t arm_r9;
49 compat_ulong_t arm_r10;
50 compat_ulong_t arm_fp;
51 compat_ulong_t arm_ip;
52 compat_ulong_t arm_sp;
53 compat_ulong_t arm_lr;
54 compat_ulong_t arm_pc;
55 compat_ulong_t arm_cpsr;
56 compat_ulong_t fault_address;
57};
58
59struct compat_ucontext {
60 compat_ulong_t uc_flags;
61 compat_uptr_t uc_link;
62 compat_stack_t uc_stack;
63 struct compat_sigcontext uc_mcontext;
64 compat_sigset_t uc_sigmask;
65 int __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
66 compat_ulong_t uc_regspace[128] __attribute__((__aligned__(8)));
67};
68
69struct compat_vfp_sigframe {
70 compat_ulong_t magic;
71 compat_ulong_t size;
72 struct compat_user_vfp {
73 compat_u64 fpregs[32];
74 compat_ulong_t fpscr;
75 } ufp;
76 struct compat_user_vfp_exc {
77 compat_ulong_t fpexc;
78 compat_ulong_t fpinst;
79 compat_ulong_t fpinst2;
80 } ufp_exc;
81} __attribute__((__aligned__(8)));
82
83#define VFP_MAGIC 0x56465001
84#define VFP_STORAGE_SIZE sizeof(struct compat_vfp_sigframe)
85
86#define FSR_WRITE_SHIFT (11)
87
88struct compat_aux_sigframe {
89 struct compat_vfp_sigframe vfp;
90
91
92 unsigned long end_magic;
93} __attribute__((__aligned__(8)));
94
95struct compat_sigframe {
96 struct compat_ucontext uc;
97 compat_ulong_t retcode[2];
98};
99
100struct compat_rt_sigframe {
101 struct compat_siginfo info;
102 struct compat_sigframe sig;
103};
104
105#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
106
107static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
108{
109 compat_sigset_t cset;
110
111 cset.sig[0] = set->sig[0] & 0xffffffffull;
112 cset.sig[1] = set->sig[0] >> 32;
113
114 return copy_to_user(uset, &cset, sizeof(*uset));
115}
116
117static inline int get_sigset_t(sigset_t *set,
118 const compat_sigset_t __user *uset)
119{
120 compat_sigset_t s32;
121
122 if (copy_from_user(&s32, uset, sizeof(*uset)))
123 return -EFAULT;
124
125 set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
126 return 0;
127}
128
129
130
131
132
133
134
135
136
137union __fpsimd_vreg {
138 __uint128_t raw;
139 struct {
140#ifdef __AARCH64EB__
141 u64 hi;
142 u64 lo;
143#else
144 u64 lo;
145 u64 hi;
146#endif
147 };
148};
149
150static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
151{
152 struct user_fpsimd_state const *fpsimd =
153 ¤t->thread.uw.fpsimd_state;
154 compat_ulong_t magic = VFP_MAGIC;
155 compat_ulong_t size = VFP_STORAGE_SIZE;
156 compat_ulong_t fpscr, fpexc;
157 int i, err = 0;
158
159
160
161
162
163
164 fpsimd_signal_preserve_current_state();
165
166
167 __put_user_error(magic, &frame->magic, err);
168 __put_user_error(size, &frame->size, err);
169
170
171
172
173
174 for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
175 union __fpsimd_vreg vreg = {
176 .raw = fpsimd->vregs[i >> 1],
177 };
178
179 __put_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
180 __put_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
181 }
182
183
184 fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
185 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
186 __put_user_error(fpscr, &frame->ufp.fpscr, err);
187
188
189
190
191
192 fpexc = (1 << 30);
193 __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
194 __put_user_error(0, &frame->ufp_exc.fpinst, err);
195 __put_user_error(0, &frame->ufp_exc.fpinst2, err);
196
197 return err ? -EFAULT : 0;
198}
199
200static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
201{
202 struct user_fpsimd_state fpsimd;
203 compat_ulong_t magic = VFP_MAGIC;
204 compat_ulong_t size = VFP_STORAGE_SIZE;
205 compat_ulong_t fpscr;
206 int i, err = 0;
207
208 __get_user_error(magic, &frame->magic, err);
209 __get_user_error(size, &frame->size, err);
210
211 if (err)
212 return -EFAULT;
213 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
214 return -EINVAL;
215
216
217 for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
218 union __fpsimd_vreg vreg;
219
220 __get_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
221 __get_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
222 fpsimd.vregs[i >> 1] = vreg.raw;
223 }
224
225
226 __get_user_error(fpscr, &frame->ufp.fpscr, err);
227 fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
228 fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
229
230
231
232
233
234 if (!err)
235 fpsimd_update_current_state(&fpsimd);
236
237 return err ? -EFAULT : 0;
238}
239
240static int compat_restore_sigframe(struct pt_regs *regs,
241 struct compat_sigframe __user *sf)
242{
243 int err;
244 sigset_t set;
245 struct compat_aux_sigframe __user *aux;
246
247 err = get_sigset_t(&set, &sf->uc.uc_sigmask);
248 if (err == 0) {
249 sigdelsetmask(&set, ~_BLOCKABLE);
250 set_current_blocked(&set);
251 }
252
253 __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
254 __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
255 __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
256 __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
257 __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
258 __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
259 __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
260 __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
261 __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
262 __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
263 __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
264 __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
265 __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
266 __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
267 __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
268 __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
269 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
270
271
272
273
274 forget_syscall(regs);
275
276 err |= !valid_user_regs(®s->user_regs, current);
277
278 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
279 if (err == 0)
280 err |= compat_restore_vfp_context(&aux->vfp);
281
282 return err;
283}
284
285COMPAT_SYSCALL_DEFINE0(sigreturn)
286{
287 struct pt_regs *regs = current_pt_regs();
288 struct compat_sigframe __user *frame;
289
290
291 current->restart_block.fn = do_no_restart_syscall;
292
293
294
295
296
297
298 if (regs->compat_sp & 7)
299 goto badframe;
300
301 frame = (struct compat_sigframe __user *)regs->compat_sp;
302
303 if (!access_ok(frame, sizeof (*frame)))
304 goto badframe;
305
306 if (compat_restore_sigframe(regs, frame))
307 goto badframe;
308
309 return regs->regs[0];
310
311badframe:
312 arm64_notify_segfault(regs->compat_sp);
313 return 0;
314}
315
316COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
317{
318 struct pt_regs *regs = current_pt_regs();
319 struct compat_rt_sigframe __user *frame;
320
321
322 current->restart_block.fn = do_no_restart_syscall;
323
324
325
326
327
328
329 if (regs->compat_sp & 7)
330 goto badframe;
331
332 frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
333
334 if (!access_ok(frame, sizeof (*frame)))
335 goto badframe;
336
337 if (compat_restore_sigframe(regs, &frame->sig))
338 goto badframe;
339
340 if (compat_restore_altstack(&frame->sig.uc.uc_stack))
341 goto badframe;
342
343 return regs->regs[0];
344
345badframe:
346 arm64_notify_segfault(regs->compat_sp);
347 return 0;
348}
349
350static void __user *compat_get_sigframe(struct ksignal *ksig,
351 struct pt_regs *regs,
352 int framesize)
353{
354 compat_ulong_t sp = sigsp(regs->compat_sp, ksig);
355 void __user *frame;
356
357
358
359
360 frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
361
362
363
364
365 if (!access_ok(frame, framesize))
366 frame = NULL;
367
368 return frame;
369}
370
371static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
372 compat_ulong_t __user *rc, void __user *frame,
373 int usig)
374{
375 compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
376 compat_ulong_t retcode;
377 compat_ulong_t spsr = regs->pstate & ~(PSR_f | COMPAT_PSR_E_BIT);
378 int thumb;
379
380
381 thumb = handler & 1;
382
383 if (thumb)
384 spsr |= COMPAT_PSR_T_BIT;
385 else
386 spsr &= ~COMPAT_PSR_T_BIT;
387
388
389 spsr &= ~COMPAT_PSR_IT_MASK;
390
391
392 spsr |= COMPAT_PSR_ENDSTATE;
393
394 if (ka->sa.sa_flags & SA_RESTORER) {
395 retcode = ptr_to_compat(ka->sa.sa_restorer);
396 } else {
397
398 unsigned int idx = thumb << 1;
399
400 if (ka->sa.sa_flags & SA_SIGINFO)
401 idx += 3;
402
403 retcode = AARCH32_VECTORS_BASE +
404 AARCH32_KERN_SIGRET_CODE_OFFSET +
405 (idx << 2) + thumb;
406 }
407
408 regs->regs[0] = usig;
409 regs->compat_sp = ptr_to_compat(frame);
410 regs->compat_lr = retcode;
411 regs->pc = handler;
412 regs->pstate = spsr;
413}
414
415static int compat_setup_sigframe(struct compat_sigframe __user *sf,
416 struct pt_regs *regs, sigset_t *set)
417{
418 struct compat_aux_sigframe __user *aux;
419 int err = 0;
420
421 __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
422 __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
423 __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
424 __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
425 __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
426 __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
427 __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
428 __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
429 __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
430 __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
431 __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
432 __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
433 __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
434 __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
435 __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
436 __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
437 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
438
439 __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
440
441 __put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) <<
442 FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
443 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
444 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
445
446 err |= put_sigset_t(&sf->uc.uc_sigmask, set);
447
448 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
449
450 if (err == 0)
451 err |= compat_preserve_vfp_context(&aux->vfp);
452 __put_user_error(0, &aux->end_magic, err);
453
454 return err;
455}
456
457
458
459
460int compat_setup_rt_frame(int usig, struct ksignal *ksig,
461 sigset_t *set, struct pt_regs *regs)
462{
463 struct compat_rt_sigframe __user *frame;
464 int err = 0;
465
466 frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
467
468 if (!frame)
469 return 1;
470
471 err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
472
473 __put_user_error(0, &frame->sig.uc.uc_flags, err);
474 __put_user_error(0, &frame->sig.uc.uc_link, err);
475
476 err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
477
478 err |= compat_setup_sigframe(&frame->sig, regs, set);
479
480 if (err == 0) {
481 compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
482 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
483 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
484 }
485
486 return err;
487}
488
489int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
490 struct pt_regs *regs)
491{
492 struct compat_sigframe __user *frame;
493 int err = 0;
494
495 frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
496
497 if (!frame)
498 return 1;
499
500 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
501
502 err |= compat_setup_sigframe(frame, regs, set);
503 if (err == 0)
504 compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
505
506 return err;
507}
508
509void compat_setup_restart_syscall(struct pt_regs *regs)
510{
511 regs->regs[7] = __NR_compat_restart_syscall;
512}
513