Home | History | Annotate | Line # | Download | only in atomic
      1  1.2  riastrad /*	$NetBSD: t___sync_lock.c,v 1.2 2025/04/07 01:34:43 riastradh Exp $	*/
      2  1.1     isaki 
      3  1.1     isaki /*
      4  1.1     isaki  * Copyright (C) 2019 Tetsuya Isaki. All rights reserved.
      5  1.1     isaki  *
      6  1.1     isaki  * Redistribution and use in source and binary forms, with or without
      7  1.1     isaki  * modification, are permitted provided that the following conditions
      8  1.1     isaki  * are met:
      9  1.1     isaki  * 1. Redistributions of source code must retain the above copyright
     10  1.1     isaki  *    notice, this list of conditions and the following disclaimer.
     11  1.1     isaki  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1     isaki  *    notice, this list of conditions and the following disclaimer in the
     13  1.1     isaki  *    documentation and/or other materials provided with the distribution.
     14  1.1     isaki  *
     15  1.1     isaki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1     isaki  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1     isaki  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1     isaki  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1     isaki  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1     isaki  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1     isaki  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1     isaki  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1     isaki  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1     isaki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1     isaki  * SUCH DAMAGE.
     26  1.1     isaki  */
     27  1.1     isaki 
     28  1.1     isaki #include <sys/cdefs.h>
     29  1.2  riastrad __RCSID("$NetBSD: t___sync_lock.c,v 1.2 2025/04/07 01:34:43 riastradh Exp $");
     30  1.1     isaki 
     31  1.1     isaki #include <atf-c.h>
     32  1.1     isaki #include <inttypes.h>
     33  1.1     isaki #include <machine/types.h>	// for __HAVE_ATOMIC64_OPS
     34  1.1     isaki 
     35  1.1     isaki /*
     36  1.1     isaki  * These tests don't examine the atomicity.
     37  1.1     isaki  */
     38  1.1     isaki 
     39  1.1     isaki /* XXX
     40  1.1     isaki  * Depending on a combination of arch and compiler, __sync_* is
     41  1.1     isaki  * implemented as compiler's builtin function.  In that case, even
     42  1.1     isaki  * if libc exports the function symbol, it is not used.  As a result
     43  1.1     isaki  * this tests will examine compiler's builtin functions.
     44  1.1     isaki  * It's better to run only when target is actually in libc.
     45  1.1     isaki  */
     46  1.1     isaki 
     47  1.2  riastrad #if defined __vax__
     48  1.2  riastrad /*
     49  1.2  riastrad  * On VAX, __sync_lock_test_and_set_* test and set the low-order bit
     50  1.2  riastrad  * with BBSSI, and __sync_lock_release_* clear the low-order bit with
     51  1.2  riastrad  * BBCCI, so the other bits are not relevant.
     52  1.2  riastrad  *
     53  1.2  riastrad  * It is possible that, by using values other than 0 and 1, we are
     54  1.2  riastrad  * relying on more than gcc guarantees about __sync_lock_test_and_set_*
     55  1.2  riastrad  * and __sync_lock_release_*.  But, well, if so, we will be alerted by
     56  1.2  riastrad  * a failing test.
     57  1.2  riastrad  */
     58  1.2  riastrad #define INITVAL 0x1122334455667788
     59  1.2  riastrad #define LOCKVAL 1
     60  1.2  riastrad #define LOCKRET 0
     61  1.2  riastrad #define LOCKEDVAL 0x1122334455667789
     62  1.2  riastrad #define UNLOCKEDVAL 0x1122334455667788
     63  1.2  riastrad #elif 0 && defined __hppa__
     64  1.2  riastrad /*
     65  1.2  riastrad  * On HPPA, the native atomic r/m/w instruction, LDCW, atomically loads
     66  1.2  riastrad  * a word and clears it, so the obvious choice is for the unlocked
     67  1.2  riastrad  * state to be nonzero and the locked state to be zero.
     68  1.2  riastrad  *
     69  1.2  riastrad  * But gcc doesn't do that.
     70  1.2  riastrad  *
     71  1.2  riastrad  * Instead, it uses zero for unlocked and nonzero for locked.  So for
     72  1.2  riastrad  * __sync_lock_test_and_set_* it issues an out-of-line call (which on
     73  1.2  riastrad  * NetBSD implements by atomic_swap_N), and for __sync_lock_release_*,
     74  1.2  riastrad  * it issues LDCW on a scratch stack location only as a barrier and
     75  1.2  riastrad  * then issues STW to store a zero.
     76  1.2  riastrad  *
     77  1.2  riastrad  * So we don't use this branch after all.  But I'm leaving it here as a
     78  1.2  riastrad  * reminder to anyone who suspects something might be wrong on HPPA.
     79  1.2  riastrad  */
     80  1.2  riastrad #define INITVAL 0x1122334455667788
     81  1.2  riastrad #define LOCKVAL 0
     82  1.2  riastrad #define LOCKRET 0x1122334455667788
     83  1.2  riastrad #define LOCKEDVAL 0
     84  1.2  riastrad #define UNLOCKEDVAL 1
     85  1.2  riastrad #else
     86  1.2  riastrad /*
     87  1.2  riastrad  * According to GCC documentation at
     88  1.2  riastrad  * <https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/_005f_005fsync-Builtins.html>,
     89  1.2  riastrad  * the only guaranteed supported value for LOCKVAL is 1, and it is not
     90  1.2  riastrad  * guaranteed that __sync_lock_release_* stores zero.  But on many
     91  1.2  riastrad  * architectures other values work too, and __sync_lock_release_* does
     92  1.2  riastrad  * just store zero, so let's test these by default; the exceptions can
     93  1.2  riastrad  * be listed above.
     94  1.2  riastrad  */
     95  1.2  riastrad #define INITVAL 0x1122334455667788
     96  1.2  riastrad #define LOCKVAL 0x8090a0b0c0d0e0f0
     97  1.2  riastrad #define LOCKRET 0x1122334455667788
     98  1.2  riastrad #define LOCKEDVAL 0x8090a0b0c0d0e0f0
     99  1.2  riastrad #define UNLOCKEDVAL 0
    100  1.2  riastrad #endif
    101  1.1     isaki 
    102  1.1     isaki #define atf_sync_tas(NAME, TYPE, FMT) \
    103  1.1     isaki ATF_TC(NAME); \
    104  1.1     isaki ATF_TC_HEAD(NAME, tc) \
    105  1.1     isaki { \
    106  1.1     isaki 	atf_tc_set_md_var(tc, "descr", #NAME); \
    107  1.1     isaki } \
    108  1.1     isaki ATF_TC_BODY(NAME, tc) \
    109  1.1     isaki { \
    110  1.1     isaki 	volatile TYPE val; \
    111  1.1     isaki 	TYPE newval; \
    112  1.1     isaki 	TYPE expval; \
    113  1.1     isaki 	TYPE expres; \
    114  1.1     isaki 	TYPE res; \
    115  1.2  riastrad 	val = (TYPE)INITVAL; \
    116  1.2  riastrad 	newval = (TYPE)LOCKVAL; \
    117  1.2  riastrad 	expval = (TYPE)LOCKEDVAL; \
    118  1.2  riastrad 	expres = (TYPE)LOCKRET; \
    119  1.1     isaki 	res = NAME(&val, newval); \
    120  1.1     isaki 	ATF_REQUIRE_MSG(val == expval, \
    121  1.1     isaki 	    "val expects 0x%" FMT " but 0x%" FMT, expval, val); \
    122  1.1     isaki 	ATF_REQUIRE_MSG(res == expres, \
    123  1.1     isaki 	    "res expects 0x%" FMT " but 0x%" FMT, expres, res); \
    124  1.1     isaki }
    125  1.1     isaki 
    126  1.1     isaki atf_sync_tas(__sync_lock_test_and_set_1, uint8_t,  PRIx8);
    127  1.1     isaki atf_sync_tas(__sync_lock_test_and_set_2, uint16_t, PRIx16);
    128  1.1     isaki atf_sync_tas(__sync_lock_test_and_set_4, uint32_t, PRIx32);
    129  1.1     isaki #ifdef __HAVE_ATOMIC64_OPS
    130  1.1     isaki atf_sync_tas(__sync_lock_test_and_set_8, uint64_t, PRIx64);
    131  1.1     isaki #endif
    132  1.1     isaki 
    133  1.1     isaki #define atf_sync_rel(NAME, TYPE, FMT) \
    134  1.1     isaki ATF_TC(NAME); \
    135  1.1     isaki ATF_TC_HEAD(NAME, tc) \
    136  1.1     isaki { \
    137  1.1     isaki 	atf_tc_set_md_var(tc, "descr", #NAME); \
    138  1.1     isaki } \
    139  1.1     isaki ATF_TC_BODY(NAME, tc) \
    140  1.1     isaki { \
    141  1.1     isaki 	volatile TYPE val; \
    142  1.1     isaki 	TYPE expval; \
    143  1.2  riastrad 	val = (TYPE)LOCKEDVAL; \
    144  1.2  riastrad 	expval = (TYPE)UNLOCKEDVAL; \
    145  1.1     isaki 	NAME(&val); \
    146  1.1     isaki 	ATF_REQUIRE_MSG(val == expval, \
    147  1.1     isaki 	    "val expects 0x%" FMT " but 0x%" FMT, expval, val); \
    148  1.1     isaki }
    149  1.1     isaki 
    150  1.1     isaki atf_sync_rel(__sync_lock_release_1, uint8_t,  PRIx8);
    151  1.1     isaki atf_sync_rel(__sync_lock_release_2, uint16_t, PRIx16);
    152  1.1     isaki atf_sync_rel(__sync_lock_release_4, uint32_t, PRIx32);
    153  1.1     isaki #ifdef __HAVE_ATOMIC64_OPS
    154  1.1     isaki atf_sync_rel(__sync_lock_release_8, uint64_t, PRIx64);
    155  1.1     isaki #endif
    156  1.1     isaki 
    157  1.1     isaki /*
    158  1.1     isaki  * __sync_synchronize(): This is just a link-time test.
    159  1.1     isaki  */
    160  1.1     isaki ATF_TC(__sync_synchronize);
    161  1.1     isaki ATF_TC_HEAD(__sync_synchronize, tc)
    162  1.1     isaki {
    163  1.1     isaki 	atf_tc_set_md_var(tc, "descr", "__sync_synchronize");
    164  1.1     isaki }
    165  1.1     isaki ATF_TC_BODY(__sync_synchronize, tc)
    166  1.1     isaki {
    167  1.1     isaki 	__sync_synchronize();
    168  1.1     isaki }
    169  1.1     isaki 
    170  1.1     isaki ATF_TP_ADD_TCS(tp)
    171  1.1     isaki {
    172  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_1);
    173  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_2);
    174  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_4);
    175  1.1     isaki #ifdef __HAVE_ATOMIC64_OPS
    176  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_8);
    177  1.1     isaki #endif
    178  1.1     isaki 
    179  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_release_1);
    180  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_release_2);
    181  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_release_4);
    182  1.1     isaki #ifdef __HAVE_ATOMIC64_OPS
    183  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_lock_release_8);
    184  1.1     isaki #endif
    185  1.1     isaki 
    186  1.1     isaki 	ATF_TP_ADD_TC(tp, __sync_synchronize);
    187  1.1     isaki 
    188  1.1     isaki 	return atf_no_error();
    189  1.1     isaki }
    190