Memory Protection Unit (MPU)

IC:

Introduction

The Memory Protection Unit (MPU) is a component provided by Arm and is used to provide hardware protection by software definition. The SDK provides the mpu_region_config struct to set the region memory attribute of MPU.

The following table lists the member variables of the mpu_region_config struct.

Member variable name

Type

Description

region_base

uint32_t

MPU region base address, 32 bytes aligned

region_size

uint32_t

MPU region size, 32 bytes aligned

xn

uint8_t

Execute Never attribute

  • MPU_EXEC_ALLOW: Allows program execution in this region

  • MPU_EXEC_NEVER: Does not allow program execution in this region

ap

uint8_t

Access permissions

  • MPU_PRIV_RW: Read/write by privileged code only

  • MPU_UN_PRIV_RW: Read/write by any privilege level

  • MPU_PRIV_R: Read only by privileged code only

  • MPU_PRIV_W: Read only by any privilege level

sh

uint8_t

Shareability of Normal memory

  • MPU_NON_SHAREABLE: Non-shareable

  • MPU_OUT_SHAREABLE: Outer shareable

  • MPU_INR_SHAREABLE: Inner shareable

attr_idx

uint8_t

Memory attribute indirect index

This parameter can be a value of 0 ~ 7, with detailed attributes defined in mpu_init() and customizable. The typical definition is as follows:

  • 0: MPU_MEM_ATTR_IDX_NC, defines memory attribute of Normal memory as non-cacheable.

  • 1: MPU_MEM_ATTR_IDX_WT_T_RA, defines memory attribute of Normal memory as write-through transient, read allocation.

  • 2: MPU_MEM_ATTR_IDX_WB_T_RWA, defines memory attribute of Normal memory as write-back transient, read and write allocation.

  • 3 ~ 7: MPU_MEM_ATTR_IDX_DEVICE, defines memory attribute of Device memory as non-gathering, non-recording, non-early Write Acknowledge.

MPU APIs

mpu_init

Items

Description

Introduction

Initialize MPU region memory attribute to typical value

Parameters

None

Return

None

mpu_set_mem_attr

Items

Description

Introduction

Change MPU region memory attribute

Parameters

  • attr_idx: region memory attribute index, which can be 0 ~ 7.

  • mem_attr: region memory attributes.

Return

None

mpu_region_cfg

Items

Description

Introduction

Configure MPU region memory attribute.

Parameters

  • region_num:

    • KM4_NS: 0 ~ 7

    • KM4_S: 0 ~ 3

  • pmpu_cfg: point to the mpu_region_config struct which has been configured

Return

None

mpu_entry_free

Items

Description

Introduction

Free MPU entry

Parameters

MPU entry index:

  • KM4_NS: 0 ~ 7

  • KM4_S: 0 ~ 3

Return

None

mpu_entry_alloc

Items

Description

Introduction

Allocate a free MPU entry

Parameters

None

Return

MPU entry index:

  • KM4_NS: 0 ~ 7

  • KM4_S: 0 ~ 3

  • Fail: -1

Usage

Follow these steps to set an MPU region:

  1. Define new variable and struct

    • Define a variable to store MPU entry index

    • Define a struct mpu_region_config to store the region memory attribute

  2. Call mpu_entry_alloc() to allocate a free MPU entry

  3. Set the struct for region memory attribute

  4. Call mpu_region_cfg() to configure MPU region memory attribute

Note

For KR4, a physical memory protection (PMP) unit is implemented, but the code in SDK does not use PMP to provide access privileges control. Refer to RISC-V privileged architecture proposal for the physical memory protection scheme to set PMP when access privileges control is needed.