2 * Copyright (C) 2013-2014 Altera Corporation
3 * Copyright (C) 2011-2012 Tobias Klauser <tklauser@distanz.ch>
4 * Copyright (C) 2004 Microtronix Datacom Ltd
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive
12 #include <linux/signal.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/uaccess.h>
16 #include <linux/unistd.h>
17 #include <linux/personality.h>
18 #include <linux/resume_user_mode.h>
20 #include <asm/ucontext.h>
21 #include <asm/cacheflush.h>
23 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
26 * Do a signal return; undo the signal stack.
28 * Keep the return code on the stack quadword aligned!
29 * That makes the cache flush below easier.
37 static inline int rt_restore_ucontext(struct pt_regs *regs,
38 struct switch_stack *sw,
39 struct ucontext __user *uc, int *pr2)
42 unsigned long __user *gregs = uc->uc_mcontext.gregs;
45 /* Always make any pending restarted system calls return -EINTR */
46 current->restart_block.fn = do_no_restart_syscall;
48 err = __get_user(temp, &uc->uc_mcontext.version);
49 if (temp != MCONTEXT_VERSION)
51 /* restore passed registers */
52 err |= __get_user(regs->r1, &gregs[0]);
53 err |= __get_user(regs->r2, &gregs[1]);
54 err |= __get_user(regs->r3, &gregs[2]);
55 err |= __get_user(regs->r4, &gregs[3]);
56 err |= __get_user(regs->r5, &gregs[4]);
57 err |= __get_user(regs->r6, &gregs[5]);
58 err |= __get_user(regs->r7, &gregs[6]);
59 err |= __get_user(regs->r8, &gregs[7]);
60 err |= __get_user(regs->r9, &gregs[8]);
61 err |= __get_user(regs->r10, &gregs[9]);
62 err |= __get_user(regs->r11, &gregs[10]);
63 err |= __get_user(regs->r12, &gregs[11]);
64 err |= __get_user(regs->r13, &gregs[12]);
65 err |= __get_user(regs->r14, &gregs[13]);
66 err |= __get_user(regs->r15, &gregs[14]);
67 err |= __get_user(sw->r16, &gregs[15]);
68 err |= __get_user(sw->r17, &gregs[16]);
69 err |= __get_user(sw->r18, &gregs[17]);
70 err |= __get_user(sw->r19, &gregs[18]);
71 err |= __get_user(sw->r20, &gregs[19]);
72 err |= __get_user(sw->r21, &gregs[20]);
73 err |= __get_user(sw->r22, &gregs[21]);
74 err |= __get_user(sw->r23, &gregs[22]);
75 /* gregs[23] is handled below */
76 err |= __get_user(sw->fp, &gregs[24]); /* Verify, should this be
78 err |= __get_user(sw->gp, &gregs[25]); /* Verify, should this be
81 err |= __get_user(temp, &gregs[26]); /* Not really necessary no user
83 err |= __get_user(regs->ea, &gregs[27]);
85 err |= __get_user(regs->ra, &gregs[23]);
86 err |= __get_user(regs->sp, &gregs[28]);
88 regs->orig_r2 = -1; /* disable syscall checks */
90 err |= restore_altstack(&uc->uc_stack);
101 asmlinkage int do_rt_sigreturn(struct switch_stack *sw)
103 struct pt_regs *regs = (struct pt_regs *)(sw + 1);
104 /* Verify, can we follow the stack back */
105 struct rt_sigframe __user *frame;
109 frame = (struct rt_sigframe __user *) regs->sp;
110 if (!access_ok(frame, sizeof(*frame)))
113 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
116 set_current_blocked(&set);
118 if (rt_restore_ucontext(regs, sw, &frame->uc, &rval))
128 static inline int rt_setup_ucontext(struct ucontext __user *uc, struct pt_regs *regs)
130 struct switch_stack *sw = (struct switch_stack *)regs - 1;
131 unsigned long __user *gregs = uc->uc_mcontext.gregs;
134 err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
135 err |= __put_user(regs->r1, &gregs[0]);
136 err |= __put_user(regs->r2, &gregs[1]);
137 err |= __put_user(regs->r3, &gregs[2]);
138 err |= __put_user(regs->r4, &gregs[3]);
139 err |= __put_user(regs->r5, &gregs[4]);
140 err |= __put_user(regs->r6, &gregs[5]);
141 err |= __put_user(regs->r7, &gregs[6]);
142 err |= __put_user(regs->r8, &gregs[7]);
143 err |= __put_user(regs->r9, &gregs[8]);
144 err |= __put_user(regs->r10, &gregs[9]);
145 err |= __put_user(regs->r11, &gregs[10]);
146 err |= __put_user(regs->r12, &gregs[11]);
147 err |= __put_user(regs->r13, &gregs[12]);
148 err |= __put_user(regs->r14, &gregs[13]);
149 err |= __put_user(regs->r15, &gregs[14]);
150 err |= __put_user(sw->r16, &gregs[15]);
151 err |= __put_user(sw->r17, &gregs[16]);
152 err |= __put_user(sw->r18, &gregs[17]);
153 err |= __put_user(sw->r19, &gregs[18]);
154 err |= __put_user(sw->r20, &gregs[19]);
155 err |= __put_user(sw->r21, &gregs[20]);
156 err |= __put_user(sw->r22, &gregs[21]);
157 err |= __put_user(sw->r23, &gregs[22]);
158 err |= __put_user(regs->ra, &gregs[23]);
159 err |= __put_user(sw->fp, &gregs[24]);
160 err |= __put_user(sw->gp, &gregs[25]);
161 err |= __put_user(regs->ea, &gregs[27]);
162 err |= __put_user(regs->sp, &gregs[28]);
166 static inline void __user *get_sigframe(struct ksignal *ksig,
167 struct pt_regs *regs,
172 /* Default to using normal stack. */
175 /* This is the X/Open sanctioned signal stack switching. */
176 usp = sigsp(usp, ksig);
178 /* Verify, is it 32 or 64 bit aligned */
179 return (void __user *)((usp - frame_size) & -8UL);
182 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
183 struct pt_regs *regs)
185 struct rt_sigframe __user *frame;
188 frame = get_sigframe(ksig, regs, sizeof(*frame));
190 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
191 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
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->sp);
197 err |= rt_setup_ucontext(&frame->uc, regs);
198 err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
203 /* Set up to return from userspace; jump to fixed address sigreturn
204 trampoline on kuser page. */
205 regs->ra = (unsigned long) (0x1044);
207 /* Set up registers for signal handler */
208 regs->sp = (unsigned long) frame;
209 regs->r4 = (unsigned long) ksig->sig;
210 regs->r5 = (unsigned long) &frame->info;
211 regs->r6 = (unsigned long) &frame->uc;
212 regs->ea = (unsigned long) ksig->ka.sa.sa_handler;
216 force_sigsegv(ksig->sig);
221 * OK, we're invoking a handler
223 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
226 sigset_t *oldset = sigmask_to_save();
228 /* set up the stack frame */
229 ret = setup_rt_frame(ksig, oldset, regs);
231 signal_setup_done(ret, ksig, 0);
234 static int do_signal(struct pt_regs *regs)
236 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
240 current->thread.kregs = regs;
243 * If we were from a system call, check for system call restarting...
245 if (regs->orig_r2 >= 0) {
246 continue_addr = regs->ea;
247 restart_addr = continue_addr - 4;
251 * Prepare for system call restart. We do this here so that a
252 * debugger will see the already changed PC.
255 case ERESTART_RESTARTBLOCK:
262 regs->r2 = regs->orig_r2;
263 regs->r7 = regs->orig_r7;
264 regs->ea = restart_addr;
269 if (get_signal(&ksig)) {
271 if (unlikely(restart && regs->ea == restart_addr)) {
272 if (retval == ERESTARTNOHAND ||
273 retval == ERESTART_RESTARTBLOCK ||
274 (retval == ERESTARTSYS
275 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
278 regs->ea = continue_addr;
281 handle_signal(&ksig, regs);
288 if (unlikely(restart) && regs->ea == restart_addr) {
289 regs->ea = continue_addr;
290 regs->r2 = __NR_restart_syscall;
294 * If there's no signal to deliver, we just put the saved sigmask back.
296 restore_saved_sigmask();
301 asmlinkage int do_notify_resume(struct pt_regs *regs)
304 * We want the common case to go fast, which is why we may in certain
305 * cases get here from kernel mode. Just return without doing anything
308 if (!user_mode(regs))
311 if (test_thread_flag(TIF_SIGPENDING) ||
312 test_thread_flag(TIF_NOTIFY_SIGNAL)) {
313 int restart = do_signal(regs);
315 if (unlikely(restart)) {
317 * Restart without handlers.
318 * Deal with it without leaving
323 } else if (test_thread_flag(TIF_NOTIFY_RESUME))
324 resume_user_mode_work(regs);