Skip to content

Commit e9f764e

Browse files
committed
Implement priority-based victim selection
Add flexpage selection algorithm that identifies eviction candidates when hardware PMP regions are exhausted. The algorithm selects the flexpage with the highest priority value (lowest importance), while protecting kernel regions (priority 0) from eviction.
1 parent 1606385 commit e9f764e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

kernel/memprot.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <lib/libc.h>
99
#include <lib/malloc.h>
10+
#include <pmp.h>
1011
#include <sys/memprot.h>
1112

1213
/* Creates and initializes a flexpage */
@@ -40,3 +41,28 @@ void mo_fpage_destroy(fpage_t *fpage)
4041

4142
free(fpage);
4243
}
44+
45+
/* Selects victim flexpage for eviction using priority-based algorithm.
46+
*
47+
* @mspace : Pointer to memory space
48+
* Returns pointer to victim flexpage, or NULL if no evictable page found.
49+
*/
50+
fpage_t *select_victim_fpage(memspace_t *mspace)
51+
{
52+
if (!mspace)
53+
return NULL;
54+
55+
fpage_t *victim = NULL;
56+
uint32_t lowest_prio = 0;
57+
58+
/* Select page with highest priority value (lowest priority).
59+
* Kernel regions (priority 0) are never selected. */
60+
for (fpage_t *fp = mspace->pmp_first; fp; fp = fp->pmp_next) {
61+
if (fp->priority > lowest_prio) {
62+
victim = fp;
63+
lowest_prio = fp->priority;
64+
}
65+
}
66+
67+
return victim;
68+
}

0 commit comments

Comments
 (0)