diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-05-13 16:04:53 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-05-13 16:04:53 +0800 |
| commit | 73773984eee26ea92b2777b40a9dc608e1b010aa (patch) | |
| tree | 74111cd10421c185920a273b3ef9eb63224b5cec /na/arch/riscv/include | |
| parent | 40aeed31ef08fa642c936fe173af7319d9ad8b3c (diff) | |
| download | lume-73773984eee26ea92b2777b40a9dc608e1b010aa.tar.gz lume-73773984eee26ea92b2777b40a9dc608e1b010aa.tar.xz | |
feat: add exception table implementation with binary search and initialization
Diffstat (limited to 'na/arch/riscv/include')
| -rw-r--r-- | na/arch/riscv/include/arch/extable.h | 55 |
1 files changed, 55 insertions, 0 deletions
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 <lume/types.h> + +/* + * 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__ */ |
