lock.h revision 1.13
1/* $NetBSD: lock.h,v 1.13 2003/06/26 13:20:55 he Exp $ */ 2 3/* 4 * Copyright (c) 2000 Ludd, University of Lule}, Sweden. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed at Ludd, University of Lule}. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#ifndef _VAX_LOCK_H_ 34#define _VAX_LOCK_H_ 35 36#ifdef _KERNEL 37#ifdef _KERNEL_OPT 38#include "opt_multiprocessor.h" 39#endif 40#include <machine/cpu.h> 41#endif 42 43typedef __volatile int __cpu_simple_lock_t; 44 45#define __SIMPLELOCK_LOCKED 1 46#define __SIMPLELOCK_UNLOCKED 0 47 48static __inline void 49__cpu_simple_lock_init(__cpu_simple_lock_t *alp) 50{ 51#ifdef _KERNEL 52 __asm__ __volatile ("movl %0,%%r1;jsb Sunlock" 53 : /* No output */ 54 : "g"(alp) 55 : "r1","cc","memory"); 56#else 57 __asm__ __volatile ("bbcci $0,%0,1f;1:" 58 : /* No output */ 59 : "m"(*alp) 60 : "cc"); 61#endif 62} 63 64static __inline int 65__cpu_simple_lock_try(__cpu_simple_lock_t *alp) 66{ 67 int ret; 68 69#ifdef _KERNEL 70 __asm__ __volatile ("movl %1,%%r1;jsb Slocktry;movl %%r0,%0" 71 : "=&r"(ret) 72 : "g"(alp) 73 : "r0","r1","cc","memory"); 74#else 75 __asm__ __volatile ("clrl %0;bbssi $0,%1,1f;incl %0;1:" 76 : "=&r"(ret) 77 : "m"(*alp) 78 : "cc"); 79#endif 80 81 return ret; 82} 83 84#ifdef _KERNEL 85#define VAX_LOCK_CHECKS ((1 << IPI_SEND_CNCHAR) | (1 << IPI_DDB)) 86#define __cpu_simple_lock(alp) \ 87do { \ 88 struct cpu_info *__ci = curcpu(); \ 89 \ 90 while (__cpu_simple_lock_try(alp) == 0) { \ 91 int __s; \ 92 \ 93 if (__ci->ci_ipimsgs & VAX_LOCK_CHECKS) { \ 94 __s = splipi(); \ 95 cpu_handle_ipi(); \ 96 splx(__s); \ 97 } \ 98 } \ 99} while (0) 100#else 101static __inline void 102__cpu_simple_lock(__cpu_simple_lock_t *alp) 103{ 104 __asm__ __volatile ("1:bbssi $0,%0,1b" 105 : /* No outputs */ 106 : "m"(*alp) 107 : "cc"); 108} 109#endif /* _KERNEL */ 110 111#if 0 112static __inline void 113__cpu_simple_lock(__cpu_simple_lock_t *alp) 114{ 115 struct cpu_info *ci = curcpu(); 116 117 while (__cpu_simple_lock_try(alp) == 0) { 118 int s; 119 120 if (ci->ci_ipimsgs & IPI_SEND_CNCHAR) { 121 s = splipi(); 122 cpu_handle_ipi(); 123 splx(s); 124 } 125 } 126 127#if 0 128 __asm__ __volatile ("movl %0,%%r1;jsb Slock" 129 : /* No output */ 130 : "g"(alp) 131 : "r0","r1","cc","memory"); 132#endif 133#if 0 134 __asm__ __volatile ("1:;bbssi $0, %0, 1b" 135 : /* No output */ 136 : "m"(*alp)); 137#endif 138} 139#endif 140 141static __inline void 142__cpu_simple_unlock(__cpu_simple_lock_t *alp) 143{ 144#ifdef _KERNEL 145 __asm__ __volatile ("movl %0,%%r1;jsb Sunlock" 146 : /* No output */ 147 : "g"(alp) 148 : "r1","cc","memory"); 149#else 150 __asm__ __volatile ("bbcci $0,%0,1f;1:" 151 : /* No output */ 152 : "m"(*alp) 153 : "cc"); 154#endif 155} 156 157#if defined(MULTIPROCESSOR) 158/* 159 * On the Vax, interprocessor interrupts can come in at device priority 160 * level or lower. This can cause some problems while waiting for r/w 161 * spinlocks from a high'ish priority level: IPIs that come in will not 162 * be processed. This can lead to deadlock. 163 * 164 * This hook allows IPIs to be processed while a spinlock's interlock 165 * is released. 166 */ 167#define SPINLOCK_SPIN_HOOK \ 168do { \ 169 struct cpu_info *__ci = curcpu(); \ 170 int __s; \ 171 \ 172 if (__ci->ci_ipimsgs != 0) { \ 173 /* printf("CPU %lu has IPIs pending\n", \ 174 __ci->ci_cpuid); */ \ 175 __s = splipi(); \ 176 cpu_handle_ipi(); \ 177 splx(__s); \ 178 } \ 179} while (0) 180#endif /* MULTIPROCESSOR */ 181#endif /* _VAX_LOCK_H_ */ 182