lib: sbi: Emulate debug CSR read/write
PATCH 1/1 lib: sbi: Emulate debug CSR read/write
From: Bin Meng <[email protected]>
Trap-n-emulate debug CSR read/write, which is needed by S-mode software to implement the self-hosted debugger functionality.
Since these CSRs might be optional, use CSR detect read/write, and redirect the illegal instruction exception if unavailable.
---
lib/sbi/sbi_emulate_csr.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
lib/sbi/sbi_emulate_csr.c +36 -0
@@ -11,6 +11,7 @@#include <sbi/riscv_encoding.h> #include <sbi/sbi_bitops.h> #include <sbi/sbi_console.h> +#include <sbi/sbi_csr_detect.h> #include <sbi/sbi_emulate_csr.h> #include <sbi/sbi_error.h> #include <sbi/sbi_hart.h>
@@ -53,6 +54,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,#else bool virt = (regs->mstatus &MSTATUS_MPV ) ? TRUE : FALSE;#endif +struct sbi_trap_info trap = {0 };switch (csr_num) {case CSR_HTIMEDELTA :
@@ -146,6 +148,22 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,#undef switchcase_hpm_2#undef switchcase_hpm +#define switchcase_debug (__csr) \ + case __csr: \ + if (prev_mode == PRV_U) \ + return SBI_ENOTSUPP; \ + *csr_val = csr_read_allowed(__csr, (ulong)&trap); \ + if (trap.cause) \ + return SBI_ENOTSUPP; \ + break; + + switchcase_debug(CSR_TSELECT) + switchcase_debug(CSR_TDATA1) + switchcase_debug(CSR_TDATA2) + switchcase_debug(CSR_TDATA3) + +#undef switchcase_debug +default : ret =SBI_ENOTSUPP ;break ;
@@ -168,6 +186,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,#else bool virt = (regs->mstatus &MSTATUS_MPV ) ? TRUE : FALSE;#endif +struct sbi_trap_info trap = {0 };switch (csr_num) {case CSR_HTIMEDELTA :
@@ -184,6 +203,23 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs, ret =SBI_ENOTSUPP ;break ;#endif + +#define switchcase_debug (__csr) \ + case __csr: \ + if (prev_mode == PRV_U) \ + return SBI_ENOTSUPP; \ + csr_write_allowed(__csr, (ulong)&trap, csr_val); \ + if (trap.cause) \ + return SBI_ENOTSUPP; \ + break; + + switchcase_debug(CSR_TSELECT) + switchcase_debug(CSR_TDATA1) + switchcase_debug(CSR_TDATA2) + switchcase_debug(CSR_TDATA3) + +#undef switchcase_debug +default : ret =SBI_ENOTSUPP ;break ;
Signature · 1 line
2.25.1
PATCH 1/1 lib: sbi: Emulate debug CSR read/write
? 2021-10-29???? 21:46 +0800?Bin Meng???
Quoted 9 lines
From: Bin Meng <[email protected]>
Trap-n-emulate debug CSR read/write, which is needed by S-mode software to implement the self-hosted debugger functionality.
Since these CSRs might be optional, use CSR detect read/write, and redirect the illegal instruction exception if unavailable.
Signed-off-by Bin Meng <[email protected]>
Quoted 104 lines
---
?lib/sbi/sbi_emulate_csr.c | 36 ++++++++++++++++++++++++++++++++++++ ?1 file changed, 36 insertions(+)
lib/sbi/sbi_emulate_csr.c +0
-0@@ -11,6 +11,7 @@?#include <
sbi/riscv_encoding.h>
?#include <sbi/sbi_bitops.h>
?#include <sbi/sbi_console.h>
+#include <sbi/sbi_csr_detect.h>
?#include <sbi/sbi_emulate_csr.h>
?#include <sbi/sbi_error.h>
?#include <sbi/sbi_hart.h>lib/sbi/sbi_emulate_csr.c +0
-0@@ -53,6 +54,7 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,?#else
????????bool virt = (regs->mstatus&MSTATUS_MPV) ? TRUE : FALSE; ?#endif
+???????structsbi_trap_infotrap = {0};
?
????????switch (csr_num) {
????????caseCSR_HTIMEDELTA:lib/sbi/sbi_emulate_csr.c +0
-0@@ -146,6 +148,22 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,?#undef
switchcase_hpm_2
?#undefswitchcase_hpm
?
+#defineswitchcase_debug(__csr)?????????????????????????????????????????????? ??\
+???????case __csr:?????????????????????????????????????????????????????\
+???????????????if (prev_mode== PRV_U)?????????????????????????????????\
+???????????????????????return SBI_ENOTSUPP;????????????????????????????\
+???????????????*csr_val =csr_read_allowed(__csr, (ulong)&trap);???????\
+???????????????if (trap.cause)?????????????????????????????????????????\
+???????????????????????return SBI_ENOTSUPP;????????????????????????????\
+???????????????break;lib/sbi/sbi_emulate_csr.c +8
-0+ +???????switchcase_debug(CSR_TSELECT) +???????switchcase_debug(CSR_TDATA1) +???????switchcase_debug(CSR_TDATA2) +???????switchcase_debug(CSR_TDATA3) + +#undef switchcase_debug +????????default:
????????????????ret =SBI_ENOTSUPP;
????????????????break;lib/sbi/sbi_emulate_csr.c +0
-0@@ -168,6 +186,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,?#else
????????bool virt = (regs->mstatus&MSTATUS_MPV) ? TRUE : FALSE; ?#endif
+???????structsbi_trap_infotrap = {0};
?
????????switch (csr_num) {
????????caseCSR_HTIMEDELTA:lib/sbi/sbi_emulate_csr.c +0
-0@@ -184,6 +203,23 @@ int sbi_emulate_csr_write(int csr_num, structsbi_trap_regs *regs, ????????????????????????ret = SBI_ENOTSUPP; ????????????????break;?#endif
lib/sbi/sbi_emulate_csr.c +2
-0+ +#define
switchcase_debug(__csr)?????????????????????????????????????????????? ??\
+???????case __csr:?????????????????????????????????????????????????????\
+???????????????if (prev_mode== PRV_U)?????????????????????????????????\
+???????????????????????return SBI_ENOTSUPP;????????????????????????????\
+???????????????csr_write_allowed(__csr, (ulong)&trap, csr_val);????????\
+???????????????if (trap.cause)?????????????????????????????????????????\
+???????????????????????return SBI_ENOTSUPP;????????????????????????????\
+???????????????break;lib/sbi/sbi_emulate_csr.c +8
-0+ +???????switchcase_debug(CSR_TSELECT) +???????switchcase_debug(CSR_TDATA1) +???????switchcase_debug(CSR_TDATA2) +???????switchcase_debug(CSR_TDATA3) + +#undef switchcase_debug +????????default:
????????????????ret =SBI_ENOTSUPP;
????????????????break;Signature · 1 line
2.25.1
PATCH 1/1 lib: sbi: Emulate debug CSR read/write
Quoted 4 lines · Xiang W
? 2021-10-29???? 21:46 +0800?Bin Meng???
Quoted 2 lines
From: Bin Meng <[email protected]>
Quoted 8 lines
Quoted 7 lines
Trap-n-emulate debug CSR read/write, which is needed by S-mode software to implement the self-hosted debugger functionality.
Since these CSRs might be optional, use CSR detect read/write, and redirect the illegal instruction exception if unavailable.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Xiang W <[email protected]>
Directly exposing machine-mode CSRs via CSR emulation is not acceptable. The S-mode should only be accessing CSRs meant for S-mode. If required we should define SBI extension and use that program debug CSRs from S-mode.
Further, directly using csr_read/write_allowed() in CSR emulation is also not appropriate because these calls are slower compared to regular csr_read/write().
I suggest the following:
1) Define SBI debug extension
2) Have a patch to detect debug CSRs in hart_detect_features()
3) Implement SBI debug extension in OpenSBI
Regards,
Anup
Quoted 111 lines
Quoted 101 lines
---
lib/sbi/sbi_emulate_csr.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)lib/sbi/sbi_emulate_csr.c +2
-0@@ -11,6 +11,7 @@#include <sbi/riscv_encoding.h> #include <sbi/sbi_bitops.h> #include <sbi/sbi_console.h> +#include <sbi/sbi_csr_detect.h> #include <sbi/sbi_emulate_csr.h> #include <sbi/sbi_error.h> #include <sbi/sbi_hart.h> @@ -53,6 +54,7 @@ int sbi_emulate_csr_read(int csr_num, structsbi_trap_regs *regs, #else bool virt = (regs->mstatus & MSTATUS_MPV) ? TRUE : FALSE;#endif +struct sbi_trap_info trap = {0 };switch (csr_num) {case
CSR_HTIMEDELTA:lib/sbi/sbi_emulate_csr.c +2
-0@@ -146,6 +148,22 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,#undef switchcase_hpm_2#undef switchcase_hpm +#define switchcase_debug (__csr) \ + case
__csr: \
+ if (prev_mode==PRV_U) \
+ returnSBI_ENOTSUPP; \
+*csr_val= csr_read_allowed(__csr,
(ulong)&trap); \
+ if
(trap.cause) \
+ returnSBI_ENOTSUPP; \lib/sbi/sbi_emulate_csr.c +10
-0+break ; + + switchcase_debug(CSR_TSELECT) + switchcase_debug(CSR_TDATA1) + switchcase_debug(CSR_TDATA2) + switchcase_debug(CSR_TDATA3) + +#undef switchcase_debug +default : ret =SBI_ENOTSUPP ;break ;@@ -168,6 +186,7 @@ int sbi_emulate_csr_write(int csr_num, structsbi_trap_regs *regs, #else bool virt = (regs->mstatus & MSTATUS_MPV) ? TRUE : FALSE;#endif +struct sbi_trap_info trap = {0 };switch (csr_num) {case
CSR_HTIMEDELTA:lib/sbi/sbi_emulate_csr.c +3
-0@@ -184,6 +203,23 @@ int sbi_emulate_csr_write(int csr_num, structsbi_trap_regs *regs, ret =SBI_ENOTSUPP ;break ;#endif + +#define switchcase_debug (__csr) \ + case
__csr: \
+ if (prev_mode==PRV_U) \
+ returnSBI_ENOTSUPP; \
+ csr_write_allowed(__csr, (ulong)&trap,csr_val); \
+ if
(trap.cause) \
+ returnSBI_ENOTSUPP; \lib/sbi/sbi_emulate_csr.c +9
-1+break ; + + switchcase_debug(CSR_TSELECT) + switchcase_debug(CSR_TDATA1) + switchcase_debug(CSR_TDATA2) + switchcase_debug(CSR_TDATA3) + +#undef switchcase_debug +default : ret =SBI_ENOTSUPP ;break ; --Quoted 3 lines
2.25.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 1/1 lib: sbi: Emulate debug CSR read/write
Quoted 15 lines · Anup Patel
Quoted 4 lines · Xiang W
? 2021-10-29???? 21:46 +0800?Bin Meng???
Quoted 2 lines
From: Bin Meng <[email protected]>
Quoted 8 lines
Quoted 7 lines
Trap-n-emulate debug CSR read/write, which is needed by S-mode software to implement the self-hosted debugger functionality.
Since these CSRs might be optional, use CSR detect read/write, and redirect the illegal instruction exception if unavailable.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Xiang W <[email protected]>
Quoted 8 lines
Directly exposing machine-mode CSRs via CSR emulation is not acceptable. The S-mode should only be accessing CSRs meant for S-mode. If required we should define SBI extension and use that program debug CSRs from S-mode.
Further, directly using csr_read/write_allowed() in CSR emulation is also not appropriate because these calls are slower compared to regular csr_read/write().
I am afraid they have to be. Even if we use SBI to abstract the register programming, it still may need to use the _allowed version for the tdata1/2/3 access as these registers may not be available for a specific trigger and may cause illegal instruction for one trigger but not another.
Quoted 5 lines
I suggest the following:
1) Define SBI debug extension
2) Have a patch to detect debug CSRs inhart_detect_features()
3) Implement SBI debug extension in OpenSBI
The RISC-V debug spec is very loose (or flexible) in defining what capabilities are to be provided by the hardware, so it becomes quite complicated to provide a complete SBI mapping to what the hardware can provide. I feel the best way is to expose such to S-mode and have S-mode to directly program these debug registers.
Regards,
Bin
PATCH 1/1 lib: sbi: Emulate debug CSR read/write
Quoted 26 lines · Bin Meng
Quoted 15 lines · Anup Patel
Quoted 4 lines · Xiang W
? 2021-10-29???? 21:46 +0800?Bin Meng???
Quoted 2 lines
From: Bin Meng <[email protected]>
Quoted 8 lines
Quoted 7 lines
Trap-n-emulate debug CSR read/write, which is needed by S-mode software to implement the self-hosted debugger functionality.
Since these CSRs might be optional, use CSR detect read/write, and redirect the illegal instruction exception if unavailable.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Xiang W <[email protected]>Quoted 8 lines
Directly exposing machine-mode CSRs via CSR emulation is not acceptable. The S-mode should only be accessing CSRs meant for S-mode. If required we should define SBI extension and use that program debug CSRs from S-mode.
Further, directly using csr_read/write_allowed() in CSR emulation is also not appropriate because these calls are slower compared to regular csr_read/write().
Quoted 5 lines
I am afraid they have to be. Even if we use SBI to abstract the register programming, it still may need to use the
_allowedversion for the tdata1/2/3 access as these registers may not be available for a specific trigger and may cause illegal instruction for one trigger but not another.
Why can't we discover/know the conditions in which some of the CSRs trigger illegal instruction ?
This still seems like HACK which is trying to avoid getting into details of debug capabilities.
Quoted 7 lines
Quoted 6 lines
I suggest the following:
1) Define SBI debug extension
2) Have a patch to detect debug CSRs inhart_detect_features()
3) Implement SBI debug extension in OpenSBI
Quoted 5 lines
The RISC-V debug spec is very loose (or flexible) in defining what capabilities are to be provided by the hardware, so it becomes quite complicated to provide a complete SBI mapping to what the hardware can provide. I feel the best way is to expose such to S-mode and have S-mode to directly program these debug registers.
Flexibility and modularity is nature of RISC-V but this does not mean we should not put efforts in defining proper abstraction. The best example is SBI PMU extension where platforms have lot of flexibility in implementation but the SBI PMU tries it's best to standardize the interface.
Allowing direct access to M-mode CSRs from S-mode is not the right way to go. What if VS-mode accesses this M-mode CSRs which will now trap to M-mode ? What about nested virtualization ?
We can discover the debug capabilities in some way and present a standard SBI debug interface to S-mode. We can also ensure that SBI debug interface is virtualization friendly so that VS-mode can also happily use it running under a hypervisor.
Regards,
Anup
PATCH 1/1 lib: sbi: Emulate debug CSR read/write
Quoted 28 lines · Anup Patel
Quoted 26 lines · Bin Meng
Quoted 23 lines · Anup Patel
Quoted 4 lines · Xiang W
? 2021-10-29???? 21:46 +0800?Bin Meng???
Quoted 2 lines
From: Bin Meng <[email protected]>
Quoted 8 lines
Quoted 7 lines
Trap-n-emulate debug CSR read/write, which is needed by S-mode software to implement the self-hosted debugger functionality.
Since these CSRs might be optional, use CSR detect read/write, and redirect the illegal instruction exception if unavailable.
Signed-off-by Bin Meng <[email protected]>Reviewed-by Xiang W <[email protected]>Directly exposing machine-mode CSRs via CSR emulation is not acceptable. The S-mode should only be accessing CSRs meant for S-mode. If required we should define SBI extension and use that program debug CSRs from S-mode.
Further, directly using csr_read/write_allowed() in CSR emulation is also not appropriate because these calls are slower compared to regular csr_read/write().
Quoted 8 lines
Quoted 5 lines
I am afraid they have to be. Even if we use SBI to abstract the register programming, it still may need to use the
_allowedversion for the tdata1/2/3 access as these registers may not be available for a specific trigger and may cause illegal instruction for one trigger but not another.Why can't we discover/know the conditions in which some of the CSRs trigger illegal instruction ?
Not possible now with current latest priv & debug spec, neither on the real hardware (SiFive U54/U74). Maybe via mconfigptr in the future.
Quoted 22 lines
This still seems like HACK which is trying to avoid getting into details of debug capabilities.
Quoted 7 lines
Quoted 6 lines
I suggest the following:
1) Define SBI debug extension
2) Have a patch to detect debug CSRs inhart_detect_features()
3) Implement SBI debug extension in OpenSBIQuoted 5 lines
The RISC-V debug spec is very loose (or flexible) in defining what capabilities are to be provided by the hardware, so it becomes quite complicated to provide a complete SBI mapping to what the hardware can provide. I feel the best way is to expose such to S-mode and have S-mode to directly program these debug registers.
Flexibility and modularity is nature of RISC-V but this does not mean we should not put efforts in defining proper abstraction. The best example is SBI PMU extension where platforms have lot of flexibility in implementation but the SBI PMU tries it's best to standardize the interface.
Quoted 3 lines
Allowing direct access to M-mode CSRs from S-mode is not the right way to go. What if VS-mode accesses this M-mode CSRs which will now trap to M-mode ? What about nested virtualization ?
The breakpoint trap needs to be delegated to VS-mode anyway. This delegation should be done regardless how this is implemented.
Quoted 4 lines
We can discover the debug capabilities in some way and present a standard SBI debug interface to S-mode. We can also ensure that SBI debug interface is virtualization friendly so that VS-mode can also happily use it running under a hypervisor.
Will need some time to think about the SBI debug interface.
Regards,
Bin