Linux 3.8-rc7
[cris-mirror.git] / arch / unicore32 / kernel / signal.c
blobb8b2ffd774d637bdf55a35bc2a5116d8872904c8
1 /*
2 * linux/arch/unicore32/kernel/signal.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Copyright (C) 2001-2010 GUAN Xue-tao
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.
12 #include <linux/errno.h>
13 #include <linux/signal.h>
14 #include <linux/personality.h>
15 #include <linux/uaccess.h>
16 #include <linux/tracehook.h>
17 #include <linux/elf.h>
18 #include <linux/unistd.h>
20 #include <asm/cacheflush.h>
21 #include <asm/ucontext.h>
24 * For UniCore syscalls, we encode the syscall number into the instruction.
26 #define SWI_SYS_SIGRETURN (0xff000000) /* error number for new abi */
27 #define SWI_SYS_RT_SIGRETURN (0xff000000 | (__NR_rt_sigreturn))
28 #define SWI_SYS_RESTART (0xff000000 | (__NR_restart_syscall))
30 #define KERN_SIGRETURN_CODE (KUSER_VECPAGE_BASE + 0x00000500)
31 #define KERN_RESTART_CODE (KERN_SIGRETURN_CODE + sizeof(sigreturn_codes))
33 const unsigned long sigreturn_codes[3] = {
34 SWI_SYS_SIGRETURN, SWI_SYS_RT_SIGRETURN,
37 const unsigned long syscall_restart_code[2] = {
38 SWI_SYS_RESTART, /* swi __NR_restart_syscall */
39 0x69efc004, /* ldr pc, [sp], #4 */
43 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
45 struct sigframe {
46 struct ucontext uc;
47 unsigned long retcode[2];
50 struct rt_sigframe {
51 struct siginfo info;
52 struct sigframe sig;
55 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
57 sigset_t set;
58 int err;
60 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
61 if (err == 0)
62 set_current_blocked(&set);
64 err |= __get_user(regs->UCreg_00, &sf->uc.uc_mcontext.regs.UCreg_00);
65 err |= __get_user(regs->UCreg_01, &sf->uc.uc_mcontext.regs.UCreg_01);
66 err |= __get_user(regs->UCreg_02, &sf->uc.uc_mcontext.regs.UCreg_02);
67 err |= __get_user(regs->UCreg_03, &sf->uc.uc_mcontext.regs.UCreg_03);
68 err |= __get_user(regs->UCreg_04, &sf->uc.uc_mcontext.regs.UCreg_04);
69 err |= __get_user(regs->UCreg_05, &sf->uc.uc_mcontext.regs.UCreg_05);
70 err |= __get_user(regs->UCreg_06, &sf->uc.uc_mcontext.regs.UCreg_06);
71 err |= __get_user(regs->UCreg_07, &sf->uc.uc_mcontext.regs.UCreg_07);
72 err |= __get_user(regs->UCreg_08, &sf->uc.uc_mcontext.regs.UCreg_08);
73 err |= __get_user(regs->UCreg_09, &sf->uc.uc_mcontext.regs.UCreg_09);
74 err |= __get_user(regs->UCreg_10, &sf->uc.uc_mcontext.regs.UCreg_10);
75 err |= __get_user(regs->UCreg_11, &sf->uc.uc_mcontext.regs.UCreg_11);
76 err |= __get_user(regs->UCreg_12, &sf->uc.uc_mcontext.regs.UCreg_12);
77 err |= __get_user(regs->UCreg_13, &sf->uc.uc_mcontext.regs.UCreg_13);
78 err |= __get_user(regs->UCreg_14, &sf->uc.uc_mcontext.regs.UCreg_14);
79 err |= __get_user(regs->UCreg_15, &sf->uc.uc_mcontext.regs.UCreg_15);
80 err |= __get_user(regs->UCreg_16, &sf->uc.uc_mcontext.regs.UCreg_16);
81 err |= __get_user(regs->UCreg_17, &sf->uc.uc_mcontext.regs.UCreg_17);
82 err |= __get_user(regs->UCreg_18, &sf->uc.uc_mcontext.regs.UCreg_18);
83 err |= __get_user(regs->UCreg_19, &sf->uc.uc_mcontext.regs.UCreg_19);
84 err |= __get_user(regs->UCreg_20, &sf->uc.uc_mcontext.regs.UCreg_20);
85 err |= __get_user(regs->UCreg_21, &sf->uc.uc_mcontext.regs.UCreg_21);
86 err |= __get_user(regs->UCreg_22, &sf->uc.uc_mcontext.regs.UCreg_22);
87 err |= __get_user(regs->UCreg_23, &sf->uc.uc_mcontext.regs.UCreg_23);
88 err |= __get_user(regs->UCreg_24, &sf->uc.uc_mcontext.regs.UCreg_24);
89 err |= __get_user(regs->UCreg_25, &sf->uc.uc_mcontext.regs.UCreg_25);
90 err |= __get_user(regs->UCreg_26, &sf->uc.uc_mcontext.regs.UCreg_26);
91 err |= __get_user(regs->UCreg_fp, &sf->uc.uc_mcontext.regs.UCreg_fp);
92 err |= __get_user(regs->UCreg_ip, &sf->uc.uc_mcontext.regs.UCreg_ip);
93 err |= __get_user(regs->UCreg_sp, &sf->uc.uc_mcontext.regs.UCreg_sp);
94 err |= __get_user(regs->UCreg_lr, &sf->uc.uc_mcontext.regs.UCreg_lr);
95 err |= __get_user(regs->UCreg_pc, &sf->uc.uc_mcontext.regs.UCreg_pc);
96 err |= __get_user(regs->UCreg_asr, &sf->uc.uc_mcontext.regs.UCreg_asr);
98 err |= !valid_user_regs(regs);
100 return err;
103 asmlinkage int __sys_rt_sigreturn(struct pt_regs *regs)
105 struct rt_sigframe __user *frame;
107 /* Always make any pending restarted system calls return -EINTR */
108 current_thread_info()->restart_block.fn = do_no_restart_syscall;
111 * Since we stacked the signal on a 64-bit boundary,
112 * then 'sp' should be word aligned here. If it's
113 * not, then the user is trying to mess with us.
115 if (regs->UCreg_sp & 7)
116 goto badframe;
118 frame = (struct rt_sigframe __user *)regs->UCreg_sp;
120 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
121 goto badframe;
123 if (restore_sigframe(regs, &frame->sig))
124 goto badframe;
126 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->UCreg_sp)
127 == -EFAULT)
128 goto badframe;
130 return regs->UCreg_00;
132 badframe:
133 force_sig(SIGSEGV, current);
134 return 0;
137 static int setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs,
138 sigset_t *set)
140 int err = 0;
142 err |= __put_user(regs->UCreg_00, &sf->uc.uc_mcontext.regs.UCreg_00);
143 err |= __put_user(regs->UCreg_01, &sf->uc.uc_mcontext.regs.UCreg_01);
144 err |= __put_user(regs->UCreg_02, &sf->uc.uc_mcontext.regs.UCreg_02);
145 err |= __put_user(regs->UCreg_03, &sf->uc.uc_mcontext.regs.UCreg_03);
146 err |= __put_user(regs->UCreg_04, &sf->uc.uc_mcontext.regs.UCreg_04);
147 err |= __put_user(regs->UCreg_05, &sf->uc.uc_mcontext.regs.UCreg_05);
148 err |= __put_user(regs->UCreg_06, &sf->uc.uc_mcontext.regs.UCreg_06);
149 err |= __put_user(regs->UCreg_07, &sf->uc.uc_mcontext.regs.UCreg_07);
150 err |= __put_user(regs->UCreg_08, &sf->uc.uc_mcontext.regs.UCreg_08);
151 err |= __put_user(regs->UCreg_09, &sf->uc.uc_mcontext.regs.UCreg_09);
152 err |= __put_user(regs->UCreg_10, &sf->uc.uc_mcontext.regs.UCreg_10);
153 err |= __put_user(regs->UCreg_11, &sf->uc.uc_mcontext.regs.UCreg_11);
154 err |= __put_user(regs->UCreg_12, &sf->uc.uc_mcontext.regs.UCreg_12);
155 err |= __put_user(regs->UCreg_13, &sf->uc.uc_mcontext.regs.UCreg_13);
156 err |= __put_user(regs->UCreg_14, &sf->uc.uc_mcontext.regs.UCreg_14);
157 err |= __put_user(regs->UCreg_15, &sf->uc.uc_mcontext.regs.UCreg_15);
158 err |= __put_user(regs->UCreg_16, &sf->uc.uc_mcontext.regs.UCreg_16);
159 err |= __put_user(regs->UCreg_17, &sf->uc.uc_mcontext.regs.UCreg_17);
160 err |= __put_user(regs->UCreg_18, &sf->uc.uc_mcontext.regs.UCreg_18);
161 err |= __put_user(regs->UCreg_19, &sf->uc.uc_mcontext.regs.UCreg_19);
162 err |= __put_user(regs->UCreg_20, &sf->uc.uc_mcontext.regs.UCreg_20);
163 err |= __put_user(regs->UCreg_21, &sf->uc.uc_mcontext.regs.UCreg_21);
164 err |= __put_user(regs->UCreg_22, &sf->uc.uc_mcontext.regs.UCreg_22);
165 err |= __put_user(regs->UCreg_23, &sf->uc.uc_mcontext.regs.UCreg_23);
166 err |= __put_user(regs->UCreg_24, &sf->uc.uc_mcontext.regs.UCreg_24);
167 err |= __put_user(regs->UCreg_25, &sf->uc.uc_mcontext.regs.UCreg_25);
168 err |= __put_user(regs->UCreg_26, &sf->uc.uc_mcontext.regs.UCreg_26);
169 err |= __put_user(regs->UCreg_fp, &sf->uc.uc_mcontext.regs.UCreg_fp);
170 err |= __put_user(regs->UCreg_ip, &sf->uc.uc_mcontext.regs.UCreg_ip);
171 err |= __put_user(regs->UCreg_sp, &sf->uc.uc_mcontext.regs.UCreg_sp);
172 err |= __put_user(regs->UCreg_lr, &sf->uc.uc_mcontext.regs.UCreg_lr);
173 err |= __put_user(regs->UCreg_pc, &sf->uc.uc_mcontext.regs.UCreg_pc);
174 err |= __put_user(regs->UCreg_asr, &sf->uc.uc_mcontext.regs.UCreg_asr);
176 err |= __put_user(current->thread.trap_no,
177 &sf->uc.uc_mcontext.trap_no);
178 err |= __put_user(current->thread.error_code,
179 &sf->uc.uc_mcontext.error_code);
180 err |= __put_user(current->thread.address,
181 &sf->uc.uc_mcontext.fault_address);
182 err |= __put_user(set->sig[0], &sf->uc.uc_mcontext.oldmask);
184 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
186 return err;
189 static inline void __user *get_sigframe(struct k_sigaction *ka,
190 struct pt_regs *regs, int framesize)
192 unsigned long sp = regs->UCreg_sp;
193 void __user *frame;
196 * This is the X/Open sanctioned signal stack switching.
198 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
199 sp = current->sas_ss_sp + current->sas_ss_size;
202 * ATPCS B01 mandates 8-byte alignment
204 frame = (void __user *)((sp - framesize) & ~7);
207 * Check that we can actually write to the signal frame.
209 if (!access_ok(VERIFY_WRITE, frame, framesize))
210 frame = NULL;
212 return frame;
215 static int setup_return(struct pt_regs *regs, struct k_sigaction *ka,
216 unsigned long __user *rc, void __user *frame, int usig)
218 unsigned long handler = (unsigned long)ka->sa.sa_handler;
219 unsigned long retcode;
220 unsigned long asr = regs->UCreg_asr & ~PSR_f;
222 unsigned int idx = 0;
224 if (ka->sa.sa_flags & SA_SIGINFO)
225 idx += 1;
227 if (__put_user(sigreturn_codes[idx], rc) ||
228 __put_user(sigreturn_codes[idx+1], rc+1))
229 return 1;
231 retcode = KERN_SIGRETURN_CODE + (idx << 2);
233 regs->UCreg_00 = usig;
234 regs->UCreg_sp = (unsigned long)frame;
235 regs->UCreg_lr = retcode;
236 regs->UCreg_pc = handler;
237 regs->UCreg_asr = asr;
239 return 0;
242 static int setup_frame(int usig, struct k_sigaction *ka,
243 sigset_t *set, struct pt_regs *regs)
245 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
246 int err = 0;
248 if (!frame)
249 return 1;
252 * Set uc.uc_flags to a value which sc.trap_no would never have.
254 err |= __put_user(0x5ac3c35a, &frame->uc.uc_flags);
256 err |= setup_sigframe(frame, regs, set);
257 if (err == 0)
258 err |= setup_return(regs, ka, frame->retcode, frame, usig);
260 return err;
263 static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
264 sigset_t *set, struct pt_regs *regs)
266 struct rt_sigframe __user *frame =
267 get_sigframe(ka, regs, sizeof(*frame));
268 stack_t stack;
269 int err = 0;
271 if (!frame)
272 return 1;
274 err |= copy_siginfo_to_user(&frame->info, info);
276 err |= __put_user(0, &frame->sig.uc.uc_flags);
277 err |= __put_user(NULL, &frame->sig.uc.uc_link);
279 memset(&stack, 0, sizeof(stack));
280 stack.ss_sp = (void __user *)current->sas_ss_sp;
281 stack.ss_flags = sas_ss_flags(regs->UCreg_sp);
282 stack.ss_size = current->sas_ss_size;
283 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
285 err |= setup_sigframe(&frame->sig, regs, set);
286 if (err == 0)
287 err |= setup_return(regs, ka, frame->sig.retcode, frame, usig);
289 if (err == 0) {
291 * For realtime signals we must also set the second and third
292 * arguments for the signal handler.
294 regs->UCreg_01 = (unsigned long)&frame->info;
295 regs->UCreg_02 = (unsigned long)&frame->sig.uc;
298 return err;
301 static inline void setup_syscall_restart(struct pt_regs *regs)
303 regs->UCreg_00 = regs->UCreg_ORIG_00;
304 regs->UCreg_pc -= 4;
308 * OK, we're invoking a handler
310 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
311 siginfo_t *info, struct pt_regs *regs, int syscall)
313 struct thread_info *thread = current_thread_info();
314 struct task_struct *tsk = current;
315 sigset_t *oldset = sigmask_to_save();
316 int usig = sig;
317 int ret;
320 * If we were from a system call, check for system call restarting...
322 if (syscall) {
323 switch (regs->UCreg_00) {
324 case -ERESTART_RESTARTBLOCK:
325 case -ERESTARTNOHAND:
326 regs->UCreg_00 = -EINTR;
327 break;
328 case -ERESTARTSYS:
329 if (!(ka->sa.sa_flags & SA_RESTART)) {
330 regs->UCreg_00 = -EINTR;
331 break;
333 /* fallthrough */
334 case -ERESTARTNOINTR:
335 setup_syscall_restart(regs);
340 * translate the signal
342 if (usig < 32 && thread->exec_domain
343 && thread->exec_domain->signal_invmap)
344 usig = thread->exec_domain->signal_invmap[usig];
347 * Set up the stack frame
349 if (ka->sa.sa_flags & SA_SIGINFO)
350 ret = setup_rt_frame(usig, ka, info, oldset, regs);
351 else
352 ret = setup_frame(usig, ka, oldset, regs);
355 * Check that the resulting registers are actually sane.
357 ret |= !valid_user_regs(regs);
359 if (ret != 0) {
360 force_sigsegv(sig, tsk);
361 return;
364 signal_delivered(sig, info, ka, regs, 0);
368 * Note that 'init' is a special process: it doesn't get signals it doesn't
369 * want to handle. Thus you cannot kill init even with a SIGKILL even by
370 * mistake.
372 * Note that we go through the signals twice: once to check the signals that
373 * the kernel can handle, and then we build all the user-level signal handling
374 * stack-frames in one go after that.
376 static void do_signal(struct pt_regs *regs, int syscall)
378 struct k_sigaction ka;
379 siginfo_t info;
380 int signr;
383 * We want the common case to go fast, which
384 * is why we may in certain cases get here from
385 * kernel mode. Just return without doing anything
386 * if so.
388 if (!user_mode(regs))
389 return;
391 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
392 if (signr > 0) {
393 handle_signal(signr, &ka, &info, regs, syscall);
394 return;
398 * No signal to deliver to the process - restart the syscall.
400 if (syscall) {
401 if (regs->UCreg_00 == -ERESTART_RESTARTBLOCK) {
402 u32 __user *usp;
404 regs->UCreg_sp -= 4;
405 usp = (u32 __user *)regs->UCreg_sp;
407 if (put_user(regs->UCreg_pc, usp) == 0) {
408 regs->UCreg_pc = KERN_RESTART_CODE;
409 } else {
410 regs->UCreg_sp += 4;
411 force_sigsegv(0, current);
414 if (regs->UCreg_00 == -ERESTARTNOHAND ||
415 regs->UCreg_00 == -ERESTARTSYS ||
416 regs->UCreg_00 == -ERESTARTNOINTR) {
417 setup_syscall_restart(regs);
420 /* If there's no signal to deliver, we just put the saved
421 * sigmask back.
423 restore_saved_sigmask();
426 asmlinkage void do_notify_resume(struct pt_regs *regs,
427 unsigned int thread_flags, int syscall)
429 if (thread_flags & _TIF_SIGPENDING)
430 do_signal(regs, syscall);
432 if (thread_flags & _TIF_NOTIFY_RESUME) {
433 clear_thread_flag(TIF_NOTIFY_RESUME);
434 tracehook_notify_resume(regs);
439 * Copy signal return handlers into the vector page, and
440 * set sigreturn to be a pointer to these.
442 void __init early_signal_init(void)
444 memcpy((void *)kuser_vecpage_to_vectors(KERN_SIGRETURN_CODE),
445 sigreturn_codes, sizeof(sigreturn_codes));
446 memcpy((void *)kuser_vecpage_to_vectors(KERN_RESTART_CODE),
447 syscall_restart_code, sizeof(syscall_restart_code));
448 /* Need not to flush icache, since early_trap_init will do it last. */