target/riscv: Expose "priv" register for GDB
v4 cover target/riscv: Expose "priv" register for GDB
This series adds a new "priv" virtual register that reports the current privilege mode. This is helpful for debugging purposes because that information is not actually available in any of the real CSRs.
The third patch in this series makes the priv virtual register writitable. I'm not entirely sure this is a good idea, so I split it out into its own patch. In particular, this change will conflict with the hypervisor extension work which assumes that the privilege mode does not change in unexpected cases.
As pointed out in a previous version of this series, GDB actually contains some support already for the accessing the privilege mode via a virtual "priv" register, including to convert the values into human readable forms:
(gdb) info reg priv
priv 0x3 prv:3 [Machine]
Changlog V4:
- Fix typo in filename
Changlog V3:
- Break patch into series
- Make priv a virtual register
Changelog V2:
- Use PRV_H and PRV_S instead of integer literals
Jonathan Behrens (3)
target/riscv: Tell gdbstub the correct number of CSRs target/riscv: Expose priv register for GDB for reads target/riscv: Make the priv register writable by GDB
configure | 4 ++-- gdb-xml/riscv-32bit-virtual.xml | 11 +++++++++++ gdb-xml/riscv-64bit-virtual.xml | 11 +++++++++++ target/riscv/gdbstub.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 4 deletions(-)
PATCH v4 1/3 target/riscv: Tell gdbstub the correct number of CSRs
If the number of registers reported to the gdbstub code does not match the number in the associated XML file, then the register numbers used by the stub may get out of sync with a remote GDB instance.
--- target/riscv/gdbstub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
target/riscv/gdbstub.c +2 -2
@@ -384,7 +384,7 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs) }gdb_register_coprocessor (cs, riscv_gdb_get_csr, riscv_gdb_set_csr, -4096 ,"riscv-32bit-csr.xml" ,0 ); +240 ,"riscv-32bit-csr.xml" ,0 );#elif defined(TARGET_RISCV64 )if (env->misa &RVF ) { gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,
@@ -392,6 +392,6 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs) }gdb_register_coprocessor (cs, riscv_gdb_get_csr, riscv_gdb_set_csr, -4096 ,"riscv-64bit-csr.xml" ,0 ); +240 ,"riscv-64bit-csr.xml" ,0 );#endif }
Signature · 1 line
2.23.0
PATCH v4 2/3 target/riscv: Expose "priv" register for GDB for reads
In reply to Jonathan BehrensThis patch enables a debugger to read the current privilege level via a virtual "priv" register. When compiled with CONFIG_USER_ONLY the register is still visible but always reports the value zero.
--- configure | 4 ++-- gdb-xml/riscv-32bit-virtual.xml | 11 +++++++++++ gdb-xml/riscv-64bit-virtual.xml | 11 +++++++++++ target/riscv/gdbstub.c | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 gdb-xml/riscv-32bit-virtual.xml create mode 100644 gdb-xml/riscv-64bit-virtual.xml
configure +2 -2
@@ -7520,13 +7520,13 @@ case "$target_name" in TARGET_BASE_ARCH=riscv TARGET_ABI_DIR=riscv mttcg=yes - gdb_xml_files="riscv-32bit-cpu.xml riscv-32bit-fpu.xml riscv-32bit-csr.xml" + gdb_xml_files="riscv-32bit-cpu.xml riscv-32bit-fpu.xml riscv-32bit-csr.xml riscv-32bit-virtual.xml" ;; riscv64) TARGET_BASE_ARCH=riscv TARGET_ABI_DIR=riscv mttcg=yes - gdb_xml_files="riscv-64bit-cpu.xml riscv-64bit-fpu.xml riscv-64bit-csr.xml" + gdb_xml_files="riscv-64bit-cpu.xml riscv-64bit-fpu.xml riscv-64bit-csr.xml riscv-64bit-virtual.xml" ;; sh4|sh4eb) TARGET_ARCH=sh4
gdb-xml/riscv-32bit-virtual.xml new file +11 -0
@@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<!-- Copyright (C) 2018-2019 Free Software Foundation, Inc. + + Copying and distribution of this file, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. --> + +<!DOCTYPE feature SYSTEM "gdb-target.dtd"> +<feature name="org.gnu.gdb.riscv.virtual"> + <reg name="priv" bitsize="32"/> +</feature>
gdb-xml/riscv-64bit-virtual.xml new file +11 -0
@@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<!-- Copyright (C) 2018-2019 Free Software Foundation, Inc. + + Copying and distribution of this file, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. --> + +<!DOCTYPE feature SYSTEM "gdb-target.dtd"> +<feature name="org.gnu.gdb.riscv.virtual"> + <reg name="priv" bitsize="64"/> +</feature>
target/riscv/gdbstub.c +23 -0
@@ -373,6 +373,23 @@ static int riscv_gdb_set_csr(CPURISCVState *env, uint8_t *mem_buf, int n)return 0 ; } +static int riscv_gdb_get_virtual (CPURISCVState *cs,uint8_t *mem_buf,int n) +{ +if (n ==0 ) { +#ifdef CONFIG_USER_ONLY +return gdb_get_regl (mem_buf,0 ); +#else +return gdb_get_regl (mem_buf, cs->priv); +#endif + } +return 0 ; +} + +static int riscv_gdb_set_virtual (CPURISCVState *cs,uint8_t *mem_buf,int n) +{ +return 0 ; +} +void riscv_cpu_register_gdb_regs_for_features (CPUState *cs) {RISCVCPU *cpu =RISCV_CPU (cs);
@@ -385,6 +402,9 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)gdb_register_coprocessor (cs, riscv_gdb_get_csr, riscv_gdb_set_csr,240 ,"riscv-32bit-csr.xml" ,0 ); + +gdb_register_coprocessor (cs, riscv_gdb_get_virtual, riscv_gdb_set_virtual, +1 ,"riscv-32bit-virtual.xml" ,0 );#elif defined(TARGET_RISCV64 )if (env->misa &RVF ) { gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,
@@ -393,5 +413,8 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)gdb_register_coprocessor (cs, riscv_gdb_get_csr, riscv_gdb_set_csr,240 ,"riscv-64bit-csr.xml" ,0 ); + +gdb_register_coprocessor (cs, riscv_gdb_get_virtual, riscv_gdb_set_virtual, +1 ,"riscv-64bit-virtual.xml" ,0 );#endif }
Signature · 1 line
2.23.0
Re: [PATCH v4 2/3] target/riscv: Expose "priv" register for GDB for reads
Quoted 6 lines · Jonathan Behrens
This patch enables a debugger to read the current privilege level via a virtual "priv" register. When compiled with
CONFIG_USER_ONLYthe register is still visible but always reports the value zero.Signed-off-by Jonathan Behrens <[email protected]>
Alistair
Quoted 113 lines
--- configure | 4 ++-- gdb-xml/riscv-32bit-virtual.xml | 11 +++++++++++ gdb-xml/riscv-64bit-virtual.xml | 11 +++++++++++ target/riscv/gdbstub.c | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 gdb-xml/riscv-32bit-virtual.xml create mode 100644 gdb-xml/riscv-64bit-virtual.xmlconfigure +2
-2@@ -7520,13 +7520,13 @@ case "$target_name" in TARGET_BASE_ARCH=riscv TARGET_ABI_DIR=riscv mttcg=yes - gdb_xml_files="riscv-32bit-cpu.xml riscv-32bit-fpu.xml riscv-32bit-csr.xml" + gdb_xml_files="riscv-32bit-cpu.xml riscv-32bit-fpu.xml riscv-32bit-csr.xml riscv-32bit-virtual.xml" ;; riscv64) TARGET_BASE_ARCH=riscv TARGET_ABI_DIR=riscv mttcg=yes - gdb_xml_files="riscv-64bit-cpu.xml riscv-64bit-fpu.xml riscv-64bit-csr.xml" + gdb_xml_files="riscv-64bit-cpu.xml riscv-64bit-fpu.xml riscv-64bit-csr.xml riscv-64bit-virtual.xml" ;; sh4|sh4eb) TARGET_ARCH=sh4gdb-xml/riscv-32bit-virtual.xml new file +11
-0@@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<!-- Copyright (C) 2018-2019 Free Software Foundation, Inc. + + Copying and distribution of this file, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. --> + +<!DOCTYPE feature SYSTEM "gdb-target.dtd"> +<feature name="org.gnu.gdb.riscv.virtual"> + <reg name="priv" bitsize="32"/> +</feature>gdb-xml/riscv-64bit-virtual.xml new file +11
-0@@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<!-- Copyright (C) 2018-2019 Free Software Foundation, Inc. + + Copying and distribution of this file, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. --> + +<!DOCTYPE feature SYSTEM "gdb-target.dtd"> +<feature name="org.gnu.gdb.riscv.virtual"> + <reg name="priv" bitsize="64"/> +</feature>target/riscv/gdbstub.c +23
-1@@ -373,6 +373,23 @@ static int riscv_gdb_set_csr(CPURISCVState *env, uint8_t *mem_buf, int n)return 0 ; } +static int riscv_gdb_get_virtual (CPURISCVState *cs,uint8_t *mem_buf,int n) +{ +if (n ==0 ) { +#ifdef CONFIG_USER_ONLY +return gdb_get_regl (mem_buf,0 ); +#else +return gdb_get_regl (mem_buf, cs->priv); +#endif + } +return 0 ; +} + +static int riscv_gdb_set_virtual (CPURISCVState *cs,uint8_t *mem_buf,int n) +{ +return 0 ; +} +void riscv_cpu_register_gdb_regs_for_features (CPUState *cs) {RISCVCPU *cpu =RISCV_CPU (cs);@@ -385,6 +402,9 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)gdb_register_coprocessor (cs, riscv_gdb_get_csr, riscv_gdb_set_csr,240 ,"riscv-32bit-csr.xml" ,0 ); + +gdb_register_coprocessor (cs, riscv_gdb_get_virtual, riscv_gdb_set_virtual, +1 ,"riscv-32bit-virtual.xml" ,0 );#elif defined(TARGET_RISCV64 )if (env->misa &RVF ) { gdb_register_coprocessor(cs, riscv_gdb_get_fpu, riscv_gdb_set_fpu,@@ -393,5 +413,8 @@ void riscv_cpu_register_gdb_regs_for_features(CPUState *cs)gdb_register_coprocessor (cs, riscv_gdb_get_csr, riscv_gdb_set_csr,240 ,"riscv-64bit-csr.xml" ,0 ); + +gdb_register_coprocessor (cs, riscv_gdb_get_virtual, riscv_gdb_set_virtual, +1 ,"riscv-64bit-virtual.xml" ,0 );#endif } --2.23.0
Re: [PATCH v4 2/3] target/riscv: Expose "priv" register for GDB for reads
In reply to Jonathan BehrensQuoted 6 lines · Jonathan Behrens
This patch enables a debugger to read the current privilege level via a virtual "priv" register. When compiled with
CONFIG_USER_ONLYthe register is still visible but always reports the value zero.Signed-off-by Jonathan Behrens <[email protected]>
Quoted 9 lines
--- configure | 4 ++-- gdb-xml/riscv-32bit-virtual.xml | 11 +++++++++++ gdb-xml/riscv-64bit-virtual.xml | 11 +++++++++++ target/riscv/gdbstub.c | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 gdb-xml/riscv-32bit-virtual.xml create mode 100644 gdb-xml/riscv-64bit-virtual.xml
PATCH v4 3/3 target/riscv: Make the priv register writable by GDB
In reply to Jonathan BehrensCurrently only PRV_U, PRV_S and PRV_M are supported, so this patch ensures that the privilege mode is set to one of them. Once support for the H-extension is added, this code will also need to properly update the virtualization status when switching between VU/VS-modes and M-mode.
--- target/riscv/gdbstub.c | 9 +++++++++ 1 file changed, 9 insertions(+)
target/riscv/gdbstub.c +9 -0
@@ -387,6 +387,15 @@ static int riscv_gdb_get_virtual(CPURISCVState *cs, uint8_t *mem_buf, int n)static int riscv_gdb_set_virtual (CPURISCVState *cs,uint8_t *mem_buf,int n) { +if (n ==0 ) { +#ifndef CONFIG_USER_ONLY + cs->priv =ldtul_p (mem_buf) &0x3 ; +if (cs->priv ==PRV_H ) { + cs->priv =PRV_S ; + } +#endif +return sizeof (target_ulong); + }return 0 ; }
Signature · 1 line
2.23.0
Quoted 32 lines · On Mon, 14 Oct 2019 08:45:26 PDT (-0700), [email protected] wrote:
This series adds a new "priv" virtual register that reports the current privilege mode. This is helpful for debugging purposes because that information is not actually available in any of the real CSRs.
The third patch in this series makes the priv virtual register writitable. I'm not entirely sure this is a good idea, so I split it out into its own patch. In particular, this change will conflict with the hypervisor extension work which assumes that the privilege mode does not change in unexpected cases.
As pointed out in a previous version of this series, GDB actually contains some support already for the accessing the privilege mode via a virtual "priv" register, including to convert the values into human readable forms:
(gdb) info reg priv
priv0x3prv:3 [Machine]Changlog V4:
- Fix typo in filenameChanglog V3:
- Break patch into series
- Make priv a virtual registerChangelog V2:
- UsePRV_HandPRV_Sinstead of integer literalsJonathan Behrens (3)
target/riscv: Tell gdbstub the correct number of CSRs target/riscv: Expose priv register for GDB for reads target/riscv: Make the priv register writable by GDB
Quoted 5 lines
configure | 4 ++-- gdb-xml/riscv-32bit-virtual.xml | 11 +++++++++++ gdb-xml/riscv-64bit-virtual.xml | 11 +++++++++++ target/riscv/gdbstub.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 4 deletions(-)
Thanks. I've taken these into my patch queue, which I hope to submit soon!