hw_stack_guard.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "hal/assist_debug_hal.h"
  7. #include "esp_private/hw_stack_guard.h"
  8. #include "esp_private/periph_ctrl.h"
  9. #include "esp_private/startup_internal.h"
  10. #include "soc/soc_caps.h"
  11. #include "esp_rom_sys.h"
  12. #include "esp_cpu.h"
  13. ESP_SYSTEM_INIT_FN(esp_hw_stack_guard_init, ESP_SYSTEM_INIT_ALL_CORES, 101)
  14. {
  15. uint32_t core_id = esp_cpu_get_core_id();
  16. if (core_id == 0) {
  17. /* initialize the peripheral only when running on core 0 */
  18. periph_module_enable(PERIPH_ASSIST_DEBUG_MODULE);
  19. periph_module_reset(PERIPH_ASSIST_DEBUG_MODULE);
  20. }
  21. /* just in case, disable the interrupt and clear pending status */
  22. assist_debug_hal_sp_int_disable(core_id);
  23. assist_debug_hal_sp_int_clear(core_id);
  24. /*
  25. * enable interrupt
  26. * Note: to control hw_stack_guard use monitor enable/disable because in case:
  27. * - monitor == active
  28. * - interrupt != active
  29. * - trigger event happened
  30. * - you get an interrupt right after enabling interrupts
  31. * So, use monitor to disable hw_guard to avoid false-positives.
  32. * And keep interrupt always enabled for better performace (don't spend cpu time for enable/disable)
  33. */
  34. assist_debug_hal_sp_int_enable(core_id);
  35. /* enable interrup routine */
  36. esp_rom_route_intr_matrix(core_id, ETS_ASSIST_DEBUG_INTR_SOURCE, ETS_ASSIST_DEBUG_INUM);
  37. esprv_intc_int_set_type(ETS_ASSIST_DEBUG_INUM, INTR_TYPE_LEVEL);
  38. esprv_intc_int_set_priority(ETS_ASSIST_DEBUG_INUM, SOC_INTERRUPT_LEVEL_MEDIUM);
  39. ESP_INTR_ENABLE(ETS_ASSIST_DEBUG_INUM);
  40. return ESP_OK;
  41. }
  42. /* The functions below are designed to be used in interrupt/panic handler
  43. * In case using them in user's code put them into critical section */
  44. void esp_hw_stack_guard_monitor_start(void)
  45. {
  46. uint32_t core_id = esp_cpu_get_core_id();
  47. /* enable monitor. Interrupt is always enabled (see comment in esp_hw_stack_guard_init()) */
  48. assist_debug_hal_sp_mon_enable(core_id);
  49. }
  50. void esp_hw_stack_guard_monitor_stop(void)
  51. {
  52. uint32_t core_id = esp_cpu_get_core_id();
  53. /* disable monitor. Interrupt is always enabled (see comment in esp_hw_stack_guard_init()) */
  54. assist_debug_hal_sp_mon_disable(core_id);
  55. }
  56. void esp_hw_stack_guard_set_bounds(uint32_t sp_min, uint32_t sp_max)
  57. {
  58. uint32_t core_id = esp_cpu_get_core_id();
  59. assist_debug_hal_set_sp_bounds(core_id, sp_min, sp_max);
  60. }
  61. void esp_hw_stack_guard_get_bounds(uint32_t *sp_min, uint32_t *sp_max)
  62. {
  63. uint32_t core_id = esp_cpu_get_core_id();
  64. assist_debug_hal_get_sp_bounds(core_id, sp_min, sp_max);
  65. }
  66. bool esp_hw_stack_guard_is_fired(void)
  67. {
  68. uint32_t core_id = esp_cpu_get_core_id();
  69. return assist_debug_hal_is_sp_ovf_fired(core_id);
  70. }
  71. uint32_t esp_hw_stack_guard_get_pc(void)
  72. {
  73. uint32_t core_id = esp_cpu_get_core_id();
  74. return assist_debug_hal_get_sp_ovf_pc(core_id);
  75. }