2021-08-10 06:43:44 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
/* INTERNAL API
|
|
|
|
* implementation of PMS memory protection features
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "soc/sensitive_reg.h"
|
|
|
|
#include "soc/dport_access.h"
|
|
|
|
#include "soc/periph_defs.h"
|
|
|
|
#include "esp_intr_alloc.h"
|
|
|
|
#include "hal/memprot_ll.h"
|
2021-01-27 16:03:07 -05:00
|
|
|
#include "esp32c3/memprot.h"
|
2021-01-04 13:38:10 -05:00
|
|
|
#include "riscv/interrupt.h"
|
2021-01-27 16:03:07 -05:00
|
|
|
#include "esp32c3/rom/ets_sys.h"
|
2021-01-04 13:38:10 -05:00
|
|
|
#include "esp_log.h"
|
2021-10-04 00:37:56 -04:00
|
|
|
#include "esp_fault.h"
|
2021-04-21 16:56:20 -04:00
|
|
|
#include "soc/cpu.h"
|
|
|
|
|
2021-01-04 13:38:10 -05:00
|
|
|
extern int _iram_text_end;
|
2021-01-27 16:03:07 -05:00
|
|
|
static const char *TAG = "memprot";
|
2021-01-04 13:38:10 -05:00
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
const char *esp_memprot_mem_type_to_str(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
switch (mem_type) {
|
|
|
|
case MEMPROT_NONE:
|
2021-01-27 16:03:07 -05:00
|
|
|
return "NONE";
|
2021-01-04 13:38:10 -05:00
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return "IRAM0_SRAM";
|
2021-01-04 13:38:10 -05:00
|
|
|
case MEMPROT_DRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return "DRAM0_SRAM";
|
2021-01-04 13:38:10 -05:00
|
|
|
case MEMPROT_ALL:
|
2021-01-27 16:03:07 -05:00
|
|
|
return "ALL";
|
2021-01-04 13:38:10 -05:00
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
const char *esp_memprot_split_line_to_str(split_line_t line_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
switch (line_type) {
|
|
|
|
case MEMPROT_IRAM0_DRAM0_SPLITLINE:
|
|
|
|
return "MEMPROT_IRAM0_DRAM0_SPLITLINE";
|
|
|
|
case MEMPROT_IRAM0_LINE_0_SPLITLINE:
|
|
|
|
return "MEMPROT_IRAM0_LINE_0_SPLITLINE";
|
|
|
|
case MEMPROT_IRAM0_LINE_1_SPLITLINE:
|
|
|
|
return "MEMPROT_IRAM0_LINE_1_SPLITLINE";
|
|
|
|
case MEMPROT_DRAM0_DMA_LINE_0_SPLITLINE:
|
|
|
|
return "MEMPROT_DRAM0_DMA_LINE_0_SPLITLINE";
|
|
|
|
case MEMPROT_DRAM0_DMA_LINE_1_SPLITLINE:
|
|
|
|
return "MEMPROT_DRAM0_DMA_LINE_1_SPLITLINE";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
const char *esp_memprot_pms_to_str(pms_area_t area_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
switch (area_type) {
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_0:
|
|
|
|
return "MEMPROT_IRAM0_PMS_AREA_0";
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_1:
|
|
|
|
return "MEMPROT_IRAM0_PMS_AREA_1";
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_2:
|
|
|
|
return "MEMPROT_IRAM0_PMS_AREA_2";
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_3:
|
|
|
|
return "MEMPROT_IRAM0_PMS_AREA_3";
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_0:
|
|
|
|
return "MEMPROT_DRAM0_PMS_AREA_0";
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_1:
|
|
|
|
return "MEMPROT_DRAM0_PMS_AREA_1";
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_2:
|
|
|
|
return "MEMPROT_DRAM0_PMS_AREA_2";
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_3:
|
|
|
|
return "MEMPROT_DRAM0_PMS_AREA_3";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* split lines */
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
void *esp_memprot_get_default_main_split_addr()
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
return &_iram_text_end;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
uint32_t *esp_memprot_get_split_addr(split_line_t line_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
switch ( line_type ) {
|
2021-10-04 00:37:56 -04:00
|
|
|
case MEMPROT_IRAM0_DRAM0_SPLITLINE:
|
|
|
|
return memprot_ll_get_iram0_split_line_main_I_D();
|
|
|
|
case MEMPROT_IRAM0_LINE_0_SPLITLINE:
|
|
|
|
return memprot_ll_get_iram0_split_line_I_0();
|
|
|
|
case MEMPROT_IRAM0_LINE_1_SPLITLINE:
|
|
|
|
return memprot_ll_get_iram0_split_line_I_1();
|
|
|
|
case MEMPROT_DRAM0_DMA_LINE_0_SPLITLINE:
|
|
|
|
return memprot_ll_get_dram0_split_line_D_0();
|
|
|
|
case MEMPROT_DRAM0_DMA_LINE_1_SPLITLINE:
|
|
|
|
return memprot_ll_get_dram0_split_line_D_1();
|
|
|
|
default:
|
|
|
|
abort();
|
2021-01-27 16:03:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_set_split_line_lock()
|
|
|
|
{
|
|
|
|
memprot_ll_set_iram0_dram0_split_line_lock();
|
2021-01-04 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_memprot_get_split_line_lock()
|
|
|
|
{
|
|
|
|
return memprot_ll_get_iram0_dram0_split_line_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_set_split_line(split_line_t line_type, const void *line_addr)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "Setting split line %s, addr: 0x%08X", esp_memprot_split_line_to_str(line_type), (uint32_t)line_addr);
|
2021-01-04 13:38:10 -05:00
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
//split-line must be divisible by 512 (PMS module restriction)
|
|
|
|
assert( ((uint32_t)line_addr) % 0x200 == 0 );
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( line_type ) {
|
|
|
|
case MEMPROT_IRAM0_DRAM0_SPLITLINE:
|
|
|
|
memprot_ll_set_iram0_split_line_main_I_D(line_addr);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_LINE_0_SPLITLINE:
|
|
|
|
memprot_ll_set_iram0_split_line_I_0(line_addr);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_LINE_1_SPLITLINE:
|
|
|
|
memprot_ll_set_iram0_split_line_I_1(line_addr);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_DMA_LINE_0_SPLITLINE:
|
|
|
|
memprot_ll_set_dram0_split_line_D_0(line_addr);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_DMA_LINE_1_SPLITLINE:
|
|
|
|
memprot_ll_set_dram0_split_line_D_1(line_addr);
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid split line type, aborting: 0x%08X", (uint32_t)line_addr);
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* PMS */
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
void esp_memprot_set_pms_lock(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_set_pms_lock(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
memprot_ll_iram0_set_pms_lock();
|
2021-01-04 13:38:10 -05:00
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
memprot_ll_dram0_set_pms_lock();
|
2021-01-04 13:38:10 -05:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_memprot_get_pms_lock(mem_type_prot_t mem_type)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_get_pms_lock(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
return memprot_ll_iram0_get_pms_lock();
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
return memprot_ll_dram0_get_pms_lock();
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_iram_set_pms_area(pms_area_t area_type, bool r, bool w, bool x)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_iram_set_pms_area(area:%s r:%u w:%u, x:%u)", esp_memprot_pms_to_str(area_type), r, w, x);
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( area_type ) {
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_0:
|
|
|
|
memprot_ll_iram0_set_pms_area_0(r, w, x);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_1:
|
|
|
|
memprot_ll_iram0_set_pms_area_1(r, w, x);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_2:
|
|
|
|
memprot_ll_iram0_set_pms_area_2(r, w, x);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_3:
|
|
|
|
memprot_ll_iram0_set_pms_area_3(r, w, x);
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid area_type %d", esp_memprot_pms_to_str(area_type));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_iram_get_pms_area(pms_area_t area_type, bool *r, bool *w, bool *x)
|
|
|
|
{
|
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_iram_get_pms_area(area:%s r:%u w:%u)", esp_memprot_pms_to_str(area_type), r, w);
|
|
|
|
|
|
|
|
switch ( area_type ) {
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_0:
|
|
|
|
memprot_ll_iram0_get_pms_area_0(r, w, x);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_1:
|
|
|
|
memprot_ll_iram0_get_pms_area_1(r, w, x);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_2:
|
|
|
|
memprot_ll_iram0_get_pms_area_2(r, w, x);
|
|
|
|
break;
|
|
|
|
case MEMPROT_IRAM0_PMS_AREA_3:
|
|
|
|
memprot_ll_iram0_get_pms_area_3(r, w, x);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid area_type %d", esp_memprot_pms_to_str(area_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_dram_set_pms_area(pms_area_t area_type, bool r, bool w)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_dram_set_pms_area(area:%s r:%u w:%u)", esp_memprot_pms_to_str(area_type), r, w);
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( area_type ) {
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_0:
|
|
|
|
memprot_ll_dram0_set_pms_area_0(r, w);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_1:
|
|
|
|
memprot_ll_dram0_set_pms_area_1(r, w);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_2:
|
|
|
|
memprot_ll_dram0_set_pms_area_2(r, w);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_3:
|
|
|
|
memprot_ll_dram0_set_pms_area_3(r, w);
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid area_type %d", esp_memprot_pms_to_str(area_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
void esp_memprot_dram_get_pms_area(pms_area_t area_type, bool *r, bool *w)
|
|
|
|
{
|
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_dram_get_pms_area(area:%s r:%u w:%u)", esp_memprot_pms_to_str(area_type), r, w);
|
|
|
|
|
|
|
|
switch ( area_type ) {
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_0:
|
|
|
|
memprot_ll_dram0_get_pms_area_0(r, w);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_1:
|
|
|
|
memprot_ll_dram0_get_pms_area_1(r, w);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_2:
|
|
|
|
memprot_ll_dram0_get_pms_area_2(r, w);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_PMS_AREA_3:
|
|
|
|
memprot_ll_dram0_get_pms_area_3(r, w);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid area_type %d", esp_memprot_pms_to_str(area_type));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* monitor */
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
void esp_memprot_set_monitor_lock(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_set_monitor_lock(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
memprot_ll_iram0_set_monitor_lock();
|
2021-01-04 13:38:10 -05:00
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
memprot_ll_dram0_set_monitor_lock();
|
2021-01-04 13:38:10 -05:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_memprot_get_monitor_lock(mem_type_prot_t mem_type)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_get_monitor_lock(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
return memprot_ll_iram0_get_monitor_lock();
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
return memprot_ll_dram0_get_monitor_lock();
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_set_monitor_en(mem_type_prot_t mem_type, bool enable)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_set_monitor_en(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
memprot_ll_iram0_set_monitor_en(enable);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
memprot_ll_dram0_set_monitor_en(enable);
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_memprot_get_monitor_en(mem_type_prot_t mem_type)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_set_monitor_en(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
return memprot_ll_iram0_get_monitor_en();
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
return memprot_ll_dram0_get_monitor_en();
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_memprot_is_intr_ena_any()
|
|
|
|
{
|
|
|
|
return esp_memprot_get_monitor_en(MEMPROT_IRAM0_SRAM) || esp_memprot_get_monitor_en(MEMPROT_DRAM0_SRAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_monitor_clear_intr(mem_type_prot_t mem_type)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_monitor_clear_intr(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
memprot_ll_iram0_clear_monitor_intr();
|
2021-01-27 16:03:07 -05:00
|
|
|
memprot_ll_iram0_reset_clear_monitor_intr();
|
2021-01-04 13:38:10 -05:00
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
memprot_ll_dram0_clear_monitor_intr();
|
2021-01-27 16:03:07 -05:00
|
|
|
memprot_ll_dram0_reset_clear_monitor_intr();
|
2021-01-04 13:38:10 -05:00
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_type_prot_t esp_memprot_get_active_intr_memtype()
|
|
|
|
{
|
|
|
|
if ( memprot_ll_iram0_get_monitor_status_intr() > 0 ) {
|
|
|
|
return MEMPROT_IRAM0_SRAM;
|
|
|
|
} else if ( memprot_ll_dram0_get_monitor_status_intr() ) {
|
|
|
|
return MEMPROT_DRAM0_SRAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MEMPROT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_memprot_is_locked_any()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
esp_memprot_get_split_line_lock() ||
|
|
|
|
esp_memprot_get_pms_lock(MEMPROT_IRAM0_SRAM) ||
|
|
|
|
esp_memprot_get_pms_lock(MEMPROT_DRAM0_SRAM) ||
|
|
|
|
esp_memprot_get_monitor_lock(MEMPROT_IRAM0_SRAM) ||
|
|
|
|
esp_memprot_get_monitor_lock(MEMPROT_DRAM0_SRAM);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
bool esp_memprot_get_violate_intr_on(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return memprot_ll_iram0_get_monitor_status_intr() == 1;
|
2021-01-04 13:38:10 -05:00
|
|
|
case MEMPROT_DRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return memprot_ll_dram0_get_monitor_status_intr() == 1;
|
2021-01-04 13:38:10 -05:00
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_memprot_get_violate_addr(mem_type_prot_t mem_type)
|
|
|
|
{
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
return memprot_ll_iram0_get_monitor_status_fault_addr();
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
return memprot_ll_dram0_get_monitor_status_fault_addr();
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
pms_world_t esp_memprot_get_violate_world(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
uint32_t world = 0;
|
|
|
|
|
2021-01-04 13:38:10 -05:00
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
world = memprot_ll_iram0_get_monitor_status_fault_world();
|
|
|
|
break;
|
2021-01-04 13:38:10 -05:00
|
|
|
case MEMPROT_DRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
world = memprot_ll_dram0_get_monitor_status_fault_world();
|
|
|
|
break;
|
2021-01-04 13:38:10 -05:00
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
2021-01-27 16:03:07 -05:00
|
|
|
|
|
|
|
switch ( world ) {
|
2021-10-04 00:37:56 -04:00
|
|
|
case 0x01: return MEMPROT_PMS_WORLD_0;
|
|
|
|
case 0x10: return MEMPROT_PMS_WORLD_1;
|
|
|
|
default: return MEMPROT_PMS_WORLD_INVALID;
|
2021-01-27 16:03:07 -05:00
|
|
|
}
|
2021-01-04 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
pms_operation_type_t esp_memprot_get_violate_wr(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return memprot_ll_iram0_get_monitor_status_fault_wr() == 1 ? MEMPROT_PMS_OP_WRITE : MEMPROT_PMS_OP_READ;
|
2021-01-04 13:38:10 -05:00
|
|
|
case MEMPROT_DRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return memprot_ll_dram0_get_monitor_status_fault_wr() == 1 ? MEMPROT_PMS_OP_WRITE : MEMPROT_PMS_OP_READ;
|
2021-01-04 13:38:10 -05:00
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
bool esp_memprot_get_violate_loadstore(mem_type_prot_t mem_type)
|
2021-01-04 13:38:10 -05:00
|
|
|
{
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
2021-01-27 16:03:07 -05:00
|
|
|
return memprot_ll_iram0_get_monitor_status_fault_loadstore() == 1;
|
2021-01-04 13:38:10 -05:00
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_memprot_get_violate_byte_en(mem_type_prot_t mem_type)
|
|
|
|
{
|
|
|
|
switch ( mem_type ) {
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
return memprot_ll_dram0_get_monitor_status_fault_byte_en();
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_memprot_intr_get_cpuid()
|
|
|
|
{
|
|
|
|
return PRO_CPU_NUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_set_intr_matrix(mem_type_prot_t mem_type)
|
|
|
|
{
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_set_intr_matrix(%s)", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
ESP_INTR_DISABLE(ETS_MEMPROT_ERR_INUM);
|
|
|
|
|
|
|
|
switch (mem_type) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
intr_matrix_set(esp_memprot_intr_get_cpuid(), memprot_ll_iram0_get_intr_source_num(), ETS_MEMPROT_ERR_INUM);
|
|
|
|
break;
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
intr_matrix_set(esp_memprot_intr_get_cpuid(), memprot_ll_dram0_get_intr_source_num(), ETS_MEMPROT_ERR_INUM);
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-27 16:03:07 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "Invalid mem_type (%s), aborting", esp_memprot_mem_type_to_str(mem_type));
|
2021-01-04 13:38:10 -05:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the type and priority to cache error interrupts. */
|
|
|
|
esprv_intc_int_set_type(BIT(ETS_MEMPROT_ERR_INUM), INTR_TYPE_LEVEL);
|
|
|
|
esprv_intc_int_set_priority(ETS_MEMPROT_ERR_INUM, SOC_INTERRUPT_LEVEL_MEDIUM);
|
|
|
|
|
|
|
|
ESP_INTR_ENABLE(ETS_MEMPROT_ERR_INUM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_set_prot(bool invoke_panic_handler, bool lock_feature, uint32_t *mem_type_mask)
|
|
|
|
{
|
|
|
|
esp_memprot_set_prot_int(invoke_panic_handler, lock_feature, NULL, mem_type_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_memprot_set_prot_int(bool invoke_panic_handler, bool lock_feature, void *split_addr, uint32_t *mem_type_mask)
|
|
|
|
{
|
2021-03-18 07:08:47 -04:00
|
|
|
ESP_EARLY_LOGD(TAG, "esp_memprot_set_prot(panic_handler: %u, lock: %u, split.addr: 0x%08X, mem.types: 0x%08X", invoke_panic_handler, lock_feature, (uint32_t)split_addr, (uint32_t)mem_type_mask);
|
2021-01-04 13:38:10 -05:00
|
|
|
|
|
|
|
uint32_t required_mem_prot = mem_type_mask == NULL ? (uint32_t)MEMPROT_ALL : *mem_type_mask;
|
|
|
|
bool use_iram0 = required_mem_prot & MEMPROT_IRAM0_SRAM;
|
|
|
|
bool use_dram0 = required_mem_prot & MEMPROT_DRAM0_SRAM;
|
|
|
|
|
|
|
|
if (required_mem_prot == MEMPROT_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//disable protection
|
|
|
|
if (use_iram0) {
|
|
|
|
esp_memprot_set_monitor_en(MEMPROT_IRAM0_SRAM, false);
|
|
|
|
}
|
|
|
|
if (use_dram0) {
|
|
|
|
esp_memprot_set_monitor_en(MEMPROT_DRAM0_SRAM, false);
|
|
|
|
}
|
|
|
|
|
2021-10-04 00:37:56 -04:00
|
|
|
//if being debugged check we are not glitched and dont enable Memprot
|
2021-04-21 16:56:20 -04:00
|
|
|
if (esp_cpu_in_ocd_debug_mode()) {
|
2021-10-04 00:37:56 -04:00
|
|
|
ESP_FAULT_ASSERT(esp_cpu_in_ocd_debug_mode());
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (invoke_panic_handler) {
|
|
|
|
if (use_iram0) {
|
|
|
|
esp_memprot_set_intr_matrix(MEMPROT_IRAM0_SRAM);
|
|
|
|
}
|
|
|
|
if (use_dram0) {
|
|
|
|
esp_memprot_set_intr_matrix(MEMPROT_DRAM0_SRAM);
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 16:56:20 -04:00
|
|
|
|
2021-10-04 00:37:56 -04:00
|
|
|
//set split lines (must-have for all mem_types)
|
|
|
|
const void *line_addr = split_addr == NULL ? esp_memprot_get_default_main_split_addr() : split_addr;
|
|
|
|
esp_memprot_set_split_line(MEMPROT_IRAM0_LINE_1_SPLITLINE, line_addr);
|
|
|
|
esp_memprot_set_split_line(MEMPROT_IRAM0_LINE_0_SPLITLINE, line_addr);
|
|
|
|
esp_memprot_set_split_line(MEMPROT_IRAM0_DRAM0_SPLITLINE, line_addr);
|
|
|
|
esp_memprot_set_split_line(MEMPROT_DRAM0_DMA_LINE_0_SPLITLINE,
|
|
|
|
(void *) (MAP_IRAM_TO_DRAM((uint32_t) line_addr)));
|
|
|
|
esp_memprot_set_split_line(MEMPROT_DRAM0_DMA_LINE_1_SPLITLINE,
|
|
|
|
(void *) (MAP_IRAM_TO_DRAM((uint32_t) line_addr)));
|
|
|
|
|
|
|
|
//set permissions
|
|
|
|
if (required_mem_prot & MEMPROT_IRAM0_SRAM) {
|
|
|
|
esp_memprot_iram_set_pms_area(MEMPROT_IRAM0_PMS_AREA_0, true, false, true);
|
|
|
|
esp_memprot_iram_set_pms_area(MEMPROT_IRAM0_PMS_AREA_1, true, false, true);
|
|
|
|
esp_memprot_iram_set_pms_area(MEMPROT_IRAM0_PMS_AREA_2, true, false, true);
|
|
|
|
esp_memprot_iram_set_pms_area(MEMPROT_IRAM0_PMS_AREA_3, true, true, false);
|
2021-01-04 13:38:10 -05:00
|
|
|
}
|
2021-10-04 00:37:56 -04:00
|
|
|
if (required_mem_prot & MEMPROT_DRAM0_SRAM) {
|
|
|
|
esp_memprot_dram_set_pms_area(MEMPROT_DRAM0_PMS_AREA_0, true, false);
|
|
|
|
esp_memprot_dram_set_pms_area(MEMPROT_DRAM0_PMS_AREA_1, true, true);
|
|
|
|
esp_memprot_dram_set_pms_area(MEMPROT_DRAM0_PMS_AREA_2, true, true);
|
|
|
|
esp_memprot_dram_set_pms_area(MEMPROT_DRAM0_PMS_AREA_3, true, true);
|
2021-01-04 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
2021-10-04 00:37:56 -04:00
|
|
|
//reenable the protection
|
2021-01-04 13:38:10 -05:00
|
|
|
if (use_iram0) {
|
2021-10-04 00:37:56 -04:00
|
|
|
esp_memprot_monitor_clear_intr(MEMPROT_IRAM0_SRAM);
|
|
|
|
esp_memprot_set_monitor_en(MEMPROT_IRAM0_SRAM, true);
|
2021-01-04 13:38:10 -05:00
|
|
|
}
|
|
|
|
if (use_dram0) {
|
2021-10-04 00:37:56 -04:00
|
|
|
esp_memprot_monitor_clear_intr(MEMPROT_DRAM0_SRAM);
|
|
|
|
esp_memprot_set_monitor_en(MEMPROT_DRAM0_SRAM, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//lock if required
|
|
|
|
if (lock_feature) {
|
|
|
|
esp_memprot_set_split_line_lock();
|
|
|
|
if (use_iram0) {
|
|
|
|
esp_memprot_set_pms_lock(MEMPROT_IRAM0_SRAM);
|
|
|
|
esp_memprot_set_monitor_lock(MEMPROT_IRAM0_SRAM);
|
|
|
|
}
|
|
|
|
if (use_dram0) {
|
|
|
|
esp_memprot_set_pms_lock(MEMPROT_DRAM0_SRAM);
|
|
|
|
esp_memprot_set_monitor_lock(MEMPROT_DRAM0_SRAM);
|
|
|
|
}
|
2021-01-04 13:38:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 16:03:07 -05:00
|
|
|
|
|
|
|
uint32_t esp_memprot_get_dram_status_reg_1()
|
|
|
|
{
|
|
|
|
return memprot_ll_dram0_get_monitor_status_register_1();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_memprot_get_dram_status_reg_2()
|
|
|
|
{
|
|
|
|
return memprot_ll_dram0_get_monitor_status_register_2();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_memprot_get_iram_status_reg()
|
|
|
|
{
|
|
|
|
return memprot_ll_iram0_get_monitor_status_register();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t esp_memprot_get_monitor_enable_reg(mem_type_prot_t mem_type)
|
|
|
|
{
|
|
|
|
switch (mem_type) {
|
|
|
|
case MEMPROT_IRAM0_SRAM:
|
|
|
|
return memprot_ll_iram0_get_monitor_enable_register();
|
|
|
|
case MEMPROT_DRAM0_SRAM:
|
|
|
|
return memprot_ll_dram0_get_monitor_enable_register();
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|