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

Commit af67435

Browse files
committed
Added support for Linux hosts
1 parent c6c61c2 commit af67435

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1246
-192
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ Win8.1Release/
1818
# macOS
1919
/darwin/hax_driver/com_intel_hax/build/
2020
.DS_Store
21+
22+
# Linux
23+
*.o
24+
*.cmd
25+
*.ko
26+
*.mod.c
27+
.tmp_versions
28+
.cache.mk
29+
modules.order
30+
Module.symvers

core/cpu.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ static void cpu_vmentry_failed(struct vcpu_t *vcpu, vmx_result_t result);
4747
static int cpu_vmexit_handler(struct vcpu_t *vcpu, exit_reason_t exit_reason,
4848
struct hax_tunnel *htun);
4949

50-
static int cpu_emt64_enable()
50+
static int cpu_emt64_enable(void)
5151
{
5252
uint32 effer;
5353

5454
effer = ia32_rdmsr(IA32_EFER);
5555
return effer & 0x400;
5656
}
5757

58-
static int cpu_nx_enable()
58+
static int cpu_nx_enable(void)
5959
{
6060
uint32 effer;
6161

@@ -71,7 +71,7 @@ bool cpu_has_feature(uint32_t feature)
7171
return cpuid_host_has_feature(&cache, feature);
7272
}
7373

74-
void cpu_init_feature_cache()
74+
void cpu_init_feature_cache(void)
7575
{
7676
cpuid_host_init(&cache);
7777
}
@@ -572,7 +572,7 @@ uint32 load_vmcs(struct vcpu_t *vcpu, preempt_flag *flags)
572572
/* when wake up from sleep, we need the barrier, as vm operation
573573
* are not serialized instructions.
574574
*/
575-
smp_mb();
575+
hax_smp_mb();
576576

577577
cpu_data = current_cpu_data();
578578

core/emulate.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static bool is_translation_required(struct em_context_t *ctxt)
288288
static uint64_t get_canonical_address(struct em_context_t *ctxt,
289289
uint64_t addr, uint vaddr_bits)
290290
{
291-
return ((int64)addr << (64 - vaddr_bits)) >> (64 - vaddr_bits);
291+
return ((int64_t)addr << (64 - vaddr_bits)) >> (64 - vaddr_bits);
292292
}
293293

294294
static em_status_t get_linear_address(struct em_context_t *ctxt,
@@ -475,24 +475,24 @@ static uint64_t insn_fetch_u64(struct em_context_t *ctxt)
475475
return result;
476476
}
477477

478-
static int8 insn_fetch_s8(struct em_context_t *ctxt)
478+
static int8_t insn_fetch_s8(struct em_context_t *ctxt)
479479
{
480-
return (int8)insn_fetch_u8(ctxt);
480+
return (int8_t)insn_fetch_u8(ctxt);
481481
}
482482

483-
static int16 insn_fetch_s16(struct em_context_t *ctxt)
483+
static int16_t insn_fetch_s16(struct em_context_t *ctxt)
484484
{
485-
return (int16)insn_fetch_u16(ctxt);
485+
return (int16_t)insn_fetch_u16(ctxt);
486486
}
487487

488-
static int32 insn_fetch_s32(struct em_context_t *ctxt)
488+
static int32_t insn_fetch_s32(struct em_context_t *ctxt)
489489
{
490-
return (int32)insn_fetch_u32(ctxt);
490+
return (int32_t)insn_fetch_u32(ctxt);
491491
}
492492

493-
static int64 insn_fetch_s64(struct em_context_t *ctxt)
493+
static int64_t insn_fetch_s64(struct em_context_t *ctxt)
494494
{
495-
return (int64)insn_fetch_u64(ctxt);
495+
return (int64_t)insn_fetch_u64(ctxt);
496496
}
497497

