|
| 1 | +/* RISC-V Physical Memory Protection (PMP) Implementation |
| 2 | + * |
| 3 | + * Provides hardware-enforced memory isolation using PMP in TOR mode. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <hal.h> |
| 7 | +#include <sys/memprot.h> |
| 8 | +#include "csr.h" |
| 9 | +#include "pmp.h" |
| 10 | + |
| 11 | +/* Static Memory Pools for Boot-time PMP Initialization |
| 12 | + * |
| 13 | + * Defines kernel memory regions protected at boot. Each pool specifies |
| 14 | + * a memory range and access permissions. |
| 15 | + */ |
| 16 | +static const mempool_t kernel_mempools[] = { |
| 17 | + DECLARE_MEMPOOL("kernel_text", &_stext, &_etext, PMPCFG_PERM_RX, |
| 18 | + PMP_PRIORITY_KERNEL), |
| 19 | + DECLARE_MEMPOOL("kernel_data", &_sdata, &_edata, PMPCFG_PERM_RW, |
| 20 | + PMP_PRIORITY_KERNEL), |
| 21 | + DECLARE_MEMPOOL("kernel_bss", &_sbss, &_ebss, PMPCFG_PERM_RW, |
| 22 | + PMP_PRIORITY_KERNEL), |
| 23 | + DECLARE_MEMPOOL("kernel_heap", &_heap_start, &_heap_end, PMPCFG_PERM_RW, |
| 24 | + PMP_PRIORITY_KERNEL), |
| 25 | + DECLARE_MEMPOOL("kernel_stack", &_stack_bottom, &_stack_top, PMPCFG_PERM_RW, |
| 26 | + PMP_PRIORITY_KERNEL), |
| 27 | +}; |
| 28 | + |
| 29 | +#define KERNEL_MEMPOOL_COUNT \ |
| 30 | + (sizeof(kernel_mempools) / sizeof(kernel_mempools[0])) |
| 31 | + |
| 32 | +/* Initialize PMP from Memory Pools |
| 33 | + * |
| 34 | + * Configures PMP regions based on static memory pool definitions. |
| 35 | + * Should be called during early boot before task scheduling begins. |
| 36 | + * |
| 37 | + * @config : Pointer to PMP configuration state |
| 38 | + * @pools : Array of memory pool descriptors |
| 39 | + * @count : Number of pools in the array |
| 40 | + * Returns 0 on success, or negative error code on failure. |
| 41 | + */ |
| 42 | +int32_t pmp_init_pools(pmp_config_t *config, const mempool_t *pools, |
| 43 | + size_t count) |
| 44 | +{ |
| 45 | + if (!config || !pools || count == 0) |
| 46 | + return ERR_PMP_INVALID_REGION; |
| 47 | + |
| 48 | + /* Initialize PMP hardware and state */ |
| 49 | + int32_t ret = pmp_init(config); |
| 50 | + if (ret < 0) |
| 51 | + return ret; |
| 52 | + |
| 53 | + /* Configure each memory pool as a PMP region */ |
| 54 | + for (size_t i = 0; i < count; i++) { |
| 55 | + const mempool_t *pool = &pools[i]; |
| 56 | + |
| 57 | + /* Validate pool boundaries */ |
| 58 | + if (pool->start >= pool->end) |
| 59 | + return ERR_PMP_ADDR_RANGE; |
| 60 | + |
| 61 | + /* Prepare PMP region configuration */ |
| 62 | + pmp_region_t region = { |
| 63 | + .addr_start = pool->start, |
| 64 | + .addr_end = pool->end, |
| 65 | + .permissions = pool->flags & (PMPCFG_R | PMPCFG_W | PMPCFG_X), |
| 66 | + .priority = pool->tag, |
| 67 | + .region_id = i, |
| 68 | + .locked = 0, |
| 69 | + }; |
| 70 | + |
| 71 | + /* Configure the PMP region */ |
| 72 | + ret = pmp_set_region(config, ®ion); |
| 73 | + if (ret < 0) |
| 74 | + return ret; |
| 75 | + } |
| 76 | + |
| 77 | + return ERR_OK; |
| 78 | +} |
| 79 | + |
| 80 | +/* Initialize Kernel Memory Protection |
| 81 | + * |
| 82 | + * Convenience function that initializes PMP with default kernel memory |
| 83 | + * pools. Should be called during kernel initialization. |
| 84 | + * |
| 85 | + * @config : Pointer to PMP configuration state |
| 86 | + * Returns 0 on success, or negative error code on failure. |
| 87 | + */ |
| 88 | +int32_t pmp_init_kernel(pmp_config_t *config) |
| 89 | +{ |
| 90 | + return pmp_init_pools(config, kernel_mempools, KERNEL_MEMPOOL_COUNT); |
| 91 | +} |
0 commit comments