Skip to content

Commit

Permalink
add a header file for atomic operations
Browse files Browse the repository at this point in the history
We're already using them in several places, but __sync builtins are just
too ugly to type, and do not provide seqcst load/store operations.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
bonzini committed Jul 24, 2012
1 parent a211434 commit 1b43934
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 75 deletions.
2 changes: 1 addition & 1 deletion cpu-exec.c
Expand Up @@ -20,7 +20,7 @@
#include "cpu.h"
#include "disas.h"
#include "tcg.h"
#include "qemu-barrier.h"
#include "qemu/atomic.h"
#include "qtest.h"

int tb_invalidated_flag;
Expand Down
3 changes: 2 additions & 1 deletion hw/qxl.c
Expand Up @@ -21,6 +21,7 @@
#include "qemu-common.h"
#include "qemu-timer.h"
#include "qemu-queue.h"
#include "qemu/atomic.h"
#include "monitor.h"
#include "sysemu.h"
#include "trace.h"
Expand Down Expand Up @@ -1539,7 +1540,7 @@ static void qxl_send_events(PCIQXLDevice *d, uint32_t events)
uint32_t le_events = cpu_to_le32(events);

assert(d->ssd.running);
old_pending = __sync_fetch_and_or(&d->ram->int_pending, le_events);
old_pending = atomic_or(&d->ram->int_pending, le_events);
if ((old_pending & le_events) == le_events) {
return;
}
Expand Down
9 changes: 4 additions & 5 deletions hw/vhost.c
Expand Up @@ -16,6 +16,7 @@
#include <sys/ioctl.h>
#include "vhost.h"
#include "hw/hw.h"
#include "qemu/atomic.h"
#include "range.h"
#include <linux/vhost.h>
#include "exec-memory.h"
Expand Down Expand Up @@ -46,11 +47,9 @@ static void vhost_dev_sync_region(struct vhost_dev *dev,
addr += VHOST_LOG_CHUNK;
continue;
}
/* Data must be read atomically. We don't really
* need the barrier semantics of __sync
* builtins, but it's easier to use them than
* roll our own. */
log = __sync_fetch_and_and(from, 0);
/* Data must be read atomically. We don't really need barrier semantics
* but it's easier to use atomic_* than roll our own. */
log = atomic_xchg(from, 0);
while ((bit = sizeof(log) > sizeof(int) ?
ffsll(log) : ffs(log))) {
ram_addr_t ram_addr;
Expand Down
2 changes: 1 addition & 1 deletion hw/virtio.c
Expand Up @@ -16,7 +16,7 @@
#include "trace.h"
#include "qemu-error.h"
#include "virtio.h"
#include "qemu-barrier.h"
#include "qemu/atomic.h"

/* The alignment to use between consumer and producer parts of vring.
* x86 pagesize again. */
Expand Down
121 changes: 121 additions & 0 deletions include/qemu/atomic.h
@@ -0,0 +1,121 @@
/*
* Simple interface for atomic operations.
*
* Copyright (C) 2012 Red Hat, Inc.
*
* Author: Paolo Bonzini <pbonzini@redhat.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/

#ifndef __QEMU_ATOMIC_H
#define __QEMU_ATOMIC_H 1

/* Compiler barrier */
#define barrier() ({ asm volatile("" ::: "memory"); (void)0; })

#if defined(__i386__) || defined(__x86_64__)

/*
* We use GCC builtin if it's available, as that can use mfence on
* 32-bit as well, e.g. if built with -march=pentium-m. However, on
* i386 the spec is buggy, and the implementation followed it until
* 4.3 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36793).
*/
#if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 4
#define smp_mb() __sync_synchronize()
#else
#define smp_mb() ({ asm volatile("lock; addl $0,0(%%esp) " ::: "memory"); (void)0; })
#endif

/*
* Because of the strongly ordered x86 storage model, wmb() is a nop
* on x86 (a compiler barrier only). Well, at least as long as
* qemu doesn't do accesses to write-combining memory or non-temporal
* load/stores from C code.
*/
#define smp_rmb() barrier()
#define smp_wmb() barrier()

/*
* __sync_lock_test_and_set() is documented to be an acquire barrier only,
* but it is a full barrier at the hardware level. Add a compiler barrier
* to make it a full barrier also at the compiler level.
*/
#define atomic_xchg(ptr, i) (barrier(), __sync_lock_test_and_set(ptr, i))

/*
* seqcst load/store.
*/
#define atomic_mb_read(ptr) atomic_read(ptr)
#define atomic_mb_set(ptr, i) ((void)atomic_xchg(ptr, i))

#elif defined(_ARCH_PPC)

/*
* We use an eieio() for a wmb() on powerpc. This assumes we don't
* need to order cacheable and non-cacheable stores with respect to
* each other.
*
* smp_mb has the same problem as on x86 for not-very-new GCC
* (http://patchwork.ozlabs.org/patch/126184/, Nov 2011).
*/
#define smp_mb() ({ asm volatile("sync" ::: "memory"); (void)0; })
#if defined(__powerpc64__)
#define smp_rmb() ({ asm volatile("lwsync" ::: "memory"); (void)0; })
#else
#define smp_rmb() ({ asm volatile("sync" ::: "memory"); (void)0; })
#endif
#define smp_wmb() ({ asm volatile("eieio" ::: "memory"); (void)0; })

#endif

/*
* For (host) platforms we don't have explicit barrier definitions
* for, we use the gcc __sync_synchronize() primitive to generate a
* full barrier. This should be safe on all platforms, though it may
* be overkill.
*/
#ifndef smp_wmb
#define smp_wmb() __sync_synchronize()
#endif

#ifndef smp_mb
#define smp_mb() __sync_synchronize()
#endif

#ifndef atomic_read
#define atomic_read(ptr) (*(__typeof__(*ptr) *volatile) (ptr))
#endif

#ifndef atomic_mb_read
#define atomic_mb_read(ptr) (smp_mb(), atomic_read(ptr), smp_mb())
#endif

#ifndef atomic_set
#define atomic_set(ptr, i) ((*(__typeof__(*ptr) *volatile) (ptr)) = (i))
#endif

#ifndef atomic_mb_set
#define atomic_mb_set(ptr, i) (smp_mb(), atomic_set(ptr, i))
#endif

/*
* __sync_lock_test_and_set() is documented to be an acquire barrier only.
*/
#ifndef atomic_xchg
#define atomic_xchg(ptr, i) (smp_mb(), __sync_lock_test_and_set(ptr, i))
#endif

/* Provide shorter names. */
#define atomic_inc(ptr) __sync_fetch_and_add(ptr, 1)
#define atomic_dec(ptr) __sync_fetch_and_add(ptr, -1)
#define atomic_add __sync_fetch_and_add
#define atomic_sub __sync_fetch_and_sub
#define atomic_and __sync_fetch_and_and
#define atomic_or __sync_fetch_and_or
#define atomic_cmpxchg __sync_val_compare_and_swap

#endif
2 changes: 1 addition & 1 deletion kvm-all.c
Expand Up @@ -21,9 +21,9 @@
#include <linux/kvm.h>

#include "qemu-common.h"
#include "qemu-barrier.h"
#include "qemu-option.h"
#include "qemu-config.h"
#include "qemu/atomic.h"
#include "sysemu.h"
#include "hw/hw.h"
#include "hw/msi.h"
Expand Down
65 changes: 0 additions & 65 deletions qemu-barrier.h

This file was deleted.

2 changes: 1 addition & 1 deletion qemu-queue.h
Expand Up @@ -78,7 +78,7 @@
* For details on the use of these macros, see the queue(3) manual page.
*/

#include "qemu-barrier.h" /* for smp_wmb() */
#include "qemu/atomic.h" /* for smp_wmb() */

/*
* List definitions.
Expand Down

0 comments on commit 1b43934

Please sign in to comment.