Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *    Copyright IBM Corp. 2000, 2006
  4 *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  5 *               Gerhard Tonn (ton@de.ibm.com)                  
  6 *
  7 *  Copyright (C) 1991, 1992  Linus Torvalds
  8 *
  9 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
 10 */
 11
 12#include <linux/compat.h>
 13#include <linux/sched.h>
 14#include <linux/sched/task_stack.h>
 15#include <linux/mm.h>
 16#include <linux/smp.h>
 17#include <linux/kernel.h>
 18#include <linux/signal.h>
 19#include <linux/errno.h>
 20#include <linux/wait.h>
 21#include <linux/ptrace.h>
 22#include <linux/unistd.h>
 23#include <linux/stddef.h>
 24#include <linux/tty.h>
 25#include <linux/personality.h>
 26#include <linux/binfmts.h>
 27#include <asm/ucontext.h>
 28#include <linux/uaccess.h>
 29#include <asm/lowcore.h>
 30#include <asm/switch_to.h>
 
 31#include "compat_linux.h"
 32#include "compat_ptrace.h"
 33#include "entry.h"
 34
 35typedef struct 
 36{
 37	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
 38	struct sigcontext32 sc;
 39	_sigregs32 sregs;
 40	int signo;
 41	_sigregs_ext32 sregs_ext;
 42	__u16 svc_insn;		/* Offset of svc_insn is NOT fixed! */
 43} sigframe32;
 44
 45typedef struct 
 46{
 47	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
 48	__u16 svc_insn;
 49	compat_siginfo_t info;
 50	struct ucontext32 uc;
 51} rt_sigframe32;
 52
 53/* Store registers needed to create the signal frame */
 54static void store_sigregs(void)
 55{
 56	save_access_regs(current->thread.acrs);
 57	save_fpu_regs();
 58}
 59
 60/* Load registers after signal return */
 61static void load_sigregs(void)
 62{
 63	restore_access_regs(current->thread.acrs);
 64}
 65
 66static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
 67{
 68	_sigregs32 user_sregs;
 69	int i;
 70
 71	user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
 72	user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
 73	user_sregs.regs.psw.mask |= PSW32_USER_BITS;
 74	user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
 75		(__u32)(regs->psw.mask & PSW_MASK_BA);
 76	for (i = 0; i < NUM_GPRS; i++)
 77		user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
 78	memcpy(&user_sregs.regs.acrs, current->thread.acrs,
 79	       sizeof(user_sregs.regs.acrs));
 80	fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
 81	if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
 82		return -EFAULT;
 83	return 0;
 84}
 85
 86static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
 87{
 88	_sigregs32 user_sregs;
 89	int i;
 90
 91	/* Alwys make any pending restarted system call return -EINTR */
 92	current->restart_block.fn = do_no_restart_syscall;
 93
 94	if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
 95		return -EFAULT;
 96
 97	if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
 98		return -EINVAL;
 99
100	/* Test the floating-point-control word. */
101	if (test_fp_ctl(user_sregs.fpregs.fpc))
102		return -EINVAL;
103
104	/* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
105	regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
106		(__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
107		(__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
108		(__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
109	/* Check for invalid user address space control. */
110	if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
111		regs->psw.mask = PSW_ASC_PRIMARY |
112			(regs->psw.mask & ~PSW_MASK_ASC);
113	regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
114	for (i = 0; i < NUM_GPRS; i++)
115		regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
116	memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
117	       sizeof(current->thread.acrs));
118	fpregs_load((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
119
120	clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
121	return 0;
122}
123
124static int save_sigregs_ext32(struct pt_regs *regs,
125			      _sigregs_ext32 __user *sregs_ext)
126{
127	__u32 gprs_high[NUM_GPRS];
128	__u64 vxrs[__NUM_VXRS_LOW];
129	int i;
130
131	/* Save high gprs to signal stack */
132	for (i = 0; i < NUM_GPRS; i++)
133		gprs_high[i] = regs->gprs[i] >> 32;
134	if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
135			   sizeof(sregs_ext->gprs_high)))
136		return -EFAULT;
137
138	/* Save vector registers to signal stack */
139	if (MACHINE_HAS_VX) {
140		for (i = 0; i < __NUM_VXRS_LOW; i++)
141			vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
142		if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
143				   sizeof(sregs_ext->vxrs_low)) ||
144		    __copy_to_user(&sregs_ext->vxrs_high,
145				   current->thread.fpu.vxrs + __NUM_VXRS_LOW,
146				   sizeof(sregs_ext->vxrs_high)))
147			return -EFAULT;
148	}
149	return 0;
150}
151
152static int restore_sigregs_ext32(struct pt_regs *regs,
153				 _sigregs_ext32 __user *sregs_ext)
154{
155	__u32 gprs_high[NUM_GPRS];
156	__u64 vxrs[__NUM_VXRS_LOW];
157	int i;
158
159	/* Restore high gprs from signal stack */
160	if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
161			     sizeof(sregs_ext->gprs_high)))
162		return -EFAULT;
163	for (i = 0; i < NUM_GPRS; i++)
164		*(__u32 *)&regs->gprs[i] = gprs_high[i];
165
166	/* Restore vector registers from signal stack */
167	if (MACHINE_HAS_VX) {
168		if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
169				     sizeof(sregs_ext->vxrs_low)) ||
170		    __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
171				     &sregs_ext->vxrs_high,
172				     sizeof(sregs_ext->vxrs_high)))
173			return -EFAULT;
174		for (i = 0; i < __NUM_VXRS_LOW; i++)
175			*((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
176	}
177	return 0;
178}
179
180COMPAT_SYSCALL_DEFINE0(sigreturn)
181{
182	struct pt_regs *regs = task_pt_regs(current);
183	sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
184	sigset_t set;
185
186	if (get_compat_sigset(&set, (compat_sigset_t __user *)frame->sc.oldmask))
187		goto badframe;
188	set_current_blocked(&set);
189	save_fpu_regs();
190	if (restore_sigregs32(regs, &frame->sregs))
191		goto badframe;
192	if (restore_sigregs_ext32(regs, &frame->sregs_ext))
193		goto badframe;
194	load_sigregs();
195	return regs->gprs[2];
196badframe:
197	force_sig(SIGSEGV);
198	return 0;
199}
200
201COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
202{
203	struct pt_regs *regs = task_pt_regs(current);
204	rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
205	sigset_t set;
206
207	if (get_compat_sigset(&set, &frame->uc.uc_sigmask))
208		goto badframe;
209	set_current_blocked(&set);
210	if (compat_restore_altstack(&frame->uc.uc_stack))
211		goto badframe;
212	save_fpu_regs();
213	if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
214		goto badframe;
215	if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
216		goto badframe;
217	load_sigregs();
218	return regs->gprs[2];
219badframe:
220	force_sig(SIGSEGV);
221	return 0;
222}	
223
224/*
225 * Set up a signal frame.
226 */
227
228
229/*
230 * Determine which stack to use..
231 */
232static inline void __user *
233get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
234{
235	unsigned long sp;
236
237	/* Default to using normal stack */
238	sp = (unsigned long) A(regs->gprs[15]);
239
240	/* Overflow on alternate signal stack gives SIGSEGV. */
241	if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
242		return (void __user *) -1UL;
243
244	/* This is the X/Open sanctioned signal stack switching.  */
245	if (ka->sa.sa_flags & SA_ONSTACK) {
246		if (! sas_ss_flags(sp))
247			sp = current->sas_ss_sp + current->sas_ss_size;
248	}
249
250	return (void __user *)((sp - frame_size) & -8ul);
251}
252
253static int setup_frame32(struct ksignal *ksig, sigset_t *set,
254			 struct pt_regs *regs)
255{
256	int sig = ksig->sig;
257	sigframe32 __user *frame;
258	unsigned long restorer;
259	size_t frame_size;
260
261	/*
262	 * gprs_high are always present for 31-bit compat tasks.
263	 * The space for vector registers is only allocated if
264	 * the machine supports it
265	 */
266	frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
267	if (!MACHINE_HAS_VX)
268		frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
269			      sizeof(frame->sregs_ext.vxrs_high);
270	frame = get_sigframe(&ksig->ka, regs, frame_size);
271	if (frame == (void __user *) -1UL)
272		return -EFAULT;
273
274	/* Set up backchain. */
275	if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
276		return -EFAULT;
277
278	/* Create struct sigcontext32 on the signal stack */
279	if (put_compat_sigset((compat_sigset_t __user *)frame->sc.oldmask,
280			      set, sizeof(compat_sigset_t)))
281		return -EFAULT;
282	if (__put_user(ptr_to_compat(&frame->sregs), &frame->sc.sregs))
283		return -EFAULT;
284
285	/* Store registers needed to create the signal frame */
286	store_sigregs();
287
288	/* Create _sigregs32 on the signal stack */
289	if (save_sigregs32(regs, &frame->sregs))
290		return -EFAULT;
291
292	/* Place signal number on stack to allow backtrace from handler.  */
293	if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
294		return -EFAULT;
295
296	/* Create _sigregs_ext32 on the signal stack */
297	if (save_sigregs_ext32(regs, &frame->sregs_ext))
298		return -EFAULT;
299
300	/* Set up to return from userspace.  If provided, use a stub
301	   already in userspace.  */
302	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
303		restorer = (unsigned long __force)
304			ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
305	} else {
306		/* Signal frames without vectors registers are short ! */
307		__u16 __user *svc = (void __user *) frame + frame_size - 2;
308		if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
309			return -EFAULT;
310		restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
311        }
312
313	/* Set up registers for signal handler */
314	regs->gprs[14] = restorer;
315	regs->gprs[15] = (__force __u64) frame;
316	/* Force 31 bit amode and default user address space control. */
317	regs->psw.mask = PSW_MASK_BA |
318		(PSW_USER_BITS & PSW_MASK_ASC) |
319		(regs->psw.mask & ~PSW_MASK_ASC);
320	regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
321
322	regs->gprs[2] = sig;
323	regs->gprs[3] = (__force __u64) &frame->sc;
324
325	/* We forgot to include these in the sigcontext.
326	   To avoid breaking binary compatibility, they are passed as args. */
327	if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
328	    sig == SIGTRAP || sig == SIGFPE) {
329		/* set extra registers only for synchronous signals */
330		regs->gprs[4] = regs->int_code & 127;
331		regs->gprs[5] = regs->int_parm_long;
332		regs->gprs[6] = current->thread.last_break;
333	}
334
335	return 0;
336}
337
338static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
339			    struct pt_regs *regs)
340{
341	rt_sigframe32 __user *frame;
342	unsigned long restorer;
343	size_t frame_size;
344	u32 uc_flags;
345
346	frame_size = sizeof(*frame) -
347		     sizeof(frame->uc.uc_mcontext_ext.__reserved);
348	/*
349	 * gprs_high are always present for 31-bit compat tasks.
350	 * The space for vector registers is only allocated if
351	 * the machine supports it
352	 */
353	uc_flags = UC_GPRS_HIGH;
354	if (MACHINE_HAS_VX) {
355		uc_flags |= UC_VXRS;
356	} else
357		frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
358			      sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
359	frame = get_sigframe(&ksig->ka, regs, frame_size);
360	if (frame == (void __user *) -1UL)
361		return -EFAULT;
362
363	/* Set up backchain. */
364	if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
365		return -EFAULT;
366
367	/* Set up to return from userspace.  If provided, use a stub
368	   already in userspace.  */
369	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
370		restorer = (unsigned long __force)
371			ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
372	} else {
373		__u16 __user *svc = &frame->svc_insn;
374		if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
375			return -EFAULT;
376		restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
377	}
378
379	/* Create siginfo on the signal stack */
380	if (copy_siginfo_to_user32(&frame->info, &ksig->info))
381		return -EFAULT;
382
383	/* Store registers needed to create the signal frame */
384	store_sigregs();
385
386	/* Create ucontext on the signal stack. */
387	if (__put_user(uc_flags, &frame->uc.uc_flags) ||
388	    __put_user(0, &frame->uc.uc_link) ||
389	    __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
390	    save_sigregs32(regs, &frame->uc.uc_mcontext) ||
391	    put_compat_sigset(&frame->uc.uc_sigmask, set, sizeof(compat_sigset_t)) ||
392	    save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
393		return -EFAULT;
394
395	/* Set up registers for signal handler */
396	regs->gprs[14] = restorer;
397	regs->gprs[15] = (__force __u64) frame;
398	/* Force 31 bit amode and default user address space control. */
399	regs->psw.mask = PSW_MASK_BA |
400		(PSW_USER_BITS & PSW_MASK_ASC) |
401		(regs->psw.mask & ~PSW_MASK_ASC);
402	regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
403
404	regs->gprs[2] = ksig->sig;
405	regs->gprs[3] = (__force __u64) &frame->info;
406	regs->gprs[4] = (__force __u64) &frame->uc;
407	regs->gprs[5] = current->thread.last_break;
408	return 0;
409}
410
411/*
412 * OK, we're invoking a handler
413 */	
414
415void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
416		     struct pt_regs *regs)
417{
418	int ret;
419
420	/* Set up the stack frame */
421	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
422		ret = setup_rt_frame32(ksig, oldset, regs);
423	else
424		ret = setup_frame32(ksig, oldset, regs);
425
426	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
427}
428
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *    Copyright IBM Corp. 2000, 2006
  4 *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  5 *               Gerhard Tonn (ton@de.ibm.com)                  
  6 *
  7 *  Copyright (C) 1991, 1992  Linus Torvalds
  8 *
  9 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
 10 */
 11
 12#include <linux/compat.h>
 13#include <linux/sched.h>
 14#include <linux/sched/task_stack.h>
 15#include <linux/mm.h>
 16#include <linux/smp.h>
 17#include <linux/kernel.h>
 18#include <linux/signal.h>
 19#include <linux/errno.h>
 20#include <linux/wait.h>
 21#include <linux/ptrace.h>
 22#include <linux/unistd.h>
 23#include <linux/stddef.h>
 24#include <linux/tty.h>
 25#include <linux/personality.h>
 26#include <linux/binfmts.h>
 27#include <asm/ucontext.h>
 28#include <linux/uaccess.h>
 29#include <asm/lowcore.h>
 30#include <asm/switch_to.h>
 31#include <asm/vdso.h>
 32#include "compat_linux.h"
 33#include "compat_ptrace.h"
 34#include "entry.h"
 35
 36typedef struct 
 37{
 38	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
 39	struct sigcontext32 sc;
 40	_sigregs32 sregs;
 41	int signo;
 42	_sigregs_ext32 sregs_ext;
 43	__u16 svc_insn;		/* Offset of svc_insn is NOT fixed! */
 44} sigframe32;
 45
 46typedef struct 
 47{
 48	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
 49	__u16 svc_insn;
 50	compat_siginfo_t info;
 51	struct ucontext32 uc;
 52} rt_sigframe32;
 53
 54/* Store registers needed to create the signal frame */
 55static void store_sigregs(void)
 56{
 57	save_access_regs(current->thread.acrs);
 58	save_fpu_regs();
 59}
 60
 61/* Load registers after signal return */
 62static void load_sigregs(void)
 63{
 64	restore_access_regs(current->thread.acrs);
 65}
 66
 67static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
 68{
 69	_sigregs32 user_sregs;
 70	int i;
 71
 72	user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
 73	user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
 74	user_sregs.regs.psw.mask |= PSW32_USER_BITS;
 75	user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
 76		(__u32)(regs->psw.mask & PSW_MASK_BA);
 77	for (i = 0; i < NUM_GPRS; i++)
 78		user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
 79	memcpy(&user_sregs.regs.acrs, current->thread.acrs,
 80	       sizeof(user_sregs.regs.acrs));
 81	fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
 82	if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
 83		return -EFAULT;
 84	return 0;
 85}
 86
 87static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
 88{
 89	_sigregs32 user_sregs;
 90	int i;
 91
 92	/* Always make any pending restarted system call return -EINTR */
 93	current->restart_block.fn = do_no_restart_syscall;
 94
 95	if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
 96		return -EFAULT;
 97
 98	if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
 99		return -EINVAL;
100
101	/* Test the floating-point-control word. */
102	if (test_fp_ctl(user_sregs.fpregs.fpc))
103		return -EINVAL;
104
105	/* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
106	regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
107		(__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
108		(__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
109		(__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
110	/* Check for invalid user address space control. */
111	if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
112		regs->psw.mask = PSW_ASC_PRIMARY |
113			(regs->psw.mask & ~PSW_MASK_ASC);
114	regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
115	for (i = 0; i < NUM_GPRS; i++)
116		regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
117	memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
118	       sizeof(current->thread.acrs));
119	fpregs_load((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
120
121	clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
122	return 0;
123}
124
125static int save_sigregs_ext32(struct pt_regs *regs,
126			      _sigregs_ext32 __user *sregs_ext)
127{
128	__u32 gprs_high[NUM_GPRS];
129	__u64 vxrs[__NUM_VXRS_LOW];
130	int i;
131
132	/* Save high gprs to signal stack */
133	for (i = 0; i < NUM_GPRS; i++)
134		gprs_high[i] = regs->gprs[i] >> 32;
135	if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
136			   sizeof(sregs_ext->gprs_high)))
137		return -EFAULT;
138
139	/* Save vector registers to signal stack */
140	if (MACHINE_HAS_VX) {
141		for (i = 0; i < __NUM_VXRS_LOW; i++)
142			vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
143		if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
144				   sizeof(sregs_ext->vxrs_low)) ||
145		    __copy_to_user(&sregs_ext->vxrs_high,
146				   current->thread.fpu.vxrs + __NUM_VXRS_LOW,
147				   sizeof(sregs_ext->vxrs_high)))
148			return -EFAULT;
149	}
150	return 0;
151}
152
153static int restore_sigregs_ext32(struct pt_regs *regs,
154				 _sigregs_ext32 __user *sregs_ext)
155{
156	__u32 gprs_high[NUM_GPRS];
157	__u64 vxrs[__NUM_VXRS_LOW];
158	int i;
159
160	/* Restore high gprs from signal stack */
161	if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
162			     sizeof(sregs_ext->gprs_high)))
163		return -EFAULT;
164	for (i = 0; i < NUM_GPRS; i++)
165		*(__u32 *)&regs->gprs[i] = gprs_high[i];
166
167	/* Restore vector registers from signal stack */
168	if (MACHINE_HAS_VX) {
169		if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
170				     sizeof(sregs_ext->vxrs_low)) ||
171		    __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
172				     &sregs_ext->vxrs_high,
173				     sizeof(sregs_ext->vxrs_high)))
174			return -EFAULT;
175		for (i = 0; i < __NUM_VXRS_LOW; i++)
176			*((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
177	}
178	return 0;
179}
180
181COMPAT_SYSCALL_DEFINE0(sigreturn)
182{
183	struct pt_regs *regs = task_pt_regs(current);
184	sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
185	sigset_t set;
186
187	if (get_compat_sigset(&set, (compat_sigset_t __user *)frame->sc.oldmask))
188		goto badframe;
189	set_current_blocked(&set);
190	save_fpu_regs();
191	if (restore_sigregs32(regs, &frame->sregs))
192		goto badframe;
193	if (restore_sigregs_ext32(regs, &frame->sregs_ext))
194		goto badframe;
195	load_sigregs();
196	return regs->gprs[2];
197badframe:
198	force_sig(SIGSEGV);
199	return 0;
200}
201
202COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
203{
204	struct pt_regs *regs = task_pt_regs(current);
205	rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
206	sigset_t set;
207
208	if (get_compat_sigset(&set, &frame->uc.uc_sigmask))
209		goto badframe;
210	set_current_blocked(&set);
211	if (compat_restore_altstack(&frame->uc.uc_stack))
212		goto badframe;
213	save_fpu_regs();
214	if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
215		goto badframe;
216	if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
217		goto badframe;
218	load_sigregs();
219	return regs->gprs[2];
220badframe:
221	force_sig(SIGSEGV);
222	return 0;
223}	
224
225/*
226 * Set up a signal frame.
227 */
228
229
230/*
231 * Determine which stack to use..
232 */
233static inline void __user *
234get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
235{
236	unsigned long sp;
237
238	/* Default to using normal stack */
239	sp = (unsigned long) A(regs->gprs[15]);
240
241	/* Overflow on alternate signal stack gives SIGSEGV. */
242	if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
243		return (void __user *) -1UL;
244
245	/* This is the X/Open sanctioned signal stack switching.  */
246	if (ka->sa.sa_flags & SA_ONSTACK) {
247		if (! sas_ss_flags(sp))
248			sp = current->sas_ss_sp + current->sas_ss_size;
249	}
250
251	return (void __user *)((sp - frame_size) & -8ul);
252}
253
254static int setup_frame32(struct ksignal *ksig, sigset_t *set,
255			 struct pt_regs *regs)
256{
257	int sig = ksig->sig;
258	sigframe32 __user *frame;
259	unsigned long restorer;
260	size_t frame_size;
261
262	/*
263	 * gprs_high are always present for 31-bit compat tasks.
264	 * The space for vector registers is only allocated if
265	 * the machine supports it
266	 */
267	frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
268	if (!MACHINE_HAS_VX)
269		frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
270			      sizeof(frame->sregs_ext.vxrs_high);
271	frame = get_sigframe(&ksig->ka, regs, frame_size);
272	if (frame == (void __user *) -1UL)
273		return -EFAULT;
274
275	/* Set up backchain. */
276	if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
277		return -EFAULT;
278
279	/* Create struct sigcontext32 on the signal stack */
280	if (put_compat_sigset((compat_sigset_t __user *)frame->sc.oldmask,
281			      set, sizeof(compat_sigset_t)))
282		return -EFAULT;
283	if (__put_user(ptr_to_compat(&frame->sregs), &frame->sc.sregs))
284		return -EFAULT;
285
286	/* Store registers needed to create the signal frame */
287	store_sigregs();
288
289	/* Create _sigregs32 on the signal stack */
290	if (save_sigregs32(regs, &frame->sregs))
291		return -EFAULT;
292
293	/* Place signal number on stack to allow backtrace from handler.  */
294	if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
295		return -EFAULT;
296
297	/* Create _sigregs_ext32 on the signal stack */
298	if (save_sigregs_ext32(regs, &frame->sregs_ext))
299		return -EFAULT;
300
301	/* Set up to return from userspace.  If provided, use a stub
302	   already in userspace.  */
303	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
304		restorer = (unsigned long __force)
305			ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
306	} else {
307		restorer = VDSO32_SYMBOL(current, sigreturn);
 
 
 
 
308        }
309
310	/* Set up registers for signal handler */
311	regs->gprs[14] = restorer;
312	regs->gprs[15] = (__force __u64) frame;
313	/* Force 31 bit amode and default user address space control. */
314	regs->psw.mask = PSW_MASK_BA |
315		(PSW_USER_BITS & PSW_MASK_ASC) |
316		(regs->psw.mask & ~PSW_MASK_ASC);
317	regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
318
319	regs->gprs[2] = sig;
320	regs->gprs[3] = (__force __u64) &frame->sc;
321
322	/* We forgot to include these in the sigcontext.
323	   To avoid breaking binary compatibility, they are passed as args. */
324	if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
325	    sig == SIGTRAP || sig == SIGFPE) {
326		/* set extra registers only for synchronous signals */
327		regs->gprs[4] = regs->int_code & 127;
328		regs->gprs[5] = regs->int_parm_long;
329		regs->gprs[6] = current->thread.last_break;
330	}
331
332	return 0;
333}
334
335static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
336			    struct pt_regs *regs)
337{
338	rt_sigframe32 __user *frame;
339	unsigned long restorer;
340	size_t frame_size;
341	u32 uc_flags;
342
343	frame_size = sizeof(*frame) -
344		     sizeof(frame->uc.uc_mcontext_ext.__reserved);
345	/*
346	 * gprs_high are always present for 31-bit compat tasks.
347	 * The space for vector registers is only allocated if
348	 * the machine supports it
349	 */
350	uc_flags = UC_GPRS_HIGH;
351	if (MACHINE_HAS_VX) {
352		uc_flags |= UC_VXRS;
353	} else
354		frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
355			      sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
356	frame = get_sigframe(&ksig->ka, regs, frame_size);
357	if (frame == (void __user *) -1UL)
358		return -EFAULT;
359
360	/* Set up backchain. */
361	if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
362		return -EFAULT;
363
364	/* Set up to return from userspace.  If provided, use a stub
365	   already in userspace.  */
366	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
367		restorer = (unsigned long __force)
368			ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
369	} else {
370		restorer = VDSO32_SYMBOL(current, rt_sigreturn);
 
 
 
371	}
372
373	/* Create siginfo on the signal stack */
374	if (copy_siginfo_to_user32(&frame->info, &ksig->info))
375		return -EFAULT;
376
377	/* Store registers needed to create the signal frame */
378	store_sigregs();
379
380	/* Create ucontext on the signal stack. */
381	if (__put_user(uc_flags, &frame->uc.uc_flags) ||
382	    __put_user(0, &frame->uc.uc_link) ||
383	    __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
384	    save_sigregs32(regs, &frame->uc.uc_mcontext) ||
385	    put_compat_sigset(&frame->uc.uc_sigmask, set, sizeof(compat_sigset_t)) ||
386	    save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
387		return -EFAULT;
388
389	/* Set up registers for signal handler */
390	regs->gprs[14] = restorer;
391	regs->gprs[15] = (__force __u64) frame;
392	/* Force 31 bit amode and default user address space control. */
393	regs->psw.mask = PSW_MASK_BA |
394		(PSW_USER_BITS & PSW_MASK_ASC) |
395		(regs->psw.mask & ~PSW_MASK_ASC);
396	regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
397
398	regs->gprs[2] = ksig->sig;
399	regs->gprs[3] = (__force __u64) &frame->info;
400	regs->gprs[4] = (__force __u64) &frame->uc;
401	regs->gprs[5] = current->thread.last_break;
402	return 0;
403}
404
405/*
406 * OK, we're invoking a handler
407 */	
408
409void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
410		     struct pt_regs *regs)
411{
412	int ret;
413
414	/* Set up the stack frame */
415	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
416		ret = setup_rt_frame32(ksig, oldset, regs);
417	else
418		ret = setup_frame32(ksig, oldset, regs);
419
420	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
421}
422