Skip to content

Commit a4782b4

Browse files
lixianglaiKexyBiscuit
authored andcommitted
DEEPIN: LoongArch: KVM: enable ptw for kvm
Upstream: no Add the ptw feature bit to cpucfg Signed-off-by: Xianglai Li <[email protected]> Link: deepin-community/kernel@bc32c6c9c36d Link: https://git.kernel.org/torvalds/c/f3b65bbaed7c # conflict Signed-off-by: Xi Ruoyao <[email protected]>
1 parent 080c0f2 commit a4782b4

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

arch/loongarch/include/asm/kvm_mmu.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
#define _KVM_FLUSH_PGTABLE 0x1
2020
#define _KVM_HAS_PGMASK 0x2
21+
22+
/* If the page entry has a write attribute,
23+
* we use the page entry 50bit(KVM_RECORD_PAGE_WRITE_ABLE)
24+
* to record it to restore the write attribute of the page entry,
25+
* in the fast path kvm_map_page_fast for page table processing
26+
*/
27+
#define KVM_RECORD_PAGE_WRITE_ABLE_SHIFT 50
28+
#define KVM_RECORD_PAGE_WRITE_ABLE (_ULCAST_(1) << KVM_RECORD_PAGE_WRITE_ABLE_SHIFT)
29+
2130
#define kvm_pfn_pte(pfn, prot) (((pfn) << PFN_PTE_SHIFT) | pgprot_val(prot))
2231
#define kvm_pte_pfn(x) ((phys_addr_t)((x & _PFN_MASK) >> PFN_PTE_SHIFT))
2332

@@ -53,6 +62,7 @@ static inline void kvm_set_pte(kvm_pte_t *ptep, kvm_pte_t val)
5362
}
5463

5564
static inline int kvm_pte_write(kvm_pte_t pte) { return pte & _PAGE_WRITE; }
65+
static inline int kvm_record_pte_write_able(kvm_pte_t pte) { return pte & KVM_RECORD_PAGE_WRITE_ABLE; }
5666
static inline int kvm_pte_dirty(kvm_pte_t pte) { return pte & _PAGE_DIRTY; }
5767
static inline int kvm_pte_young(kvm_pte_t pte) { return pte & _PAGE_ACCESSED; }
5868
static inline int kvm_pte_huge(kvm_pte_t pte) { return pte & _PAGE_HUGE; }
@@ -74,7 +84,7 @@ static inline kvm_pte_t kvm_pte_mkdirty(kvm_pte_t pte)
7484

7585
static inline kvm_pte_t kvm_pte_mkclean(kvm_pte_t pte)
7686
{
77-
return pte & ~_PAGE_DIRTY;
87+
return pte & (~(_PAGE_DIRTY | _PAGE_WRITE));
7888
}
7989

8090
static inline kvm_pte_t kvm_pte_mkhuge(kvm_pte_t pte)

arch/loongarch/kvm/exit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static int kvm_emu_cpucfg(struct kvm_vcpu *vcpu, larch_inst inst)
4444
switch (index) {
4545
case 0 ... (KVM_MAX_CPUCFG_REGS - 1):
4646
vcpu->arch.gprs[rd] = vcpu->arch.cpucfg[index];
47+
if (cpu_has_ptw && (index == LOONGARCH_CPUCFG2))
48+
vcpu->arch.gprs[rd] |= CPUCFG2_PTW;
4749
break;
4850
case CPUCFG_KVM_SIG:
4951
/* CPUCFG emulation between 0x40000000 -- 0x400000ff */

arch/loongarch/kvm/mmu.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,14 @@ static int kvm_map_page_fast(struct kvm_vcpu *vcpu, unsigned long gpa, bool writ
568568

569569
/* Track access to pages marked old */
570570
new = kvm_pte_mkyoung(*ptep);
571+
572+
/* We restore the write property of
573+
* the page table entry according to
574+
* KVM_RECORD_PAGE_WRITE_ABLE
575+
*/
576+
if (kvm_record_pte_write_able(new))
577+
new |= _PAGE_WRITE;
578+
571579
if (write && !kvm_pte_dirty(new)) {
572580
if (!kvm_pte_write(new)) {
573581
ret = -EFAULT;
@@ -856,7 +864,12 @@ static int kvm_map_page(struct kvm_vcpu *vcpu, unsigned long gpa, bool write)
856864
prot_bits |= _CACHE_SUC;
857865

858866
if (writeable) {
859-
prot_bits |= _PAGE_WRITE;
867+
/* If the page entry has a write attribute,
868+
* we use the page entry 50bit(KVM_RECORD_PAGE_WRITE_ABLE)
869+
* to record it to restore the write attribute of the page entry,
870+
* in the fast path kvm_map_page_fast for page table processing
871+
*/
872+
prot_bits |= _PAGE_WRITE | KVM_RECORD_PAGE_WRITE_ABLE;
860873
if (write)
861874
prot_bits |= __WRITEABLE;
862875
}

0 commit comments

Comments
 (0)