跳到主要內容

發表文章

目前顯示的是 5月, 2012的文章

6.828 Lab2 part 1

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()能直接以指標存取的記憶體會受限在這個範圍): boot_alloc()的記憶體會在後續持續被使用,但此時尚未完成 virtual memory 的設定,所以我們在 boot_alloc()必須以 page 的大小作為單位來使用,這樣才能在後續管理 page frame 時將 boot_alloc()過的記憶體正確標注狀態。實作如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 []; ex