alt_exceptions.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef __ALT_EXCEPTIONS_H__
  2. #define __ALT_EXCEPTIONS_H__
  3. /******************************************************************************
  4. * *
  5. * License Agreement *
  6. * *
  7. * Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
  8. * All rights reserved. *
  9. * *
  10. * Permission is hereby granted, free of charge, to any person obtaining a *
  11. * copy of this software and associated documentation files (the "Software"), *
  12. * to deal in the Software without restriction, including without limitation *
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  14. * and/or sell copies of the Software, and to permit persons to whom the *
  15. * Software is furnished to do so, subject to the following conditions: *
  16. * *
  17. * The above copyright notice and this permission notice shall be included in *
  18. * all copies or substantial portions of the Software. *
  19. * *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  26. * DEALINGS IN THE SOFTWARE. *
  27. * *
  28. * This agreement shall be governed in all respects by the laws of the State *
  29. * of California and by the laws of the United States of America. *
  30. * *
  31. ******************************************************************************/
  32. #include "alt_types.h"
  33. #include "system.h"
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #endif /* __cplusplus */
  38. /*
  39. * This file defines instruction-generated exception handling and registry
  40. * API, exception type enumeration, and handler return value enumeration for
  41. * Nios II.
  42. */
  43. /*
  44. * The following enumeration describes the value in the CPU EXCEPTION
  45. * register CAUSE bit field.
  46. */
  47. enum alt_exception_cause_e {
  48. /*
  49. * This value is passed to an exception handler's cause argument if
  50. * "extra exceptions" information (EXECPTION) register is not
  51. * present in the processor hardware configuration.
  52. */
  53. NIOS2_EXCEPTION_CAUSE_NOT_PRESENT = -1,
  54. /*
  55. * Real values
  56. */
  57. NIOS2_EXCEPTION_RESET = 0,
  58. NIOS2_EXCEPTION_CPU_ONLY_RESET_REQUEST = 1,
  59. NIOS2_EXCEPTION_INTERRUPT = 2,
  60. NIOS2_EXCEPTION_TRAP_INST = 3,
  61. NIOS2_EXCEPTION_UNIMPLEMENTED_INST = 4,
  62. NIOS2_EXCEPTION_ILLEGAL_INST = 5,
  63. NIOS2_EXCEPTION_MISALIGNED_DATA_ADDR = 6,
  64. NIOS2_EXCEPTION_MISALIGNED_TARGET_PC = 7,
  65. NIOS2_EXCEPTION_DIVISION_ERROR = 8,
  66. NIOS2_EXCEPTION_SUPERVISOR_ONLY_INST_ADDR = 9,
  67. NIOS2_EXCEPTION_SUPERVISOR_ONLY_INST = 10,
  68. NIOS2_EXCEPTION_SUPERVISOR_ONLY_DATA_ADDR = 11,
  69. NIOS2_EXCEPTION_TLB_MISS = 12,
  70. NIOS2_EXCEPTION_TLB_EXECUTE_PERM_VIOLATION = 13,
  71. NIOS2_EXCEPTION_TLB_READ_PERM_VIOLATION = 14,
  72. NIOS2_EXCEPTION_TLB_WRITE_PERM_VIOLATION = 15,
  73. NIOS2_EXCEPTION_MPU_INST_REGION_VIOLATION = 16,
  74. NIOS2_EXCEPTION_MPU_DATA_REGION_VIOLATION = 17,
  75. NIOS2_EXCEPTION_ECC_TLB_ERR = 18,
  76. NIOS2_EXCEPTION_ECC_FETCH_ERR = 19,
  77. NIOS2_EXCEPTION_ECC_REGISTER_FILE_ERR = 20,
  78. NIOS2_EXCEPTION_ECC_DATA_ERR = 21,
  79. NIOS2_EXCEPTION_ECC_DATA_CACHE_WRITEBACK_ERR = 22
  80. };
  81. typedef enum alt_exception_cause_e alt_exception_cause;
  82. /*
  83. * These define valid return values for a user-defined instruction-generated
  84. * exception handler. The handler should return one of these to indicate
  85. * whether to re-issue the instruction that triggered the exception, or to
  86. * skip it.
  87. */
  88. enum alt_exception_result_e {
  89. NIOS2_EXCEPTION_RETURN_REISSUE_INST = 0,
  90. NIOS2_EXCEPTION_RETURN_SKIP_INST = 1
  91. };
  92. typedef enum alt_exception_result_e alt_exception_result;
  93. /*
  94. * alt_instruction_exception_register() can be used to register an exception
  95. * handler for instruction-generated exceptions that are not handled by the
  96. * built-in exception handler (i.e. for interrupts).
  97. *
  98. * The registry API is optionally enabled through the "Enable
  99. * Instruction-related Exception API" HAL BSP setting, which will
  100. * define the macro below.
  101. */
  102. #ifdef ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API
  103. void alt_instruction_exception_register (
  104. alt_exception_result (*exception_handler)(
  105. alt_exception_cause cause,
  106. alt_u32 exception_pc,
  107. alt_u32 bad_addr) );
  108. #endif /*ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API */
  109. /*
  110. * alt_exception_cause_generated_bad_addr() indicates whether a particular
  111. * exception cause value was from an exception-type that generated a valid
  112. * address in the BADADDR register. The contents of BADADDR is passed to
  113. * a user-registered exception handler in all cases, whether valid or not.
  114. * This routine should be called to validate the bad_addr argument to
  115. * your exception handler.
  116. *
  117. * Note that this routine will return false (0) for causes
  118. * NIOS2_EXCEPTION_TLB_MISS and NIOS2_EXCEPTION_ECC_TLB_ERR.
  119. * You must read the TLBMISC.D field to determine if BADADDR
  120. * is valid for these (valid if TLBMISC.D = 1).
  121. */
  122. int alt_exception_cause_generated_bad_addr(alt_exception_cause cause);
  123. /*
  124. * alt_ecc_fatal_exception_register() is called to register a handler to
  125. * service likely fatal ECC error exceptions. Likely the handler will
  126. * assume that correct execution of the running software is not possible
  127. * and re-initialize the processor (e.g. jump to reset address).
  128. *
  129. * Passing null (0x0) in the handler argument will disable a previously-
  130. * registered handler.
  131. *
  132. * Note that if no handler is registered, just normal exception processing
  133. * occurs on a likely fatal ECC exception and the exception processing
  134. * code might trigger an infinite exception loop.
  135. *
  136. * Note that the handler isn't a C function: it must be written in
  137. * assembly-code because it doesn't support C language calling conventions
  138. * and it can't return.
  139. *
  140. * The handler code must be carefully written to avoid triggering
  141. * another fatal ECC exception and creating an infinite exception loop.
  142. * The handler must avoid reading registers in case the fatal ECC
  143. * error is a register file ECC error.
  144. * If a data cache is present, the handler must avoid instructions that
  145. * access the data cache in case the fatal ECC error is a data cache
  146. * related ECC error. This includes cacheable load, cacheable store,
  147. * non-cacheable store (because it looks in the data cache to update the
  148. * data cache if it hits), and all data cache management instructions except
  149. * for INITD.
  150. */
  151. void alt_ecc_fatal_exception_register(alt_u32 handler);
  152. #ifdef __cplusplus
  153. }
  154. #endif /* __cplusplus */
  155. #endif /* __ALT_EXCEPTIONS_H__ */