Skip to contentSkip to conversation

Refresh the dynamic CSR xml after updating the state of the cpu.

PATCH In review Tommy Wu 3 participants 15 messages May 23, 2023 – Jun 12, 2023 RISC-V

lore
Share & tools
lore

Tommy Wu

author
#0 lore raw

cover Refresh the dynamic CSR xml after updating the state of the cpu.

The realize function of IMSIC will force select AIA feature and setup CSRs, but the dynamic CSR xml is not updated, so we cannot print the AIA CSRs in the remote gdb debugger.

In the patches, we provide a function to refresh the dynamic CSR xml, and refresh the xml in the realize function of IMSIC.

Tommy Wu (2):
target/riscv: Add a function to refresh the dynamic CSRs xml. hw/intc: riscv_imsic: Refresh the CSRs xml after updating the state of
the cpu.

 hw/intc/riscv_imsic.c  |  4 ++++
 target/riscv/cpu.h     |  2 ++
 target/riscv/gdbstub.c | 12 ++++++++++++
 3 files changed, 18 insertions(+)
Signature · 1 line
2.38.1
author
#1 lore raw

PATCH 1/2 target/riscv: Add a function to refresh the dynamic CSRs xml.

When we change the cpu extension state after the cpu is realized, we cannot print the value of some CSRs in the remote gdb debugger. The root cause is that the dynamic CSR xml is generated when the cpu is realized.

This patch add a function to refresh the dynamic CSR xml after the cpu is realized.

---
 target/riscv/cpu.h     |  2 ++
 target/riscv/gdbstub.c | 12 ++++++++++++
 2 files changed, 14 insertions(+)
target/riscv/cpu.h +2 -0
index de7e43126a..dc8e592275 100644
@@ -494,6 +494,7 @@ struct ArchCPU {
     CPUNegativeOffsetState neg;
     CPURISCVState env;
 
+    int dyn_csr_base_reg;
     char *dyn_csr_xml;
     char *dyn_vreg_xml;
 
@@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
 
 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
+void riscv_refresh_dynamic_csr_xml(CPUState *cs);
 
 uint8_t satp_mode_max_from_map(uint32_t map);
 const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
target/riscv/gdbstub.c +12 -0
index 524bede865..9e97ee2c35 100644
@@ -230,6 +230,8 @@ static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
         bitsize = 64;
     }
 
+    cpu->dyn_csr_base_reg = base_reg;
+
     g_string_printf(s, "<?xml version=\"1.0\"?>");
     g_string_append_printf(s, "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">");
     g_string_append_printf(s, "<feature name=\"org.gnu.gdb.riscv.csr\">");
@@ -349,3 +351,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
                                  "riscv-csr.xml", 0);
     }
 }
+
+void riscv_refresh_dynamic_csr_xml(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }
+    g_free(cpu->dyn_csr_xml);
+    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);
+}
Signature · 1 line
2.38.1
#2 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Quoted 20 lines · Tommy Wu

When we change the cpu extension state after the cpu is realized, we cannot print the value of some CSRs in the remote gdb debugger. The root cause is that the dynamic CSR xml is generated when the cpu is realized.

This patch add a function to refresh the dynamic CSR xml after the cpu is realized.

---
  target/riscv/cpu.h     |  2 ++
  target/riscv/gdbstub.c | 12 ++++++++++++
  2 files changed, 14 insertions(+)
target/riscv/cpu.h +0 -0
index de7e43126a..dc8e592275 100644

target/riscv/cpu.h struct ArchCPU { · line 497

Quoted 4 lines
target/riscv/cpu.h +1 -0
@@ -494,6 +494,7 @@ struct ArchCPU {
      CPUNegativeOffsetState neg;
      CPURISCVState env;
 
+    int dyn_csr_base_reg;

dyn_csr_base_reg  seems have no additional effect on the function.

And It remains unmodified in following riscv_refresh_dynamic_csr_xml().

Regards,

Weiwei Li

Quoted 38 lines
target/riscv/cpu.h +1 -0
      char *dyn_csr_xml;
      char *dyn_vreg_xml;
 
@@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
  void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
 
  void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
+void riscv_refresh_dynamic_csr_xml(CPUState *cs);
 
  uint8_t satp_mode_max_from_map(uint32_t map);
  const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
target/riscv/gdbstub.c +12 -0
index 524bede865..9e97ee2c35 100644
@@ -230,6 +230,8 @@ static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
          bitsize = 64;
      }
 
+    cpu->dyn_csr_base_reg = base_reg;
+
      g_string_printf(s, "<?xml version=\"1.0\"?>");
      g_string_append_printf(s, "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">");
      g_string_append_printf(s, "<feature name=\"org.gnu.gdb.riscv.csr\">");
@@ -349,3 +351,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
                                   "riscv-csr.xml", 0);
      }
  }
+
+void riscv_refresh_dynamic_csr_xml(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }
+    g_free(cpu->dyn_csr_xml);
+    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);
+}
author
#3 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Hi Weiwei Li,

dyn_csr_base_reg will be used in riscv_refresh_dynamic_csr_xml We can initialize this variable when the cpu is realized. And used this variable in riscv_refresh_dynamic_csr_xml.

