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