Skip to contentSkip to conversation

target/sparc: resolve ASI_USERTXT correctly

PATCH In review M Bazz 2 participants 8 messages Apr 11, 2024 – Apr 15, 2024

lore
Share & tools
lore

M Bazz

author
#0 lore raw

PATCH 1/1 target/sparc: resolve ASI_USERTXT correctly

fixes a longstanding bug which causes a "Nonparity Synchronous Error" kernel panic while using a debugger on Solaris / SunOS systems. The panic would occur on the first attempt to single-step the process.

The problem stems from an lda instruction on ASI_USERTXT (8). This asi was not being resolved correctly by resolve_asi().

Further details can be found in #2281

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2281
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2059
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1609
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1166

---
 target/sparc/translate.c | 2 ++
 1 file changed, 2 insertions(+)
target/sparc/translate.c +2 -0
index 319934d9bd..1596005e22 100644
@@ -3,6 +3,7 @@
 
    Copyright (C) 2003 Thomas M. Ogrisegg <[email protected]>
    Copyright (C) 2003-2005 Fabrice Bellard
+   Copyright (C) 2024 M Bazz <[email protected]>
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -1159,6 +1160,7 @@ static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop)
                || (asi == ASI_USERDATA
                    && (dc->def->features & CPU_FEATURE_CASA))) {
         switch (asi) {
+        case ASI_USERTXT:    /* User text access */
         case ASI_USERDATA:   /* User data access */
             mem_idx = MMU_USER_IDX;
             type = GET_ASI_DIRECT;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 32 lines · M Bazz

fixes a longstanding bug which causes a "Nonparity Synchronous Error" kernel panic while using a debugger on Solaris / SunOS systems. The panic would occur on the first attempt to single-step the process.

The problem stems from an lda instruction on ASI_USERTXT (8). This asi was not being resolved correctly by resolve_asi().

Further details can be found in #2281

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2281
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2059
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1609
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1166

---
  target/sparc/translate.c | 2 ++
  1 file changed, 2 insertions(+)
target/sparc/translate.c +1 -0
index 319934d9bd..1596005e22 100644
@@ -3,6 +3,7 @@
 
     Copyright (C) 2003 Thomas M. Ogrisegg <[email protected]>
     Copyright (C) 2003-2005 Fabrice Bellard
+   Copyright (C) 2024 M Bazz <[email protected]>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public

target/sparc/translate.c static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop) · line 1166

Quoted 7 lines
target/sparc/translate.c +1 -0
@@ -1159,6 +1160,7 @@ static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop)
                 || (asi == ASI_USERDATA
                     && (dc->def->features & CPU_FEATURE_CASA))) {
          switch (asi) {
+        case ASI_USERTXT:    /* User text access */
          case ASI_USERDATA:   /* User data access */
              mem_idx = MMU_USER_IDX;
              type = GET_ASI_DIRECT;

I don't believe this is correct, because it operates against the page's "read" permissions instead of "execute" permissions.

r~

author
#2 lore raw
Quoted 46 lines · Richard Henderson
Quoted 32 lines · M Bazz

fixes a longstanding bug which causes a "Nonparity Synchronous Error" kernel panic while using a debugger on Solaris / SunOS systems. The panic would occur on the first attempt to single-step the process.

The problem stems from an lda instruction on ASI_USERTXT (8). This asi was not being resolved correctly by resolve_asi().

Further details can be found in #2281

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2281
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2059
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1609
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1166

---
  target/sparc/translate.c | 2 ++
  1 file changed, 2 insertions(+)
target/sparc/translate.c +1 -0
index 319934d9bd..1596005e22 100644
@@ -3,6 +3,7 @@
 
     Copyright (C) 2003 Thomas M. Ogrisegg <[email protected]>
     Copyright (C) 2003-2005 Fabrice Bellard
+   Copyright (C) 2024 M Bazz <[email protected]>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public

target/sparc/translate.c static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop) · line 1166

Quoted 7 lines
target/sparc/translate.c +1 -0
@@ -1159,6 +1160,7 @@ static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop)
                 || (asi == ASI_USERDATA
                     && (dc->def->features & CPU_FEATURE_CASA))) {
          switch (asi) {
+        case ASI_USERTXT:    /* User text access */
          case ASI_USERDATA:   /* User data access */
              mem_idx = MMU_USER_IDX;
              type = GET_ASI_DIRECT;

I don't believe this is correct, because it operates against the page's "read" permissions instead of "execute" permissions.

Quoted 1 line

r~

Hi Richard,

Thanks for your guidance. It set me in the right direction. Now I think I've got the right spot.

function helper_ld_asi has a block to help load ASI_KERNELTXT, but the ASI_USERTXT case defaults to sparc_raise_mmu_fault(); I believe this is the true culprit
source reference:
https://gitlab.com/qemu-project/qemu/-/blob/master/target/sparc/ldst_helper.c?ref_type=heads#L687

The code for the ASI_KERNELTXT seems generic enough to also use for ASI_USERTXT verbatim.
See v2 patch below. I've done a make test -- all passing (3 skips). OS boots, and the
debuggers are working without issue. What do you think?

Once we arrive at the right solution, I'll finalize the patch. -bazz

target/sparc/ldst_helper.c +0 -0
index e581bb42ac..4f87e44a93 100644
@@ -684,6 +684,7 @@ uint64_t helper_ld_asi(CPUSPARCState *env,

target_ulong addr,
case ASI_M_DIAGS: /* Turbosparc DTLB Diagnostic */
case ASI_M_IODIAG: /* Turbosparc IOTLB Diagnostic */
break;
+ case ASI_USERTXT: /* User code access */

     case ASI_KERNELTXT: /* Supervisor code access */
         oi = make_memop_idx(memop, cpu_mmu_index(env_cpu(env), true));
         switch (size) {
@@ -779,7 +780,6 @@ uint64_t helper_ld_asi(CPUSPARCState *env,
target_ulong addr,
     case 0x4c: /* SuperSPARC MMU Breakpoint Action */
         ret = env->mmubpaction;
         break;
-    case ASI_USERTXT: /* User code access, XXX */
     default:
         sparc_raise_mmu_fault(cs, addr, false, false, asi, size, GETPC());
         ret = 0;
#3 lore raw
Quoted 76 lines · M Bazz
Quoted 46 lines · Richard Henderson
Quoted 32 lines · M Bazz

fixes a longstanding bug which causes a "Nonparity Synchronous Error" kernel panic while using a debugger on Solaris / SunOS systems. The panic would occur on the first attempt to single-step the process.

The problem stems from an lda instruction on ASI_USERTXT (8). This asi was not being resolved correctly by resolve_asi().

Further details can be found in #2281

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2281
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2059
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1609
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1166

---
   target/sparc/translate.c | 2 ++
   1 file changed, 2 insertions(+)
target/sparc/translate.c +1 -0
index 319934d9bd..1596005e22 100644
@@ -3,6 +3,7 @@
 
      Copyright (C) 2003 Thomas M. Ogrisegg <[email protected]>
      Copyright (C) 2003-2005 Fabrice Bellard
+   Copyright (C) 2024 M Bazz <[email protected]>
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public

target/sparc/translate.c static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop) · line 1166

Quoted 7 lines
target/sparc/translate.c +1 -0
@@ -1159,6 +1160,7 @@ static DisasASI resolve_asi(DisasContext *dc, int asi, MemOp memop)
                  || (asi == ASI_USERDATA
                      && (dc->def->features & CPU_FEATURE_CASA))) {
           switch (asi) {
+        case ASI_USERTXT:    /* User text access */
           case ASI_USERDATA:   /* User data access */
               mem_idx = MMU_USER_IDX;
               type = GET_ASI_DIRECT;

I don't believe this is correct, because it operates against the page's "read" permissions instead of "execute" permissions.

Quoted 1 line

r~

Hi Richard,

Thanks for your guidance. It set me in the right direction. Now I think I've got the right spot.

function helper_ld_asi has a block to help load ASI_KERNELTXT, but the ASI_USERTXT case defaults to sparc_raise_mmu_fault(); I believe this is the true culprit
source reference:
https://gitlab.com/qemu-project/qemu/-/blob/master/target/sparc/ldst_helper.c?ref_type=heads#L687

The code for the ASI_KERNELTXT seems generic enough to also use for ASI_USERTXT verbatim.
See v2 patch below. I've done a make test -- all passing (3 skips). OS boots, and the
debuggers are working without issue. What do you think?

Once we arrive at the right solution, I'll finalize the patch.

(quoted diff) +0 -1
-bazz
target/sparc/ldst_helper.c +0 -0
index e581bb42ac..4f87e44a93 100644

target/sparc/ldst_helper.c uint64_t helper_ld_asi(CPUSPARCState *env, · line 690

Quoted 7 lines
target/sparc/ldst_helper.c +1 -0
@@ -684,6 +684,7 @@ uint64_t helper_ld_asi(CPUSPARCState *env,
 target_ulong addr,
      case ASI_M_DIAGS:   /* Turbosparc DTLB Diagnostic */
      case ASI_M_IODIAG:  /* Turbosparc IOTLB Diagnostic */
          break;
+    case ASI_USERTXT: /* User code access */
      case ASI_KERNELTXT: /* Supervisor code access */
          oi = make_memop_idx(memop, cpu_mmu_index(env_cpu(env), true));

No, this also does not work, because it uses the wrong permissions (kernel instead of user). I have just sent a patch to fix both problems.

r~

author
#4 lore raw

Hi Richard,

Quoted 17 lines · Richard Henderson

target/sparc/ldst_helper.c uint64_t helper_ld_asi(CPUSPARCState *env, · line 690

Quoted 12 lines · M Bazz
target/sparc/ldst_helper.c +1 -0
index e581bb42ac..4f87e44a93 100644
@@ -684,6 +684,7 @@ uint64_t helper_ld_asi(CPUSPARCState *env,
 target_ulong addr,
      case ASI_M_DIAGS:   /* Turbosparc DTLB Diagnostic */
      case ASI_M_IODIAG:  /* Turbosparc IOTLB Diagnostic */
          break;
+    case ASI_USERTXT: /* User code access */
      case ASI_KERNELTXT: /* Supervisor code access */
          oi = make_memop_idx(memop, cpu_mmu_index(env_cpu(env), true));

No, this also does not work, because it uses the wrong permissions (kernel instead of user). I have just sent a patch to fix both problems.

This thought just came to me. lda is a privileged instruction. It has to run in supervisor mode. So, I'm struggling to understand how the kernel permission
was wrong. Isn't that the right permission for this instruction?

I just want to have the right understanding, so that I'm more prepared for the next time.

Here is a related excerpt from the Sparcv8 manual:
"For each instruction access and each normal data access, the IU appends to the 32-bit
memory address an 8-bit address space identifier, or ASI. The ASI encodes whether the
processor is in supervisor or user mode, and whether the access is an instruction or data access.
There are also privileged load/store alternate instructions (see below) that can provide an arbitrary
ASI with their data addresses."

Thank you,
-bazz

Quoted 3 lines

r~

#5 lore raw
Quoted 3 lines · M Bazz

This thought just came to me. lda is a privileged instruction. It has to run in supervisor mode. So, I'm struggling to understand how the kernel permission was wrong. Isn't that the right permission for this instruction?

The "current" permission, as computed by

Review

Quoted 2 lines
(quoted diff) +0 -2
-    case ASI_KERNELTXT: /* Supervisor code access */
-        oi = make_memop_idx(memop, cpu_mmu_index(env_cpu(env), true));

was correct for ASI_KERNELTXT, because as you say "lda" is a supervisor-only instruction prior to sparcv9.

However, using the same value for ASI_USERTXT would be incorrect. For ASI_USERTXT and ASI_USERDATA (and the new names for sparcv9, ASI_AIU*, "as-if user"), we need permissions for the user context.

That's what

Review

Quoted 2 lines
(quoted diff) +2 -0
+        case ASI_USERTXT:     /* User text access */
+            mem_idx = MMU_USER_IDX;

this is for in my patch. This gets passed into the helper,

Review

Quoted 7 lines
(quoted diff) +7 -0
+    case GET_ASI_CODE:
+#if !defined(CONFIG_USER_ONLY) && !defined(TARGET_SPARC64)
+        {
+            MemOpIdx oi = make_memop_idx(da->memop, da->mem_idx);
+            TCGv_i64 t64 = tcg_temp_new_i64();
+
+            gen_helper_ld_code(t64, tcg_env, addr, tcg_constant_i32(oi));

and then into the core memory access functions,

Quoted 1 line

+ ret = cpu_ldl_code_mmu(env, addr, oi, ra);

Unfortunately, we do not have any good documentation for tcg softmmu or the intended role of the mmu_idx. Partly that's due to the final use of the mmu_idx is target-specific.

For sparc32, there are 3 mmu_idx:

Quoted 3 lines

#define MMU_USER_IDX 0
#define MMU_KERNEL_IDX 1
#define MMU_PHYS_IDX 2

The interpretation of mmu_idx happens in target/sparc/mmu_helper.c, get_physical_address (note there are two versions, for sparc32 and sparc64).

Ignoring MMU_PHYS_IDX, which is handled early, the difference between kernel and user is

    is_user = mmu_idx == MMU_USER_IDX;
...
    full->prot = perm_table[is_user][access_perms];

This controls the read/write/execute permissions for the page. Note that perm_table matches the Access Allowed table on page 248 of the Sparc V8 architecture manual.

r~

author
#6 lore raw

Hi Henry,

I want to thank you for every chance I get to learn from you. Each email excites me.

Quoted 8 lines · Richard Henderson

The "current" permission, as computed by

Review

Quoted 2 lines
(quoted diff) +0 -2
-    case ASI_KERNELTXT: /* Supervisor code access */
-        oi = make_memop_idx(memop, cpu_mmu_index(env_cpu(env), true));

was correct for ASI_KERNELTXT, because as you say "lda" is a supervisor-only instruction prior to sparcv9.

I noticed that cpu_mmu_index() would have returned MMU_USER_IDX if the supervisor bit hadn't happened to be set (not sure if this execution path can
occur for lda). Note that this check is gone in your patch.

I consider you my sensei, so while I'm confident in your work I also want to show
the things I catch.

If I understand everything you've taught me, then the following patch would have also satisfied the permissions issue. Could you confirm this please? The essential change is the MMU_USER_IDX in the call to make_memop_idx()

target/sparc/ldst_helper.c +0 -0
index e581bb42ac..be3c03a3b6 100644
@@ -702,6 +702,24 @@ uint64_t helper_ld_asi(CPUSPARCState *env,

target_ulong addr,

             break;
         }
         break;
+    case ASI_USERTXT: /* User code access */
+        oi = make_memop_idx(memop, MMU_USER_IDX);
+        switch (size) {
+        case 1:

+ ret = cpu_ldb_code_mmu(env, addr, oi, GETPC());
+ break;
+ case 2:
+ ret = cpu_ldw_code_mmu(env, addr, oi, GETPC());
+ break;
+ default:
+ case 4:
+ ret = cpu_ldl_code_mmu(env, addr, oi, GETPC());
+ break;
+ case 8:
+ ret = cpu_ldq_code_mmu(env, addr, oi, GETPC());
+ break;
+ }
+ break;

     case ASI_M_TXTC_TAG:   /* SparcStation 5 I-cache tag */
     case ASI_M_TXTC_DATA:  /* SparcStation 5 I-cache data */
     case ASI_M_DATAC_TAG:  /* SparcStation 5 D-cache tag */
@@ -779,7 +797,6 @@ uint64_t helper_ld_asi(CPUSPARCState *env,
target_ulong addr,
     case 0x4c: /* SuperSPARC MMU Breakpoint Action */
         ret = env->mmubpaction;
         break;
-    case ASI_USERTXT: /* User code access, XXX */
     default:
         sparc_raise_mmu_fault(cs, addr, false, false, asi, size, GETPC());
         ret = 0;
Quoted 2 lines

Unfortunately, we do not have any good documentation for tcg softmmu or the intended role of the mmu_idx. Partly that's due to the final use of the mmu_idx is target-specific.

I love learning from code, but the lack of documentation definitely increases the value of your
discourse with me.

Thank you,
Sincerely,
-bazz

#7 lore raw
Quoted 3 lines · M Bazz

I noticed that cpu_mmu_index() would have returned MMU_USER_IDX if the supervisor bit hadn't happened to be set (not sure if this execution path can occur for lda).

No, it cannot.

Quoted 1 line

Note that this check is gone in your patch.

Correct. Since 'lda' has already checked that supervisor mode has been enabled, the translator may jump directly to the desired result of MMU_KERNEL_IDX.

Quoted 21 lines

If I understand everything you've taught me, then the following patch would have also satisfied the permissions issue. Could you confirm this please? The essential change is the MMU_USER_IDX in the call to make_memop_idx()

target/sparc/ldst_helper.c +8 -0
index e581bb42ac..be3c03a3b6 100644
@@ -702,6 +702,24 @@ uint64_t helper_ld_asi(CPUSPARCState *env,
 target_ulong addr,
              break;
          }
          break;
+    case ASI_USERTXT: /* User code access */
+        oi = make_memop_idx(memop, MMU_USER_IDX);
+        switch (size) {
+        case 1:
+            ret = cpu_ldb_code_mmu(env, addr, oi, GETPC());
+            break;
+        case 2:
+            ret = cpu_ldw_code_mmu(env, addr, oi, GETPC());

target/sparc/ldst_helper.c uint64_t helper_ld_asi(CPUSPARCState *env, · line 723

Quoted 10 lines
target/sparc/ldst_helper.c +10 -0
+            break;
+        default:
+        case 4:
+            ret = cpu_ldl_code_mmu(env, addr, oi, GETPC());
+            break;
+        case 8:
+            ret = cpu_ldq_code_mmu(env, addr, oi, GETPC());
+            break;
+        }
+        break;

Correct, that would also work.

r~