diff options
author | ionescu007 <aionescu+git@gmail.com> | 2016-08-30 01:06:08 +0300 |
---|---|---|
committer | ionescu007 <aionescu+git@gmail.com> | 2016-08-30 01:06:08 +0300 |
commit | d580e2e5b8bac8b814927242ce773f366db62fde (patch) | |
tree | 77467f05c8ac86c8ea3b19dc6f595c6eb817f113 | |
parent | d9177eedf1993573e06f36beb41455ac36a76d42 (diff) |
More portability.
Fix the last few stragglers.
-rw-r--r-- | ntint.h | 18 | ||||
-rw-r--r-- | shv.h | 68 | ||||
-rw-r--r-- | shv_x.h | 53 | ||||
-rw-r--r-- | shvos.c | 1 | ||||
-rw-r--r-- | shvutil.c | 6 | ||||
-rw-r--r-- | shvvmx.c | 20 | ||||
-rw-r--r-- | shvvmxhv.c | 14 | ||||
-rw-r--r-- | shvvp.c | 4 | ||||
-rw-r--r-- | vmx.h | 5 |
9 files changed, 99 insertions, 90 deletions
@@ -28,11 +28,13 @@ Environment: #define FORCEINLINE __forceinline #define C_ASSERT(x) static_assert(x, "Error") #define FIELD_OFFSET offsetof +#define UNREFERENCED_PARAMETER(x) (x) + +#ifndef TRUE #define TRUE 1 #define FALSE 0 -#define UNREFERENCED_PARAMETER(x) (x) +#endif -#define PAGE_SIZE 4096 #define KERNEL_STACK_SIZE 24 * 1024 typedef struct DECLSPEC_ALIGN(16) _M128A @@ -128,12 +130,12 @@ typedef struct DECLSPEC_ALIGN(16) _CONTEXT }; }; M128A VectorRegister[26]; - ULONG64 VectorControl; - ULONG64 DebugControl; - ULONG64 LastBranchToRip; - ULONG64 LastBranchFromRip; - ULONG64 LastExceptionToRip; - ULONG64 LastExceptionFromRip; + UINT64 VectorControl; + UINT64 DebugControl; + UINT64 LastBranchToRip; + UINT64 LastBranchFromRip; + UINT64 LastExceptionToRip; + UINT64 LastExceptionFromRip; } CONTEXT, *PCONTEXT; typedef union _LARGE_INTEGER @@ -24,9 +24,12 @@ Environment: #pragma warning(disable:4201) #pragma warning(disable:4214) -#include <intrin.h> +#ifdef _WIN64 #include <basetsd.h> +#endif +#include <intrin.h> #include "ntint.h" +#include "vmx.h" #include "shv_x.h" typedef struct _SHV_SPECIAL_REGISTERS @@ -76,9 +79,9 @@ C_ASSERT((FIELD_OFFSET(SHV_VP_DATA, Epdpt) % PAGE_SIZE) == 0); typedef struct _SHV_VP_STATE { PCONTEXT VpRegs; - ULONG_PTR GuestRip; - ULONG_PTR GuestRsp; - ULONG_PTR GuestEFlags; + uintptr_t GuestRip; + uintptr_t GuestRsp; + uintptr_t GuestEFlags; UINT16 ExitReason; UINT8 ExitVm; } SHV_VP_STATE, *PSHV_VP_STATE; @@ -91,12 +94,12 @@ ShvVmxEntry ( VOID _sldt ( - _In_ PUINT16 Ldtr + _In_ UINT16* Ldtr ); VOID _str ( - _In_ PUINT16 Tr + _In_ UINT16* Tr ); VOID @@ -143,5 +146,58 @@ ShvVpRestoreAfterLaunch ( VOID ); +// +// OS Layer +// +DECLSPEC_NORETURN +VOID +__cdecl +ShvOsRestoreContext ( + _In_ PCONTEXT ContextRecord + ); + +VOID +ShvOsCaptureContext ( + _In_ PCONTEXT ContextRecord + ); + +INT32 +ShvOsGetActiveProcessorCount ( + VOID + ); + +INT32 +ShvOsGetCurrentProcessorNumber ( + VOID + ); + +VOID +ShvOsFreeContiguousAlignedMemory ( + _In_ VOID* BaseAddress + ); + +VOID* +ShvOsAllocateContigousAlignedMemory ( + _In_ size_t Size + ); + +UINT64 +ShvOsGetPhysicalAddress ( + _In_ VOID* BaseAddress + ); + +VOID +ShvOsDebugPrint ( + _In_ const char* Format, + ... + ); + +VOID +ShvOsRunCallbackOnProcessors ( + _In_ PSHV_CPU_CALLBACK Routine, + _In_opt_ VOID* Context + ); + + extern PSHV_VP_DATA* ShvGlobalData; @@ -23,8 +23,6 @@ Environment: #pragma once -#include "vmx.h" - #define SHV_STATUS_SUCCESS 0 #define SHV_STATUS_NOT_AVAILABLE -1 #define SHV_STATUS_NO_RESOURCES -2 @@ -32,7 +30,7 @@ Environment: typedef struct _SHV_CALLBACK_CONTEXT { - ULONG64 Cr3; + UINT64 Cr3; volatile long InitCount; INT32 FailedCpu; INT32 FailureStatus; @@ -45,55 +43,6 @@ SHV_CPU_CALLBACK ( ); typedef SHV_CPU_CALLBACK *PSHV_CPU_CALLBACK; -DECLSPEC_NORETURN -VOID -__cdecl -ShvOsRestoreContext ( - _In_ PCONTEXT ContextRecord - ); - -VOID -ShvOsCaptureContext ( - _In_ PCONTEXT ContextRecord - ); - -INT32 -ShvOsGetActiveProcessorCount ( - VOID - ); - -INT32 -ShvOsGetCurrentProcessorNumber ( - VOID - ); - -VOID -ShvOsFreeContiguousAlignedMemory ( - _In_ VOID* BaseAddress - ); - -VOID* -ShvOsAllocateContigousAlignedMemory ( - _In_ SIZE_T Size - ); - -UINT64 -ShvOsGetPhysicalAddress ( - _In_ VOID* BaseAddress - ); - -VOID -ShvOsDebugPrint ( - _In_ const char* Format, - ... - ); - -VOID -ShvOsRunCallbackOnProcessors ( - _In_ PSHV_CPU_CALLBACK Routine, - _In_opt_ VOID* Context - ); - INT32 ShvLoad ( VOID @@ -67,6 +67,7 @@ typedef struct _SHV_DPC_CONTEXT #define KGDT64_R3_DATA 0x28 #define KGDT64_R3_CMTEB 0x50 +#define RPL_MASK 0x03 VOID ShvVmxCleanup ( @@ -36,7 +36,7 @@ ShvUtilConvertGdtEntry ( // Windows does not use an LDT for these selectors in kernel, so the TI bit // should never be set. // - gdtEntry = (PKGDTENTRY64)((UINT_PTR)GdtBase + (Selector & ~RPL_MASK)); + gdtEntry = (PKGDTENTRY64)((uintptr_t)GdtBase + (Selector & ~RPL_MASK)); // // Write the selector directly @@ -59,9 +59,9 @@ ShvUtilConvertGdtEntry ( // VmxGdtEntry->Base = ((gdtEntry->Bytes.BaseHigh << 24) | (gdtEntry->Bytes.BaseMiddle << 16) | - (gdtEntry->BaseLow)) & MAXUINT32; + (gdtEntry->BaseLow)) & ULONG_MAX; VmxGdtEntry->Base |= ((gdtEntry->Bits.Type & 0x10) == 0) ? - ((ULONG_PTR)gdtEntry->BaseUpper << 32) : 0; + ((uintptr_t)gdtEntry->BaseUpper << 32) : 0; // // Load the access rights @@ -49,7 +49,7 @@ ShvVmxEptInitialize ( // // Construct EPT identity map for every 1GB of RAM // - __stosq((PULONG64)VpData->Epdpt, tempEpdpte.AsUlonglong, PDPTE_ENTRY_COUNT); + __stosq((UINT64*)VpData->Epdpt, tempEpdpte.AsUlonglong, PDPTE_ENTRY_COUNT); for (i = 0; i < PDPTE_ENTRY_COUNT; i++) VpData->Epdpt[i].PageFrameNumber = i; } @@ -169,7 +169,7 @@ ShvVmxSetupVmcsForVp ( // // Begin by setting the link pointer to the required value for 4KB VMCS. // - __vmx_vmwrite(VMCS_LINK_POINTER, MAXULONG64); + __vmx_vmwrite(VMCS_LINK_POINTER, ~0ULL); // // Configure the EPTP @@ -330,16 +330,16 @@ ShvVmxSetupVmcsForVp ( // // Now load the GDT itself // - __vmx_vmwrite(GUEST_GDTR_BASE, (ULONG_PTR)state->Gdtr.Base); + __vmx_vmwrite(GUEST_GDTR_BASE, (uintptr_t)state->Gdtr.Base); __vmx_vmwrite(GUEST_GDTR_LIMIT, state->Gdtr.Limit); - __vmx_vmwrite(HOST_GDTR_BASE, (ULONG_PTR)state->Gdtr.Base); + __vmx_vmwrite(HOST_GDTR_BASE, (uintptr_t)state->Gdtr.Base); // // And then the IDT // - __vmx_vmwrite(GUEST_IDTR_BASE, (ULONG_PTR)state->Idtr.Base); + __vmx_vmwrite(GUEST_IDTR_BASE, (uintptr_t)state->Idtr.Base); __vmx_vmwrite(GUEST_IDTR_LIMIT, state->Idtr.Limit); - __vmx_vmwrite(HOST_IDTR_BASE, (ULONG_PTR)state->Idtr.Base); + __vmx_vmwrite(HOST_IDTR_BASE, (uintptr_t)state->Idtr.Base); // // Load CR0 @@ -374,8 +374,8 @@ ShvVmxSetupVmcsForVp ( // corresponds exactly to the location where RtlCaptureContext will return // to inside of ShvVpInitialize. // - __vmx_vmwrite(GUEST_RSP, (ULONG_PTR)VpData->ShvStackLimit + KERNEL_STACK_SIZE - sizeof(CONTEXT)); - __vmx_vmwrite(GUEST_RIP, (ULONG_PTR)ShvVpRestoreAfterLaunch); + __vmx_vmwrite(GUEST_RSP, (uintptr_t)VpData->ShvStackLimit + KERNEL_STACK_SIZE - sizeof(CONTEXT)); + __vmx_vmwrite(GUEST_RIP, (uintptr_t)ShvVpRestoreAfterLaunch); __vmx_vmwrite(GUEST_RFLAGS, context->EFlags); // @@ -388,8 +388,8 @@ ShvVmxSetupVmcsForVp ( // the ones that RtlCaptureContext will perform. // C_ASSERT((KERNEL_STACK_SIZE - sizeof(CONTEXT)) % 16 == 0); - __vmx_vmwrite(HOST_RSP, (ULONG_PTR)VpData->ShvStackLimit + KERNEL_STACK_SIZE - sizeof(CONTEXT)); - __vmx_vmwrite(HOST_RIP, (ULONG_PTR)ShvVmxEntry); + __vmx_vmwrite(HOST_RSP, (uintptr_t)VpData->ShvStackLimit + KERNEL_STACK_SIZE - sizeof(CONTEXT)); + __vmx_vmwrite(HOST_RIP, (uintptr_t)ShvVmxEntry); } UINT8 @@ -38,13 +38,13 @@ ShvVmxResume ( __vmx_vmresume(); } -ULONG_PTR +uintptr_t FORCEINLINE ShvVmxRead ( _In_ UINT32 VmcsFieldId ) { - SIZE_T FieldData; + size_t FieldData; // // Because VMXREAD returns an error code, and not the data, it is painful @@ -215,12 +215,12 @@ ShvVmxEntryHandler ( // was actually pushed on the stack right before the call. Go dig into the // stack to find it, and overwrite the bogus value that's there now. // - Context->Rcx = *(PULONG64)((ULONG_PTR)Context - sizeof(Context->Rcx)); + Context->Rcx = *(UINT64*)((uintptr_t)Context - sizeof(Context->Rcx)); // // Get the per-VP data for this processor. // - vpData = (VOID*)((ULONG_PTR)(Context + 1) - KERNEL_STACK_SIZE); + vpData = (VOID*)((uintptr_t)(Context + 1) - KERNEL_STACK_SIZE); // // Build a little stack context to make it easier to keep track of certain @@ -250,8 +250,8 @@ ShvVmxEntryHandler ( // Return the VP Data structure in RAX:RBX which is going to be part of // the CPUID response that the caller (ShvVpUninitialize) expects back. // - Context->Rax = (ULONG_PTR)vpData >> 32; - Context->Rbx = (ULONG_PTR)vpData & 0xFFFFFFFF; + Context->Rax = (uintptr_t)vpData >> 32; + Context->Rbx = (uintptr_t)vpData & 0xFFFFFFFF; // // When running in VMX root mode, the processor will set limits of the @@ -307,7 +307,7 @@ ShvVmxEntryHandler ( // needed as RtlRestoreContext will fix all the GPRs, and what we just // did to RSP will take care of the rest. // - Context->Rip = (ULONG64)ShvVmxResume; + Context->Rip = (UINT64)ShvVmxResume; } // @@ -92,7 +92,7 @@ ShvVpRestoreAfterLaunch ( // same stack as the hypervisor (using no real stack space except the home // registers), so we can retrieve the VP the same way the hypervisor does. // - vpData = (PSHV_VP_DATA)((ULONG_PTR)_AddressOfReturnAddress() + + vpData = (PSHV_VP_DATA)((uintptr_t)_AddressOfReturnAddress() + sizeof(CONTEXT) - KERNEL_STACK_SIZE); @@ -182,7 +182,7 @@ ShvVpAllocateData ( // // Zero out the entire data region // - __stosq((PULONG64)data, 0, sizeof(*data) / sizeof(ULONG64)); + __stosq((UINT64*)data, 0, sizeof(*data) / sizeof(UINT64)); } // @@ -31,6 +31,7 @@ Environment: #define RPL_MASK 3 #define MTRR_TYPE_WB 6 #define EFLAGS_ALIGN_CHECK 0x40000 +#define PAGE_SIZE 4096 typedef struct _KDESCRIPTOR { @@ -409,7 +410,7 @@ enum vmcs_field { typedef struct _VMX_GDTENTRY64 { - ULONG_PTR Base; + uintptr_t Base; UINT32 Limit; union { @@ -441,7 +442,7 @@ typedef struct _VMX_GDTENTRY64 UINT16 Selector; } VMX_GDTENTRY64, *PVMX_GDTENTRY64; -typedef struct DECLSPEC_ALIGN(PAGE_SIZE) _VMX_VMCS +typedef struct _VMX_VMCS { UINT32 RevisionId; UINT32 AbortIndicator; |