+++ /dev/null
-/*
- *--------------------------------------------------------------------------
- * 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<printk.h>
-#include<system.h>
-#include<syscall.h>
-#include<stdio.h>
-#include<io.h>
-#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();
-}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * File Name: slub.c
+ * Author: Zhao Yanbai
+ * Wed Apr 9 16:51:07 2014
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#include <mm.h>
+#include <system.h>
+
+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; order<MAX_ORDER; ++order)
+ {
+ if((PAGE_SIZE<<order) / cache->size >= 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<SLUB_PAGE_SHIFT; ++i)
+ {
+ cache = kmalloc_caches + i;
+ kmem_cache_init(cache, "kmalloc", 1UL<<i, KMALLOC_MIN_ALIGN);
+ }
+}