History

LoongArch: Switch to relative exception tables

Similar to other architectures such as arm64, x86, riscv and so on, use offsets relative to the exception table entry values rather than their absolute addresses for both the exception location and the fixup. However, LoongArch label difference because it will actually produce two relocations, a pair of R_LARCH_ADD32 and R_LARCH_SUB32. Take simple code below for example: $ cat test_ex_table.S .section .text 1: nop .section __ex_table,"a" .balign 4 .long (1b - .) .previous $ loongarch64-unknown-linux-gnu-gcc -c test_ex_table.S $ loongarch64-unknown-linux-gnu-readelf -Wr test_ex_table.o Relocation section '.rela__ex_table' at offset 0x100 contains 2 entries: Offset Info Type Symbol's Value Symbol's Name + Addend 0000000000000000 0000000600000032 R_LARCH_ADD32 0000000000000000 .L1^B1 + 0 0000000000000000 0000000500000037 R_LARCH_SUB32 0000000000000000 L0^A + 0 The modpost will complain the R_LARCH_SUB32 relocation, so we need to patch modpost.c to skip this relocation for .rela__ex_table section.

Youling Tang committed 3 years ago

Commit 3d36f421 parent 508f28c6 files · +69 −18Browse files

Unified · Split

6 files
  1. arch/loongarch/include/asm/asm-extable.h +6 −6
  2. arch/loongarch/include/asm/extable.h +26 −0
  3. arch/loongarch/include/asm/uaccess.h +1 −1
  4. arch/loongarch/mm/extable.c +22 −10
  5. scripts/mod/modpost.c +13 −0
  6. scripts/sorttable.c +1 −1
arch/loongarch/include/asm/asm-extable.h+6−6
@@ -6,9 +6,9 @@ · 5 unmodified lines
6 6
7#define __ASM_EXTABLE_RAW(insn, fixup) \7#define __ASM_EXTABLE_RAW(insn, fixup) \
8 .pushsection __ex_table, "a"; \8 .pushsection __ex_table, "a"; \
9 .balign 8; \9 .balign 4; \
10 .quad (insn); \10 .long ((insn) - .); \
11 .quad (fixup); \11 .long ((fixup) - .); \
12 .popsection;12 .popsection;
13 13
14 .macro _asm_extable, insn, fixup14 .macro _asm_extable, insn, fixup
@@ -22,9 +22,9 @@ · 7 unmodified lines
22 22
23#define __ASM_EXTABLE_RAW(insn, fixup) \23#define __ASM_EXTABLE_RAW(insn, fixup) \
24 ".pushsection __ex_table, \"a\"\n" \24 ".pushsection __ex_table, \"a\"\n" \
25 ".balign 8\n" \25 ".balign 4\n" \
26 ".quad ((" insn "))\n" \26 ".long ((" insn ") - .)\n" \
27 ".quad ((" fixup "))\n" \27 ".long ((" fixup ") - .)\n" \
28 ".popsection\n"28 ".popsection\n"
29 29
30#define _ASM_EXTABLE(insn, fixup) \30#define _ASM_EXTABLE(insn, fixup) \
arch/loongarch/include/asm/extable.h+26−0
@@ -0,0 +1,26 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_LOONGARCH_EXTABLE_H
3#define _ASM_LOONGARCH_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
18struct exception_table_entry {
19 int insn, fixup;
20};
21
22#define ARCH_HAS_RELATIVE_EXTABLE
23
24bool fixup_exception(struct pt_regs *regs);
25
26#endif
arch/loongarch/include/asm/uaccess.h+1−1
@@ -15,8 +15,8 @@ · 14 unmodified lines
15#include <linux/string.h>15#include <linux/string.h>
16#include <linux/extable.h>16#include <linux/extable.h>
17#include <asm/pgtable.h>17#include <asm/pgtable.h>
18#include <asm/extable.h>
18#include <asm/asm-extable.h>19#include <asm/asm-extable.h>
19#include <asm-generic/extable.h>
20#include <asm-generic/access_ok.h>20#include <asm-generic/access_ok.h>
21 21
22extern u64 __ua_limit;22extern u64 __ua_limit;
arch/loongarch/mm/extable.c+22−10
@@ -3,20 +3,32 @@ · 2 unmodified lines
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 */4 */
5#include <linux/extable.h>5#include <linux/extable.h>
6#include <linux/spinlock.h>
7#include <asm/branch.h>
8#include <linux/uaccess.h>6#include <linux/uaccess.h>
7#include <asm/asm-extable.h>
8#include <asm/branch.h>
9
10static inline unsigned long
11get_ex_fixup(const struct exception_table_entry *ex)
12{
13 return ((unsigned long)&ex->fixup + ex->fixup);
14}
9 15
10int fixup_exception(struct pt_regs *regs)16static bool ex_handler_fixup(const struct exception_table_entry *ex,
17 struct pt_regs *regs)
11{18{
12 const struct exception_table_entry *fixup;19 regs->csr_era = get_ex_fixup(ex);
13 20
14 fixup = search_exception_tables(exception_era(regs));21 return true;
15 if (fixup) {22}
16 regs->csr_era = fixup->fixup;23
24
25bool fixup_exception(struct pt_regs *regs)
26{
27 const struct exception_table_entry *ex;
17 28
18 return 1;29 ex = search_exception_tables(exception_era(regs));
19 }30 if (!ex)
31 return false;
20 32
21 return 0;33 return ex_handler_fixup(ex, regs);
22}34}
scripts/mod/modpost.c+13−0
@@ -1523,6 +1523,14 @@ static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) · 1522 unmodified lines
1523#define R_RISCV_SUB32 391523#define R_RISCV_SUB32 39
1524#endif1524#endif
1525 1525
1526#ifndef EM_LOONGARCH
1527#define EM_LOONGARCH 258
1528#endif
1529
1530#ifndef R_LARCH_SUB32
1531#define R_LARCH_SUB32 55
1532#endif
1533
1526static void section_rela(const char *modname, struct elf_info *elf,1534static void section_rela(const char *modname, struct elf_info *elf,
1527 Elf_Shdr *sechdr)1535 Elf_Shdr *sechdr)
1528{1536{
@@ -1564,6 +1572,11 @@ static void section_rela(const char *modname, struct elf_info *elf, · 35 unmodified lines
1564 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)1572 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1565 continue;1573 continue;
1566 break;1574 break;
1575 case EM_LOONGARCH:
1576 if (!strcmp("__ex_table", fromsec) &&
1577 ELF_R_TYPE(r.r_info) == R_LARCH_SUB32)
1578 continue;
1579 break;
1567 }1580 }
1568 sym = elf->symtab_start + r_sym;1581 sym = elf->symtab_start + r_sym;
1569 /* Skip special sections */1582 /* Skip special sections */
scripts/sorttable.c+1−1
@@ -312,12 +312,12 @@ static int do_file(char const *const fname, void *addr) · 311 unmodified lines
312 case EM_PARISC:312 case EM_PARISC:
313 case EM_PPC:313 case EM_PPC:
314 case EM_PPC64:314 case EM_PPC64:
315 case EM_LOONGARCH:
315 custom_sort = sort_relative_table;316 custom_sort = sort_relative_table;
316 break;317 break;
317 case EM_ARCOMPACT:318 case EM_ARCOMPACT:
318 case EM_ARCV2:319 case EM_ARCV2:
319 case EM_ARM:320 case EM_ARM:
320 case EM_LOONGARCH:
321 case EM_MICROBLAZE:321 case EM_MICROBLAZE:
322 case EM_MIPS:322 case EM_MIPS:
323 case EM_XTENSA:323 case EM_XTENSA: