This commit is contained in:
ІО-23 Шмуляр Олег 2025-03-06 23:05:21 +02:00
parent b4edf54aa6
commit 9fd901eaa0
5 changed files with 64 additions and 12 deletions

View File

@ -13,7 +13,6 @@ struct RunQ {
struct RunQ *RunQ(size_t max_procs);
void RUNQ_add_process(size_t max_page_accesses, size_t total_pages_owned);
static struct RunQ *runq;
struct PhysPage {
@ -21,13 +20,10 @@ struct PhysPage {
size_t busy_flag;
struct PhysPage *prev;
struct PhysPage *next;
struct PageTable *pt;
struct PageTableEntry *pt;
size_t pt_index;
};
static struct PhysPage *first_free_page;
static struct PhysPage *first_busy_page;
void KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no);

View File

@ -1,9 +1,17 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "kernel.h"
#include "process.h"
extern struct RunQ *runq;
extern struct PhysPage *first_free_page;
extern struct PhysPage *first_busy_page;
struct RunQ *
RunQ(size_t max_procs)
{
@ -45,8 +53,39 @@ RUNQ_add_process(size_t max_page_accesses,
void
KERNEL_page_fault(struct PageTableEntry *pt, size_t page_no)
{
printf("[kernel:page_fault] Handling %d started\n", page_no);
if (first_free_page) {
printf("[kernel:page_fault] Found free page #%d, using it\n", first_free_page->ppn);
pt[page_no].ppn = first_free_page->ppn;
pt[page_no].p = 1;
first_free_page->busy_flag = 1;
first_free_page->pt = pt;
first_free_page->pt_index = page_no;
// ---- free list -> busy list ----
struct PhysPage *this_page = first_free_page;
if (first_free_page->next != first_free_page) {
// reconnect free list items together
first_free_page->prev->next = first_free_page->next;
first_free_page->next->prev = first_free_page->prev;
first_free_page = first_free_page->next;
} else {
// clear list if it only had this page
first_free_page = NULL;
}
if (!first_busy_page) {
// initialize the busy list with this page
first_busy_page = this_page;
} else {
// insert this page at the end of the list
this_page->next = first_busy_page;
this_page->prev = first_busy_page->prev;
this_page->prev->next = this_page;
this_page->next->prev = this_page;
}
}
}

View File

@ -3,15 +3,22 @@
#include <stdlib.h>
#include "kernel.h"
#include "random.h"
#include "config.h"
struct RunQ *runq;
struct PhysPage *first_free_page;
struct PhysPage *first_busy_page;
int
main(void)
{
first_busy_page = NULL;
struct PhysPage *first_free_page = malloc(sizeof(struct PhysPage));
first_free_page = malloc(sizeof(struct PhysPage));
first_free_page->ppn = 0;
first_free_page->busy_flag = 0;
first_free_page->prev = first_free_page;
@ -23,7 +30,7 @@ main(void)
pp->busy_flag = 0;
/*
* insert into here
* insert pp into here
* |
* v
* [ ] [ ] [ ]
@ -38,7 +45,13 @@ main(void)
}
runq = RunQ(10);
RUNQ_add_process(10000, 50);
RUNQ_add_process(10000, 50);
RUNQ_add_process(10000, 50);
for (size_t i = 0; i < 5; i++) {
RUNQ_add_process(10000, 10 + randint(70));
}
for (int i = 0; i < 20; i++) {
PROCESS_run_for(runq->current_proc, 20);
runq->current_proc = runq->current_proc->next;
}
}

View File

@ -1,3 +1,5 @@
#include <stdio.h>
#include "kernel.h"
void MMU_read(struct PageTableEntry *pt, size_t page_no)
@ -5,6 +7,7 @@ void MMU_read(struct PageTableEntry *pt, size_t page_no)
if (!pt[page_no].p)
KERNEL_page_fault(pt, page_no);
printf("[mmu:read] pt[%d] -> ppn %d\n", page_no, pt[page_no].ppn);
pt[page_no].r = 1;
}
@ -13,6 +16,7 @@ void MMU_write(struct PageTableEntry *pt, size_t page_no)
if (!pt[page_no].p)
KERNEL_page_fault(pt, page_no);
printf("[mmu:write] pt[%d] -> ppn %d\n", page_no, pt[page_no].ppn);
pt[page_no].r = 1;
pt[page_no].m = 1;
}

View File

@ -31,10 +31,10 @@ size_t PROCESS_run_for(struct Process *p, size_t time_bits)
int accessed_page_no = randint(p->total_pages_owned);
if (randint(10) > 7) {
printf("[process] Writing to page #%d\n", accessed_page_no);
printf("[process %d] Writing to page #%d\n", p->id, accessed_page_no);
MMU_write(p->pt, accessed_page_no);
} else {
printf("[process] Reading from page #%d\n", accessed_page_no);
printf("[process %d] Reading from page #%d\n", p->id, accessed_page_no);
MMU_read(p->pt, accessed_page_no);
}
}