test_panic.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "riscv/rvruntime-frames.h"
  7. #include "esp_private/panic_internal.h"
  8. #define MCAUSE_ILLEGAL_INSTRUCTION 2
  9. extern void esp_panic_handler(panic_info_t *info);
  10. volatile bool g_override_illegal_instruction = false;
  11. void __real_esp_panic_handler(panic_info_t *info);
  12. void __real_esp_cpu_stall(int core_id);
  13. void return_from_panic_handler(RvExcFrame *frm) __attribute__((noreturn));
  14. /* Memprot test specific IllegalInstruction exception handler:
  15. * when testing the protection against a code execution, sample code
  16. * is being injected into various memory regions which produces
  17. * Illegal instruction on execution attempt. Such a result is expected
  18. * but it causes system reboot in the standard panic handler.
  19. * The following variant of panic handling simply returns back to the
  20. * next instruction and continues normal execution.
  21. *
  22. * NOTE: if Illegal instruction comes from a different source than the testing code
  23. * the behavior is undefined
  24. * */
  25. void __wrap_esp_panic_handler(panic_info_t *info)
  26. {
  27. RvExcFrame *frm = (RvExcFrame *)info->frame;
  28. if ( frm->mcause == MCAUSE_ILLEGAL_INSTRUCTION && g_override_illegal_instruction == true ) {
  29. /* Return from exception to the return address that called the faulting function.
  30. */
  31. frm->mepc = frm->ra;
  32. /* Restore the CPU state and return from the exception.
  33. */
  34. return_from_panic_handler(frm);
  35. } else {
  36. __real_esp_panic_handler(info);
  37. }
  38. }
  39. void __wrap_esp_cpu_stall(int core_id)
  40. {
  41. if ( g_override_illegal_instruction == true ) {
  42. return;
  43. } else {
  44. __real_esp_cpu_stall(core_id);
  45. }
  46. }