1 |
8 |
alfik |
/******************************************************************************
|
2 |
|
|
* *
|
3 |
|
|
* License Agreement *
|
4 |
|
|
* *
|
5 |
|
|
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
|
6 |
|
|
* All rights reserved. *
|
7 |
|
|
* *
|
8 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
9 |
|
|
* copy of this software and associated documentation files (the "Software"), *
|
10 |
|
|
* to deal in the Software without restriction, including without limitation *
|
11 |
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
12 |
|
|
* and/or sell copies of the Software, and to permit persons to whom the *
|
13 |
|
|
* Software is furnished to do so, subject to the following conditions: *
|
14 |
|
|
* *
|
15 |
|
|
* The above copyright notice and this permission notice shall be included in *
|
16 |
|
|
* all copies or substantial portions of the Software. *
|
17 |
|
|
* *
|
18 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
21 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
22 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
23 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
24 |
|
|
* DEALINGS IN THE SOFTWARE. *
|
25 |
|
|
* *
|
26 |
|
|
* This agreement shall be governed in all respects by the laws of the State *
|
27 |
|
|
* of California and by the laws of the United States of America. *
|
28 |
|
|
* *
|
29 |
|
|
* Altera does not recommend, suggest or require that this reference design *
|
30 |
|
|
* file be used in conjunction or combination with any other product. *
|
31 |
|
|
******************************************************************************/
|
32 |
|
|
#include "sys/alt_exceptions.h"
|
33 |
|
|
#include "nios2.h"
|
34 |
|
|
#include "alt_types.h"
|
35 |
|
|
#include "system.h"
|
36 |
|
|
|
37 |
|
|
/*
|
38 |
|
|
* This file implements support for calling user-registered handlers for
|
39 |
|
|
* instruction-generated exceptions. This handler could also be reached
|
40 |
|
|
* in the event of a spurious interrupt.
|
41 |
|
|
*
|
42 |
|
|
* The handler code is optionally enabled through the "Enable
|
43 |
|
|
* Instruction-related Exception API" HAL BSP setting, which will
|
44 |
|
|
* define the macro below.
|
45 |
|
|
*/
|
46 |
|
|
#ifdef ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API
|
47 |
|
|
|
48 |
|
|
/* Function pointer to exception callback routine */
|
49 |
|
|
alt_exception_result (*alt_instruction_exception_handler)
|
50 |
|
|
(alt_exception_cause, alt_u32, alt_u32) = 0x0;
|
51 |
|
|
|
52 |
|
|
/* Link entry routine to .exceptions section */
|
53 |
|
|
int alt_instruction_exception_entry (alt_u32 exception_pc)
|
54 |
|
|
__attribute__ ((section (".exceptions")));
|
55 |
|
|
|
56 |
|
|
/*
|
57 |
|
|
* This is the entry point for instruction-generated exceptions handling.
|
58 |
|
|
* This routine will be called by alt_exceptions_entry.S, after it determines
|
59 |
|
|
* that an exception could not be handled by handlers that preceed that
|
60 |
|
|
* of instruction-generated exceptions (such as interrupts).
|
61 |
|
|
*
|
62 |
|
|
* For this to function properly, you must register an exception handler
|
63 |
|
|
* using alt_instruction_exception_register(). This routine will call
|
64 |
|
|
* that handler if it has been registered. Absent a handler, it will
|
65 |
|
|
* break break or hang as discussed below.
|
66 |
|
|
*/
|
67 |
|
|
int
|
68 |
|
|
alt_instruction_exception_entry (alt_u32 exception_pc)
|
69 |
|
|
{
|
70 |
|
|
alt_u32 cause, badaddr;
|
71 |
|
|
|
72 |
|
|
/*
|
73 |
|
|
* If the processor hardware has the optional EXCEPTIONS & BADADDR registers,
|
74 |
|
|
* read them and pass their content to the user handler. These are always
|
75 |
|
|
* present if the MMU or MPU is enabled, and optionally for other advanced
|
76 |
|
|
* exception types via the "Extra exceptions information" setting in the
|
77 |
|
|
* processor (hardware) configuration.
|
78 |
|
|
*
|
79 |
|
|
* If these registers are not present, the cause field will be set to
|
80 |
|
|
* NIOS2_EXCEPTION_CAUSE_NOT_PRESENT. Your handling routine should
|
81 |
|
|
* check the validity of the cause argument before proceeding.
|
82 |
|
|
*/
|
83 |
|
|
#ifdef NIOS2_HAS_EXTRA_EXCEPTION_INFO
|
84 |
|
|
/* Get exception cause & "badaddr" */
|
85 |
|
|
NIOS2_READ_EXCEPTION(cause);
|
86 |
|
|
cause = ( (cause & NIOS2_EXCEPTION_REG_CAUSE_MASK) >>
|
87 |
|
|
NIOS2_EXCEPTION_REG_CAUSE_OFST );
|
88 |
|
|
|
89 |
|
|
NIOS2_READ_BADADDR(badaddr);
|
90 |
|
|
#else
|
91 |
|
|
cause = NIOS2_EXCEPTION_CAUSE_NOT_PRESENT;
|
92 |
|
|
badaddr = 0;
|
93 |
|
|
#endif /* NIOS2_HAS_EXTRA_EXCEPTION_INFO */
|
94 |
|
|
|
95 |
|
|
if(alt_instruction_exception_handler) {
|
96 |
|
|
/*
|
97 |
|
|
* Call handler. Its return value indicates whether the exception-causing
|
98 |
|
|
* instruction should be re-issued. The code that called us,
|
99 |
|
|
* alt_eceptions_entry.S, will look at this value and adjust the ea
|
100 |
|
|
* register as necessary
|
101 |
|
|
*/
|
102 |
|
|
return alt_instruction_exception_handler(cause, exception_pc, badaddr);
|
103 |
|
|
}
|
104 |
|
|
/*
|
105 |
|
|
* We got here because an instruction-generated exception occured, but no
|
106 |
|
|
* handler is present. We do not presume to know how to handle it. If the
|
107 |
|
|
* debugger is present, break, otherwise hang.
|
108 |
|
|
*
|
109 |
|
|
* If you've reached here in the debugger, consider examining the
|
110 |
|
|
* EXCEPTIONS register cause bit-field, which was read into the 'cause'
|
111 |
|
|
* variable above, and compare it against the exceptions-type enumeration
|
112 |
|
|
* in alt_exceptions.h. This register is availabe if the MMU or MPU is
|
113 |
|
|
* present, or if the "Extra exceptions information" hardware option is
|
114 |
|
|
* selected.
|
115 |
|
|
*
|
116 |
|
|
* If you get here then one of the following could have happened:
|
117 |
|
|
*
|
118 |
|
|
* - An instruction-generated exception occured, and the processor
|
119 |
|
|
* does not have the extra exceptions feature enabled, or you
|
120 |
|
|
* have not registered a handler using
|
121 |
|
|
* alt_instruction_exception_register()
|
122 |
|
|
*
|
123 |
|
|
* Some examples of instruction-generated exceptions and why they
|
124 |
|
|
* might occur:
|
125 |
|
|
*
|
126 |
|
|
* - Your program could have been compiled for a full-featured
|
127 |
|
|
* Nios II core, but it is running on a smaller core, and
|
128 |
|
|
* instruction emulation has been disabled by defining
|
129 |
|
|
* ALT_NO_INSTRUCTION_EMULATION.
|
130 |
|
|
*
|
131 |
|
|
* You can work around the problem by re-enabling instruction
|
132 |
|
|
* emulation, or you can figure out why your program is being
|
133 |
|
|
* compiled for a system other than the one that it is running on.
|
134 |
|
|
*
|
135 |
|
|
* - Your program has executed a trap instruction, but has not
|
136 |
|
|
* implemented a handler for this instruction.
|
137 |
|
|
*
|
138 |
|
|
* - Your program has executed an illegal instruction (one which is
|
139 |
|
|
* not defined in the instruction set).
|
140 |
|
|
*
|
141 |
|
|
* - Your processor includes an MMU or MPU, and you have enabled it
|
142 |
|
|
* before registering an exception handler to service exceptions it
|
143 |
|
|
* generates.
|
144 |
|
|
*
|
145 |
|
|
* The problem could also be hardware related:
|
146 |
|
|
* - If your hardware is broken and is generating spurious interrupts
|
147 |
|
|
* (a peripheral which negates its interrupt output before its
|
148 |
|
|
* interrupt handler has been executed will cause spurious interrupts)
|
149 |
|
|
*/
|
150 |
|
|
else {
|
151 |
|
|
#ifdef NIOS2_HAS_DEBUG_STUB
|
152 |
|
|
NIOS2_BREAK();
|
153 |
|
|
#else
|
154 |
|
|
while(1)
|
155 |
|
|
;
|
156 |
|
|
#endif /* NIOS2_HAS_DEBUG_STUB */
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
/* We should not get here. Remove compiler warning. */
|
160 |
|
|
return NIOS2_EXCEPTION_RETURN_REISSUE_INST;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
#endif /* ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API */
|
164 |
|
|
|
165 |
|
|
/*
|
166 |
|
|
* This routine indicates whether a particular exception cause will have
|
167 |
|
|
* set a valid address into the BADADDR register, which is included
|
168 |
|
|
* in the arguments to a user-registered instruction-generated exception
|
169 |
|
|
* handler. Many exception types do not set valid contents in BADADDR;
|
170 |
|
|
* this is a convenience routine to easily test the validity of that
|
171 |
|
|
* argument in your handler.
|
172 |
|
|
*
|
173 |
|
|
* Note that this routine will return false (0) for causes
|
174 |
|
|
* NIOS2_EXCEPTION_TLB_MISS and NIOS2_EXCEPTION_ECC_TLB_ERR.
|
175 |
|
|
* You must read the TLBMISC.D field to determine if BADADDR
|
176 |
|
|
* is valid for these (valid if TLBMISC.D = 1).
|
177 |
|
|
*
|
178 |
|
|
* Arguments:
|
179 |
|
|
* cause: The 5-bit exception cause field of the EXCEPTIONS register,
|
180 |
|
|
* shifted to the LSB position. You may pass the 'cause' argument
|
181 |
|
|
* in a handler you registered directy to this routine.
|
182 |
|
|
*
|
183 |
|
|
* Return: 1: BADADDR (bad_addr argument to handler) is valid
|
184 |
|
|
* 0: BADADDR is not valid
|
185 |
|
|
*/
|
186 |
|
|
int
|
187 |
|
|
alt_exception_cause_generated_bad_addr(alt_exception_cause cause)
|
188 |
|
|
{
|
189 |
|
|
switch (cause) {
|
190 |
|
|
case NIOS2_EXCEPTION_SUPERVISOR_ONLY_DATA_ADDR:
|
191 |
|
|
case NIOS2_EXCEPTION_MISALIGNED_DATA_ADDR:
|
192 |
|
|
case NIOS2_EXCEPTION_MISALIGNED_TARGET_PC:
|
193 |
|
|
case NIOS2_EXCEPTION_TLB_READ_PERM_VIOLATION:
|
194 |
|
|
case NIOS2_EXCEPTION_TLB_WRITE_PERM_VIOLATION:
|
195 |
|
|
case NIOS2_EXCEPTION_MPU_DATA_REGION_VIOLATION:
|
196 |
|
|
case NIOS2_EXCEPTION_ECC_DATA_ERR:
|
197 |
|
|
return 1;
|
198 |
|
|
|
199 |
|
|
case NIOS2_EXCEPTION_TLB_MISS:
|
200 |
|
|
case NIOS2_EXCEPTION_ECC_TLB_ERR:
|
201 |
|
|
return 0;
|
202 |
|
|
|
203 |
|
|
default:
|
204 |
|
|
return 0;
|
205 |
|
|
}
|
206 |
|
|
}
|