lock_stubs.s revision 1.5
1/*	$NetBSD: lock_stubs.s,v 1.5 2007/02/21 20:07:42 martin Exp $	*/
2
3/*-
4 * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "opt_multiprocessor.h"
40#include "opt_lockdebug.h"
41
42#include <machine/param.h>
43#include <machine/asm.h>
44
45#include "assym.h"
46
47#undef CURLWP
48#define	CURLWP	(CPUINFO_VA+CI_CURLWP)
49
50#if defined(MULTIPROCESSOR)
51#define	MB_READ	membar #LoadLoad
52#define	MB_MEM	membar #LoadStore | #StoreStore
53#else
54#define	MB_READ	/* nothing */
55#define	MB_MEM	/* nothing */
56#endif
57
58#ifdef __arch64__
59#define	CASPTR	casx
60#define	LDPTR	ldx
61#define	STPTR	stx
62#define	CCCR	%xcc
63#else
64#define	CASPTR	cas
65#define	LDPTR	ld
66#define	STPTR	st
67#define	CCCR	%icc
68#endif /* __arch64__ */
69
70/*
71 * int _lock_cas(uintptr_t *ptr, uintptr_t old, uintptr_t new);
72 */
73_ENTRY(_C_LABEL(_lock_cas))
74	CASPTR	[%o0], %o1, %o2			! compare-and-swap
75	MB_MEM
76	cmp	%o1, %o2			! expected == actual?
77	bne,pn	%icc,1f				! nope
78	 nop
79	retl
80	 mov	1, %o0
811:	retl
82	 clr	%o0
83
84#if !defined(LOCKDEBUG)
85
86/*
87 * void mutex_enter(kmutex_t *);
88 */
89_ENTRY(_C_LABEL(mutex_enter))
90	sethi	%hi(CURLWP), %o1
91	LDPTR	[%o1 + %lo(CURLWP)], %o1	! current thread
92	CASPTR	[%o0], %g0, %o1			! compare-and-swap
93	MB_READ
94	brnz,pn	%o1, 1f				! lock was unowned?
95	 nop
96	retl					! - yes, done
97	 nop
981:	sethi	%hi(_C_LABEL(mutex_vector_enter)),%o2	! nope, do it the
99	jmp	%o2 + %lo(_C_LABEL(mutex_vector_enter))	! hard way
100	 nop
101
102/*
103 * void mutex_exit(kmutex_t *);
104 *
105 * XXX This should use a restartable sequence.  See mutex_vector_enter().
106 */
107_ENTRY(_C_LABEL(mutex_exit))
108	sethi	%hi(CURLWP), %o1
109	LDPTR	[%o1 + %lo(CURLWP)], %o1	! current thread
110	clr	%o2				! new value (0)
111	MB_MEM
112	CASPTR	[%o0], %o1, %o2			! compare-and-swap
113	cmp	%o1, %o2
114	bne,pn	CCCR, 1f			! nope, hard case
115	 nop
116	retl
117	 nop
1181:	sethi	%hi(_C_LABEL(mutex_vector_exit)), %o2
119	jmp	%o2 + %lo(_C_LABEL(mutex_vector_exit))
120	 nop
121
122#endif	/* !LOCKDEBUG */
123