Best regards,
Tommy

Quoted 80 lines · Weiwei Li
Quoted 20 lines · Tommy Wu

When we change the cpu extension state after the cpu is realized, we cannot print the value of some CSRs in the remote gdb debugger. The root cause is that the dynamic CSR xml is generated when the cpu is realized.

This patch add a function to refresh the dynamic CSR xml after the cpu is realized.

---
  target/riscv/cpu.h     |  2 ++
  target/riscv/gdbstub.c | 12 ++++++++++++
  2 files changed, 14 insertions(+)
target/riscv/cpu.h +0 -0
index de7e43126a..dc8e592275 100644

target/riscv/cpu.h struct ArchCPU { · line 497

Quoted 4 lines
target/riscv/cpu.h +1 -0
@@ -494,6 +494,7 @@ struct ArchCPU {
      CPUNegativeOffsetState neg;
      CPURISCVState env;
 
+    int dyn_csr_base_reg;

dyn_csr_base_reg seems have no additional effect on the function.

And It remains unmodified in following riscv_refresh_dynamic_csr_xml().

Regards,

Weiwei Li

target/riscv/cpu.h void riscv_get_csr_ops(int csrno,

Quoted 4 lines
target/riscv/cpu.h +0 -0
      char *dyn_csr_xml;
      char *dyn_vreg_xml;
 
@@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno,

riscv_csr_operations *ops);

target/riscv/gdbstub.c static int riscv_gen_dynamic_csr_xml(CPUState *cs,

Quoted 12 lines
target/riscv/cpu.h +1 -0
  void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
 
  void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
+void riscv_refresh_dynamic_csr_xml(CPUState *cs);
 
  uint8_t satp_mode_max_from_map(uint32_t map);
  const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
target/riscv/gdbstub.c +0 -0
index 524bede865..9e97ee2c35 100644
@@ -230,6 +230,8 @@ static int riscv_gen_dynamic_csr_xml(CPUState *cs,

int base_reg)

target/riscv/gdbstub.c static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)

Quoted 7 lines
target/riscv/gdbstub.c +2 -0
          bitsize = 64;
      }
 
+    cpu->dyn_csr_base_reg = base_reg;
+
      g_string_printf(s, "<?xml version=\"1.0\"?>");
      g_string_append_printf(s, "<!DOCTYPE feature SYSTEM

\"gdb-target.dtd\">");

target/riscv/gdbstub.c static int riscv_gen_dynamic_csr_xml(CPUState *cs, · line 237

Quoted 1 line
target/riscv/gdbstub.c +0 -0
      g_string_append_printf(s, "<feature

name=\"org.gnu.gdb.riscv.csr\">");

target/riscv/gdbstub.c void

Quoted 1 line
target/riscv/gdbstub.c +0 -0
@@ -349,3 +351,13 @@ void

riscv_cpu_register_gdb_regs_for_features(CPUState *cs)

Quoted 13 lines
target/riscv/gdbstub.c +10 -0
                                   "riscv-csr.xml", 0);
      }
  }
+
+void riscv_refresh_dynamic_csr_xml(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }
+    g_free(cpu->dyn_csr_xml);
+    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);
+}
#4 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Quoted 4 lines · Tommy Wu

Hi Weiwei Li,

dyn_csr_base_reg will be used in riscv_refresh_dynamic_csr_xml We can initialize this variable when the cpu is realized.

I didn't find this initialization in following code.

Quoted 1 line

And used this variable in riscv_refresh_dynamic_csr_xml.

That's my question. In riscv_refresh_dynamic_csr_xml() , cpu->dyn_csr_base_reg is passed to riscv_gen_dynamic_csr_xml() as base_reg.

And then base_reg is assigned to cpu->dyn_csr_base_reg again in it. So it's unchanged in this progress.

Another question is  dyn_csr_base_reg seems have no additional function currently.

Regards,

Weiwei Li

Quoted 87 lines

Best regards,
Tommy

