Loading...
1/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * Based on linux/arch/sh/kernel/signal.c
5 * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/unistd.h>
18#include <linux/freezer.h>
19#include <linux/tracehook.h>
20
21#include <asm/uaccess.h>
22#include <asm/ucontext.h>
23#include <asm/syscalls.h>
24
25asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
26 struct pt_regs *regs)
27{
28 return do_sigaltstack(uss, uoss, regs->sp);
29}
30
31struct rt_sigframe
32{
33 struct siginfo info;
34 struct ucontext uc;
35 unsigned long retcode;
36};
37
38static int
39restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
40{
41 int err = 0;
42
43#define COPY(x) err |= __get_user(regs->x, &sc->x)
44 COPY(sr);
45 COPY(pc);
46 COPY(lr);
47 COPY(sp);
48 COPY(r12);
49 COPY(r11);
50 COPY(r10);
51 COPY(r9);
52 COPY(r8);
53 COPY(r7);
54 COPY(r6);
55 COPY(r5);
56 COPY(r4);
57 COPY(r3);
58 COPY(r2);
59 COPY(r1);
60 COPY(r0);
61#undef COPY
62
63 /*
64 * Don't allow anyone to pretend they're running in supervisor
65 * mode or something...
66 */
67 err |= !valid_user_regs(regs);
68
69 return err;
70}
71
72
73asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
74{
75 struct rt_sigframe __user *frame;
76 sigset_t set;
77
78 /* Always make any pending restarted system calls return -EINTR */
79 current_thread_info()->restart_block.fn = do_no_restart_syscall;
80
81 frame = (struct rt_sigframe __user *)regs->sp;
82 pr_debug("SIG return: frame = %p\n", frame);
83
84 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
85 goto badframe;
86
87 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
88 goto badframe;
89
90 set_current_blocked(&set);
91
92 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
93 goto badframe;
94
95 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
96 goto badframe;
97
98 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
99 regs->pc, regs->lr, regs->sp);
100
101 return regs->r12;
102
103badframe:
104 force_sig(SIGSEGV, current);
105 return 0;
106}
107
108static int
109setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
110{
111 int err = 0;
112
113#define COPY(x) err |= __put_user(regs->x, &sc->x)
114 COPY(sr);
115 COPY(pc);
116 COPY(lr);
117 COPY(sp);
118 COPY(r12);
119 COPY(r11);
120 COPY(r10);
121 COPY(r9);
122 COPY(r8);
123 COPY(r7);
124 COPY(r6);
125 COPY(r5);
126 COPY(r4);
127 COPY(r3);
128 COPY(r2);
129 COPY(r1);
130 COPY(r0);
131#undef COPY
132
133 return err;
134}
135
136static inline void __user *
137get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
138{
139 unsigned long sp = regs->sp;
140
141 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
142 sp = current->sas_ss_sp + current->sas_ss_size;
143
144 return (void __user *)((sp - framesize) & ~3);
145}
146
147static int
148setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
149 sigset_t *set, struct pt_regs *regs)
150{
151 struct rt_sigframe __user *frame;
152 int err = 0;
153
154 frame = get_sigframe(ka, regs, sizeof(*frame));
155 err = -EFAULT;
156 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
157 goto out;
158
159 /*
160 * Set up the return code:
161 *
162 * mov r8, __NR_rt_sigreturn
163 * scall
164 *
165 * Note: This will blow up since we're using a non-executable
166 * stack. Better use SA_RESTORER.
167 */
168#if __NR_rt_sigreturn > 127
169# error __NR_rt_sigreturn must be < 127 to fit in a short mov
170#endif
171 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
172 &frame->retcode);
173
174 err |= copy_siginfo_to_user(&frame->info, info);
175
176 /* Set up the ucontext */
177 err |= __put_user(0, &frame->uc.uc_flags);
178 err |= __put_user(NULL, &frame->uc.uc_link);
179 err |= __put_user((void __user *)current->sas_ss_sp,
180 &frame->uc.uc_stack.ss_sp);
181 err |= __put_user(sas_ss_flags(regs->sp),
182 &frame->uc.uc_stack.ss_flags);
183 err |= __put_user(current->sas_ss_size,
184 &frame->uc.uc_stack.ss_size);
185 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
186 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
187
188 if (err)
189 goto out;
190
191 regs->r12 = sig;
192 regs->r11 = (unsigned long) &frame->info;
193 regs->r10 = (unsigned long) &frame->uc;
194 regs->sp = (unsigned long) frame;
195 if (ka->sa.sa_flags & SA_RESTORER)
196 regs->lr = (unsigned long)ka->sa.sa_restorer;
197 else {
198 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
199 current->comm, current->pid);
200 regs->lr = (unsigned long) &frame->retcode;
201 }
202
203 pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
204 current->comm, current->pid, sig, regs->sp,
205 regs->pc, ka->sa.sa_handler, regs->lr);
206
207 regs->pc = (unsigned long) ka->sa.sa_handler;
208
209out:
210 return err;
211}
212
213static inline void setup_syscall_restart(struct pt_regs *regs)
214{
215 if (regs->r12 == -ERESTART_RESTARTBLOCK)
216 regs->r8 = __NR_restart_syscall;
217 else
218 regs->r12 = regs->r12_orig;
219 regs->pc -= 2;
220}
221
222static inline void
223handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
224 struct pt_regs *regs, int syscall)
225{
226 int ret;
227
228 /*
229 * Set up the stack frame
230 */
231 ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
232
233 /*
234 * Check that the resulting registers are sane
235 */
236 ret |= !valid_user_regs(regs);
237
238 /*
239 * Block the signal if we were successful.
240 */
241 if (ret != 0)
242 force_sigsegv(sig, current);
243 else
244 signal_delivered(sig, info, ka, regs, 0);
245}
246
247/*
248 * Note that 'init' is a special process: it doesn't get signals it
249 * doesn't want to handle. Thus you cannot kill init even with a
250 * SIGKILL even by mistake.
251 */
252static void do_signal(struct pt_regs *regs, int syscall)
253{
254 siginfo_t info;
255 int signr;
256 struct k_sigaction ka;
257
258 /*
259 * We want the common case to go fast, which is why we may in
260 * certain cases get here from kernel mode. Just return
261 * without doing anything if so.
262 */
263 if (!user_mode(regs))
264 return;
265
266 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
267 if (syscall) {
268 switch (regs->r12) {
269 case -ERESTART_RESTARTBLOCK:
270 case -ERESTARTNOHAND:
271 if (signr > 0) {
272 regs->r12 = -EINTR;
273 break;
274 }
275 /* fall through */
276 case -ERESTARTSYS:
277 if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
278 regs->r12 = -EINTR;
279 break;
280 }
281 /* fall through */
282 case -ERESTARTNOINTR:
283 setup_syscall_restart(regs);
284 }
285 }
286
287 if (signr == 0) {
288 /* No signal to deliver -- put the saved sigmask back */
289 restore_saved_sigmask();
290 return;
291 }
292
293 handle_signal(signr, &ka, &info, regs, syscall);
294}
295
296asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
297{
298 int syscall = 0;
299
300 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
301 syscall = 1;
302
303 if (ti->flags & _TIF_SIGPENDING)
304 do_signal(regs, syscall);
305
306 if (ti->flags & _TIF_NOTIFY_RESUME) {
307 clear_thread_flag(TIF_NOTIFY_RESUME);
308 tracehook_notify_resume(regs);
309 }
310}
1/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * Based on linux/arch/sh/kernel/signal.c
5 * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/sched.h>
14#include <linux/mm.h>
15#include <linux/errno.h>
16#include <linux/ptrace.h>
17#include <linux/unistd.h>
18#include <linux/tracehook.h>
19
20#include <asm/uaccess.h>
21#include <asm/ucontext.h>
22#include <asm/syscalls.h>
23
24struct rt_sigframe
25{
26 struct siginfo info;
27 struct ucontext uc;
28 unsigned long retcode;
29};
30
31static int
32restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
33{
34 int err = 0;
35
36#define COPY(x) err |= __get_user(regs->x, &sc->x)
37 COPY(sr);
38 COPY(pc);
39 COPY(lr);
40 COPY(sp);
41 COPY(r12);
42 COPY(r11);
43 COPY(r10);
44 COPY(r9);
45 COPY(r8);
46 COPY(r7);
47 COPY(r6);
48 COPY(r5);
49 COPY(r4);
50 COPY(r3);
51 COPY(r2);
52 COPY(r1);
53 COPY(r0);
54#undef COPY
55
56 /*
57 * Don't allow anyone to pretend they're running in supervisor
58 * mode or something...
59 */
60 err |= !valid_user_regs(regs);
61
62 return err;
63}
64
65
66asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
67{
68 struct rt_sigframe __user *frame;
69 sigset_t set;
70
71 /* Always make any pending restarted system calls return -EINTR */
72 current->restart_block.fn = do_no_restart_syscall;
73
74 frame = (struct rt_sigframe __user *)regs->sp;
75 pr_debug("SIG return: frame = %p\n", frame);
76
77 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
78 goto badframe;
79
80 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
81 goto badframe;
82
83 set_current_blocked(&set);
84
85 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
86 goto badframe;
87
88 if (restore_altstack(&frame->uc.uc_stack))
89 goto badframe;
90
91 pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
92 regs->pc, regs->lr, regs->sp);
93
94 return regs->r12;
95
96badframe:
97 force_sig(SIGSEGV, current);
98 return 0;
99}
100
101static int
102setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
103{
104 int err = 0;
105
106#define COPY(x) err |= __put_user(regs->x, &sc->x)
107 COPY(sr);
108 COPY(pc);
109 COPY(lr);
110 COPY(sp);
111 COPY(r12);
112 COPY(r11);
113 COPY(r10);
114 COPY(r9);
115 COPY(r8);
116 COPY(r7);
117 COPY(r6);
118 COPY(r5);
119 COPY(r4);
120 COPY(r3);
121 COPY(r2);
122 COPY(r1);
123 COPY(r0);
124#undef COPY
125
126 return err;
127}
128
129static inline void __user *
130get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
131{
132 unsigned long sp = sigsp(regs->sp, ksig);
133
134 return (void __user *)((sp - framesize) & ~3);
135}
136
137static int
138setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
139{
140 struct rt_sigframe __user *frame;
141 int err = 0;
142
143 frame = get_sigframe(ksig, regs, sizeof(*frame));
144 err = -EFAULT;
145 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
146 goto out;
147
148 /*
149 * Set up the return code:
150 *
151 * mov r8, __NR_rt_sigreturn
152 * scall
153 *
154 * Note: This will blow up since we're using a non-executable
155 * stack. Better use SA_RESTORER.
156 */
157#if __NR_rt_sigreturn > 127
158# error __NR_rt_sigreturn must be < 127 to fit in a short mov
159#endif
160 err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
161 &frame->retcode);
162
163 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
164
165 /* Set up the ucontext */
166 err |= __put_user(0, &frame->uc.uc_flags);
167 err |= __put_user(NULL, &frame->uc.uc_link);
168 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
169 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
170 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
171
172 if (err)
173 goto out;
174
175 regs->r12 = ksig->sig;
176 regs->r11 = (unsigned long) &frame->info;
177 regs->r10 = (unsigned long) &frame->uc;
178 regs->sp = (unsigned long) frame;
179 if (ksig->ka.sa.sa_flags & SA_RESTORER)
180 regs->lr = (unsigned long)ksig->ka.sa.sa_restorer;
181 else {
182 printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
183 current->comm, current->pid);
184 regs->lr = (unsigned long) &frame->retcode;
185 }
186
187 pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
188 current->comm, current->pid, ksig->sig, regs->sp,
189 regs->pc, ksig->ka.sa.sa_handler, regs->lr);
190
191 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
192
193out:
194 return err;
195}
196
197static inline void setup_syscall_restart(struct pt_regs *regs)
198{
199 if (regs->r12 == -ERESTART_RESTARTBLOCK)
200 regs->r8 = __NR_restart_syscall;
201 else
202 regs->r12 = regs->r12_orig;
203 regs->pc -= 2;
204}
205
206static inline void
207handle_signal(struct ksignal *ksig, struct pt_regs *regs, int syscall)
208{
209 int ret;
210
211 /*
212 * Set up the stack frame
213 */
214 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
215
216 /*
217 * Check that the resulting registers are sane
218 */
219 ret |= !valid_user_regs(regs);
220
221 /*
222 * Block the signal if we were successful.
223 */
224 signal_setup_done(ret, ksig, 0);
225}
226
227/*
228 * Note that 'init' is a special process: it doesn't get signals it
229 * doesn't want to handle. Thus you cannot kill init even with a
230 * SIGKILL even by mistake.
231 */
232static void do_signal(struct pt_regs *regs, int syscall)
233{
234 struct ksignal ksig;
235
236 /*
237 * We want the common case to go fast, which is why we may in
238 * certain cases get here from kernel mode. Just return
239 * without doing anything if so.
240 */
241 if (!user_mode(regs))
242 return;
243
244 get_signal(&ksig);
245 if (syscall) {
246 switch (regs->r12) {
247 case -ERESTART_RESTARTBLOCK:
248 case -ERESTARTNOHAND:
249 if (ksig.sig > 0) {
250 regs->r12 = -EINTR;
251 break;
252 }
253 /* fall through */
254 case -ERESTARTSYS:
255 if (ksig.sig > 0 && !(ksig.ka.sa.sa_flags & SA_RESTART)) {
256 regs->r12 = -EINTR;
257 break;
258 }
259 /* fall through */
260 case -ERESTARTNOINTR:
261 setup_syscall_restart(regs);
262 }
263 }
264
265 if (!ksig.sig) {
266 /* No signal to deliver -- put the saved sigmask back */
267 restore_saved_sigmask();
268 return;
269 }
270
271 handle_signal(&ksig, regs, syscall);
272}
273
274asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
275{
276 int syscall = 0;
277
278 if ((sysreg_read(SR) & MODE_MASK) == MODE_SUPERVISOR)
279 syscall = 1;
280
281 if (ti->flags & _TIF_SIGPENDING)
282 do_signal(regs, syscall);
283
284 if (ti->flags & _TIF_NOTIFY_RESUME) {
285 clear_thread_flag(TIF_NOTIFY_RESUME);
286 tracehook_notify_resume(regs);
287 }
288}