From 73773984eee26ea92b2777b40a9dc608e1b010aa Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Wed, 13 May 2026 16:04:53 +0800 Subject: feat: add exception table implementation with binary search and initialization --- na/arch/riscv/include/arch/extable.h | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 na/arch/riscv/include/arch/extable.h (limited to 'na/arch/riscv/include') diff --git a/na/arch/riscv/include/arch/extable.h b/na/arch/riscv/include/arch/extable.h new file mode 100644 index 0000000..eb2daac --- /dev/null +++ b/na/arch/riscv/include/arch/extable.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +#ifndef __ASSEMBLER__ + +#include + +/* + * Exception table entry. + * insn: offset from this struct to the faulting instruction. + * fixup: offset from this struct to the fixup code. + * + * Using relative offsets avoids absolute relocations and saves space. + */ +struct ExceptionTableEntry { + int32 insn; + int32 fixup; +}; + +/* Search the exception table for a given PC. Returns fixup address, or 0 if not found. */ +uint64 search_extable(uint64 pc); + +/* Sorts the exception table at boot for binary search O(log N). */ +void extable_init(); + +#endif /* __ASSEMBLER__ */ + +/* + * EXTABLE macro for assembly usage. + * Appends an entry to the .extable section. + */ +#ifdef __ASSEMBLER__ + +#define EXTABLE(insn, fixup) \ + .pushsection .extable, "a"; \ + .balign 4; \ + .long insn - .; \ + .long fixup - .; \ + .popsection + +#else /* !__ASSEMBLER__ */ + +/* Stringified macro for inline assembly */ +#define _ASM_EXTABLE(insn, fixup) \ + ".pushsection .extable, \"a\"\n" \ + ".balign 4\n" \ + ".long " #insn " - .\n" \ + ".long " #fixup " - .\n" \ + ".popsection\n" + +#define EXTABLE(insn, fixup) _ASM_EXTABLE(insn, fixup) + +#endif /* __ASSEMBLER__ */ -- cgit v1.2.3