On Tue, May 23, 2023 at 10:38 PM Weiwei Li <[email protected]> wrote:

    On 2023/5/23 19:44, Tommy Wu wrote:
    > When we change the cpu extension state after the cpu is
    > realized, we cannot print the value of some CSRs in the remote
    > gdb debugger. The root cause is that the dynamic CSR xml is
    > generated when the cpu is realized.
    >
    > This patch add a function to refresh the dynamic CSR xml after
    > the cpu is realized.
    >
    > Signed-off-by: Tommy Wu <[email protected]>
    > Reviewed-by: Frank Chang <[email protected]>
    > ---
    >   target/riscv/cpu.h     |  2 ++
    >   target/riscv/gdbstub.c | 12 ++++++++++++
    >   2 files changed, 14 insertions(+)
    >
    > diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
    > index de7e43126a..dc8e592275 100644
    > --- a/target/riscv/cpu.h
    > +++ b/target/riscv/cpu.h
    > @@ -494,6 +494,7 @@ struct ArchCPU {
    >       CPUNegativeOffsetState neg;
    >       CPURISCVState env;
    >
    > +    int dyn_csr_base_reg;

    dyn_csr_base_reg  seems have no additional effect on the function.

    And It remains unmodified in following
    riscv_refresh_dynamic_csr_xml().

    Regards,

    Weiwei Li

    >       char *dyn_csr_xml;
    >       char *dyn_vreg_xml;
    >
    > @@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno,
    riscv_csr_operations *ops);
    >   void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
    >
    >   void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
    > +void riscv_refresh_dynamic_csr_xml(CPUState *cs);
    >
    >   uint8_t satp_mode_max_from_map(uint32_t map);
    >   const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
    > diff --git a/target/riscv/gdbstub.c b/target/riscv/gdbstub.c
    > index 524bede865..9e97ee2c35 100644
    > --- a/target/riscv/gdbstub.c
    > +++ b/target/riscv/gdbstub.c
    > @@ -230,6 +230,8 @@ static int
    riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
    >           bitsize = 64;
    >       }
    >
    > +    cpu->dyn_csr_base_reg = base_reg;
    > +
    >       g_string_printf(s, "<?xml version=\"1.0\"?>");
    >       g_string_append_printf(s, "<!DOCTYPE feature SYSTEM
    \"gdb-target.dtd\">");
    >       g_string_append_printf(s, "<feature
    name=\"org.gnu.gdb.riscv.csr\">");
    > @@ -349,3 +351,13 @@ void
    riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
    >                                    "riscv-csr.xml", 0);
    >       }
    >   }
    > +
    > +void riscv_refresh_dynamic_csr_xml(CPUState *cs)
    > +{
    > +    RISCVCPU *cpu = RISCV_CPU(cs);
    > +    if (!cpu->dyn_csr_xml) {
    > +        g_assert_not_reached();
    > +    }
    > +    g_free(cpu->dyn_csr_xml);
    > +    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);
    > +}
author
#5 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Hi WeiWei Li,

When the CPU is realizing, it will call riscv_gen_dynamic_csr_xml for the first time with the correct base_reg value.

code flow :

riscv_cpu_realize

→ riscv_cpu_register_gdb_regs_for_features

→ riscv_gen_dynamic_csr_xml

The functionality of cpu->dyn_csr_base_reg is to record the base_reg from

riscv_cpu_register_gdb_regs_for_features.

When we try to refresh the dynamic CSRs xml, we will call the function riscv_gen_dynamic_csr_xml

for the second time, and then we can give the correct base_reg value to the function

riscv_gen_dynamic_csr_xml, because we've record this value in the cpu->dyn_csr_base_reg.

Best Regards,

Tommy

Quoted 111 lines · Weiwei Li
Quoted 4 lines · Tommy Wu

Hi Weiwei Li,

dyn_csr_base_reg will be used in riscv_refresh_dynamic_csr_xml We can initialize this variable when the cpu is realized.

I didn't find this initialization in following code.

Quoted 1 line

And used this variable in riscv_refresh_dynamic_csr_xml.

That's my question. In riscv_refresh_dynamic_csr_xml() , cpu->dyn_csr_base_reg is passed to riscv_gen_dynamic_csr_xml() as base_reg.

And then base_reg is assigned to cpu->dyn_csr_base_reg again in it. So it's unchanged in this progress.

Another question is dyn_csr_base_reg seems have no additional function currently.

Regards,

Weiwei Li

Quoted 87 lines

Best regards,
Tommy

On Tue, May 23, 2023 at 10:38 PM Weiwei Li <[email protected]> wrote:

    On 2023/5/23 19:44, Tommy Wu wrote:
    > When we change the cpu extension state after the cpu is
    > realized, we cannot print the value of some CSRs in the remote
    > gdb debugger. The root cause is that the dynamic CSR xml is
    > generated when the cpu is realized.
    >
    > This patch add a function to refresh the dynamic CSR xml after
    > the cpu is realized.
    >
    > Signed-off-by: Tommy Wu <[email protected]>
    > Reviewed-by: Frank Chang <[email protected]>
    > ---
    >   target/riscv/cpu.h     |  2 ++
    >   target/riscv/gdbstub.c | 12 ++++++++++++
    >   2 files changed, 14 insertions(+)
    >
    > diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
    > index de7e43126a..dc8e592275 100644
    > --- a/target/riscv/cpu.h
    > +++ b/target/riscv/cpu.h
    > @@ -494,6 +494,7 @@ struct ArchCPU {
    >       CPUNegativeOffsetState neg;
    >       CPURISCVState env;
    >
    > +    int dyn_csr_base_reg;

    dyn_csr_base_reg  seems have no additional effect on the function.

    And It remains unmodified in following
    riscv_refresh_dynamic_csr_xml().

    Regards,

    Weiwei Li

    >       char *dyn_csr_xml;
    >       char *dyn_vreg_xml;
    >
    > @@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno,
    riscv_csr_operations *ops);
    >   void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
    >
    >   void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
    > +void riscv_refresh_dynamic_csr_xml(CPUState *cs);
    >
    >   uint8_t satp_mode_max_from_map(uint32_t map);
    >   const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
    > diff --git a/target/riscv/gdbstub.c b/target/riscv/gdbstub.c
    > index 524bede865..9e97ee2c35 100644
    > --- a/target/riscv/gdbstub.c
    > +++ b/target/riscv/gdbstub.c
    > @@ -230,6 +230,8 @@ static int
    riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
    >           bitsize = 64;
    >       }
    >
    > +    cpu->dyn_csr_base_reg = base_reg;
    > +
    >       g_string_printf(s, "<?xml version=\"1.0\"?>");
    >       g_string_append_printf(s, "<!DOCTYPE feature SYSTEM
    \"gdb-target.dtd\">");
    >       g_string_append_printf(s, "<feature
    name=\"org.gnu.gdb.riscv.csr\">");
    > @@ -349,3 +351,13 @@ void
    riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
    >                                    "riscv-csr.xml", 0);
    >       }
    >   }
    > +
    > +void riscv_refresh_dynamic_csr_xml(CPUState *cs)
    > +{
    > +    RISCVCPU *cpu = RISCV_CPU(cs);
    > +    if (!cpu->dyn_csr_xml) {
    > +        g_assert_not_reached();
    > +    }
    > +    g_free(cpu->dyn_csr_xml);
    > +    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);
    > +}
