From: AceVest Date: Sat, 26 Apr 2014 03:44:17 +0000 (+0800) Subject: add kmalloc/kfree X-Git-Tag: 0.3.0~80 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/doc/syslogd.8.html?a=commitdiff_plain;h=ec785473d78c883451f2b7d25e3795d40c8740aa;p=kernel.git add kmalloc/kfree --- diff --git a/Makefile b/Makefile index 0822f74..b68a63c 100644 --- a/Makefile +++ b/Makefile @@ -10,15 +10,17 @@ INC_DIRS = include drivers CFLAGS += ${INC_DIRS:%=-I%} SOURCE_FILES := $(foreach DIR, $(SRC_DIRS), $(wildcard $(DIR)/*.[cS])) +HEADER_FILES := $(foreach DIR, $(INC_DIRS), $(wildcard $(DIR)/*.h)) + OBJS := $(patsubst %,%.o,$(SOURCE_FILES)) ${KERNELBIN}: ${OBJS} ld -M -T$(LINKSCRIPT) $(OBJS) -o $@ > $(SYSTEMMAP) -%.S.o: %.S +%.S.o: %.S ${HEADER_FILES} ${CC} ${CFLAGS} $< -o $@ -%.c.o: %.c +%.c.o: %.c ${HEADER_FILES} ${CC} ${CFLAGS} $< -o $@ c: diff --git a/fs/ext2.c b/fs/ext2.c index da1ca6d..862b7c7 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -53,7 +53,7 @@ void setup_ext2() panic("Only Support Ext2 File System..."); /* 分配group descriptor table 的内存 */ - gdesc = (pGroupDesc) kmalloc(EXT2_BLOCK_SIZE); + gdesc = (pGroupDesc) kmalloc_old(EXT2_BLOCK_SIZE); if(gdesc == NULL) panic("out of memory for ext2 group descritpor table"); /* 初始化ext2 group descriptor table */ @@ -89,7 +89,7 @@ void setup_ext2() printk("%c", buf[i]); #endif #if 0 - unsigned char *p = (char *)kmalloc(EXT2_BLOCK_SIZE); + unsigned char *p = (char *)kmalloc_old(EXT2_BLOCK_SIZE); ext2_read_block(0, p); for(i=0; i<512; i++) @@ -138,14 +138,14 @@ int ext2_read_inode(unsigned int n, pInode ino) gidx %= EXT2_INODES_PER_BLOCK; - char *buf = kmalloc(EXT2_BLOCK_SIZE); + char *buf = kmalloc_old(EXT2_BLOCK_SIZE); if(buf == NULL) panic("faild read inode. out of memory"); ext2_read_block(inotbl, buf); memcpy((void *)ino,(void*)(((pInode)buf)+gidx), EXT2_INODE_SIZE); - kfree(buf); + kfree_old(buf); return n; } @@ -160,7 +160,7 @@ int ext2_read_file(const pInode ino, void *buf, size_t count) if(blks > EXT2_NDIR_BLOCKS) panic("file too large to read"); - p = kmalloc(blks*EXT2_BLOCK_SIZE); + p = kmalloc_old(blks*EXT2_BLOCK_SIZE); if(p == NULL) panic("out of memory when search inode in directory"); @@ -173,7 +173,7 @@ int ext2_read_file(const pInode ino, void *buf, size_t count) memcpy(buf, p, count); - kfree(p); + kfree_old(p); return count; } @@ -189,7 +189,7 @@ char *load_inode_content(const pInode ino) panic("unsupport file large than 12KB"); - buf = kmalloc(blks*EXT2_BLOCK_SIZE); + buf = kmalloc_old(blks*EXT2_BLOCK_SIZE); if(buf == NULL) panic("out of memory when search inode in directory"); for(i=0; ii_size); + buf = kmalloc_old(in->i_size); ext2_read_file(in, buf, in->i_size); #endif @@ -240,7 +240,7 @@ int ext2_search_file(const char *file, const pInode in, pInode out) ent = (pDirEnt)(ent->rec_len + (unsigned long)ent); } - kfree(buf); + kfree_old(buf); ext2_read_inode(inode_n, out); diff --git a/include/mm.h b/include/mm.h index 8991274..2f6a75b 100644 --- a/include/mm.h +++ b/include/mm.h @@ -19,3 +19,4 @@ unsigned long bootmem_total_pages(); unsigned long bootmem_page_state(unsigned long pfn); #define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE) + diff --git a/include/page.h b/include/page.h index 57798cf..7fc67f1 100644 --- a/include/page.h +++ b/include/page.h @@ -69,10 +69,15 @@ typedef unsigned long pte_t; #define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde))) + +typedef unsigned int gfp_t; + enum page_flags { PG_Private, }; - + +struct kmem_cache; +typedef struct kmem_cache kmem_cache_t; typedef struct page { @@ -85,6 +90,8 @@ typedef struct page unsigned int order; void **freelist; // for slub + kmem_cache_t *cache; + unsigned long inuse; } page_t; @@ -121,6 +128,26 @@ typedef struct free_area unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order); void free_pages(unsigned long addr); + +struct kmem_cache +{ + const char *name; + + unsigned long objsize; + unsigned long size; + unsigned long align; + unsigned long order; + unsigned long objects; + + unsigned int partial_cnt; + list_head_t partial; + + page_t *page; + + list_head_t list; +}; + + // TODO Remove typedef struct page_ { diff --git a/include/system.h b/include/system.h index bdd5906..7a071dc 100644 --- a/include/system.h +++ b/include/system.h @@ -28,26 +28,28 @@ #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) -void *kmalloc(size_t size); -void kfree(void *p); +void *kmalloc(size_t size, gfp_t gfpflags); +void kfree(void *addr); +void *kmalloc_old(size_t size); +void kfree_old(void *p); static inline void *get_virt_pages(unsigned int n) { assert(n>0); size_t size = n << PAGE_SHIFT; - return (void*) kmalloc(size); + return (void*) kmalloc_old(size); } static inline void free_virt_pages(void *p) { - kfree((void *)p); + kfree_old((void *)p); } static inline void *get_phys_pages(unsigned int n) { /* assert(n>0); size_t size = n << PAGE_SHIFT; - return (void*) va2pa(kmalloc(size)); + return (void*) va2pa(kmalloc_old(size)); */ return (void *)va2pa(get_virt_pages(n)); } diff --git a/kernel/exec.c b/kernel/exec.c index f8d3557..a3390ba 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -43,7 +43,7 @@ int sysc_exec(const char *path, char *const argv[]) printk("file %s is not exist\n", path); return -ENOENT; } - buf = (void*)kmalloc(filesz); + buf = (void*)kmalloc_old(filesz); sysc_read(fd, buf, filesz); #if 0 @@ -56,7 +56,7 @@ int sysc_exec(const char *path, char *const argv[]) if(strncmp(ELFMAG, ehdr->e_ident, sizeof(ELFMAG)-1) != 0) { printk("file %s can not execute\n", path); - kfree(buf); + kfree_old(buf); return -ENOEXEC; } //printk("Entry: %08x phnum:%d\n", ehdr->e_entry, ehdr->e_phnum); @@ -76,7 +76,7 @@ int sysc_exec(const char *path, char *const argv[]) } } - char *exe = (char *) kmalloc(size); + char *exe = (char *) kmalloc_old(size); for(i=0; ie_phnum; i++) { pElf32_Phdr phdr; @@ -190,7 +190,7 @@ int sysc_exec(const char *path, char *const argv[]) printk("stack pt: %08x pde:%08x %08x %08x\n", pt, pde, pd[get_npd(KRNLADDR)-1]); #endif - kfree(buf); + kfree_old(buf); //printk("eip: %08x \n", regs->eip); diff --git a/kernel/innerint.c b/kernel/innerint.c index 58fcd91..ec460ba 100644 --- a/kernel/innerint.c +++ b/kernel/innerint.c @@ -97,7 +97,7 @@ void doPageFault(PtRegs regs) current, errcode, addr, cr3); */ - if((errcode & (1UL<next; } - p = kmalloc(sizeof(IrqAction)); + p = kmalloc_old(sizeof(IrqAction)); if(p == NULL) return -ENOMEM; diff --git a/kernel/sched.c b/kernel/sched.c index 4172ecc..f60ef17 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -72,7 +72,7 @@ void init_root_tsk() current = root_task; /* // 栈 - void *stack = kmalloc(PAGE_SIZE); + void *stack = kmalloc_old(PAGE_SIZE); if(stack == NULL) panic("stack"); stack = va2pa(stack); @@ -80,7 +80,7 @@ void init_root_tsk() printk("Stack : %08x\n", stack); u32 *pde = pa2va(current->cr3); - u32 *pte = (u32 *)kmalloc(PAGE_SIZE); + u32 *pte = (u32 *)kmalloc_old(PAGE_SIZE); if(pte == NULL) panic("root task stack pte"); pte[1023] = stack + 7; diff --git a/kernel/test_task.c b/kernel/test_task.c index 4aab2aa..2dce581 100644 --- a/kernel/test_task.c +++ b/kernel/test_task.c @@ -37,7 +37,7 @@ void add_task(void *fun) #if 0 assert(fun != NULL); pTask tsk = NULL; - tsk = kmalloc(sizeof(Task)); + tsk = kmalloc_old(sizeof(Task)); if(tsk == NULL) panic("shit happens"); @@ -71,7 +71,7 @@ void add_task(void *fun) { if(tTasks[i] == NULL) { - tsk = kmalloc(sizeof(Task)); + tsk = kmalloc_old(sizeof(Task)); if(tsk == NULL) panic("shit happens"); //tTasks[i] = tsk; diff --git a/mm/kmalloc.c b/mm/kmalloc.c index 443ae80..b6f7b9a 100644 --- a/mm/kmalloc.c +++ b/mm/kmalloc.c @@ -1,6 +1,6 @@ /* *-------------------------------------------------------------------------- - * File Name: kmalloc.c + * File Name: kmalloc_old.c * * Author: Zhao Yanbai [zhaoyanbai@126.com] * Sat Jan 30 12:15:51 2010 @@ -31,7 +31,7 @@ static get_order(size_t size) //printk("i:%d\n", i); return i; } -void *kmalloc(size_t size) +void *kmalloc_old(size_t size) { assert(0mapNR, page->mapNR<mapNR, page->mapNR<mapNR<>PAGE_SHIFT); //printk("%08x\n", page->mapNR); old_free_pages(page); diff --git a/mm/page.c b/mm/page.c index 5181754..8b1c630 100644 --- a/mm/page.c +++ b/mm/page.c @@ -63,7 +63,7 @@ void do_no_page(void *addr) #if 0 u32 *pde = (u32*)pa2va(current->cr3); u32 *pte; - void *page = (void*)va2pa(kmalloc(PAGE_SIZE)); + void *page = (void*)va2pa(kmalloc_old(PAGE_SIZE)); if(page == NULL) panic("failed alloc page"); @@ -73,7 +73,7 @@ void do_no_page(void *addr) if(pde[npde] == 0) { printk("*a*"); - pte = (u32 *) kmalloc(PAGE_SIZE); + pte = (u32 *) kmalloc_old(PAGE_SIZE); memset((void*)pte, 0, PAGE_SIZE); if(pte == NULL) panic("failed alloc pte"); diff --git a/mm/slub.c b/mm/slub.c index 0ab0a18..2c817b6 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -12,26 +12,6 @@ list_head_t slub_caches = LIST_HEAD_INIT(slub_caches); -typedef struct kmem_cache -{ - const char *name; - - unsigned long objsize; - unsigned long size; - unsigned long align; - unsigned long order; - unsigned long objects; - - unsigned int partial_cnt; - list_head_t partial; - - page_t *page; - -// void **freelist; - - list_head_t list; -} kmem_cache_t; - #define SLUB_MIN_SHIFT 5 #define SLUB_MAX_SHIFT 12 @@ -41,8 +21,6 @@ typedef struct kmem_cache static kmem_cache_t kmalloc_caches[SLUB_INIT_CACHE_SIZE]; -typedef unsigned int gfp_t; - static bool calculate_params(kmem_cache_t *cache) { // calculate size @@ -101,7 +79,6 @@ static bool kmem_cache_init(kmem_cache_t *cache, cache->objsize = size; cache->align = align; cache->page = 0; - //cache->freelist = 0; cache->partial_cnt = 0; INIT_LIST_HEAD(&(cache->partial)); @@ -140,7 +117,6 @@ static page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) if(0 == page) return 0; - end = bgn + cache->objects*cache->size; unsigned long last = bgn; @@ -155,6 +131,7 @@ static page_t *new_slub(kmem_cache_t *cache, gfp_t gfpflags) page->freelist = (void **)bgn; page->inuse = 0; + page->cache = cache; return page; } @@ -199,6 +176,9 @@ static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) { void **object = 0; + if(cache == 0) + return 0; + unsigned long objsize = cache->objsize; if(cache->page == 0 || cache->page->freelist == 0) @@ -251,6 +231,11 @@ static void slub_free(kmem_cache_t *cache, page_t *page, void *addr) } } +void *kmem_cache_alloc(kmem_cache_t *cache, gfp_t gfpflags) +{ + return slub_alloc(cache, gfpflags); +} + void kmem_cache_free(kmem_cache_t *cache, void *addr) { page_t *page = 0; @@ -260,6 +245,32 @@ void kmem_cache_free(kmem_cache_t *cache, void *addr) slub_free(cache, page, addr); } +void *kmalloc(size_t size, gfp_t gfpflags) +{ + unsigned int i; + kmem_cache_t *cache = 0; + + for(i=0; iobjsize > size) + { + cache = p; + break; + } + } + + return kmem_cache_alloc(cache, gfpflags); +} + +void kfree(void *addr) +{ + page_t *page = get_head_page(va2page((unsigned long)addr)); + kmem_cache_t *cache = page->cache; + + slub_free(cache, page, addr); +} + void init_slub_system() { @@ -269,9 +280,7 @@ void init_slub_system() for(i=SLUB_MIN_SHIFT; iinx = i; + kmem_cache_init(cache, "kmalloc_old", 1UL<list), &slub_caches); } diff --git a/setup/system.c b/setup/system.c index b795811..c5e6881 100644 --- a/setup/system.c +++ b/setup/system.c @@ -137,8 +137,8 @@ void setup_irqs() pIRQAction pKbdAction, pClkAction; - pKbdAction = (pIRQAction) kmalloc(sizeof(IRQAction)); - pClkAction = (pIRQAction) kmalloc(sizeof(IRQAction)); + pKbdAction = (pIRQAction) kmalloc_old(sizeof(IRQAction)); + pClkAction = (pIRQAction) kmalloc_old(sizeof(IRQAction)); assert(pKbdAction != NULL);