Add CMM memory model
[urcu.git] / include / urcu / uatomic / s390.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2009 Novell, Inc.
2// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3//
4// SPDX-License-Identifier: MIT
5
9c697e4d
MD
6#ifndef _URCU_UATOMIC_ARCH_S390_H
7#define _URCU_UATOMIC_ARCH_S390_H
ac26f1a8
JB
8
9/*
7039fa6f
JB
10 * Atomic exchange operations for the S390 architecture. Based on information
11 * taken from the Principles of Operation Appendix A "Conditional Swapping
12 * Instructions (CS, CDS)".
ac26f1a8 13 *
ac26f1a8 14 * Author: Jan Blunck <jblunck@suse.de>
ac26f1a8
JB
15 */
16
b46b23cb
MD
17#include <urcu/compiler.h>
18#include <urcu/system.h>
19
36bc70a8
MD
20#ifdef __cplusplus
21extern "C" {
67ecffc0 22#endif
36bc70a8 23
6ee8df54
MD
24#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
25#define COMPILER_HAVE_SHORT_MEM_OPERAND
26#endif
27
28/*
29 * MEMOP assembler operand rules:
30 * - op refer to MEMOP_IN operand
31 * - MEMOP_IN can expand to more than a single operand. Use it at the end of
32 * operand list only.
33 */
34
35#ifdef COMPILER_HAVE_SHORT_MEM_OPERAND
36
37#define MEMOP_OUT(addr) "=Q" (*(addr))
a44291d8 38#define MEMOP_IN(addr) "Q" (*(addr))
0cdbb97c 39#define MEMOP_REF(op) #op /* op refer to MEMOP_IN operand */
6ee8df54
MD
40
41#else /* !COMPILER_HAVE_SHORT_MEM_OPERAND */
42
43#define MEMOP_OUT(addr) "=m" (*(addr))
a44291d8 44#define MEMOP_IN(addr) "a" (addr), "m" (*(addr))
6ee8df54
MD
45#define MEMOP_REF(op) "0(" #op ")" /* op refer to MEMOP_IN operand */
46
47#endif /* !COMPILER_HAVE_SHORT_MEM_OPERAND */
48
835b9ab3 49/*
71323499 50 * The __hp() macro casts the void pointer @x to a pointer to a structure
835b9ab3
MD
51 * containing an array of char of the specified size. This allows passing the
52 * @addr arguments of the following inline functions as "m" and "+m" operands
71323499 53 * to the assembly. The @size parameter should be a constant to support
13bf2f57
MD
54 * compilers such as clang which do not support VLA. Create typedefs because
55 * C++ does not allow types be defined in casts.
835b9ab3
MD
56 */
57
13bf2f57
MD
58typedef struct { char v[4]; } __hp_4;
59typedef struct { char v[8]; } __hp_8;
60
61#define __hp(size, x) ((__hp_##size *)(x))
53b8ed68 62
9c697e4d 63/* xchg */
f64acda4
MD
64
65static inline __attribute__((always_inline))
ec4e58a3 66unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
ac26f1a8
JB
67{
68 switch (len) {
69 case 4:
2837ec40 70 {
9c697e4d
MD
71 unsigned int old_val;
72
73 __asm__ __volatile__(
6ee8df54 74 "0: cs %0,%2," MEMOP_REF(%3) "\n"
9c697e4d 75 " brc 4,0b\n"
71323499
MD
76 : "=&r" (old_val), MEMOP_OUT (__hp(4, addr))
77 : "r" (val), MEMOP_IN (__hp(4, addr))
9c697e4d 78 : "memory", "cc");
1cf421dc 79 return old_val;
2837ec40 80 }
b39e1761 81#if (CAA_BITS_PER_LONG == 64)
ac26f1a8 82 case 8:
2837ec40 83 {
9c697e4d
MD
84 unsigned long old_val;
85
86 __asm__ __volatile__(
6ee8df54 87 "0: csg %0,%2," MEMOP_REF(%3) "\n"
9c697e4d 88 " brc 4,0b\n"
71323499
MD
89 : "=&r" (old_val), MEMOP_OUT (__hp(8, addr))
90 : "r" (val), MEMOP_IN (__hp(8, addr))
9c697e4d 91 : "memory", "cc");
1cf421dc 92 return old_val;
2837ec40 93 }
ac26f1a8
JB
94#endif
95 default:
96 __asm__ __volatile__(".long 0xd00d00");
97 }
98
99 return 0;
100}
101
9c697e4d 102#define uatomic_xchg(addr, v) \
e56d99bf
MD
103 (__typeof__(*(addr))) _uatomic_exchange((addr), \
104 caa_cast_long_keep_sign(v), \
105 sizeof(*(addr)))
ac26f1a8 106
9c697e4d 107/* cmpxchg */
8af57509
JB
108
109static inline __attribute__((always_inline))
9c697e4d 110unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
1cf421dc 111 unsigned long _new, int len)
8af57509
JB
112{
113 switch (len) {
114 case 4:
2837ec40 115 {
9c697e4d
MD
116 unsigned int old_val = (unsigned int)old;
117
118 __asm__ __volatile__(
6ee8df54 119 " cs %0,%2," MEMOP_REF(%3) "\n"
71323499
MD
120 : "+r" (old_val), MEMOP_OUT (__hp(4, addr))
121 : "r" (_new), MEMOP_IN (__hp(4, addr))
9c697e4d
MD
122 : "memory", "cc");
123 return old_val;
2837ec40 124 }
b39e1761 125#if (CAA_BITS_PER_LONG == 64)
8af57509 126 case 8:
f64acda4 127 {
9c697e4d 128 __asm__ __volatile__(
6ee8df54 129 " csg %0,%2," MEMOP_REF(%3) "\n"
71323499
MD
130 : "+r" (old), MEMOP_OUT (__hp(8, addr))
131 : "r" (_new), MEMOP_IN (__hp(8, addr))
9c697e4d
MD
132 : "memory", "cc");
133 return old;
f64acda4 134 }
8af57509
JB
135#endif
136 default:
137 __asm__ __volatile__(".long 0xd00d00");
138 }
139
9c697e4d 140 return 0;
8af57509
JB
141}
142
e56d99bf
MD
143#define uatomic_cmpxchg(addr, old, _new) \
144 (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
145 caa_cast_long_keep_sign(old), \
146 caa_cast_long_keep_sign(_new),\
9c697e4d 147 sizeof(*(addr)))
8af57509 148
67ecffc0 149#ifdef __cplusplus
36bc70a8
MD
150}
151#endif
152
a2e7bf9c 153#include <urcu/uatomic/generic.h>
8760d94e 154
9c697e4d 155#endif /* _URCU_UATOMIC_ARCH_S390_H */
This page took 0.057772 seconds and 4 git commands to generate.