typedef union
{
USHORT UCHARs;
struct
{
USHORT type:4; /* 0; Bit 40-43 */
USHORT s:1; /* 4; Bit 44 */
USHORT dpl:2; /* 5; Bit 45-46 */
USHORT p:1; /* 7; Bit 47 */
// gap!
USHORT avl:1; /* 8; Bit 52 */
USHORT l:1; /* 9; Bit 53 */
USHORT db:1; /* 10; Bit 54 */
USHORT g:1; /* 11; Bit 55 */
USHORT Gap:4;
} fields;
} SEGMENT_ATTRIBUTES;
typedef struct _SEGMENT_SELECTOR
{
USHORT sel;
SEGMENT_ATTRIBUTES attributes;
ULONG limit;
ULONG64 base;
} SEGMENT_SELECTOR, *PSEGMENT_SELECTOR;
typedef struct
{
USHORT limit0;
USHORT base0;
UCHAR base1;
UCHAR attr0;
UCHAR limit1attr1;
UCHAR base2;
} SEGMENT_DESCRIPTOR2, *PSEGMENT_DESCRIPTOR2;
NTSTATUS InitializeSegmentSelector( PSEGMENT_SELECTOR SegmentSelector, //代表 段寄存器 得结构体
USHORT Selector, //当前段寄存器得值 ,这里只是选择子得值
ULONG64 GdtBase ) //GDT表得基地址
{
PSEGMENT_DESCRIPTOR2 SegDesc;
SegDesc = ( PSEGMENT_DESCRIPTOR2 )( ( PUCHAR ) GdtBase + ( Selector & ~0x7 ) ); //在GDT表中得到段描述符得地址
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SegmentSelector->attributes.UCHARs = SegDesc->attr0 | ( SegDesc->limit1attr1 & 0xf0 ) << 4;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
return STATUS_SUCCESS;
}
NTSTATUS FillGuestSelectorData(ULONG64 GdtBase, //GDT基地址 ///////////////////////////////////////////////////////////////入口在这里
ULONG Segreg, // 要写入的段寄存器
USHORT Selector //读取到的当前段的选择子
)
{
SEGMENT_SELECTOR SegmentSelector = { 0 };
ULONG uAccessRights;
InitializeSegmentSelector(&SegmentSelector, Selector, GdtBase);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
uAccessRights = ((PUCHAR)& SegmentSelector.attributes)[0] + (((PUCHAR)&
SegmentSelector.attributes)[1] << 12);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
return STATUS_SUCCESS;
}