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