Reviewed-by
#6 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Quoted 17 lines · Tommy Wu

Hi WeiWei Li,

When the CPU is realizing, it will call riscv_gen_dynamic_csr_xml for the first time with the correct base_reg value.

code flow :

riscv_cpu_realize

→ riscv_cpu_register_gdb_regs_for_features

     → riscv_gen_dynamic_csr_xml

The functionality of cpu->dyn_csr_base_reg is to record the base_reg from

Quoted 9 lines

riscv_cpu_register_gdb_regs_for_features.

When we try to refresh the dynamic CSRs xml, we will call the function riscv_gen_dynamic_csr_xml

for the second time, and then we can give the correct base_reg value to the function

riscv_gen_dynamic_csr_xml, because we've record this value in the cpu->dyn_csr_base_reg.

Oh. Sorry, I stuck.

Weiwei Li

Quoted 126 lines

Best Regards,

Tommy

On Wed, May 24, 2023 at 10:10 AM Weiwei Li <[email protected]> wrote:

    On 2023/5/24 09:59, Tommy Wu wrote:
    > Hi Weiwei Li,
    >
    > `dyn_csr_base_reg` will be used in `riscv_refresh_dynamic_csr_xml`
    > We can initialize this variable when the cpu is realized.
    I didn't find this initialization in following code.
    > And used this variable in `riscv_refresh_dynamic_csr_xml`.

    That's my question. In riscv_refresh_dynamic_csr_xml() ,
    cpu->dyn_csr_base_reg is passed to riscv_gen_dynamic_csr_xml() as
    base_reg.

    And then base_reg is assigned to cpu->dyn_csr_base_reg again in
    it. So
    it's unchanged in this progress.

    Another question is  dyn_csr_base_reg seems have no additional
    function
    currently.

    Regards,

    Weiwei Li

    >
    > Best regards,
    > Tommy
    >
    >
    > On Tue, May 23, 2023 at 10:38 PM Weiwei Li
    <[email protected]> wrote:
    >
    >
    >     On 2023/5/23 19:44, Tommy Wu wrote:
    >     > When we change the cpu extension state after the cpu is
    >     > realized, we cannot print the value of some CSRs in the remote
    >     > gdb debugger. The root cause is that the dynamic CSR xml is
    >     > generated when the cpu is realized.
    >     >
    >     > This patch add a function to refresh the dynamic CSR xml after
    >     > the cpu is realized.
    >     >
    >     > Signed-off-by: Tommy Wu <[email protected]>
    >     > Reviewed-by: Frank Chang <[email protected]>
    >     > ---
    >     >   target/riscv/cpu.h     |  2 ++
    >     >   target/riscv/gdbstub.c | 12 ++++++++++++
    >     >   2 files changed, 14 insertions(+)
    >     >
    >     > diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
    >     > index de7e43126a..dc8e592275 100644
    >     > --- a/target/riscv/cpu.h
    >     > +++ b/target/riscv/cpu.h
    >     > @@ -494,6 +494,7 @@ struct ArchCPU {
    >     >       CPUNegativeOffsetState neg;
    >     >       CPURISCVState env;
    >     >
    >     > +    int dyn_csr_base_reg;
    >
    >     dyn_csr_base_reg  seems have no additional effect on the
    function.
    >
    >     And It remains unmodified in following
    >     riscv_refresh_dynamic_csr_xml().
    >
    >     Regards,
    >
    >     Weiwei Li
    >
    >     >       char *dyn_csr_xml;
    >     >       char *dyn_vreg_xml;
    >     >
    >     > @@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno,
    >     riscv_csr_operations *ops);
    >     >   void riscv_set_csr_ops(int csrno, riscv_csr_operations
    *ops);
    >     >
    >     >   void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
    >     > +void riscv_refresh_dynamic_csr_xml(CPUState *cs);
    >     >
    >     >   uint8_t satp_mode_max_from_map(uint32_t map);
    >     >   const char *satp_mode_str(uint8_t satp_mode, bool
    is_32_bit);
    >     > diff --git a/target/riscv/gdbstub.c b/target/riscv/gdbstub.c
    >     > index 524bede865..9e97ee2c35 100644
    >     > --- a/target/riscv/gdbstub.c
    >     > +++ b/target/riscv/gdbstub.c
    >     > @@ -230,6 +230,8 @@ static int
    >     riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
    >     >           bitsize = 64;
    >     >       }
    >     >
    >     > +    cpu->dyn_csr_base_reg = base_reg;
    >     > +
    >     >       g_string_printf(s, "<?xml version=\"1.0\"?>");
    >     >       g_string_append_printf(s, "<!DOCTYPE feature SYSTEM
    >     \"gdb-target.dtd\">");
    >     >       g_string_append_printf(s, "<feature
    >     name=\"org.gnu.gdb.riscv.csr\">");
    >     > @@ -349,3 +351,13 @@ void
    >     riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
    >     > "riscv-csr.xml", 0);
    >     >       }
    >     >   }
    >     > +
    >     > +void riscv_refresh_dynamic_csr_xml(CPUState *cs)
    >     > +{
    >     > +    RISCVCPU *cpu = RISCV_CPU(cs);
    >     > +    if (!cpu->dyn_csr_xml) {
    >     > +        g_assert_not_reached();
    >     > +    }
    >     > +    g_free(cpu->dyn_csr_xml);
    >     > +    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);
    >     > +}
    >
