target/riscv: set tval in breakpoints
v3 cover target/riscv: set tval in breakpoints
Hi,
This new version has a change suggested by Richard in v2. No other changes made.
Changes from v2:
- patch 2:
- use tcg_constant_tl() instead of loading a temp and doing a
movi_tl()
- v2 link: https://lore.kernel.org/qemu-riscv/[email protected]/
Daniel Henrique Barboza (2):
target/riscv/debug: set tval=pc in breakpoint exceptions trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint
target/riscv/cpu_helper.c | 1 + target/riscv/debug.c | 3 +++ target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 3 files changed, 6 insertions(+)
Signature · 1 line
2.44.0
PATCH v3 1/2 target/riscv/debug: set tval=pc in breakpoint exceptions
We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6":
"The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire."
A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol.
Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info.
Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set during riscv_cpu_do_interrupt().
--- target/riscv/cpu_helper.c | 1 + target/riscv/debug.c | 3 +++ 2 files changed, 4 insertions(+)
target/riscv/cpu_helper.c +1 -0
@@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) tval = env->bins;break ;case RISCV_EXCP_BREAKPOINT : + tval = env->badaddr;if (cs->watchpoint_hit) { tval = cs->watchpoint_hit->hitaddr; cs->watchpoint_hit =NULL ;
target/riscv/debug.c +3 -0
@@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)if ((ctrl &TYPE2_EXEC ) && (bp->pc == pc)) {/* check U/S/M bit against current privilege level */ if ((ctrl >>3 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }
@@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)if (env->virt_enabled) {/* check VU/VS bit against current privilege level */ if ((ctrl >>23 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }else {/* check U/S/M bit against current privilege level */ if ((ctrl >>3 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v3 1/2] target/riscv/debug: set tval=pc in breakpoint exceptions
Quoted 56 lines · Daniel Henrique Barboza
We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6":
"The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire."
A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol.
Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info.
Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into
env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set duringriscv_cpu_do_interrupt().Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu_helper.c | 1 + target/riscv/debug.c | 3 +++ 2 files changed, 4 insertions(+)target/riscv/cpu_helper.c +1
-0@@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) tval = env->bins;break ;case RISCV_EXCP_BREAKPOINT : + tval = env->badaddr;if (cs->watchpoint_hit) { tval = cs->watchpoint_hit->hitaddr; cs->watchpoint_hit =NULL ;target/riscv/debug.c +1
-0@@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)if ((ctrl &TYPE2_EXEC ) && (bp->pc == pc)) {/* check U/S/M bit against current privilege level */ if ((ctrl >>3 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }@@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)if (env->virt_enabled) {/* check VU/VS bit against current privilege level */ if ((ctrl >>23 ) &BIT (env->priv)) {
target/riscv/debug.c bool riscv_cpu_debug_check_breakpoint(CPUState *cs) · line 823
Quoted 10 lines
target/riscv/debug.c +2
-0+ env->badaddr = pc;return true; } }else {/* check U/S/M bit against current privilege level */ if ((ctrl >>3 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }
Zhiwei
Re: [PATCH for-9.1 v3 1/2] target/riscv/debug: set tval=pc in breakpoint exceptions
In reply to Daniel Henrique BarbozaQuoted 18 lines · Daniel Henrique Barboza
We're not setting (s/m)tval when triggering breakpoints of type 2 (mcontrol) and 6 (mcontrol6). According to the debug spec section 5.7.12, "Match Control Type 6":
"The Privileged Spec says that breakpoint exceptions that occur on instruction fetches, loads, or stores update the tval CSR with either zero or the faulting virtual address. The faulting virtual address for an mcontrol6 trigger with action = 0 is the address being accessed and which caused that trigger to fire."
A similar text is also found in the Debug spec section 5.7.11 w.r.t. mcontrol.
Note that what we're doing ATM is not violating the spec, but it's simple enough to set mtval/stval and it makes life easier for any software that relies on this info.
Quoted 6 lines
Given that we always use action = 0, save the faulting address for the mcontrol and mcontrol6 trigger breakpoints into
env->badaddr, which is used as as scratch area for traps with address information. 'tval' is then set duringriscv_cpu_do_interrupt().Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 47 lines
--- target/riscv/cpu_helper.c | 1 + target/riscv/debug.c | 3 +++ 2 files changed, 4 insertions(+)target/riscv/cpu_helper.c +1
-0@@ -1717,6 +1717,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) tval = env->bins;break ;case RISCV_EXCP_BREAKPOINT : + tval = env->badaddr;if (cs->watchpoint_hit) { tval = cs->watchpoint_hit->hitaddr; cs->watchpoint_hit =NULL ;target/riscv/debug.c +3
-1@@ -798,6 +798,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)if ((ctrl &TYPE2_EXEC ) && (bp->pc == pc)) {/* check U/S/M bit against current privilege level */ if ((ctrl >>3 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }@@ -810,11 +811,13 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)if (env->virt_enabled) {/* check VU/VS bit against current privilege level */ if ((ctrl >>23 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } }else {/* check U/S/M bit against current privilege level */ if ((ctrl >>3 ) &BIT (env->priv)) { + env->badaddr = pc;return true; } } --2.44.0
PATCH v3 2/2 trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint
In reply to Daniel Henrique BarbozaPrivileged spec section 4.1.9 mentions:
"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)
If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."
A similar text is found for mtval in section 3.1.16.
Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.
Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during riscv_do_cpu_interrrupt().
--- target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 1 file changed, 2 insertions(+)
target/riscv/insn_trans/trans_privileged.c.inc +2 -0
@@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a) if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) { generate_exception(ctx, RISCV_EXCP_SEMIHOST); } else { + tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env, + offsetof(CPURISCVState, badaddr)); generate_exception(ctx, RISCV_EXCP_BREAKPOINT); } return true;
Signature · 1 line
2.44.0
Re: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint
Quoted 16 lines · Daniel Henrique Barboza
Privileged spec section 4.1.9 mentions:
"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)
If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."
A similar text is found for mtval in section 3.1.16.
Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.
Quoted 7 lines
Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during
riscv_do_cpu_interrrupt().Signed-off-by Daniel Henrique Barboza<[email protected]>--- target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 1 file changed, 2 insertions(+)
r~
Re: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint
In reply to Daniel Henrique BarbozaQuoted 29 lines · Daniel Henrique Barboza
Privileged spec section 4.1.9 mentions:
"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)
If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."
A similar text is found for mtval in section 3.1.16.
Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.
Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during
riscv_do_cpu_interrrupt().Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 1 file changed, 2 insertions(+)target/riscv/insn_trans/trans_privileged.c.inc +0
-0
target/riscv/insn_trans/trans_privileged.c.inc static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a) · line 66
Quoted 5 lines
target/riscv/insn_trans/trans_privileged.c.inc +2
-0@@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a) if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) { generate_exception(ctx, RISCV_EXCP_SEMIHOST); } else { + tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env, + offsetof(CPURISCVState, badaddr));
Zhiwei
Quoted 3 lines
target/riscv/insn_trans/trans_privileged.c.inc +0
-0generate_exception(ctx, RISCV_EXCP_BREAKPOINT); } return true;
Re: [PATCH for-9.1 v3 2/2] trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint
In reply to Daniel Henrique BarbozaQuoted 14 lines · Daniel Henrique Barboza
Privileged spec section 4.1.9 mentions:
"When a trap is taken into S-mode, stval is written with exception-specific information to assist software in handling the trap. (...)
If stval is written with a nonzero value when a breakpoint, address-misaligned, access-fault, or page-fault exception occurs on an instruction fetch, load, or store, then stval will contain the faulting virtual address."
A similar text is found for mtval in section 3.1.16.
Quoted 7 lines
Setting mtval/stval in this scenario is optional, but some softwares read these regs when handling ebreaks.
Write 'badaddr' in all ebreak breakpoints to write the appropriate 'tval' during
riscv_do_cpu_interrrupt().Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 21 lines
--- target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 1 file changed, 2 insertions(+)target/riscv/insn_trans/trans_privileged.c.inc +2
-1@@ -62,6 +62,8 @@ static bool trans_ebreak(DisasContext *ctx, arg_ebreak *a) if (pre == 0x01f01013 && ebreak == 0x00100073 && post == 0x40705013) { generate_exception(ctx, RISCV_EXCP_SEMIHOST); } else { + tcg_gen_st_tl(tcg_constant_tl(ebreak_addr), tcg_env, + offsetof(CPURISCVState, badaddr)); generate_exception(ctx, RISCV_EXCP_BREAKPOINT); } return true; --2.44.0
Alistair Francis
AppliedQuoted 9 lines · Daniel Henrique Barboza
Hi,
This new version has a change suggested by Richard in v2. No other changes made.
Changes from v2:
- patch 2:
- usetcg_constant_tl()instead of loading a temp and doing a
Quoted 7 lines
movi_tl()- v2 link: https://lore.kernel.org/qemu-riscv/[email protected]/
Daniel Henrique Barboza (2):
target/riscv/debug: set tval=pc in breakpoint exceptions trans_privileged.c.inc: set (m|s)tval on ebreak breakpoint
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 10 lines
target/riscv/cpu_helper.c | 1 + target/riscv/debug.c | 3 +++ target/riscv/insn_trans/trans_privileged.c.inc | 2 ++ 3 files changed, 6 insertions(+)--
2.44.0