Home | History | Annotate | Line # | Download | only in rumpkern
      1 /*	$NetBSD: atomic_cas_generic.c,v 1.3 2021/03/14 22:56:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: atomic_cas_generic.c,v 1.3 2021/03/14 22:56:39 christos Exp $");
     31 
     32 /*
     33  * This is basically common/lib/libc/atomic/atomic_init_testset.c
     34  * which always assumes MP and never uses RAS.  This is used only on
     35  * some archs.  See Makefile.rumpkern for more information.
     36  */
     37 
     38 #include "atomic_op_namespace.h"
     39 
     40 #include <sys/types.h>
     41 #include <sys/atomic.h>
     42 #include <sys/lock.h>
     43 
     44 #define	I2	__SIMPLELOCK_UNLOCKED, __SIMPLELOCK_UNLOCKED,
     45 #define	I16	I2 I2 I2 I2 I2 I2 I2 I2
     46 #define	I128	I16 I16 I16 I16 I16 I16 I16 I16
     47 
     48 static __cpu_simple_lock_t atomic_locks32[128] = { I128 };
     49 
     50 uint32_t
     51 _atomic_cas_32(volatile uint32_t *ptr, uint32_t old, uint32_t new)
     52 {
     53 	__cpu_simple_lock_t *lock;
     54 	uint32_t ret;
     55 
     56 	lock = &atomic_locks32[((uintptr_t)ptr >> 3) & 127];
     57 	__cpu_simple_lock(lock);
     58 	ret = *ptr;
     59 	if (__predict_true(ret == old)) {
     60 		*ptr = new;
     61 	}
     62 	__cpu_simple_unlock(lock);
     63 
     64 	return ret;
     65 }
     66 
     67 #undef atomic_cas_32
     68 atomic_op_alias(atomic_cas_32,_atomic_cas_32)
     69 atomic_op_alias(atomic_cas_32_ni,_atomic_cas_32)
     70 __strong_alias(_atomic_cas_32_ni,_atomic_cas_32)
     71 
     72 #ifdef _LP64
     73 static __cpu_simple_lock_t atomic_locks64[128] = { I128 };
     74 
     75 uint64_t
     76 _atomic_cas_64(volatile uint64_t *ptr, uint64_t old, uint64_t new)
     77 {
     78 	__cpu_simple_lock_t *lock;
     79 	uint64_t ret;
     80 
     81 	lock = &atomic_locks64[((uintptr_t)ptr >> 4) & 127];
     82 	__cpu_simple_lock(lock);
     83 	ret = *ptr;
     84 	if (__predict_true(ret == old)) {
     85 		*ptr = new;
     86 	}
     87 	__cpu_simple_unlock(lock);
     88 
     89 	return ret;
     90 }
     91 
     92 #undef atomic_cas_64
     93 atomic_op_alias(atomic_cas_64,_atomic_cas_64)
     94 atomic_op_alias(atomic_cas_64_ni,_atomic_cas_64)
     95 __strong_alias(_atomic_cas_64_ni,_atomic_cas_64)
     96 
     97 #endif
     98 
     99 #undef atomic_cas_uint
    100 #undef atomic_cas_ulong
    101 #undef atomic_cas_ptr
    102 
    103 atomic_op_alias(atomic_cas_uint,_atomic_cas_32)
    104 __strong_alias(_atomic_cas_uint,_atomic_cas_32)
    105 atomic_op_alias(atomic_cas_uint_ni,_atomic_cas_32)
    106 __strong_alias(_atomic_cas_uint_ni,_atomic_cas_32)
    107 
    108 #ifdef _LP64
    109 atomic_op_alias(atomic_cas_ulong,_atomic_cas_64)
    110 __strong_alias(_atomic_cas_ulong,_atomic_cas_64)
    111 atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_64)
    112 __strong_alias(_atomic_cas_ulong_ni,_atomic_cas_64)
    113 atomic_op_alias(atomic_cas_ptr,_atomic_cas_64)
    114 __strong_alias(_atomic_cas_ptr,_atomic_cas_64)
    115 atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_64)
    116 __strong_alias(_atomic_cas_ptr_ni,_atomic_cas_64)
    117 #else
    118 atomic_op_alias(atomic_cas_ulong,_atomic_cas_32)
    119 __strong_alias(_atomic_cas_ulong,_atomic_cas_32)
    120 atomic_op_alias(atomic_cas_ulong_ni,_atomic_cas_32)
    121 __strong_alias(_atomic_cas_ulong_ni,_atomic_cas_32)
    122 atomic_op_alias(atomic_cas_ptr,_atomic_cas_32)
    123 __strong_alias(_atomic_cas_ptr,_atomic_cas_32)
    124 atomic_op_alias(atomic_cas_ptr_ni,_atomic_cas_32)
    125 __strong_alias(_atomic_cas_ptr_ni,_atomic_cas_32)
    126 #endif
    127