#7 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

In reply to Tommy Wu
Quoted 52 lines · Tommy Wu

When we change the cpu extension state after the cpu is realized, we cannot print the value of some CSRs in the remote gdb debugger. The root cause is that the dynamic CSR xml is generated when the cpu is realized.

This patch add a function to refresh the dynamic CSR xml after the cpu is realized.

---
 target/riscv/cpu.h     |  2 ++
 target/riscv/gdbstub.c | 12 ++++++++++++
 2 files changed, 14 insertions(+)
target/riscv/cpu.h +2 -0
index de7e43126a..dc8e592275 100644
@@ -494,6 +494,7 @@ struct ArchCPU {
     CPUNegativeOffsetState neg;
     CPURISCVState env;
 
+    int dyn_csr_base_reg;
     char *dyn_csr_xml;
     char *dyn_vreg_xml;
 
@@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
 
 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
+void riscv_refresh_dynamic_csr_xml(CPUState *cs);
 
 uint8_t satp_mode_max_from_map(uint32_t map);
 const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
target/riscv/gdbstub.c +2 -0
index 524bede865..9e97ee2c35 100644
@@ -230,6 +230,8 @@ static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
         bitsize = 64;
     }
 
+    cpu->dyn_csr_base_reg = base_reg;
+
     g_string_printf(s, "<?xml version=\"1.0\"?>");
     g_string_append_printf(s, "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">");
     g_string_append_printf(s, "<feature name=\"org.gnu.gdb.riscv.csr\">");
@@ -349,3 +351,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
                                  "riscv-csr.xml", 0);
     }

target/riscv/gdbstub.c void riscv_cpu_register_gdb_regs_for_features(CPUState *cs) · line 362

Quoted 10 lines
target/riscv/gdbstub.c +9 -0
 }
+
+void riscv_refresh_dynamic_csr_xml(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }
+    g_free(cpu->dyn_csr_xml);
+    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);

I don't really understand why we need dyn_csr_base_reg, could we just use cs->gdb_num_regs directly here?

Alistair

Quoted 5 lines
target/riscv/gdbstub.c +1 -1
+}
--

2.38.1

author
#8 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Hi Alistair,
Thanks for the suggestion! Do you mean

```
    ...
    g_free(cpu->dyn_csr_xml);
    riscv_gen_dynamic_csr_xml(cs, cpu-> gdb_num_regs  -  CSR_TABLE_SIZE);
    ...
``` ?

Or maybe we don't need this refresh function, and just add ext_ssaia & ext_smaia in the command line.

Best Regards,
Tommy

Quoted 80 lines · Alistair Francis

target/riscv/cpu.h void riscv_get_csr_ops(int csrno,

Quoted 29 lines · Tommy Wu

When we change the cpu extension state after the cpu is realized, we cannot print the value of some CSRs in the remote gdb debugger. The root cause is that the dynamic CSR xml is generated when the cpu is realized.

This patch add a function to refresh the dynamic CSR xml after the cpu is realized.

---
 target/riscv/cpu.h     |  2 ++
 target/riscv/gdbstub.c | 12 ++++++++++++
 2 files changed, 14 insertions(+)
target/riscv/cpu.h +1 -0
index de7e43126a..dc8e592275 100644
@@ -494,6 +494,7 @@ struct ArchCPU {
     CPUNegativeOffsetState neg;
     CPURISCVState env;
 
+    int dyn_csr_base_reg;
     char *dyn_csr_xml;
     char *dyn_vreg_xml;
 
@@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno,

riscv_csr_operations *ops);

target/riscv/gdbstub.c static int riscv_gen_dynamic_csr_xml(CPUState *cs,

Quoted 12 lines
target/riscv/cpu.h +1 -0
 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
 
 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
+void riscv_refresh_dynamic_csr_xml(CPUState *cs);
 
 uint8_t satp_mode_max_from_map(uint32_t map);
 const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
target/riscv/gdbstub.c +0 -0
index 524bede865..9e97ee2c35 100644
@@ -230,6 +230,8 @@ static int riscv_gen_dynamic_csr_xml(CPUState *cs,

int base_reg)

target/riscv/gdbstub.c static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)

Quoted 7 lines
target/riscv/gdbstub.c +2 -0
         bitsize = 64;
     }
 
