qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug
@ 2025-04-10 21:00 Pierrick Bouvier
  2025-04-10 21:00 ` [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space Pierrick Bouvier
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Pierrick Bouvier @ 2025-04-10 21:00 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, philmd,
	alex.bennee, qemu-arm, Yannis Bolliger, Pierrick Bouvier

It was reported that QEMU monitor command gva2gpa was reporting unmapped
memory for a valid access (qemu-system-aarch64), during a copy from
kernel to user space (__arch_copy_to_user symbol in Linux) [1].
This was affecting cpu_memory_rw_debug also, which
is used in numerous places in our codebase. After investigating, the
problem was specific to arm_cpu_get_phys_page_attrs_debug.

[1] https://lists.nongnu.org/archive/html/qemu-discuss/2025-04/msg00013.html

When performing user access from a privileged space, we need to do a
second lookup for user mmu idx, following what get_a64_user_mem_index is
doing at translation time.

This series first extract some functions, and then perform the second lookup
expected using extracted functions.

Besides running all QEMU tests, it was explicitely checked that during a linux
boot sequence, accesses now report a valid physical address inconditionnally
using this (non sent) patch:

--- a/accel/tcg/cputlb.c
+++ b/accel/tcg/cputlb.c
@@ -997,9 +997,7 @@ static inline void tlb_set_compare(CPUTLBEntryFull *full, CPUTLBEntry *ent,
     if (enable) {
         address |= flags & TLB_FLAGS_MASK;
         flags &= TLB_SLOW_FLAGS_MASK;
-        if (flags) {
             address |= TLB_FORCE_SLOW;
-        }
     } else {
         address = -1;
         flags = 0;
@@ -1658,6 +1656,10 @@ static bool mmu_lookup1(CPUState *cpu, MMULookupPageData *data, MemOp memop,
         tlb_addr = tlb_read_idx(entry, access_type) & ~TLB_INVALID_MASK;
     }

+    vaddr page = addr & TARGET_PAGE_MASK;
+    hwaddr physaddr = cpu_get_phys_page_debug(cpu, page);
+    g_assert(physaddr != -1);
+
     full = &cpu->neg.tlb.d[mmu_idx].fulltlb[index];
     flags = tlb_addr & (TLB_FLAGS_MASK & ~TLB_FORCE_SLOW);
     flags |= full->slow_flags[access_type];

Pierrick Bouvier (4):
  target/arm/ptw: extract arm_mmu_idx_to_security_space
  target/arm/ptw: get current security_space for current mmu_idx
  target/arm/ptw: extract arm_cpu_get_phys_page
  target/arm/ptw: fix arm_cpu_get_phys_page_attrs_debug

 target/arm/ptw.c | 65 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 48 insertions(+), 17 deletions(-)

-- 
2.39.5



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space
  2025-04-10 21:00 [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
@ 2025-04-10 21:00 ` Pierrick Bouvier
  2025-04-11 13:44   ` Philippe Mathieu-Daudé
  2025-04-10 21:00 ` [PATCH 2/4] target/arm/ptw: get current security_space for current mmu_idx Pierrick Bouvier
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Pierrick Bouvier @ 2025-04-10 21:00 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, philmd,
	alex.bennee, qemu-arm, Yannis Bolliger, Pierrick Bouvier

We'll reuse this function later.

Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 target/arm/ptw.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/target/arm/ptw.c b/target/arm/ptw.c
index 8d4e9e07a94..5e196cfa955 100644
--- a/target/arm/ptw.c
+++ b/target/arm/ptw.c
@@ -3550,13 +3550,9 @@ bool get_phys_addr_with_space_nogpc(CPUARMState *env, vaddr address,
                                memop, result, fi);
 }
 
