Add extern "C" to support linking userspace RCU library with C++ applications
[urcu.git] / urcu / uatomic_arch_s390.h
CommitLineData
9c697e4d
MD
1#ifndef _URCU_UATOMIC_ARCH_S390_H
2#define _URCU_UATOMIC_ARCH_S390_H
ac26f1a8
JB
3
4/*
7039fa6f
JB
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)".
ac26f1a8 8 *
7039fa6f 9 * Copyright (c) 2009 Novell, Inc.
ac26f1a8 10 * Author: Jan Blunck <jblunck@suse.de>
9c697e4d 11 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
ac26f1a8 12 *
7039fa6f
JB
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:
ac26f1a8 19 *
7039fa6f
JB
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.
ac26f1a8
JB
30 */
31
b46b23cb
MD
32#include <urcu/compiler.h>
33#include <urcu/system.h>
34
36bc70a8
MD
35#ifdef __cplusplus
36extern "C" {
37#endif
38
ac26f1a8
JB
39#ifndef __SIZEOF_LONG__
40#ifdef __s390x__
41#define __SIZEOF_LONG__ 8
42#else
43#define __SIZEOF_LONG__ 4
44#endif
45#endif
46
47#ifndef BITS_PER_LONG
48#define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
49#endif
50
6ee8df54
MD
51#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
52#define COMPILER_HAVE_SHORT_MEM_OPERAND
53#endif
54
55/*
56 * MEMOP assembler operand rules:
57 * - op refer to MEMOP_IN operand
58 * - MEMOP_IN can expand to more than a single operand. Use it at the end of
59 * operand list only.
60 */
61
62#ifdef COMPILER_HAVE_SHORT_MEM_OPERAND
63
64#define MEMOP_OUT(addr) "=Q" (*(addr))
65#define MEMOP_IN "Q" (*(addr))
0cdbb97c 66#define MEMOP_REF(op) #op /* op refer to MEMOP_IN operand */
6ee8df54
MD
67
68#else /* !COMPILER_HAVE_SHORT_MEM_OPERAND */
69
70#define MEMOP_OUT(addr) "=m" (*(addr))
71#define MEMOP_IN "a" (addr), "m" (*(addr))
72#define MEMOP_REF(op) "0(" #op ")" /* op refer to MEMOP_IN operand */
73
74#endif /* !COMPILER_HAVE_SHORT_MEM_OPERAND */
75
53b8ed68
MD
76struct __uatomic_dummy {
77 unsigned long v[10];
78};
79#define __hp(x) ((struct __uatomic_dummy *)(x))
80
b46b23cb
MD
81#define uatomic_set(addr, v) STORE_SHARED(*(addr), (v))
82#define uatomic_read(addr) LOAD_SHARED(*(addr))
8af57509 83
9c697e4d 84/* xchg */
f64acda4
MD
85
86static inline __attribute__((always_inline))
ec4e58a3 87unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
ac26f1a8
JB
88{
89 switch (len) {
90 case 4:
2837ec40 91 {
9c697e4d
MD
92 unsigned int old_val;
93
94 __asm__ __volatile__(
6ee8df54 95 "0: cs %0,%2," MEMOP_REF(%3) "\n"
9c697e4d 96 " brc 4,0b\n"
53b8ed68
MD
97 : "=&r" (old_val), MEMOP_OUT (__hp(addr))
98 : "r" (val), MEMOP_IN (__hp(addr))
9c697e4d 99 : "memory", "cc");
1cf421dc 100 return old_val;
2837ec40 101 }
ac26f1a8
JB
102#if (BITS_PER_LONG == 64)
103 case 8:
2837ec40 104 {
9c697e4d
MD
105 unsigned long old_val;
106
107 __asm__ __volatile__(
6ee8df54 108 "0: csg %0,%2," MEMOP_REF(%3) "\n"
9c697e4d 109 " brc 4,0b\n"
53b8ed68
MD
110 : "=&r" (old_val), MEMOP_OUT (__hp(addr))
111 : "r" (val), MEMOP_IN (__hp(addr))
9c697e4d 112 : "memory", "cc");
1cf421dc 113 return old_val;
2837ec40 114 }
ac26f1a8
JB
115#endif
116 default:
117 __asm__ __volatile__(".long 0xd00d00");
118 }
119
120 return 0;
121}
122
9c697e4d 123#define uatomic_xchg(addr, v) \
ec4e58a3 124 (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
a9a05d42 125 sizeof(*(addr)))
ac26f1a8 126
9c697e4d 127/* cmpxchg */
8af57509
JB
128
129static inline __attribute__((always_inline))
9c697e4d 130unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
1cf421dc 131 unsigned long _new, int len)
8af57509
JB
132{
133 switch (len) {
134 case 4:
2837ec40 135 {
9c697e4d
MD
136 unsigned int old_val = (unsigned int)old;
137
138 __asm__ __volatile__(
6ee8df54 139 " cs %0,%2," MEMOP_REF(%3) "\n"
53b8ed68
MD
140 : "+r" (old_val), MEMOP_OUT (__hp(addr))
141 : "r" (_new), MEMOP_IN (__hp(addr))
9c697e4d
MD
142 : "memory", "cc");
143 return old_val;
2837ec40 144 }
8af57509
JB
145#if (BITS_PER_LONG == 64)
146 case 8:
f64acda4 147 {
9c697e4d 148 __asm__ __volatile__(
6ee8df54 149 " csg %0,%2," MEMOP_REF(%3) "\n"
53b8ed68
MD
150 : "+r" (old), MEMOP_OUT (__hp(addr))
151 : "r" (_new), MEMOP_IN (__hp(addr))
9c697e4d
MD
152 : "memory", "cc");
153 return old;
f64acda4 154 }
8af57509
JB
155#endif
156 default:
157 __asm__ __volatile__(".long 0xd00d00");
158 }
159
9c697e4d 160 return 0;
8af57509
JB
161}
162
1cf421dc 163#define uatomic_cmpxchg(addr, old, _new) \
9c697e4d
MD
164 (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
165 (unsigned long)(old), \
1cf421dc 166 (unsigned long)(_new), \
9c697e4d 167 sizeof(*(addr)))
8af57509 168
9c697e4d 169/* uatomic_add_return */
8af57509
JB
170
171static inline __attribute__((always_inline))
9c697e4d 172unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
8af57509
JB
173{
174 switch (len) {
175 case 4:
9c697e4d
MD
176 {
177 unsigned int old, oldt;
178
179 oldt = uatomic_read((unsigned int *)addr);
180 do {
181 old = oldt;
182 oldt = _uatomic_cmpxchg(addr, old, old + val, 4);
183 } while (oldt != old);
184
185 return old + val;
186 }
8af57509
JB
187#if (BITS_PER_LONG == 64)
188 case 8:
9c697e4d
MD
189 {
190 unsigned long old, oldt;
191
192 oldt = uatomic_read((unsigned long *)addr);
193 do {
194 old = oldt;
195 oldt = _uatomic_cmpxchg(addr, old, old + val, 8);
196 } while (oldt != old);
197
198 return old + val;
199 }
8af57509 200#endif
8af57509 201 }
9c697e4d 202 __builtin_trap();
8af57509
JB
203 return 0;
204}
205
9c697e4d
MD
206#define uatomic_add_return(addr, v) \
207 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
208 (unsigned long)(v), \
209 sizeof(*(addr))))
210
211/* uatomic_sub_return, uatomic_add, uatomic_sub, uatomic_inc, uatomic_dec */
212
213#define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
214
215#define uatomic_add(addr, v) (void)uatomic_add_return((addr), (v))
216#define uatomic_sub(addr, v) (void)uatomic_sub_return((addr), (v))
217
218#define uatomic_inc(addr) uatomic_add((addr), 1)
219#define uatomic_dec(addr) uatomic_add((addr), -1)
8af57509 220
9c697e4d 221#define compat_uatomic_cmpxchg(ptr, old, _new) uatomic_cmpxchg(ptr, old, _new)
7d413817 222
36bc70a8
MD
223#ifdef __cplusplus
224}
225#endif
226
9c697e4d 227#endif /* _URCU_UATOMIC_ARCH_S390_H */
This page took 0.032893 seconds and 4 git commands to generate.