Skip to contentSkip to conversation

selftests: KVM/x86: Add APIC state into kvm_x86_state

PATCH New Mingwei Zhang 1 participant 1 message May 25, 2022

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

PATCH 1/1 selftests: KVM/x86: Add APIC state into kvm_x86_state

Add APIC state into kvm_x86_state and properly save/restore it in vcpu_{save,load}_state().

Doing so fix a bug that was triggered internally in Google. The selftest (WIP) creates an L1 which then creates an L2 and runs L2 in vcpu0. Before L2 finish, it exits to L0, save the state, stops the VM, restarts it and add the vcpu back and finally load the state back. The test uses another vcpu to shoot a posted interrupt. The result demonstrates that the L2, after this vm save/restore, was not able to receive posted interrupt.

Our debugging shows that it is the APIC state was software disabled after vcpu_load_state(). Adding the APIC state into the kvm_x86_state and loading it in vcpu_load_state() fixes the issue.

Cc Jim Mattson <[email protected]>
---
 tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
 tools/testing/selftests/kvm/lib/x86_64/processor.c     | 8 ++++++++
 2 files changed, 9 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
index d0d51adec76eb..e4683429bd8b8 100644
@@ -123,6 +123,7 @@ struct kvm_x86_state {
 		struct kvm_nested_state nested;
 		char nested_[16384];
 	};
+	struct kvm_lapic_state apic;
 	struct kvm_msrs msrs;
 };
 
tools/testing/selftests/kvm/lib/x86_64/processor.c +8 -0
index 33ea5e9955d9b..113f4ce752579 100644
@@ -1173,6 +1173,10 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_DEBUGREGS, r: %i",
 		    r);
 
+	r = ioctl(vcpu->fd, KVM_GET_LAPIC, &state->apic);
+	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_LAPIC, r: %i",
+		    r);
+
 	free(list);
 	return state;
 }
@@ -1222,6 +1226,10 @@ void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_x86_state *s
 		TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_NESTED_STATE, r: %i",
 			    r);
 	}
+
+	r = ioctl(vcpu->fd, KVM_SET_LAPIC, &state->apic);
+	TEST_ASSERT(r == 0,
+		"Unexpected result from KVM_SET_LAPIC, r: %i", r);
 }
 
 void kvm_x86_state_cleanup(struct kvm_x86_state *state)

base-commit: f3f19f939c11925dadd3f4776f99f8c278a7017b

Signature · 1 line
2.36.1.124.g0e6072fb45-goog