1 1.5 riastrad /* $NetBSD: t_spinlock.c,v 1.5 2025/05/02 22:30:44 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2022 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * Redistribution and use in source and binary forms, with or without 8 1.1 riastrad * modification, are permitted provided that the following conditions 9 1.1 riastrad * are met: 10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 11 1.1 riastrad * notice, this list of conditions and the following disclaimer. 12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 14 1.1 riastrad * documentation and/or other materials provided with the distribution. 15 1.1 riastrad * 16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 27 1.1 riastrad */ 28 1.1 riastrad 29 1.1 riastrad #include <sys/cdefs.h> 30 1.5 riastrad __RCSID("$NetBSD: t_spinlock.c,v 1.5 2025/05/02 22:30:44 riastradh Exp $"); 31 1.4 riastrad 32 1.4 riastrad #include <sys/types.h> 33 1.1 riastrad 34 1.1 riastrad #include <sys/atomic.h> 35 1.1 riastrad #include <sys/param.h> 36 1.1 riastrad #include <sys/sysctl.h> 37 1.1 riastrad 38 1.1 riastrad #include <assert.h> 39 1.1 riastrad #include <atf-c.h> 40 1.1 riastrad #include <err.h> 41 1.1 riastrad #include <errno.h> 42 1.1 riastrad #include <inttypes.h> 43 1.1 riastrad #include <pthread.h> 44 1.1 riastrad #include <stdint.h> 45 1.1 riastrad #include <stdio.h> 46 1.1 riastrad #include <unistd.h> 47 1.1 riastrad 48 1.1 riastrad #ifdef BROKEN_ACQUIRE 49 1.1 riastrad #undef membar_acquire 50 1.1 riastrad #define membar_acquire() asm volatile("" ::: "memory") 51 1.1 riastrad #endif /* BROKEN_ACQUIRE */ 52 1.1 riastrad 53 1.1 riastrad #ifdef BROKEN_RELEASE 54 1.1 riastrad #undef membar_release 55 1.1 riastrad #define membar_release() asm volatile("" ::: "memory") 56 1.1 riastrad #endif /* BROKEN_RELEASE */ 57 1.1 riastrad 58 1.1 riastrad volatile sig_atomic_t times_up; 59 1.1 riastrad 60 1.1 riastrad volatile unsigned lockbit __aligned(COHERENCY_UNIT); 61 1.1 riastrad 62 1.1 riastrad volatile struct { 63 1.1 riastrad uint64_t v; 64 1.1 riastrad } __aligned(COHERENCY_UNIT) C[8]; 65 1.1 riastrad uint64_t TC[2]; 66 1.1 riastrad 67 1.1 riastrad static void 68 1.1 riastrad lock(void) 69 1.1 riastrad { 70 1.1 riastrad 71 1.1 riastrad while (atomic_swap_uint(&lockbit, 1)) 72 1.1 riastrad continue; 73 1.1 riastrad membar_acquire(); 74 1.1 riastrad } 75 1.1 riastrad 76 1.1 riastrad static void 77 1.1 riastrad unlock(void) 78 1.1 riastrad { 79 1.1 riastrad 80 1.1 riastrad membar_release(); 81 1.4 riastrad #ifdef __HAVE_HASHLOCKED_ATOMICS 82 1.4 riastrad (void)atomic_cas_uint(&lockbit, 1, 0); 83 1.4 riastrad #else 84 1.1 riastrad lockbit = 0; 85 1.4 riastrad #endif 86 1.1 riastrad } 87 1.1 riastrad 88 1.1 riastrad static void * 89 1.1 riastrad thread(void *cookie) 90 1.1 riastrad { 91 1.1 riastrad unsigned me = (unsigned)(uintptr_t)cookie; 92 1.1 riastrad uint64_t C_local = 0, C0[__arraycount(C)]; 93 1.1 riastrad unsigned i; 94 1.1 riastrad 95 1.1 riastrad while (!times_up) { 96 1.1 riastrad C_local++; 97 1.1 riastrad lock(); 98 1.1 riastrad for (i = 0; i < __arraycount(C); i++) 99 1.1 riastrad C0[i] = C[i].v; 100 1.1 riastrad __insn_barrier(); 101 1.1 riastrad for (i = __arraycount(C); i --> 0;) 102 1.1 riastrad C[i].v = C0[i] + 1; 103 1.1 riastrad unlock(); 104 1.1 riastrad } 105 1.1 riastrad 106 1.1 riastrad TC[me] = C_local; 107 1.1 riastrad 108 1.1 riastrad return NULL; 109 1.1 riastrad } 110 1.1 riastrad 111 1.1 riastrad ATF_TC(spinlock); 112 1.1 riastrad ATF_TC_HEAD(spinlock, tc) 113 1.1 riastrad { 114 1.1 riastrad atf_tc_set_md_var(tc, "descr", 115 1.1 riastrad "Verify membar_acquire/release work for spin locks"); 116 1.1 riastrad } 117 1.1 riastrad ATF_TC_BODY(spinlock, tc) 118 1.1 riastrad { 119 1.1 riastrad pthread_t t[2]; 120 1.1 riastrad unsigned i; 121 1.1 riastrad int ncpu; 122 1.1 riastrad size_t ncpulen = sizeof(ncpu); 123 1.1 riastrad int error; 124 1.1 riastrad 125 1.3 riastrad alarm(10); 126 1.3 riastrad 127 1.1 riastrad if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1) 128 1.1 riastrad atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno)); 129 1.1 riastrad assert(ncpulen == sizeof(ncpu)); 130 1.1 riastrad if (ncpu == 1) 131 1.1 riastrad atf_tc_skip("membar tests are only for multicore systems"); 132 1.1 riastrad 133 1.5 riastrad #ifdef __powerpc__ 134 1.5 riastrad atf_tc_expect_fail("port-powerpc/59386: t_spinlock test is failing"); 135 1.5 riastrad #endif 136 1.5 riastrad 137 1.1 riastrad for (i = 0; i < 2; i++) { 138 1.1 riastrad error = pthread_create(&t[i], NULL, &thread, 139 1.1 riastrad (void *)(uintptr_t)i); 140 1.1 riastrad if (error) 141 1.1 riastrad errc(1, error, "pthread_create"); 142 1.1 riastrad } 143 1.3 riastrad sleep(5); 144 1.3 riastrad times_up = 1; 145 1.1 riastrad for (i = 0; i < 2; i++) { 146 1.1 riastrad error = pthread_join(t[i], NULL); 147 1.1 riastrad if (error) 148 1.1 riastrad errc(1, error, "pthread_join"); 149 1.1 riastrad } 150 1.1 riastrad for (i = 0; i < __arraycount(C); i++) { 151 1.1 riastrad ATF_CHECK_MSG(C[i].v == TC[0] + TC[1], "%d: " 152 1.1 riastrad "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")", 153 1.1 riastrad i, C[i].v, TC[0], TC[1], TC[0] + TC[1] - C[i].v); 154 1.1 riastrad } 155 1.1 riastrad } 156 1.1 riastrad 157 1.1 riastrad ATF_TP_ADD_TCS(tp) 158 1.1 riastrad { 159 1.1 riastrad 160 1.1 riastrad ATF_TP_ADD_TC(tp, spinlock); 161 1.1 riastrad return atf_no_error(); 162 1.1 riastrad } 163