summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sys/arch/i386/i386/genassym.cf10
-rw-r--r--sys/arch/i386/i386/locore.s4
-rw-r--r--sys/arch/i386/i386/mutex.S101
-rw-r--r--sys/arch/i386/include/mutex.h55
-rw-r--r--sys/arch/i386/include/types.h3
5 files changed, 170 insertions, 3 deletions
diff --git a/sys/arch/i386/i386/genassym.cf b/sys/arch/i386/i386/genassym.cf
index 7453c773fed..968ef80b33a 100644
--- a/sys/arch/i386/i386/genassym.cf
+++ b/sys/arch/i386/i386/genassym.cf
@@ -1,4 +1,4 @@
-# $OpenBSD: genassym.cf,v 1.17 2004/07/02 16:29:55 niklas Exp $
+# $OpenBSD: genassym.cf,v 1.18 2004/07/20 20:16:44 art Exp $
#
# Copyright (c) 1982, 1990 The Regents of the University of California.
# All rights reserved.
@@ -44,6 +44,7 @@ include <uvm/uvm_extern.h>
include <machine/trap.h>
include <machine/pmap.h>
include <machine/vmparam.h>
+include <machine/mutex.h>
if COMPAT_SVR4 > 0
include <compat/svr4/svr4_ucontext.h>
@@ -165,6 +166,12 @@ member IH_COUNT ih_count.ec_count
member ih_next
endif
+struct mutex
+member mtx_lock
+member mtx_wantipl
+member mtx_oldipl
+member mtx_owner
+
define P_MD_TSS_SEL offsetof(struct proc, p_md.md_tss_sel)
define CPU_INFO_SELF offsetof(struct cpu_info, ci_self)
@@ -187,3 +194,4 @@ define CPU_INFO_IDEPTH offsetof(struct cpu_info, ci_idepth)
define CPU_INFO_ISOURCES offsetof(struct cpu_info, ci_isources)
define SIZEOF_CPU_INFO sizeof(struct cpu_info)
+
diff --git a/sys/arch/i386/i386/locore.s b/sys/arch/i386/i386/locore.s
index 8686395cce3..86825296228 100644
--- a/sys/arch/i386/i386/locore.s
+++ b/sys/arch/i386/i386/locore.s
@@ -1,4 +1,4 @@
-/* $OpenBSD: locore.s,v 1.79 2004/07/02 16:29:55 niklas Exp $ */
+/* $OpenBSD: locore.s,v 1.80 2004/07/20 20:16:44 art Exp $ */
/* $NetBSD: locore.s,v 1.145 1996/05/03 19:41:19 christos Exp $ */
/*-
@@ -2301,3 +2301,5 @@ ENTRY(i686_pagezero)
#if NLAPIC > 0
#include <i386/i386/apicvec.s>
#endif
+
+#include <i386/i386/mutex.S>
diff --git a/sys/arch/i386/i386/mutex.S b/sys/arch/i386/i386/mutex.S
new file mode 100644
index 00000000000..51130b828eb
--- /dev/null
+++ b/sys/arch/i386/i386/mutex.S
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2004 Artur Grabowski <art@openbsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "assym.h"
+/*
+ * Yeah, we don't really need to implement mtx_init here, but let's keep
+ * all the functions in the same place.
+ */
+ENTRY(mtx_init)
+ pushl %ebp
+ movl %esp, %ebp
+ movl 8(%esp), %eax
+ movl 12(%esp), %edx
+ movl %edx, MTX_WANTIPL(%eax)
+ xorl %edx, %edx
+ movl %edx, MTX_OLDIPL(%eax)
+ movl %edx, MTX_LOCK(%eax)
+ movl %edx, MTX_OWNER(%eax)
+ leave
+ ret
+
+#define SOFF 8
+
+ENTRY(mtx_enter)
+ pushl %ebp
+ movl %esp, %ebp
+1: movl SOFF(%ebp), %ecx
+ movl MTX_WANTIPL(%ecx), %eax
+ movl CPL, %edx # oipl = cpl;
+ cmpl %edx, %eax # if (cpl < mtx->mtx_wantipl)
+ jle 2f
+ movl %eax, CPL # cpl = mtx->mtx_wantipl;
+2: /*
+ * %edx now contains the oldipl.
+ * %ecx contains the mtx.
+ */
+ movl $1, %eax
+ xchgl %eax, MTX_LOCK(%ecx) # test_and_set(mtx->mtx_lock)
+ testl %eax, %eax # if (already held)
+ jnz 3f
+ GET_CPUINFO(%eax)
+ movl %eax, MTX_OWNER(%ecx)
+ movl %edx, MTX_OLDIPL(%ecx)
+ leave
+ ret
+
+ /* We failed to obtain the lock. splx, spin and retry. */
+3: pushl %edx
+ call _C_LABEL(splx)
+ movl %ebp, %esp
+ movl SOFF(%ebp), %ecx # %ecx clobbered
+4:
+#ifdef DIAGNOSTIC
+ GET_CPUINFO(%edx)
+ cmpl MTX_OWNER(%ecx), %edx
+ je 5f
+#endif
+ movl MTX_LOCK(%ecx), %eax
+ testl %eax, %eax
+ jz 1b
+ jmp 4b
+#ifdef DIAGNOSTIC
+5: pushl $6f
+ call _C_LABEL(panic)
+6: .asciz "mtx_enter: locking against myself"
+#endif
+
+ENTRY(mtx_leave)
+ pushl %ebp
+ movl %esp, %ebp
+ movl SOFF(%ebp), %ecx
+ xorl %eax, %eax
+ movl %eax, MTX_OWNER(%ecx)
+ pushl MTX_OLDIPL(%ecx)
+ movl %eax, MTX_OLDIPL(%ecx)
+ movl %eax, MTX_LOCK(%ecx)
+ call _C_LABEL(splx)
+ leave
+ ret
diff --git a/sys/arch/i386/include/mutex.h b/sys/arch/i386/include/mutex.h
new file mode 100644
index 00000000000..0982b8fe36a
--- /dev/null
+++ b/sys/arch/i386/include/mutex.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2004 Artur Grabowski <art@openbsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef _I386_MUTEX_H_
+#define _I386_MUTEX_H_
+
+/*
+ * XXX - we don't really need the mtx_lock field, we can use mtx_oldipl
+ * as the lock to save some space.
+ */
+struct mutex {
+ __volatile int mtx_lock;
+ int mtx_wantipl;
+ int mtx_oldipl;
+ void *mtx_owner;
+};
+
+#define MUTEX_INITIALIZER(IPL) { 0, (IPL), 0, NULL }
+
+#define MUTEX_ASSERT_LOCKED(mtx) do { \
+ if ((mtx)->mtx_lock != 1 || \
+ (mtx)->mtx_owner != curcpu()) \
+ panic("mutex %p not held in %s\n", (mtx), __func__); \
+} while (0)
+
+#define MUTEX_ASSERT_UNLOCKED(mtx) do { \
+ if ((mtx)->mtx_lock == 1 && \
+ (mtx)->mtx_owner == curcpu()) \
+ panic("mutex %p held in %s\n", (mtx), __func__); \
+} while (0)
+
+#define MUTEX_OLDIPL(mtx) (mtx)->mtx_oldipl
+
+#endif
diff --git a/sys/arch/i386/include/types.h b/sys/arch/i386/include/types.h
index 7262847c9cc..c01b2537ed3 100644
--- a/sys/arch/i386/include/types.h
+++ b/sys/arch/i386/include/types.h
@@ -1,5 +1,5 @@
/* $NetBSD: types.h,v 1.12 1995/12/24 01:08:03 mycroft Exp $ */
-/* $OpenBSD: types.h,v 1.15 2004/06/28 01:41:53 aaron Exp $ */
+/* $OpenBSD: types.h,v 1.16 2004/07/20 20:16:44 art Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@@ -74,5 +74,6 @@ typedef int32_t register_t;
#define __HAVE_NWSCONS
#define __HAVE_EVCOUNT
#define __HAVE_CPUINFO
+#define __HAVE_MUTEX
#endif /* _MACHTYPES_H_ */