Skip to content

Commit

Permalink
Clean up prints
Browse files Browse the repository at this point in the history
Ready for submission of Lab2
  • Loading branch information
jonastheis committed Sep 10, 2019
1 parent 7928ad1 commit 4b1dd84
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 49 deletions.
6 changes: 2 additions & 4 deletions kernel/mem/buddy.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ int buddy_map_chunk(struct page_table *pml4, size_t index)
size_t nblocks = (1 << (12 + BUDDY_MAX_ORDER - 1)) / PAGE_SIZE; // 2MB / PAGE_SIZE = 512
size_t nalloc = ROUNDUP(nblocks * sizeof *page, PAGE_SIZE) / PAGE_SIZE; // ROUNDUP(512 * sizeof(page_info)) / PAGE_SIZE =
size_t i;
// cprintf("nblocks=%d, nalloc=%d\n", nblocks, nalloc);
// todo, check if nblocks = 512

index = ROUNDDOWN(index, nblocks); // index has to be multiple of 512
base = pages + index;

Expand All @@ -346,8 +345,7 @@ int buddy_map_chunk(struct page_table *pml4, size_t index)

for (i = 0; i < nblocks; ++i) {
page = base + i;
list_init(&page->pp_node); /// ?????
// cprintf("list_init at %p\n", page2pa(page));
list_init(&page->pp_node);
}

npages = index + nblocks;
Expand Down
9 changes: 0 additions & 9 deletions kernel/mem/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ int pml4_setup(struct boot_info *boot_info)
* Permissions: kernel RW, user NONE
* Your code goes here:
*/
cprintf("bootstack=%p, bootstack_top=%p, KSTACK_TOP=%p\n", bootstack, bootstack + KSTACK_SIZE, KSTACK_TOP);

// 1) Map kernel stack
for(int i=0; i<KSTACK_SIZE; i+=PAGE_SIZE) {
void *base_va = (void*)KSTACK_TOP-KSTACK_SIZE;
void *new_va_addr = (void *)sign_extend2(((uint64_t)base_va)+i);
cprintf("creating stack for cr3, va=%p ==> pa=%p\n", new_va_addr, (void*)(bootstack+i));
page_insert(kernel_pml4, pa2page((physaddr_t)bootstack+i), new_va_addr, PAGE_PRESENT | PAGE_WRITE | PAGE_NO_EXEC);
page_insert(kernel_pml4, pa2page((physaddr_t)bootstack+i), bootstack+KERNEL_VMA+i, PAGE_PRESENT | PAGE_WRITE | PAGE_NO_EXEC);
}
Expand All @@ -62,22 +60,17 @@ int pml4_setup(struct boot_info *boot_info)
// add guard page
// void *base_va = (void*)KSTACK_TOP-KSTACK_SIZE-PAGE_SIZE;
// page_insert(kernel_pml4, NULL, base_va, PAGE_WRITE | PAGE_NO_EXEC);
// TODO: check PTSIZE

// 3) setting kernel pages
cprintf("==== setting kernel pages ====\n");
boot_map_kernel(kernel_pml4, boot_info->elf_hdr);
cprintf("==== setting kernel pages END ====\n");

// 4) Buddy migrate
/* Migrate the struct page_info structs to the newly mapped area using
* buddy_migrate().
*/
// 3) set mapping for pages before migrate
boot_map_region(kernel_pml4, (void*)KPAGES, ROUNDUP(npages * sizeof(struct page_info), PAGE_SIZE), PADDR(pages), PAGE_PRESENT | PAGE_WRITE | PAGE_NO_EXEC);
cprintf("mapped pages before migrate\n");
buddy_migrate();


return 0;
}
Expand Down Expand Up @@ -155,9 +148,7 @@ void mem_init(struct boot_info *boot_info)
lab2_check_pml4();

/* Load the kernel PML4. */
// cprintf("works in old cr3\n");
write_cr3(PADDR(((void *)kernel_pml4)));
// cprintf("works in new cr3\n");

