ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / arch / sparc64 / kernel / signal32.c
blob97cdd1bf4a100201a60a77de7f40ab37e5fb27e8
1 /* arch/sparc64/kernel/signal32.c
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 */
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/signal.h>
13 #include <linux/errno.h>
14 #include <linux/wait.h>
15 #include <linux/ptrace.h>
16 #include <linux/unistd.h>
17 #include <linux/mm.h>
18 #include <linux/tty.h>
19 #include <linux/binfmts.h>
20 #include <linux/compat.h>
21 #include <linux/bitops.h>
23 #include <asm/uaccess.h>
24 #include <asm/ptrace.h>
25 #include <asm/pgtable.h>
26 #include <asm/psrcompat.h>
27 #include <asm/fpumacro.h>
28 #include <asm/visasm.h>
29 #include <asm/compat_signal.h>
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 /* This magic should be in g_upper[0] for all upper parts
34 * to be valid.
36 #define SIGINFO_EXTRA_V8PLUS_MAGIC 0x130e269
37 typedef struct {
38 unsigned int g_upper[8];
39 unsigned int o_upper[8];
40 unsigned int asi;
41 } siginfo_extra_v8plus_t;
43 struct signal_frame32 {
44 struct sparc_stackf32 ss;
45 __siginfo32_t info;
46 /* __siginfo_fpu32_t * */ u32 fpu_save;
47 unsigned int insns[2];
48 unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
49 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
50 /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
51 siginfo_extra_v8plus_t v8plus;
52 __siginfo_fpu_t fpu_state;
55 typedef struct compat_siginfo{
56 int si_signo;
57 int si_errno;
58 int si_code;
60 union {
61 int _pad[SI_PAD_SIZE32];
63 /* kill() */
64 struct {
65 compat_pid_t _pid; /* sender's pid */
66 unsigned int _uid; /* sender's uid */
67 } _kill;
69 /* POSIX.1b timers */
70 struct {
71 compat_timer_t _tid; /* timer id */
72 int _overrun; /* overrun count */
73 compat_sigval_t _sigval; /* same as below */
74 int _sys_private; /* not to be passed to user */
75 } _timer;
77 /* POSIX.1b signals */
78 struct {
79 compat_pid_t _pid; /* sender's pid */
80 unsigned int _uid; /* sender's uid */
81 compat_sigval_t _sigval;
82 } _rt;
84 /* SIGCHLD */
85 struct {
86 compat_pid_t _pid; /* which child */
87 unsigned int _uid; /* sender's uid */
88 int _status; /* exit code */
89 compat_clock_t _utime;
90 compat_clock_t _stime;
91 } _sigchld;
93 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGEMT */
94 struct {
95 u32 _addr; /* faulting insn/memory ref. */
96 int _trapno;
97 } _sigfault;
99 /* SIGPOLL */
100 struct {
101 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
102 int _fd;
103 } _sigpoll;
104 } _sifields;
105 }compat_siginfo_t;
107 struct rt_signal_frame32 {
108 struct sparc_stackf32 ss;
109 compat_siginfo_t info;
110 struct pt_regs32 regs;
111 compat_sigset_t mask;
112 /* __siginfo_fpu32_t * */ u32 fpu_save;
113 unsigned int insns[2];
114 stack_t32 stack;
115 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
116 /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
117 siginfo_extra_v8plus_t v8plus;
118 __siginfo_fpu_t fpu_state;
121 /* Align macros */
122 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame32) + 7) & (~7)))
123 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame32) + 7) & (~7)))
125 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
127 int err;
129 if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
130 return -EFAULT;
132 /* If you change siginfo_t structure, please be sure
133 this code is fixed accordingly.
134 It should never copy any pad contained in the structure
135 to avoid security leaks, but must copy the generic
136 3 ints plus the relevant union member.
137 This routine must convert siginfo from 64bit to 32bit as well
138 at the same time. */
139 err = __put_user(from->si_signo, &to->si_signo);
140 err |= __put_user(from->si_errno, &to->si_errno);
141 err |= __put_user((short)from->si_code, &to->si_code);
142 if (from->si_code < 0)
143 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
144 else {
145 switch (from->si_code >> 16) {
146 case __SI_TIMER >> 16:
147 err |= __put_user(from->si_tid, &to->si_tid);
148 err |= __put_user(from->si_overrun, &to->si_overrun);
149 err |= __put_user(from->si_int, &to->si_int);
150 break;
151 case __SI_CHLD >> 16:
152 err |= __put_user(from->si_utime, &to->si_utime);
153 err |= __put_user(from->si_stime, &to->si_stime);
154 err |= __put_user(from->si_status, &to->si_status);
155 default:
156 err |= __put_user(from->si_pid, &to->si_pid);
157 err |= __put_user(from->si_uid, &to->si_uid);
158 break;
159 case __SI_FAULT >> 16:
160 err |= __put_user(from->si_trapno, &to->si_trapno);
161 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
162 break;
163 case __SI_POLL >> 16:
164 err |= __put_user(from->si_band, &to->si_band);
165 err |= __put_user(from->si_fd, &to->si_fd);
166 break;
167 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
168 case __SI_MESGQ >> 16:
169 err |= __put_user(from->si_pid, &to->si_pid);
170 err |= __put_user(from->si_uid, &to->si_uid);
171 err |= __put_user(from->si_int, &to->si_int);
172 break;
175 return err;
178 /* CAUTION: This is just a very minimalist implementation for the
179 * sake of compat_sys_rt_sigqueueinfo()
181 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
183 if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
184 return -EFAULT;
186 if (copy_from_user(to, from, 3*sizeof(int)) ||
187 copy_from_user(to->_sifields._pad, from->_sifields._pad,
188 SI_PAD_SIZE))
189 return -EFAULT;
191 return 0;
194 static int restore_fpu_state32(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
196 unsigned long *fpregs = current_thread_info()->fpregs;
197 unsigned long fprs;
198 int err;
200 err = __get_user(fprs, &fpu->si_fprs);
201 fprs_write(0);
202 regs->tstate &= ~TSTATE_PEF;
203 if (fprs & FPRS_DL)
204 err |= copy_from_user(fpregs, &fpu->si_float_regs[0], (sizeof(unsigned int) * 32));
205 if (fprs & FPRS_DU)
206 err |= copy_from_user(fpregs+16, &fpu->si_float_regs[32], (sizeof(unsigned int) * 32));
207 err |= __get_user(current_thread_info()->xfsr[0], &fpu->si_fsr);
208 err |= __get_user(current_thread_info()->gsr[0], &fpu->si_gsr);
209 current_thread_info()->fpsaved[0] |= fprs;
210 return err;
213 void do_sigreturn32(struct pt_regs *regs)
215 struct signal_frame32 __user *sf;
216 unsigned int psr;
217 unsigned pc, npc, fpu_save;
218 sigset_t set;
219 unsigned seta[_COMPAT_NSIG_WORDS];
220 int err, i;
222 /* Always make any pending restarted system calls return -EINTR */
223 current_thread_info()->restart_block.fn = do_no_restart_syscall;
225 synchronize_user_stack();
227 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
228 sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
230 /* 1. Make sure we are not getting garbage from the user */
231 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
232 (((unsigned long) sf) & 3))
233 goto segv;
235 get_user(pc, &sf->info.si_regs.pc);
236 __get_user(npc, &sf->info.si_regs.npc);
238 if ((pc | npc) & 3)
239 goto segv;
241 if (test_thread_flag(TIF_32BIT)) {
242 pc &= 0xffffffff;
243 npc &= 0xffffffff;
245 regs->tpc = pc;
246 regs->tnpc = npc;
248 /* 2. Restore the state */
249 err = __get_user(regs->y, &sf->info.si_regs.y);
250 err |= __get_user(psr, &sf->info.si_regs.psr);
252 for (i = UREG_G1; i <= UREG_I7; i++)
253 err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
254 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
255 err |= __get_user(i, &sf->v8plus.g_upper[0]);
256 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
257 unsigned long asi;
259 for (i = UREG_G1; i <= UREG_I7; i++)
260 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
261 err |= __get_user(asi, &sf->v8plus.asi);
262 regs->tstate &= ~TSTATE_ASI;
263 regs->tstate |= ((asi & 0xffUL) << 24UL);
267 /* User can only change condition codes in %tstate. */
268 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
269 regs->tstate |= psr_to_tstate_icc(psr);
271 /* Prevent syscall restart. */
272 pt_regs_clear_syscall(regs);
274 err |= __get_user(fpu_save, &sf->fpu_save);
275 if (fpu_save)
276 err |= restore_fpu_state32(regs, &sf->fpu_state);
277 err |= __get_user(seta[0], &sf->info.si_mask);
278 err |= copy_from_user(seta+1, &sf->extramask,
279 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
280 if (err)
281 goto segv;
282 switch (_NSIG_WORDS) {
283 case 4: set.sig[3] = seta[6] + (((long)seta[7]) << 32);
284 case 3: set.sig[2] = seta[4] + (((long)seta[5]) << 32);
285 case 2: set.sig[1] = seta[2] + (((long)seta[3]) << 32);
286 case 1: set.sig[0] = seta[0] + (((long)seta[1]) << 32);
288 sigdelsetmask(&set, ~_BLOCKABLE);
289 spin_lock_irq(&current->sighand->siglock);
290 current->blocked = set;
291 recalc_sigpending();
292 spin_unlock_irq(&current->sighand->siglock);
293 return;
295 segv:
296 force_sig(SIGSEGV, current);
299 asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
301 struct rt_signal_frame32 __user *sf;
302 unsigned int psr, pc, npc, fpu_save, u_ss_sp;
303 mm_segment_t old_fs;
304 sigset_t set;
305 compat_sigset_t seta;
306 stack_t st;
307 int err, i;
309 /* Always make any pending restarted system calls return -EINTR */
310 current_thread_info()->restart_block.fn = do_no_restart_syscall;
312 synchronize_user_stack();
313 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
314 sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
316 /* 1. Make sure we are not getting garbage from the user */
317 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
318 (((unsigned long) sf) & 3))
319 goto segv;
321 get_user(pc, &sf->regs.pc);
322 __get_user(npc, &sf->regs.npc);
324 if ((pc | npc) & 3)
325 goto segv;
327 if (test_thread_flag(TIF_32BIT)) {
328 pc &= 0xffffffff;
329 npc &= 0xffffffff;
331 regs->tpc = pc;
332 regs->tnpc = npc;
334 /* 2. Restore the state */
335 err = __get_user(regs->y, &sf->regs.y);
336 err |= __get_user(psr, &sf->regs.psr);
338 for (i = UREG_G1; i <= UREG_I7; i++)
339 err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
340 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
341 err |= __get_user(i, &sf->v8plus.g_upper[0]);
342 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
343 unsigned long asi;
345 for (i = UREG_G1; i <= UREG_I7; i++)
346 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
347 err |= __get_user(asi, &sf->v8plus.asi);
348 regs->tstate &= ~TSTATE_ASI;
349 regs->tstate |= ((asi & 0xffUL) << 24UL);
353 /* User can only change condition codes in %tstate. */
354 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
355 regs->tstate |= psr_to_tstate_icc(psr);
357 /* Prevent syscall restart. */
358 pt_regs_clear_syscall(regs);
360 err |= __get_user(fpu_save, &sf->fpu_save);
361 if (fpu_save)
362 err |= restore_fpu_state32(regs, &sf->fpu_state);
363 err |= copy_from_user(&seta, &sf->mask, sizeof(compat_sigset_t));
364 err |= __get_user(u_ss_sp, &sf->stack.ss_sp);
365 st.ss_sp = compat_ptr(u_ss_sp);
366 err |= __get_user(st.ss_flags, &sf->stack.ss_flags);
367 err |= __get_user(st.ss_size, &sf->stack.ss_size);
368 if (err)
369 goto segv;
371 /* It is more difficult to avoid calling this function than to
372 call it and ignore errors. */
373 old_fs = get_fs();
374 set_fs(KERNEL_DS);
375 do_sigaltstack((stack_t __user *) &st, NULL, (unsigned long)sf);
376 set_fs(old_fs);
378 switch (_NSIG_WORDS) {
379 case 4: set.sig[3] = seta.sig[6] + (((long)seta.sig[7]) << 32);
380 case 3: set.sig[2] = seta.sig[4] + (((long)seta.sig[5]) << 32);
381 case 2: set.sig[1] = seta.sig[2] + (((long)seta.sig[3]) << 32);
382 case 1: set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
384 sigdelsetmask(&set, ~_BLOCKABLE);
385 spin_lock_irq(&current->sighand->siglock);
386 current->blocked = set;
387 recalc_sigpending();
388 spin_unlock_irq(&current->sighand->siglock);
389 return;
390 segv:
391 force_sig(SIGSEGV, current);
394 /* Checks if the fp is valid */
395 static int invalid_frame_pointer(void __user *fp, int fplen)
397 if ((((unsigned long) fp) & 7) || ((unsigned long)fp) > 0x100000000ULL - fplen)
398 return 1;
399 return 0;
402 static void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
404 unsigned long sp;
406 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
407 sp = regs->u_regs[UREG_FP];
410 * If we are on the alternate signal stack and would overflow it, don't.
411 * Return an always-bogus address instead so we will die with SIGSEGV.
413 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
414 return (void __user *) -1L;
416 /* This is the X/Open sanctioned signal stack switching. */
417 if (sa->sa_flags & SA_ONSTACK) {
418 if (sas_ss_flags(sp) == 0)
419 sp = current->sas_ss_sp + current->sas_ss_size;
422 /* Always align the stack frame. This handles two cases. First,
423 * sigaltstack need not be mindful of platform specific stack
424 * alignment. Second, if we took this signal because the stack
425 * is not aligned properly, we'd like to take the signal cleanly
426 * and report that.
428 sp &= ~7UL;
430 return (void __user *)(sp - framesize);
433 static int save_fpu_state32(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
435 unsigned long *fpregs = current_thread_info()->fpregs;
436 unsigned long fprs;
437 int err = 0;
439 fprs = current_thread_info()->fpsaved[0];
440 if (fprs & FPRS_DL)
441 err |= copy_to_user(&fpu->si_float_regs[0], fpregs,
442 (sizeof(unsigned int) * 32));
443 if (fprs & FPRS_DU)
444 err |= copy_to_user(&fpu->si_float_regs[32], fpregs+16,
445 (sizeof(unsigned int) * 32));
446 err |= __put_user(current_thread_info()->xfsr[0], &fpu->si_fsr);
447 err |= __put_user(current_thread_info()->gsr[0], &fpu->si_gsr);
448 err |= __put_user(fprs, &fpu->si_fprs);
450 return err;
453 static void setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
454 int signo, sigset_t *oldset)
456 struct signal_frame32 __user *sf;
457 int sigframe_size;
458 u32 psr;
459 int i, err;
460 unsigned int seta[_COMPAT_NSIG_WORDS];
462 /* 1. Make sure everything is clean */
463 synchronize_user_stack();
464 save_and_clear_fpu();
466 sigframe_size = SF_ALIGNEDSZ;
467 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF))
468 sigframe_size -= sizeof(__siginfo_fpu_t);
470 sf = (struct signal_frame32 __user *)
471 get_sigframe(&ka->sa, regs, sigframe_size);
473 if (invalid_frame_pointer(sf, sigframe_size))
474 goto sigill;
476 if (get_thread_wsaved() != 0)
477 goto sigill;
479 /* 2. Save the current process state */
480 if (test_thread_flag(TIF_32BIT)) {
481 regs->tpc &= 0xffffffff;
482 regs->tnpc &= 0xffffffff;
484 err = put_user(regs->tpc, &sf->info.si_regs.pc);
485 err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
486 err |= __put_user(regs->y, &sf->info.si_regs.y);
487 psr = tstate_to_psr(regs->tstate);
488 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
489 psr |= PSR_EF;
490 err |= __put_user(psr, &sf->info.si_regs.psr);
491 for (i = 0; i < 16; i++)
492 err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
493 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
494 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
495 for (i = 1; i < 16; i++)
496 err |= __put_user(((u32 *)regs->u_regs)[2*i],
497 &sf->v8plus.g_upper[i]);
498 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
499 &sf->v8plus.asi);
501 if (psr & PSR_EF) {
502 err |= save_fpu_state32(regs, &sf->fpu_state);
503 err |= __put_user((u64)&sf->fpu_state, &sf->fpu_save);
504 } else {
505 err |= __put_user(0, &sf->fpu_save);
508 switch (_NSIG_WORDS) {
509 case 4: seta[7] = (oldset->sig[3] >> 32);
510 seta[6] = oldset->sig[3];
511 case 3: seta[5] = (oldset->sig[2] >> 32);
512 seta[4] = oldset->sig[2];
513 case 2: seta[3] = (oldset->sig[1] >> 32);
514 seta[2] = oldset->sig[1];
515 case 1: seta[1] = (oldset->sig[0] >> 32);
516 seta[0] = oldset->sig[0];
518 err |= __put_user(seta[0], &sf->info.si_mask);
519 err |= __copy_to_user(sf->extramask, seta + 1,
520 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
522 err |= copy_in_user((u32 __user *)sf,
523 (u32 __user *)(regs->u_regs[UREG_FP]),
524 sizeof(struct reg_window32));
526 if (err)
527 goto sigsegv;
529 /* 3. signal handler back-trampoline and parameters */
530 regs->u_regs[UREG_FP] = (unsigned long) sf;
531 regs->u_regs[UREG_I0] = signo;
532 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
533 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
535 /* 4. signal handler */
536 regs->tpc = (unsigned long) ka->sa.sa_handler;
537 regs->tnpc = (regs->tpc + 4);
538 if (test_thread_flag(TIF_32BIT)) {
539 regs->tpc &= 0xffffffff;
540 regs->tnpc &= 0xffffffff;
543 /* 5. return to kernel instructions */
544 if (ka->ka_restorer) {
545 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
546 } else {
547 /* Flush instruction space. */
548 unsigned long address = ((unsigned long)&(sf->insns[0]));
549 pgd_t *pgdp = pgd_offset(current->mm, address);
550 pud_t *pudp = pud_offset(pgdp, address);
551 pmd_t *pmdp = pmd_offset(pudp, address);
552 pte_t *ptep;
553 pte_t pte;
555 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
557 err = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
558 err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
559 if (err)
560 goto sigsegv;
562 preempt_disable();
563 ptep = pte_offset_map(pmdp, address);
564 pte = *ptep;
565 if (pte_present(pte)) {
566 unsigned long page = (unsigned long)
567 page_address(pte_page(pte));
569 wmb();
570 __asm__ __volatile__("flush %0 + %1"
571 : /* no outputs */
572 : "r" (page),
573 "r" (address & (PAGE_SIZE - 1))
574 : "memory");
576 pte_unmap(ptep);
577 preempt_enable();
579 return;
581 sigill:
582 do_exit(SIGILL);
583 sigsegv:
584 force_sigsegv(signo, current);
587 static void setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
588 unsigned long signr, sigset_t *oldset,
589 siginfo_t *info)
591 struct rt_signal_frame32 __user *sf;
592 int sigframe_size;
593 u32 psr;
594 int i, err;
595 compat_sigset_t seta;
597 /* 1. Make sure everything is clean */
598 synchronize_user_stack();
599 save_and_clear_fpu();
601 sigframe_size = RT_ALIGNEDSZ;
602 if (!(current_thread_info()->fpsaved[0] & FPRS_FEF))
603 sigframe_size -= sizeof(__siginfo_fpu_t);
605 sf = (struct rt_signal_frame32 __user *)
606 get_sigframe(&ka->sa, regs, sigframe_size);
608 if (invalid_frame_pointer(sf, sigframe_size))
609 goto sigill;
611 if (get_thread_wsaved() != 0)
612 goto sigill;
614 /* 2. Save the current process state */
615 if (test_thread_flag(TIF_32BIT)) {
616 regs->tpc &= 0xffffffff;
617 regs->tnpc &= 0xffffffff;
619 err = put_user(regs->tpc, &sf->regs.pc);
620 err |= __put_user(regs->tnpc, &sf->regs.npc);
621 err |= __put_user(regs->y, &sf->regs.y);
622 psr = tstate_to_psr(regs->tstate);
623 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
624 psr |= PSR_EF;
625 err |= __put_user(psr, &sf->regs.psr);
626 for (i = 0; i < 16; i++)
627 err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
628 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
629 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
630 for (i = 1; i < 16; i++)
631 err |= __put_user(((u32 *)regs->u_regs)[2*i],
632 &sf->v8plus.g_upper[i]);
633 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
634 &sf->v8plus.asi);
636 if (psr & PSR_EF) {
637 err |= save_fpu_state32(regs, &sf->fpu_state);
638 err |= __put_user((u64)&sf->fpu_state, &sf->fpu_save);
639 } else {
640 err |= __put_user(0, &sf->fpu_save);
643 /* Update the siginfo structure. */
644 err |= copy_siginfo_to_user32(&sf->info, info);
646 /* Setup sigaltstack */
647 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
648 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
649 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
651 switch (_NSIG_WORDS) {
652 case 4: seta.sig[7] = (oldset->sig[3] >> 32);
653 seta.sig[6] = oldset->sig[3];
654 case 3: seta.sig[5] = (oldset->sig[2] >> 32);
655 seta.sig[4] = oldset->sig[2];
656 case 2: seta.sig[3] = (oldset->sig[1] >> 32);
657 seta.sig[2] = oldset->sig[1];
658 case 1: seta.sig[1] = (oldset->sig[0] >> 32);
659 seta.sig[0] = oldset->sig[0];
661 err |= __copy_to_user(&sf->mask, &seta, sizeof(compat_sigset_t));
663 err |= copy_in_user((u32 __user *)sf,
664 (u32 __user *)(regs->u_regs[UREG_FP]),
665 sizeof(struct reg_window32));
666 if (err)
667 goto sigsegv;
669 /* 3. signal handler back-trampoline and parameters */
670 regs->u_regs[UREG_FP] = (unsigned long) sf;
671 regs->u_regs[UREG_I0] = signr;
672 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
673 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
675 /* 4. signal handler */
676 regs->tpc = (unsigned long) ka->sa.sa_handler;
677 regs->tnpc = (regs->tpc + 4);
678 if (test_thread_flag(TIF_32BIT)) {
679 regs->tpc &= 0xffffffff;
680 regs->tnpc &= 0xffffffff;
683 /* 5. return to kernel instructions */
684 if (ka->ka_restorer)
685 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
686 else {
687 /* Flush instruction space. */
688 unsigned long address = ((unsigned long)&(sf->insns[0]));
689 pgd_t *pgdp = pgd_offset(current->mm, address);
690 pud_t *pudp = pud_offset(pgdp, address);
691 pmd_t *pmdp = pmd_offset(pudp, address);
692 pte_t *ptep;
694 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
696 /* mov __NR_rt_sigreturn, %g1 */
697 err |= __put_user(0x82102065, &sf->insns[0]);
699 /* t 0x10 */
700 err |= __put_user(0x91d02010, &sf->insns[1]);
701 if (err)
702 goto sigsegv;
704 preempt_disable();
705 ptep = pte_offset_map(pmdp, address);
706 if (pte_present(*ptep)) {
707 unsigned long page = (unsigned long)
708 page_address(pte_page(*ptep));
710 wmb();
711 __asm__ __volatile__("flush %0 + %1"
712 : /* no outputs */
713 : "r" (page),
714 "r" (address & (PAGE_SIZE - 1))
715 : "memory");
717 pte_unmap(ptep);
718 preempt_enable();
720 return;
722 sigill:
723 do_exit(SIGILL);
724 sigsegv:
725 force_sigsegv(signr, current);
728 static inline void handle_signal32(unsigned long signr, struct k_sigaction *ka,
729 siginfo_t *info,
730 sigset_t *oldset, struct pt_regs *regs)
732 if (ka->sa.sa_flags & SA_SIGINFO)
733 setup_rt_frame32(ka, regs, signr, oldset, info);
734 else
735 setup_frame32(ka, regs, signr, oldset);
737 spin_lock_irq(&current->sighand->siglock);
738 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
739 if (!(ka->sa.sa_flags & SA_NOMASK))
740 sigaddset(&current->blocked,signr);
741 recalc_sigpending();
742 spin_unlock_irq(&current->sighand->siglock);
745 static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
746 struct sigaction *sa)
748 switch (regs->u_regs[UREG_I0]) {
749 case ERESTART_RESTARTBLOCK:
750 case ERESTARTNOHAND:
751 no_system_call_restart:
752 regs->u_regs[UREG_I0] = EINTR;
753 regs->tstate |= TSTATE_ICARRY;
754 break;
755 case ERESTARTSYS:
756 if (!(sa->sa_flags & SA_RESTART))
757 goto no_system_call_restart;
758 /* fallthrough */
759 case ERESTARTNOINTR:
760 regs->u_regs[UREG_I0] = orig_i0;
761 regs->tpc -= 4;
762 regs->tnpc -= 4;
766 /* Note that 'init' is a special process: it doesn't get signals it doesn't
767 * want to handle. Thus you cannot kill init even with a SIGKILL even by
768 * mistake.
770 void do_signal32(sigset_t *oldset, struct pt_regs * regs,
771 int restart_syscall, unsigned long orig_i0)
773 struct k_sigaction ka;
774 siginfo_t info;
775 int signr;
777 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
779 /* If the debugger messes with the program counter, it clears
780 * the "in syscall" bit, directing us to not perform a syscall
781 * restart.
783 if (restart_syscall && !pt_regs_is_syscall(regs))
784 restart_syscall = 0;
786 if (signr > 0) {
787 if (restart_syscall)
788 syscall_restart32(orig_i0, regs, &ka.sa);
789 handle_signal32(signr, &ka, &info, oldset, regs);
791 /* A signal was successfully delivered; the saved
792 * sigmask will have been stored in the signal frame,
793 * and will be restored by sigreturn, so we can simply
794 * clear the TS_RESTORE_SIGMASK flag.
796 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
797 return;
799 if (restart_syscall &&
800 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
801 regs->u_regs[UREG_I0] == ERESTARTSYS ||
802 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
803 /* replay the system call when we are done */
804 regs->u_regs[UREG_I0] = orig_i0;
805 regs->tpc -= 4;
806 regs->tnpc -= 4;
808 if (restart_syscall &&
809 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
810 regs->u_regs[UREG_G1] = __NR_restart_syscall;
811 regs->tpc -= 4;
812 regs->tnpc -= 4;
815 /* If there's no signal to deliver, we just put the saved sigmask
816 * back
818 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
819 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
820 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
824 struct sigstack32 {
825 u32 the_stack;
826 int cur_status;
829 asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
831 struct sigstack32 __user *ssptr =
832 (struct sigstack32 __user *)((unsigned long)(u_ssptr));
833 struct sigstack32 __user *ossptr =
834 (struct sigstack32 __user *)((unsigned long)(u_ossptr));
835 int ret = -EFAULT;
837 /* First see if old state is wanted. */
838 if (ossptr) {
839 if (put_user(current->sas_ss_sp + current->sas_ss_size,
840 &ossptr->the_stack) ||
841 __put_user(on_sig_stack(sp), &ossptr->cur_status))
842 goto out;
845 /* Now see if we want to update the new state. */
846 if (ssptr) {
847 u32 ss_sp;
849 if (get_user(ss_sp, &ssptr->the_stack))
850 goto out;
852 /* If the current stack was set with sigaltstack, don't
853 * swap stacks while we are on it.
855 ret = -EPERM;
856 if (current->sas_ss_sp && on_sig_stack(sp))
857 goto out;
859 /* Since we don't know the extent of the stack, and we don't
860 * track onstack-ness, but rather calculate it, we must
861 * presume a size. Ho hum this interface is lossy.
863 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
864 current->sas_ss_size = SIGSTKSZ;
867 ret = 0;
868 out:
869 return ret;
872 asmlinkage long do_sys32_sigaltstack(u32 ussa, u32 uossa, unsigned long sp)
874 stack_t uss, uoss;
875 u32 u_ss_sp = 0;
876 int ret;
877 mm_segment_t old_fs;
878 stack_t32 __user *uss32 = compat_ptr(ussa);
879 stack_t32 __user *uoss32 = compat_ptr(uossa);
881 if (ussa && (get_user(u_ss_sp, &uss32->ss_sp) ||
882 __get_user(uss.ss_flags, &uss32->ss_flags) ||
883 __get_user(uss.ss_size, &uss32->ss_size)))
884 return -EFAULT;
885 uss.ss_sp = compat_ptr(u_ss_sp);
886 old_fs = get_fs();
887 set_fs(KERNEL_DS);
888 ret = do_sigaltstack(ussa ? (stack_t __user *) &uss : NULL,
889 uossa ? (stack_t __user *) &uoss : NULL, sp);
890 set_fs(old_fs);
891 if (!ret && uossa && (put_user(ptr_to_compat(uoss.ss_sp), &uoss32->ss_sp) ||
892 __put_user(uoss.ss_flags, &uoss32->ss_flags) ||
893 __put_user(uoss.ss_size, &uoss32->ss_size)))
894 return -EFAULT;
895 return ret;