[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



From: Bruno Verachten <gounthar@xxxxxxxxx>

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
Signed-off-by: Bruno Verachten <gounthar@xxxxxxxxx>
Signed-off-by: Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>
---
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(-)

diff --git a/xen/arch/riscv/extable.c b/xen/arch/riscv/extable.c
index 5b89c4278c65..c7898cac741f 100644
--- a/xen/arch/riscv/extable.c
+++ b/xen/arch/riscv/extable.c
@@ -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;
-- 
2.56.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.