Skip to content

Commit 2bf847d

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: extable: add type and data fields
This is a riscv port of commit d6e2cc5 ("arm64: extable: add `type` and `data` fields"). Signed-off-by: Jisheng Zhang <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 6dd10d9 commit 2bf847d

File tree

6 files changed

+57
-19
lines changed

6 files changed

+57
-19
lines changed

arch/riscv/include/asm/asm-extable.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,40 @@
22
#ifndef __ASM_ASM_EXTABLE_H
33
#define __ASM_ASM_EXTABLE_H
44

5+
#define EX_TYPE_NONE 0
6+
#define EX_TYPE_FIXUP 1
7+
#define EX_TYPE_BPF 2
8+
59
#ifdef __ASSEMBLY__
610

7-
#define __ASM_EXTABLE_RAW(insn, fixup) \
8-
.pushsection __ex_table, "a"; \
9-
.balign 4; \
10-
.long ((insn) - .); \
11-
.long ((fixup) - .); \
11+
#define __ASM_EXTABLE_RAW(insn, fixup, type, data) \
12+
.pushsection __ex_table, "a"; \
13+
.balign 4; \
14+
.long ((insn) - .); \
15+
.long ((fixup) - .); \
16+
.short (type); \
17+
.short (data); \
1218
.popsection;
1319

1420
.macro _asm_extable, insn, fixup
15-
__ASM_EXTABLE_RAW(\insn, \fixup)
21+
__ASM_EXTABLE_RAW(\insn, \fixup, EX_TYPE_FIXUP, 0)
1622
.endm
1723

1824
#else /* __ASSEMBLY__ */
1925

2026
#include <linux/stringify.h>
2127

22-
#define __ASM_EXTABLE_RAW(insn, fixup) \
28+
#define __ASM_EXTABLE_RAW(insn, fixup, type, data) \
2329
".pushsection __ex_table, \"a\"\n" \
2430
".balign 4\n" \
2531
".long ((" insn ") - .)\n" \
2632
".long ((" fixup ") - .)\n" \
33+
".short (" type ")\n" \
34+
".short (" data ")\n" \
2735
".popsection\n"
2836

29-
#define _ASM_EXTABLE(insn, fixup) __ASM_EXTABLE_RAW(#insn, #fixup)
37+
#define _ASM_EXTABLE(insn, fixup) \
38+
__ASM_EXTABLE_RAW(#insn, #fixup, __stringify(EX_TYPE_FIXUP), "0")
3039

3140
#endif /* __ASSEMBLY__ */
3241

arch/riscv/include/asm/extable.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,29 @@
1717

1818
struct exception_table_entry {
1919
int insn, fixup;
20+
short type, data;
2021
};
2122

2223
#define ARCH_HAS_RELATIVE_EXTABLE
2324

25+
#define swap_ex_entry_fixup(a, b, tmp, delta) \
26+
do { \
27+
(a)->fixup = (b)->fixup + (delta); \
28+
(b)->fixup = (tmp).fixup - (delta); \
29+
(a)->type = (b)->type; \
30+
(b)->type = (tmp).type; \
31+
(a)->data = (b)->data; \
32+
(b)->data = (tmp).data; \
33+
} while (0)
34+
2435
bool fixup_exception(struct pt_regs *regs);
2536

2637
#if defined(CONFIG_BPF_JIT) && defined(CONFIG_ARCH_RV64I)
27-
bool rv_bpf_fixup_exception(const struct exception_table_entry *ex, struct pt_regs *regs);
38+
bool ex_handler_bpf(const struct exception_table_entry *ex, struct pt_regs *regs);
2839
#else
2940
static inline bool
30-
rv_bpf_fixup_exception(const struct exception_table_entry *ex,
31-
struct pt_regs *regs)
41+
ex_handler_bpf(const struct exception_table_entry *ex,
42+
struct pt_regs *regs)
3243
{
3344
return false;
3445
}

arch/riscv/kernel/vmlinux.lds.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2017 SiFive
55
*/
66

7-
#define RO_EXCEPTION_TABLE_ALIGN 16
7+
#define RO_EXCEPTION_TABLE_ALIGN 4
88

99
#ifdef CONFIG_XIP_KERNEL
1010
#include "vmlinux-xip.lds.S"

arch/riscv/mm/extable.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
#include <linux/extable.h>
1111
#include <linux/module.h>
1212
#include <linux/uaccess.h>
13+
#include <asm/asm-extable.h>
14+
15+
static inline unsigned long
16+
get_ex_fixup(const struct exception_table_entry *ex)
17+
{
18+
return ((unsigned long)&ex->fixup + ex->fixup);
19+
}
20+
21+
static bool ex_handler_fixup(const struct exception_table_entry *ex,
22+
struct pt_regs *regs)
23+
{
24+
regs->epc = get_ex_fixup(ex);
25+
return true;
26+
}
1327

1428
bool fixup_exception(struct pt_regs *regs)
1529
{
@@ -19,9 +33,12 @@ bool fixup_exception(struct pt_regs *regs)
1933
if (!ex)
2034
return false;
2135

22-
if (regs->epc >= BPF_JIT_REGION_START && regs->epc < BPF_JIT_REGION_END)
23-
return rv_bpf_fixup_exception(ex, regs);
36+
switch (ex->type) {
37+
case EX_TYPE_FIXUP:
38+
return ex_handler_fixup(ex, regs);
39+
case EX_TYPE_BPF:
40+
return ex_handler_bpf(ex, regs);
41+
}
2442

25-
regs->epc = (unsigned long)&ex->fixup + ex->fixup;
26-
return true;
43+
BUG();
2744
}

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ static int emit_call(bool fixed, u64 addr, struct rv_jit_context *ctx)
459459
#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
460460
#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
461461

462-
bool rv_bpf_fixup_exception(const struct exception_table_entry *ex,
463-
struct pt_regs *regs)
462+
bool ex_handler_bpf(const struct exception_table_entry *ex,
463+
struct pt_regs *regs)
464464
{
465465
off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
466466
int regs_offset = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
@@ -514,6 +514,7 @@ static int add_exception_handler(const struct bpf_insn *insn,
514514

515515
ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
516516
FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
517+
ex->type = EX_TYPE_BPF;
517518

518519
ctx->nexentries++;
519520
return 0;

scripts/sorttable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ static int do_file(char const *const fname, void *addr)
337337
switch (r2(&ehdr->e_machine)) {
338338
case EM_386:
339339
case EM_AARCH64:
340+
case EM_RISCV:
340341
case EM_X86_64:
341342
custom_sort = sort_relative_table_with_data;
342343
break;
@@ -346,7 +347,6 @@ static int do_file(char const *const fname, void *addr)
346347
case EM_PARISC:
347348
case EM_PPC:
348349
case EM_PPC64:
349-
case EM_RISCV:
350350
custom_sort = sort_relative_table;
351351
break;
352352
case EM_ARCOMPACT:

0 commit comments

Comments
 (0)