memprot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // Copyright 2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /* INTERNAL API
  15. * implementation of generic interface to MMU memory protection features
  16. */
  17. #include <stdio.h>
  18. #include "sdkconfig.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "esp_system.h"
  22. #include "esp_spi_flash.h"
  23. #include "soc/sensitive_reg.h"
  24. #include "soc/dport_access.h"
  25. #include "soc/periph_defs.h"
  26. #include "esp_intr_alloc.h"
  27. #include "esp32s2/memprot.h"
  28. #include "hal/memprot_ll.h"
  29. #include "esp_fault.h"
  30. #include "esp_log.h"
  31. extern int _iram_text_end;
  32. extern int _data_start;
  33. static const char *TAG = "memprot";
  34. uint32_t *esp_memprot_iram0_get_min_split_addr(void)
  35. {
  36. return (uint32_t *)&_iram_text_end;
  37. }
  38. uint32_t *esp_memprot_dram0_get_min_split_addr(void)
  39. {
  40. return (uint32_t *)&_data_start;
  41. }
  42. uint32_t *esp_memprot_get_split_addr(mem_type_prot_t mem_type)
  43. {
  44. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  45. switch (mem_type) {
  46. case MEMPROT_IRAM0:
  47. return esp_memprot_iram0_get_min_split_addr();
  48. case MEMPROT_DRAM0:
  49. return esp_memprot_dram0_get_min_split_addr();
  50. default:
  51. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  52. abort();
  53. }
  54. }
  55. const char *esp_memprot_type_to_str(mem_type_prot_t mem_type)
  56. {
  57. switch (mem_type) {
  58. case MEMPROT_IRAM0:
  59. return "IRAM0";
  60. case MEMPROT_DRAM0:
  61. return "DRAM0";
  62. default:
  63. return "UNKOWN";
  64. }
  65. }
  66. void esp_memprot_intr_init(mem_type_prot_t mem_type)
  67. {
  68. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  69. ESP_INTR_DISABLE(ETS_MEMACCESS_ERR_INUM);
  70. switch (mem_type) {
  71. case MEMPROT_IRAM0:
  72. intr_matrix_set(PRO_CPU_NUM, esp_memprot_iram0_get_intr_source_num(), ETS_MEMACCESS_ERR_INUM);
  73. break;
  74. case MEMPROT_DRAM0:
  75. intr_matrix_set(PRO_CPU_NUM, esp_memprot_dram0_get_intr_source_num(), ETS_MEMACCESS_ERR_INUM);
  76. break;
  77. default:
  78. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  79. abort();
  80. }
  81. ESP_INTR_ENABLE(ETS_MEMACCESS_ERR_INUM);
  82. }
  83. void esp_memprot_intr_ena(mem_type_prot_t mem_type, bool enable)
  84. {
  85. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  86. switch (mem_type) {
  87. case MEMPROT_IRAM0:
  88. esp_memprot_iram0_intr_ena(enable);
  89. break;
  90. case MEMPROT_DRAM0:
  91. esp_memprot_dram0_intr_ena(enable);
  92. break;
  93. default:
  94. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  95. abort();
  96. }
  97. }
  98. bool esp_memprot_is_assoc_intr_any()
  99. {
  100. return esp_memprot_iram0_is_assoc_intr() || esp_memprot_dram0_is_assoc_intr();
  101. }
  102. mem_type_prot_t esp_memprot_get_intr_memtype()
  103. {
  104. if ( esp_memprot_is_assoc_intr(MEMPROT_IRAM0) ) {
  105. return MEMPROT_IRAM0;
  106. } else if ( esp_memprot_is_assoc_intr(MEMPROT_DRAM0) ) {
  107. return MEMPROT_DRAM0;
  108. }
  109. return MEMPROT_UNKNOWN;
  110. }
  111. bool esp_memprot_is_assoc_intr(mem_type_prot_t mem_type)
  112. {
  113. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  114. switch (mem_type) {
  115. case MEMPROT_IRAM0:
  116. return esp_memprot_iram0_is_assoc_intr();
  117. case MEMPROT_DRAM0:
  118. return esp_memprot_dram0_is_assoc_intr();
  119. default:
  120. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  121. abort();
  122. }
  123. }
  124. void esp_memprot_clear_intr(mem_type_prot_t mem_type)
  125. {
  126. switch (mem_type) {
  127. case MEMPROT_IRAM0:
  128. esp_memprot_iram0_clear_intr();
  129. break;
  130. case MEMPROT_DRAM0:
  131. esp_memprot_dram0_clear_intr();
  132. break;
  133. default:
  134. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  135. abort();
  136. }
  137. }
  138. void esp_memprot_set_lock(mem_type_prot_t mem_type)
  139. {
  140. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  141. switch (mem_type) {
  142. case MEMPROT_IRAM0:
  143. esp_memprot_iram0_set_lock();
  144. break;
  145. case MEMPROT_DRAM0:
  146. esp_memprot_dram0_set_lock();
  147. break;
  148. default:
  149. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  150. abort();
  151. }
  152. }
  153. bool esp_memprot_get_lock(mem_type_prot_t mem_type)
  154. {
  155. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  156. switch (mem_type) {
  157. case MEMPROT_IRAM0:
  158. return esp_memprot_iram0_get_lock_reg() > 0;
  159. case MEMPROT_DRAM0:
  160. return esp_memprot_dram0_get_lock_reg() > 0;
  161. default:
  162. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  163. abort();
  164. }
  165. }
  166. bool esp_memprot_is_locked_any()
  167. {
  168. return esp_memprot_iram0_get_lock_reg() > 0 || esp_memprot_iram0_get_lock_reg() > 0;
  169. }
  170. uint32_t esp_memprot_get_lock_bit(mem_type_prot_t mem_type)
  171. {
  172. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  173. switch (mem_type) {
  174. case MEMPROT_IRAM0:
  175. return esp_memprot_iram0_get_lock_bit();
  176. case MEMPROT_DRAM0:
  177. return esp_memprot_dram0_get_lock_bit();
  178. default:
  179. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  180. abort();
  181. }
  182. }
  183. uint32_t esp_memprot_get_ena_reg(mem_type_prot_t mem_type)
  184. {
  185. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  186. switch (mem_type) {
  187. case MEMPROT_IRAM0:
  188. return esp_memprot_iram0_get_ena_reg();
  189. case MEMPROT_DRAM0:
  190. return esp_memprot_dram0_get_ena_reg();
  191. default:
  192. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  193. abort();
  194. }
  195. }
  196. uint32_t esp_memprot_get_fault_reg(mem_type_prot_t mem_type)
  197. {
  198. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  199. switch (mem_type) {
  200. case MEMPROT_IRAM0:
  201. return esp_memprot_iram0_get_fault_reg();
  202. case MEMPROT_DRAM0:
  203. return esp_memprot_dram0_get_fault_reg();
  204. default:
  205. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  206. abort();
  207. }
  208. }
  209. void esp_memprot_get_fault_status(mem_type_prot_t mem_type, uint32_t **faulting_address, uint32_t *op_type, uint32_t *op_subtype)
  210. {
  211. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  212. switch (mem_type) {
  213. case MEMPROT_IRAM0:
  214. esp_memprot_iram0_get_fault_status(faulting_address, op_type, op_subtype);
  215. break;
  216. case MEMPROT_DRAM0:
  217. esp_memprot_dram0_get_fault_status(faulting_address, op_type, op_subtype);
  218. break;
  219. default:
  220. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  221. abort();
  222. }
  223. }
  224. bool esp_memprot_is_intr_ena_any()
  225. {
  226. return esp_memprot_iram0_get_intr_ena_bit() > 0 || esp_memprot_dram0_get_intr_ena_bit() > 0;
  227. }
  228. uint32_t esp_memprot_get_intr_ena_bit(mem_type_prot_t mem_type)
  229. {
  230. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  231. switch (mem_type) {
  232. case MEMPROT_IRAM0:
  233. return esp_memprot_iram0_get_intr_ena_bit();
  234. case MEMPROT_DRAM0:
  235. return esp_memprot_dram0_get_intr_ena_bit();
  236. default:
  237. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  238. abort();
  239. }
  240. }
  241. uint32_t esp_memprot_get_intr_on_bit(mem_type_prot_t mem_type)
  242. {
  243. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  244. switch (mem_type) {
  245. case MEMPROT_IRAM0:
  246. return esp_memprot_iram0_get_intr_on_bit();
  247. case MEMPROT_DRAM0:
  248. return esp_memprot_dram0_get_intr_on_bit();
  249. default:
  250. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  251. abort();
  252. }
  253. }
  254. uint32_t esp_memprot_get_intr_clr_bit(mem_type_prot_t mem_type)
  255. {
  256. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  257. switch (mem_type) {
  258. case MEMPROT_IRAM0:
  259. return esp_memprot_iram0_get_intr_clr_bit();
  260. case MEMPROT_DRAM0:
  261. return esp_memprot_dram0_get_intr_clr_bit();
  262. default:
  263. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  264. abort();
  265. }
  266. }
  267. uint32_t esp_memprot_get_uni_block_read_bit(mem_type_prot_t mem_type, uint32_t block)
  268. {
  269. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  270. switch (mem_type) {
  271. case MEMPROT_IRAM0:
  272. return esp_memprot_iram0_get_uni_block_read_bit(block);
  273. case MEMPROT_DRAM0:
  274. return esp_memprot_dram0_get_uni_block_read_bit(block);
  275. default:
  276. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  277. abort();
  278. }
  279. }
  280. uint32_t esp_memprot_get_uni_block_write_bit(mem_type_prot_t mem_type, uint32_t block)
  281. {
  282. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  283. switch (mem_type) {
  284. case MEMPROT_IRAM0:
  285. return esp_memprot_iram0_get_uni_block_write_bit(block);
  286. case MEMPROT_DRAM0:
  287. return esp_memprot_dram0_get_uni_block_write_bit(block);
  288. default:
  289. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  290. abort();
  291. }
  292. }
  293. uint32_t esp_memprot_get_uni_block_exec_bit(mem_type_prot_t mem_type, uint32_t block)
  294. {
  295. assert(mem_type == MEMPROT_IRAM0);
  296. switch (mem_type) {
  297. case MEMPROT_IRAM0:
  298. return esp_memprot_iram0_get_uni_block_exec_bit(block);
  299. default:
  300. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  301. abort();
  302. }
  303. }
  304. void esp_memprot_set_uni_block_perm_dram(mem_type_prot_t mem_type, uint32_t block, bool write_perm, bool read_perm)
  305. {
  306. assert(mem_type == MEMPROT_DRAM0);
  307. switch (mem_type) {
  308. case MEMPROT_DRAM0:
  309. esp_memprot_dram0_set_uni_block_perm(block, write_perm, read_perm);
  310. break;
  311. default:
  312. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  313. abort();
  314. }
  315. }
  316. uint32_t esp_memprot_get_perm_uni_reg(mem_type_prot_t mem_type)
  317. {
  318. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  319. switch (mem_type) {
  320. case MEMPROT_IRAM0:
  321. return esp_memprot_iram0_get_perm_uni_reg();
  322. case MEMPROT_DRAM0:
  323. return esp_memprot_dram0_get_perm_reg();
  324. default:
  325. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  326. abort();
  327. }
  328. }
  329. uint32_t esp_memprot_get_perm_split_reg(mem_type_prot_t mem_type)
  330. {
  331. assert(mem_type == MEMPROT_IRAM0 || mem_type == MEMPROT_DRAM0);
  332. switch (mem_type) {
  333. case MEMPROT_IRAM0:
  334. return esp_memprot_iram0_get_perm_split_reg();
  335. case MEMPROT_DRAM0:
  336. return esp_memprot_dram0_get_perm_reg();
  337. default:
  338. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  339. abort();
  340. }
  341. }
  342. void esp_memprot_set_prot_dram(mem_type_prot_t mem_type, uint32_t *split_addr, bool lw, bool lr, bool hw, bool hr)
  343. {
  344. assert(mem_type == MEMPROT_DRAM0);
  345. switch (mem_type) {
  346. case MEMPROT_DRAM0:
  347. esp_memprot_dram0_set_prot(split_addr != NULL ? split_addr : esp_memprot_dram0_get_min_split_addr(), lw, lr, hw, hr);
  348. break;
  349. default:
  350. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  351. abort();
  352. }
  353. }
  354. void esp_memprot_set_uni_block_perm_iram(mem_type_prot_t mem_type, uint32_t block, bool write_perm, bool read_perm, bool exec_perm)
  355. {
  356. assert(mem_type == MEMPROT_IRAM0);
  357. switch (mem_type) {
  358. case MEMPROT_IRAM0:
  359. esp_memprot_iram0_set_uni_block_perm(block, write_perm, read_perm, exec_perm);
  360. break;
  361. default:
  362. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  363. abort();
  364. }
  365. }
  366. void esp_memprot_set_prot_iram(mem_type_prot_t mem_type, uint32_t *split_addr, bool lw, bool lr, bool lx, bool hw, bool hr, bool hx)
  367. {
  368. assert(mem_type == MEMPROT_IRAM0);
  369. switch (mem_type) {
  370. case MEMPROT_IRAM0:
  371. esp_memprot_iram0_set_prot(split_addr != NULL ? split_addr : esp_memprot_iram0_get_min_split_addr(), lw, lr, lx, hw, hr, hx);
  372. break;
  373. default:
  374. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  375. abort();
  376. }
  377. }
  378. void esp_memprot_get_perm_split_bits_iram(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *lx, bool *hw, bool *hr, bool *hx)
  379. {
  380. assert(mem_type == MEMPROT_IRAM0);
  381. switch (mem_type) {
  382. case MEMPROT_IRAM0:
  383. esp_memprot_iram0_get_split_sgnf_bits(lw, lr, lx, hw, hr, hx);
  384. break;
  385. default:
  386. assert(0);
  387. }
  388. }
  389. void esp_memprot_get_perm_split_bits_dram(mem_type_prot_t mem_type, bool *lw, bool *lr, bool *hw, bool *hr)
  390. {
  391. assert(mem_type == MEMPROT_DRAM0);
  392. switch (mem_type) {
  393. case MEMPROT_DRAM0:
  394. esp_memprot_dram0_get_split_sgnf_bits(lw, lr, hw, hr);
  395. break;
  396. default:
  397. ESP_LOGE(TAG, "Invalid mem_type %d", mem_type);
  398. abort();
  399. }
  400. }
  401. void esp_memprot_set_prot(bool invoke_panic_handler, bool lock_feature)
  402. {
  403. esp_memprot_intr_ena(MEMPROT_DRAM0, false);
  404. esp_memprot_intr_ena(MEMPROT_IRAM0, false);
  405. if (!esp_cpu_in_ocd_debug_mode()) {
  406. ESP_FAULT_ASSERT(!esp_cpu_in_ocd_debug_mode());
  407. if ( invoke_panic_handler ) {
  408. esp_memprot_intr_init(MEMPROT_DRAM0);
  409. esp_memprot_intr_init(MEMPROT_IRAM0);
  410. }
  411. esp_memprot_set_prot_dram(MEMPROT_DRAM0, NULL, false, true, true, true);
  412. esp_memprot_set_prot_iram(MEMPROT_IRAM0, NULL, false, true, true, true, true, false);
  413. esp_memprot_intr_ena(MEMPROT_DRAM0, true);
  414. esp_memprot_intr_ena(MEMPROT_IRAM0, true);
  415. if ( lock_feature ) {
  416. esp_memprot_set_lock(MEMPROT_DRAM0);
  417. esp_memprot_set_lock(MEMPROT_IRAM0);
  418. }
  419. }
  420. }