Skip to contentSkip to conversation

accel/tcg: Fix computing of is_write for mips

PATCH Superseded Kele Huang 3 participants 6 messages Sep 23, 2020 – Sep 25, 2020

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kele Huang

author
#0 lore raw

PATCH v2 1/1 accel/tcg: Fix computing of is_write for mips

Detect mips store instructions in cpu_signal_handler for all MIPS versions, and set is_write if encountering such store instructions.

This fixed the error while dealing with self-modifed code for MIPS.

---
 accel/tcg/user-exec.c | 51 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +50 -1
index bb039eb32d..18784516e5 100644
@@ -710,11 +710,60 @@ int cpu_signal_handler(int host_signum, void *pinfo,
     greg_t pc = uc->uc_mcontext.pc;
     int is_write;
 
-    /* XXX: compute is_write */
     is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter. */
+    uint32_t insn = *(uint32_t *)pc;
+    switch(insn>>29) {
+    case 0x5:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SB */
+        case 0x1: /* SH */
+        case 0x2: /* SWL */
+        case 0x3: /* SW */
+        case 0x4: /* SDL */
+        case 0x5: /* SDR */
+        case 0x6: /* SWR */
+            is_write = 1;
+        }
+        break;
+    case 0x7:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SC */
+        case 0x1: /* SWC1 */
+        case 0x4: /* SCD */
+        case 0x5: /* SDC1 */
+        case 0x7: /* SD */
+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
+        case 0x2: /* SWC2 */
+        case 0x6: /* SDC2 */
+#endif
+            is_write = 1;
+        }
+        break;
+    }
+
+    /*
+     * Required in all versions of MIPS64 since MIPS64r1. Not available
+     * in MIPS32r1. Required by MIPS32r2 and subsequent versions of MIPS32.
+     */
+    switch ((insn >> 3) & 0x7) {
+    case 0x1:
+        switch (insn & 0x7) {
+        case 0x0: /* SWXC1 */
+        case 0x1: /* SDXC1 */
+            is_write = 1;
+        }
+        break;
+    }
+
     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
 }
 
+#elif defined(__misp16) || defined(__mips_micromips)
+
+#error "Unsupported encoding"
+
 #elif defined(__riscv)
 
 int cpu_signal_handler(int host_signum, void *pinfo,
Signature · 1 line
2.17.1
#1 lore raw

Cc'ing the TCG MIPS maintainers, and also
Cc'ing Richard who made a comment in v1.

Quoted 4 lines · Kele Huang

Detect mips store instructions in cpu_signal_handler for all MIPS versions, and set is_write if encountering such store instructions.

This fixed the error while dealing with self-modifed code for MIPS.

Quoting Eric Blake:

"It's better to post a v2 as a new top-level thread rather than buried in-reply-to the v1 thread; among other things, burying a reply can cause automated patch tooling to miss the updated series."

Quoted 74 lines
---
 accel/tcg/user-exec.c | 51 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +50 -1
index bb039eb32d..18784516e5 100644
@@ -710,11 +710,60 @@ int cpu_signal_handler(int host_signum, void *pinfo,
     greg_t pc = uc->uc_mcontext.pc;
     int is_write;
 
-    /* XXX: compute is_write */
     is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter. */
+    uint32_t insn = *(uint32_t *)pc;
+    switch(insn>>29) {
+    case 0x5:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SB */
+        case 0x1: /* SH */
+        case 0x2: /* SWL */
+        case 0x3: /* SW */
+        case 0x4: /* SDL */
+        case 0x5: /* SDR */
+        case 0x6: /* SWR */
+            is_write = 1;
+        }
+        break;
+    case 0x7:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SC */
+        case 0x1: /* SWC1 */
+        case 0x4: /* SCD */
+        case 0x5: /* SDC1 */
+        case 0x7: /* SD */
+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
+        case 0x2: /* SWC2 */
+        case 0x6: /* SDC2 */
+#endif
+            is_write = 1;
+        }
+        break;
+    }
+
+    /*
+     * Required in all versions of MIPS64 since MIPS64r1. Not available
+     * in MIPS32r1. Required by MIPS32r2 and subsequent versions of MIPS32.
+     */
+    switch ((insn >> 3) & 0x7) {
+    case 0x1:
+        switch (insn & 0x7) {
+        case 0x0: /* SWXC1 */
+        case 0x1: /* SDXC1 */
+            is_write = 1;
+        }
+        break;
+    }
+
     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
 }
 
