Linux Audio

Check our new training course

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