Skip to contentSkip to conversation

target/arm: fix s2prot not set for two-stage PMSA translations

PATCH In review Jose Martins 3 participants 5 messages Mar 21 – Mar 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/arm: fix s2prot not set for two-stage PMSA translations

commit 015cefc0ed65 ("target/arm: Add in_prot_check to S1Translate") changed get_phys_addr_twostage() to combine stage 1 and stage 2 permissions using the new s2prot field:

  result->f.prot = s1_prot & result->s2prot;

The LPAE stage 2 path sets result->s2prot explicitly, but the PMSA stage 2 path (get_phys_addr_pmsav8) only sets result->f.prot, leaving s2prot at zero. This causes the combined permission to be zero, resulting in addr_read being set to -1 in the TLB entry and triggering an assertion in atomic_mmu_lookup() when the guest executes an atomic instruction on a two-stage PMSA platform (e.g. Cortex-R52 with EL2).

Set s2prot from f.prot after the PMSA stage 2 lookup, consistent with what the LPAE path does.

Fixes 015cefc0ed65 ("target/arm: Add in_prot_check to S1Translate")

---
This issue was found while running a Bao hypervisor [1] demo on the mps3-an536 machine, which features a Cortex-R52 with virtualization support (PMSAv8 with EL2). Bao uses the stage 2 PMSA MPU to partition memory between guest VMs. With QEMU >= 10.2.0 the demo crashes immediately after the first guest VCPU is scheduled, when the guest executes its first atomic instruction:

ERROR:../accel/tcg/cputlb.c:1848:atomic_mmu_lookup: \
code should not be reached

To reproduce, clone the bao-demos repository [2] and follow the instructions for the mps3-an536 platform and baremetal demo. With this patch applied, the demo boots successfully.

[1] https://github.com/bao-project/bao-hypervisor
[2] https://github.com/bao-project/bao-demos

 target/arm/ptw.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/arm/ptw.c +7 -0
