1 // SPDX-License-Identifier: GPL-2.0-only
3 * Signal support for Hexagon processor
5 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
8 #include <linux/linkage.h>
9 #include <linux/syscalls.h>
10 #include <linux/sched/task_stack.h>
12 #include <asm/registers.h>
13 #include <asm/thread_info.h>
14 #include <asm/unistd.h>
15 #include <linux/uaccess.h>
16 #include <asm/ucontext.h>
17 #include <asm/cacheflush.h>
18 #include <asm/signal.h>
22 unsigned long tramp
[2];
27 static void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
,
30 unsigned long sp
= sigsp(regs
->r29
, ksig
);
32 return (void __user
*)((sp
- frame_size
) & ~(sizeof(long long) - 1));
35 static int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
40 err
|= copy_to_user(&sc
->sc_regs
.r0
, ®s
->r00
,
41 32*sizeof(unsigned long));
43 err
|= __put_user(regs
->sa0
, &sc
->sc_regs
.sa0
);
44 err
|= __put_user(regs
->lc0
, &sc
->sc_regs
.lc0
);
45 err
|= __put_user(regs
->sa1
, &sc
->sc_regs
.sa1
);
46 err
|= __put_user(regs
->lc1
, &sc
->sc_regs
.lc1
);
47 err
|= __put_user(regs
->m0
, &sc
->sc_regs
.m0
);
48 err
|= __put_user(regs
->m1
, &sc
->sc_regs
.m1
);
49 err
|= __put_user(regs
->usr
, &sc
->sc_regs
.usr
);
50 err
|= __put_user(regs
->preds
, &sc
->sc_regs
.p3_0
);
51 err
|= __put_user(regs
->gp
, &sc
->sc_regs
.gp
);
52 err
|= __put_user(regs
->ugp
, &sc
->sc_regs
.ugp
);
53 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
54 err
|= __put_user(regs
->cs0
, &sc
->sc_regs
.cs0
);
55 err
|= __put_user(regs
->cs1
, &sc
->sc_regs
.cs1
);
57 tmp
= pt_elr(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.pc
);
58 tmp
= pt_cause(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.cause
);
59 tmp
= pt_badva(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.badva
);
64 static int restore_sigcontext(struct pt_regs
*regs
,
65 struct sigcontext __user
*sc
)
70 err
|= copy_from_user(®s
->r00
, &sc
->sc_regs
.r0
,
71 32 * sizeof(unsigned long));
73 err
|= __get_user(regs
->sa0
, &sc
->sc_regs
.sa0
);
74 err
|= __get_user(regs
->lc0
, &sc
->sc_regs
.lc0
);
75 err
|= __get_user(regs
->sa1
, &sc
->sc_regs
.sa1
);
76 err
|= __get_user(regs
->lc1
, &sc
->sc_regs
.lc1
);
77 err
|= __get_user(regs
->m0
, &sc
->sc_regs
.m0
);
78 err
|= __get_user(regs
->m1
, &sc
->sc_regs
.m1
);
79 err
|= __get_user(regs
->usr
, &sc
->sc_regs
.usr
);
80 err
|= __get_user(regs
->preds
, &sc
->sc_regs
.p3_0
);
81 err
|= __get_user(regs
->gp
, &sc
->sc_regs
.gp
);
82 err
|= __get_user(regs
->ugp
, &sc
->sc_regs
.ugp
);
83 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
84 err
|= __get_user(regs
->cs0
, &sc
->sc_regs
.cs0
);
85 err
|= __get_user(regs
->cs1
, &sc
->sc_regs
.cs1
);
87 err
|= __get_user(tmp
, &sc
->sc_regs
.pc
); pt_set_elr(regs
, tmp
);
93 * Setup signal stack frame with siginfo structure
95 static int setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
99 struct rt_sigframe __user
*frame
;
100 struct hexagon_vdso
*vdso
= current
->mm
->context
.vdso
;
102 frame
= get_sigframe(ksig
, regs
, sizeof(struct rt_sigframe
));
104 if (!access_ok(frame
, sizeof(struct rt_sigframe
)))
107 if (copy_siginfo_to_user(&frame
->info
, &ksig
->info
))
110 /* The on-stack signal trampoline is no longer executed;
111 * however, the libgcc signal frame unwinding code checks for
112 * the presence of these two numeric magic values.
114 err
|= __put_user(0x7800d166, &frame
->tramp
[0]);
115 err
|= __put_user(0x5400c004, &frame
->tramp
[1]);
116 err
|= setup_sigcontext(regs
, &frame
->uc
.uc_mcontext
);
117 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
118 err
|= __save_altstack(&frame
->uc
.uc_stack
, user_stack_pointer(regs
));
122 /* Load r0/r1 pair with signumber/siginfo pointer... */
123 regs
->r0100
= ((unsigned long long)((unsigned long)&frame
->info
) << 32)
124 | (unsigned long long)ksig
->sig
;
125 regs
->r02
= (unsigned long) &frame
->uc
;
126 regs
->r31
= (unsigned long) vdso
->rt_signal_trampoline
;
127 pt_psp(regs
) = (unsigned long) frame
;
128 pt_set_elr(regs
, (unsigned long)ksig
->ka
.sa
.sa_handler
);
134 * Setup invocation of signal handler
136 static void handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
141 * If we're handling a signal that aborted a system call,
142 * set up the error return value before adding the signal
143 * frame to the stack.
146 if (regs
->syscall_nr
>= 0) {
148 case -ERESTART_RESTARTBLOCK
:
149 case -ERESTARTNOHAND
:
153 if (!(ksig
->ka
.sa
.sa_flags
& SA_RESTART
)) {
158 case -ERESTARTNOINTR
:
159 regs
->r06
= regs
->syscall_nr
;
160 pt_set_elr(regs
, pt_elr(regs
) - 4);
161 regs
->r00
= regs
->restart_r0
;
169 * Set up the stack frame; not doing the SA_SIGINFO thing. We
170 * only set up the rt_frame flavor.
172 /* If there was an error on setup, no signal was delivered. */
173 ret
= setup_rt_frame(ksig
, sigmask_to_save(), regs
);
175 signal_setup_done(ret
, ksig
, test_thread_flag(TIF_SINGLESTEP
));
179 * Called from return-from-event code.
181 void do_signal(struct pt_regs
*regs
)
185 if (!user_mode(regs
))
188 if (get_signal(&ksig
)) {
189 handle_signal(&ksig
, regs
);
194 * No (more) signals; if we came from a system call, handle the restart.
197 if (regs
->syscall_nr
>= 0) {
199 case -ERESTARTNOHAND
:
201 case -ERESTARTNOINTR
:
202 regs
->r06
= regs
->syscall_nr
;
204 case -ERESTART_RESTARTBLOCK
:
205 regs
->r06
= __NR_restart_syscall
;
210 pt_set_elr(regs
, pt_elr(regs
) - 4);
211 regs
->r00
= regs
->restart_r0
;
215 /* If there's no signal to deliver, put the saved sigmask back */
216 restore_saved_sigmask();
220 * Architecture-specific wrappers for signal-related system calls
223 SYSCALL_DEFINE0(rt_sigreturn
)
225 struct pt_regs
*regs
= current_pt_regs();
226 struct rt_sigframe __user
*frame
;
229 /* Always make any pending restarted system calls return -EINTR */
230 current
->restart_block
.fn
= do_no_restart_syscall
;
232 frame
= (struct rt_sigframe __user
*)pt_psp(regs
);
233 if (!access_ok(frame
, sizeof(*frame
)))
235 if (__copy_from_user(&blocked
, &frame
->uc
.uc_sigmask
, sizeof(blocked
)))
238 set_current_blocked(&blocked
);
240 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
243 /* Restore the user's stack as well */
244 pt_psp(regs
) = regs
->r29
;
246 regs
->syscall_nr
= -1;
248 if (restore_altstack(&frame
->uc
.uc_stack
))