+#elif defined(__misp16) || defined(__mips_micromips)
+
+#error "Unsupported encoding"
+
 #elif defined(__riscv)
 
 int cpu_signal_handler(int host_signum, void *pinfo,
author
#2 lore raw

Got it. Thank you very much!
I will resend the same v2 patch to v1 thread.

Quoted 95 lines · Philippe Mathieu-Daudé

Cc'ing the TCG MIPS maintainers, and also
Cc'ing Richard who made a comment in v1.

Quoted 4 lines · Kele Huang

Detect mips store instructions in cpu_signal_handler for all MIPS versions, and set is_write if encountering such store instructions.

This fixed the error while dealing with self-modifed code for MIPS.

Quoting Eric Blake:

"It's better to post a v2 as a new top-level thread rather than buried in-reply-to the v1 thread; among other things, burying a reply can cause automated patch tooling to miss the updated series."

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void

Quoted 12 lines
---
 accel/tcg/user-exec.c | 51 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +0 -0
index bb039eb32d..18784516e5 100644
@@ -710,11 +710,60 @@ int cpu_signal_handler(int host_signum, void

*pinfo,

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo,

Quoted 7 lines
accel/tcg/user-exec.c +2 -1
     greg_t pc = uc->uc_mcontext.pc;
     int is_write;
 
-    /* XXX: compute is_write */
     is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter.

*/

Quoted 23 lines
accel/tcg/user-exec.c +23 -0
+    uint32_t insn = *(uint32_t *)pc;
+    switch(insn>>29) {
+    case 0x5:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SB */
+        case 0x1: /* SH */
+        case 0x2: /* SWL */
+        case 0x3: /* SW */
+        case 0x4: /* SDL */
+        case 0x5: /* SDR */
+        case 0x6: /* SWR */
+            is_write = 1;
+        }
+        break;
+    case 0x7:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SC */
+        case 0x1: /* SWC1 */
+        case 0x4: /* SCD */
+        case 0x5: /* SDC1 */
+        case 0x7: /* SD */
+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
+        case 0x2: /* SWC2 */

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo,

Quoted 10 lines
accel/tcg/user-exec.c +10 -0
+        case 0x6: /* SDC2 */
+#endif
+            is_write = 1;
+        }
+        break;
+    }
+
+    /*
+     * Required in all versions of MIPS64 since MIPS64r1. Not available
+     * in MIPS32r1. Required by MIPS32r2 and subsequent versions of

MIPS32.

Quoted 22 lines
accel/tcg/user-exec.c +15 -0
+     */
+    switch ((insn >> 3) & 0x7) {
+    case 0x1:
+        switch (insn & 0x7) {
+        case 0x0: /* SWXC1 */
+        case 0x1: /* SDXC1 */
+            is_write = 1;
+        }
+        break;
+    }
+
     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
 }
 
+#elif defined(__misp16) || defined(__mips_micromips)
+
+#error "Unsupported encoding"
+
 #elif defined(__riscv)
 
 int cpu_signal_handler(int host_signum, void *pinfo,
author
#3 lore raw
In reply to Philippe Mathieu-Daudé

Sorry about my misunderstanding of your guidelines.
What is more, I have resend a new v2 patch as a new top-level thread and CC to TCG MIPS maintainers and Richard.

Quoted 95 lines · Philippe Mathieu-Daudé

Cc'ing the TCG MIPS maintainers, and also
Cc'ing Richard who made a comment in v1.

Quoted 4 lines · Kele Huang

Detect mips store instructions in cpu_signal_handler for all MIPS versions, and set is_write if encountering such store instructions.

This fixed the error while dealing with self-modifed code for MIPS.

Quoting Eric Blake:

"It's better to post a v2 as a new top-level thread rather than buried in-reply-to the v1 thread; among other things, burying a reply can cause automated patch tooling to miss the updated series."

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void

Quoted 12 lines
---
 accel/tcg/user-exec.c | 51 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +0 -0
index bb039eb32d..18784516e5 100644
@@ -710,11 +710,60 @@ int cpu_signal_handler(int host_signum, void

*pinfo,

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo,

Quoted 7 lines
accel/tcg/user-exec.c +2 -1
     greg_t pc = uc->uc_mcontext.pc;
     int is_write;
 
-    /* XXX: compute is_write */
     is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter.

*/

Quoted 23 lines
accel/tcg/user-exec.c +23 -0
+    uint32_t insn = *(uint32_t *)pc;
+    switch(insn>>29) {
+    case 0x5:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SB */
+        case 0x1: /* SH */
+        case 0x2: /* SWL */
+        case 0x3: /* SW */
+        case 0x4: /* SDL */
+        case 0x5: /* SDR */
+        case 0x6: /* SWR */
+            is_write = 1;
+        }
+        break;
+    case 0x7:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SC */
+        case 0x1: /* SWC1 */
+        case 0x4: /* SCD */
+        case 0x5: /* SDC1 */
+        case 0x7: /* SD */
+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
+        case 0x2: /* SWC2 */

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo,

Quoted 10 lines
accel/tcg/user-exec.c +10 -0
+        case 0x6: /* SDC2 */
+#endif
+            is_write = 1;
+        }
+        break;
+    }
+
+    /*
+     * Required in all versions of MIPS64 since MIPS64r1. Not available
+     * in MIPS32r1. Required by MIPS32r2 and subsequent versions of

MIPS32.

Quoted 22 lines
accel/tcg/user-exec.c +15 -0
+     */
+    switch ((insn >> 3) & 0x7) {
+    case 0x1:
+        switch (insn & 0x7) {
+        case 0x0: /* SWXC1 */
+        case 0x1: /* SDXC1 */
+            is_write = 1;
+        }
+        break;
+    }
+
     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
 }
 
