Skip to contentSkip to conversation

RISC-V: Fix riscv_isa_string memory size bug

PATCH In review Michael Clark 6 participants 13 messages Mar 16, 2018 – Mar 24, 2018 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 RISC-V: Fix riscv_isa_string memory size bug

This version uses a constant size memory buffer sized for the maximum possible ISA string length. It also uses g_new instead of g_new0, uses more efficient logic to append extensions and adds manual zero termination of the string.

Cc Palmer Dabbelt <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 target/riscv/cpu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
target/riscv/cpu.c +6 -6
index 4851890..298abbd 100644
@@ -391,16 +391,16 @@ static const TypeInfo riscv_cpu_type_info = {
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    size_t maxlen = 5 + ctz32(cpu->env.misa);
-    char *isa_string = g_new0(char, maxlen);
-    snprintf(isa_string, maxlen, "rv%d", TARGET_LONG_BITS);
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    char *isa_str = g_new(char, maxlen);
+    char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
         if (cpu->env.misa & RV(riscv_exts[i])) {
-            isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a';
-
+            *p++ = riscv_exts[i] - 'A' + 'a';
         }
     }
-    return isa_string;
+    *p = '\0';
+    return isa_str;
 }
 
 void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf)
Signature · 1 line
2.7.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 22 lines · Michael Clark

This version uses a constant size memory buffer sized for the maximum possible ISA string length. It also uses g_new instead of g_new0, uses more efficient logic to append extensions and adds manual zero termination of the string.

Cc Palmer Dabbelt <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 target/riscv/cpu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
target/riscv/cpu.c +0 -1
index 4851890..298abbd 100644
@@ -391,16 +391,16 @@ static const TypeInfo riscv_cpu_type_info = {
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    size_t maxlen = 5 + ctz32(cpu->env.misa);

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 399

Quoted 10 lines
target/riscv/cpu.c +4 -4
-    char *isa_string = g_new0(char, maxlen);
-    snprintf(isa_string, maxlen, "rv%d", TARGET_LONG_BITS);
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    char *isa_str = g_new(char, maxlen);
+    char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
         if (cpu->env.misa & RV(riscv_exts[i])) {
-            isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a';
-
+            *p++ = riscv_exts[i] - 'A' + 'a';
                                                 /* tolower() */

I prefer unobfuscated code (think newcomers and reviewers) :|

Quoted 9 lines
target/riscv/cpu.c +2 -1
         }
     }
-    return isa_string;
+    *p = '\0';
+    return isa_str;
 }
 
 void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf)
author Reviewed-by Philippe Mathieu-Daudé
#2 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Michael Clark

This version uses a constant size memory buffer sized for the maximum possible ISA string length. It also uses g_new instead of g_new0, uses more efficient logic to append extensions and adds manual zero termination of the string.

Cc Palmer Dabbelt <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 target/riscv/cpu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
target/riscv/cpu.c +0 -0
index 4851890..298abbd 100644

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 396