498498
static void decode_prefixes(struct em_context_t *ctxt)
@@ -774,7 +774,7 @@ static em_status_t decode_op_simm8(em_context_t *ctxt,
774774
{
775775
op->type = OP_IMM;
776776
op->size = 1;
777-
op->value = (int64)(insn_fetch_s8(ctxt));
777+
op->value = (int64_t)(insn_fetch_s8(ctxt));
778778
return EM_CONTINUE;
779779
}
780780

core/ept.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ bool ept_set_caps(uint64 caps)
6262
}
6363

6464
caps &= ~EPT_UNSUPPORTED_FEATURES;
65-
ASSERT(!ept_capabilities || caps == ept_capabilities);
65+
assert(!ept_capabilities || caps == ept_capabilities);
6666
// FIXME: This assignment is done by all logical processors simultaneously
6767
ept_capabilities = caps;
6868
return 1;
6969
}
7070

7171
static bool ept_has_cap(uint64 cap)
7272
{
73-
ASSERT(ept_capabilities != 0);
73+
assert(ept_capabilities != 0);
7474
// Avoid implicit conversion from uint64 to bool, because the latter may be
7575
// typedef'ed as uint8 (see hax_types_windows.h)
7676
return (ept_capabilities & cap) != 0;
@@ -185,7 +185,7 @@ static bool ept_lookup(struct vcpu_t *vcpu, paddr_t gpa, paddr_t *hpa)
185185
struct hax_ept *ept = vcpu->vm->ept;
186186
uint which_g = gpa >> 30;
187187

188-
ASSERT(ept->ept_root_page);
188+
assert(ept->ept_root_page);
189189
if (which_g >= EPT_MAX_MEM_G) {
190190
hax_debug("ept_lookup error!\n");
191191
return 0;
@@ -224,7 +224,7 @@ static bool ept_lookup(struct vcpu_t *vcpu, paddr_t gpa, paddr_t *hpa)
224224
// TODO: Do we need to consider cross-page case ??
225225
bool ept_translate(struct vcpu_t *vcpu, paddr_t gpa, uint order, paddr_t *hpa)
226226
{
227-
ASSERT(order == PG_ORDER_4K);
227+
assert(order == PG_ORDER_4K);
228228
return ept_lookup(vcpu, gpa, hpa);
229229
}
230230

@@ -301,7 +301,7 @@ void ept_free (hax_vm_t *hax_vm)
301301
struct hax_page *page, *n;
302302
struct hax_ept *ept = hax_vm->ept;
303303

304-
ASSERT(ept);
304+
assert(ept);
305305

306306
if (!ept->ept_root_page)
307307
return;
@@ -328,7 +328,7 @@ static void invept_smpfunc(struct invept_bundle *bundle)
328328
{
329329
struct per_cpu_data *cpu_data;
330330

331-
smp_mb();
331+
hax_smp_mb();
332332
cpu_data = current_cpu_data();
333333
cpu_data->invept_res = VMX_SUCCEED;
334334

@@ -373,7 +373,7 @@ void invept(hax_vm_t *hax_vm, uint type)
373373

374374
bundle.type = type;
375375
bundle.desc = &desc;
376-
smp_call_function(&cpu_online_map, (void (*)(void *))invept_smpfunc,
376+
hax_smp_call_function(&cpu_online_map, (void (*)(void *))invept_smpfunc,
377377
&bundle);
378378

379379
/*

core/hax.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ struct hax_t *hax;
6262
extern hax_atomic_t vmx_cpu_num, vmx_enabled_num;
6363
static void hax_enable_vmx(void)
6464
{
65-
smp_call_function(&cpu_online_map, cpu_init_vmx, NULL);
65+
hax_smp_call_function(&cpu_online_map, cpu_init_vmx, NULL);
6666
}
6767

6868
static void hax_disable_vmx(void)
6969
{
70-
smp_call_function(&cpu_online_map, cpu_exit_vmx, NULL);
70+
hax_smp_call_function(&cpu_online_map, cpu_exit_vmx, NULL);
7171
}
7272

7373
static void free_cpu_vmxon_region(void)
@@ -415,7 +415,7 @@ static void hax_pmu_init(void)
415415
int ref_cpu_id = -1;
416416

417417
// Execute cpu_pmu_init() on each logical processor of the host CPU
418-
smp_call_function(&cpu_online_map, cpu_pmu_init, NULL);
418+
hax_smp_call_function(&cpu_online_map, cpu_pmu_init, NULL);
419419

420420
// Find the common APM version supported by all host logical processors
421421
// TODO: Theoretically we should do the same for other APM parameters

core/ia32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern void ASMCALL asm_rdtsc(struct qword_val *qv);
4242
#else // !_M_IX86
4343
extern uint64 ASMCALL asm_rdmsr(uint32 reg);
4444
extern void ASMCALL asm_wrmsr(uint32 reg, uint64_t val);
45-
extern uint64 ASMCALL asm_rdtsc();
45+
extern uint64 ASMCALL asm_rdtsc(void);
4646
#endif // _M_IX86
4747

4848
uint64 ia32_rdmsr(uint32 reg)

core/ia32_ops.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function __nmi, 0
161161
int 2h
162162
ret
163163

164-
function __fls, 1
164+
function asm_fls, 1
165165
xor reg_ret_32, reg_ret_32
166166
bsr reg_ret_32, reg_arg1_32
167167
ret

core/include/cpu.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ struct per_cpu_data {
107107

108108
/*
109109
* These fields are used to record the result of certain VMX instructions
110-
* when they are used in a function wrapped by smp_call_function(). This is
110+
* when they are used in a function wrapped by hax_smp_call_function(). This is
111111
* because it is not safe to call hax_error(), etc. (whose underlying
112112
* implementation may use a lock) from the wrapped function to log a
113113
* failure; doing so may cause a deadlock and thus a host reboot, especially
114114
* on macOS, where mp_rendezvous_no_intrs() (the legacy Darwin API used by
115-
* HAXM to implement smp_call_function()) is known to be prone to deadlocks:
115+
* HAXM to implement hax_smp_call_function()) is known to be prone to deadlocks:
116116
* https://lists.apple.com/archives/darwin-kernel/2006/Dec/msg00006.html
117117
*/
118118
vmx_result_t vmxon_res;
@@ -174,7 +174,7 @@ void cpu_exit_vmx(void *arg);
174174

175175
void cpu_pmu_init(void *arg);
176176

177-
void cpu_init_feature_cache();
177+
void cpu_init_feature_cache(void);
178178
bool cpu_has_feature(uint32_t feature);
179179

180180
void hax_panic_log(struct vcpu_t *vcpu);

core/include/ia32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void ASMCALL asm_fxrstor(mword *addr);
7878
void ASMCALL asm_cpuid(union cpuid_args_t *state);
7979

8080
void ASMCALL __nmi(void);
81-
uint32 ASMCALL __fls(uint32 bit32);
81+
uint32 ASMCALL asm_fls(uint32 bit32);
8282

8383
uint64 ia32_rdmsr(uint32 reg);
8484
void ia32_wrmsr(uint32 reg, uint64 val);

core/include/vmx.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,17 @@ void vmx_vmwrite(struct vcpu_t *vcpu, const char *name,
527527
vmwrite(vcpu, GUEST_##seg##_AR, tmp_ar); \
528528
}
529529

530+
#elif defined(__linux__)
531+
#define VMWRITE_SEG(vcpu, seg, val) ({ \
532+
uint32_t tmp_ar = val.ar; \
533+
if (tmp_ar == 0) \
534+
tmp_ar = 0x10000; \
535+
vmwrite(vcpu, GUEST_##seg##_SELECTOR, (val).selector); \
536+
vmwrite(vcpu, GUEST_##seg##_BASE, (val).base); \
537+
vmwrite(vcpu, GUEST_##seg##_LIMIT, (val).limit); \
538+
vmwrite(vcpu, GUEST_##seg##_AR, tmp_ar); \
539+
})
540+
530541
#elif defined(__MACH__)
531542
#define VMWRITE_SEG(vcpu, seg, val) ({ \
532543
uint32_t tmp_ar = val.ar; \

0 commit comments

Comments
 (0)