trap.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021, Shenzhen Academy of Aerospace Technology
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-16 Dystopia the first version
  9. */
  10. #ifndef __TRAP_H__
  11. #define __TRAP_H__
  12. #include "c66xx.h"
  13. /*
  14. * exception operation macro
  15. */
  16. #define disable_exception()
  17. #define get_except_type() EFR
  18. #define ack_exception(type) ECR = 1ul << (type)
  19. #define get_iexcept() IERR
  20. #define set_iexcept(mask) IERR = (mask)
  21. /*
  22. * exception types
  23. */
  24. #define EXCEPT_TYPE_NXF 31 /* NMI */
  25. #define EXCEPT_TYPE_EXC 30 /* external exception */
  26. #define EXCEPT_TYPE_IXF 1 /* internal exception */
  27. #define EXCEPT_TYPE_SXF 0 /* software exception */
  28. #define EXCEPT_CAUSE_LBX (1 << 7) /* loop buffer exception */
  29. #define EXCEPT_CAUSE_PRX (1 << 6) /* privilege exception */
  30. #define EXCEPT_CAUSE_RAX (1 << 5) /* resource access exception */
  31. #define EXCEPT_CAUSE_RCX (1 << 4) /* resource conflict exception */
  32. #define EXCEPT_CAUSE_OPX (1 << 3) /* opcode exception */
  33. #define EXCEPT_CAUSE_EPX (1 << 2) /* execute packet exception */
  34. #define EXCEPT_CAUSE_FPX (1 << 1) /* fetch packet exception */
  35. #define EXCEPT_CAUSE_IFX (1 << 0) /* instruction fetch exception */
  36. enum SYSTEM_TRAP_CODE
  37. {
  38. ABORT_BUS_ADDRERR = 0, // bus address error
  39. ABORT_BUS_ACCERR, // bus access permission error
  40. ABORT_OPCODE_ILL, // illegal opcode
  41. ABORT_PRVREG_ILL, // privilege register
  42. ABORT_PRVOPC_ILL, // privileged opcode
  43. ABORT_ILLTRP_ILL, // illegal trap
  44. ABORT_BRKPT_ILL, // handling breakpoints
  45. };
  46. /*
  47. * abort types
  48. */
  49. #define ABORT_TYPE_BUS 0 // bus access abnormal
  50. #define ABORT_TYPE_MAP 1 // page table mapping error
  51. #define ABORT_TYPE_UNDDEF 0xff // undefined exception
  52. #define ABORT_TYPE_FATAL 0xffffffff // fatal error
  53. struct rt_exception_info {
  54. char *kernel_str;
  55. int type;
  56. int code;
  57. };
  58. #define BKPT_OPCODE 0x56454314 /* illegal opcode */
  59. #define INTC_MEXPMASK __SYSREGA(0x018000e0, unsigned int)
  60. extern void rt_trap_init(void);
  61. extern void rt_hw_enable_exception(void);
  62. extern int __fls(int val);
  63. extern int __ffs(int val);
  64. /*
  65. * ffz - find first zero in word.
  66. * @word: The word to search
  67. *
  68. * Undefined if no zero exists, so code should check against ~0UL first.
  69. */
  70. #define ffz(x) __ffs(~(x))
  71. /**
  72. * fls - find last (most-significant) bit set
  73. * @x: the word to search
  74. *
  75. * This is defined the same way as ffs.
  76. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  77. */
  78. static inline int fls(int x)
  79. {
  80. if (!x)
  81. {
  82. return 0;
  83. }
  84. return 32 - __fls(x);
  85. }
  86. /**
  87. * ffs - find first bit set
  88. * @x: the word to search
  89. *
  90. * This is defined the same way as
  91. * the libc and compiler builtin ffs routines, therefore
  92. * differs in spirit from the above ffz (man ffs).
  93. * Note ffs(0) = 0, ffs(1) = 1, ffs(0x80000000) = 32.
  94. */
  95. static inline int ffs(int x)
  96. {
  97. if (!x)
  98. {
  99. return 0;
  100. }
  101. return __ffs(x) + 1;
  102. }
  103. #endif /* __TRAP_H__ */