extable.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Lennox Wu <lennox.wu@sunplusct.com>
  5. * Chen Liqin <liqin.chen@sunplusct.com>
  6. * Copyright (C) 2013 Regents of the University of California
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/extable.h>
  10. #include <linux/module.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/asm-extable.h>
  13. #include <asm/ptrace.h>
  14. static inline unsigned long
  15. get_ex_fixup(const struct exception_table_entry *ex)
  16. {
  17. return ((unsigned long)&ex->fixup + ex->fixup);
  18. }
  19. static bool ex_handler_fixup(const struct exception_table_entry *ex,
  20. struct pt_regs *regs)
  21. {
  22. regs->epc = get_ex_fixup(ex);
  23. return true;
  24. }
  25. static inline unsigned long regs_get_gpr(struct pt_regs *regs, unsigned int offset)
  26. {
  27. if (unlikely(!offset || offset > MAX_REG_OFFSET))
  28. return 0;
  29. return *(unsigned long *)((unsigned long)regs + offset);
  30. }
  31. static inline void regs_set_gpr(struct pt_regs *regs, unsigned int offset,
  32. unsigned long val)
  33. {
  34. if (unlikely(offset > MAX_REG_OFFSET))
  35. return;
  36. if (offset)
  37. *(unsigned long *)((unsigned long)regs + offset) = val;
  38. }
  39. static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
  40. struct pt_regs *regs)
  41. {
  42. int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
  43. int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);
  44. regs_set_gpr(regs, reg_err * sizeof(unsigned long), -EFAULT);
  45. regs_set_gpr(regs, reg_zero * sizeof(unsigned long), 0);
  46. regs->epc = get_ex_fixup(ex);
  47. return true;
  48. }
  49. static bool
  50. ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex,
  51. struct pt_regs *regs)
  52. {
  53. int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->data);
  54. int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->data);
  55. unsigned long data, addr, offset;
  56. addr = regs_get_gpr(regs, reg_addr * sizeof(unsigned long));
  57. offset = addr & 0x7UL;
  58. addr &= ~0x7UL;
  59. data = *(unsigned long *)addr >> (offset * 8);
  60. regs_set_gpr(regs, reg_data * sizeof(unsigned long), data);
  61. regs->epc = get_ex_fixup(ex);
  62. return true;
  63. }
  64. bool fixup_exception(struct pt_regs *regs)
  65. {
  66. const struct exception_table_entry *ex;
  67. ex = search_exception_tables(regs->epc);
  68. if (!ex)
  69. return false;
  70. switch (ex->type) {
  71. case EX_TYPE_FIXUP:
  72. return ex_handler_fixup(ex, regs);
  73. case EX_TYPE_BPF:
  74. return ex_handler_bpf(ex, regs);
  75. case EX_TYPE_UACCESS_ERR_ZERO:
  76. return ex_handler_uaccess_err_zero(ex, regs);
  77. case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
  78. return ex_handler_load_unaligned_zeropad(ex, regs);
  79. }
  80. BUG();
  81. }