Skip to contentSkip to conversation

xen/riscv: look up exception table entries by faulting address

PATCH In review Oleksii Kurochko 2 participants 2 messages Dec 3 – Dec 4 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/riscv: look up exception table entries by faulting address

From: Bruno Verachten <[email protected]>

fixup_exception() searched the exception table with a key built on its own stack: key.insn = pc - &key.insn, the distance from the key.insn field to pc. key.insn is an int32_t, so this only works while the stack is within 2 GiB of Xen's text. Today that holds, as Xen runs on cpu0_boot_stack, which is part of the Xen image, and the existing users (csr_read_safe() and the A/D update probe) run at boot.

It stops holding once Xen runs on vCPU stacks: these are allocated with vzalloc() in arch_vcpu_create() and sit in the VMAP area, far more than 2 GiB away from Xen's text. The distance is then truncated, the address the key decodes to lands next to the stack rather than in Xen's text, and bsearch() misses the entry, so a fault that is meant to be recovered from reaches the BUG_ON() or do_unexpected_trap() in do_trap() and stops Xen.

Pass the faulting address itself as the key, with a comparator that takes it as such. The entries keep their relative encoding.

Fixes a651dabf873d ("xen/riscv: add exception table support")
Assisted-by Claude:claude-opus-5-5

---
I took the patch from [1] with some minimal changes which could be found in discussion from [1].

[1] https://gitlab.com/xen-project/people/olkur/xen/-/merge_requests/39
---

---
 xen/arch/riscv/extable.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
xen/arch/riscv/extable.c +16 -5
index 5b89c4278c65..c7898cac741f 100644
@@ -42,6 +42,20 @@ static int cf_check cmp_ex(const void *a, const void *b)
     return (insn_a > insn_b) - (insn_a < insn_b);
 }
 
+/*
+ * bsearch() comparator looking up an exception table entry by address.
+ *
+ * pc_ptr - pointer to the faulting address
+ * ex - exception table entry to compare it with
+ */
+static int cf_check cmp_ex_addr(const void *pc_ptr, const void *ex)
+{
+    const unsigned long pc = *(const unsigned long *)pc_ptr;
+    const unsigned long insn = ex_insn(ex);
+
+    return (pc > insn) - (pc < insn);
+}
+
 void init_or_livepatch sort_exception_table(struct exception_table_entry *start,
                                  const struct exception_table_entry *stop)
 {
@@ -64,15 +78,12 @@ bool fixup_exception(struct cpu_user_regs *regs)
     unsigned long pc = regs->sepc;
     const struct virtual_region *region = find_text_region(pc);
     const struct exception_table_entry *ex;
-    struct exception_table_entry key;
 
     if ( !region || !region->ex )
         return false;
 
-    key.insn = pc - (unsigned long)&key.insn;
-
-    ex = bsearch(&key, region->ex, region->ex_end - region->ex, sizeof(key),
-                 cmp_ex);
+    ex = bsearch(&pc, region->ex, region->ex_end - region->ex, sizeof(*ex),
+                 cmp_ex_addr);
 
     if ( !ex )
         return false;
Signature · 1 line
2.56.1
#1 lore raw
Quoted 15 lines

fixup_exception() searched the exception table with a key built on its own stack: key.insn = pc - &key.insn, the distance from the key.insn field to pc. key.insn is an int32_t, so this only works while the stack is within 2 GiB of Xen's text. Today that holds, as Xen runs on cpu0_boot_stack, which is part of the Xen image, and the existing users (csr_read_safe() and the A/D update probe) run at boot.

It stops holding once Xen runs on vCPU stacks: these are allocated with vzalloc() in arch_vcpu_create() and sit in the VMAP area, far more than 2 GiB away from Xen's text. The distance is then truncated, the address the key decodes to lands next to the stack rather than in Xen's text, and bsearch() misses the entry, so a fault that is meant to be recovered from reaches the BUG_ON() or do_unexpected_trap() in do_trap() and stops Xen.

Quoted 7 lines

Pass the faulting address itself as the key, with a comparator that takes it as such. The entries keep their relative encoding.

Fixes a651dabf873d ("xen/riscv: add exception table support")
Assisted-by Claude:claude-opus-5-5

I think you are more like a Co-developer of this patches rather than a submitter based on our internal review. A Co-developer trailer + your Signed-off-by would fit better imo.

xen/arch/riscv/extable.c static int cf_check cmp_ex(const void *a, const void *b) · line 49

Quoted 12 lines

[ ... 12 lines skipped ... ]

xen/arch/riscv/extable.c +5 -0
@@ -42,6 +42,20 @@ static int cf_check cmp_ex(const void *a, const void *b)
     return (insn_a > insn_b) - (insn_a < insn_b);
 }
 
+/*
+ * bsearch() comparator looking up an exception table entry by address.
+ *
+ * pc_ptr - pointer to the faulting address
+ * ex - exception table entry to compare it with

Arguments in Xen comments follow the kernel-doc form, "@name:
description" (e.g. asm/bitops.h). The "name - description" form is only used for the function name on the first line. That first line is also over 80 columns. Something like:

/*
* cmp_ex_addr - bsearch() comparator finding an extable entry by address
* @pc_ptr: pointer to the faulting address
* @ex: exception table entry to compare it with
*/

would fit better imo.

Signature · 1 line
Baptiste Le Duc <[email protected]>