extable.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on arch/arm/mm/extable.c
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/extable.h>
  7. #include <linux/uaccess.h>
  8. #include <asm/asm-extable.h>
  9. #include <asm/ptrace.h>
  10. static inline unsigned long
  11. get_ex_fixup(const struct exception_table_entry *ex)
  12. {
  13. return ((unsigned long)&ex->fixup + ex->fixup);
  14. }
  15. static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
  16. struct pt_regs *regs)
  17. {
  18. int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
  19. int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);
  20. pt_regs_write_reg(regs, reg_err, -EFAULT);
  21. pt_regs_write_reg(regs, reg_zero, 0);
  22. regs->pc = get_ex_fixup(ex);
  23. return true;
  24. }
  25. static bool
  26. ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex,
  27. struct pt_regs *regs)
  28. {
  29. int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->data);
  30. int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->data);
  31. unsigned long data, addr, offset;
  32. addr = pt_regs_read_reg(regs, reg_addr);
  33. offset = addr & 0x7UL;
  34. addr &= ~0x7UL;
  35. data = *(unsigned long*)addr;
  36. #ifndef __AARCH64EB__
  37. data >>= 8 * offset;
  38. #else
  39. data <<= 8 * offset;
  40. #endif
  41. pt_regs_write_reg(regs, reg_data, data);
  42. regs->pc = get_ex_fixup(ex);
  43. return true;
  44. }
  45. bool fixup_exception(struct pt_regs *regs)
  46. {
  47. const struct exception_table_entry *ex;
  48. ex = search_exception_tables(instruction_pointer(regs));
  49. if (!ex)
  50. return false;
  51. switch (ex->type) {
  52. case EX_TYPE_BPF:
  53. return ex_handler_bpf(ex, regs);
  54. case EX_TYPE_UACCESS_ERR_ZERO:
  55. case EX_TYPE_KACCESS_ERR_ZERO:
  56. return ex_handler_uaccess_err_zero(ex, regs);
  57. case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
  58. return ex_handler_load_unaligned_zeropad(ex, regs);
  59. }
  60. BUG();
  61. }