treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / hexagon / kernel / signal.c
blobd48864c48e5ac49cf6210a35b793e4cfc710fa3c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Signal support for Hexagon processor
5 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
6 */
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>
20 #include <asm/vdso.h>
22 struct rt_sigframe {
23 unsigned long tramp[2];
24 struct siginfo info;
25 struct ucontext uc;
28 static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
29 size_t frame_size)
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)
38 unsigned long tmp;
39 int err = 0;
41 err |= copy_to_user(&sc->sc_regs.r0, &regs->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);
57 #endif
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);
62 return err;
65 static int restore_sigcontext(struct pt_regs *regs,
66 struct sigcontext __user *sc)
68 unsigned long tmp;
69 int err = 0;
71 err |= copy_from_user(&regs->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);
87 #endif
88 err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
90 return err;
94 * Setup signal stack frame with siginfo structure
96 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
97 struct pt_regs *regs)
99 int err = 0;
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)))
106 return -EFAULT;
108 if (copy_siginfo_to_user(&frame->info, &ksig->info))
109 return -EFAULT;
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));
120 if (err)
121 return -EFAULT;
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);
131 return 0;
135 * Setup invocation of signal handler
137 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
139 int ret;
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) {
148 switch (regs->r00) {
149 case -ERESTART_RESTARTBLOCK:
150 case -ERESTARTNOHAND:
151 regs->r00 = -EINTR;
152 break;
153 case -ERESTARTSYS:
154 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
155 regs->r00 = -EINTR;
156 break;
158 /* Fall through */
159 case -ERESTARTNOINTR:
160 regs->r06 = regs->syscall_nr;
161 pt_set_elr(regs, pt_elr(regs) - 4);
162 regs->r00 = regs->restart_r0;
163 break;
164 default:
165 break;
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)
184 struct ksignal ksig;
186 if (!user_mode(regs))
187 return;
189 if (get_signal(&ksig)) {
190 handle_signal(&ksig, regs);
191 return;
195 * No (more) signals; if we came from a system call, handle the restart.
198 if (regs->syscall_nr >= 0) {
199 switch (regs->r00) {
200 case -ERESTARTNOHAND:
201 case -ERESTARTSYS:
202 case -ERESTARTNOINTR:
203 regs->r06 = regs->syscall_nr;
204 break;
205 case -ERESTART_RESTARTBLOCK:
206 regs->r06 = __NR_restart_syscall;
207 break;
208 default:
209 goto no_restart;
211 pt_set_elr(regs, pt_elr(regs) - 4);
212 regs->r00 = regs->restart_r0;
215 no_restart:
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;
228 sigset_t blocked;
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)))
235 goto badframe;
236 if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
237 goto badframe;
239 set_current_blocked(&blocked);
241 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
242 goto badframe;
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))
250 goto badframe;
252 return regs->r00;
254 badframe:
255 force_sig(SIGSEGV);
256 return 0;