atomic_init_testset.c revision 1.7.6.1 1 /* $NetBSD: atomic_init_testset.c,v 1.7.6.1 2012/04/17 00:01:39 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.7.6.1 2012/04/17 00:01:39 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
57 #ifdef __HAVE_ASM_ATOMIC_CAS_UP
58 extern uint32_t _atomic_cas_up(volatile uint32_t *, uint32_t, uint32_t);
59 #else
60 static uint32_t _atomic_cas_up(volatile uint32_t *, uint32_t, uint32_t);
61 #endif
62
63 static uint32_t (*_atomic_cas_fn)(volatile uint32_t *, uint32_t, uint32_t) =
64 _atomic_cas_up;
65
66 void __libc_atomic_init(void) __attribute__ ((visibility("hidden")));
67
68 RAS_DECL(_atomic_cas);
69
70 #ifndef __HAVE_ASM_ATOMIC_CAS_UP
71 static uint32_t
72 _atomic_cas_up(volatile uint32_t *ptr, uint32_t old, uint32_t new)
73 {
74 uint32_t ret;
75
76 RAS_START(_atomic_cas);
77 ret = *ptr;
78 if (__predict_false(ret != old)) {
79 return ret;
80 }
81 *ptr = new;
82 RAS_END(_atomic_cas);
83
84 return ret;
85 }
86 #endif
87
88 static uint32_t
89 _atomic_cas_mp(volatile uint32_t *ptr, uint32_t old, uint32_t new)
90 {
91 __cpu_simple_lock_t *lock;
92 uint32_t ret;
93
94 lock = &atomic_locks[((uintptr_t)ptr >> 3) & 127];
95 __cpu_simple_lock(lock);
96 ret = *ptr;
97 if (__predict_true(ret == old)) {
98 *ptr = new;
99 }
100 __cpu_simple_unlock(lock);
101
102 return ret;
103 }
104
105 uint32_t
106 _atomic_cas_32(volatile uint32_t *ptr, uint32_t old, uint32_t new)
107 {
108
109 return (*_atomic_cas_fn)(ptr, old, new);
110 }
111
112 void
113 __libc_atomic_init(void)
114 {
115 int ncpu, mib[2];
116 size_t len;
117
118 _atomic_cas_fn = _atomic_cas_mp;
119
120 mib[0] = CTL_HW;
121 mib[1] = HW_NCPU;
122 len = sizeof(ncpu);
123 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1)
124 return;
125 if (ncpu > 1)
126 return;
127 if (rasctl(RAS_ADDR(_atomic_cas), RAS_SIZE(_atomic_cas),
128 RAS_INSTALL) == 0) {
129 _atomic_cas_fn = _atomic_cas_up;
130 return;
131 }
132 }
133
134 #undef atomic_cas_32
135 #undef atomic_cas_uint
136 #undef atomic_cas_ulong
137 #undef atomic_cas_ptr
138 #undef atomic_cas_32_ni
139 #undef atomic_cas_uint_ni
140 #undef atomic_cas_ulong_ni
141 #undef atomic_cas_ptr_ni
142
143 atomic_op_alias(atomic_cas_32,_atomic_cas_32)
144 atomic_op_alias(atomic_cas_uint,_atomic_cas_32)
145 __strong_alias(_atomic_cas_uint,_atomic_cas_32)
146 atomic_op_alias(atomic_cas_ulong,_atomic_cas_32)
147 __strong_alias(_atomic_cas_ulong,_atomic_cas_32)
148 atomic_op_alias(atomic_cas_ptr,_atomic_cas_32)
149 __strong_alias(_atomic_cas_ptr,_atomic_cas_32)
150
151 atomic_op_alias(atomic_cas_32_ni,_atomic_cas_32)
152 __strong_alias(_atomic_cas_32_ni,_atomic_cas_32)
153 atomic_op_alias(atomic_cas_uint_ni,_atomic_cas_32)
154 __strong_alias(_atomic_cas_uint_ni,_atomic_cas_32)
155 atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_32)
156 __strong_alias(_atomic_cas_ulong_ni,_atomic_cas_32)
157 atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_32)
158 __strong_alias(_atomic_cas_ptr_ni,_atomic_cas_32)
159