Merge tag 'for-3.14' of git://openrisc.net/~jonas/linux

Pull OpenRISC updates from Jonas Bonn:
"The interesting change here is a rework of the OpenRISC signal
handling to make it more like other architectures in the hopes that
this makes it easier for others to comment on and understand. This
rework fixes some real bugs, like the fact that syscall restart did
not work reliably"

* tag 'for-3.14' of git://openrisc.net/~jonas/linux:
openrisc: Use get_signal() signal_setup_done()
openrisc: Rework signal handling

+155 -149
+35 -28
arch/openrisc/kernel/entry.S
··· 853 853 854 854 /* ========================================================[ return ] === */ 855 855 856 - _work_pending: 857 - /* 858 - * if (current_thread_info->flags & _TIF_NEED_RESCHED) 859 - * schedule(); 860 - */ 861 - l.lwz r5,TI_FLAGS(r10) 862 - l.andi r3,r5,_TIF_NEED_RESCHED 863 - l.sfnei r3,0 864 - l.bnf _work_notifysig 865 - l.nop 866 - l.jal schedule 867 - l.nop 868 - l.j _resume_userspace 869 - l.nop 870 - 871 - /* Handle pending signals and notify-resume requests. 872 - * do_notify_resume must be passed the latest pushed pt_regs, not 873 - * necessarily the "userspace" ones. Also, pt_regs->syscallno 874 - * must be set so that the syscall restart functionality works. 875 - */ 876 - _work_notifysig: 877 - l.jal do_notify_resume 878 - l.ori r3,r1,0 /* pt_regs */ 879 - 880 856 _resume_userspace: 881 857 DISABLE_INTERRUPTS(r3,r4) 882 - l.lwz r3,TI_FLAGS(r10) 883 - l.andi r3,r3,_TIF_WORK_MASK 884 - l.sfnei r3,0 885 - l.bf _work_pending 858 + l.lwz r4,TI_FLAGS(r10) 859 + l.andi r13,r4,_TIF_WORK_MASK 860 + l.sfeqi r13,0 861 + l.bf _restore_all 886 862 l.nop 863 + 864 + _work_pending: 865 + l.lwz r5,PT_ORIG_GPR11(r1) 866 + l.sfltsi r5,0 867 + l.bnf 1f 868 + l.nop 869 + l.andi r5,r5,0 870 + 1: 871 + l.jal do_work_pending 872 + l.ori r3,r1,0 /* pt_regs */ 873 + 874 + l.sfeqi r11,0 875 + l.bf _restore_all 876 + l.nop 877 + l.sfltsi r11,0 878 + l.bnf 1f 879 + l.nop 880 + l.and r11,r11,r0 881 + l.ori r11,r11,__NR_restart_syscall 882 + l.j _syscall_check_trace_enter 883 + l.nop 884 + 1: 885 + l.lwz r11,PT_ORIG_GPR11(r1) 886 + /* Restore arg registers */ 887 + l.lwz r3,PT_GPR3(r1) 888 + l.lwz r4,PT_GPR4(r1) 889 + l.lwz r5,PT_GPR5(r1) 890 + l.lwz r6,PT_GPR6(r1) 891 + l.lwz r7,PT_GPR7(r1) 892 + l.j _syscall_check_trace_enter 893 + l.lwz r8,PT_GPR8(r1) 887 894 888 895 _restore_all: 889 896 RESTORE_ALL
+120 -121
arch/openrisc/kernel/signal.c
··· 28 28 #include <linux/tracehook.h> 29 29 30 30 #include <asm/processor.h> 31 + #include <asm/syscall.h> 31 32 #include <asm/ucontext.h> 32 33 #include <asm/uaccess.h> 33 34 34 35 #define DEBUG_SIG 0 35 36 36 37 struct rt_sigframe { 37 - struct siginfo *pinfo; 38 - void *puc; 39 38 struct siginfo info; 40 39 struct ucontext uc; 41 40 unsigned char retcode[16]; /* trampoline code */ 42 41 }; 43 42 44 - static int restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc) 43 + static int restore_sigcontext(struct pt_regs *regs, 44 + struct sigcontext __user *sc) 45 45 { 46 - unsigned int err = 0; 46 + int err = 0; 47 47 48 - /* Alwys make any pending restarted system call return -EINTR */ 48 + /* Always make any pending restarted system calls return -EINTR */ 49 49 current_thread_info()->restart_block.fn = do_no_restart_syscall; 50 50 51 51 /* ··· 53 53 * (sc is already checked for VERIFY_READ since the sigframe was 54 54 * checked in sys_sigreturn previously) 55 55 */ 56 - if (__copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long))) 57 - goto badframe; 58 - if (__copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long))) 59 - goto badframe; 60 - if (__copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long))) 61 - goto badframe; 56 + err |= __copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long)); 57 + err |= __copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long)); 58 + err |= __copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long)); 62 59 63 60 /* make sure the SM-bit is cleared so user-mode cannot fool us */ 64 61 regs->sr &= ~SPR_SR_SM; 62 + 63 + regs->orig_gpr11 = -1; /* Avoid syscall restart checks */ 65 64 66 65 /* TODO: the other ports use regs->orig_XX to disable syscall checks 67 66 * after this completes, but we don't use that mechanism. maybe we can ··· 68 69 */ 69 70 70 71 return err; 71 - 72 - badframe: 73 - return 1; 74 72 } 75 73 76 74 asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs) ··· 107 111 * Set up a signal frame. 108 112 */ 109 113 110 - static int setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, 111 - unsigned long mask) 114 + static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) 112 115 { 113 116 int err = 0; 114 117 115 118 /* copy the regs */ 116 - 119 + /* There should be no need to save callee-saved registers here... 120 + * ...but we save them anyway. Revisit this 121 + */ 117 122 err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long)); 118 123 err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long)); 119 124 err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long)); 120 - 121 - /* then some other stuff */ 122 - 123 - err |= __put_user(mask, &sc->oldmask); 124 125 125 126 return err; 126 127 } ··· 166 173 * trampoline which performs the syscall sigreturn, or a provided 167 174 * user-mode trampoline. 168 175 */ 169 - static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 170 - sigset_t *set, struct pt_regs *regs) 176 + static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, 177 + struct pt_regs *regs) 171 178 { 172 179 struct rt_sigframe *frame; 173 180 unsigned long return_ip; 174 181 int err = 0; 175 182 176 - frame = get_sigframe(ka, regs, sizeof(*frame)); 183 + frame = get_sigframe(&ksig->ka, regs, sizeof(*frame)); 177 184 178 185 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 179 - goto give_sigsegv; 186 + return -EFAULT; 180 187 181 - err |= __put_user(&frame->info, &frame->pinfo); 182 - err |= __put_user(&frame->uc, &frame->puc); 188 + /* Create siginfo. */ 189 + if (ksig->ka.sa.sa_flags & SA_SIGINFO) 190 + err |= copy_siginfo_to_user(&frame->info, &ksig->info); 183 191 184 - if (ka->sa.sa_flags & SA_SIGINFO) 185 - err |= copy_siginfo_to_user(&frame->info, info); 186 - if (err) 187 - goto give_sigsegv; 188 - 189 - /* Clear all the bits of the ucontext we don't use. */ 190 - err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext)); 192 + /* Create the ucontext. */ 191 193 err |= __put_user(0, &frame->uc.uc_flags); 192 194 err |= __put_user(NULL, &frame->uc.uc_link); 193 195 err |= __save_altstack(&frame->uc.uc_stack, regs->sp); 194 - err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]); 196 + err |= setup_sigcontext(regs, &frame->uc.uc_mcontext); 195 197 196 198 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 197 199 198 200 if (err) 199 - goto give_sigsegv; 201 + return -EFAULT; 200 202 201 203 /* trampoline - the desired return ip is the retcode itself */ 202 204 return_ip = (unsigned long)&frame->retcode; 203 - /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */ 204 - err |= __put_user(0xa960, (short *)(frame->retcode + 0)); 205 - err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2)); 205 + /* This is: 206 + l.ori r11,r0,__NR_sigreturn 207 + l.sys 1 208 + */ 209 + err |= __put_user(0xa960, (short *)(frame->retcode + 0)); 210 + err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2)); 206 211 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4)); 207 212 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8)); 208 213 209 214 if (err) 210 - goto give_sigsegv; 215 + return -EFAULT; 211 216 212 217 /* TODO what is the current->exec_domain stuff and invmap ? */ 213 218 214 219 /* Set up registers for signal handler */ 215 - regs->pc = (unsigned long)ka->sa.sa_handler; /* what we enter NOW */ 220 + regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* what we enter NOW */ 216 221 regs->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */ 217 - regs->gpr[3] = (unsigned long)sig; /* arg 1: signo */ 222 + regs->gpr[3] = (unsigned long)ksig->sig; /* arg 1: signo */ 218 223 regs->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */ 219 224 regs->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */ 220 225 ··· 220 229 regs->sp = (unsigned long)frame; 221 230 222 231 return 0; 223 - 224 - give_sigsegv: 225 - force_sigsegv(sig, current); 226 - return -EFAULT; 227 232 } 228 233 229 234 static inline void 230 - handle_signal(unsigned long sig, 231 - siginfo_t *info, struct k_sigaction *ka, 232 - struct pt_regs *regs) 235 + handle_signal(struct ksignal *ksig, struct pt_regs *regs) 233 236 { 234 237 int ret; 235 238 236 - ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs); 237 - if (ret) 238 - return; 239 + ret = setup_rt_frame(ksig, sigmask_to_save(), regs); 239 240 240 - signal_delivered(sig, info, ka, regs, 241 - test_thread_flag(TIF_SINGLESTEP)); 241 + signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); 242 242 } 243 243 244 244 /* ··· 244 262 * mode below. 245 263 */ 246 264 247 - void do_signal(struct pt_regs *regs) 265 + int do_signal(struct pt_regs *regs, int syscall) 248 266 { 249 - siginfo_t info; 250 - int signr; 251 - struct k_sigaction ka; 267 + struct ksignal ksig; 268 + unsigned long continue_addr = 0; 269 + unsigned long restart_addr = 0; 270 + unsigned long retval = 0; 271 + int restart = 0; 272 + 273 + if (syscall) { 274 + continue_addr = regs->pc; 275 + restart_addr = continue_addr - 4; 276 + retval = regs->gpr[11]; 277 + 278 + /* 279 + * Setup syscall restart here so that a debugger will 280 + * see the already changed PC. 281 + */ 282 + switch (retval) { 283 + case -ERESTART_RESTARTBLOCK: 284 + restart = -2; 285 + /* Fall through */ 286 + case -ERESTARTNOHAND: 287 + case -ERESTARTSYS: 288 + case -ERESTARTNOINTR: 289 + restart++; 290 + regs->gpr[11] = regs->orig_gpr11; 291 + regs->pc = restart_addr; 292 + break; 293 + } 294 + } 252 295 253 296 /* 254 - * We want the common case to go fast, which 255 - * is why we may in certain cases get here from 256 - * kernel mode. Just return without doing anything 257 - * if so. 297 + * Get the signal to deliver. During the call to get_signal the 298 + * debugger may change all our registers so we may need to revert 299 + * the decision to restart the syscall; specifically, if the PC is 300 + * changed, don't restart the syscall. 258 301 */ 259 - if (!user_mode(regs)) 260 - return; 261 - 262 - signr = get_signal_to_deliver(&info, &ka, regs, NULL); 263 - 264 - /* If we are coming out of a syscall then we need 265 - * to check if the syscall was interrupted and wants to be 266 - * restarted after handling the signal. If so, the original 267 - * syscall number is put back into r11 and the PC rewound to 268 - * point at the l.sys instruction that resulted in the 269 - * original syscall. Syscall results other than the four 270 - * below mean that the syscall executed to completion and no 271 - * restart is necessary. 272 - */ 273 - if (regs->orig_gpr11) { 274 - int restart = 0; 275 - 276 - switch (regs->gpr[11]) { 277 - case -ERESTART_RESTARTBLOCK: 278 - case -ERESTARTNOHAND: 279 - /* Restart if there is no signal handler */ 280 - restart = (signr <= 0); 281 - break; 282 - case -ERESTARTSYS: 283 - /* Restart if there no signal handler or 284 - * SA_RESTART flag is set */ 285 - restart = (signr <= 0 || (ka.sa.sa_flags & SA_RESTART)); 286 - break; 287 - case -ERESTARTNOINTR: 288 - /* Always restart */ 289 - restart = 1; 290 - break; 302 + if (get_signal(&ksig)) { 303 + if (unlikely(restart) && regs->pc == restart_addr) { 304 + if (retval == -ERESTARTNOHAND || 305 + retval == -ERESTART_RESTARTBLOCK 306 + || (retval == -ERESTARTSYS 307 + && !(ksig.ka.sa.sa_flags & SA_RESTART))) { 308 + /* No automatic restart */ 309 + regs->gpr[11] = -EINTR; 310 + regs->pc = continue_addr; 311 + } 291 312 } 292 - 293 - if (restart) { 294 - if (regs->gpr[11] == -ERESTART_RESTARTBLOCK) 295 - regs->gpr[11] = __NR_restart_syscall; 296 - else 297 - regs->gpr[11] = regs->orig_gpr11; 298 - regs->pc -= 4; 299 - } else { 300 - regs->gpr[11] = -EINTR; 301 - } 302 - } 303 - 304 - if (signr <= 0) { 305 - /* no signal to deliver so we just put the saved sigmask 306 - * back */ 313 + handle_signal(&ksig, regs); 314 + } else { 315 + /* no handler */ 307 316 restore_saved_sigmask(); 308 - } else { /* signr > 0 */ 309 - /* Whee! Actually deliver the signal. */ 310 - handle_signal(signr, &info, &ka, regs); 317 + /* 318 + * Restore pt_regs PC as syscall restart will be handled by 319 + * kernel without return to userspace 320 + */ 321 + if (unlikely(restart) && regs->pc == restart_addr) { 322 + regs->pc = continue_addr; 323 + return restart; 324 + } 311 325 } 312 326 313 - return; 327 + return 0; 314 328 } 315 329 316 - asmlinkage void do_notify_resume(struct pt_regs *regs) 330 + asmlinkage int 331 + do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall) 317 332 { 318 - if (current_thread_info()->flags & _TIF_SIGPENDING) 319 - do_signal(regs); 320 - 321 - if (current_thread_info()->flags & _TIF_NOTIFY_RESUME) { 322 - clear_thread_flag(TIF_NOTIFY_RESUME); 323 - tracehook_notify_resume(regs); 324 - } 333 + do { 334 + if (likely(thread_flags & _TIF_NEED_RESCHED)) { 335 + schedule(); 336 + } else { 337 + if (unlikely(!user_mode(regs))) 338 + return 0; 339 + local_irq_enable(); 340 + if (thread_flags & _TIF_SIGPENDING) { 341 + int restart = do_signal(regs, syscall); 342 + if (unlikely(restart)) { 343 + /* 344 + * Restart without handlers. 345 + * Deal with it without leaving 346 + * the kernel space. 347 + */ 348 + return restart; 349 + } 350 + syscall = 0; 351 + } else { 352 + clear_thread_flag(TIF_NOTIFY_RESUME); 353 + tracehook_notify_resume(regs); 354 + } 355 + } 356 + local_irq_disable(); 357 + thread_flags = current_thread_info()->flags; 358 + } while (thread_flags & _TIF_WORK_MASK); 359 + return 0; 325 360 }