From f2b32d149ca034cf87d00481fe2fd9db1781c8d6 Mon Sep 17 00:00:00 2001 From: Alexandro Sanchez Bach Date: Mon, 18 Jun 2018 01:16:56 +0200 Subject: [PATCH] Ported ia32.asm to NASM --- core/haxlib.vcxproj | 1 + core/ia32.asm | 314 ++++++++++++++++++ .../intelhaxm.xcodeproj/project.pbxproj | 4 - windows/sources | 6 +- windows/sources.props | 2 - 5 files changed, 317 insertions(+), 10 deletions(-) create mode 100644 core/ia32.asm diff --git a/core/haxlib.vcxproj b/core/haxlib.vcxproj index 2c17559b..1e6d3707 100644 --- a/core/haxlib.vcxproj +++ b/core/haxlib.vcxproj @@ -180,6 +180,7 @@ Document + diff --git a/core/ia32.asm b/core/ia32.asm new file mode 100644 index 00000000..a0249905 --- /dev/null +++ b/core/ia32.asm @@ -0,0 +1,314 @@ +; +; Copyright (c) 2011 Intel Corporation +; Copyright (c) 2018 Alexandro Sanchez Bach +; +; 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. + +; +; Detect architecture +; +%ifidn __OUTPUT_FORMAT__, elf32 + %define __BITS__ 32 + %define __CONV__ x32_fastcall +%elifidn __OUTPUT_FORMAT__, win32 + %define __BITS__ 32 + %define __CONV__ x32_fastcall +%elifidn __OUTPUT_FORMAT__, macho32 + %define __BITS__ 32 + %define __CONV__ x32_fastcall +%elifidn __OUTPUT_FORMAT__, elf64 + %define __BITS__ 64 + %define __CONV__ x64_systemv +%elifidn __OUTPUT_FORMAT__, win64 + %define __BITS__ 64 + %define __CONV__ x64_microsoft +%elifidn __OUTPUT_FORMAT__, macho64 + %define __BITS__ 64 + %define __CONV__ x64_systemv +%endif + +; +; Describe calling convention +; +%ifidn __CONV__, x32_fastcall + %define reg_arg1_16 cx + %define reg_arg1_32 ecx + %define reg_arg1 reg_arg1_32 + %define reg_arg2_16 dx + %define reg_arg2_32 edx + %define reg_arg2 reg_arg2_32 + %define reg_ret_16 ax + %define reg_ret_32 eax + %define reg_ret reg_ret_32 +%elifidn __CONV__, x64_systemv + %define reg_arg1_16 di + %define reg_arg1_32 edi + %define reg_arg1_64 rdi + %define reg_arg1 reg_arg1_64 + %define reg_arg2_16 si + %define reg_arg2_32 esi + %define reg_arg2_64 rsi + %define reg_arg2 reg_arg2_64 + %define reg_ret_16 ax + %define reg_ret_32 eax + %define reg_ret_64 rax + %define reg_ret reg_ret_64 +%elifidn __CONV__, x64_microsoft + %define reg_arg1_16 cx + %define reg_arg1_32 ecx + %define reg_arg1_64 rcx + %define reg_arg1 reg_arg1_64 + %define reg_arg2_16 cx + %define reg_arg2_32 ecx + %define reg_arg2_64 rcx + %define reg_arg2 reg_arg2_64 + %define reg_ret_16 ax + %define reg_ret_32 eax + %define reg_ret_64 rax + %define reg_ret reg_ret_64 +%endif + +; +; Helpers +; +%macro function 1 + global %1 + %1: +%endmacro + +%macro function_get_reg 1 + function get_%+%1 + mov reg_ret, %1 + ret +%endmacro +%macro function_set_reg 1 + function set_%+%1 + mov %1, reg_arg1 + ret +%endmacro +%macro function_get_segment 1 + function get_kernel_%+%1 + mov reg_ret_16, %1 + ret +%endmacro +%macro function_set_segment 1 + function set_kernel_%+%1 + mov %1, reg_arg1_16 + ret +%endmacro + +section .text + +struc qword_struct + .lo resd 1 + .hi resd 1 +endstruc + +struc vcpu_state + ._rax resq 1 + ._rcx resq 1 + ._rdx resq 1 + ._rbx resq 1 + ._rsp resq 1 + ._rbp resq 1 + ._rsi resq 1 + ._rdi resq 1 + ._r8 resq 1 + ._r9 resq 1 + ._r10 resq 1 + ._r11 resq 1 + ._r12 resq 1 + ._r13 resq 1 + ._r14 resq 1 + ._r15 resq 1 +endstruc + +struc cpuid_args + ._eax resd 1 + ._ecx resd 1 + ._edx resd 1 + ._ebx resd 1 +endstruc + +function __nmi + int 2h + ret + +function __fls + bsr eax, ecx + ret + +function __handle_cpuid +%ifidn __BITS__, 64 + push rbx + mov r8, rcx + mov rax, [r8 + vcpu_state._rax] + mov rcx, [r8 + vcpu_state._rcx] + cpuid + mov [r8 + vcpu_state._rax], rax + mov [r8 + vcpu_state._rbx], rbx + mov [r8 + vcpu_state._rcx], rcx + mov [r8 + vcpu_state._rdx], rdx + pop rbx + ret +%else + push ebx + push esi + mov esi, reg_arg1 + mov eax, [esi + vcpu_state._rax] + mov ecx, [esi + vcpu_state._rcx] + cpuid + mov [esi + vcpu_state._rax], eax + mov [esi + vcpu_state._rbx], ebx + mov [esi + vcpu_state._rcx], ecx + mov [esi + vcpu_state._rdx], edx + pop esi + pop ebx + ret +%endif + +function asm_btr + lock btr [reg_arg1], reg_arg2 + ret + +function asm_bts + lock bts [reg_arg1], reg_arg2 + ret + +function asm_disable_irq + cli + ret + +function asm_enable_irq + sti + ret + +function asm_fxinit + finit + ret + +function asm_fxrstor + fxrstor [reg_arg1] + ret + +function asm_fxsave + fxsave [reg_arg1] + ret + +function asm_rdmsr +%ifidn __BITS__, 64 + mov rcx, reg_arg1 + rdmsr + shl rdx, 32 + or rax, rdx + ret +%else + mov ecx, reg_arg1 + rdmsr + mov [reg_arg2 + qword_struct.lo], eax + mov [reg_arg2 + qword_struct.hi], edx + ret +%endif + +function asm_rdtsc +%ifidn __BITS__, 64 + rdtsc + shl rdx, 32 + or rax, rdx + ret +%else + rdtsc + mov [reg_arg2 + qword_struct.lo], eax + mov [reg_arg2 + qword_struct.hi], edx + ret +%endif + +function asm_wrmsr +%ifidn __BITS__, 64 + push rbx + mov rbx, reg_arg2 + mov rcx, reg_arg1 + mov eax, ebx + mov rdx, rbx + shl rdx, 32 + wrmsr + push rbx + ret +%else + push edi + push esi + mov edi, [reg_arg2 + qword_struct.lo] + mov esi, [reg_arg2 + qword_struct.hi] + mov ecx, reg_arg1 + mov eax, edi + mov edx, esi + wrmsr + push esi + push edi + ret +%endif + +function get_kernel_rflags + pushf + pop ax + ret + +function_get_reg cr0 +function_get_reg cr2 +function_get_reg cr3 +function_get_reg cr4 +function_get_reg dr0 +function_get_reg dr1 +function_get_reg dr2 +function_get_reg dr3 +function_get_reg dr6 +function_get_reg dr7 + +function_set_reg cr0 +function_set_reg cr2 +function_set_reg cr3 +function_set_reg cr4 +function_set_reg dr0 +function_set_reg dr1 +function_set_reg dr2 +function_set_reg dr3 +function_set_reg dr6 +function_set_reg dr7 + +function_get_segment cs +function_get_segment ds +function_get_segment es +function_get_segment ss +function_get_segment gs +function_get_segment fs + +function_set_segment cs +function_set_segment ds +function_set_segment es +function_set_segment ss +function_set_segment gs +function_set_segment fs diff --git a/darwin/hax_driver/com_intel_hax/intelhaxm.xcodeproj/project.pbxproj b/darwin/hax_driver/com_intel_hax/intelhaxm.xcodeproj/project.pbxproj index 2f75a502..70ede399 100644 --- a/darwin/hax_driver/com_intel_hax/intelhaxm.xcodeproj/project.pbxproj +++ b/darwin/hax_driver/com_intel_hax/intelhaxm.xcodeproj/project.pbxproj @@ -35,7 +35,6 @@ 43C9A9E7138DDA93000A1071 /* hax_host.h in Headers */ = {isa = PBXBuildFile; fileRef = 43C9A9E6138DDA93000A1071 /* hax_host.h */; }; 43F857E013931E75008A93D6 /* com_intel_hax_mem.h in Headers */ = {isa = PBXBuildFile; fileRef = 43F857DE13931E75008A93D6 /* com_intel_hax_mem.h */; }; 43F857E113931E75008A93D6 /* com_intel_hax_mem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43F857DF13931E75008A93D6 /* com_intel_hax_mem.cpp */; }; - 4BCC4E0513FB6729005E4BE4 /* ia32.c in Sources */ = {isa = PBXBuildFile; fileRef = 4BCC4E0213FB6729005E4BE4 /* ia32.c */; }; 4BCC4E0613FB6729005E4BE4 /* segments.c in Sources */ = {isa = PBXBuildFile; fileRef = 4BCC4E0313FB6729005E4BE4 /* segments.c */; }; 4BCC4E0713FB6729005E4BE4 /* vmcs.c in Sources */ = {isa = PBXBuildFile; fileRef = 4BCC4E0413FB6729005E4BE4 /* vmcs.c */; }; 642FD41B20D9F74D00C197FF /* cpuid.h in Headers */ = {isa = PBXBuildFile; fileRef = 642FD41A20D9F74D00C197FF /* cpuid.h */; }; @@ -124,7 +123,6 @@ 43C9A9E6138DDA93000A1071 /* hax_host.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hax_host.h; sourceTree = ""; }; 43F857DE13931E75008A93D6 /* com_intel_hax_mem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = com_intel_hax_mem.h; sourceTree = ""; }; 43F857DF13931E75008A93D6 /* com_intel_hax_mem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = com_intel_hax_mem.cpp; sourceTree = ""; }; - 4BCC4E0213FB6729005E4BE4 /* ia32.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ia32.c; sourceTree = ""; }; 4BCC4E0313FB6729005E4BE4 /* segments.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = segments.c; sourceTree = ""; }; 4BCC4E0413FB6729005E4BE4 /* vmcs.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = vmcs.c; sourceTree = ""; }; 642FD41A20D9F74D00C197FF /* cpuid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cpuid.h; sourceTree = ""; }; @@ -274,7 +272,6 @@ 4BCC4E0113FB6729005E4BE4 /* asm */ = { isa = PBXGroup; children = ( - 4BCC4E0213FB6729005E4BE4 /* ia32.c */, 4BCC4E0313FB6729005E4BE4 /* segments.c */, 4BCC4E0413FB6729005E4BE4 /* vmcs.c */, ); @@ -462,7 +459,6 @@ 22BFCFCE13A59A4300AD9F0F /* ept.c in Sources */, 22BFCFD213A59A6500AD9F0F /* intr_exc.c in Sources */, 22BFCFD613A59A8200AD9F0F /* vtlb.c in Sources */, - 4BCC4E0513FB6729005E4BE4 /* ia32.c in Sources */, 4BCC4E0613FB6729005E4BE4 /* segments.c in Sources */, 64B72B851EDFFF7E00A8C202 /* hax_host_mem.cpp in Sources */, 4BCC4E0713FB6729005E4BE4 /* vmcs.c in Sources */, diff --git a/windows/sources b/windows/sources index f1e175a8..de33319d 100644 --- a/windows/sources +++ b/windows/sources @@ -19,13 +19,11 @@ SOURCES= hax_entry.c \ version.rc \ hax_host_mem.c -I386_SOURCES=i386\ia32.asm \ - i386\segments.asm \ +I386_SOURCES=i386\segments.asm \ i386\vmcs.asm \ i386\wrapper.c -AMD64_SOURCES=amd64\ia32.asm \ - amd64\segments.asm \ +AMD64_SOURCES=amd64\segments.asm \ amd64\vmcs.asm \ amd64\wrapper.c diff --git a/windows/sources.props b/windows/sources.props index 605310c5..efa7143a 100644 --- a/windows/sources.props +++ b/windows/sources.props @@ -50,13 +50,11 @@ version.rc - i386\ia32.asm i386\segments.asm i386\vmcs.asm i386\wrapper.c - amd64\ia32.asm amd64\segments.asm amd64\vmcs.asm amd64\wrapper.c