xen/riscv: add csr_read_safe() helper

Accessing some CSRs may trap when the corresponding extension is not
implemented or enabled. Introduce csr_read_safe() which attempts to
read a CSR and relies on the exception table mechanism to safely recover
if the access faults.

This helper allows Xen to probe CSR availability without taking a fatal
trap and will be used for feature detection during early boot as we
can't always rely on what is in riscv,isa string in DTS.

While touching the header, reorder the include directives to follow the
usual Xen style.

Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
This commit is contained in:
Oleksii Kurochko authored and Jan Beulich committed 2026-04-09 08:24:01 +02:00
1 parent a651dabf87
commit 889c68b648
1 file changed
+34 -1
+34 -1
View File
@@ -6,8 +6,10 @@
#ifndef ASM__RISCV__CSR_H
#define ASM__RISCV__CSR_H
#include <asm/asm.h>
#include <xen/const.h>
#include <asm/asm.h>
#include <asm/extable.h>
#include <asm/riscv_encoding.h>
#ifndef __ASSEMBLER__
@@ -78,6 +80,37 @@
: "memory" ); \
})
static always_inline bool csr_read_safe(unsigned long csr,
unsigned long *val)
{
#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
asm_inline goto (
"1: csrr %[val], %[csr]\n"
ASM_EXTABLE(1b, %l[fault])
: [val] "=r" (*val)
: [csr] "i" (csr)
:
: fault );
return true;
fault:
return false;
#else
bool allowed = false;
asm_inline volatile (
"1: csrr %[val], %[csr]\n"
" li %[allowed], 1\n"
"2:\n"
ASM_EXTABLE(1b, 2b)
: [val] "=&r" (*val), [allowed] "+r" (allowed)
: [csr] "i" (csr) );
return allowed;
#endif
}
#endif /* __ASSEMBLER__ */
#endif /* ASM__RISCV__CSR_H */
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.