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

Commit 9f9da56

Browse files
AlexAlteaAlexandro Sanchez
authored andcommitted
Added generic wrapper for IA32 assembly functions
Signed-off-by: Alexandro Sanchez Bach <alexandro@phi.nz>
1 parent c2af1d5 commit 9f9da56

10 files changed

Lines changed: 207 additions & 355 deletions

File tree

core/haxlib.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
associated with wrapped tasks will reside.-->
8383
<ItemGroup Label="WrappedTaskItems">
8484
<NASM Include="emulate_ops.asm" />
85-
<NASM Include="ia32.asm" />
85+
<NASM Include="ia32_ops.asm" />
8686
<NASM Include="vmx_ops.asm" />
8787
</ItemGroup>
8888
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -114,6 +114,7 @@
114114
<ItemGroup>
115115
<!-- We only add items (e.g. form ClSourceFiles) that do not already exist (e.g in the ClCompile list), this avoids duplication -->
116116
<ClCompile Include="@(ClSourceFiles)" Exclude="@(ClCompile)" />
117+
<ClCompile Include="ia32.c" />
117118
<ResourceCompile Include="@(RcSourceFiles)" Exclude="@(ResourceCompile)" />
118119
<Midl Include="@(IdlSourceFiles)" Exclude="@(Midl)" />
119120
<MessageCompile Include="@(McSourceFiles)" Exclude="@(MessageCompile)" />

core/ia32.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright (c) 2011 Intel Corporation
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* 3. Neither the name of the copyright holder nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include "../include/hax.h"
32+
#include "../include/asm.h"
33+
34+
struct qword_val {
35+
uint32 low;
36+
uint32 high;
37+
};
38+
39+
extern void ASMCALL asm_enable_irq(void);
40+
extern void ASMCALL asm_disable_irq(void);
41+
42+
extern void asm_btr(uint8 *addr, uint bit);
43+
extern void asm_bts(uint8 *addr, uint bit);
44+
45+
extern void asm_fxinit(void);
46+
extern void asm_fxsave(mword *addr);
47+
extern void asm_fxrstor(mword *addr);
48+
49+
50+
#ifdef _M_IX86
51+
extern uint64 ASMCALL asm_vmread(uint32 component);
52+
extern void ASMCALL asm_vmwrite(uint32 component, uint32 val);
53+
extern void ASMCALL asm_rdmsr(uint32 reg, struct qword_val *qv);
54+
extern void ASMCALL asm_wrmsr(uint32 reg, struct qword_val *qv);
55+
extern void ASMCALL asm_rdtsc(struct qword_val *qv);
56+
#else // !_M_IX86
57+
extern uint32 ASMCALL asm_vmread(uint32 component);
58+
extern void ASMCALL asm_vmwrite(uint32 component, uint64 val);
59+
extern uint64 ASMCALL asm_rdmsr(uint32 reg);
60+
extern void ASMCALL asm_wrmsr(uint32 reg, uint64_t val);
61+
extern uint64 ASMCALL asm_rdtsc();
62+
#endif // _M_IX86
63+
64+
uint64 ia32_rdmsr(uint32 reg)
65+
{
66+
#ifdef _M_IX86
67+
struct qword_val val = { 0 };
68+
69+
asm_rdmsr(reg, &val);
70+
return ((uint64)(val.low) | (uint64)(val.high) << 32);
71+
#else
72+
return asm_rdmsr(reg);
73+
#endif
74+
}
75+
76+
void ia32_wrmsr(uint32 reg, uint64 val)
77+
{
78+
#ifdef _M_IX86
79+
struct qword_val tmp = { 0 };
80+
81+
tmp.high = (uint32)(val >> 32);
82+
tmp.low = (uint32)val;
83+
asm_wrmsr(reg, &tmp);
84+
#else
85+
asm_wrmsr(reg, val);
86+
#endif
87+
}
88+
89+
uint64 rdtsc(void)
90+
{
91+
#ifdef _M_IX86
92+
struct qword_val val = { 0 };
93+
asm_rdtsc(&val);
94+
return ((uint64)(val.low) | (uint64)(val.high) << 32);
95+
#else
96+
return asm_rdtsc();
97+
#endif
98+
}
99+
100+
void fxinit(void)
101+
{
102+
asm_fxinit();
103+
}
104+
105+
void fxsave(mword *addr)
106+
{
107+
asm_fxsave(addr);
108+
}
109+
110+
void fxrstor(mword *addr)
111+
{
112+
asm_fxrstor(addr);
113+
}
114+
115+
void btr(uint8 *addr, uint bit)
116+
{
117+
// asm_btr() may not be able to handle bit offsets greater than 0xff. For
118+
// absolute safety, ensure that the bit offset is less than 8.
119+
uint8 *base = addr + bit / 8;
120+
uint offset = bit % 8;
121+
asm_btr(base, offset);
122+
}
123+
124+
void bts(uint8 *addr, uint bit)
125+
{
126+
uint8 *base = addr + bit / 8;
127+
uint offset = bit % 8;
128+
asm_bts(base, offset);
129+
}
130+
131+
void _vmx_vmwrite(struct vcpu_t *vcpu, const char *name,
132+
component_index_t component,
133+
mword source_val)
134+
{
135+
asm_vmwrite(component, source_val);
136+
}
137+
138+
void _vmx_vmwrite_64(struct vcpu_t *vcpu, const char *name,
139+
component_index_t component,
140+
uint64 source_val)
141+
{
142+
#ifdef _M_IX86
143+
asm_vmwrite(component, (uint32)source_val);
144+
asm_vmwrite(component + 1, (uint32)(source_val >> 32));
145+
#else
146+
asm_vmwrite(component, source_val);
147+
#endif
148+
}
149+
150+
void _vmx_vmwrite_natural(struct vcpu_t *vcpu, const char *name,
151+
component_index_t component,
152+
uint64 source_val)
153+
{
154+
#ifdef _M_IX86
155+
asm_vmwrite(component, (uint32)source_val);
156+
#else
157+
asm_vmwrite(component, source_val);
158+
#endif
159+
}
160+
161+
uint64 vmx_vmread(struct vcpu_t *vcpu, component_index_t component)
162+
{
163+
uint64 val = 0;
164+
165+
val = asm_vmread(component);
166+
return val;
167+
}
168+
169+
uint64 vmx_vmread_natural(struct vcpu_t *vcpu, component_index_t component)
170+
{
171+
uint64 val = 0;
172+
173+
val = asm_vmread(component);
174+
return val;
175+
}
176+
177+
uint64 vmx_vmread_64(struct vcpu_t *vcpu, component_index_t component)
178+
{
179+
uint64 val = 0;
180+
181+
val = asm_vmread(component);
182+
#ifdef _M_IX86
183+
val |= ((uint64)(asm_vmread(component + 1)) << 32);
184+
#endif
185+
return val;
186+
}
187+
188+
void hax_enable_irq(void)
189+
{
190+
asm_enable_irq();
191+
}
192+
193+
void hax_disable_irq(void)
194+
{
195+
asm_disable_irq();
196+
}
File renamed without changes.

core/include/ia32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ enum {
239239
VECTOR_MF = 16, // Floating-Point Error (Math Error)
240240
VECTOR_AC = 17, // Alignment Check
241241
VECTOR_MC = 18, // Machine Check
242-
VECTOR_XM = 19, // SIMG Floating-Point Numeric Error
242+
VECTOR_XM = 19, // SIMD Floating-Point Numeric Error
243243
VECTOR_VE = 20 // Virtualization Exception
244244
};
245245

darwin/hax_driver/com_intel_hax/hax_wrapper.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,6 @@ extern "C" uint32_t hax_cpuid()
105105
return cpu_number();
106106
}
107107

108-
extern "C" void hax_enable_irq(void)
109-
{
110-
ml_set_interrupts_enabled(true);
111-
}
112-
113-
extern "C" void hax_disable_irq(void)
114-
{
115-
ml_set_interrupts_enabled(false);
116-
}
117-
118108
extern "C" void hax_disable_preemption(preempt_flag *eflags)
119109
{
120110
mword flags;

darwin/hax_driver/com_intel_hax/intelhaxm.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
64B72B851EDFFF7E00A8C202 /* hax_host_mem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 64B72B841EDFFF7E00A8C202 /* hax_host_mem.cpp */; };
4646
64B85BE91EF4D34D00223ABD /* ept2.c in Sources */ = {isa = PBXBuildFile; fileRef = 64B85BE81EF4D34D00223ABD /* ept2.c */; };
4747
64BB0CD220F36C470064593A /* vmx_ops.asm in Sources */ = {isa = PBXBuildFile; fileRef = 64BB0CD020F36C470064593A /* vmx_ops.asm */; };
48-
64BB0CD320F36C470064593A /* ia32.asm in Sources */ = {isa = PBXBuildFile; fileRef = 64BB0CD120F36C470064593A /* ia32.asm */; };
48+
64BB0CD320F36C470064593A /* ia32_ops.asm in Sources */ = {isa = PBXBuildFile; fileRef = 64BB0CD120F36C470064593A /* ia32_ops.asm */; };
4949
6E2DBBCC18EB6125003B66C9 /* page_walker.c in Sources */ = {isa = PBXBuildFile; fileRef = 6E2DBBCB18EB6125003B66C9 /* page_walker.c */; };
5050
6E2DBBCE18EB6155003B66C9 /* page_walker.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E2DBBCD18EB6155003B66C9 /* page_walker.h */; };
51+
A669096B20F9985300739075 /* ia32.c in Sources */ = {isa = PBXBuildFile; fileRef = A669096A20F9985300739075 /* ia32.c */; };
5152
B98ECFB613A059BB00485DDB /* cpu.c in Sources */ = {isa = PBXBuildFile; fileRef = B98ECF9C13A059BB00485DDB /* cpu.c */; };
5253
B98ECFB713A059BB00485DDB /* dump_vmcs.c in Sources */ = {isa = PBXBuildFile; fileRef = B98ECF9D13A059BB00485DDB /* dump_vmcs.c */; };
5354
B98ECFB813A059BB00485DDB /* hax.c in Sources */ = {isa = PBXBuildFile; fileRef = B98ECF9E13A059BB00485DDB /* hax.c */; };
@@ -133,9 +134,10 @@
133134
64B72B841EDFFF7E00A8C202 /* hax_host_mem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = hax_host_mem.cpp; sourceTree = "<group>"; };
134135
64B85BE81EF4D34D00223ABD /* ept2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ept2.c; path = ../../../core/ept2.c; sourceTree = "<group>"; };
135136
64BB0CD020F36C470064593A /* vmx_ops.asm */ = {isa = PBXFileReference; explicitFileType = sourcecode.nasm; name = vmx_ops.asm; path = ../../../core/vmx_ops.asm; sourceTree = "<group>"; };
136-
64BB0CD120F36C470064593A /* ia32.asm */ = {isa = PBXFileReference; explicitFileType = sourcecode.nasm; name = ia32.asm; path = ../../../core/ia32.asm; sourceTree = "<group>"; };
137+
64BB0CD120F36C470064593A /* ia32_ops.asm */ = {isa = PBXFileReference; explicitFileType = sourcecode.nasm; name = ia32_ops.asm; path = ../../../core/ia32_ops.asm; sourceTree = "<group>"; };
137138
6E2DBBCB18EB6125003B66C9 /* page_walker.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = page_walker.c; path = ../../../core/page_walker.c; sourceTree = "<group>"; };
138139
6E2DBBCD18EB6155003B66C9 /* page_walker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = page_walker.h; sourceTree = "<group>"; };
140+
A669096A20F9985300739075 /* ia32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ia32.c; path = ../../../core/ia32.c; sourceTree = "<group>"; };
139141
B98ECF9C13A059BB00485DDB /* cpu.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = cpu.c; path = ../../../core/cpu.c; sourceTree = SOURCE_ROOT; };
140142
B98ECF9D13A059BB00485DDB /* dump_vmcs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = dump_vmcs.c; path = ../../../core/dump_vmcs.c; sourceTree = SOURCE_ROOT; };
141143
B98ECF9E13A059BB00485DDB /* hax.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = hax.c; path = ../../../core/hax.c; sourceTree = SOURCE_ROOT; };
@@ -211,8 +213,9 @@
211213
247142CAFF3F8F9811CA285C /* Source */ = {
212214
isa = PBXGroup;
213215
children = (
214-
64BB0CD120F36C470064593A /* ia32.asm */,
216+
64BB0CD120F36C470064593A /* ia32_ops.asm */,
215217
64BB0CD020F36C470064593A /* vmx_ops.asm */,
218+
A669096A20F9985300739075 /* ia32.c */,
216219
6496936E20D8AE0000C9BBAF /* cpuid.c */,
217220
6E2DBBCB18EB6125003B66C9 /* page_walker.c */,
218221
43038AD9145F94190014BEE6 /* memory.c */,
@@ -453,9 +456,10 @@
453456
22BFCFD213A59A6500AD9F0F /* intr_exc.c in Sources */,
454457
22BFCFD613A59A8200AD9F0F /* vtlb.c in Sources */,
455458
64B72B851EDFFF7E00A8C202 /* hax_host_mem.cpp in Sources */,
459+
A669096B20F9985300739075 /* ia32.c in Sources */,
456460
43038ADA145F94190014BEE6 /* memory.c in Sources */,
457461
645626211EEFF720005280EF /* ept_tree.c in Sources */,
458-
64BB0CD320F36C470064593A /* ia32.asm in Sources */,
462+
64BB0CD320F36C470064593A /* ia32_ops.asm in Sources */,
459463
6496936F20D8AE0000C9BBAF /* cpuid.c in Sources */,
460464
);
461465
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)