LKML Archive on
help / color / mirror / Atom feed
From: Maxim Levitsky <>
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 <>
Subject: [PATCH v2 0/6] KVM: few more SMM fixes
Date: Mon, 30 Aug 2021 15:55:33 +0300	[thread overview]
Message-ID: <> (raw)

These are few SMM fixes I was working on last week.\r
(V2: I merged here VMX's SMM fixes, while SVM SMM fixes are unchanged from V1)\r
* Patch 1 fixes a minor issue that remained after\r
  commit 37be407b2ce8 ("KVM: nSVM: Fix L1 state corruption upon return from SMM")\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
  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
* Patch 2 fixes a theoretical issue that I introduced with my SREGS2 patchset,\r
  which Sean Christopherson pointed out.\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
  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
  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
  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
  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
  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
* 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
  I do have doubts about why we need to do this on VMX though. The initial\r
  justification for this comes from\r
  7f7f1ba33cf2 ("KVM: x86: do not load vmcs12 pages while still in SMM")\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
* Patch 3 fixes guest emulation failure when unrestricted_guest=0 and we reach\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
* 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
* Patch 5 fixes another corner case where emulation_required was not updated\r
  correctly on nested VMexit when restoring the L1 segement registers.\r
I still track 2 SMM issues:\r
1. When HyperV guest is running nested, and uses SMM enabled OVMF, it crashes and\r
   reboots during the boot process.\r
2. Nested migration on VMX is still broken when L1 floods itself with SMIs.\r
Best regards,\r
	Maxim Levitsky\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
  KVM: nVMX: don't fail nested VM entry on invalid guest state if\r
  KVM: nVMX: re-evaluate emulation_required on nested VM exit\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

             reply	other threads:[~2021-08-30 12:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-30 12:55 Maxim Levitsky [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
    --subject='Re: [PATCH v2 0/6] KVM: few more SMM fixes' \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).