1
0

armtarget.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* armtarget.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include <wolfssl/wolfcrypt/settings.h>
  22. #include <wolfssl/ssl.h>
  23. #include <wolfssl/wolfcrypt/random.h> /* for CUSTOM_RAND_TYPE */
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #ifdef USE_WOLF_ARM_STARTUP
  29. /* Memory initialization */
  30. extern uint32_t __data_load_start__[];
  31. extern uint32_t __data_start__[];
  32. extern uint32_t __data_end__[];
  33. extern uint32_t __bss_start__[];
  34. extern uint32_t __bss_end__[];
  35. extern uint32_t __stack_process_end__[];
  36. extern uint32_t __heap_start__[];
  37. extern uint32_t __heap_end__[];
  38. /* Copy memory: src=Source, dst_beg=Destination Begin, dst_end=Destination End */
  39. void memcpy32(uint32_t* src, uint32_t* dst_beg, uint32_t* dst_end)
  40. {
  41. while (dst_beg < dst_end) {
  42. *dst_beg++ = *src++;
  43. }
  44. }
  45. /* Zero address in range */
  46. void meminit32(uint32_t* start, uint32_t* end)
  47. {
  48. while (start < end) {
  49. *start++ = 0;
  50. }
  51. }
  52. #endif /* USE_WOLF_ARM_STARTUP */
  53. /* Entry Point */
  54. void reset_handler(void)
  55. {
  56. #ifdef USE_WOLF_ARM_STARTUP
  57. /* Init sections */
  58. memcpy32(__data_load_start__, __data_start__, __data_end__);
  59. meminit32(__bss_start__, __bss_end__);
  60. /* Init heap */
  61. __heap_start__[0] = 0;
  62. __heap_start__[1] = ((uintptr_t)__heap_end__ - (uintptr_t)__heap_start__);
  63. #endif /* USE_WOLF_ARM_STARTUP */
  64. /* Start main */
  65. extern int main(void);
  66. main();
  67. /* Application has ended, so busy wait */
  68. while(1);
  69. }
  70. #ifdef USE_WOLF_ARM_STARTUP
  71. // Vector Exception/Interrupt Handlers
  72. static void Default_Handler(void)
  73. {
  74. /* If we get here then need to implement real IRQ handler */
  75. while(1);
  76. }
  77. __attribute__((section(".sys"))) __attribute__ ((used))
  78. void HardFault_HandlerC( uint32_t *hardfault_args )
  79. {
  80. /* These are volatile to try and prevent the compiler/linker optimizing them
  81. away as the variables never actually get used. If the debugger won't show the
  82. values of the variables, make them global my moving their declaration outside
  83. of this function. */
  84. volatile uint32_t stacked_r0;
  85. volatile uint32_t stacked_r1;
  86. volatile uint32_t stacked_r2;
  87. volatile uint32_t stacked_r3;
  88. volatile uint32_t stacked_r12;
  89. volatile uint32_t stacked_lr;
  90. volatile uint32_t stacked_pc;
  91. volatile uint32_t stacked_psr;
  92. volatile uint32_t _CFSR;
  93. volatile uint32_t _HFSR;
  94. volatile uint32_t _DFSR;
  95. volatile uint32_t _AFSR;
  96. volatile uint32_t _BFAR;
  97. volatile uint32_t _MMAR;
  98. stacked_r0 = ((uint32_t)hardfault_args[0]);
  99. stacked_r1 = ((uint32_t)hardfault_args[1]);
  100. stacked_r2 = ((uint32_t)hardfault_args[2]);
  101. stacked_r3 = ((uint32_t)hardfault_args[3]);
  102. stacked_r12 = ((uint32_t)hardfault_args[4]);
  103. stacked_lr = ((uint32_t)hardfault_args[5]);
  104. stacked_pc = ((uint32_t)hardfault_args[6]);
  105. stacked_psr = ((uint32_t)hardfault_args[7]);
  106. // Configurable Fault Status Register
  107. // Consists of MMSR, BFSR and UFSR
  108. _CFSR = (*((volatile uint32_t *)(0xE000ED28)));
  109. // Hard Fault Status Register
  110. _HFSR = (*((volatile uint32_t *)(0xE000ED2C)));
  111. // Debug Fault Status Register
  112. _DFSR = (*((volatile uint32_t *)(0xE000ED30)));
  113. // Auxiliary Fault Status Register
  114. _AFSR = (*((volatile uint32_t *)(0xE000ED3C)));
  115. // Read the Fault Address Registers. These may not contain valid values.
  116. // Check BFARVALID/MMARVALID to see if they are valid values
  117. // MemManage Fault Address Register
  118. _MMAR = (*((volatile uint32_t *)(0xE000ED34)));
  119. // Bus Fault Address Register
  120. _BFAR = (*((volatile uint32_t *)(0xE000ED38)));
  121. printf ("\n\nHard fault handler (all numbers in hex):\n");
  122. printf ("R0 = %lx\n", stacked_r0);
  123. printf ("R1 = %lx\n", stacked_r1);
  124. printf ("R2 = %lx\n", stacked_r2);
  125. printf ("R3 = %lx\n", stacked_r3);
  126. printf ("R12 = %lx\n", stacked_r12);
  127. printf ("LR [R14] = %lx subroutine call return address\n", stacked_lr);
  128. printf ("PC [R15] = %lx program counter\n", stacked_pc);
  129. printf ("PSR = %lx\n", stacked_psr);
  130. printf ("CFSR = %lx\n", _CFSR);
  131. printf ("HFSR = %lx\n", _HFSR);
  132. printf ("DFSR = %lx\n", _DFSR);
  133. printf ("AFSR = %lx\n", _AFSR);
  134. printf ("MMAR = %lx\n", _MMAR);
  135. printf ("BFAR = %lx\n", _BFAR);
  136. // Break into the debugger
  137. __asm("BKPT #0\n");
  138. }
  139. __attribute__((section(".sys"))) __attribute__( ( naked ) )
  140. void HardFault_Handler(void)
  141. {
  142. __asm volatile
  143. (
  144. " movs r0,#4 \n" /* load bit mask into R0 */
  145. " mov r1, lr \n" /* load link register into R1 */
  146. " tst r0, r1 \n" /* compare with bitmask */
  147. " beq _MSP \n" /* if bitmask is set: stack pointer is in PSP. Otherwise in MSP */
  148. " mrs r0, psp \n" /* otherwise: stack pointer is in PSP */
  149. " b _GetPC \n" /* go to part which loads the PC */
  150. "_MSP: \n" /* stack pointer is in MSP register */
  151. " mrs r0, msp \n" /* load stack pointer into R0 */
  152. "_GetPC: \n" /* find out where the hard fault happened */
  153. " ldr r1,[r0,#20] \n" /* load program counter into R1. R1 contains address of the next instruction where the hard fault happened */
  154. " ldr r2, =HardFault_HandlerC \n"
  155. " bx r2 \n"
  156. " bx lr \n" /* decode more information. R0 contains pointer to stack frame */
  157. );
  158. }
  159. /* Vectors Table */
  160. typedef void (*vector_entry)(void);
  161. const vector_entry vectors[] __attribute__ ((section(".vectors"),used)) =
  162. {
  163. /* Interrupt Vector Table Function Pointers */
  164. // Address Vector IRQ Source module Source description
  165. (vector_entry)__stack_process_end__, // ARM core Initial Supervisor SP
  166. reset_handler, // 0x0000_0004 1 - ARM core Initial Program Counter
  167. Default_Handler, // 0x0000_0008 2 - ARM core Non-maskable Interrupt (NMI)
  168. HardFault_Handler, // 0x0000_000C 3 - ARM core Hard Fault
  169. Default_Handler, // 0x0000_0010 4 -
  170. HardFault_Handler, // 0x0000_0014 5 - ARM core Bus Fault
  171. HardFault_Handler, // 0x0000_0018 6 - ARM core Usage Fault
  172. Default_Handler, // 0x0000_001C 7 -
  173. Default_Handler, // 0x0000_0020 8 -
  174. Default_Handler, // 0x0000_0024 9 -
  175. Default_Handler, // 0x0000_0028 10 -
  176. Default_Handler, // 0x0000_002C 11 - ARM core Supervisor call (SVCall)
  177. Default_Handler, // 0x0000_0030 12 - ARM core Debug Monitor
  178. Default_Handler, // 0x0000_0034 13 -
  179. Default_Handler, // 0x0000_0038 14 - ARM core Pendable request for system service (PendableSrvReq)
  180. Default_Handler, // 0x0000_003C 15 - ARM core System tick timer (SysTick)
  181. /* remainder go below */
  182. };
  183. #endif /* USE_WOLF_ARM_STARTUP */