mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / m32r / kernel / signal.c
blobba4d8d6330f1538a6a74226bc7cf01072a9b2626
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/arch/m32r/kernel/signal.c
5 * Copyright (c) 2003 Hitoshi Yamamoto
7 * Taken from i386 version.
8 * Copyright (C) 1991, 1992 Linus Torvalds
10 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
11 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/personality.h>
24 #include <linux/tracehook.h>
25 #include <asm/cacheflush.h>
26 #include <asm/ucontext.h>
27 #include <linux/uaccess.h>
29 #define DEBUG_SIG 0
32 * Do a signal return; undo the signal stack.
35 struct rt_sigframe
37 int sig;
38 struct siginfo __user *pinfo;
39 void __user *puc;
40 struct siginfo info;
41 struct ucontext uc;
42 // struct _fpstate fpstate;
45 static int
46 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
47 int *r0_p)
49 unsigned int err = 0;
51 /* Always make any pending restarted system calls return -EINTR */
52 current->restart_block.fn = do_no_restart_syscall;
54 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
55 COPY(r4);
56 COPY(r5);
57 COPY(r6);
58 COPY(pt_regs);
59 /* COPY(r0); Skip r0 */
60 COPY(r1);
61 COPY(r2);
62 COPY(r3);
63 COPY(r7);
64 COPY(r8);
65 COPY(r9);
66 COPY(r10);
67 COPY(r11);
68 COPY(r12);
69 COPY(acc0h);
70 COPY(acc0l);
71 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
72 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
73 COPY(psw);
74 COPY(bpc);
75 COPY(bbpsw);
76 COPY(bbpc);
77 COPY(spu);
78 COPY(fp);
79 COPY(lr);
80 COPY(spi);
81 #undef COPY
83 regs->syscall_nr = -1; /* disable syscall checks */
84 err |= __get_user(*r0_p, &sc->sc_r0);
86 return err;
89 asmlinkage int
90 sys_rt_sigreturn(unsigned long r0, unsigned long r1,
91 unsigned long r2, unsigned long r3, unsigned long r4,
92 unsigned long r5, unsigned long r6, struct pt_regs *regs)
94 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
95 sigset_t set;
96 int result;
98 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
99 goto badframe;
100 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
101 goto badframe;
103 set_current_blocked(&set);
105 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
106 goto badframe;
108 if (restore_altstack(&frame->uc.uc_stack))
109 goto badframe;
111 return result;
113 badframe:
114 force_sig(SIGSEGV, current);
115 return 0;
119 * Set up a signal frame.
122 static int
123 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
124 unsigned long mask)
126 int err = 0;
128 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
129 COPY(r4);
130 COPY(r5);
131 COPY(r6);
132 COPY(pt_regs);
133 COPY(r0);
134 COPY(r1);
135 COPY(r2);
136 COPY(r3);
137 COPY(r7);
138 COPY(r8);
139 COPY(r9);
140 COPY(r10);
141 COPY(r11);
142 COPY(r12);
143 COPY(acc0h);
144 COPY(acc0l);
145 COPY(acc1h); /* ISA_DSP_LEVEL2 only */
146 COPY(acc1l); /* ISA_DSP_LEVEL2 only */
147 COPY(psw);
148 COPY(bpc);
149 COPY(bbpsw);
150 COPY(bbpc);
151 COPY(spu);
152 COPY(fp);
153 COPY(lr);
154 COPY(spi);
155 #undef COPY
157 err |= __put_user(mask, &sc->oldmask);
159 return err;
163 * Determine which stack to use..
165 static inline void __user *
166 get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
168 return (void __user *)((sigsp(sp, ksig) - frame_size) & -8ul);
171 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
172 struct pt_regs *regs)
174 struct rt_sigframe __user *frame;
175 int err = 0;
176 int sig = ksig->sig;
178 frame = get_sigframe(ksig, regs->spu, sizeof(*frame));
180 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
181 return -EFAULT;
183 err |= __put_user(sig, &frame->sig);
184 if (err)
185 return -EFAULT;
187 err |= __put_user(&frame->info, &frame->pinfo);
188 err |= __put_user(&frame->uc, &frame->puc);
189 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
190 if (err)
191 return -EFAULT;
193 /* Create the ucontext. */
194 err |= __put_user(0, &frame->uc.uc_flags);
195 err |= __put_user(0, &frame->uc.uc_link);
196 err |= __save_altstack(&frame->uc.uc_stack, regs->spu);
197 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
198 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
199 if (err)
200 return -EFAULT;
202 /* Set up to return from userspace. */
203 regs->lr = (unsigned long)ksig->ka.sa.sa_restorer;
205 /* Set up registers for signal handler */
206 regs->spu = (unsigned long)frame;
207 regs->r0 = sig; /* Arg for signal handler */
208 regs->r1 = (unsigned long)&frame->info;
209 regs->r2 = (unsigned long)&frame->uc;
210 regs->bpc = (unsigned long)ksig->ka.sa.sa_handler;
212 #if DEBUG_SIG
213 printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
214 current->comm, current->pid, frame, regs->pc);
215 #endif
217 return 0;
220 static int prev_insn(struct pt_regs *regs)
222 u16 inst;
223 if (get_user(inst, (u16 __user *)(regs->bpc - 2)))
224 return -EFAULT;
225 if ((inst & 0xfff0) == 0x10f0) /* trap ? */
226 regs->bpc -= 2;
227 else
228 regs->bpc -= 4;
229 regs->syscall_nr = -1;
230 return 0;
234 * OK, we're invoking a handler
237 static void
238 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
240 int ret;
242 /* Are we from a system call? */
243 if (regs->syscall_nr >= 0) {
244 /* If so, check system call restarting.. */
245 switch (regs->r0) {
246 case -ERESTART_RESTARTBLOCK:
247 case -ERESTARTNOHAND:
248 regs->r0 = -EINTR;
249 break;
251 case -ERESTARTSYS:
252 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
253 regs->r0 = -EINTR;
254 break;
256 /* fallthrough */
257 case -ERESTARTNOINTR:
258 regs->r0 = regs->orig_r0;
259 if (prev_insn(regs) < 0)
260 return;
264 /* Set up the stack frame */
265 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
267 signal_setup_done(ret, ksig, 0);
271 * Note that 'init' is a special process: it doesn't get signals it doesn't
272 * want to handle. Thus you cannot kill init even with a SIGKILL even by
273 * mistake.
275 static void do_signal(struct pt_regs *regs)
277 struct ksignal ksig;
280 * We want the common case to go fast, which
281 * is why we may in certain cases get here from
282 * kernel mode. Just return without doing anything
283 * if so.
285 if (!user_mode(regs))
286 return;
288 if (get_signal(&ksig)) {
289 /* Re-enable any watchpoints before delivering the
290 * signal to user space. The processor register will
291 * have been cleared if the watchpoint triggered
292 * inside the kernel.
295 /* Whee! Actually deliver the signal. */
296 handle_signal(&ksig, regs);
298 return;
301 /* Did we come from a system call? */
302 if (regs->syscall_nr >= 0) {
303 /* Restart the system call - no handlers present */
304 if (regs->r0 == -ERESTARTNOHAND ||
305 regs->r0 == -ERESTARTSYS ||
306 regs->r0 == -ERESTARTNOINTR) {
307 regs->r0 = regs->orig_r0;
308 prev_insn(regs);
309 } else if (regs->r0 == -ERESTART_RESTARTBLOCK){
310 regs->r0 = regs->orig_r0;
311 regs->r7 = __NR_restart_syscall;
312 prev_insn(regs);
315 restore_saved_sigmask();
319 * notification of userspace execution resumption
320 * - triggered by current->work.notify_resume
322 void do_notify_resume(struct pt_regs *regs, __u32 thread_info_flags)
324 /* Pending single-step? */
325 if (thread_info_flags & _TIF_SINGLESTEP)
326 clear_thread_flag(TIF_SINGLESTEP);
328 /* deal with pending signal delivery */
329 if (thread_info_flags & _TIF_SIGPENDING)
330 do_signal(regs);
332 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
333 clear_thread_flag(TIF_NOTIFY_RESUME);
334 tracehook_notify_resume(regs);