2 * Copyright (c) 2021 The FreeBSD Foundation
4 * This software were developed by Konstantin Belousov <kib@FreeBSD.org>
5 * under sponsorship from the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cpuset.h>
34 #include <sys/membarrier.h>
35 #include <sys/mutex.h>
37 #include <sys/sched.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/sysproto.h>
42 #include <vm/vm_param.h>
45 #include <vm/vm_map.h>
47 #define MEMBARRIER_SUPPORTED_CMDS ( \
48 MEMBARRIER_CMD_GLOBAL | \
49 MEMBARRIER_CMD_GLOBAL_EXPEDITED | \
50 MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED | \
51 MEMBARRIER_CMD_PRIVATE_EXPEDITED | \
52 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED | \
53 MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE | \
54 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE)
57 membarrier_action_seqcst(void *arg __unused)
59 atomic_thread_fence_seq_cst();
63 membarrier_action_seqcst_sync_core(void *arg __unused)
65 atomic_thread_fence_seq_cst();
70 do_membarrier_ipi(cpuset_t *csp, void (*func)(void *))
72 atomic_thread_fence_seq_cst();
73 smp_rendezvous_cpus(*csp, smp_no_rendezvous_barrier, func,
74 smp_no_rendezvous_barrier, NULL);
75 atomic_thread_fence_seq_cst();
79 check_cpu_switched(int c, cpuset_t *csp, uint64_t *swt, bool init)
84 if (CPU_ISSET(c, csp))
87 pc = cpuid_to_pcpu[c];
88 if (pc->pc_curthread == pc->pc_idlethread) {
94 * Sync with context switch to ensure that override of
95 * pc_curthread with non-idle thread pointer is visible before
96 * reading of pc_switchtime.
98 atomic_thread_fence_acq();
100 sw = pc->pc_switchtime;
103 else if (sw != swt[c])
109 * XXXKIB: We execute the requested action (seq_cst and possibly
110 * sync_core) on current CPU as well. There is no guarantee that
111 * current thread executes anything with the full fence semantics
112 * during syscall execution. Similarly, cpu_core_sync() semantics
113 * might be not provided by the syscall return. E.g. on amd64 we
114 * typically return without IRET.
117 kern_membarrier(struct thread *td, int cmd, unsigned flags, int cpu_id)
126 if (flags != 0 || (cmd & ~MEMBARRIER_SUPPORTED_CMDS) != 0)
129 if (cmd == MEMBARRIER_CMD_QUERY) {
130 td->td_retval[0] = MEMBARRIER_SUPPORTED_CMDS;
138 case MEMBARRIER_CMD_GLOBAL:
139 swt = malloc((mp_maxid + 1) * sizeof(*swt), M_TEMP, M_WAITOK);
142 CPU_SET(PCPU_GET(cpuid), &cs);
143 for (first = true; error == 0; first = false) {
145 check_cpu_switched(c, &cs, swt, first);
146 if (CPU_CMP(&cs, &all_cpus) == 0)
148 error = pause_sig("mmbr", 1);
149 if (error == EWOULDBLOCK)
154 atomic_thread_fence_seq_cst();
157 case MEMBARRIER_CMD_GLOBAL_EXPEDITED:
158 if ((td->td_proc->p_flag2 & P2_MEMBAR_GLOBE) == 0) {
163 td1 = cpuid_to_pcpu[c]->pc_curthread;
166 (p1->p_flag2 & P2_MEMBAR_GLOBE) != 0)
169 do_membarrier_ipi(&cs, membarrier_action_seqcst);
173 case MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED:
174 if ((p->p_flag2 & P2_MEMBAR_GLOBE) == 0) {
176 p->p_flag2 |= P2_MEMBAR_GLOBE;
181 case MEMBARRIER_CMD_PRIVATE_EXPEDITED:
182 if ((td->td_proc->p_flag2 & P2_MEMBAR_PRIVE) == 0) {
185 pmap_active_cpus(vmspace_pmap(p->p_vmspace), &cs);
186 do_membarrier_ipi(&cs, membarrier_action_seqcst);
190 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED:
191 if ((p->p_flag2 & P2_MEMBAR_PRIVE) == 0) {
193 p->p_flag2 |= P2_MEMBAR_PRIVE;
198 case MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE:
199 if ((td->td_proc->p_flag2 & P2_MEMBAR_PRIVE_SYNCORE) == 0) {
203 * Calculating the IPI multicast mask from
204 * pmap active mask means that we do not call
205 * cpu_sync_core() on CPUs that were missed
206 * from pmap active mask but could be switched
207 * from or to meantime. This is fine at least
208 * on amd64 because threads always use slow
209 * (IRETQ) path to return from syscall after
212 pmap_active_cpus(vmspace_pmap(p->p_vmspace), &cs);
214 do_membarrier_ipi(&cs,
215 membarrier_action_seqcst_sync_core);
219 case MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE:
220 if ((p->p_flag2 & P2_MEMBAR_PRIVE_SYNCORE) == 0) {
222 p->p_flag2 |= P2_MEMBAR_PRIVE_SYNCORE;
236 sys_membarrier(struct thread *td, struct membarrier_args *uap)
238 return (kern_membarrier(td, uap->cmd, uap->flags, uap->cpu_id));