]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - arch/csky/kernel/signal.c
66e1b729b10b741d63cf841c0fa0bd733a3e4f3f
[thirdparty/kernel/linux.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 (verify_area(VERIFY_READ, 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 current->thread.esp0 = (unsigned long)regs;
242
243 /*
244 * If we were from a system call, check for system call restarting...
245 */
246 if (syscall) {
247 continue_addr = regs->pc;
248 #if defined(__CSKYABIV2__)
249 restart_addr = continue_addr - 4;
250 #else
251 restart_addr = continue_addr - 2;
252 #endif
253 retval = regs->a0;
254
255 /*
256 * Prepare for system call restart. We do this here so that a
257 * debugger will see the already changed.
258 */
259 switch (retval) {
260 case -ERESTARTNOHAND:
261 case -ERESTARTSYS:
262 case -ERESTARTNOINTR:
263 regs->a0 = regs->orig_a0;
264 regs->pc = restart_addr;
265 break;
266 case -ERESTART_RESTARTBLOCK:
267 regs->a0 = -EINTR;
268 break;
269 }
270 }
271
272 if (try_to_freeze())
273 goto no_signal;
274
275 /*
276 * Get the signal to deliver. When running under ptrace, at this
277 * point the debugger may change all our registers ...
278 */
279 if (get_signal(&ksig)) {
280 /*
281 * Depending on the signal settings we may need to revert the
282 * decision to restart the system call. But skip this if a
283 * debugger has chosen to restart at a different PC.
284 */
285 if (regs->pc == restart_addr) {
286 if (retval == -ERESTARTNOHAND ||
287 (retval == -ERESTARTSYS &&
288 !(ksig.ka.sa.sa_flags & SA_RESTART))) {
289 regs->a0 = -EINTR;
290 regs->pc = continue_addr;
291 }
292 }
293
294 /* Whee! Actually deliver the signal. */
295 if (handle_signal(&ksig, regs) == 0) {
296 /*
297 * A signal was successfully delivered; the saved
298 * sigmask will have been stored in the signal frame,
299 * and will be restored by sigreturn, so we can simply
300 * clear the TIF_RESTORE_SIGMASK flag.
301 */
302 if (test_thread_flag(TIF_RESTORE_SIGMASK))
303 clear_thread_flag(TIF_RESTORE_SIGMASK);
304 }
305 return;
306 }
307
308 no_signal:
309 if (syscall) {
310 /*
311 * Handle restarting a different system call. As above,
312 * if a debugger has chosen to restart at a different PC,
313 * ignore the restart.
314 */
315 if (retval == -ERESTART_RESTARTBLOCK
316 && regs->pc == continue_addr) {
317 #if defined(__CSKYABIV2__)
318 regs->regs[3] = __NR_restart_syscall;
319 regs->pc -= 4;
320 #else
321 regs->regs[9] = __NR_restart_syscall;
322 regs->pc -= 2;
323 #endif
324 }
325
326 /*
327 * If there's no signal to deliver, we just put the saved
328 * sigmask back.
329 */
330 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
331 clear_thread_flag(TIF_RESTORE_SIGMASK);
332 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
333 }
334 }
335 }
336
337 asmlinkage void
338 do_notify_resume(unsigned int thread_flags, struct pt_regs *regs, int syscall)
339 {
340 if (thread_flags & _TIF_SIGPENDING)
341 do_signal(regs, syscall);
342
343 if (thread_flags & _TIF_NOTIFY_RESUME) {
344 clear_thread_flag(TIF_NOTIFY_RESUME);
345 tracehook_notify_resume(regs);
346 }
347 }