index 8b8dc09e72..e289f88124 100644
@@ -3200,6 +3200,13 @@ static bool get_phys_addr_pmsav8(CPUARMState *env,
 
     ret = pmsav8_mpu_lookup(env, address, access_type, ptw->in_prot_check,
                             mmu_idx, secure, result, fi, NULL);
+    /*
+     * For two-stage PMSA translations, s2prot holds the stage 2
+     * permissions to be combined with stage 1 in get_phys_addr_twostage().
+     */
+    if (regime_is_stage2(mmu_idx)) {
+        result->s2prot = result->f.prot;
+    }
     if (sattrs.subpage) {
         result->f.lg_page_size = 0;
     }
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jose Martins

commit 015cefc0ed65 ("target/arm: Add in_prot_check to S1Translate") changed get_phys_addr_twostage() to combine stage 1 and stage 2 permissions using the new s2prot field:

  result->f.prot = s1_prot & result->s2prot;

Hmm? That commit doesn't add the s2prot field or touch the get_phys_addr_twostage() function. I guess you mean a811c5dafb7 ("target/arm: Implement get_S2prot_indirect") ?

Quoted 22 lines

The LPAE stage 2 path sets result->s2prot explicitly, but the PMSA stage 2 path (get_phys_addr_pmsav8) only sets result->f.prot, leaving s2prot at zero. This causes the combined permission to be zero, resulting in addr_read being set to -1 in the TLB entry and triggering an assertion in atomic_mmu_lookup() when the guest executes an atomic instruction on a two-stage PMSA platform (e.g. Cortex-R52 with EL2).

Set s2prot from f.prot after the PMSA stage 2 lookup, consistent with what the LPAE path does.

Fixes 015cefc0ed65 ("target/arm: Add in_prot_check to S1Translate")
target/arm/ptw.c +0 -0
index 8b8dc09e72..e289f88124 100644
@@ -3200,6 +3200,13 @@ static bool get_phys_addr_pmsav8(CPUARMState *env,
 
     ret = pmsav8_mpu_lookup(env, address, access_type, ptw->in_prot_check,
                             mmu_idx, secure, result, fi, NULL);

target/arm/ptw.c static bool get_phys_addr_pmsav8(CPUARMState *env, · line 3212

Quoted 10 lines
target/arm/ptw.c +7 -0
+    /*
+     * For two-stage PMSA translations, s2prot holds the stage 2
+     * permissions to be combined with stage 1 in get_phys_addr_twostage().
+     */
+    if (regime_is_stage2(mmu_idx)) {
+        result->s2prot = result->f.prot;
+    }
     if (sattrs.subpage) {
         result->f.lg_page_size = 0;
     }

The code change looks correct:

and we should
Cc: [email protected]

thanks
-- PMM

#2 lore raw
Quoted 3 lines

Hmm? That commit doesn't add the s2prot field or touch the get_phys_addr_twostage() function. I guess you mean a811c5dafb7 ("target/arm: Implement get_S2prot_indirect") ?

Yes, sorry about the confusion.

Quoted 1 line

The code change looks correct:

Perfect. Any other actions points on my side?

Also, to clarify, to reproduce the issue, the bao-tests repository branch to be used is at [2].

[2] https://github.com/bao-project/bao-demos/tree/feat/mps3-an536

Thanks

#3 lore raw
Quoted 10 lines · Jose Martins
Quoted 4 lines

Hmm? That commit doesn't add the s2prot field or touch the get_phys_addr_twostage() function. I guess you mean a811c5dafb7 ("target/arm: Implement get_S2prot_indirect") ?

Yes, sorry about the confusion.

Quoted 1 line

The code change looks correct:

Perfect. Any other actions points on my side?

Nope; I'll pick the bugfix up to go into the 11.0 release.

-- PMM

Reviewed-by on v1 1/1
#4 lore raw
In reply to Jose Martins

Hi Jose,

Quoted 47 lines · Jose Martins

commit 015cefc0ed65 ("target/arm: Add in_prot_check to S1Translate") changed get_phys_addr_twostage() to combine stage 1 and stage 2 permissions using the new s2prot field:

   result->f.prot = s1_prot & result->s2prot;

The LPAE stage 2 path sets result->s2prot explicitly, but the PMSA stage 2 path (get_phys_addr_pmsav8) only sets result->f.prot, leaving s2prot at zero. This causes the combined permission to be zero, resulting in addr_read being set to -1 in the TLB entry and triggering an assertion in atomic_mmu_lookup() when the guest executes an atomic instruction on a two-stage PMSA platform (e.g. Cortex-R52 with EL2).

Set s2prot from f.prot after the PMSA stage 2 lookup, consistent with what the LPAE path does.

Fixes 015cefc0ed65 ("target/arm: Add in_prot_check to S1Translate")

---
This issue was found while running a Bao hypervisor [1] demo on the mps3-an536 machine, which features a Cortex-R52 with virtualization support (PMSAv8 with EL2). Bao uses the stage 2 PMSA MPU to partition memory between guest VMs. With QEMU >= 10.2.0 the demo crashes immediately after the first guest VCPU is scheduled, when the guest executes its first atomic instruction:

ERROR:../accel/tcg/cputlb.c:1848:atomic_mmu_lookup: \
code should not be reached

To reproduce, clone the bao-demos repository [2] and follow the instructions for the mps3-an536 platform and baremetal demo. With this patch applied, the demo boots successfully.

[1] https://github.com/bao-project/bao-hypervisor
[2] https://github.com/bao-project/bao-demos

  target/arm/ptw.c | 7 +++++++
  1 file changed, 7 insertions(+)
target/arm/ptw.c +0 -0
index 8b8dc09e72..e289f88124 100644
@@ -3200,6 +3200,13 @@ static bool get_phys_addr_pmsav8(CPUARMState *env,
 
      ret = pmsav8_mpu_lookup(env, address, access_type, ptw->in_prot_check,
                              mmu_idx, secure, result, fi, NULL);

target/arm/ptw.c static bool get_phys_addr_pmsav8(CPUARMState *env, · line 3212

Quoted 10 lines
target/arm/ptw.c +7 -0
+    /*
+     * For two-stage PMSA translations, s2prot holds the stage 2
+     * permissions to be combined with stage 1 in get_phys_addr_twostage().
+     */
+    if (regime_is_stage2(mmu_idx)) {
+        result->s2prot = result->f.prot;
+    }
      if (sattrs.subpage) {
          result->f.lg_page_size = 0;
      }

Cheers,
Gustavo