|
|
|
@@ -62,7 +62,9 @@ RUNQ_remove_current_process(void)
|
|
|
|
|
{
|
|
|
|
|
struct Process *tp = runq->current_proc;
|
|
|
|
|
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:remove_current_process] Cleaning up process id %d\n", tp->id);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (runq->current_proc == runq->current_proc->next) {
|
|
|
|
|
runq->current_proc = NULL;
|
|
|
|
@@ -80,7 +82,9 @@ RUNQ_remove_current_process(void)
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < PHYSICAL_PAGE_AMOUNT; i++) {
|
|
|
|
|
if (cp->pt == tp->pt) {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:remove_current_process] Found ppn #%d (pte %d#%d), freeing it\n", cp->ppn, tp->id, cp->pt_index);
|
|
|
|
|
#endif
|
|
|
|
|
// detach page from the busy list
|
|
|
|
|
if (cp == cp->next) {
|
|
|
|
|
first_busy_page = NULL;
|
|
|
|
@@ -130,11 +134,15 @@ RUNQ_remove_current_process(void)
|
|
|
|
|
void
|
|
|
|
|
KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no)
|
|
|
|
|
{
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault] Handling %d started\n", page_no);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (first_free_page) {
|
|
|
|
|
free_pages_cnt--;
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault] Found free page #%d, using it\n", first_free_page->ppn);
|
|
|
|
|
#endif
|
|
|
|
|
pt[page_no].ppn = first_free_page->ppn;
|
|
|
|
|
pt[page_no].p = 1;
|
|
|
|
|
|
|
|
|
@@ -170,10 +178,14 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no)
|
|
|
|
|
|
|
|
|
|
busy_pages_cnt++;
|
|
|
|
|
} else {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault] No free pages available, trying to swap...\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if PAGE_REPLACEMENT_ALGORITHM == 1
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:random] Selected physical page #%d for replacement\n", first_busy_page->ppn);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// clear presence 'bit' from old PTE
|
|
|
|
|
first_busy_page->pt[first_busy_page->pt_index].p = 0;
|
|
|
|
@@ -189,21 +201,27 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no)
|
|
|
|
|
// move hand to next physical page
|
|
|
|
|
first_busy_page = first_busy_page->next;
|
|
|
|
|
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:random] Auto-advanced the list of busy pages to ppn %d\n", first_busy_page->ppn);
|
|
|
|
|
#endif
|
|
|
|
|
#elif PAGE_REPLACEMENT_ALGORITHM == 2
|
|
|
|
|
|
|
|
|
|
// first pass (no reference flag + out of time window)
|
|
|
|
|
for (size_t i = 0; i < PHYSICAL_PAGE_AMOUNT; i++) {
|
|
|
|
|
if (first_busy_page->pt[first_busy_page->pt_index].r) {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:wsclock] ppn %d: r = 1, time %d -> %d\n",
|
|
|
|
|
first_busy_page->ppn, first_busy_page->last_accessed, system_time);
|
|
|
|
|
#endif
|
|
|
|
|
first_busy_page->last_accessed = system_time;
|
|
|
|
|
first_busy_page->pt[first_busy_page->pt_index].r = 0;
|
|
|
|
|
|
|
|
|
|
first_busy_page = first_busy_page->next;
|
|
|
|
|
} else if ((system_time - first_busy_page->last_accessed) > WSCLOCK_TIME_WINDOW) {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:wsclock] ppn %d: r = 0, time_window = %d (> %d), using it\n",
|
|
|
|
|
first_busy_page->ppn, system_time - first_busy_page->last_accessed, WSCLOCK_TIME_WINDOW);
|
|
|
|
|
#endif
|
|
|
|
|
first_busy_page->pt[first_busy_page->pt_index].p = 0;
|
|
|
|
|
first_busy_page->pt = pt;
|
|
|
|
|
first_busy_page->pt_index = page_no;
|
|
|
|
@@ -214,15 +232,19 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no)
|
|
|
|
|
|
|
|
|
|
goto page_replacement_resolved;
|
|
|
|
|
} else {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:wsclock] ppn %d: r = 0, time_window = %d (<= %d), still active\n",
|
|
|
|
|
first_busy_page->ppn, system_time - first_busy_page->last_accessed, WSCLOCK_TIME_WINDOW);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
first_busy_page = first_busy_page->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fallback to random
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:wsclock] Failed to find non-active page, randomly selecting ppn %d\n", first_busy_page->ppn);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// clear presence 'bit' from old PTE
|
|
|
|
|
first_busy_page->pt[first_busy_page->pt_index].p = 0;
|
|
|
|
@@ -238,7 +260,9 @@ KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no)
|
|
|
|
|
// move hand to next physical page
|
|
|
|
|
first_busy_page = first_busy_page->next;
|
|
|
|
|
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:page_fault:wsclock] Auto-advanced the list of busy pages to ppn %d\n", first_busy_page->ppn);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
page_replacement_resolved:
|
|
|
|
|
#endif
|
|
|
|
@@ -252,7 +276,9 @@ page_replacement_resolved:
|
|
|
|
|
void KERNEL_update_job(size_t page_amount)
|
|
|
|
|
{
|
|
|
|
|
if (first_busy_page == NULL) {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:update_job] No pages to update\n");
|
|
|
|
|
#endif
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -260,17 +286,23 @@ void KERNEL_update_job(size_t page_amount)
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < page_amount; i++) {
|
|
|
|
|
if (first_busy_page->pt[first_busy_page->pt_index].r) {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:update_job] ppn %d updated time %d -> %d\n", first_busy_page->ppn, first_busy_page->last_accessed, system_time);
|
|
|
|
|
#endif
|
|
|
|
|
first_busy_page->pt[first_busy_page->pt_index].r = 0;
|
|
|
|
|
first_busy_page->last_accessed = system_time;
|
|
|
|
|
} else {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:update_job] ppn %d is up-to-date (time = %d)\n", first_busy_page->ppn, first_busy_page->last_accessed);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
first_busy_page = first_busy_page->next;
|
|
|
|
|
|
|
|
|
|
if (first_busy_page == starting_page) {
|
|
|
|
|
#if DISABLE_LOGGING != 1
|
|
|
|
|
printf("[kernel:update_job] Looped around the busy page list, exiting after %d iterations\n", i);
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|