esp_memprot.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /////////////////////////////////////////////////////////////////////////////////////////
  7. // ESP Memory Protection API (PMS)
  8. // - allows configuration and violation-interrupt handling of the PMS module operations
  9. // - not intended for public use.
  10. #pragma once
  11. #include "sdkconfig.h"
  12. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE || CONFIG_ESP_SYSTEM_MEMPROT_TEST
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include "esp_err.h"
  16. #include "esp_memprot_err.h"
  17. #include "soc_memprot_types.h"
  18. #include "esp_memprot_types.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define DEFAULT_CPU_NUM -1
  23. #define ESP_MEMPROT_ERR_CHECK(retval, fnc) if ((retval=fnc) != ESP_OK) { return retval; }
  24. /**
  25. * @brief Basic PMS interrupt source info
  26. */
  27. typedef struct {
  28. esp_mprot_mem_t mem_type; /*!< Memory type containing the faulting address */
  29. int core; /*!< CPU/Core ID running the faulting instruction */
  30. } esp_memp_intr_source_t;
  31. /**
  32. * @brief Clears current interrupt ON flag for given Memory type and CPU/Core ID
  33. *
  34. * This operation is non-atomic for some chips by PMS module design
  35. * In such a case the interrupt clearing happens in two steps:
  36. * 1. Interrupt CLR flag is set (clears interrupt-ON status and inhibits linked interrupt processing)
  37. * 2. Interrupt CLR flag is reset (resumes the interrupt monitoring)
  38. *
  39. * @param mem_type Memory type (see esp_mprot_mem_t enum)
  40. * @param core Target CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
  41. *
  42. * @return ESP_OK on success
  43. * ESP_ERR_INVALID_ARG on passing invalid pointer
  44. * ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
  45. */
  46. esp_err_t esp_mprot_monitor_clear_intr(const esp_mprot_mem_t mem_type, const int core);
  47. /**
  48. * @brief Checks whether any of the PMS settings is locked
  49. *
  50. * @param[out] locked Any lock on? (true/false)
  51. *
  52. * @return ESP_OK on success
  53. * ESP_ERR_INVALID_ARG on invalid locked ptr
  54. * Other failures: error code of any failing esp_mprot_get_*_lock() routine (called internally)
  55. */
  56. esp_err_t esp_mprot_is_conf_locked_any(bool *locked);
  57. /**
  58. * @brief Checks whether any PMS violation-interrupt monitoring is enabled
  59. *
  60. * @param[out] locked Any PMS violation interrupt monitor is enabled (true/false)
  61. *
  62. * @return ESP_OK on success
  63. * ESP_ERR_INVALID_ARG on invalid enabled ptr
  64. * Other failures: error code of esp_mprot_get_monitor_en() routine (called internally for all Memory types)
  65. */
  66. esp_err_t esp_mprot_is_intr_ena_any(bool *enabled);
  67. /**
  68. * @brief Returns active PMS violation-interrupt Memory type if any (MEMPROT_TYPE_NONE when none detected)
  69. * and the CPU/CoreID which was running the faulty code (-1 when no interrupt available)
  70. *
  71. * If there are more interrupts indicated on (shouldn't happen), the order of precedence is given by 'esp_mprot_mem_t' enum definition (low->high)
  72. *
  73. * @param[out] mem_type Out-pointer for Memory type given by the faulting address (see esp_mprot_mem_t enum)
  74. * @param[out] core Out-pointer for CPU/Core ID (see *_CPU_NUM defs in soc.h)
  75. *
  76. * @return ESP_OK on success
  77. * ESP_ERR_INVALID_ARG on passing invalid pointer(s)
  78. */
  79. esp_err_t esp_mprot_get_active_intr(esp_memp_intr_source_t *active_memp_intr);
  80. /**
  81. * @brief Returns the address which caused the violation interrupt for given Memory type and CPU/Core ID.
  82. * This function is to be called after a basic resolving of (current) interrupt's parameters (ie corresponding
  83. * Memory type and CPU ID see esp_mprot_get_active_intr()). This is to minimize processing time of actual exception
  84. * as this API is typicaly used in a panic-handling code.
  85. * If there is no active interrupt available for the Memory type/CPU ID required, fault_addr is set to NULL.
  86. *
  87. * @param mem_type memory type
  88. * @param[out] fault_addr Address of the operation which caused the PMS violation interrupt
  89. * @param core Faulting instruction CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
  90. *
  91. * @return ESP_OK on success
  92. * ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
  93. * ESP_ERR_INVALID_ARG on invalid fault_addr pointer
  94. */
  95. esp_err_t esp_mprot_get_violate_addr(const esp_mprot_mem_t mem_type, void **fault_addr, const int core);
  96. /**
  97. * @brief Returns PMS World identifier of the code causing the violation interrupt
  98. *
  99. * The value is read from appropriate PMS violation status register and thus might be 0 if the interrupt is not currently active.
  100. *
  101. * @param mem_type Memory type
  102. * @param[out] world PMS World type (see esp_mprot_pms_world_t)
  103. * @param core Faulting instruction CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
  104. *
  105. * @return ESP_OK on success
  106. * ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
  107. * ESP_ERR_INVALID_ARG on passing invalid pointer(s)
  108. * ESP_ERR_MEMPROT_WORLD_INVALID on invalid World identifier fetched from the register
  109. */
  110. esp_err_t esp_mprot_get_violate_world(const esp_mprot_mem_t mem_type, esp_mprot_pms_world_t *world, const int core);
  111. /**
  112. * @brief Returns an operation type which caused the violation interrupt
  113. *
  114. * The operation resolving is processed over various PMS status register flags, according to given Memory type argument.
  115. * If the interrupt is not active the result returned is irrelevant (likely evaluated to MEMPROT_OP_READ).
  116. *
  117. * @param mem_type Memory type
  118. * @param[out] oper Operation type (see MEMPROT_OP_* defines)
  119. * @param core Faulting instruction CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
  120. *
  121. * @return ESP_OK on success
  122. * ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
  123. * ESP_ERR_INVALID_ARG on invalid oper pointer
  124. */
  125. esp_err_t esp_mprot_get_violate_operation(const esp_mprot_mem_t mem_type, uint32_t *oper, const int core);
  126. /**
  127. * @brief Checks whether given memory type supports byte-enables info
  128. *
  129. * Byte-enables status is available only for DMA/DRAM operations
  130. *
  131. * @param mem_type memory type
  132. *
  133. * @return byte-enables info available true/false
  134. */
  135. bool esp_mprot_has_byte_enables(const esp_mprot_mem_t mem_type);
  136. /**
  137. * @brief Returns byte-enables for the address which caused the violation interrupt
  138. *
  139. * The value is taken from appropriate PMS violation status register, based on given Memory type
  140. *
  141. * @param mem_type Memory type (MEMPROT_TYPE_DRAM0_SRAM)
  142. * @param[out] byte_en Byte-enables bits
  143. * @param core Faulting instruction CPU/Core ID (see *_CPU_NUM defs in soc.h). Can be NULL on 1-CPU systems
  144. *
  145. * @return ESP_OK on success
  146. * ESP_ERR_MEMPROT_MEMORY_TYPE_INVALID on invalid mem_type
  147. * ESP_ERR_INVALID_ARGUMENT on invalid byte_en pointer
  148. */
  149. esp_err_t esp_mprot_get_violate_byte_enables(const esp_mprot_mem_t mem_type, uint32_t *byte_en, const int core);
  150. /**
  151. * @brief Convenient routine for setting the PMS defaults
  152. *
  153. * Called on system startup, depending on ESP_SYSTEM_MEMPROT_FEATURE Kconfig value
  154. *
  155. * @param memp_config pointer to Memprot configuration structure (esp_memp_config_t). The structure si chip-specific,
  156. * for details and defaults see appropriate [target-chip]/soc_memprot_types.h
  157. *
  158. * @return ESP_OK on success
  159. * Other failures: error code of the failing routine called internally. No specific error processing provided in such a case
  160. * due to large number of embedded calls (ie no global unique error table is provided and thus one error code can have different meanings,
  161. * depending on the routine issuing the error)
  162. */
  163. esp_err_t esp_mprot_set_prot(const esp_memp_config_t *memp_config);
  164. /**
  165. * @brief Generates PMS configuration string of actual device (diagnostics)
  166. *
  167. * The functions generates a string from current configuration, control and status registers of the PMS (or similar) module of actual device.
  168. * The values are fetched using HAL LL calls to help finding possible errors in the Memprot API implementation
  169. *
  170. * @param[out] dump_info_string configuration string buffer pointer. The string is allocated by the callee and must be freed by the caller.
  171. *
  172. * @return ESP_OK on success
  173. * ESP_ERR_NO_MEM on buffer allocation failure
  174. * ESP_ERR_INVALID_ARGUMENT on invalid dump_info_string pointer
  175. */
  176. esp_err_t esp_mprot_dump_configuration(char **dump_info_string);
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif //CONFIG_ESP_SYSTEM_MEMPROT_FEATURE || CONFIG_ESP_SYSTEM_MEMPROT_TEST