From mboxrd@z Thu Jan 1 00:00:00 1970 From: Liu Ping Fan Subject: [PATCH 01/15] atomic: introduce atomic operations Date: Wed, 8 Aug 2012 14:25:42 +0800 Message-ID: <1344407156-25562-2-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Marcelo Tosatti , qemulist@gmail.com, Blue Swirl , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= To: qemu-devel@nongnu.org Return-path: In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+gceq-qemu-devel=gmane.org@nongnu.org Sender: qemu-devel-bounces+gceq-qemu-devel=gmane.org@nongnu.org List-Id: kvm.vger.kernel.org From: Liu Ping Fan If out of global lock, we will be challenged by SMP in low level, so need atomic ops. This file is heavily copied from kernel. Currently, only x86 atomic ops included, and will be extended for other arch for future. Signed-off-by: Liu Ping Fan --- include/qemu/atomic.h | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 161 insertions(+), 0 deletions(-) create mode 100644 include/qemu/atomic.h diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h new file mode 100644 index 0000000..8e1fc3e --- /dev/null +++ b/include/qemu/atomic.h @@ -0,0 +1,161 @@ +/* + * Simple interface for atomic operations. + * + * 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 + +typedef struct Atomic { + int counter; +} Atomic; + + +#if defined(__i386__) || defined(__x86_64__) + +/** + * * atomic_read - read atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically reads the value of @v. + * */ +static inline int atomic_read(const Atomic *v) +{ + return (*(volatile int *)&(v)->counter); +} + +/** + * * atomic_set - set atomic variable + * * @v: pointer of type Atomic + * * @i: required value + * * + * * Atomically sets the value of @v to @i. + * */ +static inline void atomic_set(Atomic *v, int i) +{ + v->counter = i; +} + +/** + * * atomic_add - add integer to atomic variable + * * @i: integer value to add + * * @v: pointer of type Atomic + * * + * * Atomically adds @i to @v. + * */ +static inline void atomic_add(int i, Atomic *v) +{ + asm volatile("lock; addl %1,%0" + : "+m" (v->counter) + : "ir" (i)); +} + +/** + * * atomic_sub - subtract integer from atomic variable + * * @i: integer value to subtract + * * @v: pointer of type Atomic + * * + * * Atomically subtracts @i from @v. + * */ +static inline void atomic_sub(int i, Atomic *v) +{ + asm volatile("lock; subl %1,%0" + : "+m" (v->counter) + : "ir" (i)); +} + +/** + * * atomic_sub_and_test - subtract value from variable and test result + * * @i: integer value to subtract + * * @v: pointer of type Atomic + * * + * * Atomically subtracts @i from @v and returns + * * true if the result is zero, or false for all + * * other cases. + * */ +static inline int atomic_sub_and_test(int i, Atomic *v) +{ + unsigned char c; + + asm volatile("lock; subl %2,%0; sete %1" + : "+m" (v->counter), "=qm" (c) + : "ir" (i) : "memory"); + return c; +} + +/** + * * atomic_inc - increment atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically increments @v by 1. + * */ +static inline void atomic_inc(Atomic *v) +{ + asm volatile("lock; incl %0" + : "+m" (v->counter)); +} + +/** + * * atomic_dec - decrement atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically decrements @v by 1. + * */ +static inline void atomic_dec(Atomic *v) +{ + asm volatile("lock; decl %0" + : "+m" (v->counter)); +} + +/** + * * atomic_dec_and_test - decrement and test + * * @v: pointer of type Atomic + * * + * * Atomically decrements @v by 1 and + * * returns true if the result is 0, or false for all other + * * cases. + * */ +static inline int atomic_dec_and_test(Atomic *v) +{ + unsigned char c; + + asm volatile("lock; decl %0; sete %1" + : "+m" (v->counter), "=qm" (c) + : : "memory"); + return c != 0; +} + +/** + * * atomic_inc_and_test - increment and test + * * @v: pointer of type Atomic + * * + * * Atomically increments @v by 1 + * * and returns true if the result is zero, or false for all + * * other cases. + * */ +static inline int atomic_inc_and_test(Atomic *v) +{ + unsigned char c; + + asm volatile("lock; incl %0; sete %1" + : "+m" (v->counter), "=qm" (c) + : : "memory"); + return c != 0; +} + +static inline int atomic_add_and_return(int i, Atomic *v) +{ + int ret = i; + + asm volatile ("lock; xaddl %0, %1" + : "+r" (ret), "+m" (v->counter) + : : "memory", "cc"); + + return ret + i; +} +#endif + +#endif -- 1.7.4.4 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:43643) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzic-0007vK-Gd for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Syzib-0001ss-2X for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:10 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:62109) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzia-0001sm-RW for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:08 -0400 Received: by obbta14 with SMTP id ta14so635077obb.4 for ; Tue, 07 Aug 2012 23:26:08 -0700 (PDT) From: Liu Ping Fan Date: Wed, 8 Aug 2012 14:25:42 +0800 Message-Id: <1344407156-25562-2-git-send-email-qemulist@gmail.com> In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Subject: [Qemu-devel] [PATCH 01/15] atomic: introduce atomic operations List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Marcelo Tosatti , qemulist@gmail.com, Blue Swirl , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= From: Liu Ping Fan If out of global lock, we will be challenged by SMP in low level, so need atomic ops. This file is heavily copied from kernel. Currently, only x86 atomic ops included, and will be extended for other arch for future. Signed-off-by: Liu Ping Fan --- include/qemu/atomic.h | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 161 insertions(+), 0 deletions(-) create mode 100644 include/qemu/atomic.h diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h new file mode 100644 index 0000000..8e1fc3e --- /dev/null +++ b/include/qemu/atomic.h @@ -0,0 +1,161 @@ +/* + * Simple interface for atomic operations. + * + * 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 + +typedef struct Atomic { + int counter; +} Atomic; + + +#if defined(__i386__) || defined(__x86_64__) + +/** + * * atomic_read - read atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically reads the value of @v. + * */ +static inline int atomic_read(const Atomic *v) +{ + return (*(volatile int *)&(v)->counter); +} + +/** + * * atomic_set - set atomic variable + * * @v: pointer of type Atomic + * * @i: required value + * * + * * Atomically sets the value of @v to @i. + * */ +static inline void atomic_set(Atomic *v, int i) +{ + v->counter = i; +} + +/** + * * atomic_add - add integer to atomic variable + * * @i: integer value to add + * * @v: pointer of type Atomic + * * + * * Atomically adds @i to @v. + * */ +static inline void atomic_add(int i, Atomic *v) +{ + asm volatile("lock; addl %1,%0" + : "+m" (v->counter) + : "ir" (i)); +} + +/** + * * atomic_sub - subtract integer from atomic variable + * * @i: integer value to subtract + * * @v: pointer of type Atomic + * * + * * Atomically subtracts @i from @v. + * */ +static inline void atomic_sub(int i, Atomic *v) +{ + asm volatile("lock; subl %1,%0" + : "+m" (v->counter) + : "ir" (i)); +} + +/** + * * atomic_sub_and_test - subtract value from variable and test result + * * @i: integer value to subtract + * * @v: pointer of type Atomic + * * + * * Atomically subtracts @i from @v and returns + * * true if the result is zero, or false for all + * * other cases. + * */ +static inline int atomic_sub_and_test(int i, Atomic *v) +{ + unsigned char c; + + asm volatile("lock; subl %2,%0; sete %1" + : "+m" (v->counter), "=qm" (c) + : "ir" (i) : "memory"); + return c; +} + +/** + * * atomic_inc - increment atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically increments @v by 1. + * */ +static inline void atomic_inc(Atomic *v) +{ + asm volatile("lock; incl %0" + : "+m" (v->counter)); +} + +/** + * * atomic_dec - decrement atomic variable + * * @v: pointer of type Atomic + * * + * * Atomically decrements @v by 1. + * */ +static inline void atomic_dec(Atomic *v) +{ + asm volatile("lock; decl %0" + : "+m" (v->counter)); +} + +/** + * * atomic_dec_and_test - decrement and test + * * @v: pointer of type Atomic + * * + * * Atomically decrements @v by 1 and + * * returns true if the result is 0, or false for all other + * * cases. + * */ +static inline int atomic_dec_and_test(Atomic *v) +{ + unsigned char c; + + asm volatile("lock; decl %0; sete %1" + : "+m" (v->counter), "=qm" (c) + : : "memory"); + return c != 0; +} + +/** + * * atomic_inc_and_test - increment and test + * * @v: pointer of type Atomic + * * + * * Atomically increments @v by 1 + * * and returns true if the result is zero, or false for all + * * other cases. + * */ +static inline int atomic_inc_and_test(Atomic *v) +{ + unsigned char c; + + asm volatile("lock; incl %0; sete %1" + : "+m" (v->counter), "=qm" (c) + : : "memory"); + return c != 0; +} + +static inline int atomic_add_and_return(int i, Atomic *v) +{ + int ret = i; + + asm volatile ("lock; xaddl %0, %1" + : "+r" (ret), "+m" (v->counter) + : : "memory", "cc"); + + return ret + i; +} +#endif + +#endif -- 1.7.4.4