memprot.c 13 KB

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