summaryrefslogtreecommitdiff
path: root/test/test_atomic.c
blob: ba313753b2701733b8f9591efe011da635c9f18c (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (c) 2018 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * This test makes sure that atomic operations behave at least as their
 * non-atomic counterpart. It doesn't actually test the atomicity of the
 * operations, but rather helps check that the generated code matches
 * expectations for the target configuration.
 */

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include <kern/atomic.h>
#include <kern/error.h>
#include <kern/init.h>
#include <kern/log.h>
#include <kern/macros.h>
#include <test/test.h>

#ifdef CONFIG_TEST_MODULE_ATOMIC_64
typedef uint64_t test_int_t;
#else
typedef unsigned long test_int_t;
#endif

/*
 * Don't make this variable private, so that the compiler may not assume
 * that all accesses are known, which forces it to generate access
 * instructions for it. This only works when building without LTO.
 */
test_int_t test_n;

/*
 * Prevent these functions from being inlined in order to make the generated
 * code easy to find and review.
 */

static __noinline test_int_t
test_atomic_load(test_int_t *n)
{
    return atomic_load(n, ATOMIC_RELAXED);
}

static __noinline void
test_atomic_store(test_int_t *n, test_int_t val)
{
    atomic_store(n, val, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_cas(test_int_t *n, test_int_t oval, test_int_t nval)
{
    return atomic_cas(n, oval, nval, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_swap(test_int_t *n, test_int_t val)
{
    return atomic_swap(n, val, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_fetch_add(test_int_t *n, test_int_t val)
{
    return atomic_fetch_add(n, val, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_fetch_sub(test_int_t *n, test_int_t val)
{
    return atomic_fetch_sub(n, val, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_fetch_and(test_int_t *n, test_int_t val)
{
    return atomic_fetch_and(n, val, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_fetch_or(test_int_t *n, test_int_t val)
{
    return atomic_fetch_or(n, val, ATOMIC_RELAXED);
}

static __noinline test_int_t
test_atomic_fetch_xor(test_int_t *n, test_int_t val)
{
    return atomic_fetch_xor(n, val, ATOMIC_RELAXED);
}

static __noinline void
test_atomic_add(test_int_t *n, test_int_t val)
{
    atomic_add(n, val, ATOMIC_RELAXED);
}

static __noinline void
test_atomic_sub(test_int_t *n, test_int_t val)
{
    atomic_sub(n, val, ATOMIC_RELAXED);
}

static __noinline void
test_atomic_and(test_int_t *n, test_int_t val)
{
    atomic_and(n, val, ATOMIC_RELAXED);
}

static __noinline void
test_atomic_or(test_int_t *n, test_int_t val)
{
    atomic_or(n, val, ATOMIC_RELAXED);
}

static __noinline void
test_atomic_xor(test_int_t *n, test_int_t val)
{
    atomic_xor(n, val, ATOMIC_RELAXED);
}

static void
test_check_n(test_int_t val, const char *fn)
{
    volatile test_int_t *ptr;
    int error;

    ptr = &test_n;
    error = (val == *ptr) ? 0 : EINVAL;
    error_check(error, fn);
}

static void
test_load(void)
{
    test_int_t n;

    n = test_atomic_load(&test_n);
    test_check_n(n, __func__);
}

static void
test_store(void)
{
    test_int_t val;

    val = 0x123;
    test_atomic_store(&test_n, val);
    test_check_n(val, __func__);
}

static void
test_cas_match(void)
{
    test_int_t prev, oval, nval;
    int error;

    oval = 0;
    nval = 0x123;
    test_n = oval;
    prev = test_atomic_cas(&test_n, oval, nval);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(nval, __func__);
}

static void
test_cas_nomatch(void)
{
    test_int_t prev, oval, nval;
    int error;

    oval = 0;
    nval = 0x123;
    test_n = oval;
    prev = test_atomic_cas(&test_n, oval + 1, nval);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(oval, __func__);
}

static void
test_swap(void)
{
    test_int_t prev, oval, nval;
    int error;

    oval = 0;
    nval = 0x123;
    test_n = oval;
    prev = test_atomic_swap(&test_n, nval);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(nval, __func__);
}

static void
test_fetch_add(void)
{
    test_int_t prev, oval, delta;
    int error;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    prev = test_atomic_fetch_add(&test_n, delta);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(oval + delta, __func__);
}

static void
test_fetch_sub(void)
{
    test_int_t prev, oval, delta;
    int error;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    prev = test_atomic_fetch_sub(&test_n, delta);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(oval - delta, __func__);
}

static void
test_fetch_and(void)
{
    test_int_t prev, oval, delta;
    int error;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    prev = test_atomic_fetch_and(&test_n, delta);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(oval & delta, __func__);
}

static void
test_fetch_or(void)
{
    test_int_t prev, oval, delta;
    int error;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    prev = test_atomic_fetch_or(&test_n, delta);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(oval | delta, __func__);
}

static void
test_fetch_xor(void)
{
    test_int_t prev, oval, delta;
    int error;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    prev = test_atomic_fetch_xor(&test_n, delta);
    error = (prev == oval) ? 0 : EINVAL;
    error_check(error, __func__);
    test_check_n(oval ^ delta, __func__);
}

static void
test_add(void)
{
    test_int_t oval, delta;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    test_atomic_add(&test_n, delta);
    test_check_n(oval + delta, __func__);
}

static void
test_sub(void)
{
    test_int_t oval, delta;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    test_atomic_sub(&test_n, delta);
    test_check_n(oval - delta, __func__);
}

static void
test_and(void)
{
    test_int_t oval, delta;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    test_atomic_and(&test_n, delta);
    test_check_n(oval & delta, __func__);
}

static void
test_or(void)
{
    test_int_t oval, delta;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    test_atomic_or(&test_n, delta);
    test_check_n(oval | delta, __func__);
}

static void
test_xor(void)
{
    test_int_t oval, delta;

    oval = 0x123;
    delta = 0x456;
    test_n = oval;
    test_atomic_xor(&test_n, delta);
    test_check_n(oval ^ delta, __func__);
}

void __init
test_setup(void)
{
    test_load();
    test_store();
    test_cas_match();
    test_cas_nomatch();
    test_swap();
    test_fetch_add();
    test_fetch_sub();
    test_fetch_and();
    test_fetch_or();
    test_fetch_xor();
    test_add();
    test_sub();
    test_and();
    test_or();
    test_xor();
    log_info("test: done");
}