Quoted 9 lines
target/riscv/cpu.c +3 -3
@@ -391,16 +391,16 @@ static const TypeInfo riscv_cpu_type_info = {
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    size_t maxlen = 5 + ctz32(cpu->env.misa);
-    char *isa_string = g_new0(char, maxlen);
-    snprintf(isa_string, maxlen, "rv%d", TARGET_LONG_BITS);
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    char *isa_str = g_new(char, maxlen);
+    char *p = isa_str + snprintf(isa_str, maxlen, "rv%d",

TARGET_LONG_BITS);

Quoted 9 lines

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_info = { · line 399

Quoted 5 lines
target/riscv/cpu.c +1 -2
     for (i = 0; i < sizeof(riscv_exts); i++) {
         if (cpu->env.misa & RV(riscv_exts[i])) {
-            isa_string[strlen(isa_string)] = riscv_exts[i] - 'A' + 'a';
-
+            *p++ = riscv_exts[i] - 'A' + 'a';
                                                 /* tolower() */

I prefer unobfuscated code (think newcomers and reviewers) :|

Thanks! Done.

I think i'll add the riscv_isa_string fix onto the RISC-V post-merge spec conformance and cleanup series, and send I'll send out a (hopefully final) version of it today, so we can merge all of the spec conformance, bug fixes and cleanups in one PR.

The RISC-V post-merge spec conformance and cleanup series has had a lot of testing. I've been using it to compile QEMU inside of QEMU using the RISC-V Fedora Image and its native RISC-V GCC toolchain running inside SMP Linux 4.16-rc2. It appears to be pretty rock-solid. The rcu lock fix would likely only affect users who are ballooning memory while a guest is under load. The page walker changes have also been tested under load (including performance tests).

FYI - I also have an experimental branch containing a RISC-V TCG back-end that I started on during the RISC-V Hackathon in Portland last week:

- https://github.com/michaeljclark/riscv-qemu/tree/wip-riscv-tcg-backend

I'm able to run a very simple x86_64 hello world program on RISC-V, avoiding all of the usual libc startup. However, it may be some time before I submit a patch series for this branch. So far the RISC-V TCG backend doesn't implement softmmu, have bigendian byteswapping in its load/store implementation or 64-bit on 32-bit support (brcond2/setcond2). This will come, however, at present I'm still testing the basic codegen. That being said, it has meant that i've been using the RISC-V QEMU Fedora image pretty extensively as my main development machine, with the RISC-V post-merge spec conformance and cleanup series applied, performing a lot of parallel makes on a quad-core i7. The only thing I am really missing is gdb. The current RISC-V GDB port seems to be missing user-space debugging support as I believe the focus has been on the JTAG remote debugging use case.

[mclark@stage4 riscv-qemu]$ uname -a
Linux stage4.fedoraproject.org 4.16.0-rc2-00328-gebea62367bc4 #4 SMP Mon

Feb 26 16:05:16 GMT 2018 riscv64 riscv64 riscv64 GNU/Linux

[mclark@stage4 riscv-qemu]$ ./riscv64-linux-user/qemu-riscv64 hello.riscv64 Hello World
[mclark@stage4 riscv-qemu]$ ./x86_64-linux-user/qemu-x86_64 hello.x86_64 Hello World

Quoted 11 lines
Quoted 10 lines
target/riscv/cpu.c +2 -1
         }
     }
-    return isa_string;
+    *p = '\0';
+    return isa_str;
 }
 
 void riscv_cpu_list(FILE *f, fprintf_function cpu_fprintf)
#3 lore raw
Quoted 7 lines · Michael Clark

The RISC-V post-merge spec conformance and cleanup series has had a lot of testing. I've been using it to compile QEMU inside of QEMU using the RISC-V Fedora Image and its native RISC-V GCC toolchain running inside SMP Linux 4.16-rc2. It appears to be pretty rock-solid. The rcu lock fix would likely only affect users who are ballooning memory while a guest is under load. The page walker changes have also been tested under load (including performance tests).

Did you see the problem with restoring floating point registers on context switch? The test case is quite simple:

http://oirase.annexia.org/tmp/sched.c

Note you must compile it with gcc -O2 to manifest the bug. (We originally thought it was a problem with gcc's optimization, but it isn't.)

In Fedora's qemu tree are carrying the following patch which is just a workaround:

https://github.com/rwmjones/fedora-riscv-bootstrap/blob/master/stage1-riscv-qemu/force-float-save.patch

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
#4 lore raw
Quoted 8 lines · Richard W.M. Jones
Quoted 1 line · Michael Clark

The RISC-V post-merge spec conformance and cleanup series has had a lot

of

Quoted 1 line

testing. I've been using it to compile QEMU inside of QEMU using the

RISC-V

Quoted 2 lines

Fedora Image and its native RISC-V GCC toolchain running inside SMP Linux 4.16-rc2. It appears to be pretty rock-solid. The rcu lock fix would

likely

Quoted 8 lines
Quoted 3 lines

only affect users who are ballooning memory while a guest is under load. The page walker changes have also been tested under load (including performance tests).

Did you see the problem with restoring floating point registers on context switch? The test case is quite simple:

http://oirase.annexia.org/tmp/sched.c

No I didn't. Thanks.

Quoted 4 lines

Note you must compile it with gcc -O2 to manifest the bug. (We originally thought it was a problem with gcc's optimization, but it isn't.)

Okay. I must admit most of my testing has been integer heavy workloads.

Quoted 5 lines

In Fedora's qemu tree are carrying the following patch which is just a workaround:

https://github.com/rwmjones/fedora-riscv-bootstrap/blob/
master/stage1-riscv-qemu/force-float-save.patch

I will take a look...

There were some changes around v4 of our port upstream patch series to add MSTATUS.FS (TB_FLAGS_FP_ENABLE) to to tb->flags to avoid checking the flags on all FP ops.

static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong *pc,
target_ulong *cs_base, uint32_t
*flags)

