1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21d18c47cSCatalin Marinas /* 31d18c47cSCatalin Marinas * Based on arch/arm/mm/extable.c 41d18c47cSCatalin Marinas */ 51d18c47cSCatalin Marinas 6*2e77a62cSMark 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> 11*2e77a62cSMark 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 29*2e77a62cSMark Rutland static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex, 30*2e77a62cSMark Rutland struct pt_regs *regs) 31*2e77a62cSMark Rutland { 32*2e77a62cSMark Rutland int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data); 33*2e77a62cSMark Rutland int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data); 34*2e77a62cSMark Rutland 35*2e77a62cSMark Rutland pt_regs_write_reg(regs, reg_err, -EFAULT); 36*2e77a62cSMark Rutland pt_regs_write_reg(regs, reg_zero, 0); 37*2e77a62cSMark Rutland 38*2e77a62cSMark Rutland regs->pc = get_ex_fixup(ex); 39*2e77a62cSMark Rutland return true; 40*2e77a62cSMark Rutland } 41*2e77a62cSMark Rutland 42e8c328d7SMark Rutland bool fixup_exception(struct pt_regs *regs) 431d18c47cSCatalin Marinas { 445d0e7905SMark Rutland const struct exception_table_entry *ex; 451d18c47cSCatalin Marinas 465d0e7905SMark Rutland ex = search_exception_tables(instruction_pointer(regs)); 475d0e7905SMark Rutland if (!ex) 48e8c328d7SMark Rutland return false; 491d18c47cSCatalin Marinas 50d6e2cc56SMark Rutland switch (ex->type) { 51d6e2cc56SMark Rutland case EX_TYPE_FIXUP: 52d6e2cc56SMark Rutland return ex_handler_fixup(ex, regs); 53d6e2cc56SMark Rutland case EX_TYPE_BPF: 54d6e2cc56SMark Rutland return ex_handler_bpf(ex, regs); 55*2e77a62cSMark Rutland case EX_TYPE_UACCESS_ERR_ZERO: 56*2e77a62cSMark Rutland return ex_handler_uaccess_err_zero(ex, regs); 57d6e2cc56SMark Rutland } 5880083428SJean-Philippe Brucker 59d6e2cc56SMark Rutland BUG(); 601d18c47cSCatalin Marinas } 61