import
This commit is contained in:
68
qemu/include/qemu/aes.h
Normal file
68
qemu/include/qemu/aes.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef QEMU_AES_H
|
||||
#define QEMU_AES_H
|
||||
|
||||
#define AES_MAXNR 14
|
||||
#define AES_BLOCK_SIZE 16
|
||||
|
||||
struct aes_key_st {
|
||||
uint32_t rd_key[4 *(AES_MAXNR + 1)];
|
||||
int rounds;
|
||||
};
|
||||
typedef struct aes_key_st AES_KEY;
|
||||
|
||||
/* FreeBSD has its own AES_set_decrypt_key in -lcrypto, avoid conflicts */
|
||||
#ifdef __FreeBSD__
|
||||
#define AES_set_encrypt_key QEMU_AES_set_encrypt_key
|
||||
#define AES_set_decrypt_key QEMU_AES_set_decrypt_key
|
||||
#define AES_encrypt QEMU_AES_encrypt
|
||||
#define AES_decrypt QEMU_AES_decrypt
|
||||
#define AES_cbc_encrypt QEMU_AES_cbc_encrypt
|
||||
#endif
|
||||
|
||||
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
||||
AES_KEY *key);
|
||||
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
||||
AES_KEY *key);
|
||||
|
||||
void AES_encrypt(const unsigned char *in, unsigned char *out,
|
||||
const AES_KEY *key);
|
||||
void AES_decrypt(const unsigned char *in, unsigned char *out,
|
||||
const AES_KEY *key);
|
||||
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
||||
const unsigned long length, const AES_KEY *key,
|
||||
unsigned char *ivec, const int enc);
|
||||
|
||||
extern const uint8_t AES_sbox[256];
|
||||
extern const uint8_t AES_isbox[256];
|
||||
|
||||
/* AES ShiftRows and InvShiftRows */
|
||||
extern const uint8_t AES_shifts[16];
|
||||
extern const uint8_t AES_ishifts[16];
|
||||
|
||||
/* AES InvMixColumns */
|
||||
/* AES_imc[x][0] = [x].[0e, 09, 0d, 0b]; */
|
||||
/* AES_imc[x][1] = [x].[0b, 0e, 09, 0d]; */
|
||||
/* AES_imc[x][2] = [x].[0d, 0b, 0e, 09]; */
|
||||
/* AES_imc[x][3] = [x].[09, 0d, 0b, 0e]; */
|
||||
extern const uint32_t AES_imc[256][4];
|
||||
|
||||
/*
|
||||
AES_Te0[x] = S [x].[02, 01, 01, 03];
|
||||
AES_Te1[x] = S [x].[03, 02, 01, 01];
|
||||
AES_Te2[x] = S [x].[01, 03, 02, 01];
|
||||
AES_Te3[x] = S [x].[01, 01, 03, 02];
|
||||
AES_Te4[x] = S [x].[01, 01, 01, 01];
|
||||
|
||||
AES_Td0[x] = Si[x].[0e, 09, 0d, 0b];
|
||||
AES_Td1[x] = Si[x].[0b, 0e, 09, 0d];
|
||||
AES_Td2[x] = Si[x].[0d, 0b, 0e, 09];
|
||||
AES_Td3[x] = Si[x].[09, 0d, 0b, 0e];
|
||||
AES_Td4[x] = Si[x].[01, 01, 01, 01];
|
||||
*/
|
||||
|
||||
extern const uint32_t AES_Te0[256], AES_Te1[256], AES_Te2[256],
|
||||
AES_Te3[256], AES_Te4[256];
|
||||
extern const uint32_t AES_Td0[256], AES_Td1[256], AES_Td2[256],
|
||||
AES_Td3[256], AES_Td4[256];
|
||||
|
||||
#endif
|
||||
202
qemu/include/qemu/atomic.h
Normal file
202
qemu/include/qemu/atomic.h
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* Simple interface for atomic operations.
|
||||
*
|
||||
* Copyright (C) 2013 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
|
||||
|
||||
#include "qemu/compiler.h"
|
||||
|
||||
/* For C11 atomic ops */
|
||||
|
||||
/* Compiler barrier */
|
||||
#define barrier() ({ asm volatile("" ::: "memory"); (void)0; })
|
||||
|
||||
#ifndef __ATOMIC_RELAXED
|
||||
|
||||
/*
|
||||
* 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(__i386__) || defined(__x86_64__)
|
||||
#if !QEMU_GNUC_PREREQ(4, 4)
|
||||
#if defined __x86_64__
|
||||
#define smp_mb() ({ asm volatile("mfence" ::: "memory"); (void)0; })
|
||||
#else
|
||||
#define smp_mb() ({ asm volatile("lock; addl $0,0(%%esp) " ::: "memory"); (void)0; })
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __alpha__
|
||||
#define smp_read_barrier_depends() asm volatile("mb":::"memory")
|
||||
#endif
|
||||
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(__s390x__)
|
||||
|
||||
/*
|
||||
* Because of the strongly ordered storage model, wmb() and rmb() are nops
|
||||
* here (a compiler barrier only). QEMU doesn't do accesses to write-combining
|
||||
* qemu memory or non-temporal load/stores from C code.
|
||||
*/
|
||||
#define smp_wmb() barrier()
|
||||
#define smp_rmb() 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))
|
||||
|
||||
/*
|
||||
* Load/store with Java volatile semantics.
|
||||
*/
|
||||
#define atomic_mb_set(ptr, i) ((void)atomic_xchg(ptr, i))
|
||||
|
||||
#elif defined(_ARCH_PPC)
|
||||
|
||||
/*
|
||||
* We use an eieio() for 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_wmb() ({ asm volatile("eieio" ::: "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_mb() ({ asm volatile("sync" ::: "memory"); (void)0; })
|
||||
|
||||
#endif /* _ARCH_PPC */
|
||||
|
||||
#endif /* C11 atomics */
|
||||
|
||||
/*
|
||||
* 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 for smp_wmb() and smp_rmb().
|
||||
*/
|
||||
#ifndef smp_mb
|
||||
#define smp_mb() __sync_synchronize()
|
||||
#endif
|
||||
|
||||
#ifndef smp_wmb
|
||||
#ifdef __ATOMIC_RELEASE
|
||||
#define smp_wmb() __atomic_thread_fence(__ATOMIC_RELEASE)
|
||||
#else
|
||||
#define smp_wmb() __sync_synchronize()
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef smp_rmb
|
||||
#ifdef __ATOMIC_ACQUIRE
|
||||
#define smp_rmb() __atomic_thread_fence(__ATOMIC_ACQUIRE)
|
||||
#else
|
||||
#define smp_rmb() __sync_synchronize()
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef smp_read_barrier_depends
|
||||
#ifdef __ATOMIC_CONSUME
|
||||
#define smp_read_barrier_depends() __atomic_thread_fence(__ATOMIC_CONSUME)
|
||||
#else
|
||||
#define smp_read_barrier_depends() barrier()
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef atomic_read
|
||||
#define atomic_read(ptr) (*(__typeof__(*ptr) *volatile) (ptr))
|
||||
#endif
|
||||
|
||||
#ifndef atomic_set
|
||||
#define atomic_set(ptr, i) ((*(__typeof__(*ptr) *volatile) (ptr)) = (i))
|
||||
#endif
|
||||
|
||||
/* These have the same semantics as Java volatile variables.
|
||||
* See http://gee.cs.oswego.edu/dl/jmm/cookbook.html:
|
||||
* "1. Issue a StoreStore barrier (wmb) before each volatile store."
|
||||
* 2. Issue a StoreLoad barrier after each volatile store.
|
||||
* Note that you could instead issue one before each volatile load, but
|
||||
* this would be slower for typical programs using volatiles in which
|
||||
* reads greatly outnumber writes. Alternatively, if available, you
|
||||
* can implement volatile store as an atomic instruction (for example
|
||||
* XCHG on x86) and omit the barrier. This may be more efficient if
|
||||
* atomic instructions are cheaper than StoreLoad barriers.
|
||||
* 3. Issue LoadLoad and LoadStore barriers after each volatile load."
|
||||
*
|
||||
* If you prefer to think in terms of "pairing" of memory barriers,
|
||||
* an atomic_mb_read pairs with an atomic_mb_set.
|
||||
*
|
||||
* And for the few ia64 lovers that exist, an atomic_mb_read is a ld.acq,
|
||||
* while an atomic_mb_set is a st.rel followed by a memory barrier.
|
||||
*
|
||||
* These are a bit weaker than __atomic_load/store with __ATOMIC_SEQ_CST
|
||||
* (see docs/atomics.txt), and I'm not sure that __ATOMIC_ACQ_REL is enough.
|
||||
* Just always use the barriers manually by the rules above.
|
||||
*/
|
||||
#ifndef atomic_mb_read
|
||||
#define atomic_mb_read(ptr) ({ \
|
||||
typeof(*ptr) _val = atomic_read(ptr); \
|
||||
smp_rmb(); \
|
||||
_val; \
|
||||
})
|
||||
#endif
|
||||
|
||||
#ifndef atomic_mb_set
|
||||
#define atomic_mb_set(ptr, i) do { \
|
||||
smp_wmb(); \
|
||||
atomic_set(ptr, i); \
|
||||
smp_mb(); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifndef atomic_xchg
|
||||
#if defined(__clang__)
|
||||
#define atomic_xchg(ptr, i) __sync_swap(ptr, i)
|
||||
#elif defined(__ATOMIC_SEQ_CST)
|
||||
#define atomic_xchg(ptr, i) ({ \
|
||||
typeof(*ptr) _new = (i), _old; \
|
||||
__atomic_exchange(ptr, &_new, &_old, __ATOMIC_SEQ_CST); \
|
||||
_old; \
|
||||
})
|
||||
#else
|
||||
/* __sync_lock_test_and_set() is documented to be an acquire barrier only. */
|
||||
#define atomic_xchg(ptr, i) (smp_mb(), __sync_lock_test_and_set(ptr, i))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Provide shorter names for GCC atomic builtins. */
|
||||
#define atomic_fetch_inc(ptr) __sync_fetch_and_add(ptr, 1)
|
||||
#define atomic_fetch_dec(ptr) __sync_fetch_and_add(ptr, -1)
|
||||
#define atomic_fetch_add __sync_fetch_and_add
|
||||
#define atomic_fetch_sub __sync_fetch_and_sub
|
||||
#define atomic_fetch_and __sync_fetch_and_and
|
||||
#define atomic_fetch_or __sync_fetch_and_or
|
||||
#define atomic_cmpxchg __sync_val_compare_and_swap
|
||||
|
||||
/* And even shorter names that return void. */
|
||||
#define atomic_inc(ptr) ((void) __sync_fetch_and_add(ptr, 1))
|
||||
#define atomic_dec(ptr) ((void) __sync_fetch_and_add(ptr, -1))
|
||||
#define atomic_add(ptr, n) ((void) __sync_fetch_and_add(ptr, n))
|
||||
#define atomic_sub(ptr, n) ((void) __sync_fetch_and_sub(ptr, n))
|
||||
#define atomic_and(ptr, n) ((void) __sync_fetch_and_and(ptr, n))
|
||||
#define atomic_or(ptr, n) ((void) __sync_fetch_and_or(ptr, n))
|
||||
|
||||
#endif
|
||||
245
qemu/include/qemu/bitmap.h
Normal file
245
qemu/include/qemu/bitmap.h
Normal file
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* Bitmap Module
|
||||
*
|
||||
* Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
|
||||
*
|
||||
* Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
|
||||
*
|
||||
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
||||
* See the COPYING.LIB file in the top-level directory.
|
||||
*/
|
||||
|
||||
#ifndef BITMAP_H
|
||||
#define BITMAP_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/bitops.h"
|
||||
|
||||
/*
|
||||
* The available bitmap operations and their rough meaning in the
|
||||
* case that the bitmap is a single unsigned long are thus:
|
||||
*
|
||||
* Note that nbits should be always a compile time evaluable constant.
|
||||
* Otherwise many inlines will generate horrible code.
|
||||
*
|
||||
* bitmap_zero(dst, nbits) *dst = 0UL
|
||||
* bitmap_fill(dst, nbits) *dst = ~0UL
|
||||
* bitmap_copy(dst, src, nbits) *dst = *src
|
||||
* bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
|
||||
* bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
|
||||
* bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
|
||||
* bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
|
||||
* bitmap_complement(dst, src, nbits) *dst = ~(*src)
|
||||
* bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
|
||||
* bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
|
||||
* bitmap_empty(src, nbits) Are all bits zero in *src?
|
||||
* bitmap_full(src, nbits) Are all bits set in *src?
|
||||
* bitmap_set(dst, pos, nbits) Set specified bit area
|
||||
* bitmap_clear(dst, pos, nbits) Clear specified bit area
|
||||
* bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
|
||||
*/
|
||||
|
||||
/*
|
||||
* Also the following operations apply to bitmaps.
|
||||
*
|
||||
* set_bit(bit, addr) *addr |= bit
|
||||
* clear_bit(bit, addr) *addr &= ~bit
|
||||
* change_bit(bit, addr) *addr ^= bit
|
||||
* test_bit(bit, addr) Is bit set in *addr?
|
||||
* test_and_set_bit(bit, addr) Set bit and return old value
|
||||
* test_and_clear_bit(bit, addr) Clear bit and return old value
|
||||
* test_and_change_bit(bit, addr) Change bit and return old value
|
||||
* find_first_zero_bit(addr, nbits) Position first zero bit in *addr
|
||||
* find_first_bit(addr, nbits) Position first set bit in *addr
|
||||
* find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
|
||||
* find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
|
||||
*/
|
||||
|
||||
#define BITMAP_LAST_WORD_MASK(nbits) \
|
||||
( \
|
||||
((nbits) % BITS_PER_LONG) ? \
|
||||
(1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
|
||||
)
|
||||
|
||||
#define DECLARE_BITMAP(name,bits) \
|
||||
unsigned long name[BITS_TO_LONGS(bits)]
|
||||
|
||||
#define small_nbits(nbits) \
|
||||
((nbits) <= BITS_PER_LONG)
|
||||
|
||||
int slow_bitmap_empty(const unsigned long *bitmap, long bits);
|
||||
int slow_bitmap_full(const unsigned long *bitmap, long bits);
|
||||
int slow_bitmap_equal(const unsigned long *bitmap1,
|
||||
const unsigned long *bitmap2, long bits);
|
||||
void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
|
||||
long bits);
|
||||
void slow_bitmap_shift_right(unsigned long *dst,
|
||||
const unsigned long *src, int shift, long bits);
|
||||
void slow_bitmap_shift_left(unsigned long *dst,
|
||||
const unsigned long *src, int shift, long bits);
|
||||
int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
||||
const unsigned long *bitmap2, long bits);
|
||||
void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
||||
const unsigned long *bitmap2, long bits);
|
||||
void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
||||
const unsigned long *bitmap2, long bits);
|
||||
int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
||||
const unsigned long *bitmap2, long bits);
|
||||
int slow_bitmap_intersects(const unsigned long *bitmap1,
|
||||
const unsigned long *bitmap2, long bits);
|
||||
|
||||
static inline unsigned long *bitmap_try_new(long nbits)
|
||||
{
|
||||
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||
return g_try_malloc0(len);
|
||||
}
|
||||
|
||||
static inline unsigned long *bitmap_new(long nbits)
|
||||
{
|
||||
unsigned long *ptr = bitmap_try_new(nbits);
|
||||
if (ptr == NULL) {
|
||||
abort();
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static inline void bitmap_zero(unsigned long *dst, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
*dst = 0UL;
|
||||
} else {
|
||||
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||
memset(dst, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void bitmap_fill(unsigned long *dst, long nbits)
|
||||
{
|
||||
size_t nlongs = BITS_TO_LONGS(nbits);
|
||||
if (!small_nbits(nbits)) {
|
||||
long len = (nlongs - 1) * sizeof(unsigned long);
|
||||
memset(dst, 0xff, len);
|
||||
}
|
||||
dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
|
||||
}
|
||||
|
||||
static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
|
||||
long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
*dst = *src;
|
||||
} else {
|
||||
long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||
memcpy(dst, src, len);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
|
||||
const unsigned long *src2, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return (*dst = *src1 & *src2) != 0;
|
||||
}
|
||||
return slow_bitmap_and(dst, src1, src2, nbits);
|
||||
}
|
||||
|
||||
static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
||||
const unsigned long *src2, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
*dst = *src1 | *src2;
|
||||
} else {
|
||||
slow_bitmap_or(dst, src1, src2, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
|
||||
const unsigned long *src2, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
*dst = *src1 ^ *src2;
|
||||
} else {
|
||||
slow_bitmap_xor(dst, src1, src2, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
||||
const unsigned long *src2, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return (*dst = *src1 & ~(*src2)) != 0;
|
||||
}
|
||||
return slow_bitmap_andnot(dst, src1, src2, nbits);
|
||||
}
|
||||
|
||||
static inline void bitmap_complement(unsigned long *dst,
|
||||
const unsigned long *src,
|
||||
long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
*dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
|
||||
} else {
|
||||
slow_bitmap_complement(dst, src, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int bitmap_equal(const unsigned long *src1,
|
||||
const unsigned long *src2, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
|
||||
} else {
|
||||
return slow_bitmap_equal(src1, src2, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int bitmap_empty(const unsigned long *src, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
|
||||
} else {
|
||||
return slow_bitmap_empty(src, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int bitmap_full(const unsigned long *src, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
|
||||
} else {
|
||||
return slow_bitmap_full(src, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int bitmap_intersects(const unsigned long *src1,
|
||||
const unsigned long *src2, long nbits)
|
||||
{
|
||||
if (small_nbits(nbits)) {
|
||||
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
||||
} else {
|
||||
return slow_bitmap_intersects(src1, src2, nbits);
|
||||
}
|
||||
}
|
||||
|
||||
void bitmap_set(unsigned long *map, long i, long len);
|
||||
void bitmap_clear(unsigned long *map, long start, long nr);
|
||||
unsigned long bitmap_find_next_zero_area(unsigned long *map,
|
||||
unsigned long size,
|
||||
unsigned long start,
|
||||
unsigned long nr,
|
||||
unsigned long align_mask);
|
||||
|
||||
static inline unsigned long *bitmap_zero_extend(unsigned long *old,
|
||||
long old_nbits, long new_nbits)
|
||||
{
|
||||
long new_len = BITS_TO_LONGS(new_nbits) * sizeof(unsigned long);
|
||||
unsigned long *new = g_realloc(old, new_len);
|
||||
bitmap_clear(new, old_nbits, new_nbits - old_nbits);
|
||||
return new;
|
||||
}
|
||||
|
||||
#endif /* BITMAP_H */
|
||||
418
qemu/include/qemu/bitops.h
Normal file
418
qemu/include/qemu/bitops.h
Normal file
@@ -0,0 +1,418 @@
|
||||
/*
|
||||
* Bitops Module
|
||||
*
|
||||
* Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
|
||||
*
|
||||
* Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
|
||||
*
|
||||
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
||||
* See the COPYING.LIB file in the top-level directory.
|
||||
*/
|
||||
|
||||
#ifndef BITOPS_H
|
||||
#define BITOPS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "host-utils.h"
|
||||
|
||||
#define BITS_PER_BYTE CHAR_BIT
|
||||
#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
|
||||
|
||||
#define BIT(nr) (1UL << (nr))
|
||||
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
|
||||
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
|
||||
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
|
||||
|
||||
/**
|
||||
* set_bit - Set a bit in memory
|
||||
* @nr: the bit to set
|
||||
* @addr: the address to start counting from
|
||||
*/
|
||||
static inline void set_bit(long nr, unsigned long *addr)
|
||||
{
|
||||
unsigned long mask = BIT_MASK(nr);
|
||||
unsigned long *p = addr + BIT_WORD(nr);
|
||||
|
||||
*p |= mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* clear_bit - Clears a bit in memory
|
||||
* @nr: Bit to clear
|
||||
* @addr: Address to start counting from
|
||||
*/
|
||||
static inline void clear_bit(long nr, unsigned long *addr)
|
||||
{
|
||||
unsigned long mask = BIT_MASK(nr);
|
||||
unsigned long *p = addr + BIT_WORD(nr);
|
||||
|
||||
*p &= ~mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* change_bit - Toggle a bit in memory
|
||||
* @nr: Bit to change
|
||||
* @addr: Address to start counting from
|
||||
*/
|
||||
static inline void change_bit(long nr, unsigned long *addr)
|
||||
{
|
||||
unsigned long mask = BIT_MASK(nr);
|
||||
unsigned long *p = addr + BIT_WORD(nr);
|
||||
|
||||
*p ^= mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* test_and_set_bit - Set a bit and return its old value
|
||||
* @nr: Bit to set
|
||||
* @addr: Address to count from
|
||||
*/
|
||||
static inline int test_and_set_bit(long nr, unsigned long *addr)
|
||||
{
|
||||
unsigned long mask = BIT_MASK(nr);
|
||||
unsigned long *p = addr + BIT_WORD(nr);
|
||||
unsigned long old = *p;
|
||||
|
||||
*p = old | mask;
|
||||
return (old & mask) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* test_and_clear_bit - Clear a bit and return its old value
|
||||
* @nr: Bit to clear
|
||||
* @addr: Address to count from
|
||||
*/
|
||||
static inline int test_and_clear_bit(long nr, unsigned long *addr)
|
||||
{
|
||||
unsigned long mask = BIT_MASK(nr);
|
||||
unsigned long *p = addr + BIT_WORD(nr);
|
||||
unsigned long old = *p;
|
||||
|
||||
*p = old & ~mask;
|
||||
return (old & mask) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* test_and_change_bit - Change a bit and return its old value
|
||||
* @nr: Bit to change
|
||||
* @addr: Address to count from
|
||||
*/
|
||||
static inline int test_and_change_bit(long nr, unsigned long *addr)
|
||||
{
|
||||
unsigned long mask = BIT_MASK(nr);
|
||||
unsigned long *p = addr + BIT_WORD(nr);
|
||||
unsigned long old = *p;
|
||||
|
||||
*p = old ^ mask;
|
||||
return (old & mask) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* test_bit - Determine whether a bit is set
|
||||
* @nr: bit number to test
|
||||
* @addr: Address to start counting from
|
||||
*/
|
||||
static inline int test_bit(long nr, const unsigned long *addr)
|
||||
{
|
||||
return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* find_last_bit - find the last set bit in a memory region
|
||||
* @addr: The address to start the search at
|
||||
* @size: The maximum size to search
|
||||
*
|
||||
* Returns the bit number of the first set bit, or size.
|
||||
*/
|
||||
unsigned long find_last_bit(const unsigned long *addr,
|
||||
unsigned long size);
|
||||
|
||||
/**
|
||||
* find_next_bit - find the next set bit in a memory region
|
||||
* @addr: The address to base the search on
|
||||
* @offset: The bitnumber to start searching at
|
||||
* @size: The bitmap size in bits
|
||||
*/
|
||||
unsigned long find_next_bit(const unsigned long *addr,
|
||||
unsigned long size, unsigned long offset);
|
||||
|
||||
/**
|
||||
* find_next_zero_bit - find the next cleared bit in a memory region
|
||||
* @addr: The address to base the search on
|
||||
* @offset: The bitnumber to start searching at
|
||||
* @size: The bitmap size in bits
|
||||
*/
|
||||
|
||||
unsigned long find_next_zero_bit(const unsigned long *addr,
|
||||
unsigned long size,
|
||||
unsigned long offset);
|
||||
|
||||
/**
|
||||
* find_first_bit - find the first set bit in a memory region
|
||||
* @addr: The address to start the search at
|
||||
* @size: The maximum size to search
|
||||
*
|
||||
* Returns the bit number of the first set bit.
|
||||
*/
|
||||
static inline unsigned long find_first_bit(const unsigned long *addr,
|
||||
unsigned long size)
|
||||
{
|
||||
unsigned long result, tmp;
|
||||
|
||||
for (result = 0; result < size; result += BITS_PER_LONG) {
|
||||
tmp = *addr++;
|
||||
if (tmp) {
|
||||
result += ctzl(tmp);
|
||||
return result < size ? result : size;
|
||||
}
|
||||
}
|
||||
/* Not found */
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* find_first_zero_bit - find the first cleared bit in a memory region
|
||||
* @addr: The address to start the search at
|
||||
* @size: The maximum size to search
|
||||
*
|
||||
* Returns the bit number of the first cleared bit.
|
||||
*/
|
||||
static inline unsigned long find_first_zero_bit(const unsigned long *addr,
|
||||
unsigned long size)
|
||||
{
|
||||
return find_next_zero_bit(addr, size, 0);
|
||||
}
|
||||
|
||||
static inline unsigned long hweight_long(unsigned long w)
|
||||
{
|
||||
unsigned long count;
|
||||
|
||||
for (count = 0; w; w >>= 1) {
|
||||
count += w & 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* rol8 - rotate an 8-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint8_t rol8(uint8_t word, unsigned int shift)
|
||||
{
|
||||
return (word << shift) | (word >> (8 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror8 - rotate an 8-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint8_t ror8(uint8_t word, unsigned int shift)
|
||||
{
|
||||
return (word >> shift) | (word << (8 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* rol16 - rotate a 16-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint16_t rol16(uint16_t word, unsigned int shift)
|
||||
{
|
||||
return (word << shift) | (word >> (16 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror16 - rotate a 16-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint16_t ror16(uint16_t word, unsigned int shift)
|
||||
{
|
||||
return (word >> shift) | (word << (16 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* rol32 - rotate a 32-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint32_t rol32(uint32_t word, unsigned int shift)
|
||||
{
|
||||
return (word << shift) | (word >> (32 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror32 - rotate a 32-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint32_t ror32(uint32_t word, unsigned int shift)
|
||||
{
|
||||
return (word >> shift) | (word << (32 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* rol64 - rotate a 64-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint64_t rol64(uint64_t word, unsigned int shift)
|
||||
{
|
||||
return (word << shift) | (word >> (64 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror64 - rotate a 64-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline uint64_t ror64(uint64_t word, unsigned int shift)
|
||||
{
|
||||
return (word >> shift) | (word << (64 - shift));
|
||||
}
|
||||
|
||||
/**
|
||||
* extract32:
|
||||
* @value: the value to extract the bit field from
|
||||
* @start: the lowest bit in the bit field (numbered from 0)
|
||||
* @length: the length of the bit field
|
||||
*
|
||||
* Extract from the 32 bit input @value the bit field specified by the
|
||||
* @start and @length parameters, and return it. The bit field must
|
||||
* lie entirely within the 32 bit word. It is valid to request that
|
||||
* all 32 bits are returned (ie @length 32 and @start 0).
|
||||
*
|
||||
* Returns: the value of the bit field extracted from the input value.
|
||||
*/
|
||||
static inline uint32_t extract32(uint32_t value, int start, int length)
|
||||
{
|
||||
assert(start >= 0 && length > 0 && length <= 32 - start);
|
||||
return (value >> start) & (~0U >> (32 - length));
|
||||
}
|
||||
|
||||
/**
|
||||
* extract64:
|
||||
* @value: the value to extract the bit field from
|
||||
* @start: the lowest bit in the bit field (numbered from 0)
|
||||
* @length: the length of the bit field
|
||||
*
|
||||
* Extract from the 64 bit input @value the bit field specified by the
|
||||
* @start and @length parameters, and return it. The bit field must
|
||||
* lie entirely within the 64 bit word. It is valid to request that
|
||||
* all 64 bits are returned (ie @length 64 and @start 0).
|
||||
*
|
||||
* Returns: the value of the bit field extracted from the input value.
|
||||
*/
|
||||
static inline uint64_t extract64(uint64_t value, int start, int length)
|
||||
{
|
||||
assert(start >= 0 && length > 0 && length <= 64 - start);
|
||||
return (value >> start) & (~0ULL >> (64 - length));
|
||||
}
|
||||
|
||||
/**
|
||||
* sextract32:
|
||||
* @value: the value to extract the bit field from
|
||||
* @start: the lowest bit in the bit field (numbered from 0)
|
||||
* @length: the length of the bit field
|
||||
*
|
||||
* Extract from the 32 bit input @value the bit field specified by the
|
||||
* @start and @length parameters, and return it, sign extended to
|
||||
* an int32_t (ie with the most significant bit of the field propagated
|
||||
* to all the upper bits of the return value). The bit field must lie
|
||||
* entirely within the 32 bit word. It is valid to request that
|
||||
* all 32 bits are returned (ie @length 32 and @start 0).
|
||||
*
|
||||
* Returns: the sign extended value of the bit field extracted from the
|
||||
* input value.
|
||||
*/
|
||||
static inline int32_t sextract32(uint32_t value, int start, int length)
|
||||
{
|
||||
assert(start >= 0 && length > 0 && length <= 32 - start);
|
||||
/* Note that this implementation relies on right shift of signed
|
||||
* integers being an arithmetic shift.
|
||||
*/
|
||||
return ((int32_t)(value << (32 - length - start))) >> (32 - length);
|
||||
}
|
||||
|
||||
/**
|
||||
* sextract64:
|
||||
* @value: the value to extract the bit field from
|
||||
* @start: the lowest bit in the bit field (numbered from 0)
|
||||
* @length: the length of the bit field
|
||||
*
|
||||
* Extract from the 64 bit input @value the bit field specified by the
|
||||
* @start and @length parameters, and return it, sign extended to
|
||||
* an int64_t (ie with the most significant bit of the field propagated
|
||||
* to all the upper bits of the return value). The bit field must lie
|
||||
* entirely within the 64 bit word. It is valid to request that
|
||||
* all 64 bits are returned (ie @length 64 and @start 0).
|
||||
*
|
||||
* Returns: the sign extended value of the bit field extracted from the
|
||||
* input value.
|
||||
*/
|
||||
static inline uint64_t sextract64(uint64_t value, int start, int length)
|
||||
{
|
||||
assert(start >= 0 && length > 0 && length <= 64 - start);
|
||||
/* Note that this implementation relies on right shift of signed
|
||||
* integers being an arithmetic shift.
|
||||
*/
|
||||
return ((int64_t)(value << (64 - length - start))) >> (64 - length);
|
||||
}
|
||||
|
||||
/**
|
||||
* deposit32:
|
||||
* @value: initial value to insert bit field into
|
||||
* @start: the lowest bit in the bit field (numbered from 0)
|
||||
* @length: the length of the bit field
|
||||
* @fieldval: the value to insert into the bit field
|
||||
*
|
||||
* Deposit @fieldval into the 32 bit @value at the bit field specified
|
||||
* by the @start and @length parameters, and return the modified
|
||||
* @value. Bits of @value outside the bit field are not modified.
|
||||
* Bits of @fieldval above the least significant @length bits are
|
||||
* ignored. The bit field must lie entirely within the 32 bit word.
|
||||
* It is valid to request that all 32 bits are modified (ie @length
|
||||
* 32 and @start 0).
|
||||
*
|
||||
* Returns: the modified @value.
|
||||
*/
|
||||
static inline uint32_t deposit32(uint32_t value, int start, int length,
|
||||
uint32_t fieldval)
|
||||
{
|
||||
uint32_t mask;
|
||||
assert(start >= 0 && length > 0 && length <= 32 - start);
|
||||
mask = (~0U >> (32 - length)) << start;
|
||||
return (value & ~mask) | ((fieldval << start) & mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* deposit64:
|
||||
* @value: initial value to insert bit field into
|
||||
* @start: the lowest bit in the bit field (numbered from 0)
|
||||
* @length: the length of the bit field
|
||||
* @fieldval: the value to insert into the bit field
|
||||
*
|
||||
* Deposit @fieldval into the 64 bit @value at the bit field specified
|
||||
* by the @start and @length parameters, and return the modified
|
||||
* @value. Bits of @value outside the bit field are not modified.
|
||||
* Bits of @fieldval above the least significant @length bits are
|
||||
* ignored. The bit field must lie entirely within the 64 bit word.
|
||||
* It is valid to request that all 64 bits are modified (ie @length
|
||||
* 64 and @start 0).
|
||||
*
|
||||
* Returns: the modified @value.
|
||||
*/
|
||||
static inline uint64_t deposit64(uint64_t value, int start, int length,
|
||||
uint64_t fieldval)
|
||||
{
|
||||
uint64_t mask;
|
||||
assert(start >= 0 && length > 0 && length <= 64 - start);
|
||||
mask = (~0ULL >> (64 - length)) << start;
|
||||
return (value & ~mask) | ((fieldval << start) & mask);
|
||||
}
|
||||
|
||||
#endif
|
||||
434
qemu/include/qemu/bswap.h
Normal file
434
qemu/include/qemu/bswap.h
Normal file
@@ -0,0 +1,434 @@
|
||||
#ifndef BSWAP_H
|
||||
#define BSWAP_H
|
||||
|
||||
#include "config-host.h"
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include "fpu/softfloat.h"
|
||||
|
||||
#ifdef CONFIG_MACHINE_BSWAP_H
|
||||
# include <sys/endian.h>
|
||||
# include <sys/types.h>
|
||||
# include <machine/bswap.h>
|
||||
#elif defined(__FreeBSD__)
|
||||
# include <sys/endian.h>
|
||||
#elif defined(CONFIG_BYTESWAP_H)
|
||||
# include <byteswap.h>
|
||||
|
||||
static inline uint16_t bswap16(uint16_t x)
|
||||
{
|
||||
return bswap_16(x);
|
||||
}
|
||||
|
||||
static inline uint32_t bswap32(uint32_t x)
|
||||
{
|
||||
return bswap_32(x);
|
||||
}
|
||||
|
||||
static inline uint64_t bswap64(uint64_t x)
|
||||
{
|
||||
return bswap_64(x);
|
||||
}
|
||||
# else
|
||||
static inline uint16_t bswap16(uint16_t x)
|
||||
{
|
||||
return (((x & 0x00ff) << 8) |
|
||||
((x & 0xff00) >> 8));
|
||||
}
|
||||
|
||||
static inline uint32_t bswap32(uint32_t x)
|
||||
{
|
||||
return (((x & 0x000000ffU) << 24) |
|
||||
((x & 0x0000ff00U) << 8) |
|
||||
((x & 0x00ff0000U) >> 8) |
|
||||
((x & 0xff000000U) >> 24));
|
||||
}
|
||||
|
||||
static inline uint64_t bswap64(uint64_t x)
|
||||
{
|
||||
return (((x & 0x00000000000000ffULL) << 56) |
|
||||
((x & 0x000000000000ff00ULL) << 40) |
|
||||
((x & 0x0000000000ff0000ULL) << 24) |
|
||||
((x & 0x00000000ff000000ULL) << 8) |
|
||||
((x & 0x000000ff00000000ULL) >> 8) |
|
||||
((x & 0x0000ff0000000000ULL) >> 24) |
|
||||
((x & 0x00ff000000000000ULL) >> 40) |
|
||||
((x & 0xff00000000000000ULL) >> 56));
|
||||
}
|
||||
#endif /* ! CONFIG_MACHINE_BSWAP_H */
|
||||
|
||||
static inline void bswap16s(uint16_t *s)
|
||||
{
|
||||
*s = bswap16(*s);
|
||||
}
|
||||
|
||||
static inline void bswap32s(uint32_t *s)
|
||||
{
|
||||
*s = bswap32(*s);
|
||||
}
|
||||
|
||||
static inline void bswap64s(uint64_t *s)
|
||||
{
|
||||
*s = bswap64(*s);
|
||||
}
|
||||
|
||||
#if defined(HOST_WORDS_BIGENDIAN)
|
||||
#define be_bswap(v, size) (v)
|
||||
#define le_bswap(v, size) glue(bswap, size)(v)
|
||||
#define be_bswaps(v, size)
|
||||
#define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
|
||||
#else
|
||||
#define le_bswap(v, size) (v)
|
||||
#define be_bswap(v, size) glue(bswap, size)(v)
|
||||
#define le_bswaps(v, size)
|
||||
#define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
|
||||
#endif
|
||||
|
||||
#define CPU_CONVERT(endian, size, type)\
|
||||
static inline type endian ## size ## _to_cpu(type v)\
|
||||
{\
|
||||
return glue(endian, _bswap)(v, size);\
|
||||
}\
|
||||
\
|
||||
static inline type cpu_to_ ## endian ## size(type v)\
|
||||
{\
|
||||
return glue(endian, _bswap)(v, size);\
|
||||
}\
|
||||
\
|
||||
static inline void endian ## size ## _to_cpus(type *p)\
|
||||
{\
|
||||
glue(endian, _bswaps)(p, size);\
|
||||
}\
|
||||
\
|
||||
static inline void cpu_to_ ## endian ## size ## s(type *p)\
|
||||
{\
|
||||
glue(endian, _bswaps)(p, size);\
|
||||
}\
|
||||
\
|
||||
static inline type endian ## size ## _to_cpup(const type *p)\
|
||||
{\
|
||||
return glue(glue(endian, size), _to_cpu)(*p);\
|
||||
}\
|
||||
\
|
||||
static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
|
||||
{\
|
||||
*p = glue(glue(cpu_to_, endian), size)(v);\
|
||||
}
|
||||
|
||||
CPU_CONVERT(be, 16, uint16_t)
|
||||
CPU_CONVERT(be, 32, uint32_t)
|
||||
CPU_CONVERT(be, 64, uint64_t)
|
||||
|
||||
CPU_CONVERT(le, 16, uint16_t)
|
||||
CPU_CONVERT(le, 32, uint32_t)
|
||||
CPU_CONVERT(le, 64, uint64_t)
|
||||
|
||||
/* len must be one of 1, 2, 4 */
|
||||
static inline uint32_t qemu_bswap_len(uint32_t value, int len)
|
||||
{
|
||||
return bswap32(value) >> (32 - 8 * len);
|
||||
}
|
||||
|
||||
/* Unions for reinterpreting between floats and integers. */
|
||||
|
||||
typedef union {
|
||||
float32 f;
|
||||
uint32_t l;
|
||||
} CPU_FloatU;
|
||||
|
||||
typedef union {
|
||||
float64 d;
|
||||
#if defined(HOST_WORDS_BIGENDIAN)
|
||||
struct {
|
||||
uint32_t upper;
|
||||
uint32_t lower;
|
||||
} l;
|
||||
#else
|
||||
struct {
|
||||
uint32_t lower;
|
||||
uint32_t upper;
|
||||
} l;
|
||||
#endif
|
||||
uint64_t ll;
|
||||
} CPU_DoubleU;
|
||||
|
||||
typedef union {
|
||||
floatx80 d;
|
||||
struct {
|
||||
uint64_t lower;
|
||||
uint16_t upper;
|
||||
} l;
|
||||
} CPU_LDoubleU;
|
||||
|
||||
typedef union {
|
||||
float128 q;
|
||||
#if defined(HOST_WORDS_BIGENDIAN)
|
||||
struct {
|
||||
uint32_t upmost;
|
||||
uint32_t upper;
|
||||
uint32_t lower;
|
||||
uint32_t lowest;
|
||||
} l;
|
||||
struct {
|
||||
uint64_t upper;
|
||||
uint64_t lower;
|
||||
} ll;
|
||||
#else
|
||||
struct {
|
||||
uint32_t lowest;
|
||||
uint32_t lower;
|
||||
uint32_t upper;
|
||||
uint32_t upmost;
|
||||
} l;
|
||||
struct {
|
||||
uint64_t lower;
|
||||
uint64_t upper;
|
||||
} ll;
|
||||
#endif
|
||||
} CPU_QuadU;
|
||||
|
||||
/* unaligned/endian-independent pointer access */
|
||||
|
||||
/*
|
||||
* the generic syntax is:
|
||||
*
|
||||
* load: ld{type}{sign}{size}{endian}_p(ptr)
|
||||
*
|
||||
* store: st{type}{size}{endian}_p(ptr, val)
|
||||
*
|
||||
* Note there are small differences with the softmmu access API!
|
||||
*
|
||||
* type is:
|
||||
* (empty): integer access
|
||||
* f : float access
|
||||
*
|
||||
* sign is:
|
||||
* (empty): for floats or 32 bit size
|
||||
* u : unsigned
|
||||
* s : signed
|
||||
*
|
||||
* size is:
|
||||
* b: 8 bits
|
||||
* w: 16 bits
|
||||
* l: 32 bits
|
||||
* q: 64 bits
|
||||
*
|
||||
* endian is:
|
||||
* he : host endian
|
||||
* be : big endian
|
||||
* le : little endian
|
||||
* (except for byte accesses, which have no endian infix).
|
||||
*/
|
||||
|
||||
static inline int ldub_p(const void *ptr)
|
||||
{
|
||||
return *(uint8_t *)ptr;
|
||||
}
|
||||
|
||||
static inline int ldsb_p(const void *ptr)
|
||||
{
|
||||
return *(int8_t *)ptr;
|
||||
}
|
||||
|
||||
static inline void stb_p(void *ptr, uint8_t v)
|
||||
{
|
||||
*(uint8_t *)ptr = v;
|
||||
}
|
||||
|
||||
/* Any compiler worth its salt will turn these memcpy into native unaligned
|
||||
operations. Thus we don't need to play games with packed attributes, or
|
||||
inline byte-by-byte stores. */
|
||||
|
||||
static inline int lduw_he_p(const void *ptr)
|
||||
{
|
||||
uint16_t r;
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline int ldsw_he_p(const void *ptr)
|
||||
{
|
||||
int16_t r;
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void stw_he_p(void *ptr, uint16_t v)
|
||||
{
|
||||
memcpy(ptr, &v, sizeof(v));
|
||||
}
|
||||
|
||||
static inline int ldl_he_p(const void *ptr)
|
||||
{
|
||||
int32_t r;
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void stl_he_p(void *ptr, uint32_t v)
|
||||
{
|
||||
memcpy(ptr, &v, sizeof(v));
|
||||
}
|
||||
|
||||
static inline uint64_t ldq_he_p(const void *ptr)
|
||||
{
|
||||
uint64_t r;
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void stq_he_p(void *ptr, uint64_t v)
|
||||
{
|
||||
memcpy(ptr, &v, sizeof(v));
|
||||
}
|
||||
|
||||
static inline int lduw_le_p(const void *ptr)
|
||||
{
|
||||
return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
|
||||
}
|
||||
|
||||
static inline int ldsw_le_p(const void *ptr)
|
||||
{
|
||||
return (int16_t)le_bswap(lduw_he_p(ptr), 16);
|
||||
}
|
||||
|
||||
static inline int ldl_le_p(const void *ptr)
|
||||
{
|
||||
return le_bswap(ldl_he_p(ptr), 32);
|
||||
}
|
||||
|
||||
static inline uint64_t ldq_le_p(const void *ptr)
|
||||
{
|
||||
return le_bswap(ldq_he_p(ptr), 64);
|
||||
}
|
||||
|
||||
static inline void stw_le_p(void *ptr, uint16_t v)
|
||||
{
|
||||
stw_he_p(ptr, le_bswap(v, 16));
|
||||
}
|
||||
|
||||
static inline void stl_le_p(void *ptr, uint32_t v)
|
||||
{
|
||||
stl_he_p(ptr, le_bswap(v, 32));
|
||||
}
|
||||
|
||||
static inline void stq_le_p(void *ptr, uint64_t v)
|
||||
{
|
||||
stq_he_p(ptr, le_bswap(v, 64));
|
||||
}
|
||||
|
||||
/* float access */
|
||||
|
||||
static inline float32 ldfl_le_p(const void *ptr)
|
||||
{
|
||||
CPU_FloatU u;
|
||||
u.l = ldl_le_p(ptr);
|
||||
return u.f;
|
||||
}
|
||||
|
||||
static inline void stfl_le_p(void *ptr, float32 v)
|
||||
{
|
||||
CPU_FloatU u;
|
||||
u.f = v;
|
||||
stl_le_p(ptr, u.l);
|
||||
}
|
||||
|
||||
static inline float64 ldfq_le_p(const void *ptr)
|
||||
{
|
||||
CPU_DoubleU u;
|
||||
u.ll = ldq_le_p(ptr);
|
||||
return u.d;
|
||||
}
|
||||
|
||||
static inline void stfq_le_p(void *ptr, float64 v)
|
||||
{
|
||||
CPU_DoubleU u;
|
||||
u.d = v;
|
||||
stq_le_p(ptr, u.ll);
|
||||
}
|
||||
|
||||
static inline int lduw_be_p(const void *ptr)
|
||||
{
|
||||
return (uint16_t)be_bswap(lduw_he_p(ptr), 16);
|
||||
}
|
||||
|
||||
static inline int ldsw_be_p(const void *ptr)
|
||||
{
|
||||
return (int16_t)be_bswap(lduw_he_p(ptr), 16);
|
||||
}
|
||||
|
||||
static inline int ldl_be_p(const void *ptr)
|
||||
{
|
||||
return be_bswap(ldl_he_p(ptr), 32);
|
||||
}
|
||||
|
||||
static inline uint64_t ldq_be_p(const void *ptr)
|
||||
{
|
||||
return be_bswap(ldq_he_p(ptr), 64);
|
||||
}
|
||||
|
||||
static inline void stw_be_p(void *ptr, uint16_t v)
|
||||
{
|
||||
stw_he_p(ptr, be_bswap(v, 16));
|
||||
}
|
||||
|
||||
static inline void stl_be_p(void *ptr, uint32_t v)
|
||||
{
|
||||
stl_he_p(ptr, be_bswap(v, 32));
|
||||
}
|
||||
|
||||
static inline void stq_be_p(void *ptr, uint64_t v)
|
||||
{
|
||||
stq_he_p(ptr, be_bswap(v, 64));
|
||||
}
|
||||
|
||||
/* float access */
|
||||
|
||||
static inline float32 ldfl_be_p(const void *ptr)
|
||||
{
|
||||
CPU_FloatU u;
|
||||
u.l = ldl_be_p(ptr);
|
||||
return u.f;
|
||||
}
|
||||
|
||||
static inline void stfl_be_p(void *ptr, float32 v)
|
||||
{
|
||||
CPU_FloatU u;
|
||||
u.f = v;
|
||||
stl_be_p(ptr, u.l);
|
||||
}
|
||||
|
||||
static inline float64 ldfq_be_p(const void *ptr)
|
||||
{
|
||||
CPU_DoubleU u;
|
||||
u.ll = ldq_be_p(ptr);
|
||||
return u.d;
|
||||
}
|
||||
|
||||
static inline void stfq_be_p(void *ptr, float64 v)
|
||||
{
|
||||
CPU_DoubleU u;
|
||||
u.d = v;
|
||||
stq_be_p(ptr, u.ll);
|
||||
}
|
||||
|
||||
static inline unsigned long leul_to_cpu(unsigned long v)
|
||||
{
|
||||
/* In order to break an include loop between here and
|
||||
qemu-common.h, don't rely on HOST_LONG_BITS. */
|
||||
#if ULONG_MAX == UINT32_MAX
|
||||
return le_bswap(v, 32);
|
||||
#elif ULONG_MAX == UINT64_MAX
|
||||
return le_bswap(v, 64);
|
||||
#else
|
||||
# error Unknown sizeof long
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef le_bswap
|
||||
#undef be_bswap
|
||||
#undef le_bswaps
|
||||
#undef be_bswaps
|
||||
|
||||
#endif /* BSWAP_H */
|
||||
61
qemu/include/qemu/compiler.h
Normal file
61
qemu/include/qemu/compiler.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/* public domain */
|
||||
|
||||
#ifndef COMPILER_H
|
||||
#define COMPILER_H
|
||||
|
||||
#include "config-host.h"
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
| The macro QEMU_GNUC_PREREQ tests for minimum version of the GNU C compiler.
|
||||
| The code is a copy of SOFTFLOAT_GNUC_PREREQ, see softfloat-macros.h.
|
||||
*----------------------------------------------------------------------------*/
|
||||
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
|
||||
# define QEMU_GNUC_PREREQ(maj, min) \
|
||||
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
|
||||
#else
|
||||
# define QEMU_GNUC_PREREQ(maj, min) 0
|
||||
#endif
|
||||
|
||||
#define QEMU_NORETURN __attribute__ ((__noreturn__))
|
||||
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
#define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define QEMU_WARN_UNUSED_RESULT
|
||||
#endif
|
||||
|
||||
#if QEMU_GNUC_PREREQ(4, 3)
|
||||
#define QEMU_ARTIFICIAL __attribute__((always_inline, artificial))
|
||||
#else
|
||||
#define QEMU_ARTIFICIAL
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
# define QEMU_PACKED __attribute__((gcc_struct, packed))
|
||||
#else
|
||||
# define QEMU_PACKED __attribute__((packed))
|
||||
#endif
|
||||
|
||||
#define cat(x,y) x ## y
|
||||
#define cat2(x,y) cat(x,y)
|
||||
#define QEMU_BUILD_BUG_ON(x) \
|
||||
typedef char cat2(qemu_build_bug_on__,__LINE__)[(x)?-1:1] __attribute__((unused));
|
||||
|
||||
#if defined __GNUC__
|
||||
# if !QEMU_GNUC_PREREQ(4, 4)
|
||||
/* gcc versions before 4.4.x don't support gnu_printf, so use printf. */
|
||||
# define GCC_FMT_ATTR(n, m) __attribute__((format(printf, n, m)))
|
||||
# else
|
||||
/* Use gnu_printf when supported (qemu uses standard format strings). */
|
||||
# define GCC_FMT_ATTR(n, m) __attribute__((format(gnu_printf, n, m)))
|
||||
# if defined(_WIN32)
|
||||
/* Map __printf__ to __gnu_printf__ because we want standard format strings
|
||||
* even when MinGW or GLib include files use __printf__. */
|
||||
# define __printf__ __gnu_printf__
|
||||
# endif
|
||||
# endif
|
||||
#else
|
||||
#define GCC_FMT_ATTR(n, m)
|
||||
#endif
|
||||
|
||||
#endif /* COMPILER_H */
|
||||
35
qemu/include/qemu/crc32c.h
Normal file
35
qemu/include/qemu/crc32c.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Castagnoli CRC32C Checksum Algorithm
|
||||
*
|
||||
* Polynomial: 0x11EDC6F41
|
||||
*
|
||||
* Castagnoli93: Guy Castagnoli and Stefan Braeuer and Martin Herrman
|
||||
* "Optimization of Cyclic Redundancy-Check Codes with 24
|
||||
* and 32 Parity Bits",IEEE Transactions on Communication,
|
||||
* Volume 41, Number 6, June 1993
|
||||
*
|
||||
* Copyright (c) 2013 Red Hat, Inc.,
|
||||
*
|
||||
* Authors:
|
||||
* Jeff Cody <jcody@redhat.com>
|
||||
*
|
||||
* Based on the Linux kernel cryptographic crc32c module,
|
||||
*
|
||||
* Copyright (c) 2004 Cisco Systems, Inc.
|
||||
* Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QEMU_CRC32C_H
|
||||
#define QEMU_CRC32C_H
|
||||
|
||||
#include "qemu-common.h"
|
||||
|
||||
uint32_t crc32c(uint32_t crc, const uint8_t *data, unsigned int length);
|
||||
|
||||
#endif
|
||||
382
qemu/include/qemu/host-utils.h
Normal file
382
qemu/include/qemu/host-utils.h
Normal file
@@ -0,0 +1,382 @@
|
||||
/*
|
||||
* Utility compute operations used by translated code.
|
||||
*
|
||||
* Copyright (c) 2007 Thiemo Seufer
|
||||
* Copyright (c) 2007 Jocelyn Mayer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#ifndef HOST_UTILS_H
|
||||
#define HOST_UTILS_H 1
|
||||
|
||||
#include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef CONFIG_INT128
|
||||
static inline void mulu64(uint64_t *plow, uint64_t *phigh,
|
||||
uint64_t a, uint64_t b)
|
||||
{
|
||||
__uint128_t r = (__uint128_t)a * b;
|
||||
*plow = r;
|
||||
*phigh = r >> 64;
|
||||
}
|
||||
|
||||
static inline void muls64(uint64_t *plow, uint64_t *phigh,
|
||||
int64_t a, int64_t b)
|
||||
{
|
||||
__int128_t r = (__int128_t)a * b;
|
||||
*plow = r;
|
||||
*phigh = r >> 64;
|
||||
}
|
||||
|
||||
static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
|
||||
{
|
||||
if (divisor == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
__uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
|
||||
__uint128_t result = dividend / divisor;
|
||||
*plow = result;
|
||||
*phigh = dividend % divisor;
|
||||
return result > UINT64_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
|
||||
{
|
||||
if (divisor == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
__int128_t dividend = ((__int128_t)*phigh << 64) | *plow;
|
||||
__int128_t result = dividend / divisor;
|
||||
*plow = result;
|
||||
*phigh = dividend % divisor;
|
||||
return result != *plow;
|
||||
}
|
||||
}
|
||||
#else
|
||||
void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
|
||||
void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
|
||||
int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
|
||||
int divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* clz32 - count leading zeros in a 32-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 32 if the value is zero. Note that the GCC builtin is
|
||||
* undefined if the value is zero.
|
||||
*/
|
||||
static inline int clz32(uint32_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return val ? __builtin_clz(val) : 32;
|
||||
#else
|
||||
/* Binary search for the leading one bit. */
|
||||
int cnt = 0;
|
||||
|
||||
if (!(val & 0xFFFF0000U)) {
|
||||
cnt += 16;
|
||||
val <<= 16;
|
||||
}
|
||||
if (!(val & 0xFF000000U)) {
|
||||
cnt += 8;
|
||||
val <<= 8;
|
||||
}
|
||||
if (!(val & 0xF0000000U)) {
|
||||
cnt += 4;
|
||||
val <<= 4;
|
||||
}
|
||||
if (!(val & 0xC0000000U)) {
|
||||
cnt += 2;
|
||||
val <<= 2;
|
||||
}
|
||||
if (!(val & 0x80000000U)) {
|
||||
cnt++;
|
||||
val <<= 1;
|
||||
}
|
||||
if (!(val & 0x80000000U)) {
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* clo32 - count leading ones in a 32-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 32 if the value is -1.
|
||||
*/
|
||||
static inline int clo32(uint32_t val)
|
||||
{
|
||||
return clz32(~val);
|
||||
}
|
||||
|
||||
/**
|
||||
* clz64 - count leading zeros in a 64-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 64 if the value is zero. Note that the GCC builtin is
|
||||
* undefined if the value is zero.
|
||||
*/
|
||||
static inline int clz64(uint64_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return val ? __builtin_clzll(val) : 64;
|
||||
#else
|
||||
int cnt = 0;
|
||||
|
||||
if (!(val >> 32)) {
|
||||
cnt += 32;
|
||||
} else {
|
||||
val >>= 32;
|
||||
}
|
||||
|
||||
return cnt + clz32(val);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* clo64 - count leading ones in a 64-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 64 if the value is -1.
|
||||
*/
|
||||
static inline int clo64(uint64_t val)
|
||||
{
|
||||
return clz64(~val);
|
||||
}
|
||||
|
||||
/**
|
||||
* ctz32 - count trailing zeros in a 32-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 32 if the value is zero. Note that the GCC builtin is
|
||||
* undefined if the value is zero.
|
||||
*/
|
||||
static inline int ctz32(uint32_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return val ? __builtin_ctz(val) : 32;
|
||||
#else
|
||||
/* Binary search for the trailing one bit. */
|
||||
int cnt;
|
||||
|
||||
cnt = 0;
|
||||
if (!(val & 0x0000FFFFUL)) {
|
||||
cnt += 16;
|
||||
val >>= 16;
|
||||
}
|
||||
if (!(val & 0x000000FFUL)) {
|
||||
cnt += 8;
|
||||
val >>= 8;
|
||||
}
|
||||
if (!(val & 0x0000000FUL)) {
|
||||
cnt += 4;
|
||||
val >>= 4;
|
||||
}
|
||||
if (!(val & 0x00000003UL)) {
|
||||
cnt += 2;
|
||||
val >>= 2;
|
||||
}
|
||||
if (!(val & 0x00000001UL)) {
|
||||
cnt++;
|
||||
val >>= 1;
|
||||
}
|
||||
if (!(val & 0x00000001UL)) {
|
||||
cnt++;
|
||||
}
|
||||
|
||||
return cnt;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* cto32 - count trailing ones in a 32-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 32 if the value is -1.
|
||||
*/
|
||||
static inline int cto32(uint32_t val)
|
||||
{
|
||||
return ctz32(~val);
|
||||
}
|
||||
|
||||
/**
|
||||
* ctz64 - count trailing zeros in a 64-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 64 if the value is zero. Note that the GCC builtin is
|
||||
* undefined if the value is zero.
|
||||
*/
|
||||
static inline int ctz64(uint64_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return val ? __builtin_ctzll(val) : 64;
|
||||
#else
|
||||
int cnt;
|
||||
|
||||
cnt = 0;
|
||||
if (!((uint32_t)val)) {
|
||||
cnt += 32;
|
||||
val >>= 32;
|
||||
}
|
||||
|
||||
return cnt + ctz32(val);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* cto64 - count trailing ones in a 64-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns 64 if the value is -1.
|
||||
*/
|
||||
static inline int cto64(uint64_t val)
|
||||
{
|
||||
return ctz64(~val);
|
||||
}
|
||||
|
||||
/**
|
||||
* clrsb32 - count leading redundant sign bits in a 32-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns the number of bits following the sign bit that are equal to it.
|
||||
* No special cases; output range is [0-31].
|
||||
*/
|
||||
static inline int clrsb32(uint32_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(4, 7)
|
||||
return __builtin_clrsb(val);
|
||||
#else
|
||||
return clz32(val ^ ((int32_t)val >> 1)) - 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* clrsb64 - count leading redundant sign bits in a 64-bit value.
|
||||
* @val: The value to search
|
||||
*
|
||||
* Returns the number of bits following the sign bit that are equal to it.
|
||||
* No special cases; output range is [0-63].
|
||||
*/
|
||||
static inline int clrsb64(uint64_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(4, 7)
|
||||
return __builtin_clrsbll(val);
|
||||
#else
|
||||
return clz64(val ^ ((int64_t)val >> 1)) - 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* ctpop8 - count the population of one bits in an 8-bit value.
|
||||
* @val: The value to search
|
||||
*/
|
||||
static inline int ctpop8(uint8_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return __builtin_popcount(val);
|
||||
#else
|
||||
val = (val & 0x55) + ((val >> 1) & 0x55);
|
||||
val = (val & 0x33) + ((val >> 2) & 0x33);
|
||||
val = (val & 0x0f) + ((val >> 4) & 0x0f);
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* ctpop16 - count the population of one bits in a 16-bit value.
|
||||
* @val: The value to search
|
||||
*/
|
||||
static inline int ctpop16(uint16_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return __builtin_popcount(val);
|
||||
#else
|
||||
val = (val & 0x5555) + ((val >> 1) & 0x5555);
|
||||
val = (val & 0x3333) + ((val >> 2) & 0x3333);
|
||||
val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
|
||||
val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* ctpop32 - count the population of one bits in a 32-bit value.
|
||||
* @val: The value to search
|
||||
*/
|
||||
static inline int ctpop32(uint32_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return __builtin_popcount(val);
|
||||
#else
|
||||
val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
|
||||
val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
|
||||
val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
|
||||
val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
|
||||
val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* ctpop64 - count the population of one bits in a 64-bit value.
|
||||
* @val: The value to search
|
||||
*/
|
||||
static inline int ctpop64(uint64_t val)
|
||||
{
|
||||
#if QEMU_GNUC_PREREQ(3, 4)
|
||||
return __builtin_popcountll(val);
|
||||
#else
|
||||
val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
|
||||
val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
|
||||
val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
|
||||
val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
|
||||
val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
|
||||
val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Host type specific sizes of these routines. */
|
||||
|
||||
#if ULONG_MAX == UINT32_MAX
|
||||
# define clzl clz32
|
||||
# define ctzl ctz32
|
||||
# define clol clo32
|
||||
# define ctol cto32
|
||||
# define ctpopl ctpop32
|
||||
#elif ULONG_MAX == UINT64_MAX
|
||||
# define clzl clz64
|
||||
# define ctzl ctz64
|
||||
# define clol clo64
|
||||
# define ctol cto64
|
||||
# define ctpopl ctpop64
|
||||
#else
|
||||
# error Unknown sizeof long
|
||||
#endif
|
||||
|
||||
#endif
|
||||
149
qemu/include/qemu/int128.h
Normal file
149
qemu/include/qemu/int128.h
Normal file
@@ -0,0 +1,149 @@
|
||||
#ifndef INT128_H
|
||||
#define INT128_H
|
||||
|
||||
//#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct Int128 Int128;
|
||||
|
||||
struct Int128 {
|
||||
uint64_t lo;
|
||||
int64_t hi;
|
||||
};
|
||||
|
||||
static inline Int128 int128_make64(uint64_t a)
|
||||
{
|
||||
return (Int128) { a, 0 };
|
||||
}
|
||||
|
||||
static inline uint64_t int128_get64(Int128 a)
|
||||
{
|
||||
//assert(!a.hi);
|
||||
return a.lo;
|
||||
}
|
||||
|
||||
static inline Int128 int128_zero(void)
|
||||
{
|
||||
return int128_make64(0);
|
||||
}
|
||||
|
||||
static inline Int128 int128_one(void)
|
||||
{
|
||||
return int128_make64(1);
|
||||
}
|
||||
|
||||
static inline Int128 int128_2_64(void)
|
||||
{
|
||||
return (Int128) { 0, 1 };
|
||||
}
|
||||
|
||||
static inline Int128 int128_exts64(int64_t a)
|
||||
{
|
||||
return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
|
||||
}
|
||||
|
||||
static inline Int128 int128_and(Int128 a, Int128 b)
|
||||
{
|
||||
return (Int128) { a.lo & b.lo, a.hi & b.hi };
|
||||
}
|
||||
|
||||
static inline Int128 int128_rshift(Int128 a, int n)
|
||||
{
|
||||
int64_t h;
|
||||
if (!n) {
|
||||
return a;
|
||||
}
|
||||
h = a.hi >> (n & 63);
|
||||
if (n >= 64) {
|
||||
return (Int128) { h, h >> 63 };
|
||||
} else {
|
||||
return (Int128) { (a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h };
|
||||
}
|
||||
}
|
||||
|
||||
static inline Int128 int128_add(Int128 a, Int128 b)
|
||||
{
|
||||
uint64_t lo = a.lo + b.lo;
|
||||
|
||||
/* a.lo <= a.lo + b.lo < a.lo + k (k is the base, 2^64). Hence,
|
||||
* a.lo + b.lo >= k implies 0 <= lo = a.lo + b.lo - k < a.lo.
|
||||
* Similarly, a.lo + b.lo < k implies a.lo <= lo = a.lo + b.lo < k.
|
||||
*
|
||||
* So the carry is lo < a.lo.
|
||||
*/
|
||||
return (Int128) { lo, (uint64_t)a.hi + b.hi + (lo < a.lo) };
|
||||
}
|
||||
|
||||
static inline Int128 int128_neg(Int128 a)
|
||||
{
|
||||
uint64_t lo = -a.lo;
|
||||
return (Int128) { lo, ~(uint64_t)a.hi + !lo };
|
||||
}
|
||||
|
||||
static inline Int128 int128_sub(Int128 a, Int128 b)
|
||||
{
|
||||
return (Int128){ a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo) };
|
||||
}
|
||||
|
||||
static inline bool int128_nonneg(Int128 a)
|
||||
{
|
||||
return a.hi >= 0;
|
||||
}
|
||||
|
||||
static inline bool int128_eq(Int128 a, Int128 b)
|
||||
{
|
||||
return a.lo == b.lo && a.hi == b.hi;
|
||||
}
|
||||
|
||||
static inline bool int128_ne(Int128 a, Int128 b)
|
||||
{
|
||||
return !int128_eq(a, b);
|
||||
}
|
||||
|
||||
static inline bool int128_ge(Int128 a, Int128 b)
|
||||
{
|
||||
return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo);
|
||||
}
|
||||
|
||||
static inline bool int128_lt(Int128 a, Int128 b)
|
||||
{
|
||||
return !int128_ge(a, b);
|
||||
}
|
||||
|
||||
static inline bool int128_le(Int128 a, Int128 b)
|
||||
{
|
||||
return int128_ge(b, a);
|
||||
}
|
||||
|
||||
static inline bool int128_gt(Int128 a, Int128 b)
|
||||
{
|
||||
return !int128_le(a, b);
|
||||
}
|
||||
|
||||
static inline bool int128_nz(Int128 a)
|
||||
{
|
||||
return a.lo || a.hi;
|
||||
}
|
||||
|
||||
static inline Int128 int128_min(Int128 a, Int128 b)
|
||||
{
|
||||
return int128_le(a, b) ? a : b;
|
||||
}
|
||||
|
||||
static inline Int128 int128_max(Int128 a, Int128 b)
|
||||
{
|
||||
return int128_ge(a, b) ? a : b;
|
||||
}
|
||||
|
||||
static inline void int128_addto(Int128 *a, Int128 b)
|
||||
{
|
||||
*a = int128_add(*a, b);
|
||||
}
|
||||
|
||||
static inline void int128_subfrom(Int128 *a, Int128 b)
|
||||
{
|
||||
*a = int128_sub(*a, b);
|
||||
}
|
||||
|
||||
#endif
|
||||
118
qemu/include/qemu/log.h
Normal file
118
qemu/include/qemu/log.h
Normal file
@@ -0,0 +1,118 @@
|
||||
#ifndef QEMU_LOG_H
|
||||
#define QEMU_LOG_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "qemu/compiler.h"
|
||||
#include "qom/cpu.h"
|
||||
|
||||
/* Private global variables, don't use */
|
||||
extern FILE *qemu_logfile;
|
||||
extern int qemu_loglevel;
|
||||
|
||||
/*
|
||||
* The new API:
|
||||
*
|
||||
*/
|
||||
|
||||
/* Log settings checking macros: */
|
||||
|
||||
/* Returns true if qemu_log() will really write somewhere
|
||||
*/
|
||||
static inline bool qemu_log_enabled(void)
|
||||
{
|
||||
return qemu_logfile != NULL;
|
||||
}
|
||||
|
||||
#define CPU_LOG_TB_OUT_ASM (1 << 0)
|
||||
#define CPU_LOG_TB_IN_ASM (1 << 1)
|
||||
#define CPU_LOG_TB_OP (1 << 2)
|
||||
#define CPU_LOG_TB_OP_OPT (1 << 3)
|
||||
#define CPU_LOG_INT (1 << 4)
|
||||
#define CPU_LOG_EXEC (1 << 5)
|
||||
#define CPU_LOG_PCALL (1 << 6)
|
||||
#define CPU_LOG_IOPORT (1 << 7)
|
||||
#define CPU_LOG_TB_CPU (1 << 8)
|
||||
#define CPU_LOG_RESET (1 << 9)
|
||||
#define LOG_UNIMP (1 << 10)
|
||||
#define LOG_GUEST_ERROR (1 << 11)
|
||||
|
||||
/* Returns true if a bit is set in the current loglevel mask
|
||||
*/
|
||||
static inline bool qemu_loglevel_mask(int mask)
|
||||
{
|
||||
return (qemu_loglevel & mask) != 0;
|
||||
}
|
||||
|
||||
/* Logging functions: */
|
||||
|
||||
/* main logging function
|
||||
*/
|
||||
void GCC_FMT_ATTR(1, 2) qemu_log(const char *fmt, ...);
|
||||
|
||||
/* vfprintf-like logging function
|
||||
*/
|
||||
static inline void GCC_FMT_ATTR(1, 0)
|
||||
qemu_log_vprintf(const char *fmt, va_list va)
|
||||
{
|
||||
if (qemu_logfile) {
|
||||
vfprintf(qemu_logfile, fmt, va);
|
||||
}
|
||||
}
|
||||
|
||||
/* log only if a bit is set on the current loglevel mask
|
||||
*/
|
||||
void GCC_FMT_ATTR(2, 3) qemu_log_mask(int mask, const char *fmt, ...);
|
||||
|
||||
|
||||
/* Special cases: */
|
||||
|
||||
/* cpu_dump_state() logging functions: */
|
||||
/**
|
||||
* log_cpu_state:
|
||||
* @cpu: The CPU whose state is to be logged.
|
||||
* @flags: Flags what to log.
|
||||
*
|
||||
* Logs the output of cpu_dump_state().
|
||||
*/
|
||||
static inline void log_cpu_state(CPUState *cpu, int flags)
|
||||
{
|
||||
if (qemu_log_enabled()) {
|
||||
cpu_dump_state(cpu, qemu_logfile, fprintf, flags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* log_cpu_state_mask:
|
||||
* @mask: Mask when to log.
|
||||
* @cpu: The CPU whose state is to be logged.
|
||||
* @flags: Flags what to log.
|
||||
*
|
||||
* Logs the output of cpu_dump_state() if loglevel includes @mask.
|
||||
*/
|
||||
static inline void log_cpu_state_mask(int mask, CPUState *cpu, int flags)
|
||||
{
|
||||
if (qemu_loglevel & mask) {
|
||||
log_cpu_state(cpu, flags);
|
||||
}
|
||||
}
|
||||
|
||||
/* fflush() the log file */
|
||||
static inline void qemu_log_flush(void)
|
||||
{
|
||||
fflush(qemu_logfile);
|
||||
}
|
||||
|
||||
/* Close the log file */
|
||||
static inline void qemu_log_close(void)
|
||||
{
|
||||
if (qemu_logfile) {
|
||||
if (qemu_logfile != stderr) {
|
||||
fclose(qemu_logfile);
|
||||
}
|
||||
qemu_logfile = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
77
qemu/include/qemu/main-loop.h
Normal file
77
qemu/include/qemu/main-loop.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* QEMU System Emulator
|
||||
*
|
||||
* Copyright (c) 2003-2008 Fabrice Bellard
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef QEMU_MAIN_LOOP_H
|
||||
#define QEMU_MAIN_LOOP_H 1
|
||||
|
||||
#define SIG_IPI SIGUSR1
|
||||
|
||||
struct uc_struct;
|
||||
|
||||
/**
|
||||
* qemu_init_main_loop: Set up the process so that it can run the main loop.
|
||||
*
|
||||
* This includes setting up signal handlers. It should be called before
|
||||
* any other threads are created. In addition, threads other than the
|
||||
* main one should block signals that are trapped by the main loop.
|
||||
* For simplicity, you can consider these signals to be safe: SIGUSR1,
|
||||
* SIGUSR2, thread signals (SIGFPE, SIGILL, SIGSEGV, SIGBUS) and real-time
|
||||
* signals if available. Remember that Windows in practice does not have
|
||||
* signals, though.
|
||||
*
|
||||
* In the case of QEMU tools, this will also start/initialize timers.
|
||||
*/
|
||||
int qemu_init_main_loop(void);
|
||||
|
||||
/**
|
||||
* qemu_mutex_lock_iothread: Lock the main loop mutex.
|
||||
*
|
||||
* This function locks the main loop mutex. The mutex is taken by
|
||||
* qemu_init_main_loop and always taken except while waiting on
|
||||
* external events (such as with select). The mutex should be taken
|
||||
* by threads other than the main loop thread when calling
|
||||
* qemu_bh_new(), qemu_set_fd_handler() and basically all other
|
||||
* functions documented in this file.
|
||||
*
|
||||
* NOTE: tools currently are single-threaded and qemu_mutex_lock_iothread
|
||||
* is a no-op there.
|
||||
*/
|
||||
void qemu_mutex_lock_iothread(struct uc_struct* uc);
|
||||
|
||||
/**
|
||||
* qemu_mutex_unlock_iothread: Unlock the main loop mutex.
|
||||
*
|
||||
* This function unlocks the main loop mutex. The mutex is taken by
|
||||
* qemu_init_main_loop and always taken except while waiting on
|
||||
* external events (such as with select). The mutex should be unlocked
|
||||
* as soon as possible by threads other than the main loop thread,
|
||||
* because it prevents the main loop from processing callbacks,
|
||||
* including timers and bottom halves.
|
||||
*
|
||||
* NOTE: tools currently are single-threaded and qemu_mutex_unlock_iothread
|
||||
* is a no-op there.
|
||||
*/
|
||||
void qemu_mutex_unlock_iothread(struct uc_struct* uc);
|
||||
|
||||
#endif
|
||||
61
qemu/include/qemu/module.h
Normal file
61
qemu/include/qemu/module.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* QEMU Module Infrastructure
|
||||
*
|
||||
* Copyright IBM, Corp. 2009
|
||||
*
|
||||
* Authors:
|
||||
* Anthony Liguori <aliguori@us.ibm.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2. See
|
||||
* the COPYING file in the top-level directory.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QEMU_MODULE_H
|
||||
#define QEMU_MODULE_H
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
|
||||
#define DSO_STAMP_FUN glue(qemu_stamp, CONFIG_STAMP)
|
||||
#define DSO_STAMP_FUN_STR stringify(DSO_STAMP_FUN)
|
||||
|
||||
#ifdef BUILD_DSO
|
||||
void DSO_STAMP_FUN(void);
|
||||
/* This is a dummy symbol to identify a loaded DSO as a QEMU module, so we can
|
||||
* distinguish "version mismatch" from "not a QEMU module", when the stamp
|
||||
* check fails during module loading */
|
||||
void qemu_module_dummy(void);
|
||||
|
||||
#define module_init(function, type) \
|
||||
static void __attribute__((constructor)) do_qemu_init_ ## function(void) \
|
||||
{ \
|
||||
register_dso_module_init(function, type); \
|
||||
}
|
||||
#else
|
||||
/* This should not be used directly. Use block_init etc. instead. */
|
||||
#define module_init(function, type) \
|
||||
static void __attribute__((constructor)) do_qemu_init_ ## function(void) \
|
||||
{ \
|
||||
register_module_init(function, type); \
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
MODULE_INIT_BLOCK,
|
||||
MODULE_INIT_MACHINE,
|
||||
MODULE_INIT_QAPI,
|
||||
MODULE_INIT_QOM,
|
||||
MODULE_INIT_MAX
|
||||
} module_init_type;
|
||||
|
||||
#define block_init(function) module_init(function, MODULE_INIT_BLOCK)
|
||||
#define machine_init(function) module_init(function, MODULE_INIT_MACHINE)
|
||||
#define qapi_init(function) module_init(function, MODULE_INIT_QAPI)
|
||||
#define type_init(function) module_init(function, MODULE_INIT_QOM)
|
||||
|
||||
void register_module_init(void (*fn)(void), module_init_type type);
|
||||
void register_dso_module_init(void (*fn)(void), module_init_type type);
|
||||
|
||||
void module_call_init(struct uc_struct *uc, module_init_type type);
|
||||
|
||||
#endif
|
||||
72
qemu/include/qemu/notify.h
Normal file
72
qemu/include/qemu/notify.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Notifier lists
|
||||
*
|
||||
* Copyright IBM, Corp. 2010
|
||||
*
|
||||
* Authors:
|
||||
* Anthony Liguori <aliguori@us.ibm.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2. See
|
||||
* the COPYING file in the top-level directory.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QEMU_NOTIFY_H
|
||||
#define QEMU_NOTIFY_H
|
||||
|
||||
#include "qemu/queue.h"
|
||||
|
||||
typedef struct Notifier Notifier;
|
||||
|
||||
struct Notifier
|
||||
{
|
||||
void (*notify)(Notifier *notifier, void *data);
|
||||
QLIST_ENTRY(Notifier) node;
|
||||
};
|
||||
|
||||
typedef struct NotifierList
|
||||
{
|
||||
QLIST_HEAD(, Notifier) notifiers;
|
||||
} NotifierList;
|
||||
|
||||
#define NOTIFIER_LIST_INITIALIZER(head) \
|
||||
{ QLIST_HEAD_INITIALIZER((head).notifiers) }
|
||||
|
||||
void notifier_list_init(NotifierList *list);
|
||||
|
||||
void notifier_list_add(NotifierList *list, Notifier *notifier);
|
||||
|
||||
void notifier_remove(Notifier *notifier);
|
||||
|
||||
void notifier_list_notify(NotifierList *list, void *data);
|
||||
|
||||
/* Same as Notifier but allows .notify() to return errors */
|
||||
typedef struct NotifierWithReturn NotifierWithReturn;
|
||||
|
||||
struct NotifierWithReturn {
|
||||
/**
|
||||
* Return 0 on success (next notifier will be invoked), otherwise
|
||||
* notifier_with_return_list_notify() will stop and return the value.
|
||||
*/
|
||||
int (*notify)(NotifierWithReturn *notifier, void *data);
|
||||
QLIST_ENTRY(NotifierWithReturn) node;
|
||||
};
|
||||
|
||||
typedef struct NotifierWithReturnList {
|
||||
QLIST_HEAD(, NotifierWithReturn) notifiers;
|
||||
} NotifierWithReturnList;
|
||||
|
||||
#define NOTIFIER_WITH_RETURN_LIST_INITIALIZER(head) \
|
||||
{ QLIST_HEAD_INITIALIZER((head).notifiers) }
|
||||
|
||||
void notifier_with_return_list_init(NotifierWithReturnList *list);
|
||||
|
||||
void notifier_with_return_list_add(NotifierWithReturnList *list,
|
||||
NotifierWithReturn *notifier);
|
||||
|
||||
void notifier_with_return_remove(NotifierWithReturn *notifier);
|
||||
|
||||
int notifier_with_return_list_notify(NotifierWithReturnList *list,
|
||||
void *data);
|
||||
|
||||
#endif
|
||||
134
qemu/include/qemu/option.h
Normal file
134
qemu/include/qemu/option.h
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Commandline option parsing functions
|
||||
*
|
||||
* Copyright (c) 2003-2008 Fabrice Bellard
|
||||
* Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef QEMU_OPTIONS_H
|
||||
#define QEMU_OPTIONS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "qemu/queue.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qapi/qmp/qdict.h"
|
||||
|
||||
const char *get_opt_name(char *buf, int buf_size, const char *p, char delim);
|
||||
const char *get_opt_value(char *buf, int buf_size, const char *p);
|
||||
int get_next_param_value(char *buf, int buf_size,
|
||||
const char *tag, const char **pstr);
|
||||
int get_param_value(char *buf, int buf_size,
|
||||
const char *tag, const char *str);
|
||||
|
||||
|
||||
void parse_option_size(const char *name, const char *value,
|
||||
uint64_t *ret, Error **errp);
|
||||
bool has_help_option(const char *param);
|
||||
bool is_valid_option_list(const char *param);
|
||||
|
||||
typedef struct QemuOpt QemuOpt;
|
||||
typedef struct QemuOpts QemuOpts;
|
||||
typedef struct QemuOptsList QemuOptsList;
|
||||
|
||||
enum QemuOptType {
|
||||
QEMU_OPT_STRING = 0, /* no parsing (use string as-is) */
|
||||
QEMU_OPT_BOOL, /* on/off */
|
||||
QEMU_OPT_NUMBER, /* simple number */
|
||||
QEMU_OPT_SIZE, /* size, accepts (K)ilo, (M)ega, (G)iga, (T)era postfix */
|
||||
};
|
||||
|
||||
typedef struct QemuOptDesc {
|
||||
const char *name;
|
||||
enum QemuOptType type;
|
||||
const char *help;
|
||||
const char *def_value_str;
|
||||
} QemuOptDesc;
|
||||
|
||||
struct QemuOptsList {
|
||||
const char *name;
|
||||
const char *implied_opt_name;
|
||||
bool merge_lists; /* Merge multiple uses of option into a single list? */
|
||||
QTAILQ_HEAD(, QemuOpts) head;
|
||||
QemuOptDesc desc[];
|
||||
};
|
||||
|
||||
const char *qemu_opt_get(QemuOpts *opts, const char *name);
|
||||
char *qemu_opt_get_del(QemuOpts *opts, const char *name);
|
||||
/**
|
||||
* qemu_opt_has_help_opt:
|
||||
* @opts: options to search for a help request
|
||||
*
|
||||
* Check whether the options specified by @opts include one of the
|
||||
* standard strings which indicate that the user is asking for a
|
||||
* list of the valid values for a command line option (as defined
|
||||
* by is_help_option()).
|
||||
*
|
||||
* Returns: true if @opts includes 'help' or equivalent.
|
||||
*/
|
||||
bool qemu_opt_has_help_opt(QemuOpts *opts);
|
||||
QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name);
|
||||
bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval);
|
||||
uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval);
|
||||
uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval);
|
||||
bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval);
|
||||
uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
|
||||
uint64_t defval);
|
||||
uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
|
||||
uint64_t defval);
|
||||
int qemu_opt_unset(QemuOpts *opts, const char *name);
|
||||
int qemu_opt_set(QemuOpts *opts, const char *name, const char *value);
|
||||
void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
|
||||
Error **errp);
|
||||
int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val);
|
||||
int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val);
|
||||
typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaque);
|
||||
int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
|
||||
int abort_on_failure);
|
||||
|
||||
QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id);
|
||||
QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
|
||||
int fail_if_exists, Error **errp);
|
||||
void qemu_opts_reset(QemuOptsList *list);
|
||||
void qemu_opts_loc_restore(QemuOpts *opts);
|
||||
int qemu_opts_set(QemuOptsList *list, const char *id,
|
||||
const char *name, const char *value);
|
||||
const char *qemu_opts_id(QemuOpts *opts);
|
||||
void qemu_opts_set_id(QemuOpts *opts, char *id);
|
||||
void qemu_opts_del(QemuOpts *opts);
|
||||
void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp);
|
||||
int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
|
||||
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, int permit_abbrev);
|
||||
void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
|
||||
int permit_abbrev);
|
||||
QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
|
||||
Error **errp);
|
||||
QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict);
|
||||
void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp);
|
||||
|
||||
typedef int (*qemu_opts_loopfunc)(QemuOpts *opts, void *opaque);
|
||||
void qemu_opts_print(QemuOpts *opts);
|
||||
int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
|
||||
int abort_on_failure);
|
||||
void qemu_opts_print_help(QemuOptsList *list);
|
||||
void qemu_opts_free(QemuOptsList *list);
|
||||
QemuOptsList *qemu_opts_append(QemuOptsList *dst, QemuOptsList *list);
|
||||
|
||||
#endif
|
||||
52
qemu/include/qemu/option_int.h
Normal file
52
qemu/include/qemu/option_int.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Commandline option parsing functions
|
||||
*
|
||||
* Copyright (c) 2003-2008 Fabrice Bellard
|
||||
* Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef QEMU_OPTIONS_INTERNAL_H
|
||||
#define QEMU_OPTIONS_INTERNAL_H
|
||||
|
||||
#include "qemu/option.h"
|
||||
|
||||
struct QemuOpt {
|
||||
char *name;
|
||||
char *str;
|
||||
|
||||
const QemuOptDesc *desc;
|
||||
union {
|
||||
bool boolean;
|
||||
uint64_t uint;
|
||||
} value;
|
||||
|
||||
QemuOpts *opts;
|
||||
QTAILQ_ENTRY(QemuOpt) next;
|
||||
};
|
||||
|
||||
struct QemuOpts {
|
||||
char *id;
|
||||
QemuOptsList *list;
|
||||
QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
|
||||
QTAILQ_ENTRY(QemuOpts) next;
|
||||
};
|
||||
|
||||
#endif
|
||||
239
qemu/include/qemu/osdep.h
Normal file
239
qemu/include/qemu/osdep.h
Normal file
@@ -0,0 +1,239 @@
|
||||
#ifndef QEMU_OSDEP_H
|
||||
#define QEMU_OSDEP_H
|
||||
|
||||
#include "config-host.h"
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#ifdef __OpenBSD__
|
||||
#include <sys/signal.h>
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/wait.h>
|
||||
#else
|
||||
#define WIFEXITED(x) 1
|
||||
#define WEXITSTATUS(x) (x)
|
||||
#endif
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
|
||||
/* [u]int_fast*_t not in <sys/int_types.h> */
|
||||
typedef unsigned char uint_fast8_t;
|
||||
typedef unsigned int uint_fast16_t;
|
||||
typedef signed int int_fast16_t;
|
||||
#endif
|
||||
|
||||
#ifndef glue
|
||||
#define xglue(x, y) x ## y
|
||||
#define glue(x, y) xglue(x, y)
|
||||
#define stringify(s) tostring(s)
|
||||
#define tostring(s) #s
|
||||
#endif
|
||||
|
||||
#ifndef likely
|
||||
#if __GNUC__ < 3
|
||||
#define __builtin_expect(x, n) (x)
|
||||
#endif
|
||||
|
||||
#define likely(x) __builtin_expect(!!(x), 1)
|
||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
||||
#endif
|
||||
|
||||
#ifndef container_of
|
||||
#define container_of(ptr, type, member) ({ \
|
||||
const typeof(((type *) 0)->member) *__mptr = (ptr); \
|
||||
(type *) ((char *) __mptr - offsetof(type, member));})
|
||||
#endif
|
||||
|
||||
/* Convert from a base type to a parent type, with compile time checking. */
|
||||
#ifdef __GNUC__
|
||||
#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
|
||||
char __attribute__((unused)) offset_must_be_zero[ \
|
||||
-offsetof(type, field)]; \
|
||||
container_of(dev, type, field);}))
|
||||
#else
|
||||
#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
|
||||
#endif
|
||||
|
||||
#define typeof_field(type, field) typeof(((type *)0)->field)
|
||||
#define type_check(t1,t2) ((t1*)0 - (t2*)0)
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
/* Minimum function that returns zero only iff both values are zero.
|
||||
* Intended for use with unsigned values only. */
|
||||
#ifndef MIN_NON_ZERO
|
||||
#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef ROUND_UP
|
||||
#define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
|
||||
#endif
|
||||
|
||||
#ifndef DIV_ROUND_UP
|
||||
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||
#endif
|
||||
|
||||
#ifndef always_inline
|
||||
#if !((__GNUC__ < 3) || defined(__APPLE__))
|
||||
#ifdef __OPTIMIZE__
|
||||
#undef inline
|
||||
#define inline __attribute__ (( always_inline )) __inline__
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
#undef inline
|
||||
#define inline always_inline
|
||||
#endif
|
||||
|
||||
#define qemu_printf printf
|
||||
|
||||
int qemu_daemon(int nochdir, int noclose);
|
||||
void *qemu_try_memalign(size_t alignment, size_t size);
|
||||
void *qemu_memalign(size_t alignment, size_t size);
|
||||
void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
|
||||
void qemu_vfree(void *ptr);
|
||||
void qemu_anon_ram_free(void *ptr, size_t size);
|
||||
|
||||
#define QEMU_MADV_INVALID -1
|
||||
|
||||
#if defined(CONFIG_MADVISE)
|
||||
|
||||
#define QEMU_MADV_WILLNEED MADV_WILLNEED
|
||||
#define QEMU_MADV_DONTNEED MADV_DONTNEED
|
||||
#ifdef MADV_DONTFORK
|
||||
#define QEMU_MADV_DONTFORK MADV_DONTFORK
|
||||
#else
|
||||
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
||||
#endif
|
||||
#ifdef MADV_MERGEABLE
|
||||
#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
|
||||
#else
|
||||
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
||||
#endif
|
||||
#ifdef MADV_UNMERGEABLE
|
||||
#define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
|
||||
#else
|
||||
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
||||
#endif
|
||||
#ifdef MADV_DODUMP
|
||||
#define QEMU_MADV_DODUMP MADV_DODUMP
|
||||
#else
|
||||
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
||||
#endif
|
||||
#ifdef MADV_DONTDUMP
|
||||
#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
|
||||
#else
|
||||
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
||||
#endif
|
||||
#ifdef MADV_HUGEPAGE
|
||||
#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
|
||||
#else
|
||||
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
||||
#endif
|
||||
|
||||
#elif defined(CONFIG_POSIX_MADVISE)
|
||||
|
||||
#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
|
||||
#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
|
||||
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
||||
|
||||
#else /* no-op */
|
||||
|
||||
#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
||||
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
||||
|
||||
#endif
|
||||
|
||||
int qemu_madvise(void *addr, size_t len, int advice);
|
||||
|
||||
int qemu_open(const char *name, int flags, ...);
|
||||
int qemu_close(int fd);
|
||||
|
||||
#if defined(__HAIKU__) && defined(__i386__)
|
||||
#define FMT_pid "%ld"
|
||||
#elif defined(WIN64)
|
||||
#define FMT_pid "%" PRId64
|
||||
#else
|
||||
#define FMT_pid "%d"
|
||||
#endif
|
||||
|
||||
int qemu_create_pidfile(const char *filename);
|
||||
int qemu_get_thread_id(void);
|
||||
|
||||
#ifdef _WIN32
|
||||
static inline void qemu_timersub(const struct timeval *val1,
|
||||
const struct timeval *val2,
|
||||
struct timeval *res)
|
||||
{
|
||||
res->tv_sec = val1->tv_sec - val2->tv_sec;
|
||||
if (val1->tv_usec < val2->tv_usec) {
|
||||
res->tv_sec--;
|
||||
res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
|
||||
} else {
|
||||
res->tv_usec = val1->tv_usec - val2->tv_usec;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define qemu_timersub timersub
|
||||
#endif
|
||||
|
||||
void qemu_set_cloexec(int fd);
|
||||
|
||||
void qemu_set_version(const char *);
|
||||
const char *qemu_get_version(void);
|
||||
|
||||
void fips_set_state(bool requested);
|
||||
bool fips_get_state(void);
|
||||
|
||||
/* Return a dynamically allocated pathname denoting a file or directory that is
|
||||
* appropriate for storing local state.
|
||||
*
|
||||
* @relative_pathname need not start with a directory separator; one will be
|
||||
* added automatically.
|
||||
*
|
||||
* The caller is responsible for releasing the value returned with g_free()
|
||||
* after use.
|
||||
*/
|
||||
char *qemu_get_local_state_pathname(const char *relative_pathname);
|
||||
|
||||
/* Get the saved exec dir.
|
||||
* Caller needs to release the returned string by g_free() */
|
||||
char *qemu_get_exec_dir(void);
|
||||
|
||||
/**
|
||||
* qemu_getauxval:
|
||||
* @type: the auxiliary vector key to lookup
|
||||
*
|
||||
* Search the auxiliary vector for @type, returning the value
|
||||
* or 0 if @type is not present.
|
||||
*/
|
||||
unsigned long qemu_getauxval(unsigned long type);
|
||||
|
||||
void qemu_set_tty_echo(int fd, bool echo);
|
||||
|
||||
#endif
|
||||
414
qemu/include/qemu/queue.h
Normal file
414
qemu/include/qemu/queue.h
Normal file
@@ -0,0 +1,414 @@
|
||||
/* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
|
||||
|
||||
/*
|
||||
* QEMU version: Copy from netbsd, removed debug code, removed some of
|
||||
* the implementations. Left in singly-linked lists, lists, simple
|
||||
* queues, and tail queues.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 1991, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
||||
*/
|
||||
|
||||
#ifndef QEMU_SYS_QUEUE_H_
|
||||
#define QEMU_SYS_QUEUE_H_
|
||||
|
||||
/*
|
||||
* This file defines four types of data structures: singly-linked lists,
|
||||
* lists, simple queues, and tail queues.
|
||||
*
|
||||
* A singly-linked list is headed by a single forward pointer. The
|
||||
* elements are singly linked for minimum space and pointer manipulation
|
||||
* overhead at the expense of O(n) removal for arbitrary elements. New
|
||||
* elements can be added to the list after an existing element or at the
|
||||
* head of the list. Elements being removed from the head of the list
|
||||
* should use the explicit macro for this purpose for optimum
|
||||
* efficiency. A singly-linked list may only be traversed in the forward
|
||||
* direction. Singly-linked lists are ideal for applications with large
|
||||
* datasets and few or no removals or for implementing a LIFO queue.
|
||||
*
|
||||
* A list is headed by a single forward pointer (or an array of forward
|
||||
* pointers for a hash table header). The elements are doubly linked
|
||||
* so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before
|
||||
* or after an existing element or at the head of the list. A list
|
||||
* may only be traversed in the forward direction.
|
||||
*
|
||||
* A simple queue is headed by a pair of pointers, one the head of the
|
||||
* list and the other to the tail of the list. The elements are singly
|
||||
* linked to save space, so elements can only be removed from the
|
||||
* head of the list. New elements can be added to the list after
|
||||
* an existing element, at the head of the list, or at the end of the
|
||||
* list. A simple queue may only be traversed in the forward direction.
|
||||
*
|
||||
* A tail queue is headed by a pair of pointers, one to the head of the
|
||||
* list and the other to the tail of the list. The elements are doubly
|
||||
* linked so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before or
|
||||
* after an existing element, at the head of the list, or at the end of
|
||||
* the list. A tail queue may be traversed in either direction.
|
||||
*
|
||||
* For details on the use of these macros, see the queue(3) manual page.
|
||||
*/
|
||||
|
||||
#include "qemu/atomic.h" /* for smp_wmb() */
|
||||
|
||||
/*
|
||||
* List definitions.
|
||||
*/
|
||||
#define QLIST_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *lh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define QLIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
#define QLIST_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *le_next; /* next element */ \
|
||||
struct type **le_prev; /* address of previous next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* List functions.
|
||||
*/
|
||||
#define QLIST_INIT(head) do { \
|
||||
(head)->lh_first = NULL; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QLIST_INSERT_AFTER(listelm, elm, field) do { \
|
||||
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
|
||||
(listelm)->field.le_next->field.le_prev = \
|
||||
&(elm)->field.le_next; \
|
||||
(listelm)->field.le_next = (elm); \
|
||||
(elm)->field.le_prev = &(listelm)->field.le_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
(elm)->field.le_prev = (listelm)->field.le_prev; \
|
||||
(elm)->field.le_next = (listelm); \
|
||||
*(listelm)->field.le_prev = (elm); \
|
||||
(listelm)->field.le_prev = &(elm)->field.le_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QLIST_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
|
||||
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
|
||||
(head)->lh_first = (elm); \
|
||||
(elm)->field.le_prev = &(head)->lh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \
|
||||
(elm)->field.le_prev = &(head)->lh_first; \
|
||||
(elm)->field.le_next = (head)->lh_first; \
|
||||
smp_wmb(); /* fill elm before linking it */ \
|
||||
if ((head)->lh_first != NULL) { \
|
||||
(head)->lh_first->field.le_prev = &(elm)->field.le_next; \
|
||||
} \
|
||||
(head)->lh_first = (elm); \
|
||||
smp_wmb(); \
|
||||
} while (/* CONSTCOND*/0)
|
||||
|
||||
#define QLIST_REMOVE(elm, field) do { \
|
||||
if ((elm)->field.le_next != NULL) \
|
||||
(elm)->field.le_next->field.le_prev = \
|
||||
(elm)->field.le_prev; \
|
||||
*(elm)->field.le_prev = (elm)->field.le_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QLIST_FOREACH(var, head, field) \
|
||||
for ((var) = ((head)->lh_first); \
|
||||
(var); \
|
||||
(var) = ((var)->field.le_next))
|
||||
|
||||
#define QLIST_FOREACH_SAFE(var, head, field, next_var) \
|
||||
for ((var) = ((head)->lh_first); \
|
||||
(var) && ((next_var) = ((var)->field.le_next), 1); \
|
||||
(var) = (next_var))
|
||||
|
||||
/*
|
||||
* List access methods.
|
||||
*/
|
||||
#define QLIST_EMPTY(head) ((head)->lh_first == NULL)
|
||||
#define QLIST_FIRST(head) ((head)->lh_first)
|
||||
#define QLIST_NEXT(elm, field) ((elm)->field.le_next)
|
||||
|
||||
|
||||
/*
|
||||
* Singly-linked List definitions.
|
||||
*/
|
||||
#define QSLIST_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *slh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define QSLIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
#define QSLIST_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *sle_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Singly-linked List functions.
|
||||
*/
|
||||
#define QSLIST_INIT(head) do { \
|
||||
(head)->slh_first = NULL; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSLIST_INSERT_AFTER(slistelm, elm, field) do { \
|
||||
(elm)->field.sle_next = (slistelm)->field.sle_next; \
|
||||
(slistelm)->field.sle_next = (elm); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSLIST_INSERT_HEAD(head, elm, field) do { \
|
||||
(elm)->field.sle_next = (head)->slh_first; \
|
||||
(head)->slh_first = (elm); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSLIST_REMOVE_HEAD(head, field) do { \
|
||||
(head)->slh_first = (head)->slh_first->field.sle_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSLIST_REMOVE_AFTER(slistelm, field) do { \
|
||||
(slistelm)->field.sle_next = \
|
||||
QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSLIST_FOREACH(var, head, field) \
|
||||
for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
|
||||
|
||||
#define QSLIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = QSLIST_FIRST((head)); \
|
||||
(var) && ((tvar) = QSLIST_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
/*
|
||||
* Singly-linked List access methods.
|
||||
*/
|
||||
#define QSLIST_EMPTY(head) ((head)->slh_first == NULL)
|
||||
#define QSLIST_FIRST(head) ((head)->slh_first)
|
||||
#define QSLIST_NEXT(elm, field) ((elm)->field.sle_next)
|
||||
|
||||
|
||||
/*
|
||||
* Simple queue definitions.
|
||||
*/
|
||||
#define QSIMPLEQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *sqh_first; /* first element */ \
|
||||
struct type **sqh_last; /* addr of last next element */ \
|
||||
}
|
||||
|
||||
#define QSIMPLEQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).sqh_first }
|
||||
|
||||
#define QSIMPLEQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *sqe_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Simple queue functions.
|
||||
*/
|
||||
#define QSIMPLEQ_INIT(head) do { \
|
||||
(head)->sqh_first = NULL; \
|
||||
(head)->sqh_last = &(head)->sqh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
|
||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||
(head)->sqh_first = (elm); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
|
||||
(elm)->field.sqe_next = NULL; \
|
||||
*(head)->sqh_last = (elm); \
|
||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
|
||||
(head)->sqh_last = &(elm)->field.sqe_next; \
|
||||
(listelm)->field.sqe_next = (elm); \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
|
||||
if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
|
||||
(head)->sqh_last = &(head)->sqh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
|
||||
if ((head)->sqh_first == (elm)) { \
|
||||
QSIMPLEQ_REMOVE_HEAD((head), field); \
|
||||
} else { \
|
||||
struct type *curelm = (head)->sqh_first; \
|
||||
while (curelm->field.sqe_next != (elm)) \
|
||||
curelm = curelm->field.sqe_next; \
|
||||
if ((curelm->field.sqe_next = \
|
||||
curelm->field.sqe_next->field.sqe_next) == NULL) \
|
||||
(head)->sqh_last = &(curelm)->field.sqe_next; \
|
||||
} \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_FOREACH(var, head, field) \
|
||||
for ((var) = ((head)->sqh_first); \
|
||||
(var); \
|
||||
(var) = ((var)->field.sqe_next))
|
||||
|
||||
#define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
|
||||
for ((var) = ((head)->sqh_first); \
|
||||
(var) && ((next = ((var)->field.sqe_next)), 1); \
|
||||
(var) = (next))
|
||||
|
||||
#define QSIMPLEQ_CONCAT(head1, head2) do { \
|
||||
if (!QSIMPLEQ_EMPTY((head2))) { \
|
||||
*(head1)->sqh_last = (head2)->sqh_first; \
|
||||
(head1)->sqh_last = (head2)->sqh_last; \
|
||||
QSIMPLEQ_INIT((head2)); \
|
||||
} \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QSIMPLEQ_LAST(head, type, field) \
|
||||
(QSIMPLEQ_EMPTY((head)) ? \
|
||||
NULL : \
|
||||
((struct type *)(void *) \
|
||||
((char *)((head)->sqh_last) - offsetof(struct type, field))))
|
||||
|
||||
/*
|
||||
* Simple queue access methods.
|
||||
*/
|
||||
#define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
|
||||
#define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
|
||||
#define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
|
||||
|
||||
|
||||
/*
|
||||
* Tail queue definitions.
|
||||
*/
|
||||
#define Q_TAILQ_HEAD(name, type, qual) \
|
||||
struct name { \
|
||||
qual type *tqh_first; /* first element */ \
|
||||
qual type *qual *tqh_last; /* addr of last next element */ \
|
||||
}
|
||||
#define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,)
|
||||
|
||||
#define QTAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).tqh_first }
|
||||
|
||||
#define Q_TAILQ_ENTRY(type, qual) \
|
||||
struct { \
|
||||
qual type *tqe_next; /* next element */ \
|
||||
qual type *qual *tqe_prev; /* address of previous next element */\
|
||||
}
|
||||
#define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,)
|
||||
|
||||
/*
|
||||
* Tail queue functions.
|
||||
*/
|
||||
#define QTAILQ_INIT(head) do { \
|
||||
(head)->tqh_first = NULL; \
|
||||
(head)->tqh_last = &(head)->tqh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QTAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
|
||||
(head)->tqh_first->field.tqe_prev = \
|
||||
&(elm)->field.tqe_next; \
|
||||
else \
|
||||
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||
(head)->tqh_first = (elm); \
|
||||
(elm)->field.tqe_prev = &(head)->tqh_first; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QTAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||
(elm)->field.tqe_next = NULL; \
|
||||
(elm)->field.tqe_prev = (head)->tqh_last; \
|
||||
*(head)->tqh_last = (elm); \
|
||||
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
|
||||
(elm)->field.tqe_next->field.tqe_prev = \
|
||||
&(elm)->field.tqe_next; \
|
||||
else \
|
||||
(head)->tqh_last = &(elm)->field.tqe_next; \
|
||||
(listelm)->field.tqe_next = (elm); \
|
||||
(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
||||
(elm)->field.tqe_next = (listelm); \
|
||||
*(listelm)->field.tqe_prev = (elm); \
|
||||
(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QTAILQ_REMOVE(head, elm, field) do { \
|
||||
if (((elm)->field.tqe_next) != NULL) \
|
||||
(elm)->field.tqe_next->field.tqe_prev = \
|
||||
(elm)->field.tqe_prev; \
|
||||
else \
|
||||
(head)->tqh_last = (elm)->field.tqe_prev; \
|
||||
*(elm)->field.tqe_prev = (elm)->field.tqe_next; \
|
||||
} while (/*CONSTCOND*/0)
|
||||
|
||||
#define QTAILQ_FOREACH(var, head, field) \
|
||||
for ((var) = ((head)->tqh_first); \
|
||||
(var); \
|
||||
(var) = ((var)->field.tqe_next))
|
||||
|
||||
#define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
|
||||
for ((var) = ((head)->tqh_first); \
|
||||
(var) && ((next_var) = ((var)->field.tqe_next), 1); \
|
||||
(var) = (next_var))
|
||||
|
||||
#define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||
for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
|
||||
(var); \
|
||||
(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
|
||||
|
||||
/*
|
||||
* Tail queue access methods.
|
||||
*/
|
||||
#define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
|
||||
#define QTAILQ_FIRST(head) ((head)->tqh_first)
|
||||
#define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||
|
||||
#define QTAILQ_LAST(head, headname) \
|
||||
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
||||
#define QTAILQ_PREV(elm, headname, field) \
|
||||
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
||||
|
||||
#endif /* !QEMU_SYS_QUEUE_H_ */
|
||||
135
qemu/include/qemu/range.h
Normal file
135
qemu/include/qemu/range.h
Normal file
@@ -0,0 +1,135 @@
|
||||
#ifndef QEMU_RANGE_H
|
||||
#define QEMU_RANGE_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <qemu/typedefs.h>
|
||||
#include "qemu/queue.h"
|
||||
|
||||
/*
|
||||
* Operations on 64 bit address ranges.
|
||||
* Notes:
|
||||
* - ranges must not wrap around 0, but can include the last byte ~0x0LL.
|
||||
* - this can not represent a full 0 to ~0x0LL range.
|
||||
*/
|
||||
|
||||
/* A structure representing a range of addresses. */
|
||||
struct Range {
|
||||
uint64_t begin; /* First byte of the range, or 0 if empty. */
|
||||
uint64_t end; /* 1 + the last byte. 0 if range empty or ends at ~0x0LL. */
|
||||
};
|
||||
|
||||
static inline void range_extend(Range *range, Range *extend_by)
|
||||
{
|
||||
if (!extend_by->begin && !extend_by->end) {
|
||||
return;
|
||||
}
|
||||
if (!range->begin && !range->end) {
|
||||
*range = *extend_by;
|
||||
return;
|
||||
}
|
||||
if (range->begin > extend_by->begin) {
|
||||
range->begin = extend_by->begin;
|
||||
}
|
||||
/* Compare last byte in case region ends at ~0x0LL */
|
||||
if (range->end - 1 < extend_by->end - 1) {
|
||||
range->end = extend_by->end;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get last byte of a range from offset + length.
|
||||
* Undefined for ranges that wrap around 0. */
|
||||
static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
|
||||
{
|
||||
return offset + len - 1;
|
||||
}
|
||||
|
||||
/* Check whether a given range covers a given byte. */
|
||||
static inline int range_covers_byte(uint64_t offset, uint64_t len,
|
||||
uint64_t byte)
|
||||
{
|
||||
return offset <= byte && byte <= range_get_last(offset, len);
|
||||
}
|
||||
|
||||
/* Check whether 2 given ranges overlap.
|
||||
* Undefined if ranges that wrap around 0. */
|
||||
static inline int ranges_overlap(uint64_t first1, uint64_t len1,
|
||||
uint64_t first2, uint64_t len2)
|
||||
{
|
||||
uint64_t last1 = range_get_last(first1, len1);
|
||||
uint64_t last2 = range_get_last(first2, len2);
|
||||
|
||||
return !(last2 < first1 || last1 < first2);
|
||||
}
|
||||
|
||||
/* 0,1 can merge with 1,2 but don't overlap */
|
||||
static inline bool ranges_can_merge(Range *range1, Range *range2)
|
||||
{
|
||||
return !(range1->end < range2->begin || range2->end < range1->begin);
|
||||
}
|
||||
|
||||
static inline int range_merge(Range *range1, Range *range2)
|
||||
{
|
||||
if (ranges_can_merge(range1, range2)) {
|
||||
if (range1->end < range2->end) {
|
||||
range1->end = range2->end;
|
||||
}
|
||||
if (range1->begin > range2->begin) {
|
||||
range1->begin = range2->begin;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline GList *g_list_insert_sorted_merged(GList *list,
|
||||
gpointer data,
|
||||
GCompareFunc func)
|
||||
{
|
||||
GList *l, *next = NULL;
|
||||
Range *r, *nextr;
|
||||
|
||||
if (!list) {
|
||||
list = g_list_insert_sorted(list, data, func);
|
||||
return list;
|
||||
}
|
||||
|
||||
nextr = data;
|
||||
l = list;
|
||||
while (l && l != next && nextr) {
|
||||
r = l->data;
|
||||
if (ranges_can_merge(r, nextr)) {
|
||||
range_merge(r, nextr);
|
||||
l = g_list_remove_link(l, next);
|
||||
next = g_list_next(l);
|
||||
if (next) {
|
||||
nextr = next->data;
|
||||
} else {
|
||||
nextr = NULL;
|
||||
}
|
||||
} else {
|
||||
l = g_list_next(l);
|
||||
}
|
||||
}
|
||||
|
||||
if (!l) {
|
||||
list = g_list_insert_sorted(list, data, func);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
static inline gint range_compare(gconstpointer a, gconstpointer b)
|
||||
{
|
||||
Range *ra = (Range *)a, *rb = (Range *)b;
|
||||
if (ra->begin == rb->begin && ra->end == rb->end) {
|
||||
return 0;
|
||||
} else if (range_get_last(ra->begin, ra->end) <
|
||||
range_get_last(rb->begin, rb->end)) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
65
qemu/include/qemu/seqlock.h
Normal file
65
qemu/include/qemu/seqlock.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Seqlock implementation for QEMU
|
||||
*
|
||||
* Copyright Red Hat, Inc. 2013
|
||||
*
|
||||
* 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_SEQLOCK_H
|
||||
#define QEMU_SEQLOCK_H 1
|
||||
|
||||
#include <qemu/atomic.h>
|
||||
#include <qemu/thread.h>
|
||||
|
||||
typedef struct QemuSeqLock QemuSeqLock;
|
||||
|
||||
struct QemuSeqLock {
|
||||
QemuMutex *mutex;
|
||||
unsigned sequence;
|
||||
};
|
||||
|
||||
static inline void seqlock_init(QemuSeqLock *sl, QemuMutex *mutex)
|
||||
{
|
||||
sl->mutex = mutex;
|
||||
sl->sequence = 0;
|
||||
}
|
||||
|
||||
/* Lock out other writers and update the count. */
|
||||
static inline void seqlock_write_lock(QemuSeqLock *sl)
|
||||
{
|
||||
if (sl->mutex) {
|
||||
qemu_mutex_lock(sl->mutex);
|
||||
}
|
||||
++sl->sequence;
|
||||
|
||||
/* Write sequence before updating other fields. */
|
||||
smp_wmb();
|
||||
}
|
||||
|
||||
static inline void seqlock_write_unlock(QemuSeqLock *sl)
|
||||
{
|
||||
/* Write other fields before finalizing sequence. */
|
||||
smp_wmb();
|
||||
|
||||
++sl->sequence;
|
||||
if (sl->mutex) {
|
||||
qemu_mutex_unlock(sl->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static inline unsigned seqlock_read_begin(QemuSeqLock *sl)
|
||||
{
|
||||
/* Always fail if a write is in progress. */
|
||||
unsigned ret = sl->sequence & ~1;
|
||||
|
||||
/* Read sequence before reading other fields. */
|
||||
smp_rmb();
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
36
qemu/include/qemu/thread-posix.h
Normal file
36
qemu/include/qemu/thread-posix.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef __QEMU_THREAD_POSIX_H
|
||||
#define __QEMU_THREAD_POSIX_H 1
|
||||
#include "pthread.h"
|
||||
#include <semaphore.h>
|
||||
|
||||
struct QemuMutex {
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
struct QemuCond {
|
||||
pthread_cond_t cond;
|
||||
};
|
||||
|
||||
struct QemuSemaphore {
|
||||
#if defined(__APPLE__) || defined(__NetBSD__)
|
||||
pthread_mutex_t lock;
|
||||
pthread_cond_t cond;
|
||||
unsigned int count;
|
||||
#else
|
||||
sem_t sem;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct QemuEvent {
|
||||
#ifndef __linux__
|
||||
pthread_mutex_t lock;
|
||||
pthread_cond_t cond;
|
||||
#endif
|
||||
unsigned value;
|
||||
};
|
||||
|
||||
struct QemuThread {
|
||||
pthread_t thread;
|
||||
};
|
||||
|
||||
#endif
|
||||
33
qemu/include/qemu/thread-win32.h
Normal file
33
qemu/include/qemu/thread-win32.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef __QEMU_THREAD_WIN32_H
|
||||
#define __QEMU_THREAD_WIN32_H 1
|
||||
#include "windows.h"
|
||||
|
||||
struct QemuMutex {
|
||||
CRITICAL_SECTION lock;
|
||||
LONG owner;
|
||||
};
|
||||
|
||||
struct QemuCond {
|
||||
LONG waiters, target;
|
||||
HANDLE sema;
|
||||
HANDLE continue_event;
|
||||
};
|
||||
|
||||
struct QemuSemaphore {
|
||||
HANDLE sema;
|
||||
};
|
||||
|
||||
struct QemuEvent {
|
||||
HANDLE event;
|
||||
};
|
||||
|
||||
typedef struct QemuThreadData QemuThreadData;
|
||||
struct QemuThread {
|
||||
QemuThreadData *data;
|
||||
unsigned tid;
|
||||
};
|
||||
|
||||
/* Only valid for joinable threads. */
|
||||
HANDLE qemu_thread_get_handle(QemuThread *thread);
|
||||
|
||||
#endif
|
||||
63
qemu/include/qemu/thread.h
Normal file
63
qemu/include/qemu/thread.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef __QEMU_THREAD_H
|
||||
#define __QEMU_THREAD_H 1
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct QemuMutex QemuMutex;
|
||||
typedef struct QemuCond QemuCond;
|
||||
typedef struct QemuSemaphore QemuSemaphore;
|
||||
typedef struct QemuEvent QemuEvent;
|
||||
typedef struct QemuThread QemuThread;
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "qemu/thread-win32.h"
|
||||
#else
|
||||
#include "qemu/thread-posix.h"
|
||||
#endif
|
||||
|
||||
#define QEMU_THREAD_JOINABLE 0
|
||||
#define QEMU_THREAD_DETACHED 1
|
||||
|
||||
void qemu_mutex_init(QemuMutex *mutex);
|
||||
void qemu_mutex_destroy(QemuMutex *mutex);
|
||||
void qemu_mutex_lock(QemuMutex *mutex);
|
||||
int qemu_mutex_trylock(QemuMutex *mutex);
|
||||
void qemu_mutex_unlock(QemuMutex *mutex);
|
||||
|
||||
#define rcu_read_lock() do { } while (0)
|
||||
#define rcu_read_unlock() do { } while (0)
|
||||
|
||||
void qemu_cond_init(QemuCond *cond);
|
||||
void qemu_cond_destroy(QemuCond *cond);
|
||||
|
||||
/*
|
||||
* IMPORTANT: The implementation does not guarantee that pthread_cond_signal
|
||||
* and pthread_cond_broadcast can be called except while the same mutex is
|
||||
* held as in the corresponding pthread_cond_wait calls!
|
||||
*/
|
||||
void qemu_cond_signal(QemuCond *cond);
|
||||
void qemu_cond_broadcast(QemuCond *cond);
|
||||
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
|
||||
|
||||
void qemu_sem_init(QemuSemaphore *sem, int init);
|
||||
void qemu_sem_post(QemuSemaphore *sem);
|
||||
void qemu_sem_wait(QemuSemaphore *sem);
|
||||
int qemu_sem_timedwait(QemuSemaphore *sem, int ms);
|
||||
void qemu_sem_destroy(QemuSemaphore *sem);
|
||||
|
||||
void qemu_event_init(QemuEvent *ev, bool init);
|
||||
void qemu_event_set(QemuEvent *ev);
|
||||
void qemu_event_reset(QemuEvent *ev);
|
||||
void qemu_event_wait(QemuEvent *ev);
|
||||
void qemu_event_destroy(QemuEvent *ev);
|
||||
|
||||
void qemu_thread_create(QemuThread *thread, const char *name,
|
||||
void *(*start_routine)(void *),
|
||||
void *arg, int mode);
|
||||
void *qemu_thread_join(QemuThread *thread);
|
||||
void qemu_thread_get_self(QemuThread *thread);
|
||||
bool qemu_thread_is_self(QemuThread *thread);
|
||||
void qemu_thread_exit(void *retval);
|
||||
|
||||
#endif
|
||||
836
qemu/include/qemu/timer.h
Normal file
836
qemu/include/qemu/timer.h
Normal file
@@ -0,0 +1,836 @@
|
||||
#ifndef QEMU_TIMER_H
|
||||
#define QEMU_TIMER_H
|
||||
|
||||
#include "qemu/typedefs.h"
|
||||
#include "qemu-common.h"
|
||||
#include "qemu/notify.h"
|
||||
|
||||
/* timers */
|
||||
|
||||
#define SCALE_MS 1000000
|
||||
#define SCALE_US 1000
|
||||
#define SCALE_NS 1
|
||||
|
||||
/**
|
||||
* QEMUClockType:
|
||||
*
|
||||
* The following clock types are available:
|
||||
*
|
||||
* @QEMU_CLOCK_REALTIME: Real time clock
|
||||
*
|
||||
* The real time clock should be used only for stuff which does not
|
||||
* change the virtual machine state, as it is run even if the virtual
|
||||
* machine is stopped. The real time clock has a frequency of 1000
|
||||
* Hz.
|
||||
*
|
||||
* @QEMU_CLOCK_VIRTUAL: virtual clock
|
||||
*
|
||||
* The virtual clock is only run during the emulation. It is stopped
|
||||
* when the virtual machine is stopped. Virtual timers use a high
|
||||
* precision clock, usually cpu cycles (use ticks_per_sec).
|
||||
*
|
||||
* @QEMU_CLOCK_HOST: host clock
|
||||
*
|
||||
* The host clock should be use for device models that emulate accurate
|
||||
* real time sources. It will continue to run when the virtual machine
|
||||
* is suspended, and it will reflect system time changes the host may
|
||||
* undergo (e.g. due to NTP). The host clock has the same precision as
|
||||
* the virtual clock.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
QEMU_CLOCK_REALTIME = 0,
|
||||
QEMU_CLOCK_VIRTUAL = 1,
|
||||
QEMU_CLOCK_HOST = 2,
|
||||
QEMU_CLOCK_MAX
|
||||
} QEMUClockType;
|
||||
|
||||
typedef struct QEMUTimerList QEMUTimerList;
|
||||
|
||||
struct QEMUTimerListGroup {
|
||||
QEMUTimerList *tl[QEMU_CLOCK_MAX];
|
||||
};
|
||||
|
||||
typedef void QEMUTimerCB(void *opaque);
|
||||
typedef void QEMUTimerListNotifyCB(void *opaque);
|
||||
|
||||
struct QEMUTimer {
|
||||
int64_t expire_time; /* in nanoseconds */
|
||||
QEMUTimerList *timer_list;
|
||||
QEMUTimerCB *cb;
|
||||
void *opaque;
|
||||
QEMUTimer *next;
|
||||
int scale;
|
||||
};
|
||||
|
||||
/*
|
||||
* QEMUClockType
|
||||
*/
|
||||
|
||||
/*
|
||||
* qemu_clock_get_ns;
|
||||
* @type: the clock type
|
||||
*
|
||||
* Get the nanosecond value of a clock with
|
||||
* type @type
|
||||
*
|
||||
* Returns: the clock value in nanoseconds
|
||||
*/
|
||||
int64_t qemu_clock_get_ns(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_get_ms;
|
||||
* @type: the clock type
|
||||
*
|
||||
* Get the millisecond value of a clock with
|
||||
* type @type
|
||||
*
|
||||
* Returns: the clock value in milliseconds
|
||||
*/
|
||||
static inline int64_t qemu_clock_get_ms(QEMUClockType type)
|
||||
{
|
||||
return qemu_clock_get_ns(type) / SCALE_MS;
|
||||
}
|
||||
|
||||
/**
|
||||
* qemu_clock_get_us;
|
||||
* @type: the clock type
|
||||
*
|
||||
* Get the microsecond value of a clock with
|
||||
* type @type
|
||||
*
|
||||
* Returns: the clock value in microseconds
|
||||
*/
|
||||
static inline int64_t qemu_clock_get_us(QEMUClockType type)
|
||||
{
|
||||
return qemu_clock_get_ns(type) / SCALE_US;
|
||||
}
|
||||
|
||||
/**
|
||||
* qemu_clock_has_timers:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Determines whether a clock's default timer list
|
||||
* has timers attached
|
||||
*
|
||||
* Note that this function should not be used when other threads also access
|
||||
* the timer list. The return value may be outdated by the time it is acted
|
||||
* upon.
|
||||
*
|
||||
* Returns: true if the clock's default timer list
|
||||
* has timers attached
|
||||
*/
|
||||
bool qemu_clock_has_timers(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_expired:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Determines whether a clock's default timer list
|
||||
* has an expired clock.
|
||||
*
|
||||
* Returns: true if the clock's default timer list has
|
||||
* an expired timer
|
||||
*/
|
||||
bool qemu_clock_expired(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_use_for_deadline:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Determine whether a clock should be used for deadline
|
||||
* calculations. Some clocks, for instance vm_clock with
|
||||
* use_icount set, do not count in nanoseconds. Such clocks
|
||||
* are not used for deadline calculations, and are presumed
|
||||
* to interrupt any poll using qemu_notify/aio_notify
|
||||
* etc.
|
||||
*
|
||||
* Returns: true if the clock runs in nanoseconds and
|
||||
* should be used for a deadline.
|
||||
*/
|
||||
bool qemu_clock_use_for_deadline(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_deadline_ns_all:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Calculate the deadline across all timer lists associated
|
||||
* with a clock (as opposed to just the default one)
|
||||
* in nanoseconds, or -1 if no timer is set to expire.
|
||||
*
|
||||
* Returns: time until expiry in nanoseconds or -1
|
||||
*/
|
||||
int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_get_main_loop_timerlist:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Return the default timer list assocatiated with a clock.
|
||||
*
|
||||
* Returns: the default timer list
|
||||
*/
|
||||
QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_nofify:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Call the notifier callback connected with the default timer
|
||||
* list linked to the clock, or qemu_notify() if none.
|
||||
*/
|
||||
void qemu_clock_notify(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_enable:
|
||||
* @type: the clock type
|
||||
* @enabled: true to enable, false to disable
|
||||
*
|
||||
* Enable or disable a clock
|
||||
* Disabling the clock will wait for related timerlists to stop
|
||||
* executing qemu_run_timers. Thus, this functions should not
|
||||
* be used from the callback of a timer that is based on @clock.
|
||||
* Doing so would cause a deadlock.
|
||||
*
|
||||
* Caller should hold BQL.
|
||||
*/
|
||||
void qemu_clock_enable(QEMUClockType type, bool enabled);
|
||||
|
||||
/**
|
||||
* qemu_clock_warp:
|
||||
* @type: the clock type
|
||||
*
|
||||
* Warp a clock to a new value
|
||||
*/
|
||||
void qemu_clock_warp(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_register_reset_notifier:
|
||||
* @type: the clock type
|
||||
* @notifier: the notifier function
|
||||
*
|
||||
* Register a notifier function to call when the clock
|
||||
* concerned is reset.
|
||||
*/
|
||||
void qemu_clock_register_reset_notifier(QEMUClockType type,
|
||||
Notifier *notifier);
|
||||
|
||||
/**
|
||||
* qemu_clock_unregister_reset_notifier:
|
||||
* @type: the clock type
|
||||
* @notifier: the notifier function
|
||||
*
|
||||
* Unregister a notifier function to call when the clock
|
||||
* concerned is reset.
|
||||
*/
|
||||
void qemu_clock_unregister_reset_notifier(QEMUClockType type,
|
||||
Notifier *notifier);
|
||||
|
||||
/**
|
||||
* qemu_clock_run_timers:
|
||||
* @type: clock on which to operate
|
||||
*
|
||||
* Run all the timers associated with the default timer list
|
||||
* of a clock.
|
||||
*
|
||||
* Returns: true if any timer ran.
|
||||
*/
|
||||
bool qemu_clock_run_timers(QEMUClockType type);
|
||||
|
||||
/**
|
||||
* qemu_clock_run_all_timers:
|
||||
*
|
||||
* Run all the timers associated with the default timer list
|
||||
* of every clock.
|
||||
*
|
||||
* Returns: true if any timer ran.
|
||||
*/
|
||||
bool qemu_clock_run_all_timers(void);
|
||||
|
||||
/*
|
||||
* QEMUTimerList
|
||||
*/
|
||||
|
||||
/**
|
||||
* timerlist_new:
|
||||
* @type: the clock type to associate with the timerlist
|
||||
* @cb: the callback to call on notification
|
||||
* @opaque: the opaque pointer to pass to the callback
|
||||
*
|
||||
* Create a new timerlist associated with the clock of
|
||||
* type @type.
|
||||
*
|
||||
* Returns: a pointer to the QEMUTimerList created
|
||||
*/
|
||||
QEMUTimerList *timerlist_new(QEMUClockType type,
|
||||
QEMUTimerListNotifyCB *cb, void *opaque);
|
||||
|
||||
/**
|
||||
* timerlist_free:
|
||||
* @timer_list: the timer list to free
|
||||
*
|
||||
* Frees a timer_list. It must have no active timers.
|
||||
*/
|
||||
void timerlist_free(QEMUTimerList *timer_list);
|
||||
|
||||
/**
|
||||
* timerlist_has_timers:
|
||||
* @timer_list: the timer list to operate on
|
||||
*
|
||||
* Determine whether a timer list has active timers
|
||||
*
|
||||
* Note that this function should not be used when other threads also access
|
||||
* the timer list. The return value may be outdated by the time it is acted
|
||||
* upon.
|
||||
*
|
||||
* Returns: true if the timer list has timers.
|
||||
*/
|
||||
bool timerlist_has_timers(QEMUTimerList *timer_list);
|
||||
|
||||
/**
|
||||
* timerlist_expired:
|
||||
* @timer_list: the timer list to operate on
|
||||
*
|
||||
* Determine whether a timer list has any timers which
|
||||
* are expired.
|
||||
*
|
||||
* Returns: true if the timer list has timers which
|
||||
* have expired.
|
||||
*/
|
||||
bool timerlist_expired(QEMUTimerList *timer_list);
|
||||
|
||||
/**
|
||||
* timerlist_deadline_ns:
|
||||
* @timer_list: the timer list to operate on
|
||||
*
|
||||
* Determine the deadline for a timer_list, i.e.
|
||||
* the number of nanoseconds until the first timer
|
||||
* expires. Return -1 if there are no timers.
|
||||
*
|
||||
* Returns: the number of nanoseconds until the earliest
|
||||
* timer expires -1 if none
|
||||
*/
|
||||
int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
|
||||
|
||||
/**
|
||||
* timerlist_get_clock:
|
||||
* @timer_list: the timer list to operate on
|
||||
*
|
||||
* Determine the clock type associated with a timer list.
|
||||
*
|
||||
* Returns: the clock type associated with the
|
||||
* timer list.
|
||||
*/
|
||||
QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
|
||||
|
||||
/**
|
||||
* timerlist_run_timers:
|
||||
* @timer_list: the timer list to use
|
||||
*
|
||||
* Call all expired timers associated with the timer list.
|
||||
*
|
||||
* Returns: true if any timer expired
|
||||
*/
|
||||
bool timerlist_run_timers(QEMUTimerList *timer_list);
|
||||
|
||||
/**
|
||||
* timerlist_notify:
|
||||
* @timer_list: the timer list to use
|
||||
*
|
||||
* call the notifier callback associated with the timer list.
|
||||
*/
|
||||
void timerlist_notify(QEMUTimerList *timer_list);
|
||||
|
||||
/*
|
||||
* QEMUTimerListGroup
|
||||
*/
|
||||
|
||||
/**
|
||||
* timerlistgroup_init:
|
||||
* @tlg: the timer list group
|
||||
* @cb: the callback to call when a notify is required
|
||||
* @opaque: the opaque pointer to be passed to the callback.
|
||||
*
|
||||
* Initialise a timer list group. This must already be
|
||||
* allocated in memory and zeroed. The notifier callback is
|
||||
* called whenever a clock in the timer list group is
|
||||
* reenabled or whenever a timer associated with any timer
|
||||
* list is modified. If @cb is specified as null, qemu_notify()
|
||||
* is used instead.
|
||||
*/
|
||||
void timerlistgroup_init(QEMUTimerListGroup *tlg,
|
||||
QEMUTimerListNotifyCB *cb, void *opaque);
|
||||
|
||||
/**
|
||||
* timerlistgroup_deinit:
|
||||
* @tlg: the timer list group
|
||||
*
|
||||
* Deinitialise a timer list group. This must already be
|
||||
* initialised. Note the memory is not freed.
|
||||
*/
|
||||
void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
|
||||
|
||||
/**
|
||||
* timerlistgroup_run_timers:
|
||||
* @tlg: the timer list group
|
||||
*
|
||||
* Run the timers associated with a timer list group.
|
||||
* This will run timers on multiple clocks.
|
||||
*
|
||||
* Returns: true if any timer callback ran
|
||||
*/
|
||||
bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
|
||||
|
||||
/**
|
||||
* timerlistgroup_deadline_ns:
|
||||
* @tlg: the timer list group
|
||||
*
|
||||
* Determine the deadline of the soonest timer to
|
||||
* expire associated with any timer list linked to
|
||||
* the timer list group. Only clocks suitable for
|
||||
* deadline calculation are included.
|
||||
*
|
||||
* Returns: the deadline in nanoseconds or -1 if no
|
||||
* timers are to expire.
|
||||
*/
|
||||
int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
|
||||
|
||||
/*
|
||||
* QEMUTimer
|
||||
*/
|
||||
|
||||
/**
|
||||
* timer_init:
|
||||
* @ts: the timer to be initialised
|
||||
* @timer_list: the timer list to attach the timer to
|
||||
* @scale: the scale value for the timer
|
||||
* @cb: the callback to be called when the timer expires
|
||||
* @opaque: the opaque pointer to be passed to the callback
|
||||
*
|
||||
* Initialise a new timer and associate it with @timer_list.
|
||||
* The caller is responsible for allocating the memory.
|
||||
*
|
||||
* You need not call an explicit deinit call. Simply make
|
||||
* sure it is not on a list with timer_del.
|
||||
*/
|
||||
void timer_init(QEMUTimer *ts,
|
||||
QEMUTimerList *timer_list, int scale,
|
||||
QEMUTimerCB *cb, void *opaque);
|
||||
|
||||
/**
|
||||
* timer_new_tl:
|
||||
* @timer_list: the timer list to attach the timer to
|
||||
* @scale: the scale value for the timer
|
||||
* @cb: the callback to be called when the timer expires
|
||||
* @opaque: the opaque pointer to be passed to the callback
|
||||
*
|
||||
* Creeate a new timer and associate it with @timer_list.
|
||||
* The memory is allocated by the function.
|
||||
*
|
||||
* This is not the preferred interface unless you know you
|
||||
* are going to call timer_free. Use timer_init instead.
|
||||
*
|
||||
* Returns: a pointer to the timer
|
||||
*/
|
||||
static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
|
||||
int scale,
|
||||
QEMUTimerCB *cb,
|
||||
void *opaque)
|
||||
{
|
||||
QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
|
||||
timer_init(ts, timer_list, scale, cb, opaque);
|
||||
return ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* timer_free:
|
||||
* @ts: the timer
|
||||
*
|
||||
* Free a timer (it must not be on the active list)
|
||||
*/
|
||||
void timer_free(QEMUTimer *ts);
|
||||
|
||||
/**
|
||||
* timer_del:
|
||||
* @ts: the timer
|
||||
*
|
||||
* Delete a timer from the active list.
|
||||
*
|
||||
* This function is thread-safe but the timer and its timer list must not be
|
||||
* freed while this function is running.
|
||||
*/
|
||||
void timer_del(QEMUTimer *ts);
|
||||
|
||||
/**
|
||||
* timer_mod_ns:
|
||||
* @ts: the timer
|
||||
* @expire_time: the expiry time in nanoseconds
|
||||
*
|
||||
* Modify a timer to expire at @expire_time
|
||||
*
|
||||
* This function is thread-safe but the timer and its timer list must not be
|
||||
* freed while this function is running.
|
||||
*/
|
||||
void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
|
||||
|
||||
/**
|
||||
* timer_mod_anticipate_ns:
|
||||
* @ts: the timer
|
||||
* @expire_time: the expiry time in nanoseconds
|
||||
*
|
||||
* Modify a timer to expire at @expire_time or the current time,
|
||||
* whichever comes earlier.
|
||||
*
|
||||
* This function is thread-safe but the timer and its timer list must not be
|
||||
* freed while this function is running.
|
||||
*/
|
||||
void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
|
||||
|
||||
/**
|
||||
* timer_mod:
|
||||
* @ts: the timer
|
||||
* @expire_time: the expire time in the units associated with the timer
|
||||
*
|
||||
* Modify a timer to expiry at @expire_time, taking into
|
||||
* account the scale associated with the timer.
|
||||
*
|
||||
* This function is thread-safe but the timer and its timer list must not be
|
||||
* freed while this function is running.
|
||||
*/
|
||||
void timer_mod(QEMUTimer *ts, int64_t expire_timer);
|
||||
|
||||
/**
|
||||
* timer_mod_anticipate:
|
||||
* @ts: the timer
|
||||
* @expire_time: the expiry time in nanoseconds
|
||||
*
|
||||
* Modify a timer to expire at @expire_time or the current time, whichever
|
||||
* comes earlier, taking into account the scale associated with the timer.
|
||||
*
|
||||
* This function is thread-safe but the timer and its timer list must not be
|
||||
* freed while this function is running.
|
||||
*/
|
||||
void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
|
||||
|
||||
/**
|
||||
* timer_pending:
|
||||
* @ts: the timer
|
||||
*
|
||||
* Determines whether a timer is pending (i.e. is on the
|
||||
* active list of timers, whether or not it has not yet expired).
|
||||
*
|
||||
* Returns: true if the timer is pending
|
||||
*/
|
||||
bool timer_pending(QEMUTimer *ts);
|
||||
|
||||
/**
|
||||
* timer_expired:
|
||||
* @ts: the timer
|
||||
*
|
||||
* Determines whether a timer has expired.
|
||||
*
|
||||
* Returns: true if the timer has expired
|
||||
*/
|
||||
bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
|
||||
|
||||
/**
|
||||
* timer_expire_time_ns:
|
||||
* @ts: the timer
|
||||
*
|
||||
* Determine the expiry time of a timer
|
||||
*
|
||||
* Returns: the expiry time in nanoseconds
|
||||
*/
|
||||
uint64_t timer_expire_time_ns(QEMUTimer *ts);
|
||||
|
||||
/**
|
||||
* timer_get:
|
||||
* @f: the file
|
||||
* @ts: the timer
|
||||
*
|
||||
* Read a timer @ts from a file @f
|
||||
*/
|
||||
void timer_get(QEMUFile *f, QEMUTimer *ts);
|
||||
|
||||
/**
|
||||
* timer_put:
|
||||
* @f: the file
|
||||
* @ts: the timer
|
||||
*/
|
||||
void timer_put(QEMUFile *f, QEMUTimer *ts);
|
||||
|
||||
/*
|
||||
* General utility functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* qemu_timeout_ns_to_ms:
|
||||
* @ns: nanosecond timeout value
|
||||
*
|
||||
* Convert a nanosecond timeout value (or -1) to
|
||||
* a millisecond value (or -1), always rounding up.
|
||||
*
|
||||
* Returns: millisecond timeout value
|
||||
*/
|
||||
int qemu_timeout_ns_to_ms(int64_t ns);
|
||||
|
||||
/**
|
||||
* qemu_poll_ns:
|
||||
* @fds: Array of file descriptors
|
||||
* @nfds: number of file descriptors
|
||||
* @timeout: timeout in nanoseconds
|
||||
*
|
||||
* Perform a poll like g_poll but with a timeout in nanoseconds.
|
||||
* See g_poll documentation for further details.
|
||||
*
|
||||
* Returns: number of fds ready
|
||||
*/
|
||||
int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
|
||||
|
||||
/**
|
||||
* qemu_soonest_timeout:
|
||||
* @timeout1: first timeout in nanoseconds (or -1 for infinite)
|
||||
* @timeout2: second timeout in nanoseconds (or -1 for infinite)
|
||||
*
|
||||
* Calculates the soonest of two timeout values. -1 means infinite, which
|
||||
* is later than any other value.
|
||||
*
|
||||
* Returns: soonest timeout value in nanoseconds (or -1 for infinite)
|
||||
*/
|
||||
static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
|
||||
{
|
||||
/* we can abuse the fact that -1 (which means infinite) is a maximal
|
||||
* value when cast to unsigned. As this is disgusting, it's kept in
|
||||
* one inline function.
|
||||
*/
|
||||
return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
|
||||
}
|
||||
|
||||
/**
|
||||
* initclocks:
|
||||
*
|
||||
* Initialise the clock & timer infrastructure
|
||||
*/
|
||||
void init_clocks(void);
|
||||
|
||||
int64_t cpu_get_ticks(void);
|
||||
/* Caller must hold BQL */
|
||||
void cpu_enable_ticks(void);
|
||||
/* Caller must hold BQL */
|
||||
void cpu_disable_ticks(void);
|
||||
|
||||
static inline int64_t get_ticks_per_sec(void)
|
||||
{
|
||||
return 1000000000LL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Low level clock functions
|
||||
*/
|
||||
|
||||
/* real time host monotonic timer */
|
||||
static inline int64_t get_clock_realtime(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
|
||||
}
|
||||
|
||||
/* Warning: don't insert tracepoints into these functions, they are
|
||||
also used by simpletrace backend and tracepoints would cause
|
||||
an infinite recursion! */
|
||||
#ifdef _WIN32
|
||||
extern int64_t clock_freq;
|
||||
|
||||
static inline int64_t get_clock(void)
|
||||
{
|
||||
LARGE_INTEGER ti;
|
||||
QueryPerformanceCounter(&ti);
|
||||
return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
extern int use_rt_clock;
|
||||
|
||||
static inline int64_t get_clock(void)
|
||||
{
|
||||
return get_clock_realtime();
|
||||
}
|
||||
#endif
|
||||
|
||||
/* icount */
|
||||
int64_t cpu_get_icount(void);
|
||||
int64_t cpu_get_clock(void);
|
||||
int64_t cpu_get_clock_offset(void);
|
||||
int64_t cpu_icount_to_ns(int64_t icount);
|
||||
|
||||
/*******************************************/
|
||||
/* host CPU ticks (if available) */
|
||||
|
||||
#if defined(_ARCH_PPC)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
int64_t retval;
|
||||
#ifdef _ARCH_PPC64
|
||||
/* This reads timebase in one 64bit go and includes Cell workaround from:
|
||||
http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
|
||||
*/
|
||||
__asm__ __volatile__ ("mftb %0\n\t"
|
||||
"cmpwi %0,0\n\t"
|
||||
"beq- $-8"
|
||||
: "=r" (retval));
|
||||
#else
|
||||
/* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
|
||||
unsigned long junk;
|
||||
__asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
|
||||
"mfspr %L0,268\n\t" /* mftb */
|
||||
"mfspr %0,269\n\t" /* mftbu */
|
||||
"cmpw %0,%1\n\t"
|
||||
"bne $-16"
|
||||
: "=r" (retval), "=r" (junk));
|
||||
#endif
|
||||
return retval;
|
||||
}
|
||||
|
||||
#elif defined(__i386__)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
int64_t val;
|
||||
asm volatile ("rdtsc" : "=A" (val));
|
||||
return val;
|
||||
}
|
||||
|
||||
#elif defined(__x86_64__)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
uint32_t low,high;
|
||||
int64_t val;
|
||||
asm volatile("rdtsc" : "=a" (low), "=d" (high));
|
||||
val = high;
|
||||
val <<= 32;
|
||||
val |= low;
|
||||
return val;
|
||||
}
|
||||
|
||||
#elif defined(__hppa__)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
int val;
|
||||
asm volatile ("mfctl %%cr16, %0" : "=r"(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
#elif defined(__ia64)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
int64_t val;
|
||||
asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
|
||||
return val;
|
||||
}
|
||||
|
||||
#elif defined(__s390__)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
int64_t val;
|
||||
asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
|
||||
return val;
|
||||
}
|
||||
|
||||
#elif defined(__sparc__)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks (void)
|
||||
{
|
||||
#if defined(_LP64)
|
||||
uint64_t rval;
|
||||
asm volatile("rd %%tick,%0" : "=r"(rval));
|
||||
return rval;
|
||||
#else
|
||||
/* We need an %o or %g register for this. For recent enough gcc
|
||||
there is an "h" constraint for that. Don't bother with that. */
|
||||
union {
|
||||
uint64_t i64;
|
||||
struct {
|
||||
uint32_t high;
|
||||
uint32_t low;
|
||||
} i32;
|
||||
} rval;
|
||||
asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
|
||||
: "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
|
||||
return rval.i64;
|
||||
#endif
|
||||
}
|
||||
|
||||
#elif defined(__mips__) && \
|
||||
((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
|
||||
/*
|
||||
* binutils wants to use rdhwr only on mips32r2
|
||||
* but as linux kernel emulate it, it's fine
|
||||
* to use it.
|
||||
*
|
||||
*/
|
||||
#define MIPS_RDHWR(rd, value) { \
|
||||
__asm__ __volatile__ (".set push\n\t" \
|
||||
".set mips32r2\n\t" \
|
||||
"rdhwr %0, "rd"\n\t" \
|
||||
".set pop" \
|
||||
: "=r" (value)); \
|
||||
}
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
/* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
|
||||
uint32_t count;
|
||||
static uint32_t cyc_per_count = 0;
|
||||
|
||||
if (!cyc_per_count) {
|
||||
MIPS_RDHWR("$3", cyc_per_count);
|
||||
}
|
||||
|
||||
MIPS_RDHWR("$2", count);
|
||||
return (int64_t)(count * cyc_per_count);
|
||||
}
|
||||
|
||||
#elif defined(__alpha__)
|
||||
|
||||
static inline int64_t cpu_get_real_ticks(void)
|
||||
{
|
||||
uint64_t cc;
|
||||
uint32_t cur, ofs;
|
||||
|
||||
asm volatile("rpcc %0" : "=r"(cc));
|
||||
cur = cc;
|
||||
ofs = cc >> 32;
|
||||
return cur - ofs;
|
||||
}
|
||||
|
||||
#else
|
||||
/* The host CPU doesn't have an easily accessible cycle counter.
|
||||
Just return a monotonically increasing value. This will be
|
||||
totally wrong, but hopefully better than nothing. */
|
||||
static inline int64_t cpu_get_real_ticks (void)
|
||||
{
|
||||
static int64_t ticks = 0;
|
||||
return ticks++;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_PROFILER
|
||||
static inline int64_t profile_getclock(void)
|
||||
{
|
||||
return cpu_get_real_ticks();
|
||||
}
|
||||
|
||||
extern int64_t qemu_time, qemu_time_start;
|
||||
extern int64_t tlb_flush_time;
|
||||
extern int64_t dev_time;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
52
qemu/include/qemu/tls.h
Normal file
52
qemu/include/qemu/tls.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Abstraction layer for defining and using TLS variables
|
||||
*
|
||||
* Copyright (c) 2011 Red Hat, Inc
|
||||
* Copyright (c) 2011 Linaro Limited
|
||||
*
|
||||
* Authors:
|
||||
* Paolo Bonzini <pbonzini@redhat.com>
|
||||
* Peter Maydell <peter.maydell@linaro.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef QEMU_TLS_H
|
||||
#define QEMU_TLS_H
|
||||
|
||||
/* Per-thread variables. Note that we only have implementations
|
||||
* which are really thread-local on Linux; the dummy implementations
|
||||
* define plain global variables.
|
||||
*
|
||||
* This means that for the moment use should be restricted to
|
||||
* per-VCPU variables, which are OK because:
|
||||
* - the only -user mode supporting multiple VCPU threads is linux-user
|
||||
* - TCG system mode is single-threaded regarding VCPUs
|
||||
* - KVM system mode is multi-threaded but limited to Linux
|
||||
*
|
||||
* TODO: proper implementations via Win32 .tls sections and
|
||||
* POSIX pthread_getspecific.
|
||||
*/
|
||||
#ifdef __linux__
|
||||
#define DECLARE_TLS(type, x) extern DEFINE_TLS(type, x)
|
||||
#define DEFINE_TLS(type, x) __thread __typeof__(type) tls__##x
|
||||
#define tls_var(x) tls__##x
|
||||
#else
|
||||
/* Dummy implementations which define plain global variables */
|
||||
#define DECLARE_TLS(type, x) extern DEFINE_TLS(type, x)
|
||||
#define DEFINE_TLS(type, x) __typeof__(type) tls__##x
|
||||
#define tls_var(x) tls__##x
|
||||
#endif
|
||||
|
||||
#endif
|
||||
82
qemu/include/qemu/typedefs.h
Normal file
82
qemu/include/qemu/typedefs.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef QEMU_TYPEDEFS_H
|
||||
#define QEMU_TYPEDEFS_H
|
||||
|
||||
/* A load of opaque types so that device init declarations don't have to
|
||||
pull in all the real definitions. */
|
||||
typedef struct QEMUTimer QEMUTimer;
|
||||
typedef struct QEMUTimerListGroup QEMUTimerListGroup;
|
||||
typedef struct QEMUFile QEMUFile;
|
||||
typedef struct QEMUBH QEMUBH;
|
||||
|
||||
typedef struct AioContext AioContext;
|
||||
|
||||
typedef struct Visitor Visitor;
|
||||
|
||||
struct Monitor;
|
||||
typedef struct Monitor Monitor;
|
||||
typedef struct MigrationParams MigrationParams;
|
||||
|
||||
typedef struct Property Property;
|
||||
typedef struct PropertyInfo PropertyInfo;
|
||||
typedef struct CompatProperty CompatProperty;
|
||||
typedef struct DeviceState DeviceState;
|
||||
typedef struct BusState BusState;
|
||||
typedef struct BusClass BusClass;
|
||||
|
||||
typedef struct AddressSpace AddressSpace;
|
||||
typedef struct MemoryRegion MemoryRegion;
|
||||
typedef struct MemoryRegionSection MemoryRegionSection;
|
||||
typedef struct MemoryListener MemoryListener;
|
||||
|
||||
typedef struct MemoryMappingList MemoryMappingList;
|
||||
|
||||
typedef struct QEMUMachine QEMUMachine;
|
||||
typedef struct MachineClass MachineClass;
|
||||
typedef struct MachineState MachineState;
|
||||
typedef struct NICInfo NICInfo;
|
||||
typedef struct HCIInfo HCIInfo;
|
||||
typedef struct AudioState AudioState;
|
||||
typedef struct BlockBackend BlockBackend;
|
||||
typedef struct BlockDriverState BlockDriverState;
|
||||
typedef struct DriveInfo DriveInfo;
|
||||
typedef struct DisplayState DisplayState;
|
||||
typedef struct DisplayChangeListener DisplayChangeListener;
|
||||
typedef struct DisplaySurface DisplaySurface;
|
||||
typedef struct PixelFormat PixelFormat;
|
||||
typedef struct QemuConsole QemuConsole;
|
||||
typedef struct CharDriverState CharDriverState;
|
||||
typedef struct MACAddr MACAddr;
|
||||
typedef struct NetClientState NetClientState;
|
||||
typedef struct I2CBus I2CBus;
|
||||
typedef struct ISABus ISABus;
|
||||
typedef struct ISADevice ISADevice;
|
||||
typedef struct SMBusDevice SMBusDevice;
|
||||
typedef struct PCIHostState PCIHostState;
|
||||
typedef struct PCIExpressHost PCIExpressHost;
|
||||
typedef struct PCIBus PCIBus;
|
||||
typedef struct PCIDevice PCIDevice;
|
||||
typedef struct PCIExpressDevice PCIExpressDevice;
|
||||
typedef struct PCIBridge PCIBridge;
|
||||
typedef struct PCIEAERMsg PCIEAERMsg;
|
||||
typedef struct PCIEAERLog PCIEAERLog;
|
||||
typedef struct PCIEAERErr PCIEAERErr;
|
||||
typedef struct PCIEPort PCIEPort;
|
||||
typedef struct PCIESlot PCIESlot;
|
||||
typedef struct MSIMessage MSIMessage;
|
||||
typedef struct SerialState SerialState;
|
||||
typedef struct PCMCIACardState PCMCIACardState;
|
||||
typedef struct MouseTransformInfo MouseTransformInfo;
|
||||
typedef struct uWireSlave uWireSlave;
|
||||
typedef struct I2SCodec I2SCodec;
|
||||
typedef struct SSIBus SSIBus;
|
||||
typedef struct EventNotifier EventNotifier;
|
||||
typedef struct VirtIODevice VirtIODevice;
|
||||
typedef struct QEMUSGList QEMUSGList;
|
||||
typedef struct QEMUSizedBuffer QEMUSizedBuffer;
|
||||
typedef struct SHPCDevice SHPCDevice;
|
||||
typedef struct FWCfgState FWCfgState;
|
||||
typedef struct PcGuestInfo PcGuestInfo;
|
||||
typedef struct Range Range;
|
||||
typedef struct AdapterInfo AdapterInfo;
|
||||
|
||||
#endif /* QEMU_TYPEDEFS_H */
|
||||
Reference in New Issue
Block a user