/* Check the paging functions. */
lab2_check_paging();
Expand Down
4 changes: 0 additions & 4 deletions kernel/mem/insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ static int insert_pte(physaddr_t *entry, uintptr_t base, uintptr_t end,
page = info->page;
page->pp_ref++;
*entry = info->flags | PAGE_ADDR(page2pa(page));
// cprintf("insert_pte: va=%p ==> pa=%p\n", (void*)base, page2pa(page));

return 0;
}
Expand Down Expand Up @@ -120,9 +119,6 @@ int page_insert(struct page_table *pml4, struct page_info *page, void *va,
size = HPAGE_SIZE;
}

// TODO: handle corner case


return walk_page_range(pml4, va, (void *)((uintptr_t)va + size),
&walker);
}
Expand Down
22 changes: 4 additions & 18 deletions kernel/mem/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,12 @@ static int boot_map_pde(physaddr_t *entry, uintptr_t base, uintptr_t end,
/* LAB 2: your code here. */

uintptr_t offset = base - info->base;
// cprintf("base=%p, end=%p, pa=%p, info->base=%p, offset=%p\n", base, end, info->pa, info->base, offset);

if((*entry & PAGE_PRESENT) == 0 && end - base + 1 == HPAGE_SIZE){
*entry = info->flags | PAGE_HUGE | PAGE_PRESENT | PAGE_ADDR(info->pa+offset);
// cprintf("boot_map_pde: mapped as HUGE page | va=%p, pa=%p\n", base, info->pa+offset);
}

else if( (*entry & (PAGE_PRESENT | PAGE_HUGE)) == PAGE_PRESENT){ // page is present and not huge
} else if( (*entry & (PAGE_PRESENT | PAGE_HUGE)) == PAGE_PRESENT){ // page is present and not huge
// cprintf("boot_map_pde: entry exist, mapped as SMALL page | va=%p, pa=%p\n", base, info->pa+offset);
}
else{ // page is not present or is huge
// cprintf("boot_map_pde: entry doesnt exist or huge, pa=%p\n", base, info->pa+offset);
} else{ // page is not present or is huge
ptbl_split(entry, base, end, walker);
}

Expand Down Expand Up @@ -111,14 +105,10 @@ void boot_map_kernel(struct page_table *pml4, struct elf *elf_hdr)
/* LAB 2: your code here. */

// 1) identity mapping at the KERNEL_VMA of size BOOT_MAP_LIM * with permissions RW-.
// cprintf(">> identity mapping at the KERNEL_VMA - BOOT_MAP_LIM (%p - %p)\n", KERNEL_VMA, KERNEL_VMA + BOOT_MAP_LIM);
uint64_t pages_num = BOOT_MAP_LIM / PAGE_SIZE;
boot_map_region(kernel_pml4, (void*)KERNEL_VMA, BOOT_MAP_LIM, PADDR((void*)(KERNEL_VMA)), PAGE_PRESENT | PAGE_WRITE | PAGE_NO_EXEC);
// boot_map_region(kernel_pml4, (void*)KERNEL_VMA, BOOT_MAP_LIM, KERNEL_VMA, PAGE_PRESENT | PAGE_WRITE | PAGE_NO_EXEC);



// 2) PARSING ELF
cprintf(">> PARSING ELF\n");
for(uint64_t i = 0; i<elf_hdr->e_phnum; i++){
struct elf_proghdr hdr = prog_hdr[i];
if(hdr.p_va < KERNEL_VMA) continue;
Expand All @@ -128,12 +118,8 @@ void boot_map_kernel(struct page_table *pml4, struct elf *elf_hdr)
} else {
flags += PAGE_NO_EXEC | PAGE_WRITE;
}
cprintf("boot_map_region, flags=%lx, va=%p, pa=%p, size=%u\n", flags, hdr.p_va, hdr.p_pa, hdr.p_filesz);
// cprintf("boot_map_region, flags=%lx, va=%p, pa=%p, size=%u\n", flags, hdr.p_va, hdr.p_pa, hdr.p_filesz);
boot_map_region(pml4, (void*)hdr.p_va, hdr.p_filesz, hdr.p_pa, flags);
}

