tandasat / SimpleSvm

A minimalistic educational hypervisor for Windows on AMD processors.
MIT License
326 stars 57 forks source link

system hangs shortly after virtualizing processors #1

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hello, thank you for this simple repository, it is really helping me learn all about virtualization on AMD processors. I downloaded your hypervisor, and recoded many parts to gain a better understanding on how things work. Right now, I am having an issue where the entire system hangs a few seconds after vmrun. I found out that it always hangs at PAUSE instruction after sending an IPI request (KiIpiSendRequest) in an ntoskrnl function. I have attached two images below, showing exactly where it hangs inside ntoskrnl.

image

image

What could possibly be the reason behind this?

Thanks in advance.

tandasat commented 3 years ago

It appears to be the IPI is not processed by one or more processors.

Few questions.

ghost commented 3 years ago
  1. This happens with my modified code
  2. The hang happens in guest state, outside of VMEXIT handler so that shouldn't be a problem, but I am not calling any APIs in VMEXIT.

Is it possible that it has something to do with incorrectly setting up IDTR and segment attributes?

tandasat commented 3 years ago

Diagnosing an issue without code is not going to be easy for me. I offer several debugging tips for you can hopefully figure it out instead.

If the critical structures like IDTR is incorrectly setup, it could cause the problem like that, but since the system works for few seconds, it do not expect that's the cause. It more likely causes problems immediately unless the mistake is very subtle.

ghost commented 3 years ago

Thank you for the detailed response, I will keep you updated on my testing when I get home.

ghost commented 3 years ago

I didn't fix the problem yet, but I managed to get more information about the error. I also ported some code, from your code to my fork of your repository, but that still resulted in the same problem. The freeze happens no matter if the system is single core or multi core. When I restrict my virtual machine to one core, I can't break into the virtual machine when it hangs, at all, so I am testing with 2 cores. I switched to the second core, to view registers and call stack, and the call stack of second core looks pretty interesting.

core 1 callstack: image

core 2 callstack: image

I'll upload my code below in a ZIP, If you could take a look at it I would greatly appreciate it. Thanks in advance.

MyFirstHypervisor.zip

I compile with: x64, Debug

tandasat commented 3 years ago

Hi, thank you for sharing code and more details. Please try those:

ghost commented 3 years ago

I removed all dbgprints, no change in the result. It appears that the issue doesn't have anything to do with interrupts, I inspected core 2 and found out that it is constantly causing a guest page fault in an infinite loop.

The page fault address printed in the picture below is the output from Exitinfo2 image

The first core is executing normally without any problem.

EDIT: I fixed this page fault loop, I might have fixed the freezing problem, I'll post updates soon and I think I'm on the right track to debug this right now.

ghost commented 3 years ago

I finally fixed the freezing issue, turns out it was caused by me putting breakpoints in the VMEXIT handler. For some reason the breakpoints in VMEXIT handler caused a hang. image

Thank you for spending time to fix this with me, I will close this issue now 👍 .

tandasat commented 3 years ago

That's interesting and new to me (with outside certain vm-exits). Thank you for sharing this gotcha!