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