ionescu007 / SimpleVisor

SimpleVisor is a simple, portable, Intel VT-x hypervisor with two specific goals: using the least amount of assembly code (10 lines), and having the smallest amount of VMX-related code to support dynamic hyperjacking and unhyperjacking (that is, virtualizing the host state from within the host). It works on Windows and UEFI.
http://ionescu007.github.io/SimpleVisor/
1.69k stars 259 forks source link

How to use preemption timer in simplevisor? #42

Open liuyanfeng1818 opened 4 years ago

liuyanfeng1818 commented 4 years ago

Hi, I turn on the preemption timer in the simplevisor.When the timer count reaches 0, it will trigger vm exit(no.52).Every VM exit (No. 52), I will read the tsc value in ShvVmxEntryHandler. My question is:1.The period of Vm exit (No. 52) differs greatly from the theoretical value;2.Windows will restart randomly. Could you help me or give me some suggestions.Thanks.

rianquinn commented 4 years ago

Here is an example of how to use the preemption timer. Not sure why Windows is restarting (as that doesn't seem like it is related to the preemption timer), but the biggest thing to remember is that the preemption timer runs a lot slower than the TSC, so you have to calculate what that multiple difference is.

https://github.com/Bareflank/boxy/blob/master/bfvmm/src/hve/arch/intel_x64/vmexit/yield.cpp

liuyanfeng1818 commented 4 years ago

Here is an example of how to use the preemption timer. Not sure why Windows is restarting (as that doesn't seem like it is related to the preemption timer), but the biggest thing to remember is that the preemption timer runs a lot slower than the TSC, so you have to calculate what that multiple difference is.

https://github.com/Bareflank/boxy/blob/master/bfvmm/src/hve/arch/intel_x64/vmexit/yield.cpp

Thanks. 1.Preemption timer value:0x100000. ratio value:7. The theoretical tsc value: 0x100000 * (2<<7) = 134217728。 Actual test value: 00000003 15:12:53.922 TimerExpiredCycle:37457839830
00000004 15:12:53.922 TimerExpiredCycle:3724228740
00000005 15:12:53.922 TimerExpiredCycle:15215759138 00000006 15:12:53.922 TimerExpiredCycle:13953619180
00000007 15:12:53.922 TimerExpiredCycle:829314542 00000008 15:12:53.922 TimerExpiredCycle:425428684 00000009 15:12:53.922 TimerExpiredCycle:150000640 00000010 15:12:53.922 TimerExpiredCycle:134220310 00000011 15:12:53.922 TimerExpiredCycle:134220002 00000012 15:12:53.922 TimerExpiredCycle:134320380 2.Delete the preemption timer related code and rebulid,simplevisor run fine. 3.What is the relationship between preemption timer and apic timer、external interrupt?Is there a more detailed explanation?

guye1296 commented 3 years ago

Hi! Having the same problem (inconsistent TSC values). I could not locate the initialization of the preemption-timer in Bareflank. @rianquinn

rianquinn commented 3 years ago

@guye1296 There is no initialization. You simply write a value to the preemption timer field in the VMCS and it will trap once the time expires. The only configuration thing is whether or not you want the timer value saved on the next exit or not. As for inconsistent values, it could be a couple of things: