blob: 9d9eb91d0de1114ea7632db4bb74e54775397c7d [file] [log] [blame]
David S. Miller5526b7e2008-04-27 02:26:36 -07001/* arch/sparc64/kernel/signal32.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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 */
9
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/binfmts.h>
20#include <linux/compat.h>
21#include <linux/bitops.h>
Roland McGrath95698462008-07-27 01:08:02 -070022#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
25#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/pgtable.h>
27#include <asm/psrcompat.h>
28#include <asm/fpumacro.h>
29#include <asm/visasm.h>
David S. Miller14cc6ab2006-10-02 14:17:57 -070030#include <asm/compat_signal.h>
David Howellsd550bbd2012-03-28 18:30:03 +010031#include <asm/switch_to.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David S. Miller55984732011-08-20 17:14:54 -070033#include "sigutil.h"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* This magic should be in g_upper[0] for all upper parts
36 * to be valid.
37 */
38#define SIGINFO_EXTRA_V8PLUS_MAGIC 0x130e269
39typedef struct {
40 unsigned int g_upper[8];
41 unsigned int o_upper[8];
42 unsigned int asi;
43} siginfo_extra_v8plus_t;
44
David S. Miller5526b7e2008-04-27 02:26:36 -070045struct signal_frame32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct sparc_stackf32 ss;
47 __siginfo32_t info;
David S. Miller55984732011-08-20 17:14:54 -070048 /* __siginfo_fpu_t * */ u32 fpu_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned int insns[2];
50 unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
51 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
52 /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
53 siginfo_extra_v8plus_t v8plus;
David S. Miller55984732011-08-20 17:14:54 -070054 /* __siginfo_rwin_t * */u32 rwin_save;
55} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct rt_signal_frame32 {
58 struct sparc_stackf32 ss;
59 compat_siginfo_t info;
60 struct pt_regs32 regs;
61 compat_sigset_t mask;
David S. Miller55984732011-08-20 17:14:54 -070062 /* __siginfo_fpu_t * */ u32 fpu_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned int insns[2];
Al Viro99b06fe2012-12-23 03:41:17 -050064 compat_stack_t stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
66 /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
67 siginfo_extra_v8plus_t v8plus;
David S. Miller55984732011-08-20 17:14:54 -070068 /* __siginfo_rwin_t * */u32 rwin_save;
69} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
72{
73 int err;
74
75 if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
76 return -EFAULT;
77
78 /* If you change siginfo_t structure, please be sure
79 this code is fixed accordingly.
80 It should never copy any pad contained in the structure
81 to avoid security leaks, but must copy the generic
82 3 ints plus the relevant union member.
83 This routine must convert siginfo from 64bit to 32bit as well
84 at the same time. */
85 err = __put_user(from->si_signo, &to->si_signo);
86 err |= __put_user(from->si_errno, &to->si_errno);
87 err |= __put_user((short)from->si_code, &to->si_code);
88 if (from->si_code < 0)
89 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
90 else {
91 switch (from->si_code >> 16) {
92 case __SI_TIMER >> 16:
93 err |= __put_user(from->si_tid, &to->si_tid);
94 err |= __put_user(from->si_overrun, &to->si_overrun);
95 err |= __put_user(from->si_int, &to->si_int);
96 break;
97 case __SI_CHLD >> 16:
98 err |= __put_user(from->si_utime, &to->si_utime);
99 err |= __put_user(from->si_stime, &to->si_stime);
100 err |= __put_user(from->si_status, &to->si_status);
101 default:
102 err |= __put_user(from->si_pid, &to->si_pid);
103 err |= __put_user(from->si_uid, &to->si_uid);
104 break;
105 case __SI_FAULT >> 16:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 err |= __put_user(from->si_trapno, &to->si_trapno);
107 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
108 break;
Jurij Smakov9c7d3b32005-04-17 18:03:12 -0700109 case __SI_POLL >> 16:
110 err |= __put_user(from->si_band, &to->si_band);
111 err |= __put_user(from->si_fd, &to->si_fd);
112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
114 case __SI_MESGQ >> 16:
115 err |= __put_user(from->si_pid, &to->si_pid);
116 err |= __put_user(from->si_uid, &to->si_uid);
117 err |= __put_user(from->si_int, &to->si_int);
118 break;
119 }
120 }
121 return err;
122}
123
124/* CAUTION: This is just a very minimalist implementation for the
125 * sake of compat_sys_rt_sigqueueinfo()
126 */
127int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
128{
129 if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
130 return -EFAULT;
131
132 if (copy_from_user(to, from, 3*sizeof(int)) ||
133 copy_from_user(to->_sifields._pad, from->_sifields._pad,
134 SI_PAD_SIZE))
135 return -EFAULT;
136
137 return 0;
138}
139
David S. Miller5526b7e2008-04-27 02:26:36 -0700140void do_sigreturn32(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
David S. Miller5526b7e2008-04-27 02:26:36 -0700142 struct signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700143 compat_uptr_t fpu_save;
144 compat_uptr_t rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 unsigned int psr;
David S. Miller55984732011-08-20 17:14:54 -0700146 unsigned pc, npc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 sigset_t set;
148 unsigned seta[_COMPAT_NSIG_WORDS];
149 int err, i;
150
David S. Miller5526b7e2008-04-27 02:26:36 -0700151 /* Always make any pending restarted system calls return -EINTR */
152 current_thread_info()->restart_block.fn = do_no_restart_syscall;
153
154 synchronize_user_stack();
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
David S. Miller5526b7e2008-04-27 02:26:36 -0700157 sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* 1. Make sure we are not getting garbage from the user */
160 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
161 (((unsigned long) sf) & 3))
162 goto segv;
163
Al Viro187cd442012-04-22 16:51:36 -0400164 if (get_user(pc, &sf->info.si_regs.pc) ||
165 __get_user(npc, &sf->info.si_regs.npc))
166 goto segv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if ((pc | npc) & 3)
169 goto segv;
170
171 if (test_thread_flag(TIF_32BIT)) {
172 pc &= 0xffffffff;
173 npc &= 0xffffffff;
174 }
175 regs->tpc = pc;
176 regs->tnpc = npc;
177
178 /* 2. Restore the state */
179 err = __get_user(regs->y, &sf->info.si_regs.y);
180 err |= __get_user(psr, &sf->info.si_regs.psr);
181
182 for (i = UREG_G1; i <= UREG_I7; i++)
183 err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
184 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
185 err |= __get_user(i, &sf->v8plus.g_upper[0]);
186 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
187 unsigned long asi;
188
189 for (i = UREG_G1; i <= UREG_I7; i++)
190 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
191 err |= __get_user(asi, &sf->v8plus.asi);
192 regs->tstate &= ~TSTATE_ASI;
193 regs->tstate |= ((asi & 0xffUL) << 24UL);
194 }
195 }
196
197 /* User can only change condition codes in %tstate. */
198 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
199 regs->tstate |= psr_to_tstate_icc(psr);
200
David S. Miller2678fef2008-05-01 03:30:22 -0700201 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700202 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700205 if (!err && fpu_save)
206 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
207 err |= __get_user(rwin_save, &sf->rwin_save);
208 if (!err && rwin_save) {
209 if (restore_rwin_state(compat_ptr(rwin_save)))
210 goto segv;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 err |= __get_user(seta[0], &sf->info.si_mask);
213 err |= copy_from_user(seta+1, &sf->extramask,
214 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
215 if (err)
216 goto segv;
217 switch (_NSIG_WORDS) {
218 case 4: set.sig[3] = seta[6] + (((long)seta[7]) << 32);
219 case 3: set.sig[2] = seta[4] + (((long)seta[5]) << 32);
220 case 2: set.sig[1] = seta[2] + (((long)seta[3]) << 32);
221 case 1: set.sig[0] = seta[0] + (((long)seta[1]) << 32);
222 }
Matt Flemingfaddf592011-08-11 14:57:02 +0100223 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return;
225
226segv:
227 force_sig(SIGSEGV, current);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
231{
232 struct rt_signal_frame32 __user *sf;
Al Viro99b06fe2012-12-23 03:41:17 -0500233 unsigned int psr, pc, npc;
David S. Miller55984732011-08-20 17:14:54 -0700234 compat_uptr_t fpu_save;
235 compat_uptr_t rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 sigset_t set;
237 compat_sigset_t seta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int err, i;
239
240 /* Always make any pending restarted system calls return -EINTR */
241 current_thread_info()->restart_block.fn = do_no_restart_syscall;
242
243 synchronize_user_stack();
244 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
245 sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
246
247 /* 1. Make sure we are not getting garbage from the user */
248 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
249 (((unsigned long) sf) & 3))
250 goto segv;
251
Al Viro187cd442012-04-22 16:51:36 -0400252 if (get_user(pc, &sf->regs.pc) ||
253 __get_user(npc, &sf->regs.npc))
254 goto segv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if ((pc | npc) & 3)
257 goto segv;
258
259 if (test_thread_flag(TIF_32BIT)) {
260 pc &= 0xffffffff;
261 npc &= 0xffffffff;
262 }
263 regs->tpc = pc;
264 regs->tnpc = npc;
265
266 /* 2. Restore the state */
267 err = __get_user(regs->y, &sf->regs.y);
268 err |= __get_user(psr, &sf->regs.psr);
269
270 for (i = UREG_G1; i <= UREG_I7; i++)
271 err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
272 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
273 err |= __get_user(i, &sf->v8plus.g_upper[0]);
274 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
275 unsigned long asi;
276
277 for (i = UREG_G1; i <= UREG_I7; i++)
278 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
279 err |= __get_user(asi, &sf->v8plus.asi);
280 regs->tstate &= ~TSTATE_ASI;
281 regs->tstate |= ((asi & 0xffUL) << 24UL);
282 }
283 }
284
285 /* User can only change condition codes in %tstate. */
286 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
287 regs->tstate |= psr_to_tstate_icc(psr);
288
David S. Miller2678fef2008-05-01 03:30:22 -0700289 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700290 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700293 if (!err && fpu_save)
294 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 err |= copy_from_user(&seta, &sf->mask, sizeof(compat_sigset_t));
Al Viro99b06fe2012-12-23 03:41:17 -0500296 err |= compat_restore_altstack(&sf->stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (err)
298 goto segv;
299
David S. Miller55984732011-08-20 17:14:54 -0700300 err |= __get_user(rwin_save, &sf->rwin_save);
301 if (!err && rwin_save) {
302 if (restore_rwin_state(compat_ptr(rwin_save)))
303 goto segv;
304 }
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 switch (_NSIG_WORDS) {
307 case 4: set.sig[3] = seta.sig[6] + (((long)seta.sig[7]) << 32);
308 case 3: set.sig[2] = seta.sig[4] + (((long)seta.sig[5]) << 32);
309 case 2: set.sig[1] = seta.sig[2] + (((long)seta.sig[3]) << 32);
310 case 1: set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
311 }
Matt Flemingfaddf592011-08-11 14:57:02 +0100312 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return;
314segv:
315 force_sig(SIGSEGV, current);
316}
317
318/* Checks if the fp is valid */
319static int invalid_frame_pointer(void __user *fp, int fplen)
320{
321 if ((((unsigned long) fp) & 7) || ((unsigned long)fp) > 0x100000000ULL - fplen)
322 return 1;
323 return 0;
324}
325
326static void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
327{
328 unsigned long sp;
329
330 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
331 sp = regs->u_regs[UREG_FP];
332
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700333 /*
334 * If we are on the alternate signal stack and would overflow it, don't.
335 * Return an always-bogus address instead so we will die with SIGSEGV.
336 */
337 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
338 return (void __user *) -1L;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* This is the X/Open sanctioned signal stack switching. */
341 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700342 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 sp = current->sas_ss_sp + current->sas_ss_size;
344 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700345
David S. Millerf036d9f2010-02-09 16:18:40 -0800346 sp -= framesize;
347
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700348 /* Always align the stack frame. This handles two cases. First,
349 * sigaltstack need not be mindful of platform specific stack
350 * alignment. Second, if we took this signal because the stack
351 * is not aligned properly, we'd like to take the signal cleanly
352 * and report that.
353 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800354 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700355
David S. Millerf036d9f2010-02-09 16:18:40 -0800356 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
David S. Miller05c5e762010-09-20 23:24:52 -0700359/* The I-cache flush instruction only works in the primary ASI, which
360 * right now is the nucleus, aka. kernel space.
361 *
362 * Therefore we have to kick the instructions out using the kernel
363 * side linear mapping of the physical address backing the user
364 * instructions.
365 */
366static void flush_signal_insns(unsigned long address)
367{
368 unsigned long pstate, paddr;
369 pte_t *ptep, pte;
370 pgd_t *pgdp;
371 pud_t *pudp;
372 pmd_t *pmdp;
373
374 /* Commit all stores of the instructions we are about to flush. */
375 wmb();
376
377 /* Disable cross-call reception. In this way even a very wide
378 * munmap() on another cpu can't tear down the page table
379 * hierarchy from underneath us, since that can't complete
380 * until the IPI tlb flush returns.
381 */
382
383 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
384 __asm__ __volatile__("wrpr %0, %1, %%pstate"
385 : : "r" (pstate), "i" (PSTATE_IE));
386
387 pgdp = pgd_offset(current->mm, address);
388 if (pgd_none(*pgdp))
389 goto out_irqs_on;
390 pudp = pud_offset(pgdp, address);
391 if (pud_none(*pudp))
392 goto out_irqs_on;
393 pmdp = pmd_offset(pudp, address);
394 if (pmd_none(*pmdp))
395 goto out_irqs_on;
396
397 ptep = pte_offset_map(pmdp, address);
398 pte = *ptep;
399 if (!pte_present(pte))
400 goto out_unmap;
401
402 paddr = (unsigned long) page_address(pte_page(pte));
403
404 __asm__ __volatile__("flush %0 + %1"
405 : /* no outputs */
406 : "r" (paddr),
407 "r" (address & (PAGE_SIZE - 1))
408 : "memory");
409
410out_unmap:
411 pte_unmap(ptep);
412out_irqs_on:
413 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
414
415}
416
David S. Miller392c2182010-09-21 21:41:12 -0700417static int setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
418 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
David S. Miller5526b7e2008-04-27 02:26:36 -0700420 struct signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700421 int i, err, wsaved;
422 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int sigframe_size;
424 u32 psr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned int seta[_COMPAT_NSIG_WORDS];
426
427 /* 1. Make sure everything is clean */
428 synchronize_user_stack();
429 save_and_clear_fpu();
430
David S. Miller55984732011-08-20 17:14:54 -0700431 wsaved = get_thread_wsaved();
432
433 sigframe_size = sizeof(*sf);
434 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
435 sigframe_size += sizeof(__siginfo_fpu_t);
436 if (wsaved)
437 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David S. Miller5526b7e2008-04-27 02:26:36 -0700439 sf = (struct signal_frame32 __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 get_sigframe(&ka->sa, regs, sigframe_size);
441
442 if (invalid_frame_pointer(sf, sigframe_size))
443 goto sigill;
444
David S. Miller55984732011-08-20 17:14:54 -0700445 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* 2. Save the current process state */
448 if (test_thread_flag(TIF_32BIT)) {
449 regs->tpc &= 0xffffffff;
450 regs->tnpc &= 0xffffffff;
451 }
452 err = put_user(regs->tpc, &sf->info.si_regs.pc);
453 err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
454 err |= __put_user(regs->y, &sf->info.si_regs.y);
455 psr = tstate_to_psr(regs->tstate);
456 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
457 psr |= PSR_EF;
458 err |= __put_user(psr, &sf->info.si_regs.psr);
459 for (i = 0; i < 16; i++)
460 err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
461 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
462 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
463 for (i = 1; i < 16; i++)
464 err |= __put_user(((u32 *)regs->u_regs)[2*i],
465 &sf->v8plus.g_upper[i]);
466 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
467 &sf->v8plus.asi);
468
469 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700470 __siginfo_fpu_t __user *fp = tail;
471 tail += sizeof(*fp);
472 err |= save_fpu_state(regs, fp);
473 err |= __put_user((u64)fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
475 err |= __put_user(0, &sf->fpu_save);
476 }
David S. Miller55984732011-08-20 17:14:54 -0700477 if (wsaved) {
478 __siginfo_rwin_t __user *rwp = tail;
479 tail += sizeof(*rwp);
480 err |= save_rwin_state(wsaved, rwp);
481 err |= __put_user((u64)rwp, &sf->rwin_save);
482 set_thread_wsaved(0);
483 } else {
484 err |= __put_user(0, &sf->rwin_save);
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 switch (_NSIG_WORDS) {
488 case 4: seta[7] = (oldset->sig[3] >> 32);
489 seta[6] = oldset->sig[3];
490 case 3: seta[5] = (oldset->sig[2] >> 32);
491 seta[4] = oldset->sig[2];
492 case 2: seta[3] = (oldset->sig[1] >> 32);
493 seta[2] = oldset->sig[1];
494 case 1: seta[1] = (oldset->sig[0] >> 32);
495 seta[0] = oldset->sig[0];
496 }
497 err |= __put_user(seta[0], &sf->info.si_mask);
498 err |= __copy_to_user(sf->extramask, seta + 1,
499 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
500
David S. Miller55984732011-08-20 17:14:54 -0700501 if (!wsaved) {
502 err |= copy_in_user((u32 __user *)sf,
503 (u32 __user *)(regs->u_regs[UREG_FP]),
504 sizeof(struct reg_window32));
505 } else {
506 struct reg_window *rp;
507
508 rp = &current_thread_info()->reg_window[wsaved - 1];
509 for (i = 0; i < 8; i++)
510 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
511 for (i = 0; i < 6; i++)
512 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
513 err |= __put_user(rp->ins[6], &sf->ss.fp);
514 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (err)
517 goto sigsegv;
518
519 /* 3. signal handler back-trampoline and parameters */
520 regs->u_regs[UREG_FP] = (unsigned long) sf;
521 regs->u_regs[UREG_I0] = signo;
522 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
523 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
524
525 /* 4. signal handler */
526 regs->tpc = (unsigned long) ka->sa.sa_handler;
527 regs->tnpc = (regs->tpc + 4);
528 if (test_thread_flag(TIF_32BIT)) {
529 regs->tpc &= 0xffffffff;
530 regs->tnpc &= 0xffffffff;
531 }
532
533 /* 5. return to kernel instructions */
534 if (ka->ka_restorer) {
535 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
536 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 unsigned long address = ((unsigned long)&(sf->insns[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
540
541 err = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
542 err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
543 if (err)
544 goto sigsegv;
David S. Miller05c5e762010-09-20 23:24:52 -0700545 flush_signal_insns(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
David S. Millerc2785252010-09-21 22:30:13 -0700547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549sigill:
550 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700551 return -EINVAL;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553sigsegv:
554 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700555 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
David S. Miller392c2182010-09-21 21:41:12 -0700558static int setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
559 unsigned long signr, sigset_t *oldset,
560 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct rt_signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700563 int i, err, wsaved;
564 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int sigframe_size;
566 u32 psr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 compat_sigset_t seta;
568
569 /* 1. Make sure everything is clean */
570 synchronize_user_stack();
571 save_and_clear_fpu();
572
David S. Miller55984732011-08-20 17:14:54 -0700573 wsaved = get_thread_wsaved();
574
575 sigframe_size = sizeof(*sf);
576 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
577 sigframe_size += sizeof(__siginfo_fpu_t);
578 if (wsaved)
579 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 sf = (struct rt_signal_frame32 __user *)
582 get_sigframe(&ka->sa, regs, sigframe_size);
583
584 if (invalid_frame_pointer(sf, sigframe_size))
585 goto sigill;
586
David S. Miller55984732011-08-20 17:14:54 -0700587 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* 2. Save the current process state */
590 if (test_thread_flag(TIF_32BIT)) {
591 regs->tpc &= 0xffffffff;
592 regs->tnpc &= 0xffffffff;
593 }
594 err = put_user(regs->tpc, &sf->regs.pc);
595 err |= __put_user(regs->tnpc, &sf->regs.npc);
596 err |= __put_user(regs->y, &sf->regs.y);
597 psr = tstate_to_psr(regs->tstate);
598 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
599 psr |= PSR_EF;
600 err |= __put_user(psr, &sf->regs.psr);
601 for (i = 0; i < 16; i++)
602 err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
603 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
604 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
605 for (i = 1; i < 16; i++)
606 err |= __put_user(((u32 *)regs->u_regs)[2*i],
607 &sf->v8plus.g_upper[i]);
608 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
609 &sf->v8plus.asi);
610
611 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700612 __siginfo_fpu_t __user *fp = tail;
613 tail += sizeof(*fp);
614 err |= save_fpu_state(regs, fp);
615 err |= __put_user((u64)fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 } else {
617 err |= __put_user(0, &sf->fpu_save);
618 }
David S. Miller55984732011-08-20 17:14:54 -0700619 if (wsaved) {
620 __siginfo_rwin_t __user *rwp = tail;
621 tail += sizeof(*rwp);
622 err |= save_rwin_state(wsaved, rwp);
623 err |= __put_user((u64)rwp, &sf->rwin_save);
624 set_thread_wsaved(0);
625 } else {
626 err |= __put_user(0, &sf->rwin_save);
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* Update the siginfo structure. */
630 err |= copy_siginfo_to_user32(&sf->info, info);
631
632 /* Setup sigaltstack */
Al Viro99b06fe2012-12-23 03:41:17 -0500633 err |= __compat_save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 switch (_NSIG_WORDS) {
636 case 4: seta.sig[7] = (oldset->sig[3] >> 32);
637 seta.sig[6] = oldset->sig[3];
638 case 3: seta.sig[5] = (oldset->sig[2] >> 32);
639 seta.sig[4] = oldset->sig[2];
640 case 2: seta.sig[3] = (oldset->sig[1] >> 32);
641 seta.sig[2] = oldset->sig[1];
642 case 1: seta.sig[1] = (oldset->sig[0] >> 32);
643 seta.sig[0] = oldset->sig[0];
644 }
645 err |= __copy_to_user(&sf->mask, &seta, sizeof(compat_sigset_t));
646
David S. Miller55984732011-08-20 17:14:54 -0700647 if (!wsaved) {
648 err |= copy_in_user((u32 __user *)sf,
649 (u32 __user *)(regs->u_regs[UREG_FP]),
650 sizeof(struct reg_window32));
651 } else {
652 struct reg_window *rp;
653
654 rp = &current_thread_info()->reg_window[wsaved - 1];
655 for (i = 0; i < 8; i++)
656 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
657 for (i = 0; i < 6; i++)
658 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
659 err |= __put_user(rp->ins[6], &sf->ss.fp);
660 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (err)
663 goto sigsegv;
664
665 /* 3. signal handler back-trampoline and parameters */
666 regs->u_regs[UREG_FP] = (unsigned long) sf;
667 regs->u_regs[UREG_I0] = signr;
668 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
669 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
670
671 /* 4. signal handler */
672 regs->tpc = (unsigned long) ka->sa.sa_handler;
673 regs->tnpc = (regs->tpc + 4);
674 if (test_thread_flag(TIF_32BIT)) {
675 regs->tpc &= 0xffffffff;
676 regs->tnpc &= 0xffffffff;
677 }
678
679 /* 5. return to kernel instructions */
680 if (ka->ka_restorer)
681 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
682 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned long address = ((unsigned long)&(sf->insns[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
686
687 /* mov __NR_rt_sigreturn, %g1 */
688 err |= __put_user(0x82102065, &sf->insns[0]);
689
690 /* t 0x10 */
691 err |= __put_user(0x91d02010, &sf->insns[1]);
692 if (err)
693 goto sigsegv;
694
David S. Miller05c5e762010-09-20 23:24:52 -0700695 flush_signal_insns(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
David S. Miller392c2182010-09-21 21:41:12 -0700697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699sigill:
700 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700701 return -EINVAL;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703sigsegv:
704 force_sigsegv(signr, current);
David S. Miller392c2182010-09-21 21:41:12 -0700705 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Al Viroa610d6e2012-05-21 23:42:15 -0400708static inline void handle_signal32(unsigned long signr, struct k_sigaction *ka,
David S. Miller392c2182010-09-21 21:41:12 -0700709 siginfo_t *info,
710 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
David S. Miller392c2182010-09-21 21:41:12 -0700712 int err;
713
David S. Millerec98c6b2008-04-20 02:14:23 -0700714 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700715 err = setup_rt_frame32(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700716 else
David S. Miller392c2182010-09-21 21:41:12 -0700717 err = setup_frame32(ka, regs, signr, oldset);
718
719 if (err)
Al Viroa610d6e2012-05-21 23:42:15 -0400720 return;
David S. Miller5526b7e2008-04-27 02:26:36 -0700721
Al Viroefee9842012-04-28 02:04:15 -0400722 signal_delivered(signr, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
726 struct sigaction *sa)
727{
728 switch (regs->u_regs[UREG_I0]) {
729 case ERESTART_RESTARTBLOCK:
730 case ERESTARTNOHAND:
731 no_system_call_restart:
732 regs->u_regs[UREG_I0] = EINTR;
733 regs->tstate |= TSTATE_ICARRY;
734 break;
735 case ERESTARTSYS:
736 if (!(sa->sa_flags & SA_RESTART))
737 goto no_system_call_restart;
738 /* fallthrough */
739 case ERESTARTNOINTR:
740 regs->u_regs[UREG_I0] = orig_i0;
741 regs->tpc -= 4;
742 regs->tnpc -= 4;
743 }
744}
745
746/* Note that 'init' is a special process: it doesn't get signals it doesn't
747 * want to handle. Thus you cannot kill init even with a SIGKILL even by
748 * mistake.
749 */
David S. Miller1d299bc2011-11-14 20:32:16 -0800750void do_signal32(sigset_t *oldset, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct k_sigaction ka;
David S. Miller1d299bc2011-11-14 20:32:16 -0800753 unsigned long orig_i0;
754 int restart_syscall;
David S. Miller238468b2008-04-24 03:01:48 -0700755 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
David S. Miller28e61032008-05-11 02:07:19 -0700758 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
759
David S. Miller1d299bc2011-11-14 20:32:16 -0800760 restart_syscall = 0;
761 orig_i0 = 0;
762 if (pt_regs_is_syscall(regs) &&
763 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
764 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800765 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800766 }
David S. Miller28e61032008-05-11 02:07:19 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700769 if (restart_syscall)
770 syscall_restart32(orig_i0, regs, &ka.sa);
Al Viroa610d6e2012-05-21 23:42:15 -0400771 handle_signal32(signr, &ka, &info, oldset, regs);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800772 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
David S. Miller28e61032008-05-11 02:07:19 -0700774 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
776 regs->u_regs[UREG_I0] == ERESTARTSYS ||
777 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
778 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700779 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 regs->tpc -= 4;
781 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700782 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
David S. Miller28e61032008-05-11 02:07:19 -0700784 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
786 regs->u_regs[UREG_G1] = __NR_restart_syscall;
787 regs->tpc -= 4;
788 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700789 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800791
David S. Miller9a28dbf2008-05-12 22:45:15 -0700792 /* If there's no signal to deliver, we just put the saved sigmask
David S. Miller2d7d5f02006-01-19 02:42:49 -0800793 * back
794 */
Al Viro51a7b442012-05-21 23:33:55 -0400795 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798struct sigstack32 {
799 u32 the_stack;
800 int cur_status;
801};
802
803asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
804{
805 struct sigstack32 __user *ssptr =
806 (struct sigstack32 __user *)((unsigned long)(u_ssptr));
807 struct sigstack32 __user *ossptr =
808 (struct sigstack32 __user *)((unsigned long)(u_ossptr));
809 int ret = -EFAULT;
810
811 /* First see if old state is wanted. */
812 if (ossptr) {
813 if (put_user(current->sas_ss_sp + current->sas_ss_size,
814 &ossptr->the_stack) ||
815 __put_user(on_sig_stack(sp), &ossptr->cur_status))
816 goto out;
817 }
818
819 /* Now see if we want to update the new state. */
820 if (ssptr) {
821 u32 ss_sp;
822
823 if (get_user(ss_sp, &ssptr->the_stack))
824 goto out;
825
826 /* If the current stack was set with sigaltstack, don't
827 * swap stacks while we are on it.
828 */
829 ret = -EPERM;
830 if (current->sas_ss_sp && on_sig_stack(sp))
831 goto out;
832
833 /* Since we don't know the extent of the stack, and we don't
834 * track onstack-ness, but rather calculate it, we must
835 * presume a size. Ho hum this interface is lossy.
836 */
837 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
838 current->sas_ss_size = SIGSTKSZ;
839 }
840
841 ret = 0;
842out:
843 return ret;
844}