diff options
author | Artur Grabowski <art@cvs.openbsd.org> | 2004-07-20 20:17:17 +0000 |
---|---|---|
committer | Artur Grabowski <art@cvs.openbsd.org> | 2004-07-20 20:17:17 +0000 |
commit | a198ef56518b5f882971d94b3f3431e72121c4a2 (patch) | |
tree | 7997479ee67992f343f19728a6f6f76e01549d63 /sys | |
parent | f4495499d3272f5778d149843b42f4ad4b70347e (diff) |
MD mutex implementation on amd64.
Diffstat (limited to 'sys')
-rw-r--r-- | sys/arch/amd64/amd64/genassym.cf | 9 | ||||
-rw-r--r-- | sys/arch/amd64/amd64/mutex.S | 94 | ||||
-rw-r--r-- | sys/arch/amd64/conf/files.amd64 | 3 | ||||
-rw-r--r-- | sys/arch/amd64/include/mutex.h | 48 | ||||
-rw-r--r-- | sys/arch/amd64/include/types.h | 3 |
5 files changed, 153 insertions, 4 deletions
diff --git a/sys/arch/amd64/amd64/genassym.cf b/sys/arch/amd64/amd64/genassym.cf index 8fc08e90ef2..8b80c559e9e 100644 --- a/sys/arch/amd64/amd64/genassym.cf +++ b/sys/arch/amd64/amd64/genassym.cf @@ -1,5 +1,4 @@ -# $OpenBSD: genassym.cf,v 1.7 2004/06/28 01:52:24 deraadt Exp $ - +# $OpenBSD: genassym.cf,v 1.8 2004/07/20 20:17:16 art Exp $ # Written by Artur Grabowski art@openbsd.org, Public Domain include <sys/param.h> @@ -17,6 +16,7 @@ include <machine/vmparam.h> include <machine/intr.h> include <machine/pic.h> include <machine/i82093var.h> +include <machine/mutex.h> export SRUN export SONPROC @@ -131,6 +131,11 @@ struct ioapic_softc member IOAPIC_SC_REG sc_reg member IOAPIC_SC_DATA sc_data +struct mutex +member mtx_wantipl +member mtx_oldipl +member mtx_owner + # pte fields export PG_V export PG_KR diff --git a/sys/arch/amd64/amd64/mutex.S b/sys/arch/amd64/amd64/mutex.S new file mode 100644 index 00000000000..a4018c03333 --- /dev/null +++ b/sys/arch/amd64/amd64/mutex.S @@ -0,0 +1,94 @@ +/* + * 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" + +#include <machine/param.h> +#include <machine/asm.h> +#include <machine/segments.h> +#include <machine/specialreg.h> +#include <machine/trap.h> +#include <machine/frameasm.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) + movl %esi, MTX_WANTIPL(%rdi) + movl $0, MTX_OLDIPL(%rdi) + movq $0, MTX_OWNER(%rdi) + ret + +ENTRY(mtx_enter) +1: movl MTX_WANTIPL(%rdi), %eax + movq CPUVAR(SELF), %rcx + movl CPU_INFO_ILEVEL(%rcx), %edx # oipl = cpl; + cmpl %edx, %eax # if (cpl < mtx->mtx_wantipl) + jle 2f + movl %eax, CPU_INFO_ILEVEL(%rcx) # cpl = mtx->mtx_wantipl; +2: /* + * %edx - the old ipl + * %rcx - curcpu() + */ + xorq %rax, %rax +#ifdef MULTIPROCESSOR + lock +#endif + cmpxchgq %rcx, MTX_OWNER(%rdi) # test_and_set(mtx->mtx_owner) + cmpq $0, %rax + jne 3f + movl %edx, MTX_OLDIPL(%rdi) + ret + + /* We failed to obtain the lock. splx, spin and retry. */ +3: pushq %rdi + movl %edx, %edi + call _C_LABEL(spllower) + popq %rdi +#ifdef DIAGNOSTIC + movq CPUVAR(SELF), %rcx + cmpq MTX_OWNER(%rdi), %rcx + je 5f +#endif +4: + movq MTX_OWNER(%rdi), %rax + testq %rax, %rax + jz 1b + jmp 4b +#ifdef DIAGNOSTIC +5: movq $6f, %rdi + call _C_LABEL(panic) +6: .asciz "mtx_enter: locking against myself" +#endif + +ENTRY(mtx_leave) + movq %rdi, %rax + xorq %rcx, %rcx + movl MTX_OLDIPL(%rax), %edi + movl %ecx, MTX_OLDIPL(%rax) + movq %rcx, MTX_OWNER(%rax) + call _C_LABEL(spllower) + ret diff --git a/sys/arch/amd64/conf/files.amd64 b/sys/arch/amd64/conf/files.amd64 index b52a9083423..9d3a3766705 100644 --- a/sys/arch/amd64/conf/files.amd64 +++ b/sys/arch/amd64/conf/files.amd64 @@ -1,4 +1,4 @@ -# $OpenBSD: files.amd64,v 1.3 2004/06/25 11:03:28 art Exp $ +# $OpenBSD: files.amd64,v 1.4 2004/07/20 20:17:16 art Exp $ maxpartitions 16 maxusers 2 16 128 @@ -21,6 +21,7 @@ file arch/amd64/amd64/Locore.c file arch/amd64/amd64/softintr.c file arch/amd64/amd64/i8259.c file arch/amd64/amd64/cacheinfo.c +file arch/amd64/amd64/mutex.S file arch/amd64/amd64/intr.c file arch/amd64/amd64/bus_space.c diff --git a/sys/arch/amd64/include/mutex.h b/sys/arch/amd64/include/mutex.h new file mode 100644 index 00000000000..ccb1b95774d --- /dev/null +++ b/sys/arch/amd64/include/mutex.h @@ -0,0 +1,48 @@ +/* + * 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 _AMD64_MUTEX_H_ +#define _AMD64_MUTEX_H_ + +struct mutex { + int mtx_wantipl; + int mtx_oldipl; + __volatile void *mtx_owner; +}; + +#define MUTEX_INITIALIZER(ipl) { (ipl), 0, NULL } + +#define MUTEX_ASSERT_LOCKED(mtx) do { \ + if ((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_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/amd64/include/types.h b/sys/arch/amd64/include/types.h index 8ef62891ef4..3beda9b0da4 100644 --- a/sys/arch/amd64/include/types.h +++ b/sys/arch/amd64/include/types.h @@ -1,4 +1,4 @@ -/* $OpenBSD: types.h,v 1.4 2004/06/28 01:52:26 deraadt Exp $ */ +/* $OpenBSD: types.h,v 1.5 2004/07/20 20:17:16 art Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. @@ -71,5 +71,6 @@ typedef long register_t; #define __HAVE_GENERIC_SOFT_INTERRUPTS #define __HAVE_CPUINFO #define __HAVE_EVCOUNT +#define __HAVE_MUTEX #endif /* _MACHTYPES_H_ */ |