+    cpu->dyn_csr_base_reg = base_reg;
+
     g_string_printf(s, "<?xml version=\"1.0\"?>");
     g_string_append_printf(s, "<!DOCTYPE feature SYSTEM

\"gdb-target.dtd\">");

target/riscv/gdbstub.c static int riscv_gen_dynamic_csr_xml(CPUState *cs, · line 237

Quoted 1 line
target/riscv/gdbstub.c +0 -0
     g_string_append_printf(s, "<feature

name=\"org.gnu.gdb.riscv.csr\">");

target/riscv/gdbstub.c void

Quoted 1 line
target/riscv/gdbstub.c +0 -0
@@ -349,3 +351,13 @@ void

riscv_cpu_register_gdb_regs_for_features(CPUState *cs)

target/riscv/gdbstub.c void riscv_cpu_register_gdb_regs_for_features(CPUState *cs) · line 362

Quoted 12 lines
target/riscv/gdbstub.c +9 -0
                                  "riscv-csr.xml", 0);
     }
 }
+
+void riscv_refresh_dynamic_csr_xml(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }
+    g_free(cpu->dyn_csr_xml);
+    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);

I don't really understand why we need dyn_csr_base_reg, could we just use cs->gdb_num_regs directly here?

Alistair

Quoted 5 lines
target/riscv/gdbstub.c +1 -1
+}
--

2.38.1

#9 lore raw

Re: [PATCH 1/2] target/riscv: Add a function to refresh the dynamic CSRs xml.

Quoted 9 lines · Tommy Wu

Hi Alistair,
Thanks for the suggestion! Do you mean

```
    ...
    g_free(cpu->dyn_csr_xml);
    riscv_gen_dynamic_csr_xml(cs, cpu-> gdb_num_regs  -  CSR_TABLE_SIZE);
    ...
``` ?

Yeah, pretty much. We already have cpu-> gdb_num_regs we should be able to use it directly.

Quoted 2 lines

Or maybe we don't need this refresh function, and just add ext_ssaia & ext_smaia in the command line.

That also works!

Alistair

Quoted 80 lines

Best Regards,
Tommy

Quoted 75 lines · Alistair Francis
Quoted 52 lines · Tommy Wu

When we change the cpu extension state after the cpu is realized, we cannot print the value of some CSRs in the remote gdb debugger. The root cause is that the dynamic CSR xml is generated when the cpu is realized.

This patch add a function to refresh the dynamic CSR xml after the cpu is realized.

---
 target/riscv/cpu.h     |  2 ++
 target/riscv/gdbstub.c | 12 ++++++++++++
 2 files changed, 14 insertions(+)
target/riscv/cpu.h +2 -0
index de7e43126a..dc8e592275 100644
@@ -494,6 +494,7 @@ struct ArchCPU {
     CPUNegativeOffsetState neg;
     CPURISCVState env;
 
+    int dyn_csr_base_reg;
     char *dyn_csr_xml;
     char *dyn_vreg_xml;
 
@@ -781,6 +782,7 @@ void riscv_get_csr_ops(int csrno, riscv_csr_operations *ops);
 void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
 
 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
+void riscv_refresh_dynamic_csr_xml(CPUState *cs);
 
 uint8_t satp_mode_max_from_map(uint32_t map);
 const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
target/riscv/gdbstub.c +2 -0
index 524bede865..9e97ee2c35 100644
@@ -230,6 +230,8 @@ static int riscv_gen_dynamic_csr_xml(CPUState *cs, int base_reg)
         bitsize = 64;
     }
 
+    cpu->dyn_csr_base_reg = base_reg;
+
     g_string_printf(s, "<?xml version=\"1.0\"?>");
     g_string_append_printf(s, "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">");
     g_string_append_printf(s, "<feature name=\"org.gnu.gdb.riscv.csr\">");
@@ -349,3 +351,13 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)
                                  "riscv-csr.xml", 0);
     }

target/riscv/gdbstub.c void riscv_cpu_register_gdb_regs_for_features(CPUState *cs) · line 362

Quoted 10 lines
target/riscv/gdbstub.c +9 -0
 }
+
+void riscv_refresh_dynamic_csr_xml(CPUState *cs)
+{
+    RISCVCPU *cpu = RISCV_CPU(cs);
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }
+    g_free(cpu->dyn_csr_xml);
+    riscv_gen_dynamic_csr_xml(cs, cpu->dyn_csr_base_reg);

I don't really understand why we need dyn_csr_base_reg, could we just use cs->gdb_num_regs directly here?

Alistair

Quoted 5 lines
target/riscv/gdbstub.c +1 -1
+}
--

2.38.1

author
#10 lore raw