{
    *pc = env->pc;
    *cs_base = 0;
#ifdef CONFIG_USER_ONLY
    *flags = TB_FLAGS_FP_ENABLE;
#else
    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);

#endif
}

I'll have to get my head around the implication of this or with the privilege level and how MSTATUS.FS is currently set...

#5 lore raw
Quoted 38 lines · Michael Clark
Quoted 9 lines · Richard W.M. Jones
Quoted 1 line · Michael Clark

The RISC-V post-merge spec conformance and cleanup series has had a lot

of

Quoted 1 line

testing. I've been using it to compile QEMU inside of QEMU using the

RISC-V

Quoted 1 line

Fedora Image and its native RISC-V GCC toolchain running inside SMP

Linux

Quoted 1 line

4.16-rc2. It appears to be pretty rock-solid. The rcu lock fix would

likely

Quoted 8 lines
Quoted 3 lines

only affect users who are ballooning memory while a guest is under load. The page walker changes have also been tested under load (including performance tests).

Did you see the problem with restoring floating point registers on context switch? The test case is quite simple:

http://oirase.annexia.org/tmp/sched.c

No I didn't. Thanks.

Quoted 4 lines

Note you must compile it with gcc -O2 to manifest the bug. (We originally thought it was a problem with gcc's optimization, but it isn't.)

Okay. I must admit most of my testing has been integer heavy workloads.

Quoted 3 lines

In Fedora's qemu tree are carrying the following patch which is just a workaround:

Quoted 8 lines
Quoted 2 lines

https://github.com/rwmjones/fedora-riscv-bootstrap/blob/mast
er/stage1-riscv-qemu/force-float-save.patch

I will take a look...

From my local testing, it appears to only show up with SMP enabled. With 1

CPU enabled the sched.c test passes.

Quoted 4 lines

There were some changes around v4 of our port upstream patch series to add MSTATUS.FS (TB_FLAGS_FP_ENABLE) to to tb->flags to avoid checking the flags on all FP ops.

I'm curious about the change in v4 of our upstream patch series "Enforce MSTATUS_FS via TB flags"

-
https://github.com/riscv/riscv-qemu/commit/1b0631573008d19b2cb4bb1ef388bd51d6a1d722

I'll could try and revert this and see if it makes any difference... however I think the float code has changed substantially with respect to handling of flags. We could backport the more conservative approach of handling MSTATUS_FS in the float routines. We should also run the test case on hardware to rule out linux-kernel MSTATIS_FS bugs...

BTW We didn't have MTTCG enabled until v6 of our patch series so given it only shows up on SMP, it wouldn't have been visible until v6 or after.

static inline void cpu_get_tb_cpu_state(CPURISCVState *env, target_ulong

Quoted 16 lines

*pc,
target_ulong *cs_base, uint32_t
*flags)

{
    *pc = env->pc;
    *cs_base = 0;
#ifdef CONFIG_USER_ONLY
    *flags = TB_FLAGS_FP_ENABLE;
#else
    *flags = cpu_mmu_index(env, 0) | (env->mstatus & MSTATUS_FS);

#endif
}

I'll have to get my head around the implication of this or with the privilege level and how MSTATUS.FS is currently set...

#6 lore raw
Quoted 8 lines · Michael Clark
Quoted 7 lines · Michael Clark
Quoted 4 lines · Richard W.M. Jones

Did you see the problem with restoring floating point registers on context switch? The test case is quite simple:

http://oirase.annexia.org/tmp/sched.c

Quoted 8 lines
Quoted 1 line

No I didn't. Thanks.

From my local testing, it appears to only show up with SMP enabled. With 1 CPU enabled the sched.c test passes.
I'll could try and revert this and see if it makes any difference... however I think the float code has changed substantially with respect to handling of flags. We could backport the more conservative approach of handling MSTATUS_FS in the float routines. We should also run the test case on hardware to rule out linux-kernel MSTATUS_FS bugs...

