lock_stubs.s revision 1.6
1/* $NetBSD: lock_stubs.s,v 1.6 2007/02/21 22:14:24 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.align 32 74_ENTRY(_C_LABEL(_lock_cas)) 75 CASPTR [%o0], %o1, %o2 ! compare-and-swap 76 MB_MEM 77 xor %o1, %o2, %o2 ! expected == actual? 78 clr %o0 79 retl 80 movrz %o2, 1, %o0 81 82#if !defined(LOCKDEBUG) 83 84/* 85 * void mutex_enter(kmutex_t *); 86 */ 87.align 32 88_ENTRY(_C_LABEL(mutex_enter)) 89 sethi %hi(CURLWP), %o1 90 LDPTR [%o1 + %lo(CURLWP)], %o1 ! current thread 91 CASPTR [%o0], %g0, %o1 ! compare-and-swap 92 MB_READ 93 brnz,pn %o1, 1f ! lock was unowned? 94 nop 95 retl ! - yes, done 96 nop 971: ba _C_LABEL(mutex_vector_enter) ! nope, do it the 98 nop ! hard way 99 100/* 101 * void mutex_exit(kmutex_t *); 102 * 103 * XXX This should use a restartable sequence. See mutex_vector_enter(). 104 */ 105.align 32 106_ENTRY(_C_LABEL(mutex_exit)) 107 sethi %hi(CURLWP), %o1 108 LDPTR [%o1 + %lo(CURLWP)], %o1 ! current thread 109 clr %o2 ! new value (0) 110 MB_MEM 111 CASPTR [%o0], %o1, %o2 ! compare-and-swap 112 cmp %o1, %o2 113 bne,pn CCCR, 1f ! nope, hard case 114 nop 115 retl 116 nop 1171: ba _C_LABEL(mutex_vector_exit) 118 nop 119 120#endif /* !LOCKDEBUG */ 121