]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - arch/csky/kernel/signal.c
Merge tag 'omap-for-v5.0/fixes-rc7-signed' of git://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-eoan-kernel.git] / arch / csky / kernel / signal.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4 #include <linux/sched.h>
5 #include <linux/mm.h>
6 #include <linux/kernel.h>
7 #include <linux/signal.h>
8 #include <linux/syscalls.h>
9 #include <linux/errno.h>
10 #include <linux/wait.h>
11 #include <linux/ptrace.h>
12 #include <linux/unistd.h>
13 #include <linux/stddef.h>
14 #include <linux/highuid.h>
15 #include <linux/personality.h>
16 #include <linux/tty.h>
17 #include <linux/binfmts.h>
18 #include <linux/tracehook.h>
19 #include <linux/freezer.h>
20 #include <linux/uaccess.h>
21
22 #include <asm/setup.h>
23 #include <asm/pgtable.h>
24 #include <asm/traps.h>
25 #include <asm/ucontext.h>
26 #include <asm/vdso.h>
27
28 #include <abi/regdef.h>
29
30 #ifdef CONFIG_CPU_HAS_FPU
31 #include <abi/fpu.h>
32
33 static int restore_fpu_state(struct sigcontext *sc)
34 {
35 int err = 0;
36 struct user_fp user_fp;
37
38 err = copy_from_user(&user_fp, &sc->sc_user_fp, sizeof(user_fp));
39
40 restore_from_user_fp(&user_fp);
41
42 return err;
43 }
44
45 static int save_fpu_state(struct sigcontext *sc)
46 {
47 struct user_fp user_fp;
48
49 save_to_user_fp(&user_fp);
50
51 return copy_to_user(&sc->sc_user_fp, &user_fp, sizeof(user_fp));
52 }
53 #else
54 static inline int restore_fpu_state(struct sigcontext *sc) { return 0; }
55 static inline int save_fpu_state(struct sigcontext *sc) { return 0; }
56 #endif
57
58 struct rt_sigframe {
59 int sig;
60 struct siginfo *pinfo;
61 void *puc;
62 struct siginfo info;
63 struct ucontext uc;
64 };
65
66 static int
67 restore_sigframe(struct pt_regs *regs,
68 struct sigcontext *sc, int *pr2)
69 {
70 int err = 0;
71
72 /* Always make any pending restarted system calls return -EINTR */
73 current_thread_info()->task->restart_block.fn = do_no_restart_syscall;
74
75 err |= copy_from_user(regs, &sc->sc_pt_regs, sizeof(struct pt_regs));
76
77 err |= restore_fpu_state(sc);
78
79 *pr2 = regs->a0;
80 return err;
81 }
82
83 asmlinkage int
84 do_rt_sigreturn(void)
85 {
86 sigset_t set;
87 int a0;
88 struct pt_regs *regs = current_pt_regs();
89 struct rt_sigframe *frame = (struct rt_sigframe *)(regs->usp);
90
91 if (!access_ok(frame, sizeof(*frame)))
92 goto badframe;
93 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
94 goto badframe;
95
96 sigdelsetmask(&set, (sigmask(SIGKILL) | sigmask(SIGSTOP)));
97 spin_lock_irq(&current->sighand->siglock);
98 current->blocked = set;
99 recalc_sigpending();
100 spin_unlock_irq(&current->sighand->siglock);
101
102 if (restore_sigframe(regs, &frame->uc.uc_mcontext, &a0))
103 goto badframe;
104
105 return a0;
106
107 badframe:
108 force_sig(SIGSEGV, current);
109 return 0;
110 }
111
112 static int setup_sigframe(struct sigcontext *sc, struct pt_regs *regs)
113 {
114 int err = 0;
115
116 err |= copy_to_user(&sc->sc_pt_regs, regs, sizeof(struct pt_regs));
117 err |= save_fpu_state(sc);
118
119 return err;
120 }
121
122 static inline void *
123 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
124 {
125 unsigned long usp;
126
127 /* Default to using normal stack. */
128 usp = regs->usp;
129
130 /* This is the X/Open sanctioned signal stack switching. */
131 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(usp)) {
132 if (!on_sig_stack(usp))
133 usp = current->sas_ss_sp + current->sas_ss_size;
134 }
135 return (void *)((usp - frame_size) & -8UL);
136 }
137
138 static int
139 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
140 {
141 struct rt_sigframe *frame;
142 int err = 0;
143
144 struct csky_vdso *vdso = current->mm->context.vdso;
145
146 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
147 if (!frame)
148 return 1;
149
150 err |= __put_user(ksig->sig, &frame->sig);
151 err |= __put_user(&frame->info, &frame->pinfo);
152 err |= __put_user(&frame->uc, &frame->puc);
153 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
154
155 /* Create the ucontext. */
156 err |= __put_user(0, &frame->uc.uc_flags);
157 err |= __put_user(0, &frame->uc.uc_link);
158 err |= __put_user((void *)current->sas_ss_sp,
159 &frame->uc.uc_stack.ss_sp);
160 err |= __put_user(sas_ss_flags(regs->usp),
161 &frame->uc.uc_stack.ss_flags);
162 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
163 err |= setup_sigframe(&frame->uc.uc_mcontext, regs);
164 err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
165
166 if (err)
167 goto give_sigsegv;
168
169 /* Set up registers for signal handler */
170 regs->usp = (unsigned long)frame;
171 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
172 regs->lr = (unsigned long)vdso->rt_signal_retcode;
173
174 adjust_stack:
175 regs->a0 = ksig->sig; /* first arg is signo */
176 regs->a1 = (unsigned long)(&(frame->info));
177 regs->a2 = (unsigned long)(&(frame->uc));
178 return err;
179
180 give_sigsegv:
181 if (ksig->sig == SIGSEGV)
182 ksig->ka.sa.sa_handler = SIG_DFL;
183 force_sig(SIGSEGV, current);
184 goto adjust_stack;
185 }
186
187 /*
188 * OK, we're invoking a handler
189 */
190 static int
191 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
192 {
193 int ret;
194 sigset_t *oldset = sigmask_to_save();
195
196 /*
197 * set up the stack frame, regardless of SA_SIGINFO,
198 * and pass info anyway.
199 */
200 ret = setup_rt_frame(ksig, oldset, regs);
201
202 if (ret != 0) {
203 force_sigsegv(ksig->sig, current);
204 return ret;
205 }
206
207 /* Block the signal if we were successful. */
208 spin_lock_irq(&current->sighand->siglock);
209 sigorsets(&current->blocked, &current->blocked, &ksig->ka.sa.sa_mask);
210 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
211 sigaddset(&current->blocked, ksig->sig);
212 recalc_sigpending();
213 spin_unlock_irq(&current->sighand->siglock);
214
215 return 0;
216 }
217
218 /*
219 * Note that 'init' is a special process: it doesn't get signals it doesn't
220 * want to handle. Thus you cannot kill init even with a SIGKILL even by
221 * mistake.
222 *
223 * Note that we go through the signals twice: once to check the signals
224 * that the kernel can handle, and then we build all the user-level signal
225 * handling stack-frames in one go after that.
226 */
227 static void do_signal(struct pt_regs *regs, int syscall)
228 {
229 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
230 struct ksignal ksig;
231
232 /*
233 * We want the common case to go fast, which
234 * is why we may in certain cases get here from
235 * kernel mode. Just return without doing anything
236 * if so.
237 */
238 if (!user_mode(regs))
239 return;
240
241 /*
242 * If we were from a system call, check for system call restarting...
243 */
244 if (syscall) {
245 continue_addr = regs->pc;
246 #if defined(__CSKYABIV2__)
247 restart_addr = continue_addr - 4;
248 #else
249 restart_addr = continue_addr - 2;
250 #endif
251 retval = regs->a0;
252
253 /*
254 * Prepare for system call restart. We do this here so that a
255 * debugger will see the already changed.
256 */
257 switch (retval) {
258 case -ERESTARTNOHAND:
259 case -ERESTARTSYS:
260 case -ERESTARTNOINTR:
261 regs->a0 = regs->orig_a0;
262 regs->pc = restart_addr;
263 break;
264 case -ERESTART_RESTARTBLOCK:
265 regs->a0 = -EINTR;
266 break;
267 }
268 }
269
270 if (try_to_freeze())
271 goto no_signal;
272
273 /*
274 * Get the signal to deliver. When running under ptrace, at this
275 * point the debugger may change all our registers ...
276 */
277 if (get_signal(&ksig)) {
278 /*
279 * Depending on the signal settings we may need to revert the
280 * decision to restart the system call. But skip this if a
281 * debugger has chosen to restart at a different PC.
282 */
283 if (regs->pc == restart_addr) {
284 if (retval == -ERESTARTNOHAND ||
285 (retval == -ERESTARTSYS &&
286 !(ksig.ka.sa.sa_flags & SA_RESTART))) {
287 regs->a0 = -EINTR;
288 regs->pc = continue_addr;
289 }
290 }
291
292 /* Whee! Actually deliver the signal. */
293 if (handle_signal(&ksig, regs) == 0) {
294 /*
295 * A signal was successfully delivered; the saved
296 * sigmask will have been stored in the signal frame,
297 * and will be restored by sigreturn, so we can simply
298 * clear the TIF_RESTORE_SIGMASK flag.
299 */
300 if (test_thread_flag(TIF_RESTORE_SIGMASK))
301 clear_thread_flag(TIF_RESTORE_SIGMASK);
302 }
303 return;
304 }
305
306 no_signal:
307 if (syscall) {
308 /*
309 * Handle restarting a different system call. As above,
310 * if a debugger has chosen to restart at a different PC,
311 * ignore the restart.
312 */
313 if (retval == -ERESTART_RESTARTBLOCK
314 && regs->pc == continue_addr) {
315 #if defined(__CSKYABIV2__)
316 regs->regs[3] = __NR_restart_syscall;
317 regs->pc -= 4;
318 #else
319 regs->regs[9] = __NR_restart_syscall;
320 regs->pc -= 2;
321 #endif
322 }
323
324 /*
325 * If there's no signal to deliver, we just put the saved
326 * sigmask back.
327 */
328 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
329 clear_thread_flag(TIF_RESTORE_SIGMASK);
330 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
331 }
332 }
333 }
334
335 asmlinkage void
336 do_notify_resume(unsigned int thread_flags, struct pt_regs *regs, int syscall)
337 {
338 if (thread_flags & _TIF_SIGPENDING)
339 do_signal(regs, syscall);
340
341 if (thread_flags & _TIF_NOTIFY_RESUME) {
342 clear_thread_flag(TIF_NOTIFY_RESUME);
343 tracehook_notify_resume(regs);
344 }
345 }