1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/signal.h> 4 #include <linux/uaccess.h> 5 #include <linux/syscalls.h> 6 #include <linux/tracehook.h> 7 8 #include <asm/traps.h> 9 #include <asm/ucontext.h> 10 #include <asm/vdso.h> 11 12 #include <abi/regdef.h> 13 14 #ifdef CONFIG_CPU_HAS_FPU 15 #include <abi/fpu.h> 16 static int restore_fpu_state(struct sigcontext __user *sc) 17 { 18 int err = 0; 19 struct user_fp user_fp; 20 21 err = __copy_from_user(&user_fp, &sc->sc_user_fp, sizeof(user_fp)); 22 23 restore_from_user_fp(&user_fp); 24 25 return err; 26 } 27 28 static int save_fpu_state(struct sigcontext __user *sc) 29 { 30 struct user_fp user_fp; 31 32 save_to_user_fp(&user_fp); 33 34 return __copy_to_user(&sc->sc_user_fp, &user_fp, sizeof(user_fp)); 35 } 36 #else 37 #define restore_fpu_state(sigcontext) (0) 38 #define save_fpu_state(sigcontext) (0) 39 #endif 40 41 struct rt_sigframe { 42 /* 43 * pad[3] is compatible with the same struct defined in 44 * gcc/libgcc/config/csky/linux-unwind.h 45 */ 46 int pad[3]; 47 struct siginfo info; 48 struct ucontext uc; 49 }; 50 51 static long restore_sigcontext(struct pt_regs *regs, 52 struct sigcontext __user *sc) 53 { 54 int err = 0; 55 56 /* sc_pt_regs is structured the same as the start of pt_regs */ 57 err |= __copy_from_user(regs, &sc->sc_pt_regs, sizeof(struct pt_regs)); 58 59 /* Restore the floating-point state. */ 60 err |= restore_fpu_state(sc); 61 62 return err; 63 } 64 65 SYSCALL_DEFINE0(rt_sigreturn) 66 { 67 struct pt_regs *regs = current_pt_regs(); 68 struct rt_sigframe __user *frame; 69 struct task_struct *task; 70 sigset_t set; 71 72 /* Always make any pending restarted system calls return -EINTR */ 73 current->restart_block.fn = do_no_restart_syscall; 74 75 frame = (struct rt_sigframe __user *)regs->usp; 76 77 if (!access_ok(frame, sizeof(*frame))) 78 goto badframe; 79 80 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 81 goto badframe; 82 83 set_current_blocked(&set); 84 85 if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) 86 goto badframe; 87 88 if (restore_altstack(&frame->uc.uc_stack)) 89 goto badframe; 90 91 return regs->a0; 92 93 badframe: 94 task = current; 95 force_sig(SIGSEGV, task); 96 return 0; 97 } 98 99 static int setup_sigcontext(struct rt_sigframe __user *frame, 100 struct pt_regs *regs) 101 { 102 struct sigcontext __user *sc = &frame->uc.uc_mcontext; 103 int err = 0; 104 105 err |= __copy_to_user(&sc->sc_pt_regs, regs, sizeof(struct pt_regs)); 106 err |= save_fpu_state(sc); 107 108 return err; 109 } 110 111 static inline void __user *get_sigframe(struct ksignal *ksig, 112 struct pt_regs *regs, size_t framesize) 113 { 114 unsigned long sp; 115 /* Default to using normal stack */ 116 sp = regs->usp; 117 118 /* 119 * If we are on the alternate signal stack and would overflow it, don't. 120 * Return an always-bogus address instead so we will die with SIGSEGV. 121 */ 122 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) 123 return (void __user __force *)(-1UL); 124 125 /* This is the X/Open sanctioned signal stack switching. */ 126 sp = sigsp(sp, ksig) - framesize; 127 128 /* Align the stack frame. */ 129 sp &= -8UL; 130 131 return (void __user *)sp; 132 } 133 134 static int 135 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs) 136 { 137 struct rt_sigframe *frame; 138 int err = 0; 139 struct csky_vdso *vdso = current->mm->context.vdso; 140 141 frame = get_sigframe(ksig, regs, sizeof(*frame)); 142 if (!access_ok(frame, sizeof(*frame))) 143 return -EFAULT; 144 145 err |= copy_siginfo_to_user(&frame->info, &ksig->info); 146 147 /* Create the ucontext. */ 148 err |= __put_user(0, &frame->uc.uc_flags); 149 err |= __put_user(NULL, &frame->uc.uc_link); 150 err |= __save_altstack(&frame->uc.uc_stack, regs->usp); 151 err |= setup_sigcontext(frame, regs); 152 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 153 if (err) 154 return -EFAULT; 155 156 /* Set up to return from userspace. */ 157 regs->lr = (unsigned long)(vdso->rt_signal_retcode); 158 159 /* 160 * Set up registers for signal handler. 161 * Registers that we don't modify keep the value they had from 162 * user-space at the time we took the signal. 163 * We always pass siginfo and mcontext, regardless of SA_SIGINFO, 164 * since some things rely on this (e.g. glibc's debug/segfault.c). 165 */ 166 regs->pc = (unsigned long)ksig->ka.sa.sa_handler; 167 regs->usp = (unsigned long)frame; 168 regs->a0 = ksig->sig; /* a0: signal number */ 169 regs->a1 = (unsigned long)(&(frame->info)); /* a1: siginfo pointer */ 170 regs->a2 = (unsigned long)(&(frame->uc)); /* a2: ucontext pointer */ 171 172 return 0; 173 } 174 175 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) 176 { 177 sigset_t *oldset = sigmask_to_save(); 178 int ret; 179 180 /* Are we from a system call? */ 181 if (in_syscall(regs)) { 182 /* Avoid additional syscall restarting via ret_from_exception */ 183 forget_syscall(regs); 184 185 /* If so, check system call restarting.. */ 186 switch (regs->a0) { 187 case -ERESTART_RESTARTBLOCK: 188 case -ERESTARTNOHAND: 189 regs->a0 = -EINTR; 190 break; 191 192 case -ERESTARTSYS: 193 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { 194 regs->a0 = -EINTR; 195 break; 196 } 197 /* fallthrough */ 198 case -ERESTARTNOINTR: 199 regs->a0 = regs->orig_a0; 200 regs->pc -= TRAP0_SIZE; 201 break; 202 } 203 } 204 205 /* Set up the stack frame */ 206 ret = setup_rt_frame(ksig, oldset, regs); 207 208 signal_setup_done(ret, ksig, 0); 209 } 210 211 static void do_signal(struct pt_regs *regs) 212 { 213 struct ksignal ksig; 214 215 if (get_signal(&ksig)) { 216 /* Actually deliver the signal */ 217 handle_signal(&ksig, regs); 218 return; 219 } 220 221 /* Did we come from a system call? */ 222 if (in_syscall(regs)) { 223 /* Avoid additional syscall restarting via ret_from_exception */ 224 forget_syscall(regs); 225 226 /* Restart the system call - no handlers present */ 227 switch (regs->a0) { 228 case -ERESTARTNOHAND: 229 case -ERESTARTSYS: 230 case -ERESTARTNOINTR: 231 regs->a0 = regs->orig_a0; 232 regs->pc -= TRAP0_SIZE; 233 break; 234 case -ERESTART_RESTARTBLOCK: 235 regs->a0 = regs->orig_a0; 236 regs_syscallid(regs) = __NR_restart_syscall; 237 regs->pc -= TRAP0_SIZE; 238 break; 239 } 240 } 241 242 /* 243 * If there is no signal to deliver, we just put the saved 244 * sigmask back. 245 */ 246 restore_saved_sigmask(); 247 } 248 249 /* 250 * notification of userspace execution resumption 251 * - triggered by the _TIF_WORK_MASK flags 252 */ 253 asmlinkage void do_notify_resume(struct pt_regs *regs, 254 unsigned long thread_info_flags) 255 { 256 /* Handle pending signal delivery */ 257 if (thread_info_flags & _TIF_SIGPENDING) 258 do_signal(regs); 259 260 if (thread_info_flags & _TIF_NOTIFY_RESUME) { 261 clear_thread_flag(TIF_NOTIFY_RESUME); 262 tracehook_notify_resume(regs); 263 } 264 } 265