PATCH 2/2 hw/intc: riscv_imsic: Refresh the CSRs xml after updating the state of the cpu.

In reply to Tommy Wu

Originally, when we set the ext_smaia to true, we still cannot print the AIA CSRs in the remote gdb debugger, because the dynamic CSR xml is generated when the cpu is realized.

This patch refreshes the dynamic CSR xml after we update the ext_smaia, so that we can print the AIA CSRs in the remote gdb debugger.

---
 hw/intc/riscv_imsic.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/intc/riscv_imsic.c +4 -0
index fea3385b51..97a51d535b 100644
@@ -350,6 +350,10 @@ static void riscv_imsic_realize(DeviceState *dev, Error **errp)
         } else {
             rcpu->cfg.ext_smaia = true;
         }
+
+        /* Refresh the dynamic csr xml for the gdbstub. */
+        riscv_refresh_dynamic_csr_xml(cpu);
+
         riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ? PRV_M : PRV_S,
                                       riscv_imsic_rmw, imsic);
     }
Signature · 1 line
2.38.1
#11 lore raw

Re: [PATCH 2/2] hw/intc: riscv_imsic: Refresh the CSRs xml after updating the state of the cpu.

Quoted 18 lines · Tommy Wu

Originally, when we set the ext_smaia to true, we still cannot print the AIA CSRs in the remote gdb debugger, because the dynamic CSR xml is generated when the cpu is realized.

This patch refreshes the dynamic CSR xml after we update the ext_smaia, so that we can print the AIA CSRs in the remote gdb debugger.

---
  hw/intc/riscv_imsic.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/intc/riscv_imsic.c +0 -0
index fea3385b51..97a51d535b 100644

hw/intc/riscv_imsic.c static void riscv_imsic_realize(DeviceState *dev, Error **errp) · line 356

Quoted 7 lines
hw/intc/riscv_imsic.c +4 -0
@@ -350,6 +350,10 @@ static void riscv_imsic_realize(DeviceState *dev, Error **errp)
          } else {
              rcpu->cfg.ext_smaia = true;
          }
+
+        /* Refresh the dynamic csr xml for the gdbstub. */
+        riscv_refresh_dynamic_csr_xml(cpu);
+

There is an assert in riscv_refresh_dynamic_csr_xml():

+ if (!cpu->dyn_csr_xml) {
+ g_assert_not_reached();
+ }

So I think riscv_refresh_dynamic_csr_xml() can only be called when cpu->dyn_csr_xml is true here.

Regards,

Weiwei Li

Quoted 3 lines
hw/intc/riscv_imsic.c +0 -0
          riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ? PRV_M : PRV_S,
                                        riscv_imsic_rmw, imsic);
      }
author
#12 lore raw

Re: [PATCH 2/2] hw/intc: riscv_imsic: Refresh the CSRs xml after updating the state of the cpu.

Hi Weiwei Li,
Yes, you're right, riscv_refresh_dynamic_csr_xml() can only be called when
cpu->dyn_csr_xml isn't a NULL pointer here.

The cpu->dyn_csr_xml will be set when the cpu is realized.

Best Regards,
Tommy

Quoted 48 lines · Weiwei Li

hw/intc/riscv_imsic.c static void riscv_imsic_realize(DeviceState *dev,

Quoted 18 lines · Tommy Wu

Originally, when we set the ext_smaia to true, we still cannot print the AIA CSRs in the remote gdb debugger, because the dynamic CSR xml is generated when the cpu is realized.

This patch refreshes the dynamic CSR xml after we update the ext_smaia, so that we can print the AIA CSRs in the remote gdb debugger.

---
  hw/intc/riscv_imsic.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/intc/riscv_imsic.c +0 -0
index fea3385b51..97a51d535b 100644
@@ -350,6 +350,10 @@ static void riscv_imsic_realize(DeviceState *dev,

Error **errp)

hw/intc/riscv_imsic.c static void riscv_imsic_realize(DeviceState *dev, Error **errp) · line 356

Quoted 7 lines
hw/intc/riscv_imsic.c +4 -0
          } else {
              rcpu->cfg.ext_smaia = true;
          }
+
+        /* Refresh the dynamic csr xml for the gdbstub. */
+        riscv_refresh_dynamic_csr_xml(cpu);
+

There is an assert in riscv_refresh_dynamic_csr_xml():

(quoted diff) +3 -0
+    if (!cpu->dyn_csr_xml) {
+        g_assert_not_reached();
+    }

So I think riscv_refresh_dynamic_csr_xml() can only be called when cpu->dyn_csr_xml is true here.

Regards,

Weiwei Li

hw/intc/riscv_imsic.c static void riscv_imsic_realize(DeviceState *dev, · line 357

Quoted 1 line
hw/intc/riscv_imsic.c +0 -0
          riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ? PRV_M :

PRV_S,

Quoted 2 lines
hw/intc/riscv_imsic.c +0 -0
                                        riscv_imsic_rmw, imsic);
      }
#13 lore raw

Re: [PATCH 2/2] hw/intc: riscv_imsic: Refresh the CSRs xml after updating the state of the cpu.

Quoted 5 lines · Tommy Wu