// // 3) set mapping for pages before migrate
// boot_map_region(pml4, (void*)KPAGES, ROUNDUP(npages * sizeof(struct page_info), PAGE_SIZE), PADDR(pages), PAGE_PRESENT | PAGE_WRITE | PAGE_NO_EXEC);
// cprintf("mapped pages before migrate\n");
}

20 changes: 6 additions & 14 deletions kernel/mem/ptbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ int ptbl_alloc(physaddr_t *entry, uintptr_t base, uintptr_t end,
page->pp_ref += 1;
*entry = (PAGE_PRESENT | PAGE_WRITE | PAGE_USER | PAGE_ADDR(pa));

cprintf("ptbl_alloc, pa=%p, PAGE_ADDR(pa))=%p, KADDR(pa))=%p\n", pa, PAGE_ADDR(pa), KADDR(PAGE_ADDR(pa)));
return 0;
}

Expand Down Expand Up @@ -53,7 +52,6 @@ int ptbl_split(physaddr_t *entry, uintptr_t base, uintptr_t end,

if((*entry & PAGE_PRESENT) == 0){ // page is not present
ptbl_alloc(entry, base, end, walker);
// cprintf("boot_map_pde: created entry, will map as small page\n");
return 0;
} else if((*entry & (PAGE_PRESENT | PAGE_HUGE)) == (PAGE_PRESENT | PAGE_HUGE)) { // huge page
cprintf("!!!! ptbl_split: page is huge and we need to split up\n");
Expand All @@ -70,9 +68,6 @@ int ptbl_split(physaddr_t *entry, uintptr_t base, uintptr_t end,
}
panic("ptbl_split: else?\n");




return 0;
}

Expand Down Expand Up @@ -104,30 +99,29 @@ int ptbl_merge(physaddr_t *entry, uintptr_t base, uintptr_t end,
struct page_info *pt_page = pa2page(PAGE_ADDR(*entry));
uint64_t flags = 0;
for(int i=0; i<PAGE_TABLE_ENTRIES; i++){
// check if all entries are present
if(!(pt->entries[i] & PAGE_PRESENT)){
// cprintf("i=%d, entries[i]=%p, entry not present\n", i, pt->entries[i]);
return 0;
}
// copy flags from first entry
if(!flags) {
flags = pt->entries[i] & PAGE_MASK;
}
// check if all flags are the same
if(!(pt->entries[i] & flags)) {
cprintf("i=%d, entries[i]=%p, flags not equal\n", i, pt->entries[i]);
return 0;
}

// cprintf("i=%d, entries[i]=%p\n", i, pt->entries[i]);
}

// now pages in pt can be merged
// copy data to huge page
struct page_info *page = page_alloc(ALLOC_HUGE);
physaddr_t pa = page2pa(page);
void* start_addr = KADDR(PAGE_ADDR(pt->entries[0]));
memcpy(page2kva(page), start_addr, HPAGE_SIZE);

// point PDE to newly created huge page
flags &= ~(PAGE_ACCESSED); // feels dirty bc there could be other flags set by the CPU
cprintf("old_flags=0x%x, new_flags=0x%x\n", flags, flags | PAGE_HUGE | PAGE_ADDR(pa));
flags &= ~(PAGE_ACCESSED);
*entry = flags | PAGE_HUGE | PAGE_ADDR(pa);
page->pp_ref++;

Expand All @@ -138,8 +132,6 @@ int ptbl_merge(physaddr_t *entry, uintptr_t base, uintptr_t end,
tlb_invalidate(info->pml4, (void*)(base + PAGE_SIZE * i));
}
page_decref(pt_page);

cprintf("All pages can be merged into a huge page!\n");

return 0;
}
Expand All @@ -164,7 +156,7 @@ int ptbl_free(physaddr_t *entry, uintptr_t base, uintptr_t end,
return 0;
}
}
cprintf("removing empty table\n");

struct page_info *page = pa2page(PAGE_ADDR(*entry));
page_decref(page);

Expand Down

0 comments on commit 4b1dd84

Please sign in to comment.