lock_stubs.s revision 1.3
1/* $NetBSD: lock_stubs.s,v 1.3 2007/02/20 15:30:53 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#if defined(MULTIPROCESSOR) 49#define CURLWP (CPUINFO_VA+CI_CURLWP) 50#define MB_READ membar #LoadLoad 51#define MB_MEM membar #LoadStore | #StoreStore 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 * int _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,pn %icc,1f ! nope 76 nop 77 retl 78 mov 1, %o0 791: retl 80 clr %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), %o1 89 LDPTR [%o1 + %lo(CURLWP)], %o1 ! current thread 90 CASPTR [%o0], %g0, %o1 ! compare-and-swap 91 MB_READ 92 brnz,pn %o1, 1f ! lock was unowned? 93 nop 94 retl ! - yes, done 95 nop 961: sethi %hi(_C_LABEL(mutex_vector_enter)),%o2 ! nope, do it the 97 jmp %o2 + %lo(_C_LABEL(mutex_vector_enter)) ! hard way 98 nop 99 100/* 101 * void mutex_exit(kmutex_t *); 102 * 103 * XXX This should use a restartable sequence. See mutex_vector_enter(). 104 */ 105_ENTRY(_C_LABEL(mutex_exit)) 106 sethi %hi(CURLWP), %o1 107 LDPTR [%o1 + %lo(CURLWP)], %o1 ! current thread 108 clr %o2 ! new value (0) 109 MB_MEM 110 CASPTR [%o0], %o1, %o2 ! compare-and-swap 111 brnz,pn %o2, 1f ! nope, hard case 112 nop 113 retl 114 nop 1151: sethi %hi(_C_LABEL(mutex_vector_exit)), %o2 116 jmp %o2 + %lo(_C_LABEL(mutex_vector_exit)) 117 nop 118 119#endif /* !LOCKDEBUG */ 120