1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/compat.h>
4 #include <linux/signal.h>
5 #include <linux/uaccess.h>
6 #include <linux/syscalls.h>
7 #include <linux/linkage.h>
10 #include <asm/signal32.h>
11 #include <asm/switch_to.h>
12 #include <asm/ucontext.h>
15 #define COMPAT_DEBUG_SIG 0
17 struct compat_sigcontext
{
18 struct compat_user_regs_struct sc_regs
;
19 union __riscv_fp_state sc_fpregs
;
22 struct compat_ucontext
{
23 compat_ulong_t uc_flags
;
24 struct compat_ucontext
*uc_link
;
25 compat_stack_t uc_stack
;
27 /* There's some padding here to allow sigset_t to be expanded in the
28 * future. Though this is unlikely, other architectures put uc_sigmask
29 * at the end of this structure and explicitly state it can be
30 * expanded, so we didn't want to box ourselves in here. */
31 __u8 __unused
[1024 / 8 - sizeof(sigset_t
)];
32 /* We can't put uc_sigmask at the end of this structure because we need
33 * to be able to expand sigcontext in the future. For example, the
34 * vector ISA extension will almost certainly add ISA state. We want
35 * to ensure all user-visible ISA state can be saved and restored via a
36 * ucontext, so we're putting this at the end in order to allow for
37 * infinite extensibility. Since we know this will be extended and we
38 * assume sigset_t won't be extended an extreme amount, we're
39 * prioritizing this. */
40 struct compat_sigcontext uc_mcontext
;
43 struct compat_rt_sigframe
{
44 struct compat_siginfo info
;
45 struct compat_ucontext uc
;
49 static long compat_restore_fp_state(struct pt_regs
*regs
,
50 union __riscv_fp_state __user
*sc_fpregs
)
53 struct __riscv_d_ext_state __user
*state
= &sc_fpregs
->d
;
56 err
= __copy_from_user(¤t
->thread
.fstate
, state
, sizeof(*state
));
60 fstate_restore(current
, regs
);
62 /* We support no other extension state at this time. */
63 for (i
= 0; i
< ARRAY_SIZE(sc_fpregs
->q
.reserved
); i
++) {
66 err
= __get_user(value
, &sc_fpregs
->q
.reserved
[i
]);
76 static long compat_save_fp_state(struct pt_regs
*regs
,
77 union __riscv_fp_state __user
*sc_fpregs
)
80 struct __riscv_d_ext_state __user
*state
= &sc_fpregs
->d
;
83 fstate_save(current
, regs
);
84 err
= __copy_to_user(state
, ¤t
->thread
.fstate
, sizeof(*state
));
88 /* We support no other extension state at this time. */
89 for (i
= 0; i
< ARRAY_SIZE(sc_fpregs
->q
.reserved
); i
++) {
90 err
= __put_user(0, &sc_fpregs
->q
.reserved
[i
]);
98 #define compat_save_fp_state(task, regs) (0)
99 #define compat_restore_fp_state(task, regs) (0)
102 static long compat_restore_sigcontext(struct pt_regs
*regs
,
103 struct compat_sigcontext __user
*sc
)
106 struct compat_user_regs_struct cregs
;
108 /* sc_regs is structured the same as the start of pt_regs */
109 err
= __copy_from_user(&cregs
, &sc
->sc_regs
, sizeof(sc
->sc_regs
));
111 cregs_to_regs(&cregs
, regs
);
113 /* Restore the floating-point state. */
115 err
|= compat_restore_fp_state(regs
, &sc
->sc_fpregs
);
119 COMPAT_SYSCALL_DEFINE0(rt_sigreturn
)
121 struct pt_regs
*regs
= current_pt_regs();
122 struct compat_rt_sigframe __user
*frame
;
123 struct task_struct
*task
;
126 /* Always make any pending restarted system calls return -EINTR */
127 current
->restart_block
.fn
= do_no_restart_syscall
;
129 frame
= (struct compat_rt_sigframe __user
*)regs
->sp
;
131 if (!access_ok(frame
, sizeof(*frame
)))
134 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
137 set_current_blocked(&set
);
139 if (compat_restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
142 if (compat_restore_altstack(&frame
->uc
.uc_stack
))
149 if (show_unhandled_signals
) {
151 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
152 task
->comm
, task_pid_nr(task
), __func__
,
153 frame
, (void *)regs
->epc
, (void *)regs
->sp
);
159 static long compat_setup_sigcontext(struct compat_rt_sigframe __user
*frame
,
160 struct pt_regs
*regs
)
162 struct compat_sigcontext __user
*sc
= &frame
->uc
.uc_mcontext
;
163 struct compat_user_regs_struct cregs
;
166 regs_to_cregs(&cregs
, regs
);
168 /* sc_regs is structured the same as the start of pt_regs */
169 err
= __copy_to_user(&sc
->sc_regs
, &cregs
, sizeof(sc
->sc_regs
));
170 /* Save the floating-point state. */
172 err
|= compat_save_fp_state(regs
, &sc
->sc_fpregs
);
176 static inline void __user
*compat_get_sigframe(struct ksignal
*ksig
,
177 struct pt_regs
*regs
, size_t framesize
)
180 /* Default to using normal stack */
184 * If we are on the alternate signal stack and would overflow it, don't.
185 * Return an always-bogus address instead so we will die with SIGSEGV.
187 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
188 return (void __user __force
*)(-1UL);
190 /* This is the X/Open sanctioned signal stack switching. */
191 sp
= sigsp(sp
, ksig
) - framesize
;
193 /* Align the stack frame. */
196 return (void __user
*)sp
;
199 int compat_setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
200 struct pt_regs
*regs
)
202 struct compat_rt_sigframe __user
*frame
;
205 frame
= compat_get_sigframe(ksig
, regs
, sizeof(*frame
));
206 if (!access_ok(frame
, sizeof(*frame
)))
209 err
|= copy_siginfo_to_user32(&frame
->info
, &ksig
->info
);
211 /* Create the ucontext. */
212 err
|= __put_user(0, &frame
->uc
.uc_flags
);
213 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
214 err
|= __compat_save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
215 err
|= compat_setup_sigcontext(frame
, regs
);
216 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
220 regs
->ra
= (unsigned long)COMPAT_VDSO_SYMBOL(
221 current
->mm
->context
.vdso
, rt_sigreturn
);
224 * Set up registers for signal handler.
225 * Registers that we don't modify keep the value they had from
226 * user-space at the time we took the signal.
227 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
228 * since some things rely on this (e.g. glibc's debug/segfault.c).
230 regs
->epc
= (unsigned long)ksig
->ka
.sa
.sa_handler
;
231 regs
->sp
= (unsigned long)frame
;
232 regs
->a0
= ksig
->sig
; /* a0: signal number */
233 regs
->a1
= (unsigned long)(&frame
->info
); /* a1: siginfo pointer */
234 regs
->a2
= (unsigned long)(&frame
->uc
); /* a2: ucontext pointer */
237 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
238 current
->comm
, task_pid_nr(current
), ksig
->sig
,
239 (void *)regs
->epc
, (void *)regs
->ra
, frame
);