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

Functions for handling memory segmentations. More...

#include "pch.h"

Macros

#define SELECTOR_TABLE_LDT   0x1
 
#define SELECTOR_TABLE_GDT   0x0
 

Functions

_Use_decl_annotations_ BOOLEAN SegmentGetDescriptor (PUCHAR GdtBase, UINT16 Selector, PVMX_SEGMENT_SELECTOR SegmentSelector)
 Get Segment Descriptor.
 
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.
 

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

◆ SELECTOR_TABLE_GDT

#define SELECTOR_TABLE_GDT   0x0

◆ SELECTOR_TABLE_LDT

#define SELECTOR_TABLE_LDT   0x1

Function Documentation

◆ SegmentGetDescriptor()

_Use_decl_annotations_ BOOLEAN SegmentGetDescriptor ( PUCHAR GdtBase,
UINT16 Selector,
PVMX_SEGMENT_SELECTOR SegmentSelector )

Get Segment Descriptor.

Parameters
SegmentSelector
Selector
GdtBase
Returns
BOOLEAN
27{
28 SEGMENT_DESCRIPTOR_32 * DescriptorTable32;
29 SEGMENT_DESCRIPTOR_32 * Descriptor32;
30 SEGMENT_SELECTOR SegSelector = {.AsUInt = Selector};
31
32 if (!SegmentSelector)
33 return FALSE;
34
35#define SELECTOR_TABLE_LDT 0x1
36#define SELECTOR_TABLE_GDT 0x0
37
38 //
39 // Ignore LDT
40 //
41 if ((Selector == 0x0) || (SegSelector.Table != SELECTOR_TABLE_GDT))
42 {
43 return FALSE;
44 }
45
46 DescriptorTable32 = (SEGMENT_DESCRIPTOR_32 *)(GdtBase);
47 Descriptor32 = &DescriptorTable32[SegSelector.Index];
48
50 SegmentSelector->Limit = __segmentlimit(Selector);
51 SegmentSelector->Base = ((UINT64)Descriptor32->BaseAddressLow | (UINT64)Descriptor32->BaseAddressMiddle << 16 | (UINT64)Descriptor32->BaseAddressHigh << 24);
52
54
55 if (SegSelector.Table == 0 && SegSelector.Index == 0)
56 {
58 }
59
60 if ((Descriptor32->Type == SEGMENT_DESCRIPTOR_TYPE_TSS_BUSY) || (Descriptor32->Type == SEGMENT_DESCRIPTOR_TYPE_CALL_GATE))
61 {
62 //
63 // this is a TSS or callgate etc, save the base high part
64 //
65
66 UINT64 SegmentLimitHigh;
67 SegmentLimitHigh = (*(UINT64 *)((PUCHAR)Descriptor32 + 8));
68 SegmentSelector->Base = (SegmentSelector->Base & 0xffffffff) | (SegmentLimitHigh << 32);
69 }
70
72 {
73 //
74 // 4096-bit granularity is enabled for this segment, scale the limit
75 //
76 SegmentSelector->Limit = (SegmentSelector->Limit << 12) + 0xfff;
77 }
78
79 return TRUE;
80}
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
UINT32 AsmGetAccessRights(unsigned short Selector)
#define SELECTOR_TABLE_GDT
_In_ UINT16 Selector
Definition Segmentation.h:50
_In_ UINT16 _Out_ PVMX_SEGMENT_SELECTOR SegmentSelector
Definition Segmentation.h:51
UINT32 Limit
Definition DataTypes.h:439
VMX_SEGMENT_ACCESS_RIGHTS_TYPE Attributes
Definition DataTypes.h:438
UINT16 Selector
Definition DataTypes.h:437
UINT64 Base
Definition DataTypes.h:440
UINT32 Granularity
Definition DataTypes.h:420
UINT32 AsUInt
Definition DataTypes.h:428
UINT32 Unusable
Definition DataTypes.h:424

◆ 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}
#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