Skip to contentSkip to conversation

RFC/WIP: KVM: separate injected and pending exception + few more fixes

PATCH In review Maxim Levitsky 2 participants 9 messages Feb 25, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover RFC/WIP: KVM: separate injected and pending exception + few more fixes

clone of "kernel-starship-5.11"

Maxim Levitsky (4):
KVM: x86: determine if an exception has an error code only when
injecting it.
KVM: x86: mmu: initialize fault.async_page_fault in walk_addr_generic
KVM: x86: pending exception must be be injected even with an injected
event
kvm: WIP separation of injected and pending exception

 arch/x86/include/asm/kvm_host.h |  23 +-
 arch/x86/include/uapi/asm/kvm.h |  14 +-
 arch/x86/kvm/mmu/paging_tmpl.h  |   1 +
 arch/x86/kvm/svm/nested.c       |  57 +++--
 arch/x86/kvm/svm/svm.c          |   8 +-
 arch/x86/kvm/vmx/nested.c       | 109 +++++----
 arch/x86/kvm/vmx/vmx.c          |  14 +-
 arch/x86/kvm/x86.c              | 377 +++++++++++++++++++-------------
 arch/x86/kvm/x86.h              |   6 +-
 include/uapi/linux/kvm.h        |   1 +
 10 files changed, 374 insertions(+), 236 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/4 KVM: x86: determine if an exception has an error code only when injecting it.

A page fault can be queued while vCPU is in real paged mode on AMD, and AMD manual asks the user to always intercept it
(otherwise result is undefined).
The resulting VM exit, does have an error code.

---
 arch/x86/kvm/x86.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/x86.c +9 -4
index 9fa0c7ff6e2fb..a9d814a0b5e4f 100644
@@ -544,8 +544,6 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
 
 	if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
 	queue:
