Skip to content

Commit 9447bc9

Browse files
committed
Implement PMP region load and evict operations
Add functions to dynamically load flexpages into hardware PMP regions and evict them when no longer needed. These operations bridge the software flexpage abstraction with hardware PMP configuration, enabling runtime memory protection management.
1 parent e9f764e commit 9447bc9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

kernel/memprot.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,62 @@ fpage_t *select_victim_fpage(memspace_t *mspace)
6666

6767
return victim;
6868
}
69+
70+
/* Loads a flexpage into a PMP hardware region.
71+
*
72+
* @fpage : Pointer to flexpage to load
73+
* @region_idx : Hardware PMP region index (0-15)
74+
* Returns 0 on success, or negative error code on failure.
75+
*/
76+
int32_t pmp_load_fpage(fpage_t *fpage, uint8_t region_idx)
77+
{
78+
if (!fpage)
79+
return -1;
80+
81+
pmp_config_t *config = pmp_get_config();
82+
if (!config)
83+
return -1;
84+
85+
/* Configure PMP region from flexpage attributes */
86+
pmp_region_t region = {
87+
.addr_start = fpage->base,
88+
.addr_end = fpage->base + fpage->size,
89+
.permissions = fpage->rwx,
90+
.priority = fpage->priority,
91+
.region_id = region_idx,
92+
.locked = 0,
93+
};
94+
95+
int32_t ret = pmp_set_region(config, &region);
96+
if (ret == 0) {
97+
fpage->pmp_id = region_idx;
98+
}
99+
100+
return ret;
101+
}
102+
103+
/* Evicts a flexpage from its PMP hardware region.
104+
*
105+
* @fpage : Pointer to flexpage to evict
106+
* Returns 0 on success, or negative error code on failure.
107+
*/
108+
int32_t pmp_evict_fpage(fpage_t *fpage)
109+
{
110+
if (!fpage)
111+
return -1;
112+
113+
/* Only evict if actually loaded into PMP */
114+
if (fpage->pmp_id == 0)
115+
return 0;
116+
117+
pmp_config_t *config = pmp_get_config();
118+
if (!config)
119+
return -1;
120+
121+
int32_t ret = pmp_disable_region(config, fpage->pmp_id);
122+
if (ret == 0) {
123+
fpage->pmp_id = 0;
124+
}
125+
126+
return ret;
127+
}

0 commit comments

Comments
 (0)