4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
8 * Modifications for the OpenRISC architecture:
9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <linux/sched.h>
20 #include <linux/smp.h>
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/errno.h>
24 #include <linux/wait.h>
25 #include <linux/ptrace.h>
26 #include <linux/unistd.h>
27 #include <linux/stddef.h>
28 #include <linux/tracehook.h>
30 #include <asm/processor.h>
31 #include <asm/ucontext.h>
32 #include <asm/uaccess.h>
37 struct siginfo
*pinfo
;
41 unsigned char retcode
[16]; /* trampoline code */
44 static int restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
)
48 /* Alwys make any pending restarted system call return -EINTR */
49 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
52 * Restore the regs from &sc->regs.
53 * (sc is already checked for VERIFY_READ since the sigframe was
54 * checked in sys_sigreturn previously)
56 if (__copy_from_user(regs
, sc
->regs
.gpr
, 32 * sizeof(unsigned long)))
58 if (__copy_from_user(®s
->pc
, &sc
->regs
.pc
, sizeof(unsigned long)))
60 if (__copy_from_user(®s
->sr
, &sc
->regs
.sr
, sizeof(unsigned long)))
63 /* make sure the SM-bit is cleared so user-mode cannot fool us */
64 regs
->sr
&= ~SPR_SR_SM
;
66 /* TODO: the other ports use regs->orig_XX to disable syscall checks
67 * after this completes, but we don't use that mechanism. maybe we can
77 asmlinkage
long _sys_rt_sigreturn(struct pt_regs
*regs
)
79 struct rt_sigframe
*frame
= (struct rt_sigframe __user
*)regs
->sp
;
83 * Since we stacked the signal on a dword boundary,
84 * then frame should be dword aligned here. If it's
85 * not, then the user is trying to mess with us.
87 if (((long)frame
) & 3)
90 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
92 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
95 set_current_blocked(&set
);
97 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
100 if (restore_altstack(&frame
->uc
.uc_stack
))
103 return regs
->gpr
[11];
106 force_sig(SIGSEGV
, current
);
111 * Set up a signal frame.
114 static int setup_sigcontext(struct sigcontext
*sc
, struct pt_regs
*regs
,
121 err
|= __copy_to_user(sc
->regs
.gpr
, regs
, 32 * sizeof(unsigned long));
122 err
|= __copy_to_user(&sc
->regs
.pc
, ®s
->pc
, sizeof(unsigned long));
123 err
|= __copy_to_user(&sc
->regs
.sr
, ®s
->sr
, sizeof(unsigned long));
125 /* then some other stuff */
127 err
|= __put_user(mask
, &sc
->oldmask
);
132 static inline unsigned long align_sigframe(unsigned long sp
)
138 * Work out where the signal frame should go. It's either on the user stack
139 * or the alternate stack.
142 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
143 struct pt_regs
*regs
, size_t frame_size
)
145 unsigned long sp
= regs
->sp
;
146 int onsigstack
= on_sig_stack(sp
);
149 sp
-= STACK_FRAME_OVERHEAD
;
151 /* This is the X/Open sanctioned signal stack switching. */
152 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && !onsigstack
) {
153 if (current
->sas_ss_size
)
154 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
157 sp
= align_sigframe(sp
- frame_size
);
160 * If we are on the alternate signal stack and would overflow it, don't.
161 * Return an always-bogus address instead so we will die with SIGSEGV.
163 if (onsigstack
&& !likely(on_sig_stack(sp
)))
164 return (void __user
*)-1L;
166 return (void __user
*)sp
;
169 /* grab and setup a signal frame.
171 * basically we stack a lot of state info, and arrange for the
172 * user-mode program to return to the kernel using either a
173 * trampoline which performs the syscall sigreturn, or a provided
174 * user-mode trampoline.
176 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
177 sigset_t
*set
, struct pt_regs
*regs
)
179 struct rt_sigframe
*frame
;
180 unsigned long return_ip
;
183 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
185 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
188 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
189 err
|= __put_user(&frame
->uc
, &frame
->puc
);
191 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
192 err
|= copy_siginfo_to_user(&frame
->info
, info
);
196 /* Clear all the bits of the ucontext we don't use. */
197 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
198 err
|= __put_user(0, &frame
->uc
.uc_flags
);
199 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
200 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
201 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
203 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
208 /* trampoline - the desired return ip is the retcode itself */
209 return_ip
= (unsigned long)&frame
->retcode
;
210 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
211 err
|= __put_user(0xa960, (short *)(frame
->retcode
+ 0));
212 err
|= __put_user(__NR_rt_sigreturn
, (short *)(frame
->retcode
+ 2));
213 err
|= __put_user(0x20000001, (unsigned long *)(frame
->retcode
+ 4));
214 err
|= __put_user(0x15000000, (unsigned long *)(frame
->retcode
+ 8));
219 /* TODO what is the current->exec_domain stuff and invmap ? */
221 /* Set up registers for signal handler */
222 regs
->pc
= (unsigned long)ka
->sa
.sa_handler
; /* what we enter NOW */
223 regs
->gpr
[9] = (unsigned long)return_ip
; /* what we enter LATER */
224 regs
->gpr
[3] = (unsigned long)sig
; /* arg 1: signo */
225 regs
->gpr
[4] = (unsigned long)&frame
->info
; /* arg 2: (siginfo_t*) */
226 regs
->gpr
[5] = (unsigned long)&frame
->uc
; /* arg 3: ucontext */
228 /* actually move the usp to reflect the stacked frame */
229 regs
->sp
= (unsigned long)frame
;
234 force_sigsegv(sig
, current
);
239 handle_signal(unsigned long sig
,
240 siginfo_t
*info
, struct k_sigaction
*ka
,
241 struct pt_regs
*regs
)
245 ret
= setup_rt_frame(sig
, ka
, info
, sigmask_to_save(), regs
);
249 signal_delivered(sig
, info
, ka
, regs
,
250 test_thread_flag(TIF_SINGLESTEP
));
254 * Note that 'init' is a special process: it doesn't get signals it doesn't
255 * want to handle. Thus you cannot kill init even with a SIGKILL even by
258 * Also note that the regs structure given here as an argument, is the latest
259 * pushed pt_regs. It may or may not be the same as the first pushed registers
260 * when the initial usermode->kernelmode transition took place. Therefore
261 * we can use user_mode(regs) to see if we came directly from kernel or user
265 void do_signal(struct pt_regs
*regs
)
269 struct k_sigaction ka
;
272 * We want the common case to go fast, which
273 * is why we may in certain cases get here from
274 * kernel mode. Just return without doing anything
277 if (!user_mode(regs
))
280 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
282 /* If we are coming out of a syscall then we need
283 * to check if the syscall was interrupted and wants to be
284 * restarted after handling the signal. If so, the original
285 * syscall number is put back into r11 and the PC rewound to
286 * point at the l.sys instruction that resulted in the
287 * original syscall. Syscall results other than the four
288 * below mean that the syscall executed to completion and no
289 * restart is necessary.
291 if (regs
->orig_gpr11
) {
294 switch (regs
->gpr
[11]) {
295 case -ERESTART_RESTARTBLOCK
:
296 case -ERESTARTNOHAND
:
297 /* Restart if there is no signal handler */
298 restart
= (signr
<= 0);
301 /* Restart if there no signal handler or
302 * SA_RESTART flag is set */
303 restart
= (signr
<= 0 || (ka
.sa
.sa_flags
& SA_RESTART
));
305 case -ERESTARTNOINTR
:
312 if (regs
->gpr
[11] == -ERESTART_RESTARTBLOCK
)
313 regs
->gpr
[11] = __NR_restart_syscall
;
315 regs
->gpr
[11] = regs
->orig_gpr11
;
318 regs
->gpr
[11] = -EINTR
;
323 /* no signal to deliver so we just put the saved sigmask
325 restore_saved_sigmask();
326 } else { /* signr > 0 */
327 /* Whee! Actually deliver the signal. */
328 handle_signal(signr
, &info
, &ka
, regs
);
334 asmlinkage
void do_notify_resume(struct pt_regs
*regs
)
336 if (current_thread_info()->flags
& _TIF_SIGPENDING
)
339 if (current_thread_info()->flags
& _TIF_NOTIFY_RESUME
) {
340 clear_thread_flag(TIF_NOTIFY_RESUME
);
341 tracehook_notify_resume(regs
);