test_panic.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/xtensa_context.h"
  7. #include "esp_private/panic_internal.h"
  8. #include "hal/wdt_hal.h"
  9. extern void esp_panic_handler(panic_info_t *info);
  10. extern volatile bool g_override_illegal_instruction;
  11. void __real_esp_panic_handler(panic_info_t *info);
  12. void __real_esp_panic_handler_reconfigure_wdts(void);
  13. void __real_esp_cpu_stall(int core_id);
  14. static void disable_all_wdts(void)
  15. {
  16. wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  17. #if SOC_TIMER_GROUPS >= 2
  18. wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
  19. #endif
  20. //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
  21. //Task WDT is the Main Watchdog Timer of Timer Group 0
  22. wdt_hal_write_protect_disable(&wdt0_context);
  23. wdt_hal_disable(&wdt0_context);
  24. wdt_hal_write_protect_enable(&wdt0_context);
  25. #if SOC_TIMER_GROUPS >= 2
  26. //Interupt WDT is the Main Watchdog Timer of Timer Group 1
  27. wdt_hal_write_protect_disable(&wdt1_context);
  28. wdt_hal_disable(&wdt1_context);
  29. wdt_hal_write_protect_enable(&wdt1_context);
  30. #endif
  31. }
  32. /* Memprot test specific IllegalInstruction exception handler:
  33. * when testing the protection against a code execution, sample code
  34. * is being injected into various memory regions which produces
  35. * EXCCAUSE_ILLEGAL on execution attempt. Such a result is expected
  36. * but it causes system reboot in the standard panic handler.
  37. * The following variant of panic handling simply returns back to the
  38. * next instruction and continues normal execution.
  39. *
  40. * NOTE: if EXCCAUSE_ILLEGAL comes from a different source than the testing code
  41. * the behavior is undefined
  42. * */
  43. void __wrap_esp_panic_handler(panic_info_t *info)
  44. {
  45. XtExcFrame *frm = (XtExcFrame *)info->frame;
  46. if ( frm->exccause == EXCCAUSE_ILLEGAL && g_override_illegal_instruction == true ) {
  47. frm->pc = frm->a0;
  48. return;
  49. } else {
  50. __real_esp_panic_handler(info);
  51. }
  52. }
  53. void __wrap_esp_panic_handler_reconfigure_wdts(void)
  54. {
  55. if ( g_override_illegal_instruction == true ) {
  56. disable_all_wdts();
  57. return;
  58. } else {
  59. __real_esp_panic_handler_reconfigure_wdts();
  60. }
  61. }
  62. void __wrap_esp_cpu_stall(int core_id)
  63. {
  64. if ( g_override_illegal_instruction == true ) {
  65. return;
  66. } else {
  67. __real_esp_cpu_stall(core_id);
  68. }
  69. }