aboutsummaryrefslogtreecommitdiff
path: root/util/sys_membarrier.c
blob: 1362c0c4c5959d6a24e7af520b3f7913bc6791bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Process-global memory barriers
 *
 * Copyright (c) 2018 Red Hat, Inc.
 *
 * Author: Paolo Bonzini <pbonzini@redhat.com>
 */

#include "qemu/osdep.h"
#include "qemu/sys_membarrier.h"
#include "qemu/error-report.h"

#ifdef CONFIG_LINUX
#include <linux/membarrier.h>
#include <sys/syscall.h>

static int
membarrier(int cmd, int flags)
{
    return syscall(__NR_membarrier, cmd, flags);
}
#endif

void smp_mb_global(void)
{
#if defined CONFIG_WIN32
    FlushProcessWriteBuffers();
#elif defined CONFIG_LINUX
    membarrier(MEMBARRIER_CMD_SHARED, 0);
#else
#error --enable-membarrier is not supported on this operating system.
#endif
}

void smp_mb_global_init(void)
{
#ifdef CONFIG_LINUX
    int ret = membarrier(MEMBARRIER_CMD_QUERY, 0);
    if (ret < 0) {
        error_report("This QEMU binary requires the membarrier system call.");
        error_report("Please upgrade your system to a newer version of Linux");
        exit(1);
    }
    if (!(ret & MEMBARRIER_CMD_SHARED)) {
        error_report("This QEMU binary requires MEMBARRIER_CMD_SHARED support.");
        error_report("Please upgrade your system to a newer version of Linux");
        exit(1);
    }
#endif
}