|
| 1 | +/* RISC-V Physical Memory Protection (PMP) Hardware Layer |
| 2 | + * |
| 3 | + * Low-level interface to RISC-V PMP using TOR (Top-of-Range) mode for |
| 4 | + * flexible region management without alignment constraints. |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <types.h> |
| 10 | + |
| 11 | +/* PMP Region Priority Levels (lower value = higher priority) |
| 12 | + * |
| 13 | + * Used for eviction decisions when hardware PMP regions are exhausted. |
| 14 | + */ |
| 15 | +typedef enum { |
| 16 | + PMP_PRIORITY_KERNEL = 0, |
| 17 | + PMP_PRIORITY_STACK = 1, |
| 18 | + PMP_PRIORITY_SHARED = 2, |
| 19 | + PMP_PRIORITY_TEMPORARY = 3, |
| 20 | + PMP_PRIORITY_COUNT = 4 |
| 21 | +} pmp_priority_t; |
| 22 | + |
| 23 | +/* PMP Region Configuration */ |
| 24 | +typedef struct { |
| 25 | + uint32_t addr_start; /* Start address (inclusive) */ |
| 26 | + uint32_t addr_end; /* End address (exclusive, written to pmpaddr) */ |
| 27 | + uint8_t permissions; /* R/W/X bits (PMPCFG_R | PMPCFG_W | PMPCFG_X) */ |
| 28 | + pmp_priority_t priority; /* Eviction priority */ |
| 29 | + uint8_t region_id; /* Hardware region index (0-15) */ |
| 30 | + uint8_t locked; /* Lock bit (cannot modify until reset) */ |
| 31 | +} pmp_region_t; |
| 32 | + |
| 33 | +/* PMP Global State */ |
| 34 | +typedef struct { |
| 35 | + pmp_region_t regions[PMP_MAX_REGIONS]; /* Shadow of hardware config */ |
| 36 | + uint8_t region_count; /* Active region count */ |
| 37 | + uint8_t next_region_idx; /* Next free region index */ |
| 38 | + uint32_t initialized; /* Initialization flag */ |
| 39 | +} pmp_config_t; |
| 40 | + |
| 41 | +/* PMP Management Functions */ |
| 42 | + |
| 43 | +/* Initializes the PMP hardware and configuration state. |
| 44 | + * @config : Pointer to pmp_config_t structure to be initialized. |
| 45 | + * Returns 0 on success, or negative error code on failure. |
| 46 | + */ |
| 47 | +int32_t pmp_init(pmp_config_t *config); |
| 48 | + |
| 49 | +/* Configures a single PMP region in TOR mode. |
| 50 | + * @config : Pointer to PMP configuration state |
| 51 | + * @region : Pointer to pmp_region_t structure with desired configuration |
| 52 | + * Returns 0 on success, or negative error code on failure. |
| 53 | + */ |
| 54 | +int32_t pmp_set_region(pmp_config_t *config, const pmp_region_t *region); |
| 55 | + |
| 56 | +/* Reads the current configuration of a PMP region. |
| 57 | + * @config : Pointer to PMP configuration state |
| 58 | + * @region_idx : Index of the region to read (0-15) |
| 59 | + * @region : Pointer to pmp_region_t to store the result |
| 60 | + * Returns 0 on success, or negative error code on failure. |
| 61 | + */ |
| 62 | +int32_t pmp_get_region(const pmp_config_t *config, uint8_t region_idx, |
| 63 | + pmp_region_t *region); |
| 64 | + |
| 65 | +/* Disables a PMP region. |
| 66 | + * @config : Pointer to PMP configuration state |
| 67 | + * @region_idx : Index of the region to disable (0-15) |
| 68 | + * Returns 0 on success, or negative error code on failure. |
| 69 | + */ |
| 70 | +int32_t pmp_disable_region(pmp_config_t *config, uint8_t region_idx); |
| 71 | + |
| 72 | +/* Locks a PMP region to prevent further modification. |
| 73 | + * @config : Pointer to PMP configuration state |
| 74 | + * @region_idx : Index of the region to lock (0-15) |
| 75 | + * Returns 0 on success, or negative error code on failure. |
| 76 | + */ |
| 77 | +int32_t pmp_lock_region(pmp_config_t *config, uint8_t region_idx); |
| 78 | + |
| 79 | +/* Verifies that a memory access is allowed by the current PMP configuration. |
| 80 | + * @config : Pointer to PMP configuration state |
| 81 | + * @addr : Address to check |
| 82 | + * @size : Size of the access in bytes |
| 83 | + * @is_write : 1 for write access, 0 for read access |
| 84 | + * @is_execute : 1 for execute access, 0 for data access |
| 85 | + * Returns 1 if access is allowed, 0 if denied, or negative error code. |
| 86 | + */ |
| 87 | +int32_t pmp_check_access(const pmp_config_t *config, uint32_t addr, |
| 88 | + uint32_t size, uint8_t is_write, uint8_t is_execute); |
0 commit comments