From: David Gibson Date: Wed, 26 Sep 2018 03:47:14 +0000 Subject: Re: [RFC PATCH 16/32] KVM: PPC: Book3S HV: Make kvmppc_mmu_radix_xlate process/partition table agnos Message-Id: <20180926034714.GR30868@umbus.fritz.box> MIME-Version: 1 Content-Type: multipart/mixed; boundary="ItuD4UcHtAunF/cq" List-Id: References: <1537524123-9578-17-git-send-email-paulus@ozlabs.org> In-Reply-To: <1537524123-9578-17-git-send-email-paulus@ozlabs.org> To: kvm-ppc@vger.kernel.org --ItuD4UcHtAunF/cq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Sep 21, 2018 at 08:01:47PM +1000, Paul Mackerras wrote: > From: Suraj Jitindar Singh >=20 > kvmppc_mmu_radix_xlate() is used to translate an effective address > through the process tables. The process table and partition tables have > identical layout. Exploit this fact to make the kvmppc_mmu_radix_xlate() > function able to translate either an effective address through the > process tables or a guest real address through the partition tables. >=20 > [paulus@ozlabs.org - reduced diffs from previous code] >=20 > Signed-off-by: Suraj Jitindar Singh > Signed-off-by: Paul Mackerras Reviewed-by: David Gibson > --- > arch/powerpc/include/asm/kvm_book3s.h | 3 + > arch/powerpc/kvm/book3s_64_mmu_radix.c | 109 +++++++++++++++++++++++----= ------ > 2 files changed, 78 insertions(+), 34 deletions(-) >=20 > diff --git a/arch/powerpc/include/asm/kvm_book3s.h b/arch/powerpc/include= /asm/kvm_book3s.h > index dd18d81..91c9779 100644 > --- a/arch/powerpc/include/asm/kvm_book3s.h > +++ b/arch/powerpc/include/asm/kvm_book3s.h > @@ -188,6 +188,9 @@ extern int kvmppc_book3s_hcall_implemented(struct kvm= *kvm, unsigned long hc); > extern int kvmppc_book3s_radix_page_fault(struct kvm_run *run, > struct kvm_vcpu *vcpu, > unsigned long ea, unsigned long dsisr); > +extern int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t= eaddr, > + struct kvmppc_pte *gpte, u64 table, > + int table_index, u64 *pte_ret_p); > extern int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, > struct kvmppc_pte *gpte, bool data, bool iswrite); > extern int kvmppc_init_vm_radix(struct kvm *kvm); > diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/bo= ok3s_64_mmu_radix.c > index 71951b5..f2976f4 100644 > --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c > +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c > @@ -29,83 +29,92 @@ > */ > static int p9_supported_radix_bits[4] =3D { 5, 9, 9, 13 }; > =20 > -int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, > - struct kvmppc_pte *gpte, bool data, bool iswrite) > +/* > + * Used to walk a partition or process table radix tree in guest memory > + * Note: We exploit the fact that a partition table and a process > + * table have the same layout, a partition-scoped page table and a > + * process-scoped page table have the same layout, and the 2nd > + * doubleword of a partition table entry has the same layout as > + * the PTCR register. > + */ > +int kvmppc_mmu_radix_translate_table(struct kvm_vcpu *vcpu, gva_t eaddr, > + struct kvmppc_pte *gpte, u64 table, > + int table_index, u64 *pte_ret_p) > { > struct kvm *kvm =3D vcpu->kvm; > - u32 pid; > int ret, level, ps; > - __be64 prte, rpte; > - unsigned long ptbl; > - unsigned long root, pte, index; > + unsigned long ptbl, root; > unsigned long rts, bits, offset; > - unsigned long gpa; > - unsigned long proc_tbl_size; > + unsigned long size, index; > + struct prtb_entry entry; > + u64 pte, base, gpa; > + __be64 rpte; > =20 > - /* Work out effective PID */ > - switch (eaddr >> 62) { > - case 0: > - pid =3D vcpu->arch.pid; > - break; > - case 3: > - pid =3D 0; > - break; > - default: > + if ((table & PRTS_MASK) > 24) > return -EINVAL; > - } > - proc_tbl_size =3D 1 << ((kvm->arch.process_table & PRTS_MASK) + 12); > - if (pid * 16 >=3D proc_tbl_size) > + size =3D 1ul << ((table & PRTS_MASK) + 12); > + > + /* Is the table big enough to contain this entry? */ > + if ((table_index * sizeof(entry)) >=3D size) > return -EINVAL; > =20 > - /* Read partition table to find root of tree for effective PID */ > - ptbl =3D (kvm->arch.process_table & PRTB_MASK) + (pid * 16); > - ret =3D kvm_read_guest(kvm, ptbl, &prte, sizeof(prte)); > + /* Read the table to find the root of the radix tree */ > + ptbl =3D (table & PRTB_MASK) + (table_index * sizeof(entry)); > + ret =3D kvm_read_guest(kvm, ptbl, &entry, sizeof(entry)); > if (ret) > return ret; > =20 > - root =3D be64_to_cpu(prte); > + /* Root is stored in the first double word */ > + root =3D be64_to_cpu(entry.prtb0); > rts =3D ((root & RTS1_MASK) >> (RTS1_SHIFT - 3)) | > ((root & RTS2_MASK) >> RTS2_SHIFT); > bits =3D root & RPDS_MASK; > - root =3D root & RPDB_MASK; > + base =3D root & RPDB_MASK; > =20 > offset =3D rts + 31; > =20 > - /* current implementations only support 52-bit space */ > + /* Current implementations only support 52-bit space */ > if (offset !=3D 52) > return -EINVAL; > =20 > + /* Walk each level of the radix tree */ > for (level =3D 3; level >=3D 0; --level) { > + /* Check a valid size */ > if (level && bits !=3D p9_supported_radix_bits[level]) > return -EINVAL; > if (level =3D=3D 0 && !(bits =3D=3D 5 || bits =3D=3D 9)) > return -EINVAL; > offset -=3D bits; > index =3D (eaddr >> offset) & ((1UL << bits) - 1); > - /* check that low bits of page table base are zero */ > - if (root & ((1UL << (bits + 3)) - 1)) > + /* Check that low bits of page table base are zero */ > + if (base & ((1UL << (bits + 3)) - 1)) > return -EINVAL; > - ret =3D kvm_read_guest(kvm, root + index * 8, > + /* Read the entry from guest memory */ > + ret =3D kvm_read_guest(kvm, base + (index * sizeof(rpte)), > &rpte, sizeof(rpte)); > if (ret) > return ret; > pte =3D __be64_to_cpu(rpte); > if (!(pte & _PAGE_PRESENT)) > return -ENOENT; > + /* Check if a leaf entry */ > if (pte & _PAGE_PTE) > break; > - bits =3D pte & 0x1f; > - root =3D pte & 0x0fffffffffffff00ul; > + /* Get ready to walk the next level */ > + base =3D pte & RPDB_MASK; > + bits =3D pte & RPDS_MASK; > } > - /* need a leaf at lowest level; 512GB pages not supported */ > + > + /* Need a leaf at lowest level; 512GB pages not supported */ > if (level < 0 || level =3D=3D 3) > return -EINVAL; > =20 > - /* offset is now log base 2 of the page size */ > + /* We found a valid leaf PTE */ > + /* Offset is now log base 2 of the page size */ > gpa =3D pte & 0x01fffffffffff000ul; > if (gpa & ((1ul << offset) - 1)) > return -EINVAL; > - gpa +=3D eaddr & ((1ul << offset) - 1); > + gpa |=3D eaddr & ((1ul << offset) - 1); > for (ps =3D MMU_PAGE_4K; ps < MMU_PAGE_COUNT; ++ps) > if (offset =3D=3D mmu_psize_defs[ps].shift) > break; > @@ -118,6 +127,38 @@ int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gv= a_t eaddr, > gpte->may_read =3D !!(pte & _PAGE_READ); > gpte->may_write =3D !!(pte & _PAGE_WRITE); > gpte->may_execute =3D !!(pte & _PAGE_EXEC); > + > + if (pte_ret_p) > + *pte_ret_p =3D pte; > + > + return 0; > +} > + > +int kvmppc_mmu_radix_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, > + struct kvmppc_pte *gpte, bool data, bool iswrite) > +{ > + u32 pid; > + u64 pte; > + int ret; > + > + /* Work out effective PID */ > + switch (eaddr >> 62) { > + case 0: > + pid =3D vcpu->arch.pid; > + break; > + case 3: > + pid =3D 0; > + break; > + default: > + return -EINVAL; > + } > + > + ret =3D kvmppc_mmu_radix_translate_table(vcpu, eaddr, gpte, > + vcpu->kvm->arch.process_table, pid, &pte); > + if (ret) > + return ret; > + > + /* Check privilege (applies only to process scoped translations) */ > if (kvmppc_get_msr(vcpu) & MSR_PR) { > if (pte & _PAGE_PRIVILEGED) { > gpte->may_read =3D 0; --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --ItuD4UcHtAunF/cq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEdfRlhq5hpmzETofcbDjKyiDZs5IFAlurAUAACgkQbDjKyiDZ s5IYJBAAmgUbjS0sYlYOg73ilIkBeI6LzrnDN1aIYVBi4rAZBCASz+1ESeVjinCv ikefeQN1NBSt4H/4dmhl8vbwATqyAJj1HR8gBNXsfIN/7Vt3Zpa+vwpiLpKsiWKC lZwjvv5pJeNO6IIL63n4e/ZUbr+vSY/V8odUZKD9u9I+g7rl83vVJ6YzOdv/AWlw zmMzxWNUQm0L8NiAiyAHmVOgLtz2HsG6Zpq0596FXCia7ymik6kHM90um+N7+BtX EPTSERy9B5iBV5TL0DpR4q7am1js5Bn2lbeMqBC+EdRonylrAcnlZoHiVmlr2v+1 Z2Bc7yjP13JpvMZ9rGLuX15HgCJvvSG370Xl/Jv1cvUb29B3MhAZ+yWMSqFmwId5 9TyHS/hK4lUBd0vqZaMJReApUy0BLx79P/DHlBN9UA3uF5Ya5/PHRJI4ACLXxw1p vIgn/XaHdYhJOtuBK2y7TGVCG4RwqWQyGD4STOvGyd0/kVWsXprimYkIChOGNgot ofbV/3g85sSasHSyvyP2n5A4LihrWVakuJfcAz+dqe2KF7B0+2q/WUrwpftwg7fC Fk99lsL2S0H9QduyRRqyDqWD0tJi387xhTsybtoQLyKFfcHMY0lz6NdtKdbGqiUr jr5fQSiMJz7aXSLT1mYHoBRuyI2h/vBc6Y0Z2QTAaaywkUOx818= =AZXM -----END PGP SIGNATURE----- --ItuD4UcHtAunF/cq--