Skip to content
Permalink
Browse files

Added support for Linux hosts

Signed-off-by: Alexandro Sanchez Bach <asanchez@kryptoslogic.com>
  • Loading branch information...
AlexAltea committed Nov 8, 2018
1 parent 119dea0 commit b84536c08477b2e6bbf3203571bedad890dc4cbd
@@ -1,2 +1,6 @@
# macOS
.DS_Store

# Linux
*.o
*.cmd
@@ -39,7 +39,7 @@
(0 * FASTOP_ALIGN))

/* Instruction handlers */
typedef void(__cdecl em_handler_t)(void);
typedef void(ASMCALL em_handler_t)(void);
em_handler_t em_not;
em_handler_t em_neg;
em_handler_t em_inc;
@@ -72,7 +72,7 @@ em_handler_t em_bextr;
em_handler_t em_andn;

/* Dispatch handlers */
void __cdecl fastop_dispatch(void *handler, uint64_t *dst,
void ASMCALL fastop_dispatch(void *handler, uint64_t *dst,
uint64_t *src1, uint64_t *src2, uint64_t *flags);

#endif /* HAX_CORE_EMULATE_OPS_H_ */
@@ -681,7 +681,7 @@ void vmx_vmwrite(struct vcpu_t *vcpu, const char *name,
vmwrite(vcpu, GUEST_##seg##_AR, tmp_ar); \
}

#elif defined(HAX_PLATFORM_DARWIN)
#else
#define VMWRITE_SEG(vcpu, seg, val) ({ \
uint32_t tmp_ar = val.ar; \
if (tmp_ar == 0) \
@@ -266,6 +266,9 @@ int hax_em64t_enabled(void);
#ifdef HAX_PLATFORM_DARWIN
#include "darwin/hax_mac.h"
#endif
#ifdef HAX_PLATFORM_LINUX
#include "linux/hax_linux.h"
#endif
#ifdef HAX_PLATFORM_WINDOWS
#include "windows/hax_windows.h"
#endif
@@ -42,6 +42,9 @@
#ifdef HAX_PLATFORM_DARWIN
#include "darwin/hax_interface_mac.h"
#endif
#ifdef HAX_PLATFORM_LINUX
#include "linux/hax_interface_linux.h"
#endif
#ifdef HAX_PLATFORM_WINDOWS
#include "windows/hax_interface_windows.h"
#endif
@@ -50,6 +50,13 @@
#define HAX_COMPILER_CLANG
#define PACKED __attribute__ ((packed))
#define ALIGNED(x) __attribute__ ((aligned(x)))
// GCC
#elif defined(__GNUC__)
#define HAX_COMPILER_GCC
#define PACKED __attribute__ ((packed))
#define ALIGNED(x) __attribute__ ((aligned(x)))
#define __cdecl __attribute__ ((__cdecl__,regparm(0)))
#define __stdcall __attribute__ ((__stdcall__))
// MSVC
#elif defined(_MSC_VER)
#define HAX_COMPILER_MSVC
@@ -67,6 +74,10 @@
#if defined(__MACH__)
#define HAX_PLATFORM_DARWIN
#include "darwin/hax_types_mac.h"
// Linux
#elif defined(__linux__)
#define HAX_PLATFORM_LINUX
#include "linux/hax_types_linux.h"
// Windows
#elif defined(_WIN32)
#define HAX_PLATFORM_WINDOWS
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2011 Intel Corporation
* Copyright (c) 2018 Kryptos Logic
*
* 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 copyright holder 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/

#ifndef HAX_LINUX_HAX_INTERFACE_LINUX_H_
#define HAX_LINUX_HAX_INTERFACE_LINUX_H_

#include <asm/ioctl.h>

/* The mac specific interface to qemu because of mac's
* special handling like hax tunnel allocation etc */
/* HAX model level ioctl */
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, struct hax_module_version)
#define HAX_IOCTL_CREATE_VM _IOWR(0, 0x21, uint32_t)
#define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t)
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
#define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit)

// Only for backward compatibility with old Qemu.
#define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int)

#define HAX_VM_IOCTL_VCPU_CREATE _IOWR(0, 0x80, uint32_t)
#define HAX_VM_IOCTL_ALLOC_RAM _IOWR(0, 0x81, struct hax_alloc_ram_info)
#define HAX_VM_IOCTL_SET_RAM _IOWR(0, 0x82, struct hax_set_ram_info)
#define HAX_VM_IOCTL_VCPU_DESTROY _IOR(0, 0x83, uint32_t)
#define HAX_VM_IOCTL_ADD_RAMBLOCK _IOW(0, 0x85, struct hax_ramblock_info)
#define HAX_VM_IOCTL_SET_RAM2 _IOWR(0, 0x86, struct hax_set_ram_info2)
#define HAX_VM_IOCTL_PROTECT_RAM _IOWR(0, 0x87, struct hax_protect_ram_info)

#define HAX_VCPU_IOCTL_RUN _IO(0, 0xc0)
#define HAX_VCPU_IOCTL_SET_MSRS _IOWR(0, 0xc1, struct hax_msr_data)
#define HAX_VCPU_IOCTL_GET_MSRS _IOWR(0, 0xc2, struct hax_msr_data)

#define HAX_VCPU_IOCTL_SET_FPU _IOW(0, 0xc3, struct fx_layout)
#define HAX_VCPU_IOCTL_GET_FPU _IOR(0, 0xc4, struct fx_layout)

#define HAX_VCPU_IOCTL_SETUP_TUNNEL _IOWR(0, 0xc5, struct hax_tunnel_info)
#define HAX_VCPU_IOCTL_INTERRUPT _IOWR(0, 0xc6, uint32_t)
#define HAX_VCPU_SET_REGS _IOWR(0, 0xc7, struct vcpu_state_t)
#define HAX_VCPU_GET_REGS _IOWR(0, 0xc8, struct vcpu_state_t)

/* API 2.0 */
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION _IOW(0, 0x84, struct hax_qemu_version)

#define HAX_IOCTL_VCPU_DEBUG _IOW(0, 0xc9, struct hax_debug_t)

#define HAX_KERNEL64_CS 0x80
#define HAX_KERNEL32_CS 0x08

#define is_compatible() 0

#endif // HAX_LINUX_HAX_INTERFACE_LINUX_H_
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2011 Intel Corporation
* Copyright (c) 2018 Kryptos Logic
*
* 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 copyright holder 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/

#ifndef HAX_LINUX_HAX_LINUX_H_
#define HAX_LINUX_HAX_LINUX_H_

#define HAX_RAM_ENTRY_SIZE 0x4000000

hax_spinlock *hax_spinlock_alloc_init(void);
void hax_spinlock_free(hax_spinlock *lock);
void hax_spin_lock(hax_spinlock *lock);
void hax_spin_unlock(hax_spinlock *lock);

hax_mutex hax_mutex_alloc_init(void);
void hax_mutex_lock(hax_mutex lock);
void hax_mutex_unlock(hax_mutex lock);
void hax_mutex_free(hax_mutex lock);

/* Return true if the bit is set already */
int hax_test_and_set_bit(int bit, uint64_t *memory);

/* Return true if the bit is cleared already */
int hax_test_and_clear_bit(int bit, uint64_t *memory);

/* Don't care for the big endian situation */
static inline bool hax_test_bit(int bit, uint64_t *memory)
{
int byte = bit / 8;
unsigned char *p;
int offset = bit % 8;

p = (unsigned char *)memory + byte;
return !!(*p & (1 << offset));
}

// memcpy_s() is part of the optional Bounds Checking Interfaces specified in
// Annex K of the C11 standard:
// http://en.cppreference.com/w/c/string/byte/memcpy
// However, it is not implemented by Clang:
// https://stackoverflow.com/questions/40829032/how-to-install-c11-compiler-on-mac-os-with-optional-string-functions-included
// Provide a simplified implementation here so memcpy_s() can be used instead of
// memcpy() everywhere else, which helps reduce the number of Klocwork warnings.
static inline int memcpy_s(void *dest, size_t destsz, const void *src,
size_t count)
{
char *dest_start = (char *)dest;
char *dest_end = (char *)dest + destsz;
char *src_start = (char *)src;
char *src_end = (char *)src + count;
bool overlap;

if (count == 0)
return 0;

if (!dest || destsz == 0)
return -EINVAL;

overlap = src_start < dest_start
? dest_start < src_end : src_start < dest_end;
if (!src || count > destsz || overlap) {
memset(dest, 0, destsz);
return -EINVAL;
}

memcpy(dest, src, count);
return 0;
}

/* Why it's a bool? Strange */
bool hax_cmpxchg32(uint32_t old_val, uint32_t new_val, volatile uint32_t *addr);
bool hax_cmpxchg64(uint64_t old_val, uint64_t new_val, volatile uint64_t *addr);

static inline bool cpu_is_online(int cpu)
{
if (cpu < 0 || cpu >= max_cpus)
return 0;
return !!(((mword)1 << cpu) & cpu_online_map);
}

int hax_notify_host_event(enum hax_notify_event event, uint32_t *param,
uint32_t size);

extern int default_hax_log_level;

void hax_error(char *fmt, ...);
void hax_warning(char *fmt, ...);
void hax_info(char *fmt, ...);
void hax_debug(char *fmt, ...);
void hax_log(char *fmt, ...);

#define hax_log hax_info

//#define hax_panic DbgPrint
#define hax_panic hax_error

//#define assert(condition) BUG_ON(!(condition))
void assert(bool condition);

#endif // HAX_LINUX_HAX_LINUX_H_

0 comments on commit b84536c

Please sign in to comment.
You can’t perform that action at this time.