基于 Hypervisor-From-Scratch
Hypervisor-From-Scratch-Github 源码
Hypervisor From Scratch 文档
首先检测是否支持VT
DR0-CR4寄存器【链接】
请注意,VMX 操作要求下列位在 VMX 操作中为1: CR0.PE、 CR0.NE、 CR0.PG 和 CR4.VMXE。对 CR0.PE 和 CR0.PG 的限制意味着 VMX 操作只支持分页保护模式。因此, guest 软件不能在非分页保护模式或实地址模式下运行。
如何理解EPT呢?
他就相当于保护模式的内存分页机制9-9-9-9-12
目前x64只用48位进行寻址,其余位无视
比如我们想寻找
0x987654321
00000000 00000000 00000000 00001001 10000111 01100101 01000011 00100001
从低到高进行位的分割
无视 9 9 9 9 12
0000000000000000 000000000 000100110 000111011 001010100 001100100001
0 0x26 0x3B 0x54 0x321
首先EPTP指向的物理地址 等价与 CR3 页表目录
EPTP + 08 = PML4E
PML4E + 0x26 8 = PDPT
PTPT + 0x3B8 = PDE
PDE + 0x548 = PTE
取得的PTE指向一块4096大小首地址
PTE + 0x321 = 寻址的地址
/
/
检查是否支持VT
BOOLEAN HvIsVmxSupported()
{
CPUID Data
=
{
0
};
IA32_FEATURE_CONTROL_MSR FeatureControlMsr
=
{
0
};
/
/
VMX bit
__cpuid((
int
*
)&Data,
1
);
if
((Data.ecx & (
1
<<
5
))
=
=
0
)
return
FALSE;
FeatureControlMsr.
All
=
__readmsr(MSR_IA32_FEATURE_CONTROL);
/
/
BIOS lock check
if
(FeatureControlMsr.Fields.Lock
=
=
0
)
{
FeatureControlMsr.Fields.Lock
=
TRUE;
FeatureControlMsr.Fields.EnableVmxon
=
TRUE;
__writemsr(MSR_IA32_FEATURE_CONTROL, FeatureControlMsr.
All
);
}
else
if
(FeatureControlMsr.Fields.EnableVmxon
=
=
FALSE)
{
LogError(
"Intel VMX feature is locked in BIOS"
);
return
FALSE;
}
return
TRUE;
}
/
/
检查是否支持VT
BOOLEAN HvIsVmxSupported()
{
CPUID Data
=
{
0
};
IA32_FEATURE_CONTROL_MSR FeatureControlMsr
=
{
0
};
/
/
VMX bit
__cpuid((
int
*
)&Data,
1
);
if
((Data.ecx & (
1
<<
5
))
=
=
0
)
return
FALSE;
FeatureControlMsr.
All
=
__readmsr(MSR_IA32_FEATURE_CONTROL);
/
/
BIOS lock check
if
(FeatureControlMsr.Fields.Lock
=
=
0
)
{
FeatureControlMsr.Fields.Lock
=
TRUE;
FeatureControlMsr.Fields.EnableVmxon
=
TRUE;
__writemsr(MSR_IA32_FEATURE_CONTROL, FeatureControlMsr.
All
);
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课