1 /* $NetBSD: atomic_init_testset.c,v 1.3 2008/02/11 23:48:23 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 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 by the NetBSD 18 * Foundation, Inc. and its contributors. 19 * 4. Neither the name of The NetBSD Foundation nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * libc glue for atomic operations where the hardware does not provide 38 * compare-and-swap. It's assumed that this will only be used on 32-bit 39 * platforms. 40 * 41 * This should be compiled with '-fno-reorder-blocks -fomit-frame-pointer' 42 * if using gcc. 43 */ 44 45 #include <sys/cdefs.h> 46 __RCSID("$NetBSD: atomic_init_testset.c,v 1.3 2008/02/11 23:48:23 ad Exp $"); 47 48 #include "atomic_op_namespace.h" 49 50 #include <sys/types.h> 51 #include <sys/atomic.h> 52 #include <sys/lock.h> 53 #include <sys/ras.h> 54 #include <sys/sysctl.h> 55 56 #include <string.h> 57 58 #define I2 __SIMPLELOCK_UNLOCKED, __SIMPLELOCK_UNLOCKED, 59 #define I16 I2 I2 I2 I2 I2 I2 I2 I2 60 #define I128 I16 I16 I16 I16 I16 I16 I16 I16 61 62 static __cpu_simple_lock_t atomic_locks[128] = { I128 }; 63 static uint32_t (*_atomic_cas_fn)(volatile uint32_t *, uint32_t, uint32_t); 64 65 void __libc_atomic_init(void) __attribute__ ((visibility("hidden"))); 66 67 RAS_DECL(_atomic_cas); 68 69 static uint32_t 70 _atomic_cas_up(volatile uint32_t *ptr, uint32_t old, uint32_t new) 71 { 72 uint32_t ret; 73 74 RAS_START(_atomic_cas); 75 ret = *ptr; 76 if (__predict_false(ret != old)) { 77 return ret; 78 } 79 *ptr = new; 80 RAS_END(_atomic_cas); 81 82 return ret; 83 } 84 85 static uint32_t 86 _atomic_cas_mp(volatile uint32_t *ptr, uint32_t old, uint32_t new) 87 { 88 __cpu_simple_lock_t *lock; 89 uint32_t ret; 90 91 lock = &atomic_locks[((uint32_t)ptr >> 3) & 127]; 92 __cpu_simple_lock(lock); 93 ret = *ptr; 94 if (__predict_true(ret == old)) { 95 *ptr = new; 96 } 97 __cpu_simple_unlock(lock); 98 99 return ret; 100 } 101 102 uint32_t 103 _atomic_cas_32(volatile uint32_t *ptr, uint32_t old, uint32_t new) 104 { 105 106 return (*_atomic_cas_fn)(ptr, old, new); 107 } 108 109 void 110 __libc_atomic_init(void) 111 { 112 int ncpu, mib[2]; 113 size_t len; 114 115 _atomic_cas_fn = _atomic_cas_mp; 116 117 mib[0] = CTL_HW; 118 mib[1] = HW_NCPU; 119 len = sizeof(ncpu); 120 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) 121 return; 122 if (ncpu > 1) 123 return; 124 if (rasctl(RAS_ADDR(_atomic_cas), RAS_SIZE(_atomic_cas), 125 RAS_INSTALL) == 0) { 126 _atomic_cas_fn = _atomic_cas_up; 127 return; 128 } 129 } 130 131 #undef atomic_cas_32 132 #undef atomic_cas_uint 133 #undef atomic_cas_ulong 134 #undef atomic_cas_ptr 135 136 atomic_op_alias(atomic_cas_32,_atomic_cas_32) 137 atomic_op_alias(atomic_cas_uint,_atomic_cas_32) 138 __strong_alias(_atomic_cas_uint,_atomic_cas_32) 139 atomic_op_alias(atomic_cas_ulong,_atomic_cas_32) 140 __strong_alias(_atomic_cas_ulong,_atomic_cas_32) 141 atomic_op_alias(atomic_cas_ptr,_atomic_cas_32) 142 __strong_alias(_atomic_cas_ptr,_atomic_cas_32) 143 144 atomic_op_alias(atomic_cas_32_ni,_atomic_cas_32) 145 __strong_alias(_atomic_cas_32_ni,_atomic_cas_32) 146 atomic_op_alias(atomic_cas_uint_ni,_atomic_cas_32) 147 __strong_alias(_atomic_cas_uint_ni,_atomic_cas_32) 148 atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_32) 149 __strong_alias(_atomic_cas_ulong_ni,_atomic_cas_32) 150 atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_32) 151 __strong_alias(_atomic_cas_ptr_ni,_atomic_cas_32) 152