Linux Audio

Check our new training course

Loading...
v4.10.11
  1/*
  2 * Signal Handling for ARC
  3 *
  4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 *
 10 * vineetg: Jan 2010 (Restarting of timer related syscalls)
 11 *
 12 * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
 13 *  -do_signal() supports TIF_RESTORE_SIGMASK
 14 *  -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
 15 *  -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
 16 *  -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
 17 *  -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
 18 *   the job to do_signal()
 19 *
 20 * vineetg: July 2009
 21 *  -Modified Code to support the uClibc provided userland sigreturn stub
 22 *   to avoid kernel synthesing it on user stack at runtime, costing TLB
 23 *   probes and Cache line flushes.
 24 *
 25 * vineetg: July 2009
 26 *  -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
 27 *   in done in block copy rather than one word at a time.
 28 *   This saves around 2K of code and improves LMBench lat_sig <catch>
 29 *
 30 * rajeshwarr: Feb 2009
 31 *  - Support for Realtime Signals
 32 *
 33 * vineetg: Aug 11th 2008: Bug #94183
 34 *  -ViXS were still seeing crashes when using insmod to load drivers.
 35 *   It turned out that the code to change Execute permssions for TLB entries
 36 *   of user was not guarded for interrupts (mod_tlb_permission)
 37 *   This was causing TLB entries to be overwritten on unrelated indexes
 38 *
 39 * Vineetg: July 15th 2008: Bug #94183
 40 *  -Exception happens in Delay slot of a JMP, and before user space resumes,
 41 *   Signal is delivered (Ctrl + C) = >SIGINT.
 42 *   setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
 43 *   to run, but doesn't clear the Delay slot bit from status32. As a result,
 44 *   on resuming user mode, signal handler branches off to BTA of orig JMP
 45 *  -FIX: clear the DE bit from status32 in setup_frame( )
 46 *
 47 * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
 48 */
 49
 50#include <linux/signal.h>
 51#include <linux/ptrace.h>
 52#include <linux/personality.h>
 53#include <linux/uaccess.h>
 54#include <linux/syscalls.h>
 55#include <linux/tracehook.h>
 
 
 56#include <asm/ucontext.h>
 57
 58struct rt_sigframe {
 59	struct siginfo info;
 60	struct ucontext uc;
 61#define MAGIC_SIGALTSTK		0x07302004
 62	unsigned int sigret_magic;
 63};
 64
 65static int
 66stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
 67	       sigset_t *set)
 68{
 69	int err;
 70	struct user_regs_struct uregs;
 71
 72	uregs.scratch.bta	= regs->bta;
 73	uregs.scratch.lp_start	= regs->lp_start;
 74	uregs.scratch.lp_end	= regs->lp_end;
 75	uregs.scratch.lp_count	= regs->lp_count;
 76	uregs.scratch.status32	= regs->status32;
 77	uregs.scratch.ret	= regs->ret;
 78	uregs.scratch.blink	= regs->blink;
 79	uregs.scratch.fp	= regs->fp;
 80	uregs.scratch.gp	= regs->r26;
 81	uregs.scratch.r12	= regs->r12;
 82	uregs.scratch.r11	= regs->r11;
 83	uregs.scratch.r10	= regs->r10;
 84	uregs.scratch.r9	= regs->r9;
 85	uregs.scratch.r8	= regs->r8;
 86	uregs.scratch.r7	= regs->r7;
 87	uregs.scratch.r6	= regs->r6;
 88	uregs.scratch.r5	= regs->r5;
 89	uregs.scratch.r4	= regs->r4;
 90	uregs.scratch.r3	= regs->r3;
 91	uregs.scratch.r2	= regs->r2;
 92	uregs.scratch.r1	= regs->r1;
 93	uregs.scratch.r0	= regs->r0;
 94	uregs.scratch.sp	= regs->sp;
 95
 96	err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch,
 97			     sizeof(sf->uc.uc_mcontext.regs.scratch));
 98	err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
 99