+#elif defined(__misp16) || defined(__mips_micromips)
+
+#error "Unsupported encoding"
+
 #elif defined(__riscv)
 
 int cpu_signal_handler(int host_signum, void *pinfo,
#4 lore raw
In reply to Kele Huang
Quoted 16 lines · Kele Huang

Detect mips store instructions in cpu_signal_handler for all MIPS versions, and set is_write if encountering such store instructions.

This fixed the error while dealing with self-modifed code for MIPS.

---
 accel/tcg/user-exec.c | 51 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +0 -0
index bb039eb32d..18784516e5 100644

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 717

Quoted 9 lines
accel/tcg/user-exec.c +4 -1
@@ -710,11 +710,60 @@ int cpu_signal_handler(int host_signum, void *pinfo,
     greg_t pc = uc->uc_mcontext.pc;
     int is_write;
 
-    /* XXX: compute is_write */
     is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter. */
+    uint32_t insn = *(uint32_t *)pc;
+    switch(insn>>29) {

This would be easier if you simply looked at the entire major opcode field, beginning at bit 26.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 728

Quoted 11 lines
accel/tcg/user-exec.c +11 -0
+    case 0x5:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SB */
+        case 0x1: /* SH */
+        case 0x2: /* SWL */
+        case 0x3: /* SW */
+        case 0x4: /* SDL */
+        case 0x5: /* SDR */
+        case 0x6: /* SWR */
+            is_write = 1;
+        }

So this becomes

    case 050: /* SB */
    case 051: /* SH */
      ...

I know there are some who don't like octal, but IMO MIPS and its 6 bit fields and 8x8 tables is a natural fit -- one can read the two octal digits right off of the table.

Otherwise, perhaps you'd prefer binary constants like 0b101000. But with these tables I find the mental bit-extract from hex to be tiresome.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 741

Quoted 13 lines
accel/tcg/user-exec.c +13 -0
+        break;
+    case 0x7:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SC */
+        case 0x1: /* SWC1 */
+        case 0x4: /* SCD */
+        case 0x5: /* SDC1 */
+        case 0x7: /* SD */
+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
+        case 0x2: /* SWC2 */
+        case 0x6: /* SDC2 */
+#endif
+            is_write = 1;

Similarly.

Quoted 7 lines
accel/tcg/user-exec.c +7 -0
+        }
+        break;
+    }
+
+    /*
+     * Required in all versions of MIPS64 since MIPS64r1. Not available
+     * in MIPS32r1. Required by MIPS32r2 and subsequent versions of MIPS32.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 758

Quoted 10 lines
accel/tcg/user-exec.c +10 -0
+     */
+    switch ((insn >> 3) & 0x7) {
+    case 0x1:
+        switch (insn & 0x7) {
+        case 0x0: /* SWXC1 */
+        case 0x1: /* SDXC1 */
+            is_write = 1;
+        }
+        break;
+    }

This switch is incorrectly placed. It must be within the first switch, under major opcode 023 (COP1X). And again, you should extract the entire 6-bit minor opcode all at once, not one octal digit at a time.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 765

