From 0040fa8b6ffa8b972ec3d5af038a610382641b6b Mon Sep 17 00:00:00 2001 From: AceVest Date: Tue, 15 Apr 2014 23:54:43 +0800 Subject: [PATCH] add some code to support slub --- Makefile | 9 +- boot/multiboot.S | 4 +- drivers/keyboard.c.bake | 265 ------------------------------------- include/page.h | 28 ++-- include/types.h | 27 ++-- mm/buddy.c | 4 +- mm/mm.c | 5 +- mm/slub.c | 123 +++++++++++++++++ copy.sh => scripts/copy.sh | 1 - scripts/link.ld | 2 +- 10 files changed, 168 insertions(+), 300 deletions(-) delete mode 100644 drivers/keyboard.c.bake create mode 100644 mm/slub.c rename copy.sh => scripts/copy.sh (97%) diff --git a/Makefile b/Makefile index 5009e7f..266ccb7 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,10 @@ $(KERNELBIN): .PHONY:real real: +.PHONY:c +c: + rm -f $(KERNELBIN) + .PHONY:clean clean: cd boot && make clean @@ -40,6 +44,9 @@ clean: rm -f $(KERNELBIN) System.map snapshot.txt log.txt rm -f KRNL.ELF +.PHONY:install +install: + cp KERNEL.BIN /boot/ .PHONY:copy copy: - ./copy.sh + ./scripts/copy.sh diff --git a/boot/multiboot.S b/boot/multiboot.S index 2c98aa9..36675ab 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -16,7 +16,7 @@ #define ASM #include "boot/boot.h" #include "system.h" -.global _start +.global kernel_entry .extern CheckKernel .extern SetupKernel .extern KernelEntry @@ -39,7 +39,7 @@ .text .code32 .align 32 -_start: +kernel_entry: # Reset EFLAGS pushl $0 popf diff --git a/drivers/keyboard.c.bake b/drivers/keyboard.c.bake deleted file mode 100644 index 0dd77d9..0000000 --- a/drivers/keyboard.c.bake +++ /dev/null @@ -1,265 +0,0 @@ -/* - *-------------------------------------------------------------------------- - * File Name: keyboard.h - * - * Description: none - * - * - * Author: Zhao Yanbai [zhaoyanbai@126.com] - * - * Version: 1.0 - * Create Date: Thu Jul 16 18:39:57 2009 - * Last Update: Thu Jul 16 18:39:57 2009 - * - *-------------------------------------------------------------------------- - */ -#include -#include -#include -#include -#include -#define EXT_KEY 0x80000000 /* None Print Key */ -#define L_SHIFT_DOWN 0x00000100 -#define R_SHIFT_DOWN 0x00000200 -#define L_CTRL_DOWN 0x00000400 -#define R_CTRL_DOWN 0x00000800 -#define L_ALT_DOWN 0x00001000 -#define R_ALT_DOWN 0x00002000 -#define L_SHIFT_UP (~L_SHIFT_DOWN) -#define R_SHIFT_UP (~R_SHIFT_DOWN) -#define L_CTRL_UP (~L_CTRL_DOWN) -#define R_CTRL_UP (~R_CTRL_DOWN) -#define L_ALT_UP (~L_ALT_DOWN) -#define R_ALT_UP (~R_ALT_DOWN) -#define SET_L_SHIFT_DOWN(key) (key |= L_SHIFT_DOWN) -#define SET_R_SHIFT_DOWN(key) (key |= R_SHIFT_DOWN) -#define SET_L_CTRL_DOWN(key) (key |= L_CTRL_DOWN) -#define SET_R_CTRL_DOWN(key) (key |= R_CTRL_DOWN) -#define SET_L_ALT_DOWN(key) (key |= L_ALT_DOWN) -#define SET_R_ALT_DOWN(key) (key |= R_ALT_DOWN) -#define SET_L_SHIFT_UP(key) (key &= L_SHIFT_UP) -#define SET_R_SHIFT_UP(key) (key &= R_SHIFT_UP) -#define SET_L_CTRL_UP(key) (key &= L_CTRL_UP) -#define SET_R_CTRL_UP(key) (key &= R_CTRL_UP) -#define SET_L_ALT_UP(key) (key &= L_ALT_UP) -#define SET_R_ALT_UP(key) (key &= R_ALT_UP) -#define IS_L_SHIFT_DOWN(key) (key & L_SHIFT_DOWN) -#define IS_R_SHIFT_DOWN(key) (key & R_SHIFT_DOWN) -#define IS_L_CTRL_DOWN(key) (key & L_CTRL_DOWN) -#define IS_R_CTRL_DOWN(key) (key & R_CTRL_DOWN) -#define IS_L_ALT_DOWN(key) (key & L_ALT_DOWN) -#define IS_R_ALT_DOWN(key) (key & R_ALT_DOWN) - -const unsigned char kbdCharTable[]={0,0, -'1','2','3','4','5','6','7','8','9','0','-','=','\b',0, -'q','w','e','r','t','y','u','i','o','p','[',']','\n',0, -'a','s','d','f','g','h','j','k','l',';','\'','`',0,'\\', -'z','x','c','v','b','n','m',',','.','/',0,0,0,' ', -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -}; -const unsigned char kbdShiftCharTable[]={0,0, -'!','@','#','$','%','^','&','*','(',')','_','+','\b',0, -'Q','W','E','R','T','Y','U','I','O','P','{','}','\n',0, -'A','S','D','F','G','H','J','K','L',':','"','~',0,'|', -'Z','X','C','V','B','N','M','<','>','?',0,0,0,' ', -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -}; - -/* Make Code */ -#define MC_BACKSPACE 0x0E -#define MC_CTRL 0x1D -#define MC_L_SHIFT 0x2A -#define MC_R_SHIFT 0x36 -#define MC_ALT 0x38 -#define MC_CAPSLOCK 0x3A -#define MC_DELETE 0x53 -#define BC_BACKSPACE (0x80 | MC_BACKSPACE) -#define BC_CTRL (0x80 | MC_CTRL) -#define BC_L_SHIFT (0x80 | MC_L_SHIFT) -#define BC_R_SHIFT (0x80 | MC_R_SHIFT) -#define BC_ALT (0x80 | MC_ALT) -#define BC_DELETE (0x80 | MC_DELETE) -#define BC_CAPSLOCK (0x80 | MC_CAPSLOCK) - -#define KBD_BUF_SIZE 256 -static struct -{ - unsigned int count; - unsigned int head,tail; - unsigned char buf[KBD_BUF_SIZE]; -} kbd_buf; -#define count kbd_buf.count -#define head kbd_buf.head -#define tail kbd_buf.tail -#define buf kbd_buf.buf - -void kbd_handler(unsigned int irq, pPtRegs regs, void *dev_id) -{ - unsigned char ScanCode; - //printk("%s\n", dev_id); - ScanCode = inb(0x60); - //printk("%02x", ScanCode); - if(count < KBD_BUF_SIZE) - { - count++; - buf[tail++] = ScanCode; - tail %= KBD_BUF_SIZE; - } -} - -inline int getScanCode() -{ - unsigned int ScanCode; - - //while(count <= 0); - if(count <= 0) return -1; - - ScanCode = buf[head++]; - head %= KBD_BUF_SIZE; - count--; //很明显这是临界资源但现在只能这样了 - - return (0xFF & ScanCode); -} - - -unsigned char read_kbd() -{ - char k; - int ret = -1; - - while(ret == -1) - { - syscall0(SYSC_READ_KBD); - asm("":"=a"(ret)); - } - - k = (unsigned char) ret; - - return k; -} - -unsigned int _parse_kbd() -{ - int ScanCode; - static unsigned int key; - static unsigned char E0Flag = 0; - ScanCode = read_kbd(); - - if(ScanCode == 0xE0) - { - E0Flag = 1; - ScanCode = read_kbd(); - } - - if(ScanCode & 0x80) // Make Or Break Code ? - goto BreakCode; - - // Ctrl + Alt + Del - if( IS_L_CTRL_DOWN(key) && IS_L_ALT_DOWN(key)) - { - if(E0Flag == 1 && ScanCode == MC_DELETE) - { - extern void reboot(); - printf("Reboot System Now ...\n"); - int i = 100000; - while(i--); - reboot(); - } - if(ScanCode == MC_BACKSPACE) - { - extern void poweroff(); - printf("Shutdown System Now ...\n"); - int i = 100000; - while(i--); - poweroff(); - } - } - - - switch(ScanCode) - { - case MC_L_SHIFT: SET_L_SHIFT_DOWN(key); goto End; - case MC_R_SHIFT: SET_R_SHIFT_DOWN(key); goto End; - - case MC_CTRL: - E0Flag?SET_R_CTRL_DOWN(key):SET_L_CTRL_DOWN(key); - goto End; - case MC_ALT: - - E0Flag?SET_R_ALT_DOWN(key):SET_L_ALT_DOWN(key); - goto End; - } - - goto End; - -BreakCode: - switch(ScanCode) - { - case BC_L_SHIFT: SET_L_SHIFT_UP(key); goto End; - case BC_R_SHIFT: SET_R_SHIFT_UP(key); goto End; - case BC_CTRL: - E0Flag?SET_R_CTRL_UP(key):SET_L_CTRL_UP(key); - goto End; - case BC_ALT: - E0Flag?SET_R_ALT_UP(key):SET_L_ALT_UP(key); - goto End; - } - -End: - - key &= 0xFFFFFF00; - key |= (((E0Flag)?0x00:ScanCode) & 0xFF); - - E0Flag = 0; - - return key; - -} - - - -void ParseKbdInput() -{ - unsigned int k; - unsigned int inx; - unsigned char chr; - static unsigned char CapsLock; - - k = _parse_kbd(); - - if(k == -1) return ; - - inx = k & 0xFF; - chr = 0; - - if(inx >= sizeof(kbdCharTable)) goto End; - - // CapsLock ON OR OFF - if(inx == MC_CAPSLOCK) - CapsLock = (CapsLock)?0:1; - - - //if((!IS_L_SHIFT_DOWN(k) && !IS_R_SHIFT_DOWN(k) && CapsLock == 0) - //|| ((IS_L_SHIFT_DOWN(k) || IS_R_SHIFT_DOWN(k)) && CapsLock ==1)) - if(!IS_L_SHIFT_DOWN(k) && !IS_R_SHIFT_DOWN(k)) - { - chr = kbdCharTable[inx]; - if(CapsLock ==1 && chr >= 'a' && chr <= 'z') - chr -= 32; - } - else - { - chr = kbdShiftCharTable[inx]; - if(CapsLock ==1 && chr >= 'A' && chr <= 'Z') - chr += 32; - } - -End: - if(chr != 0) - printf("%c", chr); -} - -int sysc_read_kbd() -{ - return getScanCode(); -} diff --git a/include/page.h b/include/page.h index 930a24d..6d17634 100644 --- a/include/page.h +++ b/include/page.h @@ -14,21 +14,21 @@ *-------------------------------------------------------------------------- */ -#ifndef _PAGE_H +#ifndef _PAGE_H #define _PAGE_H -#define PAGE_P 0x0 -#define PAGE_WR 0x1 -#define PAGE_US 0x2 +#define PAGE_P 0x0 +#define PAGE_WR 0x1 +#define PAGE_US 0x2 -#define PAGE_SHIFT (12) -#define PAGE_SIZE (1UL << PAGE_SHIFT) -#define PAGE_MASK (~((1UL << PAGE_SHIFT)-1)) -#define PAGE_OFFSET (0xC0000000) +#define PAGE_SHIFT (12) +#define PAGE_SIZE (1UL << PAGE_SHIFT) +#define PAGE_MASK (~((1UL << PAGE_SHIFT)-1)) +#define PAGE_OFFSET (0xC0000000) -#ifndef ASM +#ifndef ASM #include #include #define get_npd(vaddr) (((u32)(vaddr))>>22) @@ -59,13 +59,13 @@ typedef unsigned long pte_t; #define valid_va(addr) ((addr) >= PAGE_OFFSET) -#define PFN_UP(addr) (((addr) + PAGE_SIZE - 1) >> PAGE_SHIFT) -#define PFN_DW(addr) ((addr) >> PAGE_SHIFT) +#define PFN_UP(addr) (((addr) + PAGE_SIZE - 1) >> PAGE_SHIFT) +#define PFN_DW(addr) ((addr) >> PAGE_SHIFT) +#define MAX_ORDER (5) +#define MAX_OLD_ORDER (11) -#define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde))) - -#define MAX_OLD_ORDER (11) +#define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde))) enum page_flags { PG_Private, diff --git a/include/types.h b/include/types.h index 5aff1f3..8d5fc30 100644 --- a/include/types.h +++ b/include/types.h @@ -14,28 +14,29 @@ *-------------------------------------------------------------------------- */ -#ifndef _TYPES_H +#ifndef _TYPES_H #define _TYPES_H -typedef unsigned int size_t; -typedef int ssize_t; +typedef unsigned int size_t; +typedef int ssize_t; -typedef signed char s8; -typedef signed short s16; -typedef signed long s32; -typedef signed long long s64; +typedef signed char s8; +typedef signed short s16; +typedef signed long s32; +typedef signed long long s64; -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned long u32; -typedef unsigned long long u64; +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned long u32; +typedef unsigned long long u64; -typedef unsigned long pid_t; -typedef unsigned long mode_t; +typedef unsigned long pid_t; +typedef unsigned long mode_t; #define NULL ((void*)0) +typedef enum { false, true } bool; //========================================================================= //Define kinds of function's type ... diff --git a/mm/buddy.c b/mm/buddy.c index 934c7f4..62e3ad9 100644 --- a/mm/buddy.c +++ b/mm/buddy.c @@ -9,7 +9,6 @@ #include #include -#define MAX_ORDER 5 struct buddy_system { page_t *page_map; @@ -149,7 +148,7 @@ void dump_buddy_system() printk("\n"); } - +#if 0 printk("alloc 1 pages va 0x%08x\n", alloc_pages(0, 0)); printk("alloc 1 pages va 0x%08x\n", alloc_pages(0, 0)); printk("alloc 1 pages va 0x%08x\n", alloc_pages(0, 0)); @@ -162,6 +161,7 @@ void dump_buddy_system() printk("alloc 2 pages va 0x%08x\n", alloc_pages(0, 1)); printk("alloc 4 pages va 0x%08x\n", alloc_pages(0, 2)); printk("alloc 8 pages va 0x%08x\n", alloc_pages(0, 3)); +#endif } diff --git a/mm/mm.c b/mm/mm.c index 544f336..22f02a5 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -26,6 +26,7 @@ extern char kernel_begin, kernel_end; extern char etext,edata,end; extern void init_buddy_system(); +extern void init_slub_system(); static void e820_print_type(unsigned long type) { @@ -337,5 +338,7 @@ void init_mm() init_paging(); printk("init buddy system...\n"); init_buddy_system(); - printk("memory init finish...\n"); + printk("init slub system...\n"); + init_slub_system(); + printk("memory init finished...\n"); } diff --git a/mm/slub.c b/mm/slub.c new file mode 100644 index 0000000..10b2ee9 --- /dev/null +++ b/mm/slub.c @@ -0,0 +1,123 @@ +/* + * ------------------------------------------------------------------------ + * File Name: slub.c + * Author: Zhao Yanbai + * Wed Apr 9 16:51:07 2014 + * Description: none + * ------------------------------------------------------------------------ + */ + +#include +#include + +list_head_t 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; +} kmem_cache_t; + + +#define SLUB_PAGE_SHIFT PAGE_SHIFT +#define KMALLOC_MIN_SIZE 32 +#define KMALLOC_MIN_ALIGN 32 +static kmem_cache_t kmalloc_caches[SLUB_PAGE_SHIFT]; + +typedef unsigned int gfp_t; + +static bool calculate_params(kmem_cache_t *cache) +{ + // calculate size + unsigned long size = cache->objsize; + unsigned long align= cache->align; + align = KMALLOC_MIN_ALIGN > align ? KMALLOC_MIN_ALIGN : align; + size = ALIGN(size, align); + size = ALIGN(size, sizeof(void *)); + cache->size = size; + + // calculate order + unsigned long order; + for(order=1; ordersize >= 4) + { + cache->order = order; + break; + } + } + + if(0 == cache->order) + { + printk("can not find a valid order\n"); + return false; + } + + + cache->objects = (PAGE_SIZE << cache->order) / cache->size; + + if(0 == cache->objects) + return false; + + return true; +} + +kmem_cache_t *kmem_cache_create(const char *name, + size_t size, + size_t align) +{ + + + return 0; +} + + +static bool kmem_cache_init(kmem_cache_t *cache, + const char *name, + size_t size, + size_t align) +{ + + memset(cache, 0, sizeof(kmem_cache_t)); + + cache->name = name; + cache->objsize = size; + cache->align = align; + cache->page = 0; + cache->freelist = 0; + cache->partial_cnt = 0; + INIT_LIST_HEAD(&(cache->partial)); + + if(!calculate_params(cache)) + goto err; + + return true; + +err: + panic("kmem_cache_init can not create cache\n"); + return false; +} + +void init_slub_system() +{ + unsigned int i; + kmem_cache_t *cache; + + for(i=5; i