atomic_init_testset.c revision 1.4 1 /* $NetBSD: atomic_init_testset.c,v 1.4 2008/04/28 20:22:53 martin 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.4 2008/04/28 20:22:53 martin 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 static uint32_t
63 _atomic_cas_up(volatile uint32_t *ptr, uint32_t old, uint32_t new)
64 {
65 uint32_t ret;
66
67 RAS_START(_atomic_cas);
68 ret = *ptr;
69 if (__predict_false(ret != old)) {
70 return ret;
71 }
72 *ptr = new;
73 RAS_END(_atomic_cas);
74
75 return ret;
76 }
77
78 static uint32_t
79 _atomic_cas_mp(volatile uint32_t *ptr, uint32_t old, uint32_t new)
80 {
81 __cpu_simple_lock_t *lock;
82 uint32_t ret;
83
84 lock = &atomic_locks[((uint32_t)ptr >> 3) & 127];
85 __cpu_simple_lock(lock);
86 ret = *ptr;
87 if (__predict_true(ret == old)) {
88 *ptr = new;
89 }
90 __cpu_simple_unlock(lock);
91
92 return ret;
93 }
94
95 uint32_t
96 _atomic_cas_32(volatile uint32_t *ptr, uint32_t old, uint32_t new)
97 {
98
99 return (*_atomic_cas_fn)(ptr, old, new);
100 }
101
102 void
103 __libc_atomic_init(void)
104 {
105 int ncpu, mib[2];
106 size_t len;
107
108 _atomic_cas_fn = _atomic_cas_mp;
109
110 mib[0] = CTL_HW;
111 mib[1] = HW_NCPU;
112 len = sizeof(ncpu);
113 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1)
114 return;
115 if (ncpu > 1)
116 return;
117 if (rasctl(RAS_ADDR(_atomic_cas), RAS_SIZE(_atomic_cas),
118 RAS_INSTALL) == 0) {
119 _atomic_cas_fn = _atomic_cas_up;
120 return;
121 }
122 }
123
124 #undef atomic_cas_32
125 #undef atomic_cas_uint
126 #undef atomic_cas_ulong
127 #undef atomic_cas_ptr
128
129 atomic_op_alias(atomic_cas_32,_atomic_cas_32)
130 atomic_op_alias(atomic_cas_uint,_atomic_cas_32)
131 __strong_alias(_atomic_cas_uint,_atomic_cas_32)
132 atomic_op_alias(atomic_cas_ulong,_atomic_cas_32)
133 __strong_alias(_atomic_cas_ulong,_atomic_cas_32)
134 atomic_op_alias(atomic_cas_ptr,_atomic_cas_32)
135 __strong_alias(_atomic_cas_ptr,_atomic_cas_32)
136
137 atomic_op_alias(atomic_cas_32_ni,_atomic_cas_32)
138 __strong_alias(_atomic_cas_32_ni,_atomic_cas_32)
139 atomic_op_alias(atomic_cas_uint_ni,_atomic_cas_32)
140 __strong_alias(_atomic_cas_uint_ni,_atomic_cas_32)
141 atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_32)
142 __strong_alias(_atomic_cas_ulong_ni,_atomic_cas_32)
143 atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_32)
144 __strong_alias(_atomic_cas_ptr_ni,_atomic_cas_32)
145