SimpleVisor icon indicating copy to clipboard operation
SimpleVisor copied to clipboard

How to use preemption timer in simplevisor?

Open liuyanfeng1818 opened this issue 5 years ago • 4 comments

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.

liuyanfeng1818 avatar Oct 19 '19 08:10 liuyanfeng1818

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

rianquinn avatar Oct 19 '19 13:10 rianquinn

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?

liuyanfeng1818 avatar Oct 21 '19 01:10 liuyanfeng1818

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

guye1296 avatar Apr 19 '21 14:04 guye1296

@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:

  • Make sure you are reading the full 64bit value from RDTSC.
  • If you don't have an invariant TSC, you might see RDTSC change the rate at which it counts, but it should ALWAYS be increasing.

rianquinn avatar Apr 19 '21 17:04 rianquinn