blob: c3d53811a15e1188d29308fd10878e2710f08bca [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Catalin Marinas1d18c472012-03-05 11:49:27 +00002/*
3 * Based on arch/arm/mm/extable.c
4 */
5
Mark Rutland2e77a622021-10-19 17:02:17 +01006#include <linux/bitfield.h>
Paul Gortmaker0edfa832016-09-19 17:38:55 -04007#include <linux/extable.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +00008#include <linux/uaccess.h>
9
Mark Rutlandd6e2cc52021-10-19 17:02:16 +010010#include <asm/asm-extable.h>
Mark Rutland2e77a622021-10-19 17:02:17 +010011#include <asm/ptrace.h>
Mark Rutlandd6e2cc52021-10-19 17:02:16 +010012
13typedef bool (*ex_handler_t)(const struct exception_table_entry *,
14 struct pt_regs *);
15
16static inline unsigned long
17get_ex_fixup(const struct exception_table_entry *ex)
18{
19 return ((unsigned long)&ex->fixup + ex->fixup);
20}
21
22static bool ex_handler_fixup(const struct exception_table_entry *ex,
23 struct pt_regs *regs)
24{
25 regs->pc = get_ex_fixup(ex);
26 return true;
27}
28
Mark Rutland2e77a622021-10-19 17:02:17 +010029static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
30 struct pt_regs *regs)
31{
32 int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
33 int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);
34
35 pt_regs_write_reg(regs, reg_err, -EFAULT);
36 pt_regs_write_reg(regs, reg_zero, 0);
37
38 regs->pc = get_ex_fixup(ex);
39 return true;
40}
41
Mark Rutland753b3232021-10-19 17:02:18 +010042static bool
43ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex,
44 struct pt_regs *regs)
45{
46 int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->type);
47 int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->type);
48 unsigned long data, addr, offset;
49
50 addr = pt_regs_read_reg(regs, reg_addr);
51
52 offset = addr & 0x7UL;
53 addr &= ~0x7UL;
54
55 data = *(unsigned long*)addr;
56
57#ifndef __AARCH64EB__
58 data >>= 8 * offset;
59#else
60 data <<= 8 * offset;
61#endif
62
63 pt_regs_write_reg(regs, reg_data, data);
64
65 regs->pc = get_ex_fixup(ex);
66 return true;
67}
68
Mark Rutlande8c328d2021-10-19 17:02:14 +010069bool fixup_exception(struct pt_regs *regs)
Catalin Marinas1d18c472012-03-05 11:49:27 +000070{
Mark Rutland5d0e7902021-10-19 17:02:15 +010071 const struct exception_table_entry *ex;
Catalin Marinas1d18c472012-03-05 11:49:27 +000072
Mark Rutland5d0e7902021-10-19 17:02:15 +010073 ex = search_exception_tables(instruction_pointer(regs));
74 if (!ex)
Mark Rutlande8c328d2021-10-19 17:02:14 +010075 return false;
Catalin Marinas1d18c472012-03-05 11:49:27 +000076
Mark Rutlandd6e2cc52021-10-19 17:02:16 +010077 switch (ex->type) {
78 case EX_TYPE_FIXUP:
79 return ex_handler_fixup(ex, regs);
80 case EX_TYPE_BPF:
81 return ex_handler_bpf(ex, regs);
Mark Rutland2e77a622021-10-19 17:02:17 +010082 case EX_TYPE_UACCESS_ERR_ZERO:
83 return ex_handler_uaccess_err_zero(ex, regs);
Mark Rutland753b3232021-10-19 17:02:18 +010084 case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
85 return ex_handler_load_unaligned_zeropad(ex, regs);
Mark Rutlandd6e2cc52021-10-19 17:02:16 +010086 }
Jean-Philippe Brucker80083422020-07-28 17:21:26 +020087
Mark Rutlandd6e2cc52021-10-19 17:02:16 +010088 BUG();
Catalin Marinas1d18c472012-03-05 11:49:27 +000089}