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 | |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 13 | static inline unsigned long |
| 14 | get_ex_fixup(const struct exception_table_entry *ex) |
| 15 | { |
| 16 | return ((unsigned long)&ex->fixup + ex->fixup); |
| 17 | } |
| 18 | |
| 19 | static bool ex_handler_fixup(const struct exception_table_entry *ex, |
| 20 | struct pt_regs *regs) |
| 21 | { |
| 22 | regs->pc = get_ex_fixup(ex); |
| 23 | return true; |
| 24 | } |
| 25 | |
Mark Rutland | 2e77a62 | 2021-10-19 17:02:17 +0100 | [diff] [blame] | 26 | static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex, |
| 27 | struct pt_regs *regs) |
| 28 | { |
| 29 | int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data); |
| 30 | int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data); |
| 31 | |
| 32 | pt_regs_write_reg(regs, reg_err, -EFAULT); |
| 33 | pt_regs_write_reg(regs, reg_zero, 0); |
| 34 | |
| 35 | regs->pc = get_ex_fixup(ex); |
| 36 | return true; |
| 37 | } |
| 38 | |
Mark Rutland | 753b323 | 2021-10-19 17:02:18 +0100 | [diff] [blame] | 39 | static bool |
| 40 | ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex, |
| 41 | struct pt_regs *regs) |
| 42 | { |
Evgenii Stepanov | 3758a6c | 2022-01-25 10:22:17 -0800 | [diff] [blame] | 43 | int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->data); |
| 44 | int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->data); |
Mark Rutland | 753b323 | 2021-10-19 17:02:18 +0100 | [diff] [blame] | 45 | unsigned long data, addr, offset; |
| 46 | |
| 47 | addr = pt_regs_read_reg(regs, reg_addr); |
| 48 | |
| 49 | offset = addr & 0x7UL; |
| 50 | addr &= ~0x7UL; |
| 51 | |
| 52 | data = *(unsigned long*)addr; |
| 53 | |
| 54 | #ifndef __AARCH64EB__ |
| 55 | data >>= 8 * offset; |
| 56 | #else |
| 57 | data <<= 8 * offset; |
| 58 | #endif |
| 59 | |
| 60 | pt_regs_write_reg(regs, reg_data, data); |
| 61 | |
| 62 | regs->pc = get_ex_fixup(ex); |
| 63 | return true; |
| 64 | } |
| 65 | |
Mark Rutland | e8c328d | 2021-10-19 17:02:14 +0100 | [diff] [blame] | 66 | bool fixup_exception(struct pt_regs *regs) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 67 | { |
Mark Rutland | 5d0e790 | 2021-10-19 17:02:15 +0100 | [diff] [blame] | 68 | const struct exception_table_entry *ex; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 69 | |
Mark Rutland | 5d0e790 | 2021-10-19 17:02:15 +0100 | [diff] [blame] | 70 | ex = search_exception_tables(instruction_pointer(regs)); |
| 71 | if (!ex) |
Mark Rutland | e8c328d | 2021-10-19 17:02:14 +0100 | [diff] [blame] | 72 | return false; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 73 | |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 74 | switch (ex->type) { |
| 75 | case EX_TYPE_FIXUP: |
| 76 | return ex_handler_fixup(ex, regs); |
| 77 | case EX_TYPE_BPF: |
| 78 | return ex_handler_bpf(ex, regs); |
Mark Rutland | 2e77a62 | 2021-10-19 17:02:17 +0100 | [diff] [blame] | 79 | case EX_TYPE_UACCESS_ERR_ZERO: |
| 80 | return ex_handler_uaccess_err_zero(ex, regs); |
Mark Rutland | 753b323 | 2021-10-19 17:02:18 +0100 | [diff] [blame] | 81 | case EX_TYPE_LOAD_UNALIGNED_ZEROPAD: |
| 82 | return ex_handler_load_unaligned_zeropad(ex, regs); |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 83 | } |
Jean-Philippe Brucker | 8008342 | 2020-07-28 17:21:26 +0200 | [diff] [blame] | 84 | |
Mark Rutland | d6e2cc5 | 2021-10-19 17:02:16 +0100 | [diff] [blame] | 85 | BUG(); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 86 | } |