1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21d18c47cSCatalin Marinas /* 31d18c47cSCatalin Marinas * Based on arch/arm/mm/extable.c 41d18c47cSCatalin Marinas */ 51d18c47cSCatalin Marinas 62e77a62cSMark Rutland #include <linux/bitfield.h> 70edfa839SPaul Gortmaker #include <linux/extable.h> 81d18c47cSCatalin Marinas #include <linux/uaccess.h> 91d18c47cSCatalin Marinas 10d6e2cc56SMark Rutland #include <asm/asm-extable.h> 112e77a62cSMark Rutland #include <asm/ptrace.h> 12d6e2cc56SMark Rutland 13d6e2cc56SMark Rutland typedef bool (*ex_handler_t)(const struct exception_table_entry *, 14d6e2cc56SMark Rutland struct pt_regs *); 15d6e2cc56SMark Rutland 16d6e2cc56SMark Rutland static inline unsigned long 17d6e2cc56SMark Rutland get_ex_fixup(const struct exception_table_entry *ex) 18d6e2cc56SMark Rutland { 19d6e2cc56SMark Rutland return ((unsigned long)&ex->fixup + ex->fixup); 20d6e2cc56SMark Rutland } 21d6e2cc56SMark Rutland 22d6e2cc56SMark Rutland static bool ex_handler_fixup(const struct exception_table_entry *ex, 23d6e2cc56SMark Rutland struct pt_regs *regs) 24d6e2cc56SMark Rutland { 25d6e2cc56SMark Rutland regs->pc = get_ex_fixup(ex); 26d6e2cc56SMark Rutland return true; 27d6e2cc56SMark Rutland } 28d6e2cc56SMark Rutland 292e77a62cSMark Rutland static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex, 302e77a62cSMark Rutland struct pt_regs *regs) 312e77a62cSMark Rutland { 322e77a62cSMark Rutland int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data); 332e77a62cSMark Rutland int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data); 342e77a62cSMark Rutland 352e77a62cSMark Rutland pt_regs_write_reg(regs, reg_err, -EFAULT); 362e77a62cSMark Rutland pt_regs_write_reg(regs, reg_zero, 0); 372e77a62cSMark Rutland 382e77a62cSMark Rutland regs->pc = get_ex_fixup(ex); 392e77a62cSMark Rutland return true; 402e77a62cSMark Rutland } 412e77a62cSMark Rutland 42*753b3236SMark Rutland static bool 43*753b3236SMark Rutland ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex, 44*753b3236SMark Rutland struct pt_regs *regs) 45*753b3236SMark Rutland { 46*753b3236SMark Rutland int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->type); 47*753b3236SMark Rutland int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->type); 48*753b3236SMark Rutland unsigned long data, addr, offset; 49*753b3236SMark Rutland 50*753b3236SMark Rutland addr = pt_regs_read_reg(regs, reg_addr); 51*753b3236SMark Rutland 52*753b3236SMark Rutland offset = addr & 0x7UL; 53*753b3236SMark Rutland addr &= ~0x7UL; 54*753b3236SMark Rutland 55*753b3236SMark Rutland data = *(unsigned long*)addr; 56*753b3236SMark Rutland 57*753b3236SMark Rutland #ifndef __AARCH64EB__ 58*753b3236SMark Rutland data >>= 8 * offset; 59*753b3236SMark Rutland #else 60*753b3236SMark Rutland data <<= 8 * offset; 61*753b3236SMark Rutland #endif 62*753b3236SMark Rutland 63*753b3236SMark Rutland pt_regs_write_reg(regs, reg_data, data); 64*753b3236SMark Rutland 65*753b3236SMark Rutland regs->pc = get_ex_fixup(ex); 66*753b3236SMark Rutland return true; 67*753b3236SMark Rutland } 68*753b3236SMark Rutland 69e8c328d7SMark Rutland bool fixup_exception(struct pt_regs *regs) 701d18c47cSCatalin Marinas { 715d0e7905SMark Rutland const struct exception_table_entry *ex; 721d18c47cSCatalin Marinas 735d0e7905SMark Rutland ex = search_exception_tables(instruction_pointer(regs)); 745d0e7905SMark Rutland if (!ex) 75e8c328d7SMark Rutland return false; 761d18c47cSCatalin Marinas 77d6e2cc56SMark Rutland switch (ex->type) { 78d6e2cc56SMark Rutland case EX_TYPE_FIXUP: 79d6e2cc56SMark Rutland return ex_handler_fixup(ex, regs); 80d6e2cc56SMark Rutland case EX_TYPE_BPF: 81d6e2cc56SMark Rutland return ex_handler_bpf(ex, regs); 822e77a62cSMark Rutland case EX_TYPE_UACCESS_ERR_ZERO: 832e77a62cSMark Rutland return ex_handler_uaccess_err_zero(ex, regs); 84*753b3236SMark Rutland case EX_TYPE_LOAD_UNALIGNED_ZEROPAD: 85*753b3236SMark Rutland return ex_handler_load_unaligned_zeropad(ex, regs); 86d6e2cc56SMark Rutland } 8780083428SJean-Philippe Brucker 88d6e2cc56SMark Rutland BUG(); 891d18c47cSCatalin Marinas } 90