ide_pci_controller[i].bus_cmd = iobase + PCI_IDE_CMD;
ide_pci_controller[i].bus_status = iobase + PCI_IDE_STATUS;
ide_pci_controller[i].bus_prdt = iobase + PCI_IDE_PRDT;
- ide_pci_controller[i].prdt = (prdte_t *)alloc_one_page(0);
+ ide_pci_controller[i].prdt = (prdte_t *)page2va(alloc_one_page(0));
ide_pci_controller[i].pci = pci;
}
page_t *page = NULL;
for (int j = 0; j < MAX_BBUFFER_CNT; j++) {
if (page_left_space < blocksize) {
- data = (void *)(alloc_one_page(0));
+ data = (void *)page2va(alloc_one_page(0));
page = va2page(data);
}
#define PAGE_UP(page) (((unsigned long)page + PAGE_SIZE - 1) & PAGE_MASK)
#define PAGE_DOWN PAGE_ALIGN
-#define PAGE_FLAGS(addr) ((addr)-PAGE_ALIGN(addr))
+#define PAGE_FLAGS(addr) ((addr) - PAGE_ALIGN(addr))
#define va2pa(x) (((unsigned long)(x)) - PAGE_OFFSET)
#define pa2va(x) ((void *)(((unsigned long)(x)) + PAGE_OFFSET))
// pfn: page frame number
#define pa2pfn(addr) ((addr) >> PAGE_SHIFT)
-#define pfn2pa(pfn) ((pfn) << PAGE_SHIFT)
+#define pfn2pa(pfn) ((void *)((pfn) << PAGE_SHIFT))
#define va2pfn(addr) pa2pfn(va2pa(addr))
#define pfn2va(pfn) pa2va(pfn2pa(pfn))
} page_t;
void *page2va(page_t *page);
+void *page2pa(page_t *page);
page_t *_va2page(unsigned long addr);
page_t *_pa2page(unsigned long addr);
list_head_t free_list;
} free_area_t;
-unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order);
+page_t *alloc_pages(unsigned int gfp_mask, unsigned int order);
void free_pages(unsigned long addr);
#define alloc_one_page(gfp_mask) alloc_pages(gfp_mask, 0)
pte_t *page_table = (pte_t *)PAGE_ALIGN(page_dir[npde]);
if (page_table == 0) {
- page_table = (pte_t *)alloc_one_page(0);
+ page_table = (pte_t *)page2va(alloc_one_page(0));
memset(page_table, 0, PAGE_SIZE);
page_table = (pte_t *)va2pa(page_table);
assert(page_table != 0);
ext2_read_inode(ino, &inode);
- Elf32_Ehdr *ehdr = (Elf32_Ehdr *)alloc_one_page(0);
+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(page2va(alloc_one_page(0)));
assert(ehdr != 0);
ext2_read_data(&inode, 0, PAGE_SIZE, (char *)ehdr);
printk("%08x\n", *((unsigned long *)ehdr->e_ident));
Elf32_Phdr *phdr;
phdr = (Elf32_Phdr *)(((unsigned long)ehdr) + ehdr->e_phoff + (i * ehdr->e_phentsize));
- printk("Type %08x Off %08x Va %08x Pa %08x Fsz %08x Mmsz %08x\n", phdr->p_type, phdr->p_offset, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
- phdr->p_memsz);
+ printk("Type %08x Off %08x Va %08x Pa %08x Fsz %08x Mmsz %08x\n", phdr->p_type, phdr->p_offset, phdr->p_vaddr,
+ phdr->p_paddr, phdr->p_filesz, phdr->p_memsz);
unsigned long vaddr = phdr->p_vaddr;
unsigned long offset = phdr->p_offset;
list_add(&tsk->list, &all_tasks);
irq_restore(iflags);
- tsk->cr3 = va2pa((unsigned long)alloc_one_page(0));
+ tsk->cr3 = (uint32_t)page2pa(alloc_one_page(0));
assert(tsk->cr3 != 0);
unsigned int i, j;
// 这里不用再为每个PDE拷贝一次PageTable,只需要拷贝PageDirectory并将其低于768的写权限去掉
// 同时需要修改缺页异常doPageFault的逻辑
if (PAGE_ALIGN(spde) != 0) {
- dpde = alloc_one_page(0);
+ dpde = page2va(alloc_one_page(0));
assert(dpde != 0);
memset((void *)dpde, 0, PAGE_SIZE);
dpde = PAGE_FLAGS(spde) | (unsigned long)va2pa(dpde);
strcpy(tsk->name, (char *)(child_regs->eax));
child_regs->eax = 0;
} else {
- child_regs->eip = *((unsigned long *)&&fork_child);
+ child_regs->eip = *((unsigned long *) && fork_child);
}
// 这一句已经不需要了,通过fork_child已经能给子进程返回0了
current->priority = 7;
unsigned long ring3_text_page = va2pa(ring3_entry);
- unsigned long ring3_bss_page = va2pa(alloc_one_page(0));
+ unsigned long ring3_bss_page = (unsigned long)page2pa(alloc_one_page(0));
- unsigned long *pt_text_page = (unsigned long *)(alloc_one_page(0));
- unsigned long *pt_bss_page = (unsigned long *)(alloc_one_page(0));
+ unsigned long *pt_text_page = (unsigned long *)page2va(alloc_one_page(0));
+ unsigned long *pt_bss_page = (unsigned long *)page2va(alloc_one_page(0));
unsigned long *p = (unsigned long *)(pa2va(current->cr3));
}
void *page2va(page_t *page) { return pfn2va((page)-buddy_system.page_map); }
+void *page2pa(page_t *page) { return pfn2pa((page)-buddy_system.page_map); }
page_t *__alloc_pages(unsigned int order) {
//
page_t *page = 0;
- page_t *buddy = 0;
free_area_t *area;
- unsigned long size;
unsigned int select_order;
unsigned int i;
+
+ // 找到首个满足 >= order的page块
for (select_order = order; select_order < MAX_ORDER; ++select_order) {
area = buddy_system.free_area + select_order;
if (!list_empty(&(area->free_list))) {
return 0;
found:
+ // 从链表取下这个连续的page块
page = list_entry(area->free_list.next, page_t, lru);
list_del(&(page->lru));
ClearPagePrivate(page);
page->private = 0;
area->free_count--;
+ // 二分这个page块,把空闲的挂到对应的order队列上
+ // 直到得到order大小的page块
while (select_order > order) {
area--;
select_order--;
- size = 1UL << select_order;
+ unsigned long buddy_offset = 1UL << select_order;
- buddy = page + size;
- list_add(&(buddy->lru), &(area->free_list));
+ page_t *buddy_page = page + buddy_offset;
+ list_add(&(buddy_page->lru), &(area->free_list));
area->free_count++;
- buddy->private = select_order;
- SetPagePrivate(buddy);
+ buddy_page->private = select_order;
+ SetPagePrivate(buddy_page);
}
- //
+ // 初始化这一连续的page块
for (i = 0; i < (1UL << order); ++i) {
page_t *p = page + i;
p->head_page = page;
return page;
}
-unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) {
+page_t *alloc_pages(unsigned int gfp_mask, unsigned int order) {
// gfp_mask
// ...
page_t *page = __alloc_pages(order);
irq_restore(flags);
- unsigned long addr = (unsigned long)page2va(page);
- return addr;
+ // unsigned long addr = (unsigned long)page2va(page);
+ // return addr;
+
+ return page;
}
void __free_pages(page_t *page, unsigned int order) {
buddy_system.page_map = alloc_from_bootmem(page_map_size, "buddy");
if (0 == buddy_system.page_map) {
printk("can not go on playing...\n");
- while (1)
- ;
+ while (1);
}
buddy_system.page_map_end = buddy_system.page_map + pfn_cnt + 1;
// 最大限制内存1G
for (pfn = pa2pfn(BOOT_INIT_PAGETBL_CNT << 22); pfn < bootmem_data.max_pfn; ++pfn) {
unsigned long ti = pfn % PAGE_PTE_CNT;
- unsigned long page_addr = pfn2pa(pfn);
+ unsigned long page_addr = (unsigned long)pfn2pa(pfn);
if (ti == 0) {
pgtb_addr = (unsigned long *)alloc_from_bootmem(PAGE_SIZE, "paging");
if (0 == pgtb_addr) {
pde_t *page_dir = (pde_t *)pa2va(current->cr3);
pte_t *page_tbl = 0;
- unsigned long page = alloc_one_page(0);
+ unsigned long page = (unsigned long)page2va(alloc_one_page(0));
assert(page != 0);
int npde = get_npde(addr);
int npte = get_npte(addr);
if (page_dir[npde] == 0) {
- page_tbl = (pte_t *)alloc_one_page(0);
+ page_tbl = (pte_t *)page2va(alloc_one_page(0));
assert(page_tbl != 0);
memset((void *)page_tbl, 0, PAGE_SIZE);
// 如果是因为PDE被写保护
if (*pde & PDE_RW == 0) {
// 1. 分配一个页表
- unsigned long newtbl = alloc_one_page(0);
+ unsigned long newtbl = (unsigned long)page2va(alloc_one_page(0));
assert(newtbl != 0);
// 2. 拷贝页表
// 如果PTE的位置被写保护
if (*pte & PTE_RW == 0) {
// 1. 分配一个页表
- unsigned long newaddr = alloc_one_page(0);
+ unsigned long newaddr = (unsigned long)page2va(alloc_one_page(0));
assert(newaddr != 0);
// 2. 拷贝页表
page->count--;
unsigned long flags = PAGE_FLAGS(page_tbl[npte]);
unsigned long wp_va_addr = (unsigned long)pa2va(wp_pa_addr);
- unsigned long newtbl = alloc_one_page(0);
+ unsigned long newtbl = (unsigned long) page2va(alloc_one_page(0));
assert(newtbl != 0);
memcpy((void *)newtbl, (void *)wp_va_addr, PAGE_SIZE);
// 从伙伴系统批发页,并将之初始化成一个链表
page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) {
- // alloc pages from buddy system
- unsigned long bgn = alloc_pages(gfpflags, cache->order);
- unsigned long end = 0;
-
- // 找到该页的管理结构
- page_t *page = va2page(bgn);
-
- //
+ page_t *page = alloc_pages(gfpflags, cache->order);
if (0 == page) {
return 0;
}
- end = bgn + cache->objects * cache->size;
+ unsigned long bgn = (unsigned long)page2va(page);
+ unsigned long end = bgn + cache->objects * cache->size;
-#if 1
unsigned long last = bgn;
unsigned long addr;
// 最后一个赋值为0
*((void **)last) = 0;
-#else
- unsigned long addr;
- for (addr = bgn; addr < end; addr += cache->size) {
- *(unsigned long *)addr = addr + cache->size;
- }
-
- addr = end - cache->size;
- *(unsigned long *)addr = 0;
-#endif
page->freelist = (void **)bgn;
page->inuse = 0;