lock_stubs.s revision 1.2
11.2Sad/*	$NetBSD: lock_stubs.s,v 1.2 2007/02/09 21:55:12 ad Exp $	*/
21.2Sad
31.2Sad/*-
41.2Sad * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
51.2Sad * All rights reserved.
61.2Sad *
71.2Sad * This code is derived from software contributed to The NetBSD Foundation
81.2Sad * by Jason R. Thorpe and Andrew Doran.
91.2Sad *
101.2Sad * Redistribution and use in source and binary forms, with or without
111.2Sad * modification, are permitted provided that the following conditions
121.2Sad * are met:
131.2Sad * 1. Redistributions of source code must retain the above copyright
141.2Sad *    notice, this list of conditions and the following disclaimer.
151.2Sad * 2. Redistributions in binary form must reproduce the above copyright
161.2Sad *    notice, this list of conditions and the following disclaimer in the
171.2Sad *    documentation and/or other materials provided with the distribution.
181.2Sad * 3. All advertising materials mentioning features or use of this software
191.2Sad *    must display the following acknowledgement:
201.2Sad *	This product includes software developed by the NetBSD
211.2Sad *	Foundation, Inc. and its contributors.
221.2Sad * 4. Neither the name of The NetBSD Foundation nor the names of its
231.2Sad *    contributors may be used to endorse or promote products derived
241.2Sad *    from this software without specific prior written permission.
251.2Sad *
261.2Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.2Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.2Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.2Sad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.2Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.2Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.2Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.2Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.2Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.2Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.2Sad * POSSIBILITY OF SUCH DAMAGE.
371.2Sad */
381.2Sad
391.2Sad#include "opt_multiprocessor.h"
401.2Sad#include "opt_lockdebug.h"
411.2Sad
421.2Sad#include <machine/param.h>
431.2Sad#include <machine/asm.h>
441.2Sad
451.2Sad#include "assym.h"
461.2Sad
471.2Sad#undef CURLWP
481.2Sad#if defined(MULTIPROCESSOR)
491.2Sad#define	CURLWP	(CPUINFO_VA+CI_CURLWP)
501.2Sad#define	MB_READ	membar #LoadLoad
511.2Sad#define	MB_MEM	membar #LoadLoad | #StoreLoad | #LoadStore
521.2Sad#else
531.2Sad#define	CURLWP	_C_LABEL(curlwp)
541.2Sad#define	MB_READ	/* nothing */
551.2Sad#define	MB_MEM	/* nothing */
561.2Sad#endif
571.2Sad
581.2Sad#ifdef __arch64__
591.2Sad#define	CASPTR	casx
601.2Sad#define	LDPTR	ldx
611.2Sad#define	STPTR	stx
621.2Sad#else
631.2Sad#define	CASPTR	cas
641.2Sad#define	LDPTR	ld
651.2Sad#define	STPTR	st
661.2Sad#endif /* __arch64__ */
671.2Sad
681.2Sad/*
691.2Sad * void _lock_cas(uintptr_t *ptr, uintptr_t old, uintptr_t new);
701.2Sad */
711.2Sad_ENTRY(_C_LABEL(_lock_cas))
721.2Sad	CASPTR	[%o0], %o1, %o2			! compare-and-swap
731.2Sad	MB_MEM
741.2Sad	cmp	%o1, %o2			! expected == actual?
751.2Sad	bne	1f				! nope
761.2Sad	 or	%g0, 1, %o0
771.2Sad	retl
781.2Sad	 nop
791.2Sad1:	retl
801.2Sad	 mov	%g0, %o0
811.2Sad
821.2Sad#if !defined(LOCKDEBUG)
831.2Sad
841.2Sad/*
851.2Sad * void mutex_enter(kmutex_t *);
861.2Sad */
871.2Sad_ENTRY(_C_LABEL(mutex_enter))
881.2Sad	sethi	%hi(CURLWP), %o3
891.2Sad	LDPTR	[%o3 + %lo(CURLWP)], %o3	! current thread
901.2Sad	CASPTR	[%o0], %g0, %o3			! compare-and-swap
911.2Sad	MB_READ
921.2Sad	tst	%o3				! lock was unowned?
931.2Sad	bnz	_C_LABEL(mutex_vector_enter)	! nope, hard case
941.2Sad	 nop
951.2Sad	retl
961.2Sad	 nop
971.2Sad
981.2Sad/*
991.2Sad * void mutex_exit(kmutex_t *);
1001.2Sad *
1011.2Sad * XXX This should use a restartable sequence.  See mutex_vector_enter().
1021.2Sad */
1031.2Sad_ENTRY(_C_LABEL(mutex_exit))
1041.2Sad	sethi	%hi(CURLWP), %o3
1051.2Sad	LDPTR	[%o3 + %lo(CURLWP)], %o3	! current thread
1061.2Sad	mov	%g0, %o4			! new value (0)
1071.2Sad	MB_MEM
1081.2Sad	CASPTR	[%o0], %o3, %o4			! compare-and-swap
1091.2Sad	cmp	%o3, %o4			! were they the same?
1101.2Sad	bne	_C_LABEL(mutex_vector_exit)	! nope, hard case
1111.2Sad	 nop
1121.2Sad	retl
1131.2Sad	 nop
1141.2Sad
1151.2Sad#endif	/* !LOCKDEBUG */
116