Contiki 3.x
exceptions.c
1 /*
2  * Copyright (c) 2015 NXP B.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of NXP B.V. nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY NXP B.V. AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL NXP B.V. OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Thomas Haydon
32  * Integrated into Contiki by Beshr Al Nahas
33  *
34  */
35 
36 #include <jendefs.h>
37 #include <AppHardwareApi.h>
38 #include <MicroInt.h>
39 #include "exceptions.h"
40 
41 #ifndef EXCEPTION_STALLS_SYSTEM
42 #define EXCEPTION_STALLS_SYSTEM 0
43 #endif /* EXCEPTION_STALLS_SYSTEM */
44 
45 #ifndef PRINT_STACK_ON_REBOOT
46 #define PRINT_STACK_ON_REBOOT 1
47 #endif /* PRINT_STACK_ON_REBOOT */
48 
49 /** Define to dump the stack on exception */
50 #ifndef EXC_DUMP_STACK
51 #define EXC_DUMP_STACK
52 #endif /* EXC_DUMP_STACK */
53 
54 /** Define to dump registers on exception */
55 #ifndef EXC_DUMP_REGS
56 /* #define EXC_DUMP_REGS */
57 #endif /* EXC_DUMP_REGS */
58 
59 /* Select whether exception vectors should be in RAM or Flash based on chip family */
60 #if (defined JENNIC_CHIP_FAMILY_JN514x)
61 #define EXCEPTION_VECTORS_LOCATION_RAM
62 #elif (defined JENNIC_CHIP_FAMILY_JN516x)
63 #define EXCEPTION_VECTORS_LOCATION_FLASH
64 #else
65 #error Unsupported chip family selected
66 #endif /* JENNIC_CHIP_FAMILY */
67 
68 #if (defined EXCEPTION_VECTORS_LOCATION_RAM)
69 /* RAM exception vectors are set up at run time */
70 /* Addresses of exception vectors in RAM */
71 #define BUS_ERROR *((volatile uint32 *)(0x4000000))
72 #define TICK_TIMER *((volatile uint32 *)(0x4000004))
73 #define UNALIGNED_ACCESS *((volatile uint32 *)(0x4000008))
74 #define ILLEGAL_INSTRUCTION *((volatile uint32 *)(0x400000c))
75 #define EXTERNAL_INTERRUPT *((volatile uint32 *)(0x4000010))
76 #define SYSCALL *((volatile uint32 *)(0x4000014))
77 #define TRAP *((volatile uint32 *)(0x4000018))
78 #define GENERIC *((volatile uint32 *)(0x400001c))
79 #define STACK_OVERFLOW *((volatile uint32 *)(0x4000020))
80 #elif (defined EXCEPTION_VECTORS_LOCATION_FLASH)
81 /* Flash exception vectors are set up at compile time */
82 #else
83 #error Unknown exception vector location
84 #endif /* EXCEPTION_VECTORS_LOCATION */
85 
86 /* Locations in stack trace of important information */
87 #define STACK_REG 1
88 #define PROGRAM_COUNTER 18
89 #define EFFECTIVE_ADDR 19
90 
91 /* Number of registers */
92 #define REG_COUNT 16
93 
94 /* Chip dependant RAM size */
95 #if defined(JENNIC_CHIP_JN5148) || defined(JENNIC_CHIP_JN5148J01)
96 #define EXCEPTION_RAM_TOP 0x04020000
97 #else
98 #define EXCEPTION_RAM_TOP 0x04008000
99 #endif
100 
101 static void exception_handler(uint32 *pu32Stack, eExceptionType eType);
102 static void *heap_alloc_overflow_protect(void *pvPointer, uint32 u32Size, bool_t bClear);
103 /*---------------------------------------------------------------------------*/
104 #if PRINT_STACK_ON_REBOOT
105 static void hexprint(uint8 v);
106 static void hexprint32(uint32 v);
107 static void printstring(const char *s);
108 #endif /* PRINT_STACK_ON_REBOOT */
109 
110 /* For debugging */
111 static const char *debug_filename = "nothing";
112 static int debug_line = -1;
113 
114 void
115 debug_file_line(const char *file, int line)
116 {
117  debug_filename = file;
118  debug_line = line;
119 }
120 extern uint32 heap_location;
121 extern void *(*prHeap_AllocFunc)(void *, uint32, bool_t);
122 PRIVATE void *(*prHeap_AllocOrig)(void *, uint32, bool_t);
123 
124 /* Symbol defined by the linker script */
125 /* marks the end of the stack */
126 extern void *stack_low_water_mark;
127 
128 /****************************************************************************/
129 /*** Local Functions ***/
130 /****************************************************************************/
131 /*---------------------------------------------------------------------------*/
132 #if PRINT_STACK_ON_REBOOT
133 #include "dev/uart0.h"
134 #define printchar(X) uart0_write_direct(X)
135 /*---------------------------------------------------------------------------*/
136 static void
137 hexprint(uint8 v)
138 {
139  const char hexconv[] = "0123456789abcdef";
140  printchar(hexconv[v >> 4]);
141  printchar(hexconv[v & 0x0f]);
142 }
143 /*---------------------------------------------------------------------------*/
144 static void
145 hexprint32(uint32 v)
146 {
147  hexprint(((uint32)v) >> (uint32)24);
148  hexprint(((uint32)v) >> (uint32)16);
149  hexprint(((uint32)v) >> (uint32)8);
150  hexprint((v) & 0xff);
151 }
152 /*---------------------------------------------------------------------------*/
153 static void
154 printstring(const char *s)
155 {
156  while(*s) {
157  printchar(*s++);
158  }
159 }
160 #endif /* PRINT_STACK_ON_REBOOT */
161 
162 /****************************************************************************
163  *
164  * NAME: vEXC_Register
165  *
166  * DESCRIPTION:
167  * Set up exceptions. When in RAM, overwrite the default vectors with ours.
168  * We also patch the heap allocation function so that we can keep tabs on
169  * the amount of free heap.
170  *
171  * PARAMETERS: None
172  *
173  * RETURNS:
174  * None
175  *
176  ****************************************************************************/
177 PUBLIC void
178 vEXC_Register(void)
179 {
180 #ifdef EXCEPTION_VECTORS_LOCATION_RAM
181  /* Overwrite exception vectors, pointing them all at the generic handler */
182  BUS_ERROR = (uint32)exception_handler;
183  UNALIGNED_ACCESS = (uint32)exception_handler;
184  ILLEGAL_INSTRUCTION = (uint32)exception_handler;
185  SYSCALL = (uint32)exception_handler;
186  TRAP = (uint32)exception_handler;
187  GENERIC = (uint32)exception_handler;
188  STACK_OVERFLOW = (uint32)exception_handler;
189 #endif /* EXCEPTION_VECTORS_LOCATION */
190 
191  prHeap_AllocOrig = prHeap_AllocFunc;
192  prHeap_AllocFunc = heap_alloc_overflow_protect;
193 }
194 #ifdef EXCEPTION_VECTORS_LOCATION_FLASH
195 /* If exception vectors are in flash, define the handler functions here to be linked in */
196 /* These function names are defined in the 6x linker script for the various exceptions */
197 /* Point them all at the generic handler */
198 PUBLIC void
199 vException_BusError(uint32 *pu32Stack, eExceptionType eType)
200 {
201  exception_handler(pu32Stack, eType);
202 }
203 PUBLIC void
204 vException_UnalignedAccess(uint32 *pu32Stack, eExceptionType eType)
205 {
206  exception_handler(pu32Stack, eType);
207 }
208 PUBLIC void
209 vException_IllegalInstruction(uint32 *pu32Stack, eExceptionType eType)
210 {
211  exception_handler(pu32Stack, eType);
212 }
213 PUBLIC void
214 vException_SysCall(uint32 *pu32Stack, eExceptionType eType)
215 {
216  exception_handler(pu32Stack, eType);
217 }
218 PUBLIC void
219 vException_Trap(uint32 *pu32Stack, eExceptionType eType)
220 {
221  exception_handler(pu32Stack, eType);
222 }
223 PUBLIC void
224 vException_StackOverflow(uint32 *pu32Stack, eExceptionType eType)
225 {
226  exception_handler(pu32Stack, eType);
227 }
228 #endif /* EXCEPTION_VECTORS_LOCATION_FLASH */
229 
230 /****************************************************************************
231  *
232  * NAME: exception_handler
233  *
234  * DESCRIPTION:
235  * Generic exception handler which is called whether the vectors are in RAM or flash
236  *
237  * PARAMETERS: None
238  *
239  * RETURNS:
240  * None
241  *
242  ****************************************************************************/
243 static void
244 exception_handler(uint32 *pu32Stack, eExceptionType eType)
245 {
246 #if (defined EXC_DUMP_STACK) || (defined EXC_DUMP_REGS)
247  int i;
248 #endif
249  uint32 u32EPCR, u32EEAR, u32Stack;
250  char *pcString;
251 
252  MICRO_DISABLE_INTERRUPTS();
253 
254  switch(eType) {
255  case E_EXC_BUS_ERROR:
256  pcString = "BUS";
257  break;
258 
259  case E_EXC_UNALIGNED_ACCESS:
260  pcString = "ALIGN";
261  break;
262 
263  case E_EXC_ILLEGAL_INSTRUCTION:
264  pcString = "ILLEGAL";
265  break;
266 
267  case E_EXC_SYSCALL:
268  pcString = "SYSCALL";
269  break;
270 
271  case E_EXC_TRAP:
272  pcString = "TRAP";
273  break;
274 
275  case E_EXC_GENERIC:
276  pcString = "GENERIC";
277  break;
278 
279  case E_EXC_STACK_OVERFLOW:
280  pcString = "STACK";
281  break;
282 
283  default:
284  pcString = "UNKNOWN";
285  break;
286  }
287 
288  if(bAHI_WatchdogResetEvent()) {
289  pcString = "WATCHDOG";
290  }
291  vAHI_WatchdogStop();
292 
293  /* Pull the EPCR and EEAR values from where they've been saved by the ROM exception handler */
294  u32EPCR = pu32Stack[PROGRAM_COUNTER];
295  u32EEAR = pu32Stack[EFFECTIVE_ADDR];
296  u32Stack = pu32Stack[STACK_REG];
297 
298  /* Log the exception */
299  printstring("\n\n\n");
300  printstring(pcString);
301  printstring(" EXCEPTION @ $");
302  hexprint32(u32EPCR);
303  printstring(" EA: ");
304  hexprint32(u32EEAR);
305  printstring(" SK: ");
306  hexprint32(u32Stack);
307  printstring(" HP: ");
308  hexprint32(((uint32 *)&heap_location)[0]);
309  printstring("\n");
310  printstring(" File: ");
311  printstring(debug_filename);
312  printstring(" Line: ");
313  hexprint32(debug_line);
314  printstring("\n");
315 
316 #ifdef EXC_DUMP_REGS
317  printstring("\nREGS: ");
318  /* Pull and print the registers from saved locations */
319  for(i = 0; i < REG_COUNT; i += 4) {
320  printstring("R");
321  hexprint(i);
322  printstring("-");
323  hexprint(i + 3);
324  printstring(": ");
325  hexprint(pu32Stack[i]);
326  printstring(" ");
327  hexprint32(pu32Stack[i + 1]);
328  printstring(" ");
329  hexprint32(pu32Stack[i + 2]);
330  printstring(" ");
331  hexprint32(pu32Stack[i + 3]);
332  printstring("\n");
333  }
334 #endif
335 
336 #ifdef EXC_DUMP_STACK
337  /* Print the stack */
338  printstring("\nRAM top: ");
339  hexprint32(EXCEPTION_RAM_TOP);
340  printstring("\nSTACK: \n");
341  pu32Stack = (uint32 *)(u32Stack & 0xFFFFFFF0);
342  for(i = 0; (pu32Stack + i) < (uint32 *)(EXCEPTION_RAM_TOP); i += 4) {
343  printstring("@");
344  hexprint32((uint32)(pu32Stack + i));
345  printstring(": ");
346  hexprint32(pu32Stack[i]);
347  printstring(" ");
348  hexprint32(pu32Stack[i + 1]);
349  printstring(" ");
350  hexprint32(pu32Stack[i + 2]);
351  printstring(" ");
352  hexprint32(pu32Stack[i + 3]);
353  printstring("\n");
354  }
355 #endif
356 
357 #if EXCEPTION_STALLS_SYSTEM
358  while(1) {
359  }
360 #else /* EXCEPTION_STALLS_SYSTEM */
361  /* Software reset */
362  vAHI_WatchdogException(0);
363  vAHI_SwReset();
364 #endif /* EXCEPTION_STALLS_SYSTEM */
365 }
366 /****************************************************************************
367  *
368  * NAME: heap_alloc_overflow_protect
369  *
370  * DESCRIPTION:
371  * New heap allocation function that sets the stack overflow location to the new
372  * top address of the heap.
373  *
374  * PARAMETERS: Name RW Usage
375  * pvPointer W Location of allocated heap memory
376  * u32Size R Number of bytes to allocate
377  * bClear R Flag to set new memory to 0
378  *
379  * RETURNS:
380  * Pointer to new memory
381  *
382  ****************************************************************************/
383 static void *
384 heap_alloc_overflow_protect(void *pvPointer, uint32 u32Size, bool_t bClear)
385 {
386  void *pvAlloc;
387  /* Call original heap allocation function */
388  pvAlloc = prHeap_AllocOrig(pvPointer, u32Size, bClear);
389  /*
390  * Initialise the stack overflow exception to trigger if the end of the
391  * stack is reached. See the linker command file to adjust the allocated
392  * stack size.
393  */
394  /* Set stack overflow address */
395  vAHI_SetStackOverflow(TRUE, ((uint32 *)&heap_location)[0]);
396  return pvAlloc;
397 }
#define TRUE
An alias for one, used for clarity.