| 1 |
2 |
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 alt_instruction_exception_entry (alt_u32 exception_pc)
|
| 68 |
|
|
{
|
| 69 |
|
|
alt_u32 cause, badaddr;
|
| 70 |
|
|
|
| 71 |
|
|
/*
|
| 72 |
|
|
* If the processor hardware has the optional EXCEPTIONS & BADADDR registers,
|
| 73 |
|
|
* read them and pass their content to the user handler. These are always
|
| 74 |
|
|
* present if the MMU or MPU is enabled, and optionally for other advanced
|
| 75 |
|
|
* exception types via the "Extra exceptions information" setting in the
|
| 76 |
|
|
* processor (hardware) configuration.
|
| 77 |
|
|
*
|
| 78 |
|
|
* If these registers are not present, the cause field will be set to
|
| 79 |
|
|
* NIOS2_EXCEPTION_CAUSE_NOT_PRESENT. Your handling routine should
|
| 80 |
|
|
* check the validity of the cause argument before proceeding.
|
| 81 |
|
|
*/
|
| 82 |
|
|
#ifdef NIOS2_HAS_EXTRA_EXCEPTION_INFO
|
| 83 |
|
|
/* Get exception cause & "badaddr" */
|
| 84 |
|
|
NIOS2_READ_EXCEPTION(cause);
|
| 85 |
|
|
cause = ( (cause & NIOS2_EXCEPTION_REG_CAUSE_MASK) >>
|
| 86 |
|
|
NIOS2_EXCEPTION_REG_CAUSE_OFST );
|
| 87 |
|
|
|
| 88 |
|
|
NIOS2_READ_BADADDR(badaddr);
|
| 89 |
|
|
#else
|
| 90 |
|
|
cause = NIOS2_EXCEPTION_CAUSE_NOT_PRESENT;
|
| 91 |
|
|
badaddr = 0;
|
| 92 |
|
|
#endif /* NIOS2_HAS_EXTRA_EXCEPTION_INFO */
|
| 93 |
|
|
|
| 94 |
|
|
if(alt_instruction_exception_handler) {
|
| 95 |
|
|
/*
|
| 96 |
|
|
* Call handler. Its return value indicates whether the exception-causing
|
| 97 |
|
|
* instruction should be re-issued. The code that called us,
|
| 98 |
|
|
* alt_eceptions_entry.S, will look at this value and adjust the ea
|
| 99 |
|
|
* register as necessary
|
| 100 |
|
|
*/
|
| 101 |
|
|
return alt_instruction_exception_handler(cause, exception_pc, badaddr);
|
| 102 |
|
|
}
|
| 103 |
|
|
/*
|
| 104 |
|
|
* We got here because an instruction-generated exception occured, but no
|
| 105 |
|
|
* handler is present. We do not presume to know how to handle it. If the
|
| 106 |
|
|
* debugger is present, break, otherwise hang.
|
| 107 |
|
|
*
|
| 108 |
|
|
* If you've reached here in the debugger, consider examining the
|
| 109 |
|
|
* EXCEPTIONS register cause bit-field, which was read into the 'cause'
|
| 110 |
|
|
* variable above, and compare it against the exceptions-type enumeration
|
| 111 |
|
|
* in alt_exceptions.h. This register is availabe if the MMU or MPU is
|
| 112 |
|
|
* present, or if the "Extra exceptions information" hardware option is
|
| 113 |
|
|
* selected.
|
| 114 |
|
|
*
|
| 115 |
|
|
* If you get here then one of the following could have happened:
|
| 116 |
|
|
*
|
| 117 |
|
|
* - An instruction-generated exception occured, and the processor
|
| 118 |
|
|
* does not have the extra exceptions feature enabled, or you
|
| 119 |
|
|
* have not registered a handler using
|
| 120 |
|
|
* alt_instruction_exception_register()
|
| 121 |
|
|
*
|
| 122 |
|
|
* Some examples of instruction-generated exceptions and why they
|
| 123 |
|
|
* might occur:
|
| 124 |
|
|
*
|
| 125 |
|
|
* - Your program could have been compiled for a full-featured
|
| 126 |
|
|
* Nios II core, but it is running on a smaller core, and
|
| 127 |
|
|
* instruction emulation has been disabled by defining
|
| 128 |
|
|
* ALT_NO_INSTRUCTION_EMULATION.
|
| 129 |
|
|
*
|
| 130 |
|
|
* You can work around the problem by re-enabling instruction
|
| 131 |
|
|
* emulation, or you can figure out why your program is being
|
| 132 |
|
|
* compiled for a system other than the one that it is running on.
|
| 133 |
|
|
*
|
| 134 |
|
|
* - Your program has executed a trap instruction, but has not
|
| 135 |
|
|
* implemented a handler for this instruction.
|
| 136 |
|
|
*
|
| 137 |
|
|
* - Your program has executed an illegal instruction (one which is
|
| 138 |
|
|
* not defined in the instruction set).
|
| 139 |
|
|
*
|
| 140 |
|
|
* - Your processor includes an MMU or MPU, and you have enabled it
|
| 141 |
|
|
* before registering an exception handler to service exceptions it
|
| 142 |
|
|
* generates.
|
| 143 |
|
|
*
|
| 144 |
|
|
* The problem could also be hardware related:
|
| 145 |
|
|
* - If your hardware is broken and is generating spurious interrupts
|
| 146 |
|
|
* (a peripheral which negates its interrupt output before its
|
| 147 |
|
|
* interrupt handler has been executed will cause spurious interrupts)
|
| 148 |
|
|
*/
|
| 149 |
|
|
else {
|
| 150 |
|
|
#ifdef NIOS2_HAS_DEBUG_STUB
|
| 151 |
|
|
NIOS2_BREAK();
|
| 152 |
|
|
#else
|
| 153 |
|
|
while(1)
|
| 154 |
|
|
;
|
| 155 |
|
|
#endif /* NIOS2_HAS_DEBUG_STUB */
|
| 156 |
|
|
}
|
| 157 |
|
|
|
| 158 |
|
|
/* // We should not get here. Remove compiler warning. */
|
| 159 |
|
|
return NIOS2_EXCEPTION_RETURN_REISSUE_INST;
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
#endif /* ALT_INCLUDE_INSTRUCTION_RELATED_EXCEPTION_API */
|
| 163 |
|
|
|
| 164 |
|
|
/*
|
| 165 |
|
|
* This routine indicates whether a particular exception cause will have
|
| 166 |
|
|
* set a valid address into the BADADDR register, which is included
|
| 167 |
|
|
* in the arguments to a user-registered instruction-generated exception
|
| 168 |
|
|
* handler. Many exception types do not set valid contents in BADADDR;
|
| 169 |
|
|
* this is a convenience routine to easily test the validity of that
|
| 170 |
|
|
* argument in your handler.
|
| 171 |
|
|
*
|
| 172 |
|
|
* Note that this routine will return false (0) for cause '12',
|
| 173 |
|
|
* TLB miss. This is because there are four exception types that
|
| 174 |
|
|
* share that cause, two of which do not have a valid BADADDR. You
|
| 175 |
|
|
* must determine BADADDR's validity for these.
|
| 176 |
|
|
*
|
| 177 |
|
|
* Arguments:
|
| 178 |
|
|
* cause: The 5-bit exception cause field of the EXCEPTIONS register,
|
| 179 |
|
|
* shifted to the LSB position. You may pass the 'cause' argument
|
| 180 |
|
|
* in a handler you registered directy to this routine.
|
| 181 |
|
|
*
|
| 182 |
|
|
* Return: 1: BADADDR (bad_addr argument to handler) is valid
|
| 183 |
|
|
* 0: BADADDR is not valid
|
| 184 |
|
|
*/
|
| 185 |
|
|
int alt_exception_cause_generated_bad_addr(alt_exception_cause cause)
|
| 186 |
|
|
{
|
| 187 |
|
|
switch (cause) {
|
| 188 |
|
|
case NIOS2_EXCEPTION_SUPERVISOR_ONLY_DATA_ADDR:
|
| 189 |
|
|
return 1;
|
| 190 |
|
|
case NIOS2_EXCEPTION_MISALIGNED_DATA_ADDR:
|
| 191 |
|
|
return 1;
|
| 192 |
|
|
case NIOS2_EXCEPTION_MISALIGNED_TARGET_PC:
|
| 193 |
|
|
return 1;
|
| 194 |
|
|
case NIOS2_EXCEPTION_TLB_READ_PERM_VIOLATION:
|
| 195 |
|
|
return 1;
|
| 196 |
|
|
case NIOS2_EXCEPTION_TLB_WRITE_PERM_VIOLATION:
|
| 197 |
|
|
return 1;
|
| 198 |
|
|
case NIOS2_EXCEPTION_MPU_DATA_REGION_VIOLATION:
|
| 199 |
|
|
return 1;
|
| 200 |
|
|
default:
|
| 201 |
|
|
return 0;
|
| 202 |
|
|
}
|
| 203 |
|
|
}
|