-		if (has_error && !is_protmode(vcpu))
-			has_error = false;
 		if (reinject) {
 			/*
 			 * On vmentry, vcpu->arch.exception.pending is only
@@ -8345,6 +8343,13 @@ static void update_cr8_intercept(struct kvm_vcpu *vcpu)
 	static_call(kvm_x86_update_cr8_intercept)(vcpu, tpr, max_irr);
 }
 
+static void kvm_inject_exception(struct kvm_vcpu *vcpu)
+{
+	if (vcpu->arch.exception.error_code && !is_protmode(vcpu))
+		vcpu->arch.exception.error_code = false;
+	static_call(kvm_x86_queue_exception)(vcpu);
+}
+
 static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit)
 {
 	int r;
@@ -8353,7 +8358,7 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit
 	/* try to reinject previous events if any */
 
 	if (vcpu->arch.exception.injected) {
-		static_call(kvm_x86_queue_exception)(vcpu);
+		kvm_inject_exception(vcpu);
 		can_inject = false;
 	}
 	/*
@@ -8416,7 +8421,7 @@ static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit
 			}
 		}
 
-		static_call(kvm_x86_queue_exception)(vcpu);
+		kvm_inject_exception(vcpu);
 		can_inject = false;
 	}
 
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/4 KVM: x86: mmu: initialize fault.async_page_fault in walk_addr_generic

In reply to Maxim Levitsky

This field was left uninitialized by a mistake.

---
 arch/x86/kvm/mmu/paging_tmpl.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kvm/mmu/paging_tmpl.h +1 -0
index d9f66cc459e84..3dc9a25772bd8 100644
@@ -503,6 +503,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
 #endif
 	walker->fault.address = addr;
 	walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
+	walker->fault.async_page_fault = false;
 
 	trace_kvm_mmu_walker_error(walker->fault.error_code);
 	return 0;
Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/4 KVM: x86: pending exception must be be injected even with an injected event

In reply to Maxim Levitsky

Injected events should not block a pending exception, but rather, should either be lost or be delivered to the nested hypervisor as part of exitintinfo/IDT_VECTORING_INFO
(if nested hypervisor intercepts the pending exception)

---
 arch/x86/kvm/svm/nested.c | 7 ++++++-
 arch/x86/kvm/vmx/nested.c | 9 +++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +6 -1
index 881e3954d753b..4c82abce0ea0c 100644
@@ -1024,7 +1024,12 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu)
 	}
 
 	if (vcpu->arch.exception.pending) {
-		if (block_nested_events)
+		/*
+		 * Only pending nested run can block an pending exception
+		 * Otherwise an injected NMI/interrupt should either be
+		 * lost or delivered to the nested hypervisor in EXITINTINFO
+		 * */
+		if (svm->nested.nested_run_pending)
                         return -EBUSY;
 		if (!nested_exit_on_exception(svm))
 			return 0;
arch/x86/kvm/vmx/nested.c +7 -2
index b34e284bfa62a..20ed1a351b2d9 100644
@@ -3810,9 +3810,14 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
 
 	/*
 	 * Process any exceptions that are not debug traps before MTF.
+	 *
+	 * Note that only pending nested run can block an pending exception
+	 * Otherwise an injected NMI/interrupt should either be
+	 * lost or delivered to the nested hypervisor in EXITINTINFO
 	 */
+
 	if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
-		if (block_nested_events)
+		if (vmx->nested.nested_run_pending)
 			return -EBUSY;
 		if (!nested_vmx_check_exception(vcpu, &exit_qual))
 			goto no_vmexit;
@@ -3829,7 +3834,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
 	}
 
 	if (vcpu->arch.exception.pending) {
-		if (block_nested_events)
+		if (vmx->nested.nested_run_pending)
 			return -EBUSY;
 		if (!nested_vmx_check_exception(vcpu, &exit_qual))
 			goto no_vmexit;
Signature · 1 line
2.26.2
maintainer
#4 lore raw

Re: [PATCH 3/4] KVM: x86: pending exception must be be injected even with an injected event

Quoted 6 lines · Maxim Levitsky

Injected events should not block a pending exception, but rather, should either be lost or be delivered to the nested hypervisor as part of exitintinfo/IDT_VECTORING_INFO
(if nested hypervisor intercepts the pending exception)

Does this already fix some of your new test cases?

Paolo

Quoted 53 lines
---
  arch/x86/kvm/svm/nested.c | 7 ++++++-
  arch/x86/kvm/vmx/nested.c | 9 +++++++--
  2 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +6 -1
index 881e3954d753b..4c82abce0ea0c 100644
@@ -1024,7 +1024,12 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu)
  	}
 
  	if (vcpu->arch.exception.pending) {
-		if (block_nested_events)
+		/*
+		 * Only pending nested run can block an pending exception
+		 * Otherwise an injected NMI/interrupt should either be
+		 * lost or delivered to the nested hypervisor in EXITINTINFO
+		 * */
+		if (svm->nested.nested_run_pending)
                          return -EBUSY;
  		if (!nested_exit_on_exception(svm))
  			return 0;
arch/x86/kvm/vmx/nested.c +7 -2
index b34e284bfa62a..20ed1a351b2d9 100644
@@ -3810,9 +3810,14 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
 
  	/*
  	 * Process any exceptions that are not debug traps before MTF.
+	 *
+	 * Note that only pending nested run can block an pending exception
+	 * Otherwise an injected NMI/interrupt should either be
+	 * lost or delivered to the nested hypervisor in EXITINTINFO
  	 */
+
  	if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
-		if (block_nested_events)
+		if (vmx->nested.nested_run_pending)
  			return -EBUSY;
  		if (!nested_vmx_check_exception(vcpu, &exit_qual))
  			goto no_vmexit;
@@ -3829,7 +3834,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
  	}
 
  	if (vcpu->arch.exception.pending) {
-		if (block_nested_events)
+		if (vmx->nested.nested_run_pending)
  			return -EBUSY;
  		if (!nested_vmx_check_exception(vcpu, &exit_qual))
  			goto no_vmexit;
#5 lore raw

Re: [PATCH 3/4] KVM: x86: pending exception must be be injected even with an injected event

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Maxim Levitsky

Injected events should not block a pending exception, but rather, should either be lost or be delivered to the nested hypervisor as part of exitintinfo/IDT_VECTORING_INFO
(if nested hypervisor intercepts the pending exception)

Does this already fix some of your new test cases?

Yes, this fixes the 'interrupted' interrupt delivery test, while patch fixes th 'interrupted' exception delivery. Both interrupted by an exception.

Best regards
Maxim Levitsky

Quoted 56 lines

Paolo

Quoted 53 lines
---
  arch/x86/kvm/svm/nested.c | 7 ++++++-
  arch/x86/kvm/vmx/nested.c | 9 +++++++--
  2 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +6 -1
index 881e3954d753b..4c82abce0ea0c 100644
@@ -1024,7 +1024,12 @@ static int svm_check_nested_events(struct kvm_vcpu *vcpu)
  	}
 
  	if (vcpu->arch.exception.pending) {
-		if (block_nested_events)
+		/*
+		 * Only pending nested run can block an pending exception
+		 * Otherwise an injected NMI/interrupt should either be
+		 * lost or delivered to the nested hypervisor in EXITINTINFO
+		 * */
+		if (svm->nested.nested_run_pending)
                          return -EBUSY;
  		if (!nested_exit_on_exception(svm))
  			return 0;
arch/x86/kvm/vmx/nested.c +7 -2
index b34e284bfa62a..20ed1a351b2d9 100644
@@ -3810,9 +3810,14 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
 
  	/*
  	 * Process any exceptions that are not debug traps before MTF.
+	 *
+	 * Note that only pending nested run can block an pending exception
+	 * Otherwise an injected NMI/interrupt should either be
+	 * lost or delivered to the nested hypervisor in EXITINTINFO
  	 */
+
  	if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
-		if (block_nested_events)
+		if (vmx->nested.nested_run_pending)
  			return -EBUSY;
  		if (!nested_vmx_check_exception(vcpu, &exit_qual))
  			goto no_vmexit;
@@ -3829,7 +3834,7 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
  	}
 
  	if (vcpu->arch.exception.pending) {
-		if (block_nested_events)
+		if (vmx->nested.nested_run_pending)
  			return -EBUSY;
  		if (!nested_vmx_check_exception(vcpu, &exit_qual))
  			goto no_vmexit;
maintainer
#6 lore raw

Re: [PATCH 3/4] KVM: x86: pending exception must be be injected even with an injected event

Quoted 7 lines · Maxim Levitsky
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Maxim Levitsky

Injected events should not block a pending exception, but rather, should either be lost or be delivered to the nested hypervisor as part of exitintinfo/IDT_VECTORING_INFO
(if nested hypervisor intercepts the pending exception)

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Does this already fix some of your new test cases?

Yes, this fixes the 'interrupted' interrupt delivery test, while patch fixes th 'interrupted' exception delivery. Both interrupted by an exception.

Could you post the tests, marking them as XFAIL if possible?

Thanks,

Paolo

#7 lore raw

PATCH 4/4 kvm: WIP separation of injected and pending exception

In reply to Maxim Levitsky
---
 arch/x86/include/asm/kvm_host.h |  23 +-
 arch/x86/include/uapi/asm/kvm.h |  14 +-
 arch/x86/kvm/svm/nested.c       |  62 +++---
 arch/x86/kvm/svm/svm.c          |   8 +-
 arch/x86/kvm/vmx/nested.c       | 114 +++++-----
 arch/x86/kvm/vmx/vmx.c          |  14 +-
 arch/x86/kvm/x86.c              | 370 +++++++++++++++++++-------------
 arch/x86/kvm/x86.h              |   6 +-
 include/uapi/linux/kvm.h        |   1 +
 9 files changed, 367 insertions(+), 245 deletions(-)
arch/x86/include/asm/kvm_host.h +19 -4
index 4aa48fb55361d..190e245aa6670 100644
@@ -637,16 +637,22 @@ struct kvm_vcpu_arch {
 
 	u8 event_exit_inst_len;
 
-	struct kvm_queued_exception {
-		bool pending;
-		bool injected;
+	struct kvm_pending_exception {
+		bool valid;
 		bool has_error_code;
 		u8 nr;
 		u32 error_code;
 		unsigned long payload;
 		bool has_payload;
 		u8 nested_apf;
-	} exception;
+	} pending_exception;
+
+	struct kvm_queued_exception {
+		bool valid;
+		bool has_error_code;
+		u8 nr;
+		u32 error_code;
+	} injected_exception;
 
 	struct kvm_queued_interrupt {
 		bool injected;
@@ -1018,6 +1024,7 @@ struct kvm_arch {
 
 	bool guest_can_read_msr_platform_info;
 	bool exception_payload_enabled;
+	bool exception_separate_injected_pending;
 
 	/* Deflect RDMSR and WRMSR to user space when they trigger a #GP */
 	u32 user_space_msr_mask;
@@ -1351,6 +1358,14 @@ struct kvm_x86_ops {
 
 struct kvm_x86_nested_ops {
 	int (*check_events)(struct kvm_vcpu *vcpu);
+
+	/*
+	 * return value: 0 - delivered vm exit, 1 - exception not intercepted,
+	 * negative - failure
+	 * */
+
+	int (*deliver_exception)(struct kvm_vcpu *vcpu);
+
 	bool (*hv_timer_pending)(struct kvm_vcpu *vcpu);
 	int (*get_state)(struct kvm_vcpu *vcpu,
 			 struct kvm_nested_state __user *user_kvm_nested_state,
arch/x86/include/uapi/asm/kvm.h +11 -3
index 5a3022c8af82b..9556e420e8ecb 100644
@@ -345,9 +345,17 @@ struct kvm_vcpu_events {
 		__u8 smm_inside_nmi;
 		__u8 latched_init;
 	} smi;
-	__u8 reserved[27];
-	__u8 exception_has_payload;
-	__u64 exception_payload;
+
+	__u8 reserved[20];
+
+	struct {
+		__u32 error_code;
+		__u8 nr;
+		__u8 pad;
+		__u8 has_error_code;
+		__u8 has_payload;
+		__u64 payload;
+	} pending_exception;
 };
 
 /* for KVM_GET/SET_DEBUGREGS */
arch/x86/kvm/svm/nested.c +36 -26

121 lines not on this page · raw message

arch/x86/kvm/svm/svm.c +4 -4
index cdbbda37b9419..0a1857f5fe55e 100644
@@ -363,11 +363,11 @@ static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
 static void svm_queue_exception(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-	unsigned nr = vcpu->arch.exception.nr;
-	bool has_error_code = vcpu->arch.exception.has_error_code;
-	u32 error_code = vcpu->arch.exception.error_code;
+	unsigned nr = vcpu->arch.injected_exception.nr;
+	bool has_error_code = vcpu->arch.injected_exception.has_error_code;
+	u32 error_code = vcpu->arch.injected_exception.error_code;
 
-	kvm_deliver_exception_payload(vcpu);
+	WARN_ON(vcpu->arch.pending_exception.valid);
 
 	if (nr == BP_VECTOR && !nrips) {
 		unsigned long rip, old_rip = kvm_rip_read(vcpu);
arch/x86/kvm/vmx/nested.c +63 -51

186 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +7 -7
index f2714d22228de..d480bd48d786f 100644
@@ -1630,8 +1630,8 @@ static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
 	 * vmx_check_nested_events().
 	 */
 	if (nested_cpu_has_mtf(vmcs12) &&
-	    (!vcpu->arch.exception.pending ||
-	     vcpu->arch.exception.nr == DB_VECTOR))
+	    (!vcpu->arch.pending_exception.valid ||
+	     vcpu->arch.pending_exception.nr == DB_VECTOR))
 		vmx->nested.mtf_pending = true;
 	else
 		vmx->nested.mtf_pending = false;
@@ -1659,12 +1659,12 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
 static void vmx_queue_exception(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	unsigned nr = vcpu->arch.exception.nr;
-	bool has_error_code = vcpu->arch.exception.has_error_code;
-	u32 error_code = vcpu->arch.exception.error_code;
+	unsigned nr = vcpu->arch.injected_exception.nr;
+	bool has_error_code = vcpu->arch.injected_exception.has_error_code;
+	u32 error_code = vcpu->arch.injected_exception.error_code;
 	u32 intr_info = nr | INTR_INFO_VALID_MASK;
 
-	kvm_deliver_exception_payload(vcpu);
+	WARN_ON(vcpu->arch.pending_exception.valid);
 
 	if (has_error_code) {
 		vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
@@ -5400,7 +5400,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
 			return 0;
 
 		if (vmx->emulation_required && !vmx->rmode.vm86_active &&
-		    vcpu->arch.exception.pending) {
+		    vcpu->arch.pending_exception.valid) {
 			vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
 			vcpu->run->internal.suberror =
 						KVM_INTERNAL_ERROR_EMULATION;
arch/x86/kvm/x86.c +223 -147

511 lines not on this page · raw message

arch/x86/kvm/x86.h +3 -3
index ee6e01067884d..e3848072c5bdb 100644
@@ -58,8 +58,8 @@ static inline unsigned int __shrink_ple_window(unsigned int val,
 
 static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
 {
-	vcpu->arch.exception.pending = false;
-	vcpu->arch.exception.injected = false;
+	vcpu->arch.pending_exception.valid = false;
+	vcpu->arch.injected_exception.valid = false;
 }
 
 static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
@@ -77,7 +77,7 @@ static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
 
 static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
 {
-	return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
+	return vcpu->arch.injected_exception.valid || vcpu->arch.interrupt.injected ||
 		vcpu->arch.nmi_injected;
 }
 
include/uapi/linux/kvm.h +1 -0
index 63f8f6e956487..d913a46d36b04 100644
@@ -1077,6 +1077,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_SYS_HYPERV_CPUID 191
 #define KVM_CAP_DIRTY_LOG_RING 192
 #define KVM_CAP_X86_BUS_LOCK_EXIT 193
+#define KVM_CAP_EXCEPTION_INJECTED_PENDING 194
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
2.26.2
#8 lore raw
In reply to Maxim Levitsky
Quoted 10 lines · Maxim Levitsky

clone of "kernel-starship-5.11"

Maxim Levitsky (4):
KVM: x86: determine if an exception has an error code only when
injecting it.
KVM: x86: mmu: initialize fault.async_page_fault in walk_addr_generic
KVM: x86: pending exception must be be injected even with an injected
event
kvm: WIP separation of injected and pending exception

Quoted 15 lines
 arch/x86/include/asm/kvm_host.h |  23 +-
 arch/x86/include/uapi/asm/kvm.h |  14 +-
 arch/x86/kvm/mmu/paging_tmpl.h  |   1 +
 arch/x86/kvm/svm/nested.c       |  57 +++--
 arch/x86/kvm/svm/svm.c          |   8 +-
 arch/x86/kvm/vmx/nested.c       | 109 +++++----
 arch/x86/kvm/vmx/vmx.c          |  14 +-
 arch/x86/kvm/x86.c              | 377 +++++++++++++++++++-------------
 arch/x86/kvm/x86.h              |   6 +-
 include/uapi/linux/kvm.h        |   1 +
 10 files changed, 374 insertions(+), 236 deletions(-)
Signature · 1 line
2.26.2

git-publish ate the cover letter, so here it goes:

RFC/WIP: KVM: separate injected and pending exception + few more fixes

This is a result of my deep dive on why do we need special .inject_page_fault for cases when TDP paging is disabled on the host for running nested guests.

First 3 patches fix relatively small issues I found.
Some of them can be squashed with patch 4 assuming that it is accepted.

Patch 4 is WIP and I would like to hear your feedback on it:

Basically the issue is that during delivery of one exception
we (emulator or mmu) can signal another exception, and if the new exception is intercepted by the nested guest, we should do VM exit with
former exception signaled in exitintinfo (or equivalent IDT_VECTORING_INFO_FIELD)

We sadly either loose the former exception and signal an VM exit, or deliver a #DF since we only store either pending or injected exception and we merge them in kvm_multiple_exception although we shouldn't.

Only later we deliver the VM exit in .check_nested_events when already wrong data is in the pending/injected exception.

There are multiple ways to fix it, and I choose somewhat hard but I think the most correct way of dealing with it.

1. I split pending and injected exceptions in kvm_vcpu_arch thus allowing both to co-exist.

2. I made kvm_multiple_exception avoid merging exceptions, but instead only setup either pending or injected exception
(there is another bug that we don't deliver triple fault as nested vm exit, which I'll fix later)

3. I created kvm_deliver_pending_exception which its goal is to convert the pending exception to injected exception or deliver a VM exit with both pending and injected exception/interrupt/nmi.

It itself only deals with non-vmexit cases while it calls a new 'kvm_x86_ops.nested_ops->deliver_exception' to deliver exception VM exit if needed.

The later implementation is simple as it just checks if we should VM exit and then delivers both exceptions (or interrupt and exception, in case interrupt delivery was interrupted by exception).
This new callback returns 0 if it had delivered this VM exit, 0 if no vm exit is needed, or -EBUSY when nested run is pending, in which case the exception delivery will be retried after nested run is done.

kvm_deliver_pending_exception is called each time we inject pending events and all exception related code is removed from .check_nested_events which now only deals with pending interrupts and events such as INIT,NMI,SMI, etc.

New KVM cap is added to expose both pending and injected exception via KVM_GET_VCPU_EVENTS/KVM_SET_VCPU_EVENTS

If this cap is not enabled, and we have both pending and injected exception when KVM_GET_VCPU_EVENTS is called, the exception is delivered.

The code was tested with SVM, and it currently seems to pass all the tests I usually do (including nested migration). KVM unit tests seem to pass as well.

I still almost sure that I broke something since this is far from trivial change, therefore this is RFC/WIP.

Also VMX side was not yet tested other than basic compile and I am sure that there are at least few issues that remain to be fixed.

I should also note that with these patches I can boot nested guests with npt=0 without any changes to .inject_page_fault.

I also wrote 2 KVM unit tests to test for this issue, and for similar issue when interrupt is lost when delivery of it causes exception.
These tests pass now.

Best regards,
Maxim Levitsky