]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: arm64: Handle endianness in read helper for emulated PTW
authorOliver Upton <oupton@kernel.org>
Mon, 24 Nov 2025 19:01:50 +0000 (11:01 -0800)
committerOliver Upton <oupton@kernel.org>
Mon, 1 Dec 2025 08:44:02 +0000 (00:44 -0800)
Implementing FEAT_HAFDBS means adding another descriptor accessor that
needs to deal with the guest-configured endianness. Prepare by moving
the endianness handling into the read accessor and out of the main body
of the S1/S2 PTWs.

Reviewed-by: Marc Zyngier <maz@kernel.org>
Tested-by: Marc Zyngier <maz@kernel.org>
Link: https://msgid.link/20251124190158.177318-9-oupton@kernel.org
Signed-off-by: Oliver Upton <oupton@kernel.org>
arch/arm64/kvm/at.c
arch/arm64/kvm/nested.c

index be26d5aa668c39bf1c48742f5f730b4d4ad6d90f..a295a37dd3b1306e6684091f3c5c0074f5599719 100644 (file)
@@ -362,6 +362,24 @@ transfault:
        return -EFAULT;
 }
 
+static int kvm_read_s1_desc(struct kvm_vcpu *vcpu, u64 pa, u64 *desc,
+                           struct s1_walk_info *wi)
+{
+       u64 val;
+       int r;
+
+       r = kvm_read_guest(vcpu->kvm, pa, &val, sizeof(val));
+       if (r)
+               return r;
+
+       if (wi->be)
+               *desc = be64_to_cpu((__force __be64)val);
+       else
+               *desc = le64_to_cpu((__force __le64)val);
+
+       return 0;
+}
+
 static int walk_s1(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
                   struct s1_walk_result *wr, u64 va)
 {
@@ -414,17 +432,12 @@ static int walk_s1(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
                                return ret;
                }
 
-               ret = kvm_read_guest(vcpu->kvm, ipa, &desc, sizeof(desc));
+               ret = kvm_read_s1_desc(vcpu, ipa, &desc, wi);
                if (ret) {
                        fail_s1_walk(wr, ESR_ELx_FSC_SEA_TTW(level), false);
                        return ret;
                }
 
-               if (wi->be)
-                       desc = be64_to_cpu((__force __be64)desc);
-               else
-                       desc = le64_to_cpu((__force __le64)desc);
-
                /* Invalid descriptor */
                if (!(desc & BIT(0)))
                        goto transfault;
index 94eff8307aada85c33fdad38684ffdc15d749d84..75d26c0ba3e0cd9d8038d6cbe546ae10a2881ceb 100644 (file)
@@ -197,9 +197,26 @@ static int check_output_size(struct s2_walk_info *wi, phys_addr_t output)
        return 0;
 }
 
-static int read_guest_s2_desc(struct kvm_vcpu *vcpu, phys_addr_t pa, u64 *desc)
+static int read_guest_s2_desc(struct kvm_vcpu *vcpu, phys_addr_t pa, u64 *desc,
+                             struct s2_walk_info *wi)
 {
-       return kvm_read_guest(vcpu->kvm, pa, desc, sizeof(*desc));
+       u64 val;
+       int r;
+
+       r = kvm_read_guest(vcpu->kvm, pa, &val, sizeof(val));
+       if (r)
+               return r;
+
+       /*
+        * Handle reversedescriptors if endianness differs between the
+        * host and the guest hypervisor.
+        */
+       if (wi->be)
+               *desc = be64_to_cpu((__force __be64)val);
+       else
+               *desc = le64_to_cpu((__force __le64)val);
+
+       return 0;
 }
 
 /*
@@ -260,19 +277,10 @@ static int walk_nested_s2_pgd(struct kvm_vcpu *vcpu, phys_addr_t ipa,
                        >> (addr_bottom - 3);
 
                paddr = base_addr | index;
-               ret = read_guest_s2_desc(vcpu, paddr, &desc);
+               ret = read_guest_s2_desc(vcpu, paddr, &desc, wi);
                if (ret < 0)
                        return ret;
 
-               /*
-                * Handle reversedescriptors if endianness differs between the
-                * host and the guest hypervisor.
-                */
-               if (wi->be)
-                       desc = be64_to_cpu((__force __be64)desc);
-               else
-                       desc = le64_to_cpu((__force __le64)desc);
-
                /* Check for valid descriptor at this point */
                if (!(desc & 1) || ((desc & 3) == 1 && level == 3)) {
                        out->esr = compute_fsc(level, ESR_ELx_FSC_FAULT);