Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added annotations, improved const correctness #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions nt/shvos.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ VOID
__cdecl
RtlRestoreContext (
_In_ PCONTEXT ContextRecord,
_In_opt_ struct _EXCEPTION_RECORD * ExceptionRecord
_In_opt_ PEXCEPTION_RECORD ExceptionRecord
);

typedef struct _SHV_DPC_CONTEXT
Expand All @@ -70,14 +70,14 @@ typedef struct _SHV_DPC_CONTEXT

VOID
ShvVmxCleanup (
_In_ UINT16 Data,
_In_ UINT16 Teb
_In_ const UINT16 Data,
_In_ const UINT16 Teb
);

NTSTATUS
FORCEINLINE
ShvOsErrorToError (
INT32 Error
const INT32 Error
)
{
//
Expand Down Expand Up @@ -109,12 +109,12 @@ ShvOsErrorToError (
VOID
ShvOsDpcRoutine (
_In_ struct _KDPC *Dpc,
_In_opt_ PVOID DeferredContext,
_In_opt_ PVOID SystemArgument1,
_In_opt_ PVOID SystemArgument2
_In_ PVOID DeferredContext,
_In_ PVOID SystemArgument1,
_In_ PVOID SystemArgument2
)
{
PSHV_DPC_CONTEXT dpcContext = DeferredContext;
PSHV_DPC_CONTEXT const dpcContext = DeferredContext;
UNREFERENCED_PARAMETER(Dpc);

//
Expand Down Expand Up @@ -153,7 +153,7 @@ ShvOsDpcRoutine (

VOID
ShvOsPrepareProcessor (
_In_ PSHV_VP_DATA VpData
_In_ PCSHV_VP_DATA VpData
)
{
//
Expand All @@ -165,7 +165,7 @@ ShvOsPrepareProcessor (

VOID
ShvOsUnprepareProcessor (
_In_ PSHV_VP_DATA VpData
_In_ PCSHV_VP_DATA const VpData
)
{
//
Expand All @@ -180,9 +180,12 @@ ShvOsUnprepareProcessor (
__lidt(&VpData->SpecialRegisters.Idtr.Limit);
}

//
// Is there supposed to be a size parameter?
//
VOID
ShvOsFreeContiguousAlignedMemory (
_In_ PVOID BaseAddress
_In_ _Post_ptr_invalid_ PVOID const BaseAddress
)
{
//
Expand All @@ -191,9 +194,10 @@ ShvOsFreeContiguousAlignedMemory (
MmFreeContiguousMemory(BaseAddress);
}

_When_(return != NULL, _Post_writable_byte_size_(Size))
PVOID
ShvOsAllocateContigousAlignedMemory (
_In_ SIZE_T Size
_In_ SIZE_T const Size
)
{
PHYSICAL_ADDRESS lowest, highest;
Expand All @@ -218,19 +222,23 @@ ShvOsAllocateContigousAlignedMemory (

ULONGLONG
ShvOsGetPhysicalAddress (
_In_ PVOID BaseAddress
_In_ VOID *const BaseAddress
)
{
//
// Let the memory manager convert it
//

//
// is MmGetPhysicalAddress incorrectly non-const?
//
return MmGetPhysicalAddress(BaseAddress).QuadPart;
}

VOID
ShvOsRunCallbackOnProcessors (
_In_ PSHV_CPU_CALLBACK Routine,
_In_opt_ PVOID Context
_In_ SHV_CPU_CALLBACK *const Routine,
_Inout_opt_ PVOID const Context
)
{
SHV_DPC_CONTEXT dpcContext;
Expand All @@ -247,18 +255,22 @@ DECLSPEC_NORETURN
VOID
__cdecl
ShvOsRestoreContext (
_In_ PCONTEXT ContextRecord
_In_ PCONTEXT const ContextRecord
)
{
//
// Windows provides a nice OS function to do this
//

//
// is RtlRestoreContext correctly non-const?
//
RtlRestoreContext(ContextRecord, NULL);
}

VOID
ShvOsCaptureContext (
_In_ PCONTEXT ContextRecord
_Out_ PCONTEXT const ContextRecord
)
{
//
Expand Down Expand Up @@ -291,7 +303,7 @@ ShvOsGetActiveProcessorCount (

VOID
ShvOsDebugPrint (
_In_ PCCH Format,
_In_z_ _Printf_format_string_ PCCH const Format,
...
)
{
Expand All @@ -305,6 +317,8 @@ ShvOsDebugPrint (
va_end(arglist);
}

DRIVER_UNLOAD DriverUnload;

VOID
DriverUnload (
_In_ PDRIVER_OBJECT DriverObject
Expand All @@ -318,6 +332,8 @@ DriverUnload (
ShvUnload();
}

DRIVER_INITIALIZE DriverEntry;

NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
Expand Down
32 changes: 17 additions & 15 deletions shv.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct _SHV_VP_STATE
UINT16 ExitReason;
UINT8 ExitVm;
} SHV_VP_STATE, *PSHV_VP_STATE;
typedef const SHV_VP_STATE *PCSHV_VP_STATE;

typedef struct _SHV_CALLBACK_CONTEXT
{
Expand All @@ -60,19 +61,19 @@ ShvVmxEntry (

INT32
ShvVmxLaunchOnVp (
_In_ PSHV_VP_DATA VpData
_Inout_ PSHV_VP_DATA const VpData
);

VOID
ShvUtilConvertGdtEntry (
_In_ VOID* GdtBase,
_In_ UINT16 Offset,
_Out_ PVMX_GDTENTRY64 VmxGdtEntry
_In_ const UINT16 Offset,
_Out_ PVMX_GDTENTRY64 const VmxGdtEntry
);

UINT32
ShvUtilAdjustMsr (
_In_ LARGE_INTEGER ControlValue,
_In_ const LARGE_INTEGER ControlValue,
_In_ UINT32 DesiredValue
);

Expand All @@ -83,7 +84,7 @@ ShvVpAllocateData (

VOID
ShvVpFreeData (
_In_ PSHV_VP_DATA Data,
_In_ _Frees_ptr_ PSHV_VP_DATA Data,
_In_ UINT32 CpuCount
);

Expand All @@ -99,7 +100,7 @@ ShvVmxProbe (

VOID
ShvVmxEptInitialize (
_In_ PSHV_VP_DATA VpData
_Inout_ PSHV_VP_DATA const VpData
);

DECLSPEC_NORETURN
Expand All @@ -115,22 +116,22 @@ DECLSPEC_NORETURN
VOID
__cdecl
ShvOsRestoreContext (
_In_ PCONTEXT ContextRecord
_In_ PCONTEXT const ContextRecord
);

VOID
ShvOsCaptureContext (
_In_ PCONTEXT ContextRecord
_Out_ PCONTEXT ContextRecord
);

VOID
ShvOsUnprepareProcessor (
_In_ PSHV_VP_DATA VpData
_In_ PCSHV_VP_DATA VpData
);

INT32
ShvOsPrepareProcessor (
_In_ PSHV_VP_DATA VpData
_In_ PCSHV_VP_DATA VpData
);

INT32
Expand All @@ -145,10 +146,11 @@ ShvOsGetCurrentProcessorNumber (

VOID
ShvOsFreeContiguousAlignedMemory (
_In_ VOID* BaseAddress,
_In_ _Post_ptr_invalid_ VOID* BaseAddress,
_In_ size_t Size
);

_When_ (return != NULL, _Post_writable_byte_size_ (Size))
VOID*
ShvOsAllocateContigousAlignedMemory (
_In_ size_t Size
Expand All @@ -162,22 +164,22 @@ ShvOsGetPhysicalAddress (
#ifndef __BASE_H__
VOID
ShvOsDebugPrint (
_In_ const char* Format,
_In_z_ _Printf_format_string_ const char* Format,
...
);
#else
VOID
ShvOsDebugPrintWide (
_In_ const CHAR16* Format,
_In_z_ _Printf_format_string_ const CHAR16* Format,
...
);
#define ShvOsDebugPrint(format, ...) ShvOsDebugPrintWide(_CRT_WIDE(format), __VA_ARGS__)
#endif

VOID
ShvOsRunCallbackOnProcessors (
_In_ PSHV_CPU_CALLBACK Routine,
_In_opt_ VOID* Context
_In_ SHV_CPU_CALLBACK *Routine,
_Inout_opt_ VOID* Context
);

extern PSHV_VP_DATA* ShvGlobalData;
Expand Down
10 changes: 6 additions & 4 deletions shv_x.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct _SHV_CALLBACK_CONTEXT;
typedef
void
SHV_CPU_CALLBACK (
struct _SHV_CALLBACK_CONTEXT* Context
_Inout_ struct _SHV_CALLBACK_CONTEXT* const Context
);
typedef SHV_CPU_CALLBACK *PSHV_CPU_CALLBACK;

Expand All @@ -52,6 +52,7 @@ typedef struct _SHV_SPECIAL_REGISTERS
KDESCRIPTOR Idtr;
KDESCRIPTOR Gdtr;
} SHV_SPECIAL_REGISTERS, *PSHV_SPECIAL_REGISTERS;
typedef const SHV_SPECIAL_REGISTERS *PCSHV_SPECIAL_REGISTERS;

typedef struct _SHV_VP_DATA
{
Expand Down Expand Up @@ -79,12 +80,13 @@ typedef struct _SHV_VP_DATA
DECLSPEC_ALIGN(PAGE_SIZE) VMX_VMCS VmxOn;
DECLSPEC_ALIGN(PAGE_SIZE) VMX_VMCS Vmcs;
} SHV_VP_DATA, *PSHV_VP_DATA;
typedef const SHV_VP_DATA *PCSHV_VP_DATA;

C_ASSERT(sizeof(SHV_VP_DATA) == (KERNEL_STACK_SIZE + 5 * PAGE_SIZE));

VOID
_sldt (
_In_ UINT16* Ldtr
_Out_ UINT16* Ldtr
);

VOID
Expand All @@ -94,12 +96,12 @@ _ltr (

VOID
_str (
_In_ UINT16* Tr
_Out_ UINT16* Tr
);

VOID
__lgdt (
_In_ VOID* Gdtr
_In_ const VOID* Gdtr
);

INT32
Expand Down
8 changes: 4 additions & 4 deletions shvutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Module Name:

VOID
ShvUtilConvertGdtEntry (
_In_ VOID* GdtBase,
_In_ UINT16 Selector,
_Out_ PVMX_GDTENTRY64 VmxGdtEntry
_In_ VOID* const GdtBase,
_In_ const UINT16 Selector,
_Out_ PVMX_GDTENTRY64 const VmxGdtEntry
)
{
PKGDTENTRY64 gdtEntry;
Expand Down Expand Up @@ -90,7 +90,7 @@ ShvUtilConvertGdtEntry (

UINT32
ShvUtilAdjustMsr (
_In_ LARGE_INTEGER ControlValue,
_In_ const LARGE_INTEGER ControlValue,
_In_ UINT32 DesiredValue
)
{
Expand Down
Loading