blob: e0d09bf5b01b80a8ed637bc1f0dd51168f0a55a2 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Catalin Marinas2c020ed2012-03-05 11:49:31 +00002/*
3 * Based on arch/arm/kernel/signal.c
4 *
5 * Copyright (C) 1995-2009 Russell King
6 * Copyright (C) 2012 ARM Ltd.
Catalin Marinas2c020ed2012-03-05 11:49:31 +00007 */
8
Dave Martin94b07c12018-06-01 11:10:14 +01009#include <linux/cache.h>
AKASHI Takahirofd92d4a2014-04-30 10:51:32 +010010#include <linux/compat.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000011#include <linux/errno.h>
Dave Martin20987de2017-06-15 15:03:38 +010012#include <linux/kernel.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000013#include <linux/signal.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000014#include <linux/freezer.h>
Dave Martin47ccb022017-06-15 15:03:39 +010015#include <linux/stddef.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000016#include <linux/uaccess.h>
Dave Martin33f08262017-06-20 18:23:39 +010017#include <linux/sizes.h>
Dave Martinbb4891a2017-06-15 15:03:40 +010018#include <linux/string.h>
Eric W. Biederman03248ad2022-02-09 12:20:45 -060019#include <linux/resume_user_mode.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000020#include <linux/ratelimit.h>
Thomas Garniercf7de272017-06-14 18:12:03 -070021#include <linux/syscalls.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000022
James Morse8d667722017-11-02 12:12:37 +000023#include <asm/daifflags.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000024#include <asm/debug-monitors.h>
25#include <asm/elf.h>
26#include <asm/cacheflush.h>
27#include <asm/ucontext.h>
28#include <asm/unistd.h>
29#include <asm/fpsimd.h>
Dave Martin17c28952017-08-01 15:35:54 +010030#include <asm/ptrace.h>
Mark Rutlande30e8d42021-08-02 11:42:00 +010031#include <asm/syscall.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000032#include <asm/signal32.h>
Will Deaconf71016a2018-02-20 15:05:17 +000033#include <asm/traps.h>
Catalin Marinas2c020ed2012-03-05 11:49:31 +000034#include <asm/vdso.h>
35
36/*
37 * Do a signal return; undo the signal stack. These are aligned to 128-bit.
38 */
39struct rt_sigframe {
40 struct siginfo info;
41 struct ucontext uc;
Dave Martin20987de2017-06-15 15:03:38 +010042};
43
44struct frame_record {
Will Deacon304ef4e2012-11-23 12:34:13 +000045 u64 fp;
46 u64 lr;
Catalin Marinas2c020ed2012-03-05 11:49:31 +000047};
48
Dave Martin20987de2017-06-15 15:03:38 +010049struct rt_sigframe_user_layout {
50 struct rt_sigframe __user *sigframe;
51 struct frame_record __user *next_frame;
Dave Martinbb4891a2017-06-15 15:03:40 +010052
53 unsigned long size; /* size of allocated sigframe data */
54 unsigned long limit; /* largest allowed size */
55
56 unsigned long fpsimd_offset;
57 unsigned long esr_offset;
Dave Martin8cd969d2017-10-31 15:51:07 +000058 unsigned long sve_offset;
Mark Brown39782212022-04-19 12:22:27 +010059 unsigned long za_offset;
Dave Martin33f08262017-06-20 18:23:39 +010060 unsigned long extra_offset;
Dave Martinbb4891a2017-06-15 15:03:40 +010061 unsigned long end_offset;
Dave Martin20987de2017-06-15 15:03:38 +010062};
63
Dave Martin33f08262017-06-20 18:23:39 +010064#define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
65#define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
66#define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
67
Dave Martinbb4891a2017-06-15 15:03:40 +010068static void init_user_layout(struct rt_sigframe_user_layout *user)
69{
Dave Martin33f08262017-06-20 18:23:39 +010070 const size_t reserved_size =
71 sizeof(user->sigframe->uc.uc_mcontext.__reserved);
72
Dave Martinbb4891a2017-06-15 15:03:40 +010073 memset(user, 0, sizeof(*user));
74 user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
75
Dave Martin33f08262017-06-20 18:23:39 +010076 user->limit = user->size + reserved_size;
77
78 user->limit -= TERMINATOR_SIZE;
79 user->limit -= EXTRA_CONTEXT_SIZE;
80 /* Reserve space for extension and terminator ^ */
Dave Martinbb4891a2017-06-15 15:03:40 +010081}
82
83static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
84{
85 return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
86}
87
Dave Martinbb4322f2017-06-15 15:03:41 +010088/*
Dave Martin33f08262017-06-20 18:23:39 +010089 * Sanity limit on the approximate maximum size of signal frame we'll
90 * try to generate. Stack alignment padding and the frame record are
91 * not taken into account. This limit is not a guarantee and is
92 * NOT ABI.
93 */
Mark Brown7ddcaf72022-08-17 19:23:21 +010094#define SIGFRAME_MAXSZ SZ_256K
Dave Martin33f08262017-06-20 18:23:39 +010095
96static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
97 unsigned long *offset, size_t size, bool extend)
98{
99 size_t padded_size = round_up(size, 16);
100
101 if (padded_size > user->limit - user->size &&
102 !user->extra_offset &&
103 extend) {
104 int ret;
105
106 user->limit += EXTRA_CONTEXT_SIZE;
107 ret = __sigframe_alloc(user, &user->extra_offset,
108 sizeof(struct extra_context), false);
109 if (ret) {
110 user->limit -= EXTRA_CONTEXT_SIZE;
111 return ret;
112 }
113
114 /* Reserve space for the __reserved[] terminator */
115 user->size += TERMINATOR_SIZE;
116
117 /*
118 * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
119 * the terminator:
120 */
121 user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
122 }
123
124 /* Still not enough space? Bad luck! */
125 if (padded_size > user->limit - user->size)
126 return -ENOMEM;
127
128 *offset = user->size;
129 user->size += padded_size;
130
131 return 0;
132}
133
134/*
Dave Martinbb4322f2017-06-15 15:03:41 +0100135 * Allocate space for an optional record of <size> bytes in the user
136 * signal frame. The offset from the signal frame base address to the
137 * allocated block is assigned to *offset.
138 */
139static int sigframe_alloc(struct rt_sigframe_user_layout *user,
140 unsigned long *offset, size_t size)
141{
Dave Martin33f08262017-06-20 18:23:39 +0100142 return __sigframe_alloc(user, offset, size, true);
143}
Dave Martinbb4322f2017-06-15 15:03:41 +0100144
Dave Martin33f08262017-06-20 18:23:39 +0100145/* Allocate the null terminator record and prevent further allocations */
146static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
147{
148 int ret;
Dave Martinbb4322f2017-06-15 15:03:41 +0100149
Dave Martin33f08262017-06-20 18:23:39 +0100150 /* Un-reserve the space reserved for the terminator: */
151 user->limit += TERMINATOR_SIZE;
152
153 ret = sigframe_alloc(user, &user->end_offset,
154 sizeof(struct _aarch64_ctx));
155 if (ret)
156 return ret;
157
158 /* Prevent further allocation: */
159 user->limit = user->size;
Dave Martinbb4322f2017-06-15 15:03:41 +0100160 return 0;
161}
162
Dave Martinbb4891a2017-06-15 15:03:40 +0100163static void __user *apply_user_offset(
164 struct rt_sigframe_user_layout const *user, unsigned long offset)
165{
166 char __user *base = (char __user *)user->sigframe;
167
168 return base + offset;
169}
170
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000171static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
172{
Dave Martin65896542018-03-28 10:50:49 +0100173 struct user_fpsimd_state const *fpsimd =
174 &current->thread.uw.fpsimd_state;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000175 int err;
176
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000177 /* copy the FP and status/control registers */
178 err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
179 __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
180 __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
181
182 /* copy the magic/size information */
183 __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
184 __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
185
186 return err ? -EFAULT : 0;
187}
188
189static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
190{
Dave Martin0abdeff2017-12-15 18:34:38 +0000191 struct user_fpsimd_state fpsimd;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000192 __u32 magic, size;
193 int err = 0;
194
195 /* check the magic/size information */
196 __get_user_error(magic, &ctx->head.magic, err);
197 __get_user_error(size, &ctx->head.size, err);
198 if (err)
199 return -EFAULT;
200 if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
201 return -EINVAL;
202
203 /* copy the FP and status/control registers */
204 err = __copy_from_user(fpsimd.vregs, ctx->vregs,
205 sizeof(fpsimd.vregs));
206 __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
207 __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
208
Dave Martin8cd969d2017-10-31 15:51:07 +0000209 clear_thread_flag(TIF_SVE);
Mark Brownbaa85152022-11-15 09:46:34 +0000210 current->thread.fp_type = FP_STATE_FPSIMD;
Dave Martin8cd969d2017-10-31 15:51:07 +0000211
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000212 /* load the hardware registers from the fpsimd_state structure */
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100213 if (!err)
214 fpsimd_update_current_state(&fpsimd);
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000215
216 return err ? -EFAULT : 0;
217}
218
Dave Martin8cd969d2017-10-31 15:51:07 +0000219
Dave Martin47ccb022017-06-15 15:03:39 +0100220struct user_ctxs {
221 struct fpsimd_context __user *fpsimd;
Dave Martin8cd969d2017-10-31 15:51:07 +0000222 struct sve_context __user *sve;
Mark Brown39782212022-04-19 12:22:27 +0100223 struct za_context __user *za;
Dave Martin47ccb022017-06-15 15:03:39 +0100224};
225
Dave Martin8cd969d2017-10-31 15:51:07 +0000226#ifdef CONFIG_ARM64_SVE
227
228static int preserve_sve_context(struct sve_context __user *ctx)
229{
230 int err = 0;
231 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
Mark Brown85ed24d2022-04-19 12:22:26 +0100232 u16 flags = 0;
Mark Brown0423eed2021-10-19 18:22:11 +0100233 unsigned int vl = task_get_sve_vl(current);
Dave Martin8cd969d2017-10-31 15:51:07 +0000234 unsigned int vq = 0;
235
Mark Brown85ed24d2022-04-19 12:22:26 +0100236 if (thread_sm_enabled(&current->thread)) {
237 vl = task_get_sme_vl(current);
Dave Martin8cd969d2017-10-31 15:51:07 +0000238 vq = sve_vq_from_vl(vl);
Mark Brown85ed24d2022-04-19 12:22:26 +0100239 flags |= SVE_SIG_FLAG_SM;
240 } else if (test_thread_flag(TIF_SVE)) {
241 vq = sve_vq_from_vl(vl);
242 }
Dave Martin8cd969d2017-10-31 15:51:07 +0000243
244 memset(reserved, 0, sizeof(reserved));
245
246 __put_user_error(SVE_MAGIC, &ctx->head.magic, err);
247 __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
248 &ctx->head.size, err);
249 __put_user_error(vl, &ctx->vl, err);
Mark Brown85ed24d2022-04-19 12:22:26 +0100250 __put_user_error(flags, &ctx->flags, err);
Dave Martin8cd969d2017-10-31 15:51:07 +0000251 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
252 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
253
254 if (vq) {
255 /*
256 * This assumes that the SVE state has already been saved to
Julien Grall68a4c522020-08-28 19:11:49 +0100257 * the task struct by calling the function
258 * fpsimd_signal_preserve_current_state().
Dave Martin8cd969d2017-10-31 15:51:07 +0000259 */
260 err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
261 current->thread.sve_state,
262 SVE_SIG_REGS_SIZE(vq));
263 }
264
265 return err ? -EFAULT : 0;
266}
267
268static int restore_sve_fpsimd_context(struct user_ctxs *user)
269{
270 int err;
Mark Brown85ed24d2022-04-19 12:22:26 +0100271 unsigned int vl, vq;
Dave Martin0abdeff2017-12-15 18:34:38 +0000272 struct user_fpsimd_state fpsimd;
Dave Martin8cd969d2017-10-31 15:51:07 +0000273 struct sve_context sve;
274
275 if (__copy_from_user(&sve, user->sve, sizeof(sve)))
276 return -EFAULT;
277
Mark Brown85ed24d2022-04-19 12:22:26 +0100278 if (sve.flags & SVE_SIG_FLAG_SM) {
279 if (!system_supports_sme())
280 return -EINVAL;
281
282 vl = task_get_sme_vl(current);
283 } else {
Mark Browndf074432022-06-24 18:21:08 +0100284 if (!system_supports_sve())
285 return -EINVAL;
286
Mark Brown85ed24d2022-04-19 12:22:26 +0100287 vl = task_get_sve_vl(current);
288 }
289
290 if (sve.vl != vl)
Dave Martin8cd969d2017-10-31 15:51:07 +0000291 return -EINVAL;
292
293 if (sve.head.size <= sizeof(*user->sve)) {
294 clear_thread_flag(TIF_SVE);
Mark Brownec0067a2022-05-10 17:12:01 +0100295 current->thread.svcr &= ~SVCR_SM_MASK;
Mark Brownbaa85152022-11-15 09:46:34 +0000296 current->thread.fp_type = FP_STATE_FPSIMD;
Dave Martin8cd969d2017-10-31 15:51:07 +0000297 goto fpsimd_only;
298 }
299
300 vq = sve_vq_from_vl(sve.vl);
301
302 if (sve.head.size < SVE_SIG_CONTEXT_SIZE(vq))
303 return -EINVAL;
304
305 /*
306 * Careful: we are about __copy_from_user() directly into
307 * thread.sve_state with preemption enabled, so protection is
308 * needed to prevent a racing context switch from writing stale
309 * registers back over the new data.
310 */
311
312 fpsimd_flush_task_state(current);
Dave Martin8cd969d2017-10-31 15:51:07 +0000313 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
314
Mark Brown826a4fd2022-08-17 19:23:23 +0100315 sve_alloc(current, true);
Mark Brown7559b7d2021-08-24 16:34:17 +0100316 if (!current->thread.sve_state) {
317 clear_thread_flag(TIF_SVE);
318 return -ENOMEM;
319 }
320
Dave Martin8cd969d2017-10-31 15:51:07 +0000321 err = __copy_from_user(current->thread.sve_state,
322 (char __user const *)user->sve +
323 SVE_SIG_REGS_OFFSET,
324 SVE_SIG_REGS_SIZE(vq));
325 if (err)
326 return -EFAULT;
327
Mark Brown85ed24d2022-04-19 12:22:26 +0100328 if (sve.flags & SVE_SIG_FLAG_SM)
Mark Brownec0067a2022-05-10 17:12:01 +0100329 current->thread.svcr |= SVCR_SM_MASK;
Mark Brown85ed24d2022-04-19 12:22:26 +0100330 else
331 set_thread_flag(TIF_SVE);
Mark Brownbaa85152022-11-15 09:46:34 +0000332 current->thread.fp_type = FP_STATE_SVE;
Dave Martin8cd969d2017-10-31 15:51:07 +0000333
334fpsimd_only:
335 /* copy the FP and status/control registers */
336 /* restore_sigframe() already checked that user->fpsimd != NULL. */
337 err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
338 sizeof(fpsimd.vregs));
339 __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
340 __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
341
342 /* load the hardware registers from the fpsimd_state structure */
343 if (!err)
344 fpsimd_update_current_state(&fpsimd);
345
346 return err ? -EFAULT : 0;
347}
348
349#else /* ! CONFIG_ARM64_SVE */
350
Mark Browndf074432022-06-24 18:21:08 +0100351static int restore_sve_fpsimd_context(struct user_ctxs *user)
352{
353 WARN_ON_ONCE(1);
354 return -EINVAL;
355}
356
357/* Turn any non-optimised out attempts to use this into a link error: */
Dave Martin8cd969d2017-10-31 15:51:07 +0000358extern int preserve_sve_context(void __user *ctx);
Dave Martin8cd969d2017-10-31 15:51:07 +0000359
360#endif /* ! CONFIG_ARM64_SVE */
361
Mark Brown39782212022-04-19 12:22:27 +0100362#ifdef CONFIG_ARM64_SME
363
364static int preserve_za_context(struct za_context __user *ctx)
365{
366 int err = 0;
367 u16 reserved[ARRAY_SIZE(ctx->__reserved)];
368 unsigned int vl = task_get_sme_vl(current);
369 unsigned int vq;
370
371 if (thread_za_enabled(&current->thread))
372 vq = sve_vq_from_vl(vl);
373 else
374 vq = 0;
375
376 memset(reserved, 0, sizeof(reserved));
377
378 __put_user_error(ZA_MAGIC, &ctx->head.magic, err);
379 __put_user_error(round_up(ZA_SIG_CONTEXT_SIZE(vq), 16),
380 &ctx->head.size, err);
381 __put_user_error(vl, &ctx->vl, err);
382 BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
383 err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
384
385 if (vq) {
386 /*
387 * This assumes that the ZA state has already been saved to
388 * the task struct by calling the function
389 * fpsimd_signal_preserve_current_state().
390 */
391 err |= __copy_to_user((char __user *)ctx + ZA_SIG_REGS_OFFSET,
392 current->thread.za_state,
393 ZA_SIG_REGS_SIZE(vq));
394 }
395
396 return err ? -EFAULT : 0;
397}
398
Catalin Marinas1bec8772022-06-01 18:13:38 +0100399static int restore_za_context(struct user_ctxs *user)
Mark Brown39782212022-04-19 12:22:27 +0100400{
401 int err;
402 unsigned int vq;
403 struct za_context za;
404
405 if (__copy_from_user(&za, user->za, sizeof(za)))
406 return -EFAULT;
407
408 if (za.vl != task_get_sme_vl(current))
409 return -EINVAL;
410
411 if (za.head.size <= sizeof(*user->za)) {
Mark Brownec0067a2022-05-10 17:12:01 +0100412 current->thread.svcr &= ~SVCR_ZA_MASK;
Mark Brown39782212022-04-19 12:22:27 +0100413 return 0;
414 }
415
416 vq = sve_vq_from_vl(za.vl);
417
418 if (za.head.size < ZA_SIG_CONTEXT_SIZE(vq))
419 return -EINVAL;
420
421 /*
422 * Careful: we are about __copy_from_user() directly into
423 * thread.za_state with preemption enabled, so protection is
424 * needed to prevent a racing context switch from writing stale
425 * registers back over the new data.
426 */
427
428 fpsimd_flush_task_state(current);
429 /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
430
431 sme_alloc(current);
432 if (!current->thread.za_state) {
Mark Brownec0067a2022-05-10 17:12:01 +0100433 current->thread.svcr &= ~SVCR_ZA_MASK;
Mark Brown39782212022-04-19 12:22:27 +0100434 clear_thread_flag(TIF_SME);
435 return -ENOMEM;
436 }
437
438 err = __copy_from_user(current->thread.za_state,
439 (char __user const *)user->za +
440 ZA_SIG_REGS_OFFSET,
441 ZA_SIG_REGS_SIZE(vq));
442 if (err)
443 return -EFAULT;
444
445 set_thread_flag(TIF_SME);
Mark Brownec0067a2022-05-10 17:12:01 +0100446 current->thread.svcr |= SVCR_ZA_MASK;
Mark Brown39782212022-04-19 12:22:27 +0100447
448 return 0;
449}
450#else /* ! CONFIG_ARM64_SME */
451
452/* Turn any non-optimised out attempts to use these into a link error: */
453extern int preserve_za_context(void __user *ctx);
454extern int restore_za_context(struct user_ctxs *user);
455
456#endif /* ! CONFIG_ARM64_SME */
Dave Martin8cd969d2017-10-31 15:51:07 +0000457
Dave Martin47ccb022017-06-15 15:03:39 +0100458static int parse_user_sigframe(struct user_ctxs *user,
459 struct rt_sigframe __user *sf)
460{
461 struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
Dave Martinbb4891a2017-06-15 15:03:40 +0100462 struct _aarch64_ctx __user *head;
463 char __user *base = (char __user *)&sc->__reserved;
Dave Martin47ccb022017-06-15 15:03:39 +0100464 size_t offset = 0;
Dave Martinbb4891a2017-06-15 15:03:40 +0100465 size_t limit = sizeof(sc->__reserved);
Dave Martin33f08262017-06-20 18:23:39 +0100466 bool have_extra_context = false;
467 char const __user *const sfp = (char const __user *)sf;
Dave Martin47ccb022017-06-15 15:03:39 +0100468
469 user->fpsimd = NULL;
Dave Martin8cd969d2017-10-31 15:51:07 +0000470 user->sve = NULL;
Mark Brown39782212022-04-19 12:22:27 +0100471 user->za = NULL;
Dave Martin47ccb022017-06-15 15:03:39 +0100472
Dave Martinbb4891a2017-06-15 15:03:40 +0100473 if (!IS_ALIGNED((unsigned long)base, 16))
474 goto invalid;
475
Dave Martin47ccb022017-06-15 15:03:39 +0100476 while (1) {
Dave Martinbb4891a2017-06-15 15:03:40 +0100477 int err = 0;
Dave Martin47ccb022017-06-15 15:03:39 +0100478 u32 magic, size;
Dave Martin33f08262017-06-20 18:23:39 +0100479 char const __user *userp;
480 struct extra_context const __user *extra;
481 u64 extra_datap;
482 u32 extra_size;
483 struct _aarch64_ctx const __user *end;
484 u32 end_magic, end_size;
Dave Martin47ccb022017-06-15 15:03:39 +0100485
Dave Martinbb4891a2017-06-15 15:03:40 +0100486 if (limit - offset < sizeof(*head))
Dave Martin47ccb022017-06-15 15:03:39 +0100487 goto invalid;
488
Dave Martinbb4891a2017-06-15 15:03:40 +0100489 if (!IS_ALIGNED(offset, 16))
490 goto invalid;
491
492 head = (struct _aarch64_ctx __user *)(base + offset);
Dave Martin47ccb022017-06-15 15:03:39 +0100493 __get_user_error(magic, &head->magic, err);
494 __get_user_error(size, &head->size, err);
495 if (err)
496 return err;
497
Dave Martinbb4891a2017-06-15 15:03:40 +0100498 if (limit - offset < size)
499 goto invalid;
500
Dave Martin47ccb022017-06-15 15:03:39 +0100501 switch (magic) {
502 case 0:
503 if (size)
504 goto invalid;
505
506 goto done;
507
508 case FPSIMD_MAGIC:
Suzuki K Poulose6d502b62020-01-13 23:30:22 +0000509 if (!system_supports_fpsimd())
510 goto invalid;
Dave Martin47ccb022017-06-15 15:03:39 +0100511 if (user->fpsimd)
512 goto invalid;
513
Dave Martinbb4891a2017-06-15 15:03:40 +0100514 if (size < sizeof(*user->fpsimd))
Dave Martin47ccb022017-06-15 15:03:39 +0100515 goto invalid;
516
517 user->fpsimd = (struct fpsimd_context __user *)head;
518 break;
519
520 case ESR_MAGIC:
521 /* ignore */
522 break;
523
Dave Martin8cd969d2017-10-31 15:51:07 +0000524 case SVE_MAGIC:
Mark Brown85ed24d2022-04-19 12:22:26 +0100525 if (!system_supports_sve() && !system_supports_sme())
Dave Martin8cd969d2017-10-31 15:51:07 +0000526 goto invalid;
527
528 if (user->sve)
529 goto invalid;
530
531 if (size < sizeof(*user->sve))
532 goto invalid;
533
534 user->sve = (struct sve_context __user *)head;
535 break;
536
Mark Brown39782212022-04-19 12:22:27 +0100537 case ZA_MAGIC:
538 if (!system_supports_sme())
539 goto invalid;
540
541 if (user->za)
542 goto invalid;
543
544 if (size < sizeof(*user->za))
545 goto invalid;
546
547 user->za = (struct za_context __user *)head;
548 break;
549
Dave Martin33f08262017-06-20 18:23:39 +0100550 case EXTRA_MAGIC:
551 if (have_extra_context)
552 goto invalid;
553
554 if (size < sizeof(*extra))
555 goto invalid;
556
557 userp = (char const __user *)head;
558
559 extra = (struct extra_context const __user *)userp;
560 userp += size;
561
562 __get_user_error(extra_datap, &extra->datap, err);
563 __get_user_error(extra_size, &extra->size, err);
564 if (err)
565 return err;
566
567 /* Check for the dummy terminator in __reserved[]: */
568
569 if (limit - offset - size < TERMINATOR_SIZE)
570 goto invalid;
571
572 end = (struct _aarch64_ctx const __user *)userp;
573 userp += TERMINATOR_SIZE;
574
575 __get_user_error(end_magic, &end->magic, err);
576 __get_user_error(end_size, &end->size, err);
577 if (err)
578 return err;
579
580 if (end_magic || end_size)
581 goto invalid;
582
583 /* Prevent looping/repeated parsing of extra_context */
584 have_extra_context = true;
585
586 base = (__force void __user *)extra_datap;
587 if (!IS_ALIGNED((unsigned long)base, 16))
588 goto invalid;
589
590 if (!IS_ALIGNED(extra_size, 16))
591 goto invalid;
592
593 if (base != userp)
594 goto invalid;
595
596 /* Reject "unreasonably large" frames: */
597 if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
598 goto invalid;
599
600 /*
601 * Ignore trailing terminator in __reserved[]
602 * and start parsing extra data:
603 */
604 offset = 0;
605 limit = extra_size;
Dave Martinabf73982017-10-31 15:50:55 +0000606
Linus Torvalds96d4f262019-01-03 18:57:57 -0800607 if (!access_ok(base, limit))
Dave Martinabf73982017-10-31 15:50:55 +0000608 goto invalid;
609
Dave Martin33f08262017-06-20 18:23:39 +0100610 continue;
611
Dave Martin47ccb022017-06-15 15:03:39 +0100612 default:
613 goto invalid;
614 }
615
616 if (size < sizeof(*head))
617 goto invalid;
618
Dave Martinbb4891a2017-06-15 15:03:40 +0100619 if (limit - offset < size)
Dave Martin47ccb022017-06-15 15:03:39 +0100620 goto invalid;
621
622 offset += size;
623 }
624
625done:
Dave Martin47ccb022017-06-15 15:03:39 +0100626 return 0;
627
628invalid:
629 return -EINVAL;
630}
631
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000632static int restore_sigframe(struct pt_regs *regs,
633 struct rt_sigframe __user *sf)
634{
635 sigset_t set;
636 int i, err;
Dave Martin47ccb022017-06-15 15:03:39 +0100637 struct user_ctxs user;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000638
639 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
640 if (err == 0)
641 set_current_blocked(&set);
642
643 for (i = 0; i < 31; i++)
644 __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
645 err);
646 __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
647 __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
648 __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
649
650 /*
651 * Avoid sys_rt_sigreturn() restarting.
652 */
Dave Martin17c28952017-08-01 15:35:54 +0100653 forget_syscall(regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000654
Mark Rutlanddbd4d7c2016-03-01 14:18:50 +0000655 err |= !valid_user_regs(&regs->user_regs, current);
Dave Martin47ccb022017-06-15 15:03:39 +0100656 if (err == 0)
657 err = parse_user_sigframe(&user, sf);
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000658
Suzuki K Poulose6d502b62020-01-13 23:30:22 +0000659 if (err == 0 && system_supports_fpsimd()) {
Dave Martin8cd969d2017-10-31 15:51:07 +0000660 if (!user.fpsimd)
661 return -EINVAL;
662
Mark Browndf074432022-06-24 18:21:08 +0100663 if (user.sve)
Dave Martin8cd969d2017-10-31 15:51:07 +0000664 err = restore_sve_fpsimd_context(&user);
Mark Browndf074432022-06-24 18:21:08 +0100665 else
Dave Martin8cd969d2017-10-31 15:51:07 +0000666 err = restore_fpsimd_context(user.fpsimd);
Dave Martin8cd969d2017-10-31 15:51:07 +0000667 }
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000668
Mark Brown39782212022-04-19 12:22:27 +0100669 if (err == 0 && system_supports_sme() && user.za)
670 err = restore_za_context(&user);
671
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000672 return err;
673}
674
Mark Rutlandbf4ce5c2018-07-11 14:56:53 +0100675SYSCALL_DEFINE0(rt_sigreturn)
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000676{
Mark Rutland3085e162018-07-11 14:56:41 +0100677 struct pt_regs *regs = current_pt_regs();
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000678 struct rt_sigframe __user *frame;
679
680 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800681 current->restart_block.fn = do_no_restart_syscall;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000682
683 /*
684 * Since we stacked the signal on a 128-bit boundary, then 'sp' should
685 * be word aligned here.
686 */
687 if (regs->sp & 15)
688 goto badframe;
689
690 frame = (struct rt_sigframe __user *)regs->sp;
691
Linus Torvalds96d4f262019-01-03 18:57:57 -0800692 if (!access_ok(frame, sizeof (*frame)))
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000693 goto badframe;
694
695 if (restore_sigframe(regs, frame))
696 goto badframe;
697
Al Viro207bdae2012-12-23 01:56:45 -0500698 if (restore_altstack(&frame->uc.uc_stack))
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000699 goto badframe;
700
701 return regs->regs[0];
702
703badframe:
Will Deaconf71016a2018-02-20 15:05:17 +0000704 arm64_notify_segfault(regs->sp);
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000705 return 0;
706}
707
Dave Martin94b07c12018-06-01 11:10:14 +0100708/*
709 * Determine the layout of optional records in the signal frame
710 *
711 * add_all: if true, lays out the biggest possible signal frame for
712 * this task; otherwise, generates a layout for the current state
713 * of the task.
714 */
715static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
716 bool add_all)
Dave Martinbb4891a2017-06-15 15:03:40 +0100717{
Dave Martinbb4322f2017-06-15 15:03:41 +0100718 int err;
719
David Engraf0a32c882022-02-25 11:40:08 +0100720 if (system_supports_fpsimd()) {
721 err = sigframe_alloc(user, &user->fpsimd_offset,
722 sizeof(struct fpsimd_context));
723 if (err)
724 return err;
725 }
Dave Martinbb4891a2017-06-15 15:03:40 +0100726
727 /* fault information, if valid */
Dave Martin94b07c12018-06-01 11:10:14 +0100728 if (add_all || current->thread.fault_code) {
Dave Martinbb4322f2017-06-15 15:03:41 +0100729 err = sigframe_alloc(user, &user->esr_offset,
730 sizeof(struct esr_context));
731 if (err)
732 return err;
Dave Martinbb4891a2017-06-15 15:03:40 +0100733 }
734
Dave Martin8cd969d2017-10-31 15:51:07 +0000735 if (system_supports_sve()) {
736 unsigned int vq = 0;
737
Mark Brown85ed24d2022-04-19 12:22:26 +0100738 if (add_all || test_thread_flag(TIF_SVE) ||
739 thread_sm_enabled(&current->thread)) {
740 int vl = max(sve_max_vl(), sme_max_vl());
Dave Martin94b07c12018-06-01 11:10:14 +0100741
742 if (!add_all)
Mark Brown85ed24d2022-04-19 12:22:26 +0100743 vl = thread_get_cur_vl(&current->thread);
Dave Martin94b07c12018-06-01 11:10:14 +0100744
745 vq = sve_vq_from_vl(vl);
746 }
Dave Martin8cd969d2017-10-31 15:51:07 +0000747
748 err = sigframe_alloc(user, &user->sve_offset,
749 SVE_SIG_CONTEXT_SIZE(vq));
750 if (err)
751 return err;
752 }
753
Mark Brown39782212022-04-19 12:22:27 +0100754 if (system_supports_sme()) {
755 unsigned int vl;
756 unsigned int vq = 0;
757
758 if (add_all)
759 vl = sme_max_vl();
760 else
761 vl = task_get_sme_vl(current);
762
763 if (thread_za_enabled(&current->thread))
764 vq = sve_vq_from_vl(vl);
765
766 err = sigframe_alloc(user, &user->za_offset,
767 ZA_SIG_CONTEXT_SIZE(vq));
768 if (err)
769 return err;
770 }
771
Dave Martin33f08262017-06-20 18:23:39 +0100772 return sigframe_alloc_end(user);
Dave Martinbb4891a2017-06-15 15:03:40 +0100773}
774
Dave Martin20987de2017-06-15 15:03:38 +0100775static int setup_sigframe(struct rt_sigframe_user_layout *user,
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000776 struct pt_regs *regs, sigset_t *set)
777{
778 int i, err = 0;
Dave Martin20987de2017-06-15 15:03:38 +0100779 struct rt_sigframe __user *sf = user->sigframe;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000780
Will Deacon304ef4e2012-11-23 12:34:13 +0000781 /* set up the stack frame for unwinding */
Dave Martin20987de2017-06-15 15:03:38 +0100782 __put_user_error(regs->regs[29], &user->next_frame->fp, err);
783 __put_user_error(regs->regs[30], &user->next_frame->lr, err);
Will Deacon304ef4e2012-11-23 12:34:13 +0000784
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000785 for (i = 0; i < 31; i++)
786 __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
787 err);
788 __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
789 __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
790 __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
791
792 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
793
794 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
795
Suzuki K Poulose6d502b62020-01-13 23:30:22 +0000796 if (err == 0 && system_supports_fpsimd()) {
Dave Martinbb4891a2017-06-15 15:03:40 +0100797 struct fpsimd_context __user *fpsimd_ctx =
798 apply_user_offset(user, user->fpsimd_offset);
Catalin Marinas0e0276d2014-04-04 15:42:16 +0100799 err |= preserve_fpsimd_context(fpsimd_ctx);
Catalin Marinas0e0276d2014-04-04 15:42:16 +0100800 }
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000801
Catalin Marinas15af1942013-09-16 15:19:27 +0100802 /* fault information, if valid */
Dave Martinbb4891a2017-06-15 15:03:40 +0100803 if (err == 0 && user->esr_offset) {
804 struct esr_context __user *esr_ctx =
805 apply_user_offset(user, user->esr_offset);
806
Catalin Marinas15af1942013-09-16 15:19:27 +0100807 __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
808 __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
809 __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
Catalin Marinas15af1942013-09-16 15:19:27 +0100810 }
811
Mark Brown85ed24d2022-04-19 12:22:26 +0100812 /* Scalable Vector Extension state (including streaming), if present */
813 if ((system_supports_sve() || system_supports_sme()) &&
814 err == 0 && user->sve_offset) {
Dave Martin8cd969d2017-10-31 15:51:07 +0000815 struct sve_context __user *sve_ctx =
816 apply_user_offset(user, user->sve_offset);
817 err |= preserve_sve_context(sve_ctx);
818 }
819
Mark Brown39782212022-04-19 12:22:27 +0100820 /* ZA state if present */
821 if (system_supports_sme() && err == 0 && user->za_offset) {
822 struct za_context __user *za_ctx =
823 apply_user_offset(user, user->za_offset);
824 err |= preserve_za_context(za_ctx);
825 }
826
Dave Martin33f08262017-06-20 18:23:39 +0100827 if (err == 0 && user->extra_offset) {
828 char __user *sfp = (char __user *)user->sigframe;
829 char __user *userp =
830 apply_user_offset(user, user->extra_offset);
831
832 struct extra_context __user *extra;
833 struct _aarch64_ctx __user *end;
834 u64 extra_datap;
835 u32 extra_size;
836
837 extra = (struct extra_context __user *)userp;
838 userp += EXTRA_CONTEXT_SIZE;
839
840 end = (struct _aarch64_ctx __user *)userp;
841 userp += TERMINATOR_SIZE;
842
843 /*
844 * extra_datap is just written to the signal frame.
845 * The value gets cast back to a void __user *
846 * during sigreturn.
847 */
848 extra_datap = (__force u64)userp;
849 extra_size = sfp + round_up(user->size, 16) - userp;
850
851 __put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
852 __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
853 __put_user_error(extra_datap, &extra->datap, err);
854 __put_user_error(extra_size, &extra->size, err);
855
856 /* Add the terminator */
857 __put_user_error(0, &end->magic, err);
858 __put_user_error(0, &end->size, err);
859 }
860
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000861 /* set the "end" magic */
Dave Martinbb4891a2017-06-15 15:03:40 +0100862 if (err == 0) {
863 struct _aarch64_ctx __user *end =
864 apply_user_offset(user, user->end_offset);
865
866 __put_user_error(0, &end->magic, err);
867 __put_user_error(0, &end->size, err);
868 }
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000869
870 return err;
871}
872
Dave Martin20987de2017-06-15 15:03:38 +0100873static int get_sigframe(struct rt_sigframe_user_layout *user,
874 struct ksignal *ksig, struct pt_regs *regs)
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000875{
876 unsigned long sp, sp_top;
Dave Martinbb4891a2017-06-15 15:03:40 +0100877 int err;
878
879 init_user_layout(user);
Dave Martin94b07c12018-06-01 11:10:14 +0100880 err = setup_sigframe_layout(user, false);
Dave Martinbb4891a2017-06-15 15:03:40 +0100881 if (err)
882 return err;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000883
Richard Weinberger38a7be32014-03-05 13:31:20 +0100884 sp = sp_top = sigsp(regs->sp, ksig);
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000885
Dave Martin20987de2017-06-15 15:03:38 +0100886 sp = round_down(sp - sizeof(struct frame_record), 16);
887 user->next_frame = (struct frame_record __user *)sp;
888
Dave Martinbb4891a2017-06-15 15:03:40 +0100889 sp = round_down(sp, 16) - sigframe_size(user);
Dave Martin20987de2017-06-15 15:03:38 +0100890 user->sigframe = (struct rt_sigframe __user *)sp;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000891
892 /*
893 * Check that we can actually write to the signal frame.
894 */
Linus Torvalds96d4f262019-01-03 18:57:57 -0800895 if (!access_ok(user->sigframe, sp_top - sp))
Dave Martin20987de2017-06-15 15:03:38 +0100896 return -EFAULT;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000897
Dave Martin20987de2017-06-15 15:03:38 +0100898 return 0;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000899}
900
Will Deacon304ef4e2012-11-23 12:34:13 +0000901static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
Dave Martin20987de2017-06-15 15:03:38 +0100902 struct rt_sigframe_user_layout *user, int usig)
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000903{
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000904 __sigrestore_t sigtramp;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000905
906 regs->regs[0] = usig;
Dave Martin20987de2017-06-15 15:03:38 +0100907 regs->sp = (unsigned long)user->sigframe;
908 regs->regs[29] = (unsigned long)&user->next_frame->fp;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000909 regs->pc = (unsigned long)ka->sa.sa_handler;
910
Dave Martin8ef8f3602020-03-16 16:50:45 +0000911 /*
912 * Signal delivery is a (wacky) indirect function call in
913 * userspace, so simulate the same setting of BTYPE as a BLR
914 * <register containing the signal handler entry point>.
915 * Signal delivery to a location in a PROT_BTI guarded page
916 * that is not a function entry point will now trigger a
917 * SIGILL in userspace.
918 *
919 * If the signal handler entry point is not in a PROT_BTI
920 * guarded page, this is harmless.
921 */
922 if (system_supports_bti()) {
923 regs->pstate &= ~PSR_BTYPE_MASK;
924 regs->pstate |= PSR_BTYPE_C;
925 }
926
Vincenzo Frascino637ec832019-09-16 11:51:17 +0100927 /* TCO (Tag Check Override) always cleared for signal handlers */
928 regs->pstate &= ~PSR_TCO_BIT;
929
Mark Brown40a8e872022-04-19 12:22:25 +0100930 /* Signal handlers are invoked with ZA and streaming mode disabled */
931 if (system_supports_sme()) {
Mark Brownea64baa2022-08-17 19:23:22 +0100932 /*
933 * If we were in streaming mode the saved register
934 * state was SVE but we will exit SM and use the
935 * FPSIMD register state - flush the saved FPSIMD
936 * register state in case it gets loaded.
937 */
Mark Brownbaa85152022-11-15 09:46:34 +0000938 if (current->thread.svcr & SVCR_SM_MASK) {
Mark Brownea64baa2022-08-17 19:23:22 +0100939 memset(&current->thread.uw.fpsimd_state, 0,
940 sizeof(current->thread.uw.fpsimd_state));
Mark Brownbaa85152022-11-15 09:46:34 +0000941 current->thread.fp_type = FP_STATE_FPSIMD;
942 }
Mark Brownea64baa2022-08-17 19:23:22 +0100943
Mark Brownec0067a2022-05-10 17:12:01 +0100944 current->thread.svcr &= ~(SVCR_ZA_MASK |
945 SVCR_SM_MASK);
Mark Brown40a8e872022-04-19 12:22:25 +0100946 sme_smstop();
947 }
948
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000949 if (ka->sa.sa_flags & SA_RESTORER)
950 sigtramp = ka->sa.sa_restorer;
951 else
952 sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
953
954 regs->regs[30] = (unsigned long)sigtramp;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000955}
956
Richard Weinberger00554fa2013-10-06 22:52:44 +0200957static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
958 struct pt_regs *regs)
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000959{
Dave Martin20987de2017-06-15 15:03:38 +0100960 struct rt_sigframe_user_layout user;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000961 struct rt_sigframe __user *frame;
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000962 int err = 0;
963
Dave Martin8cd969d2017-10-31 15:51:07 +0000964 fpsimd_signal_preserve_current_state();
965
Dave Martin20987de2017-06-15 15:03:38 +0100966 if (get_sigframe(&user, ksig, regs))
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000967 return 1;
968
Dave Martin20987de2017-06-15 15:03:38 +0100969 frame = user.sigframe;
970
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000971 __put_user_error(0, &frame->uc.uc_flags, err);
972 __put_user_error(NULL, &frame->uc.uc_link, err);
973
Al Viro207bdae2012-12-23 01:56:45 -0500974 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
Dave Martin20987de2017-06-15 15:03:38 +0100975 err |= setup_sigframe(&user, regs, set);
Will Deacon304ef4e2012-11-23 12:34:13 +0000976 if (err == 0) {
Dave Martin20987de2017-06-15 15:03:38 +0100977 setup_return(regs, &ksig->ka, &user, usig);
Richard Weinberger00554fa2013-10-06 22:52:44 +0200978 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
979 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Will Deacon304ef4e2012-11-23 12:34:13 +0000980 regs->regs[1] = (unsigned long)&frame->info;
981 regs->regs[2] = (unsigned long)&frame->uc;
982 }
Catalin Marinas2c020ed2012-03-05 11:49:31 +0000983 }
984
985 return err;
986}
987
988static void setup_restart_syscall(struct pt_regs *regs)
989{
990 if (is_compat_task())
991 compat_setup_restart_syscall(regs);
992 else
993 regs->regs[8] = __NR_restart_syscall;
994}
995
996/*
997 * OK, we're invoking a handler
998 */
Richard Weinberger00554fa2013-10-06 22:52:44 +0200999static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001000{
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001001 sigset_t *oldset = sigmask_to_save();
Richard Weinberger00554fa2013-10-06 22:52:44 +02001002 int usig = ksig->sig;
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001003 int ret;
1004
Will Deacon409d5db2018-06-20 14:46:50 +01001005 rseq_signal_deliver(ksig, regs);
1006
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001007 /*
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001008 * Set up the stack frame
1009 */
1010 if (is_compat_task()) {
Richard Weinberger00554fa2013-10-06 22:52:44 +02001011 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
1012 ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001013 else
Richard Weinberger00554fa2013-10-06 22:52:44 +02001014 ret = compat_setup_frame(usig, ksig, oldset, regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001015 } else {
Richard Weinberger00554fa2013-10-06 22:52:44 +02001016 ret = setup_rt_frame(usig, ksig, oldset, regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001017 }
1018
1019 /*
1020 * Check that the resulting registers are actually sane.
1021 */
Mark Rutlanddbd4d7c2016-03-01 14:18:50 +00001022 ret |= !valid_user_regs(&regs->user_regs, current);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001023
Will Deaconac2081c2020-07-02 21:16:20 +01001024 /* Step into the signal handler if we are stepping */
1025 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001026}
1027
1028/*
1029 * Note that 'init' is a special process: it doesn't get signals it doesn't
1030 * want to handle. Thus you cannot kill init even with a SIGKILL even by
1031 * mistake.
1032 *
1033 * Note that we go through the signals twice: once to check the signals that
1034 * the kernel can handle, and then we build all the user-level signal handling
1035 * stack-frames in one go after that.
1036 */
1037static void do_signal(struct pt_regs *regs)
1038{
1039 unsigned long continue_addr = 0, restart_addr = 0;
Richard Weinberger00554fa2013-10-06 22:52:44 +02001040 int retval = 0;
Richard Weinberger00554fa2013-10-06 22:52:44 +02001041 struct ksignal ksig;
Dave Martin0fe42512018-06-07 12:32:05 +01001042 bool syscall = in_syscall(regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001043
1044 /*
1045 * If we were from a system call, check for system call restarting...
1046 */
Dave Martin0fe42512018-06-07 12:32:05 +01001047 if (syscall) {
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001048 continue_addr = regs->pc;
1049 restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
1050 retval = regs->regs[0];
1051
1052 /*
1053 * Avoid additional syscall restarting via ret_to_user.
1054 */
Dave Martin17c28952017-08-01 15:35:54 +01001055 forget_syscall(regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001056
1057 /*
1058 * Prepare for system call restart. We do this here so that a
1059 * debugger will see the already changed PC.
1060 */
1061 switch (retval) {
1062 case -ERESTARTNOHAND:
1063 case -ERESTARTSYS:
1064 case -ERESTARTNOINTR:
1065 case -ERESTART_RESTARTBLOCK:
1066 regs->regs[0] = regs->orig_x0;
1067 regs->pc = restart_addr;
1068 break;
1069 }
1070 }
1071
1072 /*
1073 * Get the signal to deliver. When running under ptrace, at this point
1074 * the debugger may change all of our registers.
1075 */
Richard Weinberger00554fa2013-10-06 22:52:44 +02001076 if (get_signal(&ksig)) {
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001077 /*
1078 * Depending on the signal settings, we may need to revert the
1079 * decision to restart the system call, but skip this if a
1080 * debugger has chosen to restart at a different PC.
1081 */
1082 if (regs->pc == restart_addr &&
1083 (retval == -ERESTARTNOHAND ||
1084 retval == -ERESTART_RESTARTBLOCK ||
1085 (retval == -ERESTARTSYS &&
Richard Weinberger00554fa2013-10-06 22:52:44 +02001086 !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
Mark Rutlande30e8d42021-08-02 11:42:00 +01001087 syscall_set_return_value(current, regs, -EINTR, 0);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001088 regs->pc = continue_addr;
1089 }
1090
Richard Weinberger00554fa2013-10-06 22:52:44 +02001091 handle_signal(&ksig, regs);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001092 return;
1093 }
1094
1095 /*
1096 * Handle restarting a different system call. As above, if a debugger
1097 * has chosen to restart at a different PC, ignore the restart.
1098 */
Dave Martin0fe42512018-06-07 12:32:05 +01001099 if (syscall && regs->pc == restart_addr) {
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001100 if (retval == -ERESTART_RESTARTBLOCK)
1101 setup_restart_syscall(regs);
1102 user_rewind_single_step(current);
1103 }
1104
1105 restore_saved_sigmask();
1106}
1107
Mark Rutland4d1c2ee2021-08-02 15:07:32 +01001108void do_notify_resume(struct pt_regs *regs, unsigned long thread_flags)
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001109{
Chris Metcalf421dd6f2016-07-14 16:48:14 -04001110 do {
1111 if (thread_flags & _TIF_NEED_RESCHED) {
James Morse8d667722017-11-02 12:12:37 +00001112 /* Unmask Debug and SError for the next task */
1113 local_daif_restore(DAIF_PROCCTX_NOIRQ);
1114
Chris Metcalf421dd6f2016-07-14 16:48:14 -04001115 schedule();
1116 } else {
James Morse8d667722017-11-02 12:12:37 +00001117 local_daif_restore(DAIF_PROCCTX);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001118
Pratyush Anand9842cea2016-11-02 14:40:46 +05301119 if (thread_flags & _TIF_UPROBE)
1120 uprobe_notify_resume(regs);
1121
Vincenzo Frascino637ec832019-09-16 11:51:17 +01001122 if (thread_flags & _TIF_MTE_ASYNC_FAULT) {
1123 clear_thread_flag(TIF_MTE_ASYNC_FAULT);
1124 send_sig_fault(SIGSEGV, SEGV_MTEAERR,
1125 (void __user *)NULL, current);
1126 }
1127
Jens Axboe192caab2020-10-22 20:09:23 -06001128 if (thread_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
Chris Metcalf421dd6f2016-07-14 16:48:14 -04001129 do_signal(regs);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001130
Sean Christophersona68de802021-09-01 13:30:27 -07001131 if (thread_flags & _TIF_NOTIFY_RESUME)
Eric W. Biederman03248ad2022-02-09 12:20:45 -06001132 resume_user_mode_work(regs);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001133
Chris Metcalf421dd6f2016-07-14 16:48:14 -04001134 if (thread_flags & _TIF_FOREIGN_FPSTATE)
1135 fpsimd_restore_current_state();
1136 }
1137
James Morse8d667722017-11-02 12:12:37 +00001138 local_daif_mask();
Mark Rutland342b3802021-11-29 13:06:48 +00001139 thread_flags = read_thread_flags();
Chris Metcalf421dd6f2016-07-14 16:48:14 -04001140 } while (thread_flags & _TIF_WORK_MASK);
Catalin Marinas2c020ed2012-03-05 11:49:31 +00001141}
Dave Martin94b07c12018-06-01 11:10:14 +01001142
1143unsigned long __ro_after_init signal_minsigstksz;
1144
1145/*
1146 * Determine the stack space required for guaranteed signal devliery.
1147 * This function is used to populate AT_MINSIGSTKSZ at process startup.
1148 * cpufeatures setup is assumed to be complete.
1149 */
1150void __init minsigstksz_setup(void)
1151{
1152 struct rt_sigframe_user_layout user;
1153
1154 init_user_layout(&user);
1155
1156 /*
1157 * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't
1158 * be big enough, but it's our best guess:
1159 */
1160 if (WARN_ON(setup_sigframe_layout(&user, true)))
1161 return;
1162
1163 signal_minsigstksz = sigframe_size(&user) +
1164 round_up(sizeof(struct frame_record), 16) +
1165 16; /* max alignment padding */
1166}
Marco Elver726e3372021-04-29 21:07:34 +02001167
1168/*
1169 * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
1170 * changes likely come with new fields that should be added below.
1171 */
1172static_assert(NSIGILL == 11);
1173static_assert(NSIGFPE == 15);
1174static_assert(NSIGSEGV == 9);
1175static_assert(NSIGBUS == 5);
1176static_assert(NSIGTRAP == 6);
1177static_assert(NSIGCHLD == 6);
1178static_assert(NSIGSYS == 2);
Eric W. Biederman50ae8132021-05-04 11:25:22 -05001179static_assert(sizeof(siginfo_t) == 128);
1180static_assert(__alignof__(siginfo_t) == 8);
Marco Elver726e3372021-04-29 21:07:34 +02001181static_assert(offsetof(siginfo_t, si_signo) == 0x00);
1182static_assert(offsetof(siginfo_t, si_errno) == 0x04);
1183static_assert(offsetof(siginfo_t, si_code) == 0x08);
1184static_assert(offsetof(siginfo_t, si_pid) == 0x10);
1185static_assert(offsetof(siginfo_t, si_uid) == 0x14);
1186static_assert(offsetof(siginfo_t, si_tid) == 0x10);
1187static_assert(offsetof(siginfo_t, si_overrun) == 0x14);
1188static_assert(offsetof(siginfo_t, si_status) == 0x18);
1189static_assert(offsetof(siginfo_t, si_utime) == 0x20);
1190static_assert(offsetof(siginfo_t, si_stime) == 0x28);
1191static_assert(offsetof(siginfo_t, si_value) == 0x18);
1192static_assert(offsetof(siginfo_t, si_int) == 0x18);
1193static_assert(offsetof(siginfo_t, si_ptr) == 0x18);
1194static_assert(offsetof(siginfo_t, si_addr) == 0x10);
1195static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18);
1196static_assert(offsetof(siginfo_t, si_lower) == 0x20);
1197static_assert(offsetof(siginfo_t, si_upper) == 0x28);
1198static_assert(offsetof(siginfo_t, si_pkey) == 0x20);
1199static_assert(offsetof(siginfo_t, si_perf_data) == 0x18);
1200static_assert(offsetof(siginfo_t, si_perf_type) == 0x20);
Marco Elver78ed93d2022-04-04 13:12:04 +02001201static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
Marco Elver726e3372021-04-29 21:07:34 +02001202static_assert(offsetof(siginfo_t, si_band) == 0x10);
1203static_assert(offsetof(siginfo_t, si_fd) == 0x18);
1204static_assert(offsetof(siginfo_t, si_call_addr) == 0x10);
1205static_assert(offsetof(siginfo_t, si_syscall) == 0x18);
1206static_assert(offsetof(siginfo_t, si_arch) == 0x1c);