f8e8dffa6c04e5d2e74599fea8b3c9c11dddf8f4
[urcu.git] / urcu / uatomic_arch_s390.h
1 #ifndef _URCU_UATOMIC_ARCH_S390_H
2 #define _URCU_UATOMIC_ARCH_S390_H
3
4 /*
5 * Atomic exchange operations for the S390 architecture. Based on information
6 * taken from the Principles of Operation Appendix A "Conditional Swapping
7 * Instructions (CS, CDS)".
8 *
9 * Copyright (c) 2009 Novell, Inc.
10 * Author: Jan Blunck <jblunck@suse.de>
11 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to
15 * deal in the Software without restriction, including without limitation the
16 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
17 * sell copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
31
32 #include <urcu/compiler.h>
33 #include <urcu/system.h>
34
35 #ifndef __SIZEOF_LONG__
36 #ifdef __s390x__
37 #define __SIZEOF_LONG__ 8
38 #else
39 #define __SIZEOF_LONG__ 4
40 #endif
41 #endif
42
43 #ifndef BITS_PER_LONG
44 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
45 #endif
46
47 #define uatomic_set(addr, v) STORE_SHARED(*(addr), (v))
48 #define uatomic_read(addr) LOAD_SHARED(*(addr))
49
50 /* xchg */
51
52 static inline __attribute__((always_inline))
53 unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
54 {
55 switch (len) {
56 case 4:
57 {
58 unsigned int old_val;
59
60 __asm__ __volatile__(
61 "0: cs %0,%2,%1\n"
62 " brc 4,0b\n"
63 : "=&r"(old_val), "=m" (*addr)
64 : "r"(val), "m" (*addr)
65 : "memory", "cc");
66 }
67 #if (BITS_PER_LONG == 64)
68 case 8:
69 {
70 unsigned long old_val;
71
72 __asm__ __volatile__(
73 "0: csg %0,%2,%1\n"
74 " brc 4,0b\n"
75 : "=&r"(old_val), "=m" (*addr)
76 : "r"(val), "m" (*addr)
77 : "memory", "cc");
78 }
79 #endif
80 default:
81 __asm__ __volatile__(".long 0xd00d00");
82 }
83
84 return 0;
85 }
86
87 #define uatomic_xchg(addr, v) \
88 (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
89 sizeof(*(addr)))
90
91 /* cmpxchg */
92
93 static inline __attribute__((always_inline))
94 unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
95 unsigned long new, int len)
96 {
97 switch (len) {
98 case 4:
99 {
100 unsigned int old_val = (unsigned int)old;
101
102 __asm__ __volatile__(
103 " cs %0,%2,%1\n"
104 : "+r"(old_val), "+m"(*addr)
105 : "r"(new)
106 : "memory", "cc");
107 return old_val;
108 }
109 #if (BITS_PER_LONG == 64)
110 case 8:
111 {
112 __asm__ __volatile__(
113 " csg %0,%2,%1\n"
114 : "+r"(old), "+m"(*addr)
115 : "r"(new)
116 : "memory", "cc");
117 return old;
118 }
119 #endif
120 default:
121 __asm__ __volatile__(".long 0xd00d00");
122 }
123
124 return 0;
125 }
126
127 #define uatomic_cmpxchg(addr, old, new) \
128 (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
129 (unsigned long)(old), \
130 (unsigned long)(new), \
131 sizeof(*(addr)))
132
133 /* uatomic_add_return */
134
135 static inline __attribute__((always_inline))
136 unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
137 {
138 switch (len) {
139 case 4:
140 {
141 unsigned int old, oldt;
142
143 oldt = uatomic_read((unsigned int *)addr);
144 do {
145 old = oldt;
146 oldt = _uatomic_cmpxchg(addr, old, old + val, 4);
147 } while (oldt != old);
148
149 return old + val;
150 }
151 #if (BITS_PER_LONG == 64)
152 case 8:
153 {
154 unsigned long old, oldt;
155
156 oldt = uatomic_read((unsigned long *)addr);
157 do {
158 old = oldt;
159 oldt = _uatomic_cmpxchg(addr, old, old + val, 8);
160 } while (oldt != old);
161
162 return old + val;
163 }
164 #endif
165 }
166 __builtin_trap();
167 return 0;
168 }
169
170 #define uatomic_add_return(addr, v) \
171 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
172 (unsigned long)(v), \
173 sizeof(*(addr))))
174
175 /* uatomic_sub_return, uatomic_add, uatomic_sub, uatomic_inc, uatomic_dec */
176
177 #define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
178
179 #define uatomic_add(addr, v) (void)uatomic_add_return((addr), (v))
180 #define uatomic_sub(addr, v) (void)uatomic_sub_return((addr), (v))
181
182 #define uatomic_inc(addr) uatomic_add((addr), 1)
183 #define uatomic_dec(addr) uatomic_add((addr), -1)
184
185 #define compat_uatomic_cmpxchg(ptr, old, _new) uatomic_cmpxchg(ptr, old, _new)
186
187 #endif /* _URCU_UATOMIC_ARCH_S390_H */
This page took 0.032711 seconds and 4 git commands to generate.