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:
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 */
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++)
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;
}
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");
memcpy(buf, p, count);
- kfree(p);
+ kfree_old(p);
return count;
}
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; i<blks; i++)
#if 0
buf = load_inode_content(in);
#else
- buf = kmalloc(in->i_size);
+ buf = kmalloc_old(in->i_size);
ext2_read_file(in, buf, in->i_size);
#endif
ent = (pDirEnt)(ent->rec_len + (unsigned long)ent);
}
- kfree(buf);
+ kfree_old(buf);
ext2_read_inode(inode_n, out);
unsigned long bootmem_page_state(unsigned long pfn);
#define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE)
+
#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
{
unsigned int order;
void **freelist; // for slub
+ kmem_cache_t *cache;
+
unsigned long inuse;
} page_t;
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_
{
#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));
}
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
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);
}
}
- char *exe = (char *) kmalloc(size);
+ char *exe = (char *) kmalloc_old(size);
for(i=0; i<ehdr->e_phnum; i++)
{
pElf32_Phdr phdr;
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);
current, errcode, addr, cr3);
*/
- if((errcode & (1UL<<PAGE_P)) == 0)
+ if((errcode & PAGE_P) == 0)
{
extern void do_no_page(void *);
do_no_page(addr);
p = p->next;
}
- p = kmalloc(sizeof(IrqAction));
+ p = kmalloc_old(sizeof(IrqAction));
if(p == NULL)
return -ENOMEM;
current = root_task;
/*
// 栈
- void *stack = kmalloc(PAGE_SIZE);
+ void *stack = kmalloc_old(PAGE_SIZE);
if(stack == NULL)
panic("stack");
stack = va2pa(stack);
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;
#if 0
assert(fun != NULL);
pTask tsk = NULL;
- tsk = kmalloc(sizeof(Task));
+ tsk = kmalloc_old(sizeof(Task));
if(tsk == NULL)
panic("shit happens");
{
if(tTasks[i] == NULL)
{
- tsk = kmalloc(sizeof(Task));
+ tsk = kmalloc_old(sizeof(Task));
if(tsk == NULL)
panic("shit happens");
//tTasks[i] = tsk;
/*
*--------------------------------------------------------------------------
- * File Name: kmalloc.c
+ * File Name: kmalloc_old.c
*
* Author: Zhao Yanbai [zhaoyanbai@126.com]
* Sat Jan 30 12:15:51 2010
//printk("i:%d\n", i);
return i;
}
-void *kmalloc(size_t size)
+void *kmalloc_old(size_t size)
{
assert(0<size && size<=32*PAGE_SIZE);
int order = get_order(size);
if(page == NULL)
return NULL;
- //printk("kmalloc:%08x %08x ", page->mapNR, page->mapNR<<PAGE_SHIFT);
+ //printk("kmalloc_old:%08x %08x ", page->mapNR, page->mapNR<<PAGE_SHIFT);
p = (void*)pa2va(page->mapNR<<PAGE_SHIFT);
- //printk("kmalloc: %08x\n", p);
+ //printk("kmalloc_old: %08x\n", p);
return p;
}
-void kfree(void *p)
+void kfree_old(void *p)
{
assert(p != NULL);
pPage page = system.page_map;
- //printk("kfree:%08x %08x %08x ", p, va2pa(p));
+ //printk("kfree_old:%08x %08x %08x ", p, va2pa(p));
page += ((unsigned long)va2pa(p)>>PAGE_SHIFT);
//printk("%08x\n", page->mapNR);
old_free_pages(page);
#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");
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");
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
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
cache->objsize = size;
cache->align = align;
cache->page = 0;
- //cache->freelist = 0;
cache->partial_cnt = 0;
INIT_LIST_HEAD(&(cache->partial));
if(0 == page)
return 0;
-
end = bgn + cache->objects*cache->size;
unsigned long last = bgn;
page->freelist = (void **)bgn;
page->inuse = 0;
+ page->cache = cache;
return page;
}
{
void **object = 0;
+ if(cache == 0)
+ return 0;
+
unsigned long objsize = cache->objsize;
if(cache->page == 0 || cache->page->freelist == 0)
}
}
+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;
slub_free(cache, page, addr);
}
+void *kmalloc(size_t size, gfp_t gfpflags)
+{
+ unsigned int i;
+ kmem_cache_t *cache = 0;
+
+ for(i=0; i<SLUB_INIT_CACHE_SIZE; ++i)
+ {
+ kmem_cache_t *p = kmalloc_caches + i;
+ if(p->objsize > 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()
{
for(i=SLUB_MIN_SHIFT; i<SLUB_MAX_SHIFT; ++i)
{
cache = kmalloc_caches + i - SLUB_MIN_SHIFT;
- kmem_cache_init(cache, "kmalloc", 1UL<<i, KMALLOC_MIN_ALIGN);
-
- //cache->inx = i;
+ kmem_cache_init(cache, "kmalloc_old", 1UL<<i, KMALLOC_MIN_ALIGN);
list_add(&(cache->list), &slub_caches);
}
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);