4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2008-2009 PetaLogix
6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7 * Copyright (C) 2001 NEC Corporation
8 * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10 * Copyright (C) 1991,1992 Linus Torvalds
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
21 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/kernel.h>
25 #include <linux/signal.h>
26 #include <linux/errno.h>
27 #include <linux/wait.h>
28 #include <linux/ptrace.h>
29 #include <linux/unistd.h>
30 #include <linux/stddef.h>
31 #include <linux/personality.h>
32 #include <linux/percpu.h>
33 #include <linux/linkage.h>
34 #include <linux/tracehook.h>
35 #include <asm/entry.h>
36 #include <asm/ucontext.h>
37 #include <linux/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/pgalloc.h>
40 #include <linux/syscalls.h>
41 #include <asm/cacheflush.h>
42 #include <asm/syscalls.h>
45 * Do a signal return; undo the signal stack.
49 unsigned long extramask
[_NSIG_WORDS
-1];
50 unsigned long tramp
[2]; /* signal trampoline */
56 unsigned long tramp
[2]; /* signal trampoline */
59 static int restore_sigcontext(struct pt_regs
*regs
,
60 struct sigcontext __user
*sc
, int *rval_p
)
64 #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
67 COPY(r2
); COPY(r3
); COPY(r4
); COPY(r5
);
68 COPY(r6
); COPY(r7
); COPY(r8
); COPY(r9
);
69 COPY(r10
); COPY(r11
); COPY(r12
); COPY(r13
);
70 COPY(r14
); COPY(r15
); COPY(r16
); COPY(r17
);
71 COPY(r18
); COPY(r19
); COPY(r20
); COPY(r21
);
72 COPY(r22
); COPY(r23
); COPY(r24
); COPY(r25
);
73 COPY(r26
); COPY(r27
); COPY(r28
); COPY(r29
);
75 COPY(pc
); COPY(ear
); COPY(esr
); COPY(fsr
);
83 asmlinkage
long sys_rt_sigreturn(struct pt_regs
*regs
)
85 struct rt_sigframe __user
*frame
=
86 (struct rt_sigframe __user
*)(regs
->r1
);
91 /* Always make any pending restarted system calls return -EINTR */
92 current
->restart_block
.fn
= do_no_restart_syscall
;
94 if (!access_ok(frame
, sizeof(*frame
)))
97 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
100 set_current_blocked(&set
);
102 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &rval
))
105 if (restore_altstack(&frame
->uc
.uc_stack
))
116 * Set up a signal frame.
120 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
,
125 #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
128 COPY(r2
); COPY(r3
); COPY(r4
); COPY(r5
);
129 COPY(r6
); COPY(r7
); COPY(r8
); COPY(r9
);
130 COPY(r10
); COPY(r11
); COPY(r12
); COPY(r13
);
131 COPY(r14
); COPY(r15
); COPY(r16
); COPY(r17
);
132 COPY(r18
); COPY(r19
); COPY(r20
); COPY(r21
);
133 COPY(r22
); COPY(r23
); COPY(r24
); COPY(r25
);
134 COPY(r26
); COPY(r27
); COPY(r28
); COPY(r29
);
135 COPY(r30
); COPY(r31
);
136 COPY(pc
); COPY(ear
); COPY(esr
); COPY(fsr
);
139 err
|= __put_user(mask
, &sc
->oldmask
);
145 * Determine which stack to use..
147 static inline void __user
*
148 get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, size_t frame_size
)
150 /* Default to using normal stack */
151 unsigned long sp
= sigsp(regs
->r1
, ksig
);
153 return (void __user
*)((sp
- frame_size
) & -8UL);
156 static int setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
157 struct pt_regs
*regs
)
159 struct rt_sigframe __user
*frame
;
160 int err
= 0, sig
= ksig
->sig
;
161 unsigned long address
= 0;
170 frame
= get_sigframe(ksig
, regs
, sizeof(*frame
));
172 if (!access_ok(frame
, sizeof(*frame
)))
175 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
176 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
178 /* Create the ucontext. */
179 err
|= __put_user(0, &frame
->uc
.uc_flags
);
180 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
181 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->r1
);
182 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
184 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
186 /* Set up to return from userspace. If provided, use a stub
187 already in userspace. */
188 /* minus 8 is offset to cater for "rtsd r15,8" */
189 /* addi r12, r0, __NR_sigreturn */
190 err
|= __put_user(0x31800000 | __NR_rt_sigreturn
,
193 err
|= __put_user(0xb9cc0008, frame
->tramp
+ 1);
195 /* Return from sighandler will jump to the tramp.
196 Negative 8 offset because return is rtsd r15, 8 */
197 regs
->r15
= ((unsigned long)frame
->tramp
)-8;
199 address
= ((unsigned long)frame
->tramp
);
201 pgdp
= pgd_offset(current
->mm
, address
);
202 p4dp
= p4d_offset(pgdp
, address
);
203 pudp
= pud_offset(p4dp
, address
);
204 pmdp
= pmd_offset(pudp
, address
);
207 ptep
= pte_offset_map(pmdp
, address
);
208 if (pte_present(*ptep
)) {
209 address
= (unsigned long) page_address(pte_page(*ptep
));
210 /* MS: I need add offset in page */
211 address
+= ((unsigned long)frame
->tramp
) & ~PAGE_MASK
;
212 /* MS address is virtual */
213 address
= __virt_to_phys(address
);
214 invalidate_icache_range(address
, address
+ 8);
215 flush_dcache_range(address
, address
+ 8);
220 flush_icache_range(address
, address
+ 8);
221 flush_dcache_range(address
, address
+ 8);
226 /* Set up registers for signal handler */
227 regs
->r1
= (unsigned long) frame
;
229 /* Signal handler args: */
230 regs
->r5
= sig
; /* arg 0: signum */
231 regs
->r6
= (unsigned long) &frame
->info
; /* arg 1: siginfo */
232 regs
->r7
= (unsigned long) &frame
->uc
; /* arg2: ucontext */
233 /* Offset to handle microblaze rtid r14, 0 */
234 regs
->pc
= (unsigned long)ksig
->ka
.sa
.sa_handler
;
237 pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n",
238 current
->comm
, current
->pid
, frame
, regs
->pc
);
244 /* Handle restarting system calls */
246 handle_restart(struct pt_regs
*regs
, struct k_sigaction
*ka
, int has_handler
)
249 case -ERESTART_RESTARTBLOCK
:
250 case -ERESTARTNOHAND
:
256 if (has_handler
&& !(ka
->sa
.sa_flags
& SA_RESTART
)) {
261 case -ERESTARTNOINTR
:
263 /* offset of 4 bytes to re-execute trap (brki) instruction */
270 * OK, we're invoking a handler
274 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
276 sigset_t
*oldset
= sigmask_to_save();
279 /* Set up the stack frame */
280 ret
= setup_rt_frame(ksig
, oldset
, regs
);
282 signal_setup_done(ret
, ksig
, test_thread_flag(TIF_SINGLESTEP
));
286 * Note that 'init' is a special process: it doesn't get signals it doesn't
287 * want to handle. Thus you cannot kill init even with a SIGKILL even by
290 * Note that we go through the signals twice: once to check the signals that
291 * the kernel can handle, and then we build all the user-level signal handling
292 * stack-frames in one go after that.
294 static void do_signal(struct pt_regs
*regs
, int in_syscall
)
299 pr_info("do signal: %p %d\n", regs
, in_syscall
);
300 pr_info("do signal2: %lx %lx %ld [%lx]\n", regs
->pc
, regs
->r1
,
301 regs
->r12
, current_thread_info()->flags
);
304 if (get_signal(&ksig
)) {
305 /* Whee! Actually deliver the signal. */
307 handle_restart(regs
, &ksig
.ka
, 1);
308 handle_signal(&ksig
, regs
);
313 handle_restart(regs
, NULL
, 0);
316 * If there's no signal to deliver, we just put the saved sigmask
319 restore_saved_sigmask();
322 asmlinkage
void do_notify_resume(struct pt_regs
*regs
, int in_syscall
)
324 if (test_thread_flag(TIF_SIGPENDING
))
325 do_signal(regs
, in_syscall
);
327 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME
))
328 tracehook_notify_resume(regs
);