wbenny / hvpp

hvpp is a lightweight Intel x64/VT-x hypervisor written in C++ focused primarily on virtualization of already running operating system
MIT License
1.12k stars 221 forks source link

Invalid use of invd #3

Closed rianquinn closed 6 years ago

rianquinn commented 6 years ago

In the following: https://github.com/wbenny/hvpp/blob/master/src/hvpp/hvpp/vmexit.cpp#L289

You execute invd, which will invalidate the cache of your VMM including the guest which would result in corruption if this exit ever occurred. Instead, when this exit occurs a wbinvd should be executed instead, or the instruction can be ignored.

wbenny commented 6 years ago

That's a good point and makes sense. By Alex's comment here: https://github.com/ionescu007/SimpleVisor/blob/master/shvvmxhv.c#L82 I've assumed that it would be actually right.

Is there any mention about this in the Intel Manual or is it one of those things you have to actually figure out yourself by trial & error? :)

rianquinn commented 6 years ago

I personally have never seen the invd instruction executed, and I have been doing this for 11 years, so in this particular case, it was merely an observation. The only thing the Intel Manual states about this instruction is that is should be used with care, specifically for the reason I stated above. You would only execute this if you didn't care about the contents of the cache, which is highly unlikely.

wbenny commented 6 years ago

I've poke into KVM & XEN sources, KVM seems to really "do nothing" on INVD, XEN does WBINVD on both INVD/WBINVD.

What I didn't figure out yet is that both do IPI which executes WBINVD on all CPUs if WBINVD exiting is enabled.

rianquinn commented 6 years ago

I'm not sure why they do an IPI, but Xen does a lot of things that I wouldn't do, so I would take their code with a grain of salt. I think the patch that you provided is fine.