-bool get_phys_addr(CPUARMState *env, vaddr address,
-                   MMUAccessType access_type, MemOp memop, ARMMMUIdx mmu_idx,
-                   GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
+static ARMSecuritySpace arm_mmu_idx_to_security_space
+(CPUARMState *env, ARMMMUIdx mmu_idx)
 {
-    S1Translate ptw = {
-        .in_mmu_idx = mmu_idx,
-    };
     ARMSecuritySpace ss;
 
     switch (mmu_idx) {
@@ -3617,7 +3613,18 @@ bool get_phys_addr(CPUARMState *env, vaddr address,
         g_assert_not_reached();
     }
 
-    ptw.in_space = ss;
+    return ss;
+}
+
+bool get_phys_addr(CPUARMState *env, vaddr address,
+                   MMUAccessType access_type, MemOp memop, ARMMMUIdx mmu_idx,
+                   GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
+{
+    S1Translate ptw = {
+        .in_mmu_idx = mmu_idx,
+        .in_space = arm_mmu_idx_to_security_space(env, mmu_idx),
+    };
+
     return get_phys_addr_gpc(env, &ptw, address, access_type,
                              memop, result, fi);
 }
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/4] target/arm/ptw: get current security_space for current mmu_idx
  2025-04-10 21:00 [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
  2025-04-10 21:00 ` [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space Pierrick Bouvier
@ 2025-04-10 21:00 ` Pierrick Bouvier
  2025-04-10 21:00 ` [PATCH 3/4] target/arm/ptw: extract arm_cpu_get_phys_page Pierrick Bouvier
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Pierrick Bouvier @ 2025-04-10 21:00 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, philmd,
	alex.bennee, qemu-arm, Yannis Bolliger, Pierrick Bouvier

It should be equivalent to previous code.
Allow to call common function to get a page address later.

Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 target/arm/ptw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/target/arm/ptw.c b/target/arm/ptw.c
index 5e196cfa955..754ef0d3a25 100644
--- a/target/arm/ptw.c
+++ b/target/arm/ptw.c
@@ -3635,7 +3635,7 @@ hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
     ARMMMUIdx mmu_idx = arm_mmu_idx(env);
-    ARMSecuritySpace ss = arm_security_space(env);
+    ARMSecuritySpace ss = arm_mmu_idx_to_security_space(env, mmu_idx);
     S1Translate ptw = {
         .in_mmu_idx = mmu_idx,
         .in_space = ss,
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/4] target/arm/ptw: extract arm_cpu_get_phys_page
  2025-04-10 21:00 [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
  2025-04-10 21:00 ` [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space Pierrick Bouvier
  2025-04-10 21:00 ` [PATCH 2/4] target/arm/ptw: get current security_space for current mmu_idx Pierrick Bouvier
@ 2025-04-10 21:00 ` Pierrick Bouvier
  2025-04-11 13:45   ` Philippe Mathieu-Daudé
  2025-04-10 21:00 ` [PATCH 4/4] target/arm/ptw: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
  2025-04-12 17:11 ` [PATCH 0/4] target/arm: " Richard Henderson
  4 siblings, 1 reply; 9+ messages in thread
From: Pierrick Bouvier @ 2025-04-10 21:00 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, philmd,
	alex.bennee, qemu-arm, Yannis Bolliger, Pierrick Bouvier

Allow to call that function easily several times in next commit.

Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 target/arm/ptw.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/target/arm/ptw.c b/target/arm/ptw.c
index 754ef0d3a25..6ea39ee5755 100644
--- a/target/arm/ptw.c
+++ b/target/arm/ptw.c
@@ -3629,23 +3629,17 @@ bool get_phys_addr(CPUARMState *env, vaddr address,
                              memop, result, fi);
 }
 
-hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
-                                         MemTxAttrs *attrs)
+static hwaddr arm_cpu_get_phys_page(CPUARMState *env, vaddr addr,
+                                    MemTxAttrs *attrs, ARMMMUIdx mmu_idx)
 {
-    ARMCPU *cpu = ARM_CPU(cs);
-    CPUARMState *env = &cpu->env;
-    ARMMMUIdx mmu_idx = arm_mmu_idx(env);
-    ARMSecuritySpace ss = arm_mmu_idx_to_security_space(env, mmu_idx);
     S1Translate ptw = {
         .in_mmu_idx = mmu_idx,
-        .in_space = ss,
+        .in_space = arm_mmu_idx_to_security_space(env, mmu_idx),
         .in_debug = true,
     };
     GetPhysAddrResult res = {};
     ARMMMUFaultInfo fi = {};
-    bool ret;
-
-    ret = get_phys_addr_gpc(env, &ptw, addr, MMU_DATA_LOAD, 0, &res, &fi);
+    bool ret = get_phys_addr_gpc(env, &ptw, addr, MMU_DATA_LOAD, 0, &res, &fi);
     *attrs = res.f.attrs;
 
     if (ret) {
@@ -3653,3 +3647,13 @@ hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
     }
     return res.f.phys_addr;
 }
+
+hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
+                                         MemTxAttrs *attrs)
+{
+    ARMCPU *cpu = ARM_CPU(cs);
+    CPUARMState *env = &cpu->env;
+    ARMMMUIdx mmu_idx = arm_mmu_idx(env);
+
+    return arm_cpu_get_phys_page(env, addr, attrs, mmu_idx);
+}
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 4/4] target/arm/ptw: fix arm_cpu_get_phys_page_attrs_debug
  2025-04-10 21:00 [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
                   ` (2 preceding siblings ...)
  2025-04-10 21:00 ` [PATCH 3/4] target/arm/ptw: extract arm_cpu_get_phys_page Pierrick Bouvier
@ 2025-04-10 21:00 ` Pierrick Bouvier
  2025-04-12 17:11 ` [PATCH 0/4] target/arm: " Richard Henderson
  4 siblings, 0 replies; 9+ messages in thread
From: Pierrick Bouvier @ 2025-04-10 21:00 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, philmd,
	alex.bennee, qemu-arm, Yannis Bolliger, Pierrick Bouvier

It was reported that QEMU monitor command gva2gpa was reporting unmapped
memory for a valid access (qemu-system-aarch64), during a copy from
kernel to user space (__arch_copy_to_user symbol in Linux) [1].
This was affecting cpu_memory_rw_debug also, which
is used in numerous places in our codebase. After investigating, the
problem was specific to arm_cpu_get_phys_page_attrs_debug.

When performing user access from a privileged space, we need to do a
second lookup for user mmu idx, following what get_a64_user_mem_index is
doing at translation time.

[1] https://lists.nongnu.org/archive/html/qemu-discuss/2025-04/msg00013.html

Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
---
 target/arm/ptw.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/target/arm/ptw.c b/target/arm/ptw.c
index 6ea39ee5755..5b8d84d44df 100644
--- a/target/arm/ptw.c
+++ b/target/arm/ptw.c
@@ -3655,5 +3655,25 @@ hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
     CPUARMState *env = &cpu->env;
     ARMMMUIdx mmu_idx = arm_mmu_idx(env);
 
-    return arm_cpu_get_phys_page(env, addr, attrs, mmu_idx);
+    hwaddr res = arm_cpu_get_phys_page(env, addr, attrs, mmu_idx);
+
+    if (res != -1) {
+        return res;
+    }
+
+    /*
+     * Memory may be accessible for an "unprivileged load/store" variant.
+     * In this case, get_a64_user_mem_index function generates an op using an
+     * unprivileged mmu idx, so we need to try with it.
+     */
+    switch (mmu_idx) {
+    case ARMMMUIdx_E10_1:
+    case ARMMMUIdx_E10_1_PAN:
+        return arm_cpu_get_phys_page(env, addr, attrs, ARMMMUIdx_E10_0);
+    case ARMMMUIdx_E20_2:
+    case ARMMMUIdx_E20_2_PAN:
+        return arm_cpu_get_phys_page(env, addr, attrs, ARMMMUIdx_E20_0);
+    default:
+        return -1;
+    }
 }
-- 
2.39.5



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space
  2025-04-10 21:00 ` [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space Pierrick Bouvier
@ 2025-04-11 13:44   ` Philippe Mathieu-Daudé
  2025-04-11 16:43     ` Pierrick Bouvier
  0 siblings, 1 reply; 9+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-11 13:44 UTC (permalink / raw)
  To: Pierrick Bouvier, qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, alex.bennee,
	qemu-arm, Yannis Bolliger

On 10/4/25 23:00, Pierrick Bouvier wrote:
> We'll reuse this function later.
> 
> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> ---
>   target/arm/ptw.c | 21 ++++++++++++++-------
>   1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/target/arm/ptw.c b/target/arm/ptw.c
> index 8d4e9e07a94..5e196cfa955 100644
> --- a/target/arm/ptw.c
> +++ b/target/arm/ptw.c
> @@ -3550,13 +3550,9 @@ bool get_phys_addr_with_space_nogpc(CPUARMState *env, vaddr address,
>                                  memop, result, fi);
>   }
>   
> -bool get_phys_addr(CPUARMState *env, vaddr address,
> -                   MMUAccessType access_type, MemOp memop, ARMMMUIdx mmu_idx,
> -                   GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
> +static ARMSecuritySpace arm_mmu_idx_to_security_space
> +(CPUARMState *env, ARMMMUIdx mmu_idx)

Style is:

static ARMSecuritySpace arm_mmu_idx_to_security_space(CPUARMState *env,
                                                       ARMMMUIdx mmu_idx)

or:

static ARMSecuritySpace
arm_mmu_idx_to_security_space(CPUARMState *env, ARMMMUIdx mmu_idx)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 3/4] target/arm/ptw: extract arm_cpu_get_phys_page
  2025-04-10 21:00 ` [PATCH 3/4] target/arm/ptw: extract arm_cpu_get_phys_page Pierrick Bouvier
@ 2025-04-11 13:45   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 9+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-04-11 13:45 UTC (permalink / raw)
  To: Pierrick Bouvier, qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, alex.bennee,
	qemu-arm, Yannis Bolliger

On 10/4/25 23:00, Pierrick Bouvier wrote:
> Allow to call that function easily several times in next commit.
> 
> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> ---
>   target/arm/ptw.c | 24 ++++++++++++++----------
>   1 file changed, 14 insertions(+), 10 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space
  2025-04-11 13:44   ` Philippe Mathieu-Daudé
@ 2025-04-11 16:43     ` Pierrick Bouvier
  0 siblings, 0 replies; 9+ messages in thread
From: Pierrick Bouvier @ 2025-04-11 16:43 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Peter Maydell, Richard Henderson, Paolo Bonzini, alex.bennee,
	qemu-arm, Yannis Bolliger

On 4/11/25 06:44, Philippe Mathieu-Daudé wrote:
> On 10/4/25 23:00, Pierrick Bouvier wrote:
>> We'll reuse this function later.
>>
>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>> ---
>>    target/arm/ptw.c | 21 ++++++++++++++-------
>>    1 file changed, 14 insertions(+), 7 deletions(-)
>>
>> diff --git a/target/arm/ptw.c b/target/arm/ptw.c
>> index 8d4e9e07a94..5e196cfa955 100644
>> --- a/target/arm/ptw.c
>> +++ b/target/arm/ptw.c
>> @@ -3550,13 +3550,9 @@ bool get_phys_addr_with_space_nogpc(CPUARMState *env, vaddr address,
>>                                   memop, result, fi);
>>    }
>>    
>> -bool get_phys_addr(CPUARMState *env, vaddr address,
>> -                   MMUAccessType access_type, MemOp memop, ARMMMUIdx mmu_idx,
>> -                   GetPhysAddrResult *result, ARMMMUFaultInfo *fi)
>> +static ARMSecuritySpace arm_mmu_idx_to_security_space
>> +(CPUARMState *env, ARMMMUIdx mmu_idx)
> 
> Style is:
> 
> static ARMSecuritySpace arm_mmu_idx_to_security_space(CPUARMState *env,
>                                                         ARMMMUIdx mmu_idx)
> 
> or:
> 
> static ARMSecuritySpace
> arm_mmu_idx_to_security_space(CPUARMState *env, ARMMMUIdx mmu_idx)
> 

Thanks, I'll update to this one.

> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug
  2025-04-10 21:00 [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
                   ` (3 preceding siblings ...)
  2025-04-10 21:00 ` [PATCH 4/4] target/arm/ptw: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
@ 2025-04-12 17:11 ` Richard Henderson
  4 siblings, 0 replies; 9+ messages in thread
From: Richard Henderson @ 2025-04-12 17:11 UTC (permalink / raw)
  To: Pierrick Bouvier, qemu-devel
  Cc: Peter Maydell, Paolo Bonzini, philmd, alex.bennee, qemu-arm,
	Yannis Bolliger

On 4/10/25 14:00, Pierrick Bouvier wrote:
> Pierrick Bouvier (4):
>    target/arm/ptw: extract arm_mmu_idx_to_security_space
>    target/arm/ptw: get current security_space for current mmu_idx
>    target/arm/ptw: extract arm_cpu_get_phys_page
>    target/arm/ptw: fix arm_cpu_get_phys_page_attrs_debug

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-04-12 17:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-10 21:00 [PATCH 0/4] target/arm: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
2025-04-10 21:00 ` [PATCH 1/4] target/arm/ptw: extract arm_mmu_idx_to_security_space Pierrick Bouvier
2025-04-11 13:44   ` Philippe Mathieu-Daudé
2025-04-11 16:43     ` Pierrick Bouvier
2025-04-10 21:00 ` [PATCH 2/4] target/arm/ptw: get current security_space for current mmu_idx Pierrick Bouvier
2025-04-10 21:00 ` [PATCH 3/4] target/arm/ptw: extract arm_cpu_get_phys_page Pierrick Bouvier
2025-04-11 13:45   ` Philippe Mathieu-Daudé
2025-04-10 21:00 ` [PATCH 4/4] target/arm/ptw: fix arm_cpu_get_phys_page_attrs_debug Pierrick Bouvier
2025-04-12 17:11 ` [PATCH 0/4] target/arm: " Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).