Commit | Line | Data |
---|---|---|
2148daa9 MS |
1 | /* |
2 | * Signal handling | |
3 | * | |
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 | |
11 | * | |
12 | * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson | |
13 | * | |
14 | * This file was was derived from the sh version, arch/sh/kernel/signal.c | |
15 | * | |
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. | |
19 | */ | |
20 | ||
21 | #include <linux/sched.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/smp.h> | |
2148daa9 MS |
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> | |
969a9616 | 34 | #include <linux/tracehook.h> |
2148daa9 MS |
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> | |
2148daa9 MS |
40 | #include <linux/syscalls.h> |
41 | #include <asm/cacheflush.h> | |
42 | #include <asm/syscalls.h> | |
43 | ||
2148daa9 MS |
44 | /* |
45 | * Do a signal return; undo the signal stack. | |
46 | */ | |
2148daa9 MS |
47 | struct sigframe { |
48 | struct sigcontext sc; | |
49 | unsigned long extramask[_NSIG_WORDS-1]; | |
50 | unsigned long tramp[2]; /* signal trampoline */ | |
51 | }; | |
52 | ||
53 | struct rt_sigframe { | |
54 | struct siginfo info; | |
55 | struct ucontext uc; | |
56 | unsigned long tramp[2]; /* signal trampoline */ | |
57 | }; | |
58 | ||
353b431b AB |
59 | static int restore_sigcontext(struct pt_regs *regs, |
60 | struct sigcontext __user *sc, int *rval_p) | |
2148daa9 MS |
61 | { |
62 | unsigned int err = 0; | |
63 | ||
64 | #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); } | |
65 | COPY(r0); | |
66 | COPY(r1); | |
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); | |
74 | COPY(r30); COPY(r31); | |
75 | COPY(pc); COPY(ear); COPY(esr); COPY(fsr); | |
76 | #undef COPY | |
77 | ||
78 | *rval_p = regs->r3; | |
79 | ||
80 | return err; | |
81 | } | |
82 | ||
3183e068 | 83 | asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) |
2148daa9 | 84 | { |
353b431b | 85 | struct rt_sigframe __user *frame = |
6e83557c | 86 | (struct rt_sigframe __user *)(regs->r1); |
2921e2bd | 87 | |
2148daa9 | 88 | sigset_t set; |
2148daa9 MS |
89 | int rval; |
90 | ||
58e4257b | 91 | /* Always make any pending restarted system calls return -EINTR */ |
f56141e3 | 92 | current->restart_block.fn = do_no_restart_syscall; |
58e4257b | 93 | |
96d4f267 | 94 | if (!access_ok(frame, sizeof(*frame))) |
2148daa9 MS |
95 | goto badframe; |
96 | ||
97 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
98 | goto badframe; | |
99 | ||
60c59751 | 100 | set_current_blocked(&set); |
2148daa9 MS |
101 | |
102 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval)) | |
103 | goto badframe; | |
104 | ||
4a9d32d3 | 105 | if (restore_altstack(&frame->uc.uc_stack)) |
353b431b | 106 | goto badframe; |
2148daa9 MS |
107 | |
108 | return rval; | |
109 | ||
110 | badframe: | |
111 | force_sig(SIGSEGV, current); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | /* | |
116 | * Set up a signal frame. | |
117 | */ | |
118 | ||
119 | static int | |
353b431b | 120 | setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, |
2148daa9 MS |
121 | unsigned long mask) |
122 | { | |
123 | int err = 0; | |
124 | ||
125 | #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); } | |
126 | COPY(r0); | |
127 | COPY(r1); | |
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); | |
137 | #undef COPY | |
138 | ||
139 | err |= __put_user(mask, &sc->oldmask); | |
140 | ||
141 | return err; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Determine which stack to use.. | |
146 | */ | |
353b431b | 147 | static inline void __user * |
c001cd21 | 148 | get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size) |
2148daa9 MS |
149 | { |
150 | /* Default to using normal stack */ | |
c001cd21 | 151 | unsigned long sp = sigsp(regs->r1, ksig); |
2148daa9 | 152 | |
353b431b | 153 | return (void __user *)((sp - frame_size) & -8UL); |
2148daa9 MS |
154 | } |
155 | ||
9c53c7ec RW |
156 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
157 | struct pt_regs *regs) | |
2148daa9 | 158 | { |
353b431b | 159 | struct rt_sigframe __user *frame; |
9c53c7ec | 160 | int err = 0, sig = ksig->sig; |
2ee2ff87 MS |
161 | unsigned long address = 0; |
162 | #ifdef CONFIG_MMU | |
163 | pmd_t *pmdp; | |
164 | pte_t *ptep; | |
165 | #endif | |
2148daa9 | 166 | |
c001cd21 | 167 | frame = get_sigframe(ksig, regs, sizeof(*frame)); |
2148daa9 | 168 | |
96d4f267 | 169 | if (!access_ok(frame, sizeof(*frame))) |
9c53c7ec | 170 | return -EFAULT; |
2148daa9 | 171 | |
9c53c7ec RW |
172 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) |
173 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); | |
2148daa9 MS |
174 | |
175 | /* Create the ucontext. */ | |
176 | err |= __put_user(0, &frame->uc.uc_flags); | |
2cfedb97 | 177 | err |= __put_user(NULL, &frame->uc.uc_link); |
4a9d32d3 | 178 | err |= __save_altstack(&frame->uc.uc_stack, regs->r1); |
2148daa9 MS |
179 | err |= setup_sigcontext(&frame->uc.uc_mcontext, |
180 | regs, set->sig[0]); | |
181 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | |
182 | ||
183 | /* Set up to return from userspace. If provided, use a stub | |
184 | already in userspace. */ | |
185 | /* minus 8 is offset to cater for "rtsd r15,8" */ | |
3183e068 AB |
186 | /* addi r12, r0, __NR_sigreturn */ |
187 | err |= __put_user(0x31800000 | __NR_rt_sigreturn , | |
188 | frame->tramp + 0); | |
189 | /* brki r14, 0x8 */ | |
190 | err |= __put_user(0xb9cc0008, frame->tramp + 1); | |
191 | ||
192 | /* Return from sighandler will jump to the tramp. | |
193 | Negative 8 offset because return is rtsd r15, 8 */ | |
194 | regs->r15 = ((unsigned long)frame->tramp)-8; | |
195 | ||
2ee2ff87 MS |
196 | address = ((unsigned long)frame->tramp); |
197 | #ifdef CONFIG_MMU | |
198 | pmdp = pmd_offset(pud_offset( | |
199 | pgd_offset(current->mm, address), | |
200 | address), address); | |
201 | ||
202 | preempt_disable(); | |
203 | ptep = pte_offset_map(pmdp, address); | |
204 | if (pte_present(*ptep)) { | |
205 | address = (unsigned long) page_address(pte_page(*ptep)); | |
206 | /* MS: I need add offset in page */ | |
207 | address += ((unsigned long)frame->tramp) & ~PAGE_MASK; | |
208 | /* MS address is virtual */ | |
a66a6265 | 209 | address = __virt_to_phys(address); |
2ee2ff87 MS |
210 | invalidate_icache_range(address, address + 8); |
211 | flush_dcache_range(address, address + 8); | |
212 | } | |
213 | pte_unmap(ptep); | |
214 | preempt_enable(); | |
215 | #else | |
216 | flush_icache_range(address, address + 8); | |
217 | flush_dcache_range(address, address + 8); | |
218 | #endif | |
2148daa9 | 219 | if (err) |
9c53c7ec | 220 | return -EFAULT; |
2148daa9 MS |
221 | |
222 | /* Set up registers for signal handler */ | |
6e83557c | 223 | regs->r1 = (unsigned long) frame; |
2921e2bd | 224 | |
2148daa9 | 225 | /* Signal handler args: */ |
6140de5a | 226 | regs->r5 = sig; /* arg 0: signum */ |
2148daa9 MS |
227 | regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */ |
228 | regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */ | |
229 | /* Offset to handle microblaze rtid r14, 0 */ | |
9c53c7ec | 230 | regs->pc = (unsigned long)ksig->ka.sa.sa_handler; |
2148daa9 | 231 | |
2148daa9 | 232 | #ifdef DEBUG_SIG |
6bd55f0b | 233 | pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n", |
2148daa9 MS |
234 | current->comm, current->pid, frame, regs->pc); |
235 | #endif | |
236 | ||
bcb8c8d0 | 237 | return 0; |
2148daa9 MS |
238 | } |
239 | ||
240 | /* Handle restarting system calls */ | |
241 | static inline void | |
242 | handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler) | |
243 | { | |
244 | switch (regs->r3) { | |
245 | case -ERESTART_RESTARTBLOCK: | |
246 | case -ERESTARTNOHAND: | |
247 | if (!has_handler) | |
248 | goto do_restart; | |
249 | regs->r3 = -EINTR; | |
250 | break; | |
251 | case -ERESTARTSYS: | |
252 | if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) { | |
253 | regs->r3 = -EINTR; | |
254 | break; | |
255 | } | |
256 | /* fallthrough */ | |
257 | case -ERESTARTNOINTR: | |
258 | do_restart: | |
259 | /* offset of 4 bytes to re-execute trap (brki) instruction */ | |
260 | regs->pc -= 4; | |
261 | break; | |
262 | } | |
263 | } | |
264 | ||
265 | /* | |
266 | * OK, we're invoking a handler | |
267 | */ | |
268 | ||
a610d6e6 | 269 | static void |
9c53c7ec | 270 | handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
2148daa9 | 271 | { |
b7f9a11a | 272 | sigset_t *oldset = sigmask_to_save(); |
bcb8c8d0 MF |
273 | int ret; |
274 | ||
2148daa9 | 275 | /* Set up the stack frame */ |
9c53c7ec | 276 | ret = setup_rt_frame(ksig, oldset, regs); |
bcb8c8d0 | 277 | |
9c53c7ec | 278 | signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); |
2148daa9 MS |
279 | } |
280 | ||
281 | /* | |
282 | * Note that 'init' is a special process: it doesn't get signals it doesn't | |
283 | * want to handle. Thus you cannot kill init even with a SIGKILL even by | |
284 | * mistake. | |
285 | * | |
286 | * Note that we go through the signals twice: once to check the signals that | |
287 | * the kernel can handle, and then we build all the user-level signal handling | |
288 | * stack-frames in one go after that. | |
289 | */ | |
83140191 | 290 | static void do_signal(struct pt_regs *regs, int in_syscall) |
2148daa9 | 291 | { |
9c53c7ec RW |
292 | struct ksignal ksig; |
293 | ||
2148daa9 | 294 | #ifdef DEBUG_SIG |
6bd55f0b MS |
295 | pr_info("do signal: %p %d\n", regs, in_syscall); |
296 | pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1, | |
2148daa9 MS |
297 | regs->r12, current_thread_info()->flags); |
298 | #endif | |
2148daa9 | 299 | |
9c53c7ec | 300 | if (get_signal(&ksig)) { |
2148daa9 MS |
301 | /* Whee! Actually deliver the signal. */ |
302 | if (in_syscall) | |
9c53c7ec RW |
303 | handle_restart(regs, &ksig.ka, 1); |
304 | handle_signal(&ksig, regs); | |
83140191 | 305 | return; |
2148daa9 MS |
306 | } |
307 | ||
308 | if (in_syscall) | |
309 | handle_restart(regs, NULL, 0); | |
310 | ||
3183e068 AB |
311 | /* |
312 | * If there's no signal to deliver, we just put the saved sigmask | |
313 | * back. | |
314 | */ | |
51a7b448 | 315 | restore_saved_sigmask(); |
2148daa9 | 316 | } |
969a9616 | 317 | |
4378bb69 | 318 | asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall) |
969a9616 | 319 | { |
969a9616 | 320 | if (test_thread_flag(TIF_SIGPENDING)) |
83140191 | 321 | do_signal(regs, in_syscall); |
969a9616 | 322 | |
a42c6ded | 323 | if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME)) |
969a9616 | 324 | tracehook_notify_resume(regs); |
969a9616 | 325 | } |