Skip to content

Commit 1f77ed9

Browse files
riscv: switch to relative extable and other improvements
Similar as other architectures such as arm64, x86 and so on, use offsets relative to the exception table entry values rather than absolute addresses for both the exception locationand the fixup. And recently, arm64 and x86 remove anonymous out-of-line fixups, we want to acchieve the same result.
2 parents dacef01 + a2ceb8c commit 1f77ed9

File tree

13 files changed

+309
-235
lines changed

13 files changed

+309
-235
lines changed

arch/riscv/include/asm/Kbuild

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
22
generic-y += early_ioremap.h
3-
generic-y += extable.h
43
generic-y += flat.h
54
generic-y += kvm_para.h
65
generic-y += user.h

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef __ASM_ASM_EXTABLE_H
3+
#define __ASM_ASM_EXTABLE_H
4+
5+
#define EX_TYPE_NONE 0
6+
#define EX_TYPE_FIXUP 1
7+
#define EX_TYPE_BPF 2
8+
#define EX_TYPE_UACCESS_ERR_ZERO 3
9+
10+
#ifdef __ASSEMBLY__
11+
12+
#define __ASM_EXTABLE_RAW(insn, fixup, type, data) \
13+
.pushsection __ex_table, "a"; \
14+
.balign 4; \
15+
.long ((insn) - .); \
16+
.long ((fixup) - .); \
17+
.short (type); \
18+
.short (data); \
19+
.popsection;
20+
21+
.macro _asm_extable, insn, fixup
22+
__ASM_EXTABLE_RAW(\insn, \fixup, EX_TYPE_FIXUP, 0)
23+
.endm
24+
25+
#else /* __ASSEMBLY__ */
26+
27+
#include <linux/bits.h>
28+
#include <linux/stringify.h>
29+
#include <asm/gpr-num.h>
30+
31+
#define __ASM_EXTABLE_RAW(insn, fixup, type, data) \
32+
".pushsection __ex_table, \"a\"\n" \
33+
".balign 4\n" \
34+
".long ((" insn ") - .)\n" \
35+
".long ((" fixup ") - .)\n" \
36+
".short (" type ")\n" \
37+
".short (" data ")\n" \
38+
".popsection\n"
39+
40+
#define _ASM_EXTABLE(insn, fixup) \
41+
__ASM_EXTABLE_RAW(#insn, #fixup, __stringify(EX_TYPE_FIXUP), "0")
42+
43+
#define EX_DATA_REG_ERR_SHIFT 0
44+
#define EX_DATA_REG_ERR GENMASK(4, 0)
45+
#define EX_DATA_REG_ZERO_SHIFT 5
46+
#define EX_DATA_REG_ZERO GENMASK(9, 5)
47+
48+
#define EX_DATA_REG(reg, gpr) \
49+
"((.L__gpr_num_" #gpr ") << " __stringify(EX_DATA_REG_##reg##_SHIFT) ")"
50+
51+
#define _ASM_EXTABLE_UACCESS_ERR_ZERO(insn, fixup, err, zero) \
52+
__DEFINE_ASM_GPR_NUMS \
53+
__ASM_EXTABLE_RAW(#insn, #fixup, \
54+
__stringify(EX_TYPE_UACCESS_ERR_ZERO), \
55+
"(" \
56+
EX_DATA_REG(ERR, err) " | " \
57+
EX_DATA_REG(ZERO, zero) \
58+
")")
59+
60+
#define _ASM_EXTABLE_UACCESS_ERR(insn, fixup, err) \
61+
_ASM_EXTABLE_UACCESS_ERR_ZERO(insn, fixup, err, zero)
62+
63+
#endif /* __ASSEMBLY__ */
64+
65+
#endif /* __ASM_ASM_EXTABLE_H */

arch/riscv/include/asm/extable.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_RISCV_EXTABLE_H
3+
#define _ASM_RISCV_EXTABLE_H
4+
5+
/*
6+
* The exception table consists of pairs of relative offsets: the first
7+
* is the relative offset to an instruction that is allowed to fault,
8+
* and the second is the relative offset at which the program should
9+
* continue. No registers are modified, so it is entirely up to the
10+
* continuation code to figure out what to do.
11+
*
12+
* All the routines below use bits of fixup code that are out of line
13+
* with the main instruction path. This means when everything is well,
14+
* we don't even have to jump over them. Further, they do not intrude
15+
* on our cache or tlb entries.
16+
*/
17+
18+
struct exception_table_entry {
19+
int insn, fixup;
20+
short type, data;
21+
};
22+
23+
#define ARCH_HAS_RELATIVE_EXTABLE
24+
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+
35+
bool fixup_exception(struct pt_regs *regs);
36+
37+
#if defined(CONFIG_BPF_JIT) && defined(CONFIG_ARCH_RV64I)
38+
bool ex_handler_bpf(const struct exception_table_entry *ex, struct pt_regs *regs);
39+
#else
40+
static inline bool
41+
ex_handler_bpf(const struct exception_table_entry *ex,
42+
struct pt_regs *regs)
43+
{
44+
return false;
45+
}
46+
#endif
47+
48+
#endif

arch/riscv/include/asm/futex.h

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/uaccess.h>
1212
#include <linux/errno.h>
1313
#include <asm/asm.h>
14+
#include <asm/asm-extable.h>
1415

1516
/* We don't even really need the extable code, but for now keep it simple */
1617
#ifndef CONFIG_MMU
@@ -20,23 +21,14 @@
2021

2122
#define __futex_atomic_op(insn, ret, oldval, uaddr, oparg) \
2223
{ \
23-
uintptr_t tmp; \
2424
__enable_user_access(); \
2525
__asm__ __volatile__ ( \
2626
"1: " insn " \n" \
2727
"2: \n" \
28-
" .section .fixup,\"ax\" \n" \
29-
" .balign 4 \n" \
30-
"3: li %[r],%[e] \n" \
31-
" jump 2b,%[t] \n" \
32-
" .previous \n" \
33-
" .section __ex_table,\"a\" \n" \
34-
" .balign " RISCV_SZPTR " \n" \
35-
" " RISCV_PTR " 1b, 3b \n" \
36-
" .previous \n" \
28+
_ASM_EXTABLE_UACCESS_ERR(1b, 2b, %[r]) \
3729
: [r] "+r" (ret), [ov] "=&r" (oldval), \
38-
[u] "+m" (*uaddr), [t] "=&r" (tmp) \
39-
: [op] "Jr" (oparg), [e] "i" (-EFAULT) \
30+
[u] "+m" (*uaddr) \
31+
: [op] "Jr" (oparg) \
4032
: "memory"); \
4133
__disable_user_access(); \
4234
}
@@ -98,18 +90,10 @@ futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
9890
"2: sc.w.aqrl %[t],%z[nv],%[u] \n"
9991
" bnez %[t],1b \n"
10092
"3: \n"
101-
" .section .fixup,\"ax\" \n"
102-
" .balign 4 \n"
103-
"4: li %[r],%[e] \n"
104-
" jump 3b,%[t] \n"
105-
" .previous \n"
106-
" .section __ex_table,\"a\" \n"
107-
" .balign " RISCV_SZPTR " \n"
108-
" " RISCV_PTR " 1b, 4b \n"
109-
" " RISCV_PTR " 2b, 4b \n"
110-
" .previous \n"
93+
_ASM_EXTABLE_UACCESS_ERR(1b, 3b, %[r]) \
94+
_ASM_EXTABLE_UACCESS_ERR(2b, 3b, %[r]) \
11195
: [r] "+r" (ret), [v] "=&r" (val), [u] "+m" (*uaddr), [t] "=&r" (tmp)
112-
: [ov] "Jr" (oldval), [nv] "Jr" (newval), [e] "i" (-EFAULT)
96+
: [ov] "Jr" (oldval), [nv] "Jr" (newval)
11397
: "memory");
11498
__disable_user_access();
11599

