Ex1:physical page management in JOS.
Ans:這題要求完成在 JOS 中對 physical pages 的管理,第一個要完成的是 bootstrap 時一開始所需要的 boot_alloc()。首先,從 Lab1 我們可以知道進入 kernel 後,physical memory 的使用狀況如下(但在entry.S中只先將[0, 4MB)對映到[KERNBASE, KERBASE+4MB),所以boot_alloc()能直接以指標存取的記憶體會受限在這個範圍):
接著是完成page_init(),這邊要完成對全域變數 pages[] 與 free_page_list 的設定。free_page_list會指向目前可用的free pages的頭。由於boot_alloc()已使用了部份free memory,所以在串free_page_list時要將那些記憶體跳過。如下:
接下來的page_alloc()與page_free()就先以最簡單的first-fit & simple free list來處裡配置策略。如下:
Ans:這題要求完成在 JOS 中對 physical pages 的管理,第一個要完成的是 bootstrap 時一開始所需要的 boot_alloc()。首先,從 Lab1 我們可以知道進入 kernel 後,physical memory 的使用狀況如下(但在entry.S中只先將[0, 4MB)對映到[KERNBASE, KERBASE+4MB),所以boot_alloc()能直接以指標存取的記憶體會受限在這個範圍):
boot_alloc()的記憶體會在後續持續被使用,但此時尚未完成 virtual memory 的設定,所以我們在 boot_alloc()必須以 page 的大小作為單位來使用,這樣才能在後續管理 page frame 時將 boot_alloc()過的記憶體正確標注狀態。實作如下:
static void *
boot_alloc(uint32_t n)
{
static char *nextfree = 0; // virtual address of next byte of free memory
static size_t used_npages = 0;
static size_t want_npages = 0;
char *result = NULL;
// this is defined by linker script, kern.ld,
// the very beginning virtual address of kernel image
extern char btext[];
extern char end[];
// Initialize nextfree if this is the first time.
// 'end' is a magic symbol automatically generated by the linker,
// which points to the end of the kernel's bss segment:
// the first virtual address that the linker did *not* assign
// to any kernel code or global variables.
if (!nextfree) {
nextfree = ROUNDUP((char *) end, PGSIZE);
}
// Allocate a chunk large enough to hold 'n' bytes, then update
// nextfree. Make sure nextfree is kept aligned
// to a multiple of PGSIZE.
//
// LAB 2: Your code here.
// calculate how much memory callers want in page unit.
if (n) {
want_npages = (n/PGSIZE);
if (n%PGSIZE) {
want_npages = (n/PGSIZE) + 1;
}
} else {
result = nextfree;
goto done;
}
// npage is calculated in i386_detect_memory()
if (want_npages > (npages - ROUNDUP((end - btext + 1), PGSIZE) - used_npages)) {
panic("out of memory in boot_alloc\n");
} else {
result = nextfree;
nextfree += PGSIZE*want_npages;
used_npages += want_npages;
}
done:
return result;
}
|
實作如下:
void
page_init(void)
{
// The example code here marks all physical pages as free.
// However this is not truly the case. What memory is free?
// 1) Mark physical page 0 as in use.
// This way we preserve the real-mode IDT and BIOS structures
// in case we ever need them. (Currently we don't, but...)
// 2) The rest of base memory, [PGSIZE, npages_basemem * PGSIZE)
// is free.
// 3) Then comes the IO hole [IOPHYSMEM, EXTPHYSMEM), which must
// never be allocated.
// 4) Then extended memory [EXTPHYSMEM, ...).
// Some of it is in use, some is free. Where is the kernel
// in physical memory? Which pages are already in use for
// page tables and other data structures?
//
// Change the code to reflect this.
// NB: DO NOT actually touch the physical memory corresponding to
// free pages!
size_t next_free_pgnum = PGNUM(PADDR(boot_alloc(0)));
size_t i;
for (i = 0; i < (npages-1); i++) {
pages[i].pp_ref = 0;
pages[i].pp_link = &pages[i+1];
pages[i].pp_reserved = 0;
}
pages[i].pp_ref = 0;
pages[i].pp_link = NULL;
pages[i].pp_reserved = 0;
// page 0 is for real mode IDT and BIOS structure
pages[0].pp_ref = 1;
pages[0].pp_link = NULL;
pages[0].pp_reserved = 1;
// page_free_list points to page 1
page_free_list = &pages[1];
// free pages do not include [IOPHYSMEM, nextfree)
pages[PGNUM(IOPHYSMEM) - 1].pp_link = &pages[next_free_pgnum];
// pages in [IOPHYSMEM, EXTPHYSMEM) are reserved for BIOS
for (i = PGNUM(IOPHYSMEM);i < next_free_pgnum;++i) {
pages[i].pp_ref = 1;
pages[i].pp_link = NULL;
pages[i].pp_reserved = 1;
}
}
|
接下來的page_alloc()與page_free()就先以最簡單的first-fit & simple free list來處裡配置策略。如下:
//
// Allocates a physical page. If (alloc_flags & ALLOC_ZERO), fills the entire
// returned physical page with '\0' bytes. Does NOT increment the reference
// count of the page - the caller must do these if necessary (either explicitly
// or via page_insert).
//
// Returns NULL if out of free memory.
//
// Hint: use page2kva and memset
struct Page *
page_alloc(int alloc_flags)
{
// Fill this function in
struct Page *p = NULL;
if (!page_free_list) {
return NULL;
}
p = page_free_list;
page_free_list = p->pp_link;
p->pp_link = NULL;
if (alloc_flags & ALLOC_ZERO) {
memset ((void *)KADDR(page2pa(p)), 0, PGSIZE);
}
return p;
}
//
// Return a page to the free list.
// (This function should only be called when pp->pp_ref reaches 0.)
//
void
page_free(struct Page *pp)
{
// Fill this function in
if (!pp) { return; }
if (PGNUM(page2pa(pp)) >= npages) {
return;
}
pp->pp_link = page_free_list;
page_free_list = pp;
}
|
留言
張貼留言