Quoted 3 lines
accel/tcg/user-exec.c +3 -0
+#elif defined(__misp16) || defined(__mips_micromips)
+
+#error "Unsupported encoding"

This is incorrectly placed, because we've already successfully entered the preceeding #elif defined(__mips__). This needs to be

#elif defined(__mips__)

# if defined(__mips16) || defined(__mips_micromips)
#  error
# endif

int cpu_signal_handler(int host_signum, void *pinfo,
void *puc)
{
...
}

#elif defined(__riscv)

r~

author
#5 lore raw

Got it. Thank you again!
I have resend a brand new v3 patch.

Quoted 125 lines · Richard Henderson

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void

Quoted 16 lines · Kele Huang

Detect mips store instructions in cpu_signal_handler for all MIPS versions, and set is_write if encountering such store instructions.

This fixed the error while dealing with self-modifed code for MIPS.

---
 accel/tcg/user-exec.c | 51 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
accel/tcg/user-exec.c +0 -0
index bb039eb32d..18784516e5 100644
@@ -710,11 +710,60 @@ int cpu_signal_handler(int host_signum, void

*pinfo,

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo,

Quoted 7 lines
accel/tcg/user-exec.c +2 -1
     greg_t pc = uc->uc_mcontext.pc;
     int is_write;
 
-    /* XXX: compute is_write */
     is_write = 0;
+
+    /* Detect store by reading the instruction at the program counter.

*/

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 717

Quoted 2 lines
accel/tcg/user-exec.c +2 -0
+    uint32_t insn = *(uint32_t *)pc;
+    switch(insn>>29) {

This would be easier if you simply looked at the entire major opcode field, beginning at bit 26.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 728

Quoted 11 lines
accel/tcg/user-exec.c +11 -0
+    case 0x5:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SB */
+        case 0x1: /* SH */
+        case 0x2: /* SWL */
+        case 0x3: /* SW */
+        case 0x4: /* SDL */
+        case 0x5: /* SDR */
+        case 0x6: /* SWR */
+            is_write = 1;
+        }

So this becomes

    case 050: /* SB */
    case 051: /* SH */
      ...

I know there are some who don't like octal, but IMO MIPS and its 6 bit fields
and 8x8 tables is a natural fit -- one can read the two octal digits right off
of the table.

Otherwise, perhaps you'd prefer binary constants like 0b101000. But with these
tables I find the mental bit-extract from hex to be tiresome.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 741

Quoted 13 lines
accel/tcg/user-exec.c +13 -0
+        break;
+    case 0x7:
+        switch((insn>>26) & 0x7) {
+        case 0x0: /* SC */
+        case 0x1: /* SWC1 */
+        case 0x4: /* SCD */
+        case 0x5: /* SDC1 */
+        case 0x7: /* SD */
+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
+        case 0x2: /* SWC2 */
+        case 0x6: /* SDC2 */
+#endif
+            is_write = 1;

Similarly.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo,

Quoted 7 lines
accel/tcg/user-exec.c +7 -0
+        }
+        break;
+    }
+
+    /*
+     * Required in all versions of MIPS64 since MIPS64r1. Not available
+     * in MIPS32r1. Required by MIPS32r2 and subsequent versions of

MIPS32.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 758

Quoted 10 lines
accel/tcg/user-exec.c +10 -0
+     */
+    switch ((insn >> 3) & 0x7) {
+    case 0x1:
+        switch (insn & 0x7) {
+        case 0x0: /* SWXC1 */
+        case 0x1: /* SDXC1 */
+            is_write = 1;
+        }
+        break;
+    }

This switch is incorrectly placed. It must be within the first switch, under
major opcode 023 (COP1X). And again, you should extract the entire 6-bit minor
opcode all at once, not one octal digit at a time.

accel/tcg/user-exec.c int cpu_signal_handler(int host_signum, void *pinfo, · line 765

Quoted 3 lines
accel/tcg/user-exec.c +3 -0
+#elif defined(__misp16) || defined(__mips_micromips)
+
+#error "Unsupported encoding"

This is incorrectly placed, because we've already successfully entered the preceeding #elif defined(__mips__). This needs to be

#elif defined(__mips__)

# if defined(__mips16) || defined(__mips_micromips)
#  error
# endif

int cpu_signal_handler(int host_signum, void *pinfo,
void *puc)

{
   ...
}

#elif defined(__riscv)

r~