1 /* $NetBSD: t_spinlock.c,v 1.3 2022/04/10 11:36:32 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2022 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 __RCSID("$NetBSD: t_spinlock.c,v 1.3 2022/04/10 11:36:32 riastradh Exp $"); 31 32 #include <sys/atomic.h> 33 #include <sys/param.h> 34 #include <sys/sysctl.h> 35 36 #include <assert.h> 37 #include <atf-c.h> 38 #include <err.h> 39 #include <errno.h> 40 #include <inttypes.h> 41 #include <pthread.h> 42 #include <stdint.h> 43 #include <stdio.h> 44 #include <unistd.h> 45 46 #ifdef BROKEN_ACQUIRE 47 #undef membar_acquire 48 #define membar_acquire() asm volatile("" ::: "memory") 49 #endif /* BROKEN_ACQUIRE */ 50 51 #ifdef BROKEN_RELEASE 52 #undef membar_release 53 #define membar_release() asm volatile("" ::: "memory") 54 #endif /* BROKEN_RELEASE */ 55 56 volatile sig_atomic_t times_up; 57 58 volatile unsigned lockbit __aligned(COHERENCY_UNIT); 59 60 volatile struct { 61 uint64_t v; 62 } __aligned(COHERENCY_UNIT) C[8]; 63 uint64_t TC[2]; 64 65 static void 66 lock(void) 67 { 68 69 while (atomic_swap_uint(&lockbit, 1)) 70 continue; 71 membar_acquire(); 72 } 73 74 static void 75 unlock(void) 76 { 77 78 membar_release(); 79 lockbit = 0; 80 } 81 82 static void * 83 thread(void *cookie) 84 { 85 unsigned me = (unsigned)(uintptr_t)cookie; 86 uint64_t C_local = 0, C0[__arraycount(C)]; 87 unsigned i; 88 89 while (!times_up) { 90 C_local++; 91 lock(); 92 for (i = 0; i < __arraycount(C); i++) 93 C0[i] = C[i].v; 94 __insn_barrier(); 95 for (i = __arraycount(C); i --> 0;) 96 C[i].v = C0[i] + 1; 97 unlock(); 98 } 99 100 TC[me] = C_local; 101 102 return NULL; 103 } 104 105 ATF_TC(spinlock); 106 ATF_TC_HEAD(spinlock, tc) 107 { 108 atf_tc_set_md_var(tc, "descr", 109 "Verify membar_acquire/release work for spin locks"); 110 } 111 ATF_TC_BODY(spinlock, tc) 112 { 113 pthread_t t[2]; 114 unsigned i; 115 int ncpu; 116 size_t ncpulen = sizeof(ncpu); 117 int error; 118 119 alarm(10); 120 121 if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1) 122 atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno)); 123 assert(ncpulen == sizeof(ncpu)); 124 if (ncpu == 1) 125 atf_tc_skip("membar tests are only for multicore systems"); 126 127 for (i = 0; i < 2; i++) { 128 error = pthread_create(&t[i], NULL, &thread, 129 (void *)(uintptr_t)i); 130 if (error) 131 errc(1, error, "pthread_create"); 132 } 133 sleep(5); 134 times_up = 1; 135 for (i = 0; i < 2; i++) { 136 error = pthread_join(t[i], NULL); 137 if (error) 138 errc(1, error, "pthread_join"); 139 } 140 for (i = 0; i < __arraycount(C); i++) { 141 ATF_CHECK_MSG(C[i].v == TC[0] + TC[1], "%d: " 142 "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")", 143 i, C[i].v, TC[0], TC[1], TC[0] + TC[1] - C[i].v); 144 } 145 } 146 147 ATF_TP_ADD_TCS(tp) 148 { 149 150 ATF_TP_ADD_TC(tp, spinlock); 151 return atf_no_error(); 152 } 153