From 912cbd74feab3458b788908ee8248eb0648f3b6b Mon Sep 17 00:00:00 2001
From: Matthew Wilcox <matthew@wil.cx>
Date: Sun, 27 Jan 2008 19:38:33 -0500
Subject: [PATCH] h8300: Implement down_killable

Signed-off-by: Matthew Wilcox <matthew@wil.cx>
---
 arch/h8300/kernel/semaphore.c        |   20 ++++++++++++++++++++
 include/asm-h8300/semaphore-helper.h |   19 +++++++++++++++++++
 include/asm-h8300/semaphore.h        |   30 ++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/arch/h8300/kernel/semaphore.c b/arch/h8300/kernel/semaphore.c
index d12cbbf..735f80d 100644
--- a/arch/h8300/kernel/semaphore.c
+++ b/arch/h8300/kernel/semaphore.c
@@ -126,6 +126,26 @@ int __sched __down_interruptible(struct semaphore * sem)
 	return ret;
 }
 
+int __sched __down_killable(struct semaphore * sem)
+{
+	DECLARE_WAITQUEUE(wait, current);
+	int ret = 0;
+
+	DOWN_HEAD(TASK_KILLABLE)
+
+	ret = waking_non_zero_killable(sem, current);
+	if (ret)
+	{
+		if (ret == 1)
+			/* ret != 0 only if we get interrupted -arca */
+			ret = 0;
+		break;
+	}
+	schedule();
+	DOWN_TAIL(TASK_KILLABLE)
+	return ret;
+}
+
 int __down_trylock(struct semaphore * sem)
 {
 	return waking_non_zero_trylock(sem);
diff --git a/include/asm-h8300/semaphore-helper.h b/include/asm-h8300/semaphore-helper.h
index 4fea36b..0756aaa 100644
--- a/include/asm-h8300/semaphore-helper.h
+++ b/include/asm-h8300/semaphore-helper.h
@@ -60,6 +60,25 @@ static inline int waking_non_zero_interruptible(struct semaphore *sem,
 	return ret;
 }
 
+static inline int waking_non_zero_killable(struct semaphore *sem,
+						struct task_struct *tsk)
+{
+	int ret;
+	unsigned long flags;
+
+	spin_lock_irqsave(&semaphore_wake_lock, flags);
+	ret = 0;
+	if (sem->sleepers > 0) {
+		sem->sleepers--;
+		ret = 1;
+	} else if (fatal_signal_pending(tsk)) {
+		atomic_inc(&sem->count);
+		ret = -EINTR;
+	}
+	spin_unlock_irqrestore(&semaphore_wake_lock, flags);
+	return ret;
+}
+
 /*
  * waking_non_zero_trylock:
  *	1	failed to lock
diff --git a/include/asm-h8300/semaphore.h b/include/asm-h8300/semaphore.h
index f3ffff8..a6719eb 100644
--- a/include/asm-h8300/semaphore.h
+++ b/include/asm-h8300/semaphore.h
@@ -57,11 +57,13 @@ static inline void init_MUTEX_LOCKED (struct semaphore *sem)
 
 asmlinkage void __down_failed(void /* special register calling convention */);
 asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
+asmlinkage int  __down_failed_killable(void  /* params in registers */);
 asmlinkage int  __down_failed_trylock(void  /* params in registers */);
 asmlinkage void __up_wakeup(void /* special register calling convention */);
 
 asmlinkage void __down(struct semaphore * sem);
 asmlinkage int  __down_interruptible(struct semaphore * sem);
+asmlinkage int  __down_killable(struct semaphore * sem);
 asmlinkage int  __down_trylock(struct semaphore * sem);
 asmlinkage void __up(struct semaphore * sem);
 
@@ -126,6 +128,34 @@ static inline int down_interruptible(struct semaphore * sem)
 	return (int)count;
 }
 
+static inline int down_killable(struct semaphore * sem)
+{
+	register atomic_t *count asm("er0");
+
+	might_sleep();
+
+	count = &(sem->count);
+	__asm__ __volatile__(
+		"stc ccr,r1l\n\t"
+		"orc #0x80,ccr\n\t"
+		"mov.l %3, er2\n\t"
+		"dec.l #1,er2\n\t"
+		"mov.l er2,%1\n\t"
+		"bpl 1f\n\t"
+		"ldc r1l,ccr\n\t"
+		"mov.l %2,er0\n\t"
+		"jsr @___down_killable\n\t"
+		"bra 2f\n"
+		"1:\n\t"
+		"ldc r1l,ccr\n\t"
+		"sub.l %0,%0\n\t"
+		"2:\n\t"
+		: "=r" (count),"=m" (*count)
+		: "g"(sem),"m"(*count)
+		: "cc", "er1", "er2", "er3");
+	return (int)count;
+}
+
 static inline int down_trylock(struct semaphore * sem)
 {
 	register atomic_t *count asm("er0");
-- 
1.5.3.8


