From 0935aedc251045de8b74512262c0afa4a14c5d89 Mon Sep 17 00:00:00 2001 From: Zero Tang Date: Wed, 27 Jan 2021 00:56:05 +0800 Subject: [PATCH] Update by Jan.26th, 2021 Replace the compiler from WDK7.1 to EWDK10-2004. The code will be compliant to C99 instead of Microsoft C98. Add spectre-mitigation by compiler. Add "Release/Free" compilation preset. Add cleanup script. --- .gitignore | 4 +++- README.md | 11 +++++++---- build_prep.bat | 8 +++++--- cleanup.bat | 15 +++++++++++++++ compchk_win10x64.bat | 33 +++++++++++++++++++++++++++++++++ compchk_win7x64.bat | 32 -------------------------------- compfre_win10x64.bat | 33 +++++++++++++++++++++++++++++++++ src/mars.c | 1 + src/vcpu.c | 7 ++----- 9 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 cleanup.bat create mode 100644 compchk_win10x64.bat delete mode 100644 compchk_win7x64.bat create mode 100644 compfre_win10x64.bat diff --git a/.gitignore b/.gitignore index 49a815f..6c41ff0 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,6 @@ modules.order Module.symvers Mkfile.old -dkms.conf \ No newline at end of file +dkms.conf + +bin/ \ No newline at end of file diff --git a/README.md b/README.md index d6115c3..59d2186 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ To virtualize the processor, we implement the VMCB structure. VMCB is the acrony - Register File, including GPR and FPU - Execution Control, controlling the behavior of vCPU -Duly note that mipsivm simulates in LE byte order. +Duly note that mipsivm simulates in `Little-Endian` byte order. ### Glossary of Processor Virtualization | Acronyms | Abbreviated for | Explanation | @@ -70,6 +70,7 @@ Each PTE is an 8-byte entry. There are 512 PTEs. For each entry, there are 64 bi | PTE | Page-Table Entry | The first level of address-translation | | PDE | Page-Directory Entry | The second level of address-translation | | PDPTE | Page-Directory Pointer Table Entry | The third level of address-translation | +| XTLB | Execution Translate Lookaside Buffer | TLB that accelerates instruction fetching | ## FAQ Following lists the frequently asked questions, and corresponding answers. @@ -82,6 +83,7 @@ Yes, there is. The famous PCSX2 is a free and open-source PlayStation 2 (PS2) em ### What big difference does this project make in comparison to CSE3666 assignment? - There is access-controlled memory-virtualization feature. +- There is a simple implementation of TLB for instruction fetching. - This project is written in C programming language. - This software is implemented by inspirations from AMD64 architecture. - This project aims to support more instructions. @@ -92,8 +94,9 @@ If it is your first time to build mipsivm, you will have to execute `build_prep. Currently, mipsivm supports 64-bit Windows. ### Windows -To build mipsivm for Windows, you will need to install [Windows Driver Kit 7.1.0](https://www.microsoft.com/en-us/download/details.aspx?id=11800) to default path on C drive. Compiler of WDK 7.1.0 can be considered as Visual C++ 2008 with a minor update.
-Execute `compchk_win7x64.bat` to compile mipsivm with `Debug/Checked` (unoptimized) compilation preset. +To build mipsivm for Windows, you will need to install [Enterprise Windows Driver Kit 10](https://docs.microsoft.com/en-us/legal/windows/hardware/enterprise-wdk-license-2019) and mount it to T disk. Make sure you downloaded EWDK 10 version 2004 with Visual Studio Tools 16.7.
+Execute `compchk_win10x64.bat` to compile mipsivm with `Debug/Checked` (unoptimized) compilation preset.
+Execute `compfre_win10x64.bat` to compile mipsivm with `Release/Free` (optimized) compilation preset. ## Command-Line Argument Here list all command-line arguments. @@ -104,7 +107,7 @@ Here list all command-line arguments. Following runtime presets are available: ### MARS Runtime Preset -MARS, acronym that stands for MIPS Assembler and Runtime Simulator, is an MIPS Assembly Development IDE written by Dr. Pete Sanderson and Dr.Ken Vollmar at Missouri State University. This preset would let mipsivm to simulate the MARS environment.
+MARS, acronym that stands for MIPS Assembler and Runtime Simulator, is an MIPS Assembly Development IDE written by Dr. Pete Sanderson and Dr. Ken Vollmar at Missouri State University. This preset would let mipsivm to simulate the MARS environment.
MARS runtime preset simulates the program in user-mode. It is a uniprocessor preset - no multi-processing is available.
Here list command-line arguments specific to MARS Runtime Preset. diff --git a/build_prep.bat b/build_prep.bat index 358de23..88f778b 100644 --- a/build_prep.bat +++ b/build_prep.bat @@ -1,14 +1,16 @@ @echo off -title Compiling mipsivm, Checked Build, 64-Bit Windows (AMD64 Architecture) +title Preparing Compilation of Project mipsivm echo Project: mipsivm echo Platform: Universal echo Preset: Preparation pause mkdir .\bin -mkdir .\bin\compchk_win7x64 -mkdir .\bin\compchk_win7x64\Intermediate +mkdir .\bin\compchk_win10x64 +mkdir .\bin\compchk_win10x64\Intermediate +mkdir .\bin\compfre_win10x64 +mkdir .\bin\compfre_win10x64\Intermediate echo Completed! pause. \ No newline at end of file diff --git a/cleanup.bat b/cleanup.bat new file mode 100644 index 0000000..9813bce --- /dev/null +++ b/cleanup.bat @@ -0,0 +1,15 @@ +@echo off + +title mipsivm Cleanup +echo Project: mipsivm +echo Platform: Universal (Non-Binary Build) +echo Preset: Cleanup +echo Powered by zero.tangptr@gmail.com +echo Warning: All compiled binaries, including intermediate files, will be deleted! +pause. + +echo Performing cleanup... +del .\bin /q /s + +echo Cleanup Completed! +pause. \ No newline at end of file diff --git a/compchk_win10x64.bat b/compchk_win10x64.bat new file mode 100644 index 0000000..ee44df6 --- /dev/null +++ b/compchk_win10x64.bat @@ -0,0 +1,33 @@ +@echo off +set ddkpath=T:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110 +set path=%ddkpath%\bin\Hostx64\x64;T:\Program Files\Windows Kits\10\bin\10.0.19041.0\x64;%path% +set incpath=T:\Program Files\Windows Kits\10\Include\10.0.19041.0 +set libpath=T:\Program Files\Windows Kits\10\Lib\10.0.19041.0 +set binpath=.\bin\compchk_win10x64 +set objpath=.\bin\compchk_win10x64\Intermediate + +title Compiling mipsivm, Checked Build, 64-Bit Windows (AMD64 Architecture) +echo Project: mipsivm +echo Platform: 64-Bit Windows +echo Preset: Debug/Checked Build +pause + +echo ============Start Compiling============ +cl .\src\xpf\windows\system.c /I"%incpath%\shared" /I"%incpath%\ucrt" /I"%incpath%\um" /I"%ddkpath%\include" /I".\src\include" /Zi /nologo /W3 /WX /Od /D"_MBCS" /D"_WIN64" /D"_M_AMD64" /D"_AMD64_" /D"_CRT_SECURE_NO_WARNINGS" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\system.cod" /Fo"%objpath%\system.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\entry.c /I"%incpath%\shared" /I"%incpath%\ucrt" /I"%incpath%\um" /I"%ddkpath%\include" /I".\src\include" /Zi /nologo /W3 /WX /Od /D"_msvc" /D"_amd64" /D"_CRT_SECURE_NO_WARNINGS" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\entry.cod" /Fo"%objpath%\entry.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\vcpu.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_hvm" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\vcpu.cod" /Fo"%objpath%\vcpu.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\mars.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_hvm_mars" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\mars.cod" /Fo"%objpath%\mars.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\sim-r.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_simr" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-r.cod" /Fo"%objpath%\sim-r.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\sim-i.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_simi" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-i.cod" /Fo"%objpath%\sim-i.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\sim-j.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_simj" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-j.cod" /Fo"%objpath%\sim-j.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +echo ============Start Linking============ +link "%objpath%\*.obj" /LIBPATH:"%libpath%\um\x64" /LIBPATH:"%libpath%\ucrt\x64" /LIBPATH:"%ddkpath%\lib\x64" /NOLOGO /DEBUG /PDB:"%objpath%\mipsivm.pdb" /INCREMENTAL:NO /OUT:"%binpath%\mipsivm.exe" /SUBSYSTEM:CONSOLE /Machine:X64 /ERRORREPORT:QUEUE + +pause \ No newline at end of file diff --git a/compchk_win7x64.bat b/compchk_win7x64.bat deleted file mode 100644 index 677c4e9..0000000 --- a/compchk_win7x64.bat +++ /dev/null @@ -1,32 +0,0 @@ -@echo off -set ddkpath=C:\WinDDK\7600.16385.1\bin\x86 -set incpath=C:\WinDDK\7600.16385.1\inc -set libpath=C:\WinDDK\7600.16385.1\lib -set binpath=.\bin\compchk_win7x64 -set objpath=.\bin\compchk_win7x64\Intermediate - -title Compiling mipsivm, Checked Build, 64-Bit Windows (AMD64 Architecture) -echo Project: mipsivm -echo Platform: 64-Bit Windows -echo Preset: Debug/Checked Build -pause - -echo ============Start Compiling============ -%ddkpath%\amd64\cl.exe .\src\xpf\windows\system.c /I"%incpath%\api" /I"%incpath%\crt" /I".\src\include" /Zi /nologo /W3 /WX /Od /D"_MBCS" /D"_WIN64" /D"_M_AMD64" /D"_AMD64_" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\system.cod" /Fo"%objpath%\system.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -%ddkpath%\amd64\cl.exe .\src\entry.c /I"%incpath%\api" /I"%incpath%\crt" /I".\src\include" /Zi /nologo /W3 /WX /Od /D"_msvc" /D"_amd64" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\entry.cod" /Fo"%objpath%\entry.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -%ddkpath%\amd64\cl.exe .\src\vcpu.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_hvm" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\vcpu.cod" /Fo"%objpath%\vcpu.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -%ddkpath%\amd64\cl.exe .\src\mars.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_hvm_mars" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\mars.cod" /Fo"%objpath%\mars.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -%ddkpath%\amd64\cl.exe .\src\sim-r.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_simr" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-r.cod" /Fo"%objpath%\sim-r.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -%ddkpath%\amd64\cl.exe .\src\sim-i.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_simi" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-i.cod" /Fo"%objpath%\sim-i.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -%ddkpath%\amd64\cl.exe .\src\sim-j.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /Od /D"_msvc" /D"_amd64" /D"_mips_simj" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-j.cod" /Fo"%objpath%\sim-j.obj" /Fd"%objpath%\vc90.pdb" /GS- /Gd /TC /c /errorReport:queue - -echo ============Start Linking============ -%ddkpath%\amd64\link.exe "%objpath%\*.obj" /LIBPATH:"%libpath%\win7\amd64" /LIBPATH:"%libpath%\Crt\amd64" /NODEFAULTLIB "libcmt.lib" "msvcrt.lib" "kernel32.lib" /NOLOGO /DEBUG /PDB:"%objpath%\mipsivm.pdb" /INCREMENTAL:NO /OUT:"%binpath%\mipsivm.exe" /SUBSYSTEM:CONSOLE /Machine:X64 /ERRORREPORT:QUEUE - -pause \ No newline at end of file diff --git a/compfre_win10x64.bat b/compfre_win10x64.bat new file mode 100644 index 0000000..64139ca --- /dev/null +++ b/compfre_win10x64.bat @@ -0,0 +1,33 @@ +@echo off +set ddkpath=T:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110 +set path=%ddkpath%\bin\Hostx64\x64;T:\Program Files\Windows Kits\10\bin\10.0.19041.0\x64;%path% +set incpath=T:\Program Files\Windows Kits\10\Include\10.0.19041.0 +set libpath=T:\Program Files\Windows Kits\10\Lib\10.0.19041.0 +set binpath=.\bin\compfre_win10x64 +set objpath=.\bin\compfre_win10x64\Intermediate + +title Compiling mipsivm, Free Build, 64-Bit Windows (AMD64 Architecture) +echo Project: mipsivm +echo Platform: 64-Bit Windows +echo Preset: Release/Free Build +pause + +echo ============Start Compiling============ +cl .\src\xpf\windows\system.c /I"%incpath%\shared" /I"%incpath%\ucrt" /I"%incpath%\um" /I"%ddkpath%\include" /I".\src\include" /Zi /nologo /W3 /WX /O2 /D"_MBCS" /D"_WIN64" /D"_M_AMD64" /D"_AMD64_" /D"_CRT_SECURE_NO_WARNINGS" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\system.cod" /Fo"%objpath%\system.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\entry.c /I"%incpath%\shared" /I"%incpath%\ucrt" /I"%incpath%\um" /I"%ddkpath%\include" /I".\src\include" /Zi /nologo /W3 /WX /O2 /D"_msvc" /D"_amd64" /D"_CRT_SECURE_NO_WARNINGS" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\entry.cod" /Fo"%objpath%\entry.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\vcpu.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /O2 /D"_msvc" /D"_amd64" /D"_mips_hvm" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\vcpu.cod" /Fo"%objpath%\vcpu.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\mars.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /O2 /D"_msvc" /D"_amd64" /D"_mips_hvm_mars" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\mars.cod" /Fo"%objpath%\mars.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\sim-r.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /O2 /D"_msvc" /D"_amd64" /D"_mips_simr" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-r.cod" /Fo"%objpath%\sim-r.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\sim-i.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /O2 /D"_msvc" /D"_amd64" /D"_mips_simi" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-i.cod" /Fo"%objpath%\sim-i.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +cl .\src\sim-j.c /I".\src\include" /Zi /nologo /W3 /WX /Oi /O2 /D"_msvc" /D"_amd64" /D"_mips_simj" /Zc:wchar_t /Zc:forScope /FAcs /Fa"%objpath%\sim-j.cod" /Fo"%objpath%\sim-j.obj" /Fd"%objpath%\vc140.pdb" /Qspectre /GS- /Gd /TC /c /errorReport:queue + +echo ============Start Linking============ +link "%objpath%\*.obj" /LIBPATH:"%libpath%\um\x64" /LIBPATH:"%libpath%\ucrt\x64" /LIBPATH:"%ddkpath%\lib\x64" /NOLOGO /DEBUG /PDB:"%objpath%\mipsivm.pdb" /INCREMENTAL:NO /OUT:"%binpath%\mipsivm.exe" /SUBSYSTEM:CONSOLE /Machine:X64 /ERRORREPORT:QUEUE + +pause \ No newline at end of file diff --git a/src/mars.c b/src/mars.c index 61b95e5..682addc 100644 --- a/src/mars.c +++ b/src/mars.c @@ -188,6 +188,7 @@ void mips_run_mars_vm() sim_printf("Guest Ticks Elapsed: %lld\n",vm.vcpu.tsc); tsc_diff=t2-t1; sim_printf("Host Ticks Elapsed: %lld\n",tsc_diff); + sim_printf("XTLB Hits: %lld\tMisses: %lld\n",vm.vcpu.xtlb.hits,vm.vcpu.xtlb.misses); cpi=(double)vm.vcpu.tsc/(double)vm.vcpu.eic; sim_printf("Guest Program CPI: %f (Higher CPI indicates less efficiency in program)\n",cpi); ghr=(double)vm.vcpu.tsc/(double)tsc_diff; diff --git a/src/vcpu.c b/src/vcpu.c index a4e1f84..d1463c5 100644 --- a/src/vcpu.c +++ b/src/vcpu.c @@ -95,14 +95,12 @@ bool mips_npt_edit_entry(vmcb_p vcpu,u32 gpa,void* hva,bool r,bool w,bool x) void mips_npt_cleanup(vmcb_p vcpu) { #if defined(_amd64) - u32 i=0; - for(;i<4;i++) + for(u32 i=0;i<4;i++) { l3p_pde_p pdeb=(l3p_pde_p)(vcpu->npt_base[i].pde_page<<12); if(pdeb) { - u32 j=0; - for(;j<512;j++) + for(u32 j=0;j<512;j++) { l3p_pte_p pteb=(l3p_pte_p)(pdeb[j].pte_page<<12); if(pteb)free_page(pteb); @@ -145,7 +143,6 @@ void mips_vcpu_dump_state(vmcb_p vcpu) sim_printf("hi\t\t0x%08X\n",vcpu->gpr.v.hi); sim_printf("lo\t\t0x%08X\n",vcpu->gpr.v.lo); sim_printf("pc\t\t0x%08X\n",vcpu->gpr.pc); - sim_printf("XTLB Hits: %lld\tMisses: %lld\n",vcpu->xtlb.hits,vcpu->xtlb.misses); sim_printf("======================Dump Complete======================\n"); }