xref: /linux/arch/microblaze/kernel/signal.c (revision afca12e35e711ae8f97e835a3704cc305592eac9)
1 /*
2  * Signal handling
3  *
4  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5  * Copyright (C) 2008-2009 PetaLogix
6  * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7  * Copyright (C) 2001 NEC Corporation
8  * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9  * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10  * Copyright (C) 1991,1992 Linus Torvalds
11  *
12  * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
13  *
14  * This file was derived from the sh version, arch/sh/kernel/signal.c
15  *
16  * This file is subject to the terms and conditions of the GNU General
17  * Public License. See the file COPYING in the main directory of this
18  * archive for more details.
19  */
20 
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/kernel.h>
25 #include <linux/signal.h>
26 #include <linux/errno.h>
27 #include <linux/wait.h>
28 #include <linux/ptrace.h>
29 #include <linux/unistd.h>
30 #include <linux/stddef.h>
31 #include <linux/personality.h>
32 #include <linux/percpu.h>
33 #include <linux/linkage.h>
34 #include <linux/resume_user_mode.h>
35 #include <asm/entry.h>
36 #include <asm/ucontext.h>
37 #include <linux/uaccess.h>
38 #include <linux/syscalls.h>
39 #include <asm/cacheflush.h>
40 #include <asm/syscalls.h>
41 
42 /*
43  * Do a signal return; undo the signal stack.
44  */
45 struct sigframe {
46 	struct sigcontext sc;
47 	unsigned long extramask[_NSIG_WORDS-1];
48 	unsigned long tramp[2];	/* signal trampoline */
49 };
50 
51 struct rt_sigframe {
52 	struct siginfo info;
53 	struct ucontext uc;
54 	unsigned long tramp[2];	/* signal trampoline */
55 };
56 
57 static int restore_sigcontext(struct pt_regs *regs,
58 				struct sigcontext __user *sc, int *rval_p)
59 {
60 	unsigned int err = 0;
61 
62 #define COPY(x)		{err |= __get_user(regs->x, &sc->regs.x); }
63 	COPY(r0);
64 	COPY(r1);
65 	COPY(r2);	COPY(r3);	COPY(r4);	COPY(r5);
66 	COPY(r6);	COPY(r7);	COPY(r8);	COPY(r9);
67 	COPY(r10);	COPY(r11);	COPY(r12);	COPY(r13);
68 	COPY(r14);	COPY(r15);	COPY(r16);	COPY(r17);
69 	COPY(r18);	COPY(r19);	COPY(r20);	COPY(r21);
70 	COPY(r22);	COPY(r23);	COPY(r24);	COPY(r25);
71 	COPY(r26);	COPY(r27);	COPY(r28);	COPY(r29);
72 	COPY(r30);	COPY(r31);
73 	COPY(pc);	COPY(ear);	COPY(esr);	COPY(fsr);
74 #undef COPY
75 
76 	*rval_p = regs->r3;
77 
78 	return err;
79 }
80 
81 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
82 {
83 	struct rt_sigframe __user *frame =
84 		(struct rt_sigframe __user *)(regs->r1);
85 
86 	sigset_t set;
87 	int rval;
88 
89 	/* Always make any pending restarted system calls return -EINTR */
90 	current->restart_block.fn = do_no_restart_syscall;
91 
92 	if (!access_ok(frame, sizeof(*frame)))
93 		goto badframe;
94 
95 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
96 		goto badframe;
97 
98 	set_current_blocked(&set);
99 
100 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
101 		goto badframe;
102 
103 	if (restore_altstack(&frame->uc.uc_stack))
104 		goto badframe;
105 
106 	return rval;
107 
108 badframe:
109 	force_sig(SIGSEGV);
110 	return 0;
111 }
112 
113 /*
114  * Set up a signal frame.
115  */
116 
117 static int
118 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
119 		unsigned long mask)
120 {
121 	int err = 0;
122 
123 #define COPY(x)		{err |= __put_user(regs->x, &sc->regs.x); }
124 	COPY(r0);
125 	COPY(r1);
126 	COPY(r2);	COPY(r3);	COPY(r4);	COPY(r5);
127 	COPY(r6);	COPY(r7);	COPY(r8);	COPY(r9);
128 	COPY(r10);	COPY(r11);	COPY(r12);	COPY(r13);
129 	COPY(r14);	COPY(r15);	COPY(r16);	COPY(r17);
130 	COPY(r18);	COPY(r19);	COPY(r20);	COPY(r21);
131 	COPY(r22);	COPY(r23);	COPY(r24);	COPY(r25);
132 	COPY(r26);	COPY(r27);	COPY(r28);	COPY(r29);
133 	COPY(r30);	COPY(r31);
134 	COPY(pc);	COPY(ear);	COPY(esr);	COPY(fsr);
135 #undef COPY
136 
137 	err |= __put_user(mask, &sc->oldmask);
138 
139 	return err;
140 }
141 
142 /*
143  * Determine which stack to use..
144  */
145 static inline void __user *
146 get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size)
147 {
148 	/* Default to using normal stack */
149 	unsigned long sp = sigsp(regs->r1, ksig);
150 
151 	return (void __user *)((sp - frame_size) & -8UL);
152 }
153 
154 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
155 			  struct pt_regs *regs)
156 {
157 	struct rt_sigframe __user *frame;
158 	int err = 0, sig = ksig->sig;
159 	unsigned long address = 0;
160 	pmd_t *pmdp;
161 	pte_t *ptep;
162 
163 	frame = get_sigframe(ksig, regs, sizeof(*frame));
164 
165 	if (!access_ok(frame, sizeof(*frame)))
166 		return -EFAULT;
167 
168 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
169 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
170 
171 	/* Create the ucontext. */
172 	err |= __put_user(0, &frame->uc.uc_flags);
173 	err |= __put_user(NULL, &frame->uc.uc_link);
174 	err |= __save_altstack(&frame->uc.uc_stack, regs->r1);
175 	err |= setup_sigcontext(&frame->uc.uc_mcontext,
176 			regs, set->sig[0]);
177 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
178 
179 	/* Set up to return from userspace. If provided, use a stub
180 	 already in userspace. */
181 	/* minus 8 is offset to cater for "rtsd r15,8" */
182 	/* addi r12, r0, __NR_sigreturn */
183 	err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
184 			frame->tramp + 0);
185 	/* brki r14, 0x8 */
186 	err |= __put_user(0xb9cc0008, frame->tramp + 1);
187 
188 	/* Return from sighandler will jump to the tramp.
189 	 Negative 8 offset because return is rtsd r15, 8 */
190 	regs->r15 = ((unsigned long)frame->tramp)-8;
191 
192 	address = ((unsigned long)frame->tramp);
193 	pmdp = pmd_off(current->mm, address);
194 
195 	preempt_disable();
196 	ptep = pte_offset_map(pmdp, address);
197 	if (ptep && pte_present(*ptep)) {
198 		address = (unsigned long) page_address(pte_page(*ptep));
199 		/* MS: I need add offset in page */
200 		address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
201 		/* MS address is virtual */
202 		address = __virt_to_phys(address);
203 		invalidate_icache_range(address, address + 8);
204 		flush_dcache_range(address, address + 8);
205 	}
206 	if (ptep)
207 		pte_unmap(ptep);
208 	preempt_enable();
209 	if (err)
210 		return -EFAULT;
211 
212 	/* Set up registers for signal handler */
213 	regs->r1 = (unsigned long) frame;
214 
215 	/* Signal handler args: */
216 	regs->r5 = sig; /* arg 0: signum */
217 	regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
218 	regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
219 	/* Offset to handle microblaze rtid r14, 0 */
220 	regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
221 
222 #ifdef DEBUG_SIG
223 	pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n",
224 		current->comm, current->pid, frame, regs->pc);
225 #endif
226 
227 	return 0;
228 }
229 
230 /* Handle restarting system calls */
231 static inline void
232 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
233 {
234 	switch (regs->r3) {
235 	case -ERESTART_RESTARTBLOCK:
236 	case -ERESTARTNOHAND:
237 		if (!has_handler)
238 			goto do_restart;
239 		regs->r3 = -EINTR;
240 		break;
241 	case -ERESTARTSYS:
242 		if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
243 			regs->r3 = -EINTR;
244 			break;
245 	}
246 		fallthrough;
247 	case -ERESTARTNOINTR:
248 do_restart:
249 		/* offset of 4 bytes to re-execute trap (brki) instruction */
250 		regs->pc -= 4;
251 		break;
252 	}
253 }
254 
255 /*
256  * OK, we're invoking a handler
257  */
258 
259 static void
260 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
261 {
262 	sigset_t *oldset = sigmask_to_save();
263 	int ret;
264 
265 	/* Set up the stack frame */
266 	ret = setup_rt_frame(ksig, oldset, regs);
267 
268 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
269 }
270 
271 /*
272  * Note that 'init' is a special process: it doesn't get signals it doesn't
273  * want to handle. Thus you cannot kill init even with a SIGKILL even by
274  * mistake.
275  *
276  * Note that we go through the signals twice: once to check the signals that
277  * the kernel can handle, and then we build all the user-level signal handling
278  * stack-frames in one go after that.
279  */
280 static void do_signal(struct pt_regs *regs, int in_syscall)
281 {
282 	struct ksignal ksig;
283 
284 #ifdef DEBUG_SIG
285 	pr_info("do signal: %p %d\n", regs, in_syscall);
286 	pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
287 			regs->r12, read_thread_flags());
288 #endif
289 
290 	if (get_signal(&ksig)) {
291 		/* Whee! Actually deliver the signal. */
292 		if (in_syscall)
293 			handle_restart(regs, &ksig.ka, 1);
294 		handle_signal(&ksig, regs);
295 		return;
296 	}
297 
298 	if (in_syscall)
299 		handle_restart(regs, NULL, 0);
300 
301 	/*
302 	 * If there's no signal to deliver, we just put the saved sigmask
303 	 * back.
304 	 */
305 	restore_saved_sigmask();
306 }
307 
308 asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall)
309 {
310 	if (test_thread_flag(TIF_SIGPENDING) ||
311 	    test_thread_flag(TIF_NOTIFY_SIGNAL))
312 		do_signal(regs, in_syscall);
313 
314 	if (test_thread_flag(TIF_NOTIFY_RESUME))
315 		resume_user_mode_work(regs);
316 }
317