Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Commit a99ba16

Browse files
committed
Added support for Linux hosts
Signed-off-by: Alexandro Sanchez Bach <[email protected]>
1 parent 119dea0 commit a99ba16

21 files changed

+2110
-3
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# macOS
22
.DS_Store
3+
4+
# Linux
5+
*.o
6+
*.cmd

core/include/emulate_ops.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
(0 * FASTOP_ALIGN))
4040

4141
/* Instruction handlers */
42-
typedef void(__cdecl em_handler_t)(void);
42+
typedef void(ASMCALL em_handler_t)(void);
4343
em_handler_t em_not;
4444
em_handler_t em_neg;
4545
em_handler_t em_inc;
@@ -72,7 +72,7 @@ em_handler_t em_bextr;
7272
em_handler_t em_andn;
7373

7474
/* Dispatch handlers */
75-
void __cdecl fastop_dispatch(void *handler, uint64_t *dst,
75+
void ASMCALL fastop_dispatch(void *handler, uint64_t *dst,
7676
uint64_t *src1, uint64_t *src2, uint64_t *flags);
7777

7878
#endif /* HAX_CORE_EMULATE_OPS_H_ */

core/include/vmx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ void vmx_vmwrite(struct vcpu_t *vcpu, const char *name,
681681
vmwrite(vcpu, GUEST_##seg##_AR, tmp_ar); \
682682
}
683683

684-
#elif defined(HAX_PLATFORM_DARWIN)
684+
#else
685685
#define VMWRITE_SEG(vcpu, seg, val) ({ \
686686
uint32_t tmp_ar = val.ar; \
687687
if (tmp_ar == 0) \

include/hax.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ int hax_em64t_enabled(void);
266266
#ifdef HAX_PLATFORM_DARWIN
267267
#include "darwin/hax_mac.h"
268268
#endif
269+
#ifdef HAX_PLATFORM_LINUX
270+
#include "linux/hax_linux.h"
271+
#endif
269272
#ifdef HAX_PLATFORM_WINDOWS
270273
#include "windows/hax_windows.h"
271274
#endif

include/hax_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#ifdef HAX_PLATFORM_DARWIN
4343
#include "darwin/hax_interface_mac.h"
4444
#endif
45+
#ifdef HAX_PLATFORM_LINUX
46+
#include "linux/hax_interface_linux.h"
47+
#endif
4548
#ifdef HAX_PLATFORM_WINDOWS
4649
#include "windows/hax_interface_windows.h"
4750
#endif

include/hax_types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
#define HAX_COMPILER_CLANG
5151
#define PACKED __attribute__ ((packed))
5252
#define ALIGNED(x) __attribute__ ((aligned(x)))
53+
// GCC
54+
#elif defined(__GNUC__)
55+
#define HAX_COMPILER_GCC
56+
#define PACKED __attribute__ ((packed))
57+
#define ALIGNED(x) __attribute__ ((aligned(x)))
58+
#define __cdecl __attribute__ ((__cdecl__,regparm(0)))
59+
#define __stdcall __attribute__ ((__stdcall__))
5360
// MSVC
5461
#elif defined(_MSC_VER)
5562
#define HAX_COMPILER_MSVC
@@ -67,6 +74,10 @@
6774
#if defined(__MACH__)
6875
#define HAX_PLATFORM_DARWIN
6976
#include "darwin/hax_types_mac.h"
77+
// Linux
78+
#elif defined(__linux__)
79+
#define HAX_PLATFORM_LINUX
80+
#include "linux/hax_types_linux.h"
7081
// Windows
7182
#elif defined(_WIN32)
7283
#define HAX_PLATFORM_WINDOWS
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2011 Intel Corporation
3+
* Copyright (c) 2018 Kryptos Logic
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef HAX_LINUX_HAX_INTERFACE_LINUX_H_
33+
#define HAX_LINUX_HAX_INTERFACE_LINUX_H_
34+
35+
#include <asm/ioctl.h>
36+
37+
/* The mac specific interface to qemu because of mac's
38+
* special handling like hax tunnel allocation etc */
39+
/* HAX model level ioctl */
40+
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, struct hax_module_version)
41+
#define HAX_IOCTL_CREATE_VM _IOWR(0, 0x21, uint32_t)
42+
#define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t)
43+
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
44+
#define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit)
45+
46+
// Only for backward compatibility with old Qemu.
47+
#define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int)
48+
49+
#define HAX_VM_IOCTL_VCPU_CREATE _IOWR(0, 0x80, uint32_t)
50+
#define HAX_VM_IOCTL_ALLOC_RAM _IOWR(0, 0x81, struct hax_alloc_ram_info)
51+
#define HAX_VM_IOCTL_SET_RAM _IOWR(0, 0x82, struct hax_set_ram_info)
52+
#define HAX_VM_IOCTL_VCPU_DESTROY _IOR(0, 0x83, uint32_t)
53+
#define HAX_VM_IOCTL_ADD_RAMBLOCK _IOW(0, 0x85, struct hax_ramblock_info)
54+
#define HAX_VM_IOCTL_SET_RAM2 _IOWR(0, 0x86, struct hax_set_ram_info2)
55+
#define HAX_VM_IOCTL_PROTECT_RAM _IOWR(0, 0x87, struct hax_protect_ram_info)
56+
57+
#define HAX_VCPU_IOCTL_RUN _IO(0, 0xc0)
58+
#define HAX_VCPU_IOCTL_SET_MSRS _IOWR(0, 0xc1, struct hax_msr_data)
59+
#define HAX_VCPU_IOCTL_GET_MSRS _IOWR(0, 0xc2, struct hax_msr_data)
60+
61+
#define HAX_VCPU_IOCTL_SET_FPU _IOW(0, 0xc3, struct fx_layout)
62+
#define HAX_VCPU_IOCTL_GET_FPU _IOR(0, 0xc4, struct fx_layout)
63+
64+
#define HAX_VCPU_IOCTL_SETUP_TUNNEL _IOWR(0, 0xc5, struct hax_tunnel_info)
65+
#define HAX_VCPU_IOCTL_INTERRUPT _IOWR(0, 0xc6, uint32_t)
66+
#define HAX_VCPU_SET_REGS _IOWR(0, 0xc7, struct vcpu_state_t)
67+
#define HAX_VCPU_GET_REGS _IOWR(0, 0xc8, struct vcpu_state_t)
68+
69+
/* API 2.0 */
70+
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION _IOW(0, 0x84, struct hax_qemu_version)
71+
72+
#define HAX_IOCTL_VCPU_DEBUG _IOW(0, 0xc9, struct hax_debug_t)
73+
74+
#define HAX_KERNEL64_CS 0x80
75+
#define HAX_KERNEL32_CS 0x08
76+
77+
#define is_compatible() 0
78+
79+
#endif // HAX_LINUX_HAX_INTERFACE_LINUX_H_

include/linux/hax_linux.h

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2011 Intel Corporation
3+
* Copyright (c) 2018 Kryptos Logic
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef HAX_LINUX_HAX_LINUX_H_
33+
#define HAX_LINUX_HAX_LINUX_H_
34+
35+
#define HAX_RAM_ENTRY_SIZE 0x4000000
36+
37+
hax_spinlock *hax_spinlock_alloc_init(void);
38+
void hax_spinlock_free(hax_spinlock *lock);
39+
void hax_spin_lock(hax_spinlock *lock);
40+
void hax_spin_unlock(hax_spinlock *lock);
41+
42+
hax_mutex hax_mutex_alloc_init(void);
43+
void hax_mutex_lock(hax_mutex lock);
44+
void hax_mutex_unlock(hax_mutex lock);
45+
void hax_mutex_free(hax_mutex lock);
46+
47+
/* Return true if the bit is set already */
48+
int hax_test_and_set_bit(int bit, uint64_t *memory);
49+
50+
/* Return true if the bit is cleared already */
51+
int hax_test_and_clear_bit(int bit, uint64_t *memory);
52+
53+
/* Don't care for the big endian situation */
54+
static inline bool hax_test_bit(int bit, uint64_t *memory)
55+
{
56+
int byte = bit / 8;
57+
unsigned char *p;
58+
int offset = bit % 8;
59+
60+
p = (unsigned char *)memory + byte;
61+
return !!(*p & (1 << offset));
62+
}
63+
64+
// memcpy_s() is part of the optional Bounds Checking Interfaces specified in
65+
// Annex K of the C11 standard:
66+
// http://en.cppreference.com/w/c/string/byte/memcpy
67+
// However, it is not implemented by Clang:
68+
// https://stackoverflow.com/questions/40829032/how-to-install-c11-compiler-on-mac-os-with-optional-string-functions-included
69+
// Provide a simplified implementation here so memcpy_s() can be used instead of
70+
// memcpy() everywhere else, which helps reduce the number of Klocwork warnings.
71+
static inline int memcpy_s(void *dest, size_t destsz, const void *src,
72+
size_t count)
73+
{
74+
char *dest_start = (char *)dest;
75+
char *dest_end = (char *)dest + destsz;
76+
char *src_start = (char *)src;
77+
char *src_end = (char *)src + count;
78+
bool overlap;
79+
80+
if (count == 0)
81+
return 0;
82+
83+
if (!dest || destsz == 0)
84+
return -EINVAL;
85+
86+
overlap = src_start < dest_start
87+
? dest_start < src_end : src_start < dest_end;
88+
if (!src || count > destsz || overlap) {
89+
memset(dest, 0, destsz);
90+
return -EINVAL;
91+
}
92+
93+
memcpy(dest, src, count);
94+
return 0;
95+
}
96+
97+
/* Why it's a bool? Strange */
98+
bool hax_cmpxchg32(uint32_t old_val, uint32_t new_val, volatile uint32_t *addr);
99+
bool hax_cmpxchg64(uint64_t old_val, uint64_t new_val, volatile uint64_t *addr);
100+
101+
static inline bool cpu_is_online(int cpu)
102+
{
103+
if (cpu < 0 || cpu >= max_cpus)
104+
return 0;
105+
return !!(((mword)1 << cpu) & cpu_online_map);
106+
}
107+
108+
int hax_notify_host_event(enum hax_notify_event event, uint32_t *param,
109+
uint32_t size);
110+
111+
extern int default_hax_log_level;
112+
113+
void hax_error(char *fmt, ...);
114+
void hax_warning(char *fmt, ...);
115+
void hax_info(char *fmt, ...);
116+
void hax_debug(char *fmt, ...);
117+
void hax_log(char *fmt, ...);
118+
119+
#define hax_log hax_info
120+
121+
//#define hax_panic DbgPrint
122+
#define hax_panic hax_error
123+
124+
//#define assert(condition) BUG_ON(!(condition))
125+
void assert(bool condition);
126+
127+
#endif // HAX_LINUX_HAX_LINUX_H_

0 commit comments

Comments
 (0)