Commit | Line | Data |
---|---|---|
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> |
6982d6d7 | 11 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
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 |
36 | extern "C" { | |
37 | #endif | |
38 | ||
6ee8df54 MD |
39 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2) |
40 | #define COMPILER_HAVE_SHORT_MEM_OPERAND | |
41 | #endif | |
42 | ||
43 | /* | |
44 | * MEMOP assembler operand rules: | |
45 | * - op refer to MEMOP_IN operand | |
46 | * - MEMOP_IN can expand to more than a single operand. Use it at the end of | |
47 | * operand list only. | |
48 | */ | |
49 | ||
50 | #ifdef COMPILER_HAVE_SHORT_MEM_OPERAND | |
51 | ||
52 | #define MEMOP_OUT(addr) "=Q" (*(addr)) | |
a44291d8 | 53 | #define MEMOP_IN(addr) "Q" (*(addr)) |
0cdbb97c | 54 | #define MEMOP_REF(op) #op /* op refer to MEMOP_IN operand */ |
6ee8df54 MD |
55 | |
56 | #else /* !COMPILER_HAVE_SHORT_MEM_OPERAND */ | |
57 | ||
58 | #define MEMOP_OUT(addr) "=m" (*(addr)) | |
a44291d8 | 59 | #define MEMOP_IN(addr) "a" (addr), "m" (*(addr)) |
6ee8df54 MD |
60 | #define MEMOP_REF(op) "0(" #op ")" /* op refer to MEMOP_IN operand */ |
61 | ||
62 | #endif /* !COMPILER_HAVE_SHORT_MEM_OPERAND */ | |
63 | ||
53b8ed68 MD |
64 | struct __uatomic_dummy { |
65 | unsigned long v[10]; | |
66 | }; | |
67 | #define __hp(x) ((struct __uatomic_dummy *)(x)) | |
68 | ||
9c697e4d | 69 | /* xchg */ |
f64acda4 MD |
70 | |
71 | static inline __attribute__((always_inline)) | |
ec4e58a3 | 72 | unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len) |
ac26f1a8 JB |
73 | { |
74 | switch (len) { | |
75 | case 4: | |
2837ec40 | 76 | { |
9c697e4d MD |
77 | unsigned int old_val; |
78 | ||
79 | __asm__ __volatile__( | |
6ee8df54 | 80 | "0: cs %0,%2," MEMOP_REF(%3) "\n" |
9c697e4d | 81 | " brc 4,0b\n" |
53b8ed68 MD |
82 | : "=&r" (old_val), MEMOP_OUT (__hp(addr)) |
83 | : "r" (val), MEMOP_IN (__hp(addr)) | |
9c697e4d | 84 | : "memory", "cc"); |
1cf421dc | 85 | return old_val; |
2837ec40 | 86 | } |
ac26f1a8 JB |
87 | #if (BITS_PER_LONG == 64) |
88 | case 8: | |
2837ec40 | 89 | { |
9c697e4d MD |
90 | unsigned long old_val; |
91 | ||
92 | __asm__ __volatile__( | |
6ee8df54 | 93 | "0: csg %0,%2," MEMOP_REF(%3) "\n" |
9c697e4d | 94 | " brc 4,0b\n" |
53b8ed68 MD |
95 | : "=&r" (old_val), MEMOP_OUT (__hp(addr)) |
96 | : "r" (val), MEMOP_IN (__hp(addr)) | |
9c697e4d | 97 | : "memory", "cc"); |
1cf421dc | 98 | return old_val; |
2837ec40 | 99 | } |
ac26f1a8 JB |
100 | #endif |
101 | default: | |
102 | __asm__ __volatile__(".long 0xd00d00"); | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
9c697e4d | 108 | #define uatomic_xchg(addr, v) \ |
ec4e58a3 | 109 | (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \ |
a9a05d42 | 110 | sizeof(*(addr))) |
ac26f1a8 | 111 | |
9c697e4d | 112 | /* cmpxchg */ |
8af57509 JB |
113 | |
114 | static inline __attribute__((always_inline)) | |
9c697e4d | 115 | unsigned long _uatomic_cmpxchg(void *addr, unsigned long old, |
1cf421dc | 116 | unsigned long _new, int len) |
8af57509 JB |
117 | { |
118 | switch (len) { | |
119 | case 4: | |
2837ec40 | 120 | { |
9c697e4d MD |
121 | unsigned int old_val = (unsigned int)old; |
122 | ||
123 | __asm__ __volatile__( | |
6ee8df54 | 124 | " cs %0,%2," MEMOP_REF(%3) "\n" |
53b8ed68 MD |
125 | : "+r" (old_val), MEMOP_OUT (__hp(addr)) |
126 | : "r" (_new), MEMOP_IN (__hp(addr)) | |
9c697e4d MD |
127 | : "memory", "cc"); |
128 | return old_val; | |
2837ec40 | 129 | } |
8af57509 JB |
130 | #if (BITS_PER_LONG == 64) |
131 | case 8: | |
f64acda4 | 132 | { |
9c697e4d | 133 | __asm__ __volatile__( |
6ee8df54 | 134 | " csg %0,%2," MEMOP_REF(%3) "\n" |
53b8ed68 MD |
135 | : "+r" (old), MEMOP_OUT (__hp(addr)) |
136 | : "r" (_new), MEMOP_IN (__hp(addr)) | |
9c697e4d MD |
137 | : "memory", "cc"); |
138 | return old; | |
f64acda4 | 139 | } |
8af57509 JB |
140 | #endif |
141 | default: | |
142 | __asm__ __volatile__(".long 0xd00d00"); | |
143 | } | |
144 | ||
9c697e4d | 145 | return 0; |
8af57509 JB |
146 | } |
147 | ||
1cf421dc | 148 | #define uatomic_cmpxchg(addr, old, _new) \ |
9c697e4d MD |
149 | (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \ |
150 | (unsigned long)(old), \ | |
1cf421dc | 151 | (unsigned long)(_new), \ |
9c697e4d | 152 | sizeof(*(addr))) |
8af57509 | 153 | |
36bc70a8 MD |
154 | #ifdef __cplusplus |
155 | } | |
156 | #endif | |
157 | ||
8760d94e PB |
158 | #include <urcu/uatomic_generic.h> |
159 | ||
9c697e4d | 160 | #endif /* _URCU_UATOMIC_ARCH_S390_H */ |