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 static inline unsigned long 14d6e2cc56SMark Rutland get_ex_fixup(const struct exception_table_entry *ex) 15d6e2cc56SMark Rutland { 16d6e2cc56SMark Rutland return ((unsigned long)&ex->fixup + ex->fixup); 17d6e2cc56SMark Rutland } 18d6e2cc56SMark Rutland 19d6e2cc56SMark Rutland static bool ex_handler_fixup(const struct exception_table_entry *ex, 20d6e2cc56SMark Rutland struct pt_regs *regs) 21d6e2cc56SMark Rutland { 22d6e2cc56SMark Rutland regs->pc = get_ex_fixup(ex); 23d6e2cc56SMark Rutland return true; 24d6e2cc56SMark Rutland } 25d6e2cc56SMark Rutland 262e77a62cSMark Rutland static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex, 272e77a62cSMark Rutland struct pt_regs *regs) 282e77a62cSMark Rutland { 292e77a62cSMark Rutland int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data); 302e77a62cSMark Rutland int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data); 312e77a62cSMark Rutland 322e77a62cSMark Rutland pt_regs_write_reg(regs, reg_err, -EFAULT); 332e77a62cSMark Rutland pt_regs_write_reg(regs, reg_zero, 0); 342e77a62cSMark Rutland 352e77a62cSMark Rutland regs->pc = get_ex_fixup(ex); 362e77a62cSMark Rutland return true; 372e77a62cSMark Rutland } 382e77a62cSMark Rutland 39753b3236SMark Rutland static bool 40753b3236SMark Rutland ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex, 41753b3236SMark Rutland struct pt_regs *regs) 42753b3236SMark Rutland { 433758a6c7SEvgenii Stepanov int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->data); 443758a6c7SEvgenii Stepanov int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->data); 45753b3236SMark Rutland unsigned long data, addr, offset; 46753b3236SMark Rutland 47753b3236SMark Rutland addr = pt_regs_read_reg(regs, reg_addr); 48753b3236SMark Rutland 49753b3236SMark Rutland offset = addr & 0x7UL; 50753b3236SMark Rutland addr &= ~0x7UL; 51753b3236SMark Rutland 52753b3236SMark Rutland data = *(unsigned long*)addr; 53753b3236SMark Rutland 54753b3236SMark Rutland #ifndef __AARCH64EB__ 55753b3236SMark Rutland data >>= 8 * offset; 56753b3236SMark Rutland #else 57753b3236SMark Rutland data <<= 8 * offset; 58753b3236SMark Rutland #endif 59753b3236SMark Rutland 60753b3236SMark Rutland pt_regs_write_reg(regs, reg_data, data); 61753b3236SMark Rutland 62753b3236SMark Rutland regs->pc = get_ex_fixup(ex); 63753b3236SMark Rutland return true; 64753b3236SMark Rutland } 65753b3236SMark Rutland 66e8c328d7SMark Rutland bool fixup_exception(struct pt_regs *regs) 671d18c47cSCatalin Marinas { 685d0e7905SMark Rutland const struct exception_table_entry *ex; 691d18c47cSCatalin Marinas 705d0e7905SMark Rutland ex = search_exception_tables(instruction_pointer(regs)); 715d0e7905SMark Rutland if (!ex) 72e8c328d7SMark Rutland return false; 731d18c47cSCatalin Marinas 74d6e2cc56SMark Rutland switch (ex->type) { 75d6e2cc56SMark Rutland case EX_TYPE_FIXUP: 76d6e2cc56SMark Rutland return ex_handler_fixup(ex, regs); 77d6e2cc56SMark Rutland case EX_TYPE_BPF: 78d6e2cc56SMark Rutland return ex_handler_bpf(ex, regs); 792e77a62cSMark Rutland case EX_TYPE_UACCESS_ERR_ZERO: 80*4953fc3dSTong Tiangen case EX_TYPE_KACCESS_ERR_ZERO: 812e77a62cSMark Rutland return ex_handler_uaccess_err_zero(ex, regs); 82753b3236SMark Rutland case EX_TYPE_LOAD_UNALIGNED_ZEROPAD: 83753b3236SMark Rutland return ex_handler_load_unaligned_zeropad(ex, regs); 84d6e2cc56SMark Rutland } 8580083428SJean-Philippe Brucker 86d6e2cc56SMark Rutland BUG(); 871d18c47cSCatalin Marinas } 88