test_panic.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "riscv/rvruntime-frames.h"
  14. #include "esp_private/panic_internal.h"
  15. #define MCAUSE_ILLEGAL_INSTRUCTION 2
  16. extern void esp_panic_handler(panic_info_t *info);
  17. volatile bool g_override_illegal_instruction = false;
  18. void __real_esp_panic_handler(panic_info_t *info);
  19. void return_from_panic_handler(RvExcFrame *frm) __attribute__((noreturn));
  20. /* Memprot test specific IllegalInstruction exception handler:
  21. * when testing the protection against a code execution, sample code
  22. * is being injected into various memory regions which produces
  23. * Illegal instruction on execution attempt. Such a result is expected
  24. * but it causes system reboot in the standard panic handler.
  25. * The following variant of panic handling simply returns back to the
  26. * next instruction and continues normal execution.
  27. *
  28. * NOTE: if Illegal instruction comes from a different source than the testing code
  29. * the behavior is undefined
  30. * */
  31. void __wrap_esp_panic_handler(panic_info_t *info)
  32. {
  33. RvExcFrame *frm = (RvExcFrame *)info->frame;
  34. if ( frm->mcause == MCAUSE_ILLEGAL_INSTRUCTION && g_override_illegal_instruction == true ) {
  35. /* Return from exception to the return address that called the faulting function.
  36. */
  37. frm->mepc = frm->ra;
  38. /* Restore the CPU state and return from the exception.
  39. */
  40. return_from_panic_handler(frm);
  41. } else {
  42. __real_esp_panic_handler(info);
  43. }
  44. }