esp_memprot.h 8.2 KB

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