import Unicorn2
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
#ifndef BSWAP_H
|
||||
#define BSWAP_H
|
||||
|
||||
#include "config-host.h"
|
||||
#include "unicorn/platform.h"
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include "fpu/softfloat.h"
|
||||
#include "osdep.h"
|
||||
#include "fpu/softfloat-types.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>
|
||||
@@ -85,6 +81,64 @@ static inline void bswap64s(uint64_t *s)
|
||||
#define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Endianness conversion functions between host cpu and specified endianness.
|
||||
* (We list the complete set of prototypes produced by the macros below
|
||||
* to assist people who search the headers to find their definitions.)
|
||||
*
|
||||
* uint16_t le16_to_cpu(uint16_t v);
|
||||
* uint32_t le32_to_cpu(uint32_t v);
|
||||
* uint64_t le64_to_cpu(uint64_t v);
|
||||
* uint16_t be16_to_cpu(uint16_t v);
|
||||
* uint32_t be32_to_cpu(uint32_t v);
|
||||
* uint64_t be64_to_cpu(uint64_t v);
|
||||
*
|
||||
* Convert the value @v from the specified format to the native
|
||||
* endianness of the host CPU by byteswapping if necessary, and
|
||||
* return the converted value.
|
||||
*
|
||||
* uint16_t cpu_to_le16(uint16_t v);
|
||||
* uint32_t cpu_to_le32(uint32_t v);
|
||||
* uint64_t cpu_to_le64(uint64_t v);
|
||||
* uint16_t cpu_to_be16(uint16_t v);
|
||||
* uint32_t cpu_to_be32(uint32_t v);
|
||||
* uint64_t cpu_to_be64(uint64_t v);
|
||||
*
|
||||
* Convert the value @v from the native endianness of the host CPU to
|
||||
* the specified format by byteswapping if necessary, and return
|
||||
* the converted value.
|
||||
*
|
||||
* void le16_to_cpus(uint16_t *v);
|
||||
* void le32_to_cpus(uint32_t *v);
|
||||
* void le64_to_cpus(uint64_t *v);
|
||||
* void be16_to_cpus(uint16_t *v);
|
||||
* void be32_to_cpus(uint32_t *v);
|
||||
* void be64_to_cpus(uint64_t *v);
|
||||
*
|
||||
* Do an in-place conversion of the value pointed to by @v from the
|
||||
* specified format to the native endianness of the host CPU.
|
||||
*
|
||||
* void cpu_to_le16s(uint16_t *v);
|
||||
* void cpu_to_le32s(uint32_t *v);
|
||||
* void cpu_to_le64s(uint64_t *v);
|
||||
* void cpu_to_be16s(uint16_t *v);
|
||||
* void cpu_to_be32s(uint32_t *v);
|
||||
* void cpu_to_be64s(uint64_t *v);
|
||||
*
|
||||
* Do an in-place conversion of the value pointed to by @v from the
|
||||
* native endianness of the host CPU to the specified format.
|
||||
*
|
||||
* Both X_to_cpu() and cpu_to_X() perform the same operation; you
|
||||
* should use whichever one is better documenting of the function your
|
||||
* code is performing.
|
||||
*
|
||||
* Do not use these functions for conversion of values which are in guest
|
||||
* memory, since the data may not be sufficiently aligned for the host CPU's
|
||||
* load and store instructions. Instead you should use the ld*_p() and
|
||||
* st*_p() functions, which perform loads and stores of data of any
|
||||
* required size and endianness and handle possible misalignment.
|
||||
*/
|
||||
|
||||
#define CPU_CONVERT(endian, size, type)\
|
||||
static inline type endian ## size ## _to_cpu(type v)\
|
||||
{\
|
||||
@@ -104,16 +158,6 @@ static inline void endian ## size ## _to_cpus(type *p)\
|
||||
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)
|
||||
@@ -130,6 +174,25 @@ static inline uint32_t qemu_bswap_len(uint32_t value, int len)
|
||||
return bswap32(value) >> (32 - 8 * len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Same as cpu_to_le{16,32}, except that gcc will figure the result is
|
||||
* a compile-time constant if you pass in a constant. So this can be
|
||||
* used to initialize static variables.
|
||||
*/
|
||||
#if defined(HOST_WORDS_BIGENDIAN)
|
||||
# define const_le32(_x) \
|
||||
((((_x) & 0x000000ffU) << 24) | \
|
||||
(((_x) & 0x0000ff00U) << 8) | \
|
||||
(((_x) & 0x00ff0000U) >> 8) | \
|
||||
(((_x) & 0xff000000U) >> 24))
|
||||
# define const_le16(_x) \
|
||||
((((_x) & 0x00ff) << 8) | \
|
||||
(((_x) & 0xff00) >> 8))
|
||||
#else
|
||||
# define const_le32(_x) (_x)
|
||||
# define const_le16(_x) (_x)
|
||||
#endif
|
||||
|
||||
/* Unions for reinterpreting between floats and integers. */
|
||||
|
||||
typedef union {
|
||||
@@ -193,9 +256,9 @@ typedef union {
|
||||
/*
|
||||
* the generic syntax is:
|
||||
*
|
||||
* load: ld{type}{sign}{size}{endian}_p(ptr)
|
||||
* load: ld{type}{sign}{size}_{endian}_p(ptr)
|
||||
*
|
||||
* store: st{type}{size}{endian}_p(ptr, val)
|
||||
* store: st{type}{size}_{endian}_p(ptr, val)
|
||||
*
|
||||
* Note there are small differences with the softmmu access API!
|
||||
*
|
||||
@@ -204,7 +267,7 @@ typedef union {
|
||||
* f : float access
|
||||
*
|
||||
* sign is:
|
||||
* (empty): for floats or 32 bit size
|
||||
* (empty): for 32 or 64 bit sizes (including floats and doubles)
|
||||
* u : unsigned
|
||||
* s : signed
|
||||
*
|
||||
@@ -218,7 +281,25 @@ typedef union {
|
||||
* he : host endian
|
||||
* be : big endian
|
||||
* le : little endian
|
||||
* te : target endian
|
||||
* (except for byte accesses, which have no endian infix).
|
||||
*
|
||||
* The target endian accessors are obviously only available to source
|
||||
* files which are built per-target; they are defined in cpu-all.h.
|
||||
*
|
||||
* In all cases these functions take a host pointer.
|
||||
* For accessors that take a guest address rather than a
|
||||
* host address, see the cpu_{ld,st}_* accessors defined in
|
||||
* cpu_ldst.h.
|
||||
*
|
||||
* For cases where the size to be used is not fixed at compile time,
|
||||
* there are
|
||||
* stn_{endian}_p(ptr, sz, val)
|
||||
* which stores @val to @ptr as an @endian-order number @sz bytes in size
|
||||
* and
|
||||
* ldn_{endian}_p(ptr, sz)
|
||||
* which loads @sz bytes from @ptr as an unsigned @endian-order number
|
||||
* and returns it in a uint64_t.
|
||||
*/
|
||||
|
||||
static inline int ldub_p(const void *ptr)
|
||||
@@ -236,51 +317,85 @@ 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. */
|
||||
/*
|
||||
* 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.
|
||||
* Some compilation environments (eg some fortify-source implementations)
|
||||
* may intercept memcpy() in a way that defeats the compiler optimization,
|
||||
* though, so we use __builtin_memcpy() to give ourselves the best chance
|
||||
* of good performance.
|
||||
*/
|
||||
|
||||
static inline int lduw_he_p(const void *ptr)
|
||||
{
|
||||
uint16_t r;
|
||||
#ifdef _MSC_VER
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
#else
|
||||
__builtin_memcpy(&r, ptr, sizeof(r));
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline int ldsw_he_p(const void *ptr)
|
||||
{
|
||||
int16_t r;
|
||||
#ifdef _MSC_VER
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
#else
|
||||
__builtin_memcpy(&r, ptr, sizeof(r));
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void stw_he_p(void *ptr, uint16_t v)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
memcpy(ptr, &v, sizeof(v));
|
||||
#else
|
||||
__builtin_memcpy(ptr, &v, sizeof(v));
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int ldl_he_p(const void *ptr)
|
||||
{
|
||||
int32_t r;
|
||||
#ifdef _MSC_VER
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
#else
|
||||
__builtin_memcpy(&r, ptr, sizeof(r));
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void stl_he_p(void *ptr, uint32_t v)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
memcpy(ptr, &v, sizeof(v));
|
||||
#else
|
||||
__builtin_memcpy(ptr, &v, sizeof(v));
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline uint64_t ldq_he_p(const void *ptr)
|
||||
{
|
||||
uint64_t r;
|
||||
#ifdef _MSC_VER
|
||||
memcpy(&r, ptr, sizeof(r));
|
||||
#else
|
||||
__builtin_memcpy(&r, ptr, sizeof(r));
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline void stq_he_p(void *ptr, uint64_t v)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
memcpy(ptr, &v, sizeof(v));
|
||||
#else
|
||||
__builtin_memcpy(ptr, &v, sizeof(v));
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int lduw_le_p(const void *ptr)
|
||||
@@ -415,17 +530,58 @@ static inline void stfq_be_p(void *ptr, float64 v)
|
||||
|
||||
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
|
||||
#if HOST_LONG_BITS == 32
|
||||
return le_bswap(v, 32);
|
||||
#elif ULONG_MAX == UINT64_MAX
|
||||
#elif HOST_LONG_BITS == 64
|
||||
return le_bswap(v, 64);
|
||||
#else
|
||||
# error Unknown sizeof long
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Store v to p as a sz byte value in host order */
|
||||
#define DO_STN_LDN_P(END) \
|
||||
static inline void stn_## END ## _p(void *ptr, int sz, uint64_t v) \
|
||||
{ \
|
||||
switch (sz) { \
|
||||
case 1: \
|
||||
stb_p(ptr, v); \
|
||||
break; \
|
||||
case 2: \
|
||||
stw_ ## END ## _p(ptr, v); \
|
||||
break; \
|
||||
case 4: \
|
||||
stl_ ## END ## _p(ptr, v); \
|
||||
break; \
|
||||
case 8: \
|
||||
stq_ ## END ## _p(ptr, v); \
|
||||
break; \
|
||||
default: \
|
||||
break; /* g_assert_not_reached(); */ \
|
||||
} \
|
||||
} \
|
||||
static inline uint64_t ldn_## END ## _p(const void *ptr, int sz) \
|
||||
{ \
|
||||
switch (sz) { \
|
||||
case 1: \
|
||||
return ldub_p(ptr); \
|
||||
case 2: \
|
||||
return lduw_ ## END ## _p(ptr); \
|
||||
case 4: \
|
||||
return (uint32_t)ldl_ ## END ## _p(ptr); \
|
||||
case 8: \
|
||||
return ldq_ ## END ## _p(ptr); \
|
||||
default: \
|
||||
return 0; /* g_assert_not_reached(); */ \
|
||||
} \
|
||||
}
|
||||
|
||||
DO_STN_LDN_P(he)
|
||||
DO_STN_LDN_P(le)
|
||||
DO_STN_LDN_P(be)
|
||||
|
||||
#undef DO_STN_LDN_P
|
||||
|
||||
#undef le_bswap
|
||||
#undef be_bswap
|
||||
#undef le_bswaps
|
||||
|
||||
Reference in New Issue
Block a user