1 /*
2  * Copyright (C) 2003 Broadcom Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 #include <linux/cache.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/kernel.h>
23 #include <linux/signal.h>
24 #include <linux/errno.h>
25 #include <linux/wait.h>
26 #include <linux/ptrace.h>
27 #include <linux/unistd.h>
28 #include <linux/compat.h>
29 #include <linux/bitops.h>
30 
31 #include <asm/abi.h>
32 #include <asm/asm.h>
33 #include <asm/cacheflush.h>
34 #include <asm/compat-signal.h>
35 #include <asm/sim.h>
36 #include <asm/uaccess.h>
37 #include <asm/ucontext.h>
38 #include <asm/system.h>
39 #include <asm/fpu.h>
40 #include <asm/cpu-features.h>
41 #include <asm/war.h>
42 #include <asm/vdso.h>
43 
44 #include "signal-common.h"
45 
46 /*
47  * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
48  */
49 #define __NR_N32_restart_syscall	6214
50 
51 extern int setup_sigcontext(struct pt_regs *, struct sigcontext __user *);
52 extern int restore_sigcontext(struct pt_regs *, struct sigcontext __user *);
53 
54 
55 /* IRIX compatible stack_t  */
56 typedef struct sigaltstack32 {
57 	s32 ss_sp;
58 	compat_size_t ss_size;
59 	int ss_flags;
60 } stack32_t;
61 
62 struct ucontextn32 {
63 	u32                 uc_flags;
64 	s32                 uc_link;
65 	stack32_t           uc_stack;
66 	struct sigcontext   uc_mcontext;
67 	compat_sigset_t     uc_sigmask;   /* mask last for extensibility */
68 };
69 
70 struct rt_sigframe_n32 {
71 	u32 rs_ass[4];			/* argument save space for o32 */
72 	u32 rs_pad[2];			/* Was: signal trampoline */
73 	struct compat_siginfo rs_info;
74 	struct ucontextn32 rs_uc;
75 };
76 
77 extern void sigset_from_compat(sigset_t *set, compat_sigset_t *compat);
78 
sysn32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)79 asmlinkage int sysn32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
80 {
81 	compat_sigset_t __user *unewset;
82 	compat_sigset_t uset;
83 	size_t sigsetsize;
84 	sigset_t newset;
85 
86 	/* XXX Don't preclude handling different sized sigset_t's.  */
87 	sigsetsize = regs.regs[5];
88 	if (sigsetsize != sizeof(sigset_t))
89 		return -EINVAL;
90 
91 	unewset = (compat_sigset_t __user *) regs.regs[4];
92 	if (copy_from_user(&uset, unewset, sizeof(uset)))
93 		return -EFAULT;
94 	sigset_from_compat(&newset, &uset);
95 	sigdelsetmask(&newset, ~_BLOCKABLE);
96 
97 	spin_lock_irq(&current->sighand->siglock);
98 	current->saved_sigmask = current->blocked;
99 	current->blocked = newset;
100 	recalc_sigpending();
101 	spin_unlock_irq(&current->sighand->siglock);
102 
103 	current->state = TASK_INTERRUPTIBLE;
104 	schedule();
105 	set_thread_flag(TIF_RESTORE_SIGMASK);
106 	return -ERESTARTNOHAND;
107 }
108 
sysn32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)109 asmlinkage void sysn32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
110 {
111 	struct rt_sigframe_n32 __user *frame;
112 	mm_segment_t old_fs;
113 	sigset_t set;
114 	stack_t st;
115 	s32 sp;
116 	int sig;
117 
118 	frame = (struct rt_sigframe_n32 __user *) regs.regs[29];
119 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
120 		goto badframe;
121 	if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
122 		goto badframe;
123 
124 	sigdelsetmask(&set, ~_BLOCKABLE);
125 	spin_lock_irq(&current->sighand->siglock);
126 	current->blocked = set;
127 	recalc_sigpending();
128 	spin_unlock_irq(&current->sighand->siglock);
129 
130 	sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
131 	if (sig < 0)
132 		goto badframe;
133 	else if (sig)
134 		force_sig(sig, current);
135 
136 	/* The ucontext contains a stack32_t, so we must convert!  */
137 	if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
138 		goto badframe;
139 	st.ss_sp = (void __user *)(long) sp;
140 	if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
141 		goto badframe;
142 	if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
143 		goto badframe;
144 
145 	/* It is more difficult to avoid calling this function than to
146 	   call it and ignore errors.  */
147 	old_fs = get_fs();
148 	set_fs(KERNEL_DS);
149 	do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
150 	set_fs(old_fs);
151 
152 
153 	/*
154 	 * Don't let your children do this ...
155 	 */
156 	__asm__ __volatile__(
157 		"move\t$29, %0\n\t"
158 		"j\tsyscall_exit"
159 		:/* no outputs */
160 		:"r" (&regs));
161 	/* Unreached */
162 
163 badframe:
164 	force_sig(SIGSEGV, current);
165 }
166 
setup_rt_frame_n32(void * sig_return,struct k_sigaction * ka,struct pt_regs * regs,int signr,sigset_t * set,siginfo_t * info)167 static int setup_rt_frame_n32(void *sig_return, struct k_sigaction *ka,
168 	struct pt_regs *regs, int signr, sigset_t *set, siginfo_t *info)
169 {
170 	struct rt_sigframe_n32 __user *frame;
171 	int err = 0;
172 	s32 sp;
173 
174 	frame = get_sigframe(ka, regs, sizeof(*frame));
175 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
176 		goto give_sigsegv;
177 
178 	/* Create siginfo.  */
179 	err |= copy_siginfo_to_user32(&frame->rs_info, info);
180 
181 	/* Create the ucontext.  */
182 	err |= __put_user(0, &frame->rs_uc.uc_flags);
183 	err |= __put_user(0, &frame->rs_uc.uc_link);
184 	sp = (int) (long) current->sas_ss_sp;
185 	err |= __put_user(sp,
186 	                  &frame->rs_uc.uc_stack.ss_sp);
187 	err |= __put_user(sas_ss_flags(regs->regs[29]),
188 	                  &frame->rs_uc.uc_stack.ss_flags);
189 	err |= __put_user(current->sas_ss_size,
190 	                  &frame->rs_uc.uc_stack.ss_size);
191 	err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
192 	err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
193 
194 	if (err)
195 		goto give_sigsegv;
196 
197 	/*
198 	 * Arguments to signal handler:
199 	 *
200 	 *   a0 = signal number
201 	 *   a1 = 0 (should be cause)
202 	 *   a2 = pointer to ucontext
203 	 *
204 	 * $25 and c0_epc point to the signal handler, $29 points to
205 	 * the struct rt_sigframe.
206 	 */
207 	regs->regs[ 4] = signr;
208 	regs->regs[ 5] = (unsigned long) &frame->rs_info;
209 	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
210 	regs->regs[29] = (unsigned long) frame;
211 	regs->regs[31] = (unsigned long) sig_return;
212 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
213 
214 	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
215 	       current->comm, current->pid,
216 	       frame, regs->cp0_epc, regs->regs[31]);
217 
218 	return 0;
219 
220 give_sigsegv:
221 	force_sigsegv(signr, current);
222 	return -EFAULT;
223 }
224 
225 struct mips_abi mips_abi_n32 = {
226 	.setup_rt_frame	= setup_rt_frame_n32,
227 	.rt_signal_return_offset =
228 		offsetof(struct mips_vdso, n32_rt_signal_trampoline),
229 	.restart	= __NR_N32_restart_syscall
230 };
231