123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /*
- * 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");
- }
|