以编程方式检测Windows 7上是否启用了硬件虚拟化 [英] Programmatically detect if hardware virtualization is enabled on Windows 7

查看:632
本文介绍了以编程方式检测Windows 7上是否启用了硬件虚拟化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在反弹一段时间,仍然没有提出一个适当的解决方案,希望有人

基本上我需要确定我是否可以在目标机器上运行64位虚拟机(在GO中工作,但很乐意考虑绑定c代码或一些程序集(尽管我觉得有点深入)

为了运行64位虚拟机,系统需要硬件虚拟化支持,并在BIOS中启用(im目前只关注intel / amd)



到目前为止



<从Windows 8开始,Windows随附Hyper-V,并且有一个很好的功能,你可以从kernel32.dll中调用IsProcessorFeaturePresent,并使用'PF_VIRT_FIRMWARE_ENABLED'参数来告诉你硬件虚拟化是否在固件中启用: p>

IsProcessorFeaturePresent



现在我不太喜欢这种行为方式它说,如果安装了hyper-v,则不可用),但是我可以通过检查是否通过其他方式启用hyper-v来应付它,所以这几乎可以完成win8以上的工作。


$ b $问题是这个函数在win 7上总是返回false,因为某些原因 - 即使在我知道启用硬件虚拟化的系统上。



从另一个角度来看我使用这个库来确定可用的指令集:英特尔处理器功能库 - 这允许我知道处理器上有什么类型的虚拟化指令可用(如果有的话)

但是我仍然错过了最后一块了解它是否在BIOS中启用赢了7.我原则上认为从这里应该很容易 - 我应该可以打电话给某人ich利用虚拟化扩展并查看它是否按预期做出响应。但不幸的是,我不知道如何做到这一点。



有人对我如何做到这一点有任何建议吗?

注意:我很高兴考虑第三方库,但是这将用于商业软件,因此许可必须允许(例如,微软没有提供)

解决方案

除非您准备好提供内核驱动程序,否则恐怕您无法实现所需内容,因为检查BIOS是否已启用虚拟化需要内核特权。 b
$ b

英特尔软件开发人员手册描述了一个编号为3Ah的型号特定寄存器(MSR),称为IA32_FEATURE_CONTROL。其位1和2控制SMX和非SMX模式中是否允许VMX指令。此外,还有一个位为零,当写入1时,锁定整个寄存器的值,因此在下一个处理器复位之前无法启用/禁用功能。这意味着,如果BIOS代码已禁用VMX并将其锁定,则稍后启动的操作系统将无法更改该事实,只能查看它。



阅读此内容或任何其他MSR应该使用机器指令RDMSR,并且该指令仅在CPL为零时,即在OS上下文中可用。它会抛出一个异常,如果试图从应用程序代码中使用。



除非你找到一个程序接口方法来包装RDMSR并将它提供给应用程序,运气。通常意味着加载并运行专用的内核驱动程序。我知道关于Linux的一个,但不能说是否有任何Windows。



另外需要注意的是,如果您的代码已经在虚拟机内运行,它适用于某些Windows安装,它为常规桌面启用了Hyper-V环境,那么您甚至无法看到实际的主机MSR值。 VMM会为您提供一个模拟值,并且它会显示您希望看到的任何CPUID值,而不是来自主机的值。


Background

I've been bouncing around this for a while and still haven't come up with an adequate solution, hoping someone out there can point me in the right direction.

Essentially I need to identify whether I can run 64bit VM on a target machine (working in GO but happy to consider binding c code or some assembly (though I feel a bit out of depth there)

In order to run a 64 bit VM the system need Hardware Virtualisation support available and enabled in the bios (im only concerned with intel/amd at this time)

Journey so far

From windows 8 onwards, Windows ships with Hyper-V, and there is a nice function you can call IsProcessorFeaturePresent from the kernel32.dll with an arg of 'PF_VIRT_FIRMWARE_ENABLED' which will tell you if hardware virtualisation is enabled in firmware:

IsProcessorFeaturePresent

now I dont really like the way this behaves (it says not available if hyper-v is installed) but i can cope with it by checking if hyper-v is enabled through other means so this pretty much does the job from win8 upwards.

Problem is this function always return false on win 7 for some reason - even on a system on which I know hardware virtualization is enabled.

Coming from another angle I have used this lib to determine what instruction sets are available: intel processor feature lib - this allows me to know what type of virtualization instructions are available on the processor (if any)

But I'm still missing the final piece of knowing if its enabled in the bios on win 7. I figure in principle it should be easy from here - I should be able to call something which utilizes the virtualization extensions and see if it responds as expected. But unfortunately I have no idea how to do this.

Does anyone have any suggestions as to how I might do this?

Note: Im happy to consider 3rd party libs but this would be used in commercial software so licensing would have to allow for that (e.g nothing from Microsoft)

解决方案

I am afraid you won't be able to achieve what you want unless you are ready to provide a kernel driver, because checking if BIOS has enabled virtualization requires kernel privileges.

Intel Software Developer Manual describes a model-specific register (MSR) with number 3Ah called IA32_FEATURE_CONTROL. Its bits 1 and 2 control whether VMX instructions are allowed in SMX and non-SMX modes. Also there is bit zero which, when written with 1, locks the whole register's value, thus making impossible to enable/disabled features until the next processor reset. This means that, if BIOS code has disabled VMX and locked it, an OS that boots later will be unable to change that fact, only to see it.

To read this or any other MSR one should use machine instruction RDMSR, and this instruction is only available when CPL is zero, that is, within an OS context. It will throw an exception if attempted to be used from application code.

Unless you find a program interface method that wraps RDMSR around and provides it to applications, you are out of luck. Typically that implies loading and running a dedicated kernel driver. I am aware about one for Linux, but cannot say if there is anything for Windows.

As an extra note, if your code is already running inside a virtual machine, like it is for some Windows installations which enable a Hyper-V environment for regular desktop, then you won't even be able to see an actual host MSR value. It will be up to the VMM to provide you with an emulated value, as well as it will show you whatever CPUID value it wants you to see, not the one from the host.

这篇关于以编程方式检测Windows 7上是否启用了硬件虚拟化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