100	return err;
101}
102
103static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
104{
105	sigset_t set;
106	int err;
107	struct user_regs_struct uregs;
108
109	err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
110	err |= __copy_from_user(&uregs.scratch,
111				&(sf->uc.uc_mcontext.regs.scratch),
112				sizeof(sf->uc.uc_mcontext.regs.scratch));
113	if (err)
114		return err;
115
116	set_current_blocked(&set);
117	regs->bta	= uregs.scratch.bta;
118	regs->lp_start	= uregs.scratch.lp_start;
119	regs->lp_end	= uregs.scratch.lp_end;
120	regs->lp_count	= uregs.scratch.lp_count;
121	regs->status32	= uregs.scratch.status32;
122	regs->ret	= uregs.scratch.ret;
123	regs->blink	= uregs.scratch.blink;
124	regs->fp	= uregs.scratch.fp;
125	regs->r26	= uregs.scratch.gp;
126	regs->r12	= uregs.scratch.r12;
127	regs->r11	= uregs.scratch.r11;
128	regs->r10	= uregs.scratch.r10;
129	regs->r9	= uregs.scratch.r9;
130	regs->r8	= uregs.scratch.r8;
131	regs->r7	= uregs.scratch.r7;
132	regs->r6	= uregs.scratch.r6;
133	regs->r5	= uregs.scratch.r5;
134	regs->r4	= uregs.scratch.r4;
135	regs->r3	= uregs.scratch.r3;
136	regs->r2	= uregs.scratch.r2;
137	regs->r1	= uregs.scratch.r1;
138	regs->r0	= uregs.scratch.r0;
139	regs->sp	= uregs.scratch.sp;
140
141	return 0;
142}
143
144static inline int is_do_ss_needed(unsigned int magic)
145{
146	if (MAGIC_SIGALTSTK == magic)
147		return 1;
148	else
149		return 0;
150}
151
152SYSCALL_DEFINE0(rt_sigreturn)
153{
154	struct rt_sigframe __user *sf;
155	unsigned int magic;
156	struct pt_regs *regs = current_pt_regs();
157
158	/* Always make any pending restarted system calls return -EINTR */
159	current->restart_block.fn = do_no_restart_syscall;
160
161	/* Since we stacked the signal on a word boundary,
162	 * then 'sp' should be word aligned here.  If it's
163	 * not, then the user is trying to mess with us.
164	 */
165	if (regs->sp & 3)
166		goto badframe;
167
168	sf = (struct rt_sigframe __force __user *)(regs->sp);
169
170	if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
171		goto badframe;
172
173	if (__get_user(magic, &sf->sigret_magic))
174		goto badframe;
175
176	if (unlikely(is_do_ss_needed(magic)))
177		if (restore_altstack(&sf->uc.uc_stack))
178			goto badframe;
179
180	if (restore_usr_regs(regs, sf))
181		goto badframe;
182
183	/* Don't restart from sigreturn */
184	syscall_wont_restart(regs);
185
186	/*
187	 * Ensure that sigreturn always returns to user mode (in case the
188	 * regs saved on user stack got fudged between save and sigreturn)
189	 * Otherwise it is easy to panic the kernel with a custom
190	 * signal handler and/or restorer which clobberes the status32/ret
191	 * to return to a bogus location in kernel mode.
192	 */
193	regs->status32 |= STATUS_U_MASK;
194
195	return regs->r0;
196
197badframe:
198	force_sig(SIGSEGV, current);
199	return 0;
200}
201
202/*
203 * Determine which stack to use..
204 */
205static inline void __user *get_sigframe(struct ksignal *ksig,
206					struct pt_regs *regs,
207					unsigned long framesize)
208{
209	unsigned long sp = sigsp(regs->sp, ksig);
210	void __user *frame;
211
212	/* No matter what happens, 'sp' must be word
213	 * aligned otherwise nasty things could happen
214	 */
215
216	/* ATPCS B01 mandates 8-byte alignment */
217	frame = (void __user *)((sp - framesize) & ~7);
218
219	/* Check that we can actually write to the signal frame */
220	if (!access_ok(VERIFY_WRITE, frame, framesize))
221		frame = NULL;
222
223	return frame;
224}
225
226static int
227setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
228{
229	struct rt_sigframe __user *sf;
230	unsigned int magic = 0;
231	int err = 0;
232
233	sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
234	if (!sf)
235		return 1;
236
237	/*
238	 * w/o SA_SIGINFO, struct ucontext is partially populated (only
239	 * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
240	 * during signal handler execution. This works for SA_SIGINFO as well
241	 * although the semantics are now overloaded (the same reg state can be
242	 * inspected by userland: but are they allowed to fiddle with it ?
243	 */
244	err |= stash_usr_regs(sf, regs, set);
245
246	/*
247	 * SA_SIGINFO requires 3 args to signal handler:
248	 *  #1: sig-no (common to any handler)
249	 *  #2: struct siginfo
250	 *  #3: struct ucontext (completely populated)
251	 */
252	if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
253		err |= copy_siginfo_to_user(&sf->info, &ksig->info);
254		err |= __put_user(0, &sf->uc.uc_flags);
255		err |= __put_user(NULL, &sf->uc.uc_link);
256		err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
257
258		/* setup args 2 and 3 for user mode handler */
259		regs->r1 = (unsigned long)&sf->info;
260		regs->r2 = (unsigned long)&sf->uc;
261
262		/*
263		 * small optim to avoid unconditonally calling do_sigaltstack
264		 * in sigreturn path, now that we only have rt_sigreturn
265		 */
266		magic = MAGIC_SIGALTSTK;
267	}
268
269	err |= __put_user(magic, &sf->sigret_magic);
270	if (err)
271		return err;
272
273	/* #1 arg to the user Signal handler */
274	regs->r0 = ksig->sig;
275
276	/* setup PC of user space signal handler */
277	regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
278
279	/*
280	 * handler returns using sigreturn stub provided already by userpsace
281	 * If not, nuke the process right away
282	 */
283	if(!(ksig->ka.sa.sa_flags & SA_RESTORER))
284		return 1;
285
286	regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
287
288	/* User Stack for signal handler will be above the frame just carved */
289	regs->sp = (unsigned long)sf;
290
291	/*
292	 * Bug 94183, Clear the DE bit, so that when signal handler
293	 * starts to run, it doesn't use BTA
294	 */
295	regs->status32 &= ~STATUS_DE_MASK;
296	regs->status32 |= STATUS_L_MASK;
297
298	return err;
299}
300
301static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
302{
303	switch (regs->r0) {
304	case -ERESTART_RESTARTBLOCK:
305	case -ERESTARTNOHAND:
306		/*
307		 * ERESTARTNOHAND means that the syscall should
308		 * only be restarted if there was no handler for
309		 * the signal, and since we only get here if there
310		 * is a handler, we don't restart
311		 */
312		regs->r0 = -EINTR;   /* ERESTART_xxx is internal */
313		break;
314
315	case -ERESTARTSYS:
316		/*
317		 * ERESTARTSYS means to restart the syscall if
318		 * there is no handler or the handler was
319		 * registered with SA_RESTART
320		 */
321		if (!(ka->sa.sa_flags & SA_RESTART)) {
322			regs->r0 = -EINTR;
323			break;
324		}
325		/* fallthrough */
326
327	case -ERESTARTNOINTR:
328		/*
329		 * ERESTARTNOINTR means that the syscall should
330		 * be called again after the signal handler returns.
331		 * Setup reg state just as it was before doing the trap
332		 * r0 has been clobbered with sys call ret code thus it
333		 * needs to be reloaded with orig first arg to syscall
334		 * in orig_r0. Rest of relevant reg-file:
335		 * r8 (syscall num) and (r1 - r7) will be reset to
336		 * their orig user space value when we ret from kernel
337		 */
338		regs->r0 = regs->orig_r0;
339		regs->ret -= is_isa_arcv2() ? 2 : 4;
340		break;
341	}
342}
343
344/*
345 * OK, we're invoking a handler
346 */
347static void
348handle_signal(struct ksignal *ksig, struct pt_regs *regs)
349{
350	sigset_t *oldset = sigmask_to_save();
351	int failed;
352
353	/* Set up the stack frame */
354	failed = setup_rt_frame(ksig, oldset, regs);
355
356	signal_setup_done(failed, ksig, 0);
357}
358
359void do_signal(struct pt_regs *regs)
360{
361	struct ksignal ksig;
362	int restart_scall;
363
364	restart_scall = in_syscall(regs) && syscall_restartable(regs);
365
366	if (get_signal(&ksig)) {
367		if (restart_scall) {
368			arc_restart_syscall(&ksig.ka, regs);
369			syscall_wont_restart(regs);	/* No more restarts */
370		}
371		handle_signal(&ksig, regs);
372		return;
373	}
374
375	if (restart_scall) {
376		/* No handler for syscall: restart it */
377		if (regs->r0 == -ERESTARTNOHAND ||
378		    regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
379			regs->r0 = regs->orig_r0;
380			regs->ret -= is_isa_arcv2() ? 2 : 4;
381		} else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
382			regs->r8 = __NR_restart_syscall;
383			regs->ret -= is_isa_arcv2() ? 2 : 4;
384		}
385		syscall_wont_restart(regs);	/* No more restarts */
386	}
387
388	/* If there's no signal to deliver, restore the saved sigmask back */
389	restore_saved_sigmask();
390}
391
392void do_notify_resume(struct pt_regs *regs)
393{
394	/*
395	 * ASM glue gaurantees that this is only called when returning to
396	 * user mode
397	 */
398	if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
399		tracehook_notify_resume(regs);
400}
v4.17
  1/*
  2 * Signal Handling for ARC
  3 *
  4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 *
 10 * vineetg: Jan 2010 (Restarting of timer related syscalls)
 11 *
 12 * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
 13 *  -do_signal() supports TIF_RESTORE_SIGMASK
 14 *  -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
 15 *  -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
 16 *  -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
 17 *  -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
 18 *   the job to do_signal()
 19 *
 20 * vineetg: July 2009
 21 *  -Modified Code to support the uClibc provided userland sigreturn stub
 22 *   to avoid kernel synthesing it on user stack at runtime, costing TLB
 23 *   probes and Cache line flushes.
 24 *
 25 * vineetg: July 2009
 26 *  -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
 27 *   in done in block copy rather than one word at a time.
 28 *   This saves around 2K of code and improves LMBench lat_sig <catch>
 29 *
 30 * rajeshwarr: Feb 2009
 31 *  - Support for Realtime Signals
 32 *
 33 * vineetg: Aug 11th 2008: Bug #94183
 34 *  -ViXS were still seeing crashes when using insmod to load drivers.
 35 *   It turned out that the code to change Execute permssions for TLB entries
 36 *   of user was not guarded for interrupts (mod_tlb_permission)
 37 *   This was causing TLB entries to be overwritten on unrelated indexes
 38 *
 39 * Vineetg: July 15th 2008: Bug #94183
 40 *  -Exception happens in Delay slot of a JMP, and before user space resumes,
 41 *   Signal is delivered (Ctrl + C) = >SIGINT.
 42 *   setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
 43 *   to run, but doesn't clear the Delay slot bit from status32. As a result,
 44 *   on resuming user mode, signal handler branches off to BTA of orig JMP
 45 *  -FIX: clear the DE bit from status32 in setup_frame( )
 46 *
 47 * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
 48 */
 49
 50#include <linux/signal.h>
 51#include <linux/ptrace.h>
 52#include <linux/personality.h>
 53#include <linux/uaccess.h>
 54#include <linux/syscalls.h>
 55#include <linux/tracehook.h>
 56#include <linux/sched/task_stack.h>
 57
 58#include <asm/ucontext.h>
 59
 60struct rt_sigframe {
 61	struct siginfo info;
 62	struct ucontext uc;
 63#define MAGIC_SIGALTSTK		0x07302004
 64	unsigned int sigret_magic;
 65};
 66
 67static int
 68stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
 69	       sigset_t *set)
 70{
 71	int err;
 72	struct user_regs_struct uregs;
 73
 74	uregs.scratch.bta	= regs->bta;
 75	uregs.scratch.lp_start	= regs->lp_start;
 76	uregs.scratch.lp_end	= regs->lp_end;
 77	uregs.scratch.lp_count	= regs->lp_count;
 78	uregs.scratch.status32	= regs->status32;
 79	uregs.scratch.ret	= regs->ret;
 80	uregs.scratch.blink	= regs->blink;
 81	uregs.scratch.fp	= regs->fp;
 82	uregs.scratch.gp	= regs->r26;
 83	uregs.scratch.r12	= regs->r12;
 84	uregs.scratch.r11	= regs->r11;
 85	uregs.scratch.r10	= regs->r10;
 86	uregs.scratch.r9	= regs->r9;
 87	uregs.scratch.r8	= regs->r8;
 88	uregs.scratch.r7	= regs->r7;
 89	uregs.scratch.r6	= regs->r6;
 90	uregs.scratch.r5	= regs->r5;
 91	uregs.scratch.r4	= regs->r4;
 92	uregs.scratch.r3	= regs->r3;
 93	uregs.scratch.r2	= regs->r2;
 94	uregs.scratch.r1	= regs->r1;
 95	uregs.scratch.r0	= regs->r0;
 96	uregs.scratch.sp	= regs->sp;
 97
 98	err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch,
 99			     sizeof(sf->uc.uc_mcontext.regs.scratch));
