summaryrefslogtreecommitdiff
path: root/usr/src/compat/freebsd/amd64/machine/atomic.h
blob: 47a26bd2d3af8ba4bffc2d7be8f765506f4a8494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2014 Pluribus Networks Inc.
 * Copyright 2017 Joyent, Inc.
 */

#ifndef _COMPAT_FREEBSD_AMD64_MACHINE_ATOMIC_H_
#define	_COMPAT_FREEBSD_AMD64_MACHINE_ATOMIC_H_

static __inline u_char
atomic_load_acq_char(volatile u_char *p)
{
	u_char res;

	__asm volatile("lock ; " "cmpxchgb %b0,%1"
		       : "=a" (res), "=m" (*p)
		       : "m" (*p) : "memory", "cc");
	return (res);
}

static __inline u_short
atomic_load_acq_short(volatile u_short *p)
{
	u_short res;

	__asm volatile("lock ; " "cmpxchgw %w0,%1"
		       : "=a" (res), "=m" (*p)
		       : "m" (*p)
		       : "memory", "cc");
	return (res);
}

static __inline u_int
atomic_load_acq_int(volatile u_int *p)
{
	u_int res;

	__asm volatile("lock ; " "cmpxchgl %0,%1"
		       : "=a" (res), "=m" (*p)
		       : "m" (*p)
		       : "memory", "cc");
	return (res);
}

static __inline u_long
atomic_load_acq_long(volatile u_long *p)
{
	u_long res;

	__asm volatile("lock ; " "cmpxchgq %0,%1"
		       : "=a" (res), "=m" (*p)
		       : "m" (*p)
		       : "memory", "cc");
	return (res);
}

static __inline void
atomic_store_rel_char(volatile u_char *p, u_char v)
{
	__asm volatile("" : : : "memory");
	*p = v;
}

static __inline void
atomic_store_rel_short(volatile u_short *p, u_short v)
{
	__asm volatile("" : : : "memory");
	*p = v;
}

static __inline void
atomic_store_rel_int(volatile u_int *p, u_int v)
{
	__asm volatile("" : : : "memory");
	*p = v;
}

static __inline void
atomic_store_rel_long(volatile u_long *p, u_long v)
{
	__asm volatile("" : : : "memory");
	*p = v;
}

/*
 * Atomic compare and set.
 *
 * if (*dst == expect) *dst = src (all 32 bit words)
 *
 * Returns 0 on failure, non-zero on success
 */
static __inline int
atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src)
{
	u_char res;

	__asm __volatile(
	"	lock ;			"
	"	cmpxchgl %3,%1 ;	"
	"       sete	%0 ;		"
	"# atomic_cmpset_int"
	: "=q" (res),			/* 0 */
	  "+m" (*dst),			/* 1 */
	  "+a" (expect)			/* 2 */
	: "r" (src)			/* 3 */
	: "memory", "cc");
	return (res);
}

static __inline int
atomic_cmpset_long(volatile u_long *dst, u_long expect, u_long src)
{
	u_char res;

	__asm __volatile(
	"	lock ;			"
	"	cmpxchgq %3,%1 ;	"
	"       sete	%0 ;		"
	"# atomic_cmpset_long"
	: "=q" (res),			/* 0 */
	  "+m" (*dst),			/* 1 */
	  "+a" (expect)			/* 2 */
	: "r" (src)			/* 3 */
	: "memory", "cc");
	return (res);
}

/*
 * Atomically add the value of v to the integer pointed to by p and return
 * the previous value of *p.
 */
static __inline u_int
atomic_fetchadd_int(volatile u_int *p, u_int v)
{

	__asm __volatile(
	"	lock ;			"
	"	xaddl	%0, %1 ;	"
	"# atomic_fetchadd_int"
	: "+r" (v),			/* 0 (result) */
	  "=m" (*p)			/* 1 */
	: "m" (*p)			/* 2 */
	: "cc");
	return (v);
}

static __inline void
atomic_set_int(volatile u_int *p, u_int v)
{
	__asm volatile(
	"lock ; " "orl %1,%0"
	: "=m" (*p)
	: "ir" (v), "m" (*p)
	: "cc");
}

static __inline void
atomic_clear_int(volatile u_int *p, u_int v)
{
	__asm volatile(
	"lock ; " "andl %1,%0"
	: "=m" (*p)
	: "ir" (~v), "m" (*p)
	: "cc");
}

static __inline void
atomic_subtract_int(volatile u_int *p, u_int v)
{
	__asm volatile(
	"lock ; " "subl %1,%0"
	: "=m" (*p)
	: "ir" (v), "m" (*p)
	: "cc");
}

static __inline void
atomic_set_long(volatile u_long *p, u_long v)
{
	__asm volatile(
	"lock ; " "orq %1,%0"
	: "+m" (*p)
	: "ir" (v)
	: "cc");
}

static __inline void
atomic_clear_long(volatile u_long *p, u_long v)
{
	__asm volatile("lock ; " "andq %1,%0"
	: "+m" (*p)
	: "ir" (~v)
	: "cc");
}

static __inline u_int
atomic_swap_int(volatile u_int *p, u_int v)
{

	__asm __volatile(
	"	xchgl	%1,%0 ;		"
	"# atomic_swap_int"
	: "+r" (v),			/* 0 */
	  "+m" (*p));			/* 1 */
	return (v);
}

static __inline u_long
atomic_swap_long(volatile u_long *p, u_long v)
{

	__asm __volatile(
	"	xchgq	%1,%0 ;		"
	"# atomic_swap_long"
	: "+r" (v),			/* 0 */
	  "+m" (*p));			/* 1 */
	return (v);
}

#define	atomic_readandclear_int(p)	atomic_swap_int(p, 0)
#define	atomic_readandclear_long(p)	atomic_swap_long(p, 0)

/* Operations on 32-bit double words. */
#define	atomic_load_acq_32	atomic_load_acq_int
#define	atomic_store_rel_32	atomic_store_rel_int
#define	atomic_cmpset_32	atomic_cmpset_int

/* Operations on 64-bit quad words. */
#define	atomic_cmpset_64	atomic_cmpset_long
#define	atomic_readandclear_64	atomic_readandclear_long

/* Operations on pointers. */
#define	atomic_cmpset_ptr	atomic_cmpset_long

#define      mb()    __asm __volatile("mfence;" : : : "memory")

#endif	/* _COMPAT_FREEBSD_AMD64_MACHINE_ATOMIC_H_ */