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/tracehook.h>
11 #include <linux/sched/task_stack.h>
13 #include <asm/registers.h>
14 #include <asm/thread_info.h>
15 #include <asm/unistd.h>
16 #include <linux/uaccess.h>
17 #include <asm/ucontext.h>
18 #include <asm/cacheflush.h>
19 #include <asm/signal.h>
23 unsigned long tramp
[2];
28 static void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
,
31 unsigned long sp
= sigsp(regs
->r29
, ksig
);
33 return (void __user
*)((sp
- frame_size
) & ~(sizeof(long long) - 1));
36 static int setup_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
41 err
|= copy_to_user(&sc
->sc_regs
.r0
, ®s
->r00
,
42 32*sizeof(unsigned long));
44 err
|= __put_user(regs
->sa0
, &sc
->sc_regs
.sa0
);
45 err
|= __put_user(regs
->lc0
, &sc
->sc_regs
.lc0
);
46 err
|= __put_user(regs
->sa1
, &sc
->sc_regs
.sa1
);
47 err
|= __put_user(regs
->lc1
, &sc
->sc_regs
.lc1
);
48 err
|= __put_user(regs
->m0
, &sc
->sc_regs
.m0
);
49 err
|= __put_user(regs
->m1
, &sc
->sc_regs
.m1
);
50 err
|= __put_user(regs
->usr
, &sc
->sc_regs
.usr
);
51 err
|= __put_user(regs
->preds
, &sc
->sc_regs
.p3_0
);
52 err
|= __put_user(regs
->gp
, &sc
->sc_regs
.gp
);
53 err
|= __put_user(regs
->ugp
, &sc
->sc_regs
.ugp
);
54 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
55 err
|= __put_user(regs
->cs0
, &sc
->sc_regs
.cs0
);
56 err
|= __put_user(regs
->cs1
, &sc
->sc_regs
.cs1
);
58 tmp
= pt_elr(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.pc
);
59 tmp
= pt_cause(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.cause
);
60 tmp
= pt_badva(regs
); err
|= __put_user(tmp
, &sc
->sc_regs
.badva
);
65 static int restore_sigcontext(struct pt_regs
*regs
,
66 struct sigcontext __user
*sc
)
71 err
|= copy_from_user(®s
->r00
, &sc
->sc_regs
.r0
,
72 32 * sizeof(unsigned long));
74 err
|= __get_user(regs
->sa0
, &sc
->sc_regs
.sa0
);
75 err
|= __get_user(regs
->lc0
, &sc
->sc_regs
.lc0
);
76 err
|= __get_user(regs
->sa1
, &sc
->sc_regs
.sa1
);
77 err
|= __get_user(regs
->lc1
, &sc
->sc_regs
.lc1
);
78 err
|= __get_user(regs
->m0
, &sc
->sc_regs
.m0
);
79 err
|= __get_user(regs
->m1
, &sc
->sc_regs
.m1
);
80 err
|= __get_user(regs
->usr
, &sc
->sc_regs
.usr
);
81 err
|= __get_user(regs
->preds
, &sc
->sc_regs
.p3_0
);
82 err
|= __get_user(regs
->gp
, &sc
->sc_regs
.gp
);
83 err
|= __get_user(regs
->ugp
, &sc
->sc_regs
.ugp
);
84 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
85 err
|= __get_user(regs
->cs0
, &sc
->sc_regs
.cs0
);
86 err
|= __get_user(regs
->cs1
, &sc
->sc_regs
.cs1
);
88 err
|= __get_user(tmp
, &sc
->sc_regs
.pc
); pt_set_elr(regs
, tmp
);
94 * Setup signal stack frame with siginfo structure
96 static int setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
100 struct rt_sigframe __user
*frame
;
101 struct hexagon_vdso
*vdso
= current
->mm
->context
.vdso
;
103 frame
= get_sigframe(ksig
, regs
, sizeof(struct rt_sigframe
));
105 if (!access_ok(frame
, sizeof(struct rt_sigframe
)))
108 if (copy_siginfo_to_user(&frame
->info
, &ksig
->info
))
111 /* The on-stack signal trampoline is no longer executed;
112 * however, the libgcc signal frame unwinding code checks for
113 * the presence of these two numeric magic values.
115 err
|= __put_user(0x7800d166, &frame
->tramp
[0]);
116 err
|= __put_user(0x5400c004, &frame
->tramp
[1]);
117 err
|= setup_sigcontext(regs
, &frame
->uc
.uc_mcontext
);
118 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
119 err
|= __save_altstack(&frame
->uc
.uc_stack
, user_stack_pointer(regs
));
123 /* Load r0/r1 pair with signumber/siginfo pointer... */
124 regs
->r0100
= ((unsigned long long)((unsigned long)&frame
->info
) << 32)
125 | (unsigned long long)ksig
->sig
;
126 regs
->r02
= (unsigned long) &frame
->uc
;
127 regs
->r31
= (unsigned long) vdso
->rt_signal_trampoline
;
128 pt_psp(regs
) = (unsigned long) frame
;
129 pt_set_elr(regs
, (unsigned long)ksig
->ka
.sa
.sa_handler
);
135 * Setup invocation of signal handler
137 static void handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
142 * If we're handling a signal that aborted a system call,
143 * set up the error return value before adding the signal
144 * frame to the stack.
147 if (regs
->syscall_nr
>= 0) {
149 case -ERESTART_RESTARTBLOCK
:
150 case -ERESTARTNOHAND
:
154 if (!(ksig
->ka
.sa
.sa_flags
& SA_RESTART
)) {
159 case -ERESTARTNOINTR
:
160 regs
->r06
= regs
->syscall_nr
;
161 pt_set_elr(regs
, pt_elr(regs
) - 4);
162 regs
->r00
= regs
->restart_r0
;
170 * Set up the stack frame; not doing the SA_SIGINFO thing. We
171 * only set up the rt_frame flavor.
173 /* If there was an error on setup, no signal was delivered. */
174 ret
= setup_rt_frame(ksig
, sigmask_to_save(), regs
);
176 signal_setup_done(ret
, ksig
, test_thread_flag(TIF_SINGLESTEP
));
180 * Called from return-from-event code.
182 void do_signal(struct pt_regs
*regs
)
186 if (!user_mode(regs
))
189 if (get_signal(&ksig
)) {
190 handle_signal(&ksig
, regs
);
195 * No (more) signals; if we came from a system call, handle the restart.
198 if (regs
->syscall_nr
>= 0) {
200 case -ERESTARTNOHAND
:
202 case -ERESTARTNOINTR
:
203 regs
->r06
= regs
->syscall_nr
;
205 case -ERESTART_RESTARTBLOCK
:
206 regs
->r06
= __NR_restart_syscall
;
211 pt_set_elr(regs
, pt_elr(regs
) - 4);
212 regs
->r00
= regs
->restart_r0
;
216 /* If there's no signal to deliver, put the saved sigmask back */
217 restore_saved_sigmask();
221 * Architecture-specific wrappers for signal-related system calls
224 asmlinkage
int sys_rt_sigreturn(void)
226 struct pt_regs
*regs
= current_pt_regs();
227 struct rt_sigframe __user
*frame
;
230 /* Always make any pending restarted system calls return -EINTR */
231 current
->restart_block
.fn
= do_no_restart_syscall
;
233 frame
= (struct rt_sigframe __user
*)pt_psp(regs
);
234 if (!access_ok(frame
, sizeof(*frame
)))
236 if (__copy_from_user(&blocked
, &frame
->uc
.uc_sigmask
, sizeof(blocked
)))
239 set_current_blocked(&blocked
);
241 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
244 /* Restore the user's stack as well */
245 pt_psp(regs
) = regs
->r29
;
247 regs
->syscall_nr
= -1;
249 if (restore_altstack(&frame
->uc
.uc_stack
))