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