Skip to contentSkip to conversation

powerpc: Make syscall restart code more common

PATCH In review Benjamin Herrenschmidt 2 participants 3 messages May 30, 2007 – Jun 2, 2007 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 4/4 powerpc: Make syscall restart code more common

This patch moves the code in signal_32.c and signal_64.c for handling syscall restart into a common signal-common.h file and converge around a single implementation that is based on the 32 bits one, using trap, ccr and r3 rather than the special "result" field for deciding what to do.

The "result" field is now pretty much deprecated. We still set it for the sake of whatever might rely on it in userland but we no longer use it's content.

This, along with a previous patch that enables ptracers to write to "trap" and "orig_r3" should allow gdb to properly handle syscall restarting.

There is only one common function for now and it's small enough so having it inline is fine. However, there is probably room for more merging of _32.c and _64.c in which case we'll probably want to have a common .c file and avoid inlines.

---

 arch/powerpc/kernel/signal-common.h |   66 ++++++++++++++++++++++++++++++++++++
 arch/powerpc/kernel/signal_32.c     |   28 ++-------------
 arch/powerpc/kernel/signal_64.c     |   59 +++-----------------------------
 3 files changed, 77 insertions(+), 76 deletions(-)
arch/powerpc/kernel/signal-common.h new file +66 -0
Index: linux-cell/arch/powerpc/kernel/signal-common.h
===================================================================
@@ -0,0 +1,66 @@
+/*
+ *    Copyright (c) 2007 Benjamin Herrenschmidt, IBM Coproration
+ *    Extracted from signal_32.c and signal_64.c
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License.  See the file README.legal in the main directory of
+ * this archive for more details.
+ */
+
+#ifndef _POWERPC_SIGNAL_COMMON_H
+#define _POWERPC_SIGNAL_COMMON_H
+
+static inline void check_syscall_restart(struct pt_regs *regs,
+					 struct k_sigaction *ka,
+					 int has_handler)
+{
+	unsigned long ret = regs->gpr[3];
+	int restart = 1;
+
+	/* syscall ? */
+	if (TRAP(regs) != 0x0C00)
+		return;
+
+	/* error signalled ? */
+	if (!(regs->ccr & 0x10000000))
+		return;
+
+	switch (ret) {
+	case ERESTART_RESTARTBLOCK:
+	case ERESTARTNOHAND:
+		/* ERESTARTNOHAND means that the syscall should only be
+		 * restarted if there was no handler for the signal, and since
+		 * we only get here if there is a handler, we dont restart.
+		 */
+		restart = !has_handler;
+		break;
+	case ERESTARTSYS:
+		/* ERESTARTSYS means to restart the syscall if there is no
+		 * handler or the handler was registered with SA_RESTART
+		 */
+		restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
+		break;
+	case ERESTARTNOINTR:
+		/* ERESTARTNOINTR means that the syscall should be
+		 * called again after the signal handler returns.
+		 */
+		break;
+	default:
+		return;
+	}
+	if (restart) {
+		if (ret == ERESTART_RESTARTBLOCK)
+			regs->gpr[0] = __NR_restart_syscall;
+		else
+			regs->gpr[3] = regs->orig_gpr3;
+		regs->nip -= 4;
+		regs->result = 0;
+	} else {
+		regs->result = -EINTR;
+		regs->gpr[3] = EINTR;
+		regs->ccr |= 0x10000000;
+	}
+}
+
+
+#endif /* _POWERPC_SIGNAL_COMMON_H */
arch/powerpc/kernel/signal_32.c +4 -24
Index: linux-cell/arch/powerpc/kernel/signal_32.c
===================================================================
@@ -51,6 +51,8 @@
 #include <asm/pgtable.h>
 #endif
 
+#include "signal-common.h"
+
 #undef DEBUG_SIG
 
 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