arch/riscv/include/asm/gpr-num.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef __ASM_GPR_NUM_H
3+
#define __ASM_GPR_NUM_H
4+
5+
#ifdef __ASSEMBLY__
6+
.equ .L__gpr_num_zero, 0
7+
.equ .L__gpr_num_ra, 1
8+
.equ .L__gpr_num_sp, 2
9+
.equ .L__gpr_num_gp, 3
10+
.equ .L__gpr_num_tp, 4
11+
.equ .L__gpr_num_t0, 5
12+
.equ .L__gpr_num_t1, 6
13+
.equ .L__gpr_num_t2, 7
14+
.equ .L__gpr_num_s0, 8
15+
.equ .L__gpr_num_s1, 9
16+
.equ .L__gpr_num_a0, 10
17+
.equ .L__gpr_num_a1, 11
18+
.equ .L__gpr_num_a2, 12
19+
.equ .L__gpr_num_a3, 13
20+
.equ .L__gpr_num_a4, 14
21+
.equ .L__gpr_num_a5, 15
22+
.equ .L__gpr_num_a6, 16
23+
.equ .L__gpr_num_a7, 17
24+
.equ .L__gpr_num_s2, 18
25+
.equ .L__gpr_num_s3, 19
26+
.equ .L__gpr_num_s4, 20
27+
.equ .L__gpr_num_s5, 21
28+
.equ .L__gpr_num_s6, 22
29+
.equ .L__gpr_num_s7, 23
30+
.equ .L__gpr_num_s8, 24
31+
.equ .L__gpr_num_s9, 25
32+
.equ .L__gpr_num_s10, 26
33+
.equ .L__gpr_num_s11, 27
34+
.equ .L__gpr_num_t3, 28
35+
.equ .L__gpr_num_t4, 29
36+
.equ .L__gpr_num_t5, 30
37+
.equ .L__gpr_num_t6, 31
38+
39+
#else /* __ASSEMBLY__ */
40+
41+
#define __DEFINE_ASM_GPR_NUMS \
42+
" .equ .L__gpr_num_zero, 0\n" \
43+
" .equ .L__gpr_num_ra, 1\n" \
44+
" .equ .L__gpr_num_sp, 2\n" \
45+
" .equ .L__gpr_num_gp, 3\n" \
46+
" .equ .L__gpr_num_tp, 4\n" \
47+
" .equ .L__gpr_num_t0, 5\n" \
48+
" .equ .L__gpr_num_t1, 6\n" \
49+
" .equ .L__gpr_num_t2, 7\n" \
50+
" .equ .L__gpr_num_s0, 8\n" \
51+
" .equ .L__gpr_num_s1, 9\n" \
52+
" .equ .L__gpr_num_a0, 10\n" \
53+
" .equ .L__gpr_num_a1, 11\n" \
54+
" .equ .L__gpr_num_a2, 12\n" \
55+
" .equ .L__gpr_num_a3, 13\n" \
56+
" .equ .L__gpr_num_a4, 14\n" \
57+
" .equ .L__gpr_num_a5, 15\n" \
58+
" .equ .L__gpr_num_a6, 16\n" \
59+
" .equ .L__gpr_num_a7, 17\n" \
60+
" .equ .L__gpr_num_s2, 18\n" \
61+
" .equ .L__gpr_num_s3, 19\n" \
62+
" .equ .L__gpr_num_s4, 20\n" \
63+
" .equ .L__gpr_num_s5, 21\n" \
64+
" .equ .L__gpr_num_s6, 22\n" \
65+
" .equ .L__gpr_num_s7, 23\n" \
66+
" .equ .L__gpr_num_s8, 24\n" \
67+
" .equ .L__gpr_num_s9, 25\n" \
68+
" .equ .L__gpr_num_s10, 26\n" \
69+
" .equ .L__gpr_num_s11, 27\n" \
70+
" .equ .L__gpr_num_t3, 28\n" \
71+
" .equ .L__gpr_num_t4, 29\n" \
72+
" .equ .L__gpr_num_t5, 30\n" \
73+
" .equ .L__gpr_num_t6, 31\n"
74+
75+
#endif /* __ASSEMBLY__ */
76+
77+
#endif /* __ASM_GPR_NUM_H */

0 commit comments

Comments
 (0)