HyperDbg Debugger
Loading...
Searching...
No Matches
Segmentation.h File Reference

Functions for handling memory segmentations. More...

Go to the source code of this file.

Macros

#define USE_DEFAULT_OS_GDT_AS_HOST_GDT   FALSE
 Whether the hypervisor should use the default OS's GDT as the host GDT in VMCS or not.
 
#define HOST_GDT_DESCRIPTOR_COUNT   10
 Maximum number of entries in GDT.
 
#define HOST_INTERRUPT_STACK_SIZE   4 * PAGE_SIZE
 Size of host interrupt stack.
 
#define USE_INTERRUPT_STACK_TABLE   FALSE
 Use Interrupt Stack Table (IST1..IST7)
 

Functions

 _Success_ (return) BOOLEAN SegmentGetDescriptor(_In_ PUCHAR GdtBase
 
VOID SegmentPrepareHostGdt (SEGMENT_DESCRIPTOR_32 *OsGdtBase, UINT16 OsGdtLimit, UINT16 TrSelector, UINT64 HostStack, SEGMENT_DESCRIPTOR_32 *AllocatedHostGdt, TASK_STATE_SEGMENT_64 *AllocatedHostTss)
 Initialize the host GDT.
 

Variables

_In_ UINT16 Selector
 
_In_ UINT16 _Out_ PVMX_SEGMENT_SELECTOR SegmentSelector
 

Detailed Description

Functions for handling memory segmentations.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.9
Date
2024-06-03

Macro Definition Documentation

◆ HOST_GDT_DESCRIPTOR_COUNT

#define HOST_GDT_DESCRIPTOR_COUNT   10

Maximum number of entries in GDT.

◆ HOST_INTERRUPT_STACK_SIZE

#define HOST_INTERRUPT_STACK_SIZE   4 * PAGE_SIZE

Size of host interrupt stack.

◆ USE_DEFAULT_OS_GDT_AS_HOST_GDT

#define USE_DEFAULT_OS_GDT_AS_HOST_GDT   FALSE

Whether the hypervisor should use the default OS's GDT as the host GDT in VMCS or not.

◆ USE_INTERRUPT_STACK_TABLE

#define USE_INTERRUPT_STACK_TABLE   FALSE

Use Interrupt Stack Table (IST1..IST7)

Function Documentation

◆ _Success_()

_Success_ ( return )

◆ SegmentPrepareHostGdt()

VOID SegmentPrepareHostGdt ( SEGMENT_DESCRIPTOR_32 * OsGdtBase,
UINT16 OsGdtLimit,
UINT16 TrSelector,
UINT64 HostStack,
SEGMENT_DESCRIPTOR_32 * AllocatedHostGdt,
TASK_STATE_SEGMENT_64 * AllocatedHostTss )

Initialize the host GDT.

Parameters
OsGdtBase
OsGdtLimit
TrSelector
HostStack
AllocatedHostGdt
AllocatedHostTss
Returns
VOID
102{
103 //
104 // Copy current OS GDT into host GDT
105 // Note that the limit is the maximum addressable byte offset within the segment,
106 // so the actual size of the GDT is limit + 1
107 //
108 RtlCopyBytes(AllocatedHostGdt, OsGdtBase, OsGdtLimit + 1);
109
110 //
111 // Make sure host TSS is empty
112 //
113 RtlZeroBytes(AllocatedHostTss, sizeof(TASK_STATE_SEGMENT_64));
114
115#if USE_INTERRUPT_STACK_TABLE == TRUE
116
117 UINT64 EndOfStack = 0;
118
119 //
120 // Setup TSS memory for host (same host stack is used for all interrupts and privilege levels)
121 //
122 EndOfStack = ((UINT64)HostStack + HOST_INTERRUPT_STACK_SIZE - 1);
123 EndOfStack = ((UINT64)((ULONG_PTR)(EndOfStack) & ~(16 - 1)));
124
125 LogDebugInfo("Host Interrupt Stack, from: %llx, to: %llx", HostStack, EndOfStack);
126
127 AllocatedHostTss->Rsp0 = EndOfStack;
128 AllocatedHostTss->Rsp1 = EndOfStack;
129 AllocatedHostTss->Rsp2 = EndOfStack;
130 AllocatedHostTss->Ist1 = EndOfStack;
131 AllocatedHostTss->Ist2 = EndOfStack;
132 AllocatedHostTss->Ist3 = EndOfStack;
133 AllocatedHostTss->Ist4 = EndOfStack;
134 AllocatedHostTss->Ist5 = EndOfStack;
135 AllocatedHostTss->Ist6 = EndOfStack;
136 AllocatedHostTss->Ist7 = EndOfStack;
137
138#else
139
140 UNREFERENCED_PARAMETER(HostStack);
141
142#endif // USE_INTERRUPT_STACK_TABLE == TRUE
143
144 //
145 // Setup the TSS segment descriptor
146 //
147 SEGMENT_DESCRIPTOR_64 * GdtTssDesc = (SEGMENT_DESCRIPTOR_64 *)&AllocatedHostGdt[TrSelector];
148
149 //
150 // Point the TSS descriptor to our TSS
151 //
152 UINT64 Base = (UINT64)AllocatedHostTss;
153 GdtTssDesc->BaseAddressLow = (Base >> 00) & 0xFFFF;
154 GdtTssDesc->BaseAddressMiddle = (Base >> 16) & 0xFF;
155 GdtTssDesc->BaseAddressHigh = (Base >> 24) & 0xFF;
156 GdtTssDesc->BaseAddressUpper = (Base >> 32) & 0xFFFFFFFF;
157
159
160 // SEGMENT_SELECTOR HostCsSelector = {0, 0, 1};
161 // SEGMENT_SELECTOR HostTrSelector = {0, 0, 2};
162 //
163 // //
164 // // Setup the CS segment descriptor
165 // //
166 // SEGMENT_DESCRIPTOR_32 CsDesc = AllocatedHostGdt[HostCsSelector.Index];
167 // CsDesc.Type = SEGMENT_DESCRIPTOR_TYPE_CODE_EXECUTE_READ;
168 // CsDesc.DescriptorType = SEGMENT_DESCRIPTOR_TYPE_CODE_OR_DATA;
169 // CsDesc.DescriptorPrivilegeLevel = 0;
170 // CsDesc.Present = 1;
171 // CsDesc.LongMode = 1;
172 // CsDesc.DefaultBig = 0;
173 // CsDesc.Granularity = 0;
174 //
175 // //
176 // // Setup the TSS segment descriptor
177 // //
178 // SEGMENT_DESCRIPTOR_64 * TssDesc = (SEGMENT_DESCRIPTOR_64 *)&AllocatedHostGdt[HostTrSelector.Index];
179 // TssDesc->Type = SEGMENT_DESCRIPTOR_TYPE_TSS_BUSY;
180 // TssDesc->DescriptorType = SEGMENT_DESCRIPTOR_TYPE_SYSTEM;
181 // TssDesc->DescriptorPrivilegeLevel = 0;
182 // TssDesc->Present = 1;
183 // TssDesc->Granularity = 0;
184 // TssDesc->SegmentLimitLow = 0x67;
185 // TssDesc->SegmentLimitHigh = 0;
186 //
187 // //
188 // // Point the TSS descriptor to our TSS
189 // //
190 // UINT64 Base = (UINT64)AllocatedHostTss;
191 // TssDesc->BaseAddressLow = (Base >> 00) & 0xFFFF;
192 // TssDesc->BaseAddressMiddle = (Base >> 16) & 0xFF;
193 // TssDesc->BaseAddressHigh = (Base >> 24) & 0xFF;
194 // TssDesc->BaseAddressUpper = (Base >> 32) & 0xFFFFFFFF;
195}
unsigned __int64 UINT64
Definition BasicTypes.h:21
#define LogDebugInfo(format,...)
Log, initialize boot information and debug information.
Definition HyperDbgHyperLogIntrinsics.h:155
#define HOST_INTERRUPT_STACK_SIZE
Size of host interrupt stack.
Definition Segmentation.h:35

Variable Documentation

◆ SegmentSelector

_In_ UINT16 _Out_ PVMX_SEGMENT_SELECTOR SegmentSelector

◆ Selector

_In_ UINT16 Selector