1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/sched.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>
22 #include <asm/setup.h>
23 #include <asm/pgtable.h>
24 #include <asm/traps.h>
25 #include <asm/ucontext.h>
28 #include <abi/regdef.h>
30 #ifdef CONFIG_CPU_HAS_FPU
33 static int restore_fpu_state(struct sigcontext *sc)
36 struct user_fp user_fp;
38 err = copy_from_user(&user_fp, &sc->sc_user_fp, sizeof(user_fp));
40 restore_from_user_fp(&user_fp);
45 static int save_fpu_state(struct sigcontext *sc)
47 struct user_fp user_fp;
49 save_to_user_fp(&user_fp);
51 return copy_to_user(&sc->sc_user_fp, &user_fp, sizeof(user_fp));
54 static inline int restore_fpu_state(struct sigcontext *sc) { return 0; }
55 static inline int save_fpu_state(struct sigcontext *sc) { return 0; }
60 struct siginfo *pinfo;
67 restore_sigframe(struct pt_regs *regs,
68 struct sigcontext *sc, int *pr2)
72 /* Always make any pending restarted system calls return -EINTR */
73 current_thread_info()->task->restart_block.fn = do_no_restart_syscall;
75 err |= copy_from_user(regs, &sc->sc_pt_regs, sizeof(struct pt_regs));
77 err |= restore_fpu_state(sc);
88 struct pt_regs *regs = current_pt_regs();
89 struct rt_sigframe *frame = (struct rt_sigframe *)(regs->usp);
91 if (!access_ok(frame, sizeof(*frame)))
93 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
96 sigdelsetmask(&set, (sigmask(SIGKILL) | sigmask(SIGSTOP)));
97 spin_lock_irq(¤t->sighand->siglock);
98 current->blocked = set;
100 spin_unlock_irq(¤t->sighand->siglock);
102 if (restore_sigframe(regs, &frame->uc.uc_mcontext, &a0))
108 force_sig(SIGSEGV, current);
112 static int setup_sigframe(struct sigcontext *sc, struct pt_regs *regs)
116 err |= copy_to_user(&sc->sc_pt_regs, regs, sizeof(struct pt_regs));
117 err |= save_fpu_state(sc);
123 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
127 /* Default to using normal stack. */
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;
135 return (void *)((usp - frame_size) & -8UL);
139 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
141 struct rt_sigframe *frame;
144 struct csky_vdso *vdso = current->mm->context.vdso;
146 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
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);
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));
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;
175 regs->a0 = ksig->sig; /* first arg is signo */
176 regs->a1 = (unsigned long)(&(frame->info));
177 regs->a2 = (unsigned long)(&(frame->uc));
181 if (ksig->sig == SIGSEGV)
182 ksig->ka.sa.sa_handler = SIG_DFL;
183 force_sig(SIGSEGV, current);
188 * OK, we're invoking a handler
191 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
194 sigset_t *oldset = sigmask_to_save();
197 * set up the stack frame, regardless of SA_SIGINFO,
198 * and pass info anyway.
200 ret = setup_rt_frame(ksig, oldset, regs);
203 force_sigsegv(ksig->sig, current);
207 /* Block the signal if we were successful. */
208 spin_lock_irq(¤t->sighand->siglock);
209 sigorsets(¤t->blocked, ¤t->blocked, &ksig->ka.sa.sa_mask);
210 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
211 sigaddset(¤t->blocked, ksig->sig);
213 spin_unlock_irq(¤t->sighand->siglock);
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
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.
227 static void do_signal(struct pt_regs *regs, int syscall)
229 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
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
238 if (!user_mode(regs))
242 * If we were from a system call, check for system call restarting...
245 continue_addr = regs->pc;
246 #if defined(__CSKYABIV2__)
247 restart_addr = continue_addr - 4;
249 restart_addr = continue_addr - 2;
254 * Prepare for system call restart. We do this here so that a
255 * debugger will see the already changed.
258 case -ERESTARTNOHAND:
260 case -ERESTARTNOINTR:
261 regs->a0 = regs->orig_a0;
262 regs->pc = restart_addr;
264 case -ERESTART_RESTARTBLOCK:
274 * Get the signal to deliver. When running under ptrace, at this
275 * point the debugger may change all our registers ...
277 if (get_signal(&ksig)) {
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.
283 if (regs->pc == restart_addr) {
284 if (retval == -ERESTARTNOHAND ||
285 (retval == -ERESTARTSYS &&
286 !(ksig.ka.sa.sa_flags & SA_RESTART))) {
288 regs->pc = continue_addr;
292 /* Whee! Actually deliver the signal. */
293 if (handle_signal(&ksig, regs) == 0) {
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.
300 if (test_thread_flag(TIF_RESTORE_SIGMASK))
301 clear_thread_flag(TIF_RESTORE_SIGMASK);
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.
313 if (retval == -ERESTART_RESTARTBLOCK
314 && regs->pc == continue_addr) {
315 #if defined(__CSKYABIV2__)
316 regs->regs[3] = __NR_restart_syscall;
319 regs->regs[9] = __NR_restart_syscall;
325 * If there's no signal to deliver, we just put the saved
328 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
329 clear_thread_flag(TIF_RESTORE_SIGMASK);
330 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
336 do_notify_resume(unsigned int thread_flags, struct pt_regs *regs, int syscall)
338 if (thread_flags & _TIF_SIGPENDING)
339 do_signal(regs, syscall);
341 if (thread_flags & _TIF_NOTIFY_RESUME) {
342 clear_thread_flag(TIF_NOTIFY_RESUME);
343 tracehook_notify_resume(regs);