100	err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
101
102	return err;
103}
104
105static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
106{
107	sigset_t set;
108	int err;
109	struct user_regs_struct uregs;
110
111	err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
112	err |= __copy_from_user(&uregs.scratch,
113				&(sf->uc.uc_mcontext.regs.scratch),
114				sizeof(sf->uc.uc_mcontext.regs.scratch));
115	if (err)
116		return err;
117
118	set_current_blocked(&set);
119	regs->bta	= uregs.scratch.bta;
120	regs->lp_start	= uregs.scratch.lp_start;
121	regs->lp_end	= uregs.scratch.lp_end;
122	regs->lp_count	= uregs.scratch.lp_count;
123	regs->status32	= uregs.scratch.status32;
124	regs->ret	= uregs.scratch.ret;
125	regs->blink	= uregs.scratch.blink;
126	regs->fp	= uregs.scratch.fp;
127	regs->r26	= uregs.scratch.gp;
128	regs->r12	= uregs.scratch.r12;
129	regs->r11	= uregs.scratch.r11;
130	regs->r10	= uregs.scratch.r10;
131	regs->r9	= uregs.scratch.r9;
132	regs->r8	= uregs.scratch.r8;
133	regs->r7	= uregs.scratch.r7;
134	regs->r6	= uregs.scratch.r6;
135	regs->r5	= uregs.scratch.r5;
136	regs->r4	= uregs.scratch.r4;
137	regs->r3	= uregs.scratch.r3;
138	regs->r2	= uregs.scratch.r2;
139	regs->r1	= uregs.scratch.r1;
140	regs->r0	= uregs.scratch.r0;
141	regs->sp	= uregs.scratch.sp;
142
143	return 0;
144}
145
146static inline int is_do_ss_needed(unsigned int magic)
147{
148	if (MAGIC_SIGALTSTK == magic)
149		return 1;
150	else
151		return 0;
152}
153
154SYSCALL_DEFINE0(rt_sigreturn)
155{
156	struct rt_sigframe __user *sf;
157	unsigned int magic;
158	struct pt_regs *regs = current_pt_regs();
159
160	/* Always make any pending restarted system calls return -EINTR */
161	current->restart_block.fn = do_no_restart_syscall;
162
163	/* Since we stacked the signal on a word boundary,
164	 * then 'sp' should be word aligned here.  If it's
165	 * not, then the user is trying to mess with us.
166	 */
167	if (regs->sp & 3)
168		goto badframe;
169
170	sf = (struct rt_sigframe __force __user *)(regs->sp);
171
172	if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
173		goto badframe;
174
175	if (__get_user(magic, &sf->sigret_magic))
176		goto badframe;
177
178	if (unlikely(is_do_ss_needed(magic)))
179		if (restore_altstack(&sf->uc.uc_stack))
180			goto badframe;
181
182	if (restore_usr_regs(regs, sf))
183		goto badframe;
184
185	/* Don't restart from sigreturn */
186	syscall_wont_restart(regs);
187
188	/*
189	 * Ensure that sigreturn always returns to user mode (in case the
190	 * regs saved on user stack got fudged between save and sigreturn)
191	 * Otherwise it is easy to panic the kernel with a custom
192	 * signal handler and/or restorer which clobberes the status32/ret
193	 * to return to a bogus location in kernel mode.
194	 */
195	regs->status32 |= STATUS_U_MASK;
196
197	return regs->r0;
198
199badframe:
200	force_sig(SIGSEGV, current);
201	return 0;
202}
203
204/*
205 * Determine which stack to use..
206 */
207static inline void __user *get_sigframe(struct ksignal *ksig,
208					struct pt_regs *regs,
209					unsigned long framesize)
210{
211	unsigned long sp = sigsp(regs->sp, ksig);
212	void __user *frame;
213
214	/* No matter what happens, 'sp' must be word
215	 * aligned otherwise nasty things could happen
216	 */
217
218	/* ATPCS B01 mandates 8-byte alignment */
219	frame = (void __user *)((sp - framesize) & ~7);
220
221	/* Check that we can actually write to the signal frame */
222	if (!access_ok(VERIFY_WRITE, frame, framesize))
223		frame = NULL;
224
225	return frame;
226}
227
228static int
229setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
230{
231	struct rt_sigframe __user *sf;
232	unsigned int magic = 0;
233	int err = 0;
234
235	sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
236	if (!sf)
237		return 1;
238
239	/*
240	 * w/o SA_SIGINFO, struct ucontext is partially populated (only
241	 * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
242	 * during signal handler execution. This works for SA_SIGINFO as well
243	 * although the semantics are now overloaded (the same reg state can be
244	 * inspected by userland: but are they allowed to fiddle with it ?
245	 */
246	err |= stash_usr_regs(sf, regs, set);
247
248	/*
249	 * SA_SIGINFO requires 3 args to signal handler:
250	 *  #1: sig-no (common to any handler)
251	 *  #2: struct siginfo
252	 *  #3: struct ucontext (completely populated)
253	 */
254	if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
255		err |= copy_siginfo_to_user(&sf->info, &ksig->info);
256		err |= __put_user(0, &sf->uc.uc_flags);
257		err |= __put_user(NULL, &sf->uc.uc_link);
258		err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
259
260		/* setup args 2 and 3 for user mode handler */
261		regs->r1 = (unsigned long)&sf->info;
262		regs->r2 = (unsigned long)&sf->uc;
263
264		/*
265		 * small optim to avoid unconditonally calling do_sigaltstack
266		 * in sigreturn path, now that we only have rt_sigreturn
267		 */
268		magic = MAGIC_SIGALTSTK;
269	}
270
271	err |= __put_user(magic, &sf->sigret_magic);
272	if (err)
273		return err;
274
275	/* #1 arg to the user Signal handler */
276	regs->r0 = ksig->sig;
277
278	/* setup PC of user space signal handler */
279	regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
280
281	/*
282	 * handler returns using sigreturn stub provided already by userpsace
283	 * If not, nuke the process right away
284	 */
285	if(!(ksig->ka.sa.sa_flags & SA_RESTORER))
286		return 1;
287
288	regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
289
290	/* User Stack for signal handler will be above the frame just carved */
291	regs->sp = (unsigned long)sf;
292
293	/*
294	 * Bug 94183, Clear the DE bit, so that when signal handler
295	 * starts to run, it doesn't use BTA
296	 */
297	regs->status32 &= ~STATUS_DE_MASK;
298	regs->status32 |= STATUS_L_MASK;
299
300	return err;
301}
302
303static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
304{
305	switch (regs->r0) {
306	case -ERESTART_RESTARTBLOCK:
307	case -ERESTARTNOHAND:
308		/*
309		 * ERESTARTNOHAND means that the syscall should
310		 * only be restarted if there was no handler for
311		 * the signal, and since we only get here if there
312		 * is a handler, we don't restart
313		 */
314		regs->r0 = -EINTR;   /* ERESTART_xxx is internal */
315		break;
316
317	case -ERESTARTSYS:
318		/*
319		 * ERESTARTSYS means to restart the syscall if
320		 * there is no handler or the handler was
321		 * registered with SA_RESTART
322		 */
323		if (!(ka->sa.sa_flags & SA_RESTART)) {
324			regs->r0 = -EINTR;
325			break;
326		}
327		/* fallthrough */
328
329	case -ERESTARTNOINTR:
330		/*
331		 * ERESTARTNOINTR means that the syscall should
332		 * be called again after the signal handler returns.
333		 * Setup reg state just as it was before doing the trap
334		 * r0 has been clobbered with sys call ret code thus it
335		 * needs to be reloaded with orig first arg to syscall
336		 * in orig_r0. Rest of relevant reg-file:
337		 * r8 (syscall num) and (r1 - r7) will be reset to
338		 * their orig user space value when we ret from kernel
339		 */
340		regs->r0 = regs->orig_r0;
341		regs->ret -= is_isa_arcv2() ? 2 : 4;
342		break;
343	}
344}
345
346/*
347 * OK, we're invoking a handler
348 */
349static void
350handle_signal(struct ksignal *ksig, struct pt_regs *regs)
351{
352	sigset_t *oldset = sigmask_to_save();
353	int failed;
354
355	/* Set up the stack frame */
356	failed = setup_rt_frame(ksig, oldset, regs);
357
358	signal_setup_done(failed, ksig, 0);
359}
360
361void do_signal(struct pt_regs *regs)
362{
363	struct ksignal ksig;
364	int restart_scall;
365
366	restart_scall = in_syscall(regs) && syscall_restartable(regs);
367
368	if (get_signal(&ksig)) {
369		if (restart_scall) {
370			arc_restart_syscall(&ksig.ka, regs);
371			syscall_wont_restart(regs);	/* No more restarts */
372		}
373		handle_signal(&ksig, regs);
374		return;
375	}
376
377	if (restart_scall) {
378		/* No handler for syscall: restart it */
379		if (regs->r0 == -ERESTARTNOHAND ||
380		    regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
381			regs->r0 = regs->orig_r0;
382			regs->ret -= is_isa_arcv2() ? 2 : 4;
383		} else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
384			regs->r8 = __NR_restart_syscall;
385			regs->ret -= is_isa_arcv2() ? 2 : 4;
386		}
387		syscall_wont_restart(regs);	/* No more restarts */
388	}
389
390	/* If there's no signal to deliver, restore the saved sigmask back */
391	restore_saved_sigmask();
392}
393
394void do_notify_resume(struct pt_regs *regs)
395{
396	/*
397	 * ASM glue gaurantees that this is only called when returning to
398	 * user mode
399	 */
400	if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
401		tracehook_notify_resume(regs);
402}