From: acevest Date: Wed, 21 Aug 2024 07:47:56 +0000 (+0800) Subject: 更改alloc_pages接口返回值 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=7b01bd07632c4f2264440dc3d93fff3cdcac82eb;p=kernel.git 更改alloc_pages接口返回值 --- diff --git a/drivers/ide.c b/drivers/ide.c index fa65c10..7985baa 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -70,7 +70,7 @@ void ide_pci_init(pci_device_t *pci) { 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; } diff --git a/fs/buffer.c b/fs/buffer.c index 823d38f..597eae6 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -213,7 +213,7 @@ void init_buffer() { 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); } diff --git a/include/page.h b/include/page.h index 30e2238..bbe407d 100644 --- a/include/page.h +++ b/include/page.h @@ -103,14 +103,14 @@ typedef unsigned long pte_t; #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)) @@ -155,6 +155,7 @@ typedef struct page { } page_t; void *page2va(page_t *page); +void *page2pa(page_t *page); page_t *_va2page(unsigned long addr); page_t *_pa2page(unsigned long addr); @@ -181,7 +182,7 @@ typedef struct free_area { 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) diff --git a/kernel/exec.c b/kernel/exec.c index cac3df4..0a328c3 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -35,7 +35,7 @@ void put_paging(unsigned long vaddr, unsigned long paddr, unsigned long flags) { 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); @@ -56,7 +56,7 @@ int sysc_exec(const char *path, char *const argv[]) { 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)); @@ -68,8 +68,8 @@ int sysc_exec(const char *path, char *const argv[]) { 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; diff --git a/kernel/fork.c b/kernel/fork.c index ab6d887..c4e1b46 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -39,7 +39,7 @@ int do_fork(pt_regs_t *regs, unsigned long flags) { 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; @@ -67,7 +67,7 @@ int do_fork(pt_regs_t *regs, unsigned long flags) { // 这里不用再为每个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); @@ -102,7 +102,7 @@ int do_fork(pt_regs_t *regs, unsigned long flags) { 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了 diff --git a/kernel/task_user.c b/kernel/task_user.c index cb8b880..8356609 100644 --- a/kernel/task_user.c +++ b/kernel/task_user.c @@ -63,10 +63,10 @@ void user_task_entry() { 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)); diff --git a/mm/buddy.c b/mm/buddy.c index 15dd0ee..7463ebd 100644 --- a/mm/buddy.c +++ b/mm/buddy.c @@ -46,15 +46,16 @@ page_t *_pa2page(unsigned long paddr) { } 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))) { @@ -65,25 +66,28 @@ page_t *__alloc_pages(unsigned int order) { 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; @@ -95,7 +99,7 @@ found: 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 // ... @@ -104,8 +108,10 @@ unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) { 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) { @@ -190,8 +196,7 @@ void init_buddy_system() { 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; diff --git a/mm/mm.c b/mm/mm.c index 71deb05..8fa0dce 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -50,7 +50,7 @@ void init_paging() { // 最大限制内存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) { diff --git a/mm/page.c b/mm/page.c index 925580b..3b27a8d 100644 --- a/mm/page.c +++ b/mm/page.c @@ -21,14 +21,14 @@ void do_no_page(void *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); @@ -57,7 +57,7 @@ void do_wp_page(void *addr) { // 如果是因为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. 拷贝页表 @@ -88,7 +88,7 @@ void do_wp_page(void *addr) { // 如果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. 拷贝页表 @@ -113,7 +113,7 @@ void do_wp_page(void *addr) { 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); diff --git a/mm/slub.c b/mm/slub.c index d700130..92e48e8 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -28,21 +28,14 @@ page_t *get_partial(kmem_cache_t *cache, gfp_t gfpflags) { // 从伙伴系统批发页,并将之初始化成一个链表 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; @@ -55,15 +48,6 @@ page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) { // 最后一个赋值为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;