DJ, am I remembering correctly that you tried the test case on the HiFive evaluation board and it didn't demonstrate the bug?

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
#7 lore raw
Quoted 2 lines · Richard W.M. Jones

DJ, am I remembering correctly that you tried the test case on the HiFive evaluation board and it didn't demonstrate the bug?

I tested it on the vc707 board, without seeing the bug.

I can test other test cases if needed, I've got the board running Fedora at the moment.

#8 lore raw
Quoted 9 lines · DJ Delorie
Quoted 2 lines · Richard W.M. Jones

DJ, am I remembering correctly that you tried the test case on the HiFive evaluation board and it didn't demonstrate the bug?

I tested it on the vc707 board, without seeing the bug.

I can test other test cases if needed, I've got the board running Fedora at the moment.

Okay, I'll assume its a QEMU RISC-V bug.

#9 lore raw
In reply to DJ Delorie
Quoted 5 lines · On Thu, 22 Mar 2018 15:17:16 PDT (-0700), [email protected] wrote:
Quoted 2 lines · Richard W.M. Jones

DJ, am I remembering correctly that you tried the test case on the HiFive evaluation board and it didn't demonstrate the bug?

I tested it on the vc707 board, without seeing the bug.

FWIW, the cores in the bitstream for the vc707 are almost identical to the FU540-C000.

Quoted 2 lines

I can test other test cases if needed, I've got the board running Fedora at the moment.

Thanks!

#10 lore raw
In reply to Michael Clark
Quoted 4 lines · Michael Clark

FYI - I also have an experimental branch containing a RISC-V TCG back-end that I started on during the RISC-V Hackathon in Portland last week:

- https://github.com/michaeljclark/riscv-qemu/tree/wip-riscv-tcg-backend

Cool.

Quoted 5 lines

I'm able to run a very simple x86_64 hello world program on RISC-V, avoiding all of the usual libc startup. However, it may be some time before I submit a patch series for this branch. So far the RISC-V TCG backend doesn't implement softmmu, have bigendian byteswapping in its load/store implementation ...

Before you work too hard on this last, ping me. I've been intending to change TCG such that hosts that do not support byte-swapping memory operations do not need to handle them specially. Instead, we'd expand a "normal" byte swap from a temporary after a load (and similarly before a store).

I expect this to improve the state of affairs for i386 (without movbe), where we are currently extremely limited in the available registers.

r~

#11 lore raw
Quoted 15 lines · Richard Henderson
Quoted 4 lines · Michael Clark

FYI - I also have an experimental branch containing a RISC-V TCG back-end that I started on during the RISC-V Hackathon in Portland last week:

- https://github.com/michaeljclark/riscv-qemu/tree/wip-riscv-tcg-backend

Cool.

Quoted 2 lines

I'm able to run a very simple x86_64 hello world program on RISC-V, avoiding all of the usual libc startup. However, it may be some time

before

Quoted 3 lines

I submit a patch series for this branch. So far the RISC-V TCG backend doesn't implement softmmu, have bigendian byteswapping in its load/store implementation ...

Quoted 8 lines

Before you work too hard on this last, ping me. I've been intending to change
TCG such that hosts that do not support byte-swapping memory operations do not
need to handle them specially. Instead, we'd expand a "normal" byte swap from
a temporary after a load (and similarly before a store).

Okay. I'll leave the byte swapping out for now. The first priority is to test the basic codegen with little endian front-ends e.g. riscv -> riscv and x86 -> riscv, and the second priority is to implement softmmu. I still have some bugs as its pretty new code, but as I mentioned, the bones are there. i.e. we have relocs and instruction encoders for riscv.

Quoted 4 lines

I expect this to improve the state of affairs for i386 (without movbe), where
we are currently extremely limited in the available registers.

Okay, so this would move byte swapping into TCG generic code instead of the TCG backend, unless the backend explicitly supports load/store with byte swap?

#12 lore raw
Quoted 2 lines · Michael Clark

Okay, so this would move byte swapping into TCG generic code instead of the TCG backend, unless the backend explicitly supports load/store with byte swap?

Yes.

r~