PMPs

API for Configuring Physical Memory Protection on RISC-V Cores.

The Physical Memory Protection (PMP) interface on RISC-V cores is a form of memory protection unit which allows for a finite number of physical memory regions to be configured with certain access permissions.

Additional information about the use and configuration rules for PMPs can be found by reading the RISC-V Privileged Architecture Specification.

Enums

enum metal_pmp_address_mode

Set of available PMP addressing modes.

Values:

enumerator METAL_PMP_OFF

Disable the PMP region.

enumerator METAL_PMP_TOR

Use Top-of-Range mode.

enumerator METAL_PMP_NA4

Use naturally-aligned 4-byte region mode.

enumerator METAL_PMP_NAPOT

Use naturally-aligned power-of-two mode.

Functions

struct metal_pmp *metal_pmp_get_device(void)

Get the PMP device handle.

int metal_pmp_num_regions(int hartid)

Get the number of pmp regions for the hartid.

void metal_pmp_init(struct metal_pmp *pmp)

Initialize the PMP.

The PMP initialization routine is optional and may be called as many times as is desired. The effect of the initialization routine is to attempt to set all regions to unlocked and disabled, as well as to clear the X, W, and R bits. Only the pmp configuration of the hart which executes the routine will be affected.

Parameters
  • pmp: The PMP device handle to be initialized

If any regions are fused to preset values by the implementation or locked, those PMP regions will silently remain uninitialized.

int metal_pmp_set_region(struct metal_pmp *pmp, unsigned int region, struct metal_pmp_config config, size_t address)

Configure a PMP region.

Return

0 upon success

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to configure

  • config: The desired configuration of the PMP region

  • address: The desired address of the PMP region

int metal_pmp_get_region(struct metal_pmp *pmp, unsigned int region, struct metal_pmp_config *config, size_t *address)

Get the configuration for a PMP region.

Return

0 if the region is read successfully

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to read

  • config: Variable to store the PMP region configuration

  • address: Variable to store the PMP region address

int metal_pmp_lock(struct metal_pmp *pmp, unsigned int region)

Lock a PMP region.

Return

0 if the region is successfully locked

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to lock

int metal_pmp_set_address(struct metal_pmp *pmp, unsigned int region, size_t address)

Set the address for a PMP region.

Return

0 if the address is successfully set

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to set

  • address: The desired address of the PMP region

size_t metal_pmp_get_address(struct metal_pmp *pmp, unsigned int region)

Get the address of a PMP region.

Return

The address of the PMP region, or 0 if the region could not be read

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to read

int metal_pmp_set_address_mode(struct metal_pmp *pmp, unsigned int region, enum metal_pmp_address_mode mode)

Set the addressing mode of a PMP region.

Return

0 if the addressing mode is successfully set

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to set

  • mode: The PMP addressing mode to set

enum metal_pmp_address_mode metal_pmp_get_address_mode(struct metal_pmp *pmp, unsigned int region)

Get the addressing mode of a PMP region.

Return

The address mode of the PMP region

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to read

int metal_pmp_set_executable(struct metal_pmp *pmp, unsigned int region, int X)

Set the executable bit for a PMP region.

Return

0 if the executable bit is successfully set

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to set

  • X: The desired value of the executable bit

int metal_pmp_get_executable(struct metal_pmp *pmp, unsigned int region)

Get the executable bit for a PMP region.

Return

the value of the executable bit

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to read

int metal_pmp_set_writeable(struct metal_pmp *pmp, unsigned int region, int W)

Set the writable bit for a PMP region.

Return

0 if the writable bit is successfully set

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to set

  • W: The desired value of the writable bit

int metal_pmp_get_writeable(struct metal_pmp *pmp, unsigned int region)

Get the writable bit for a PMP region.

Return

the value of the writable bit

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to read

int metal_pmp_set_readable(struct metal_pmp *pmp, unsigned int region, int R)

Set the readable bit for a PMP region.

Return

0 if the readable bit is successfully set

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to set

  • R: The desired value of the readable bit

int metal_pmp_get_readable(struct metal_pmp *pmp, unsigned int region)

Set the readable bit for a PMP region.

Return

the value of the readable bit

Parameters
  • pmp: The PMP device handle

  • region: The PMP region to read

struct metal_pmp_config
#include <pmp.h>

Configuration for a PMP region.

Public Types

enum metal_pmp_locked

Sets whether the PMP region is locked.

Values:

enumerator METAL_PMP_UNLOCKED
enumerator METAL_PMP_LOCKED

Public Members

unsigned int R

Sets whether reads to the PMP region succeed.

unsigned int W

Sets whether writes to the PMP region succeed.

unsigned int X

Sets whether the PMP region is executable.

enum metal_pmp_address_mode A

Sets the addressing mode of the PMP region.

enum metal_pmp_config::metal_pmp_locked L
struct metal_pmp
#include <pmp.h>

A handle for the PMP device.