xref: /openbmc/qemu/util/sys_membarrier.c (revision dda2441b)
1 /*
2  * Process-global memory barriers
3  *
4  * Copyright (c) 2018 Red Hat, Inc.
5  *
6  * Author: Paolo Bonzini <pbonzini@redhat.com>
7  */
8 
9 #include <qemu/osdep.h>
10 #include <qemu/sys_membarrier.h>
11 #include <qemu/error-report.h>
12 
13 #ifdef CONFIG_LINUX
14 #include <linux/membarrier.h>
15 #include <sys/syscall.h>
16 
17 static int
18 membarrier(int cmd, int flags)
19 {
20     return syscall(__NR_membarrier, cmd, flags);
21 }
22 #endif
23 
24 void smp_mb_global(void)
25 {
26 #if defined CONFIG_WIN32
27     FlushProcessWriteBuffers();
28 #elif defined CONFIG_LINUX
29     membarrier(MEMBARRIER_CMD_SHARED, 0);
30 #else
31 #error --enable-membarrier is not supported on this operating system.
32 #endif
33 }
34 
35 void smp_mb_global_init(void)
36 {
37 #ifdef CONFIG_LINUX
38     int ret = membarrier(MEMBARRIER_CMD_QUERY, 0);
39     if (ret < 0) {
40         error_report("This QEMU binary requires the membarrier system call.");
41         error_report("Please upgrade your system to a newer version of Linux");
42         exit(1);
43     }
44     if (!(ret & MEMBARRIER_CMD_SHARED)) {
45         error_report("This QEMU binary requires MEMBARRIER_CMD_SHARED support.");
46         error_report("Please upgrade your system to a newer version of Linux");
47         exit(1);
48     }
49 #endif
50 }
51