Skip to content

Commit 8dc9377

Browse files
committed
KVM: x86/PVM: Use the same guest CR3 setting logic when PCID is not available
Use the same guest CR3 setting logic when PCID is not available for simplicity, so merge pvm_set_host_cr3_for_guest_{with|without}_host_pcid() as pvm_set_host_cr3(). Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
1 parent 8ce7ef8 commit 8dc9377

1 file changed

Lines changed: 26 additions & 44 deletions

File tree

arch/x86/kvm/pvm/pvm.c

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,16 @@ static bool check_switch_cr3(struct vcpu_pvm *pvm, u64 switch_host_cr3)
754754
return false;
755755
if (!VALID_PAGE(root))
756756
return false;
757-
if (host_pcid_owner(switch_host_cr3 & X86_CR3_PCID_MASK) != pvm)
758-
return false;
759-
if (host_pcid_root(switch_host_cr3 & X86_CR3_PCID_MASK) != root)
760-
return false;
761757
if (root != (switch_host_cr3 & CR3_ADDR_MASK))
762758
return false;
763759

760+
if (static_cpu_has(X86_FEATURE_PCID)) {
761+
if (host_pcid_owner(switch_host_cr3 & X86_CR3_PCID_MASK) != pvm)
762+
return false;
763+
if (host_pcid_root(switch_host_cr3 & X86_CR3_PCID_MASK) != root)
764+
return false;
765+
}
766+
764767
return true;
765768
}
766769

@@ -785,24 +788,31 @@ static void pvm_pgtbl_preload_for_guest_with_host_pcid(struct vcpu_pvm *pvm, u64
785788
return;
786789
}
787790

788-
static void pvm_set_host_cr3_for_guest_with_host_pcid(struct vcpu_pvm *pvm)
791+
static void pvm_set_host_cr3_for_guest(struct vcpu_pvm *pvm)
789792
{
790-
u64 root_hpa = pvm->vcpu.arch.mmu->root.hpa;
791-
bool flush = false;
792-
u32 host_pcid = host_pcid_get(pvm, root_hpa, &flush);
793-
u64 hw_cr3 = root_hpa | host_pcid;
793+
u64 hw_cr3 = pvm->vcpu.arch.mmu->root.hpa;
794+
u64 enter_hw_cr3 = hw_cr3;
794795
u64 switch_host_cr3;
795796

796-
if (!flush)
797-
hw_cr3 |= CR3_NOFLUSH;
798-
this_cpu_write(cpu_tss_rw.tss_ex.enter_cr3, hw_cr3);
797+
if (static_cpu_has(X86_FEATURE_PCID)) {
798+
bool flush = false;
799+
u32 host_pcid = host_pcid_get(pvm, hw_cr3, &flush);
800+
801+
enter_hw_cr3 |= host_pcid;
802+
if (!flush)
803+
enter_hw_cr3 |= CR3_NOFLUSH;
804+
hw_cr3 |= host_pcid | CR3_NOFLUSH;
805+
}
806+
807+
this_cpu_write(cpu_tss_rw.tss_ex.enter_cr3, enter_hw_cr3);
799808

800809
if (is_smod(pvm)) {
801-
this_cpu_write(cpu_tss_rw.tss_ex.smod_cr3, hw_cr3 | CR3_NOFLUSH);
810+
this_cpu_write(cpu_tss_rw.tss_ex.smod_cr3, hw_cr3);
802811
switch_host_cr3 = this_cpu_read(cpu_tss_rw.tss_ex.umod_cr3);
803-
pvm_pgtbl_preload_for_guest_with_host_pcid(pvm, &switch_host_cr3);
812+
if (static_cpu_has(X86_FEATURE_PCID))
813+
pvm_pgtbl_preload_for_guest_with_host_pcid(pvm, &switch_host_cr3);
804814
} else {
805-
this_cpu_write(cpu_tss_rw.tss_ex.umod_cr3, hw_cr3 | CR3_NOFLUSH);
815+
this_cpu_write(cpu_tss_rw.tss_ex.umod_cr3, hw_cr3);
806816
switch_host_cr3 = this_cpu_read(cpu_tss_rw.tss_ex.smod_cr3);
807817
}
808818

@@ -812,30 +822,6 @@ static void pvm_set_host_cr3_for_guest_with_host_pcid(struct vcpu_pvm *pvm)
812822
pvm->switch_flags |= SWITCH_FLAGS_NO_DS_CR3;
813823
}
814824

815-
static void pvm_set_host_cr3_for_guest_without_host_pcid(struct vcpu_pvm *pvm)
816-
{
817-
u64 root_hpa = pvm->vcpu.arch.mmu->root.hpa;
818-
u64 switch_root = 0;
819-
u64 prev_root_hpa = pvm->vcpu.arch.mmu->prev_roots[0].hpa;
820-
821-
if (VALID_PAGE(prev_root_hpa) &&
822-
pvm->vcpu.arch.mmu->prev_roots[0].pgd == pvm->msr_switch_cr3) {
823-
switch_root = prev_root_hpa;
824-
pvm->switch_flags &= ~SWITCH_FLAGS_NO_DS_CR3;
825-
} else {
826-
pvm->switch_flags |= SWITCH_FLAGS_NO_DS_CR3;
827-
}
828-
829-
this_cpu_write(cpu_tss_rw.tss_ex.enter_cr3, root_hpa);
830-
if (is_smod(pvm)) {
831-
this_cpu_write(cpu_tss_rw.tss_ex.smod_cr3, root_hpa);
832-
this_cpu_write(cpu_tss_rw.tss_ex.umod_cr3, switch_root);
833-
} else {
834-
this_cpu_write(cpu_tss_rw.tss_ex.umod_cr3, root_hpa);
835-
this_cpu_write(cpu_tss_rw.tss_ex.smod_cr3, switch_root);
836-
}
837-
}
838-
839825
static void pvm_set_host_cr3_for_hypervisor(struct vcpu_pvm *pvm)
840826
{
841827
unsigned long cr3;
@@ -854,11 +840,7 @@ static void pvm_set_host_cr3_for_hypervisor(struct vcpu_pvm *pvm)
854840
static void pvm_set_host_cr3(struct vcpu_pvm *pvm)
855841
{
856842
pvm_set_host_cr3_for_hypervisor(pvm);
857-
858-
if (static_cpu_has(X86_FEATURE_PCID))
859-
pvm_set_host_cr3_for_guest_with_host_pcid(pvm);
860-
else
861-
pvm_set_host_cr3_for_guest_without_host_pcid(pvm);
843+
pvm_set_host_cr3_for_guest(pvm);
862844
}
863845

864846
static void pvm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,

0 commit comments

Comments
 (0)