lock_stubs.s revision 1.2
1/* $NetBSD: lock_stubs.s,v 1.2 2007/02/09 21:55:12 ad 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#if defined(MULTIPROCESSOR) 49#define CURLWP (CPUINFO_VA+CI_CURLWP) 50#define MB_READ membar #LoadLoad 51#define MB_MEM membar #LoadLoad | #StoreLoad | #LoadStore 52#else 53#define CURLWP _C_LABEL(curlwp) 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#else 63#define CASPTR cas 64#define LDPTR ld 65#define STPTR st 66#endif /* __arch64__ */ 67 68/* 69 * void _lock_cas(uintptr_t *ptr, uintptr_t old, uintptr_t new); 70 */ 71_ENTRY(_C_LABEL(_lock_cas)) 72 CASPTR [%o0], %o1, %o2 ! compare-and-swap 73 MB_MEM 74 cmp %o1, %o2 ! expected == actual? 75 bne 1f ! nope 76 or %g0, 1, %o0 77 retl 78 nop 791: retl 80 mov %g0, %o0 81 82#if !defined(LOCKDEBUG) 83 84/* 85 * void mutex_enter(kmutex_t *); 86 */ 87_ENTRY(_C_LABEL(mutex_enter)) 88 sethi %hi(CURLWP), %o3 89 LDPTR [%o3 + %lo(CURLWP)], %o3 ! current thread 90 CASPTR [%o0], %g0, %o3 ! compare-and-swap 91 MB_READ 92 tst %o3 ! lock was unowned? 93 bnz _C_LABEL(mutex_vector_enter) ! nope, hard case 94 nop 95 retl 96 nop 97 98/* 99 * void mutex_exit(kmutex_t *); 100 * 101 * XXX This should use a restartable sequence. See mutex_vector_enter(). 102 */ 103_ENTRY(_C_LABEL(mutex_exit)) 104 sethi %hi(CURLWP), %o3 105 LDPTR [%o3 + %lo(CURLWP)], %o3 ! current thread 106 mov %g0, %o4 ! new value (0) 107 MB_MEM 108 CASPTR [%o0], %o3, %o4 ! compare-and-swap 109 cmp %o3, %o4 ! were they the same? 110 bne _C_LABEL(mutex_vector_exit) ! nope, hard case 111 nop 112 retl 113 nop 114 115#endif /* !LOCKDEBUG */ 116