Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/mm/extable.c |
| 4 | */ |
| 5 | |
Mark Rutland | 2e77a62 | 2021-10-19 17:02:17 +0100 | [diff] [blame] | 6 | #include <linux/bitfield.h> |
Paul Gortmaker | 0edfa83 | 2016-09-19 17:38:55 -0400 | [diff] [blame] | 7 | #include <linux/extable.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 8 | #include <linux/uaccess.h> |
| 9 | |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 10 | #include <asm/asm-extable.h> |
Mark Rutland | 2e77a62 | 2021-10-19 17:02:17 +0100 | [diff] [blame] | 11 | #include <asm/ptrace.h> |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 12 | |
| 13 | typedef bool (*ex_handler_t)(const struct exception_table_entry *, |
| 14 | struct pt_regs *); |
| 15 | |
| 16 | static inline unsigned long |
| 17 | get_ex_fixup(const struct exception_table_entry *ex) |
| 18 | { |
| 19 | return ((unsigned long)&ex->fixup + ex->fixup); |
| 20 | } |
| 21 | |
| 22 | static 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 Rutland | 2e77a62 | 2021-10-19 17:02:17 +0100 | [diff] [blame] | 29 | static 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 Rutland | 753b323 | 2021-10-19 17:02:18 +0100 | [diff] [blame] | 42 | static bool |
| 43 | ex_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 Rutland | e8c328d | 2021-10-19 17:02:14 +0100 | [diff] [blame] | 69 | bool fixup_exception(struct pt_regs *regs) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 70 | { |
Mark Rutland | 5d0e790 | 2021-10-19 17:02:15 +0100 | [diff] [blame] | 71 | const struct exception_table_entry *ex; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 72 | |
Mark Rutland | 5d0e790 | 2021-10-19 17:02:15 +0100 | [diff] [blame] | 73 | ex = search_exception_tables(instruction_pointer(regs)); |
| 74 | if (!ex) |
Mark Rutland | e8c328d | 2021-10-19 17:02:14 +0100 | [diff] [blame] | 75 | return false; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 76 | |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 77 | 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 Rutland | 2e77a62 | 2021-10-19 17:02:17 +0100 | [diff] [blame] | 82 | case EX_TYPE_UACCESS_ERR_ZERO: |
| 83 | return ex_handler_uaccess_err_zero(ex, regs); |
Mark Rutland | 753b323 | 2021-10-19 17:02:18 +0100 | [diff] [blame] | 84 | case EX_TYPE_LOAD_UNALIGNED_ZEROPAD: |
| 85 | return ex_handler_load_unaligned_zeropad(ex, regs); |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 86 | } |
Jean-Philippe Brucker | 8008342 | 2020-07-28 17:21:26 +0200 | [diff] [blame] | 87 | |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 88 | BUG(); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 89 | } |