LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v2 0/6] KVM: few more SMM fixes
@ 2021-08-30 12:55 Maxim Levitsky
  2021-08-30 12:55 ` [PATCH v2 1/6] KVM: SVM: restore the L1 host state prior to resuming nested guest on SMM exit Maxim Levitsky
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Maxim Levitsky @ 2021-08-30 12:55 UTC (permalink / raw)
  To: kvm
  Cc: Jim Mattson, Sean Christopherson, Paolo Bonzini,
	maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
	Ingo Molnar, Vitaly Kuznetsov, Thomas Gleixner, Maxim Levitsky,
	open list:X86 ARCHITECTURE (32-BIT AND 64-BIT),
	Joerg Roedel, Wanpeng Li, H. Peter Anvin, Borislav Petkov

These are few SMM fixes I was working on last week.\r
\r
(V2: I merged here VMX's SMM fixes, while SVM SMM fixes are unchanged from V1)\r
\r
* Patch 1 fixes a minor issue that remained after\r
  commit 37be407b2ce8 ("KVM: nSVM: Fix L1 state corruption upon return from SMM")\r
\r
  While now, returns to guest mode from SMM work due to restored state from HSAVE\r
  area, the guest entry still sees incorrect HSAVE state.\r
\r
  This for example breaks return from SMM when the guest is 32 bit, due to PDPTRs\r
  loading which are done using incorrect MMU state which is incorrect,\r
  because it was setup with incorrect L1 HSAVE state.\r
\r
* Patch 2 fixes a theoretical issue that I introduced with my SREGS2 patchset,\r
  which Sean Christopherson pointed out.\r
\r
  The issue is that KVM_REQ_GET_NESTED_STATE_PAGES request is not only used\r
  for completing the load of the nested state, but it is also used to complete\r
  exit from SMM to guest mode, and my compatibility hack of pdptrs_from_userspace\r
  was done assuming that this is not done.\r
\r
  While it is safe to just reset 'pdptrs_from_userspace' on each VM entry,\r
  I don't want to slow down the common code for this very rare hack.\r
  Instead I explicitly zero this variable when SMM exit to guest mode is done,\r
  because in this case PDPTRs do need to be reloaded from memory always.\r
\r
  Note that this is a theoretical issue only, because after 'vendor' return from\r
  smm code (aka .leave_smm) is done, even when it returned to the guest mode,\r
  which loads some of L2 CPU state, we still load again all of the L2 cpu state\r
  captured in SMRAM which includes CR3, at which point guest PDPTRs are re-loaded\r
  anyway.\r
\r
  Also note that across SMI entries the CR3 seems not to be updated, and Intel's\r
  SDM notes that it saved value in SMRAM isn't writable, thus it is possible\r
  that if SMM handler didn't change CR3, the pdptrs would not be touched.\r
\r
  I guess that means that a SMI handler can in theory preserve PDPTRs by never\r
  touching CR3, but since recently we removed that code that didn't update PDPTRs\r
  if CR3 didn't change, I guess it won't work.\r
\r
  Anyway I don't think any OS bothers to have PDPTRs not synced with whatever\r
  page CR3 points at, thus I didn't bother to try and test what the real hardware\r
  does in this case.\r
\r
* 3rd patch makes SVM SMM exit to be a bit more similar to how VMX does it\r
  by also raising KVM_REQ_GET_NESTED_STATE_PAGES requests.\r
\r
  I do have doubts about why we need to do this on VMX though. The initial\r
  justification for this comes from\r
\r
  7f7f1ba33cf2 ("KVM: x86: do not load vmcs12 pages while still in SMM")\r
\r
  With all the MMU changes, I am not sure that we can still have a case\r
  of not up to date MMU when we enter the nested guest from SMM.\r
  On SVM it does seem to work anyway without this.\r
\r
* Patch 3 fixes guest emulation failure when unrestricted_guest=0 and we reach\r
  handle_exception_nmi_irqoff.\r
  That function takes stale values from current vmcs and fails not taking into account\r
  the fact that we are emulating invalid guest state now, and thus no VM exit happened.\r
\r
* Patch 4 fixed a corner case where return from SMM is slightly corrupting\r
  the L2 segment register state when unrestricted_guest=0 due to real mode segement\r
  caching register logic, but later it restores it correctly from SMMRAM.\r
  Fix this by not failing nested_vmx_enter_non_root_mode and delaying this\r
  failure to the next nested VM entry.\r
\r
* Patch 5 fixes another corner case where emulation_required was not updated\r
  correctly on nested VMexit when restoring the L1 segement registers.\r
\r
I still track 2 SMM issues:\r
\r
1. When HyperV guest is running nested, and uses SMM enabled OVMF, it crashes and\r
   reboots during the boot process.\r
\r
2. Nested migration on VMX is still broken when L1 floods itself with SMIs.\r
\r
Best regards,\r
	Maxim Levitsky\r
\r
Maxim Levitsky (6):\r
  KVM: SVM: restore the L1 host state prior to resuming nested guest on\r
    SMM exit\r
  KVM: x86: force PDPTR reload on SMM exit\r
  KVM: nSVM: call KVM_REQ_GET_NESTED_STATE_PAGES on exit from SMM mode\r
  KVM: VMX: synthesize invalid VM exit when emulating invalid guest\r
    state\r
  KVM: nVMX: don't fail nested VM entry on invalid guest state if\r
    !from_vmentry\r
  KVM: nVMX: re-evaluate emulation_required on nested VM exit\r
\r
 arch/x86/kvm/svm/nested.c | 10 +++++++---\r
 arch/x86/kvm/svm/svm.c    | 27 ++++++++++++++++++---------\r
 arch/x86/kvm/svm/svm.h    |  3 ++-\r
 arch/x86/kvm/vmx/nested.c |  9 ++++++++-\r
 arch/x86/kvm/vmx/vmx.c    | 35 ++++++++++++++++++++++++++++-------\r
 arch/x86/kvm/vmx/vmx.h    |  1 +\r
 6 files changed, 64 insertions(+), 21 deletions(-)\r
\r
-- \r
2.26.3\r
\r


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-10-08 23:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-30 12:55 [PATCH v2 0/6] KVM: few more SMM fixes Maxim Levitsky
2021-08-30 12:55 ` [PATCH v2 1/6] KVM: SVM: restore the L1 host state prior to resuming nested guest on SMM exit Maxim Levitsky
2021-08-30 12:55 ` [PATCH v2 2/6] KVM: x86: force PDPTR reload " Maxim Levitsky
2021-08-30 12:55 ` [PATCH v2 3/6] KVM: nSVM: call KVM_REQ_GET_NESTED_STATE_PAGES on exit from SMM mode Maxim Levitsky
2021-08-30 12:55 ` [PATCH v2 4/6] KVM: VMX: synthesize invalid VM exit when emulating invalid guest state Maxim Levitsky
2021-08-30 12:55 ` [PATCH v2 5/6] KVM: nVMX: don't fail nested VM entry on invalid guest state if !from_vmentry Maxim Levitsky
2021-10-08 23:37   ` Sean Christopherson
2021-08-30 12:55 ` [PATCH v2 6/6] KVM: nVMX: re-evaluate emulation_required on nested VM exit Maxim Levitsky
2021-09-21 22:55   ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).