@@ -1156,30 +1158,8 @@ int do_signal(sigset_t *oldset, struct p
 #ifdef CONFIG_PPC32
 no_signal:
 #endif
-	if (TRAP(regs) == 0x0C00		/* System Call! */
-	    && regs->ccr & 0x10000000		/* error signalled */
-	    && ((ret = regs->gpr[3]) == ERESTARTSYS
-		|| ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
-		|| ret == ERESTART_RESTARTBLOCK)) {
-
-		if (signr > 0
-		    && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
-			|| (ret == ERESTARTSYS
-			    && !(ka.sa.sa_flags & SA_RESTART)))) {
-			/* make the system call return an EINTR error */
-			regs->result = -EINTR;
-			regs->gpr[3] = EINTR;
-			/* note that the cr0.SO bit is already set */
-		} else {
-			regs->nip -= 4;	/* Back up & retry system call */
-			regs->result = 0;
-			regs->trap = 0;
-			if (ret == ERESTART_RESTARTBLOCK)
-				regs->gpr[0] = __NR_restart_syscall;
-			else
-				regs->gpr[3] = regs->orig_gpr3;
-		}
-	}
+	/* Is there any syscall restart business here ? */
+	check_syscall_restart(regs, &ka, signr > 0);
 
 	if (signr == 0) {
 		/* No signal to deliver -- put the saved sigmask back */
arch/powerpc/kernel/signal_64.c +7 -52

92 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Benjamin Herrenschmidt

This patch moves the code in signal_32.c and signal_64.c for handling syscall restart into a common signal-common.h file and converge around a single implementation that is based on the 32 bits one, using trap, ccr and r3 rather than the special "result" field for deciding what to do.

The "result" field is now pretty much deprecated. We still set it for the sake of whatever might rely on it in userland but we no longer use it's content.

Quoted 8 lines

This, along with a previous patch that enables ptracers to write to "trap" and "orig_r3" should allow gdb to properly handle syscall restarting.

There is only one common function for now and it's small enough so having it inline is fine. However, there is probably room for more merging of _32.c and _64.c in which case we'll probably want to have a common .c file and avoid inlines.

Here's an updated version to create a signal.[ch] instead. It will be used by the signal consolidation patches I'll post now.

arch/powerpc/kernel/signal_32.c +4 -24
Index: linux-2.6/arch/powerpc/kernel/signal_32.c
===================================================================
@@ -51,6 +51,8 @@
 #include <asm/pgtable.h>
 #endif
 
+#include "signal.h"
+
 #undef DEBUG_SIG
 
 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
@@ -1156,30 +1158,8 @@ int do_signal(sigset_t *oldset, struct p
 #ifdef CONFIG_PPC32
 no_signal:
 #endif
-	if (TRAP(regs) == 0x0C00		/* System Call! */
-	    && regs->ccr & 0x10000000		/* error signalled */
-	    && ((ret = regs->gpr[3]) == ERESTARTSYS
-		|| ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
-		|| ret == ERESTART_RESTARTBLOCK)) {
-
-		if (signr > 0
-		    && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
-			|| (ret == ERESTARTSYS
-			    && !(ka.sa.sa_flags & SA_RESTART)))) {
-			/* make the system call return an EINTR error */
-			regs->result = -EINTR;
-			regs->gpr[3] = EINTR;
-			/* note that the cr0.SO bit is already set */
-		} else {
-			regs->nip -= 4;	/* Back up & retry system call */
-			regs->result = 0;
-			regs->trap = 0;
-			if (ret == ERESTART_RESTARTBLOCK)
-				regs->gpr[0] = __NR_restart_syscall;
-			else
-				regs->gpr[3] = regs->orig_gpr3;
-		}
-	}
+	/* Is there any syscall restart business here ? */
+	check_syscall_restart(regs, &ka, signr > 0);
 
 	if (signr == 0) {
 		/* No signal to deliver -- put the saved sigmask back */
arch/powerpc/kernel/signal_64.c +7 -52

92 lines not on this page · raw message

arch/powerpc/kernel/Makefile +2 -1
Index: linux-2.6/arch/powerpc/kernel/Makefile
===================================================================
@@ -12,7 +12,8 @@ endif
 
 obj-y				:= semaphore.o cputable.o ptrace.o syscalls.o \
 				   irq.o align.o signal_32.o pmc.o vdso.o \
-				   init_task.o process.o systbl.o idle.o
+				   init_task.o process.o systbl.o idle.o \
+				   signal.o
 obj-y				+= vdso32/
 obj-$(CONFIG_PPC64)		+= setup_64.o binfmt_elf32.o sys_ppc32.o \
 				   signal_64.o ptrace32.o \
arch/powerpc/kernel/signal.c new file +62 -0

62 lines not on this page · raw message

arch/powerpc/kernel/signal.h new file +3 -0
Index: linux-2.6/arch/powerpc/kernel/signal.h
===================================================================
@@ -0,0 +1,3 @@
+
+void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
+		int has_handler);
#2 lore raw
Quoted 3 lines

Here's an updated version to create a signal.[ch] instead. It will be used by the signal consolidation patches I'll post now.

Excellent, thanks !

Cheers,
Ben.