Hi Weiwei Li,
Yes, you're right,  riscv_refresh_dynamic_csr_xml()  can only be called when cpu->dyn_csr_xml isn't a NULL pointer here.

The cpu->dyn_csr_xml will be set when the cpu is realized.

Yeah, It will  be set only when Zicsr is supported.  And I think aia requires Zicsr.

However, another question:  whether we should add a check in riscv_imsic.c that it requires Zicsr?

Regards,

Weiwei Li

Quoted 56 lines

Best Regards,
Tommy

On Tue, May 23, 2023 at 10:44 PM Weiwei Li <[email protected]> wrote:

    On 2023/5/23 19:44, Tommy Wu wrote:
    > Originally, when we set the ext_smaia to true, we still cannot
    print the
    > AIA CSRs in the remote gdb debugger, because the dynamic CSR xml is
    > generated when the cpu is realized.
    >
    > This patch refreshes the dynamic CSR xml after we update the
    ext_smaia,
    > so that we can print the AIA CSRs in the remote gdb debugger.
    >
    > Signed-off-by: Tommy Wu <[email protected]>
    > Reviewed-by: Frank Chang <[email protected]>
    > ---
    >   hw/intc/riscv_imsic.c | 4 ++++
    >   1 file changed, 4 insertions(+)
    >
    > diff --git a/hw/intc/riscv_imsic.c b/hw/intc/riscv_imsic.c
    > index fea3385b51..97a51d535b 100644
    > --- a/hw/intc/riscv_imsic.c
    > +++ b/hw/intc/riscv_imsic.c
    > @@ -350,6 +350,10 @@ static void riscv_imsic_realize(DeviceState
    *dev, Error **errp)
    >           } else {
    >               rcpu->cfg.ext_smaia = true;
    >           }
    > +
    > +        /* Refresh the dynamic csr xml for the gdbstub. */
    > +        riscv_refresh_dynamic_csr_xml(cpu);
    > +

    There is an assert in riscv_refresh_dynamic_csr_xml():

    +    if (!cpu->dyn_csr_xml) {
    +        g_assert_not_reached();
    +    }

    So I think riscv_refresh_dynamic_csr_xml() can only be called when
    cpu->dyn_csr_xml is true here.

    Regards,

    Weiwei Li

    >           riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ?
    PRV_M : PRV_S,
    >                                         riscv_imsic_rmw, imsic);
    >       }
author
#14 lore raw

Re: [PATCH 2/2] hw/intc: riscv_imsic: Refresh the CSRs xml after updating the state of the cpu.

Hi Weiwei Li,
You're right, it seems that we need to add a check in riscv_imsic.c Thanks for the advice!

Best Regards,
Tommy

Quoted 77 lines · Weiwei Li
Quoted 6 lines · Tommy Wu

Hi Weiwei Li,
Yes, you're right, riscv_refresh_dynamic_csr_xml() can only be called when
cpu->dyn_csr_xml isn't a NULL pointer here.

The cpu->dyn_csr_xml will be set when the cpu is realized.

Yeah, It will be set only when Zicsr is supported. And I think aia requires Zicsr.

However, another question: whether we should add a check in riscv_imsic.c that it requires Zicsr?

Regards,

Weiwei Li

Quoted 56 lines

Best Regards,
Tommy

On Tue, May 23, 2023 at 10:44 PM Weiwei Li <[email protected]> wrote:

    On 2023/5/23 19:44, Tommy Wu wrote:
    > Originally, when we set the ext_smaia to true, we still cannot
    print the
    > AIA CSRs in the remote gdb debugger, because the dynamic CSR xml is
    > generated when the cpu is realized.
    >
    > This patch refreshes the dynamic CSR xml after we update the
    ext_smaia,
    > so that we can print the AIA CSRs in the remote gdb debugger.
    >
    > Signed-off-by: Tommy Wu <[email protected]>
    > Reviewed-by: Frank Chang <[email protected]>
    > ---
    >   hw/intc/riscv_imsic.c | 4 ++++
    >   1 file changed, 4 insertions(+)
    >
    > diff --git a/hw/intc/riscv_imsic.c b/hw/intc/riscv_imsic.c
    > index fea3385b51..97a51d535b 100644
    > --- a/hw/intc/riscv_imsic.c
    > +++ b/hw/intc/riscv_imsic.c
    > @@ -350,6 +350,10 @@ static void riscv_imsic_realize(DeviceState
    *dev, Error **errp)
    >           } else {
    >               rcpu->cfg.ext_smaia = true;
    >           }
    > +
    > +        /* Refresh the dynamic csr xml for the gdbstub. */
    > +        riscv_refresh_dynamic_csr_xml(cpu);
    > +

    There is an assert in riscv_refresh_dynamic_csr_xml():

    +    if (!cpu->dyn_csr_xml) {
    +        g_assert_not_reached();
    +    }

    So I think riscv_refresh_dynamic_csr_xml() can only be called when
    cpu->dyn_csr_xml is true here.

    Regards,

    Weiwei Li

    >           riscv_cpu_set_aia_ireg_rmw_fn(env, (imsic->mmode) ?
    PRV_M : PRV_S,
    >                                         riscv_imsic_rmw, imsic);
    >       }