]> Zhao Yanbai Git Server - kernel.git/commitdiff
add some code to support slub
authorAceVest <zhaoyanbai@126.com>
Tue, 15 Apr 2014 15:54:43 +0000 (23:54 +0800)
committerAceVest <zhaoyanbai@126.com>
Tue, 15 Apr 2014 15:54:43 +0000 (23:54 +0800)
Makefile
boot/multiboot.S
drivers/keyboard.c.bake [deleted file]
include/page.h
include/types.h
mm/buddy.c
mm/mm.c
mm/slub.c [new file with mode: 0644]
scripts/copy.sh [moved from copy.sh with 97% similarity]
scripts/link.ld

index 5009e7f6fe757e2721914177963b6bbcc8156d79..266ccb72b2309b13c6f5e1bb1ba1f01109b8efb5 100644 (file)
--- 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
index 2c98aa9be7e84e5f1ea688430002ecb8ff39bfff..36675abe523f898b9f78995302a5370ab67f1dee 100644 (file)
@@ -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 (file)
index 0dd77d9..0000000
+++ /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<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();
-}
index 930a24d4c87469818cb9aef0c0eee9064946014b..6d17634bab2835ad848dda654cd2c545d77e4a65 100644 (file)
  *--------------------------------------------------------------------------
  */
 
-#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 <types.h>
 #include <bits.h>
 #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,
index 5aff1f349dd19c964edcf33c226c672681ae0cdf..8d5fc302d29f3ba60770c0646d86bcf19d690d9c 100644 (file)
  *--------------------------------------------------------------------------
  */
 
-#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 ...
index 934c7f46072b568726def0cbc2d1350e5042ea50..62e3ad9cb6088b363aedbebc5b892796cf56791d 100644 (file)
@@ -9,7 +9,6 @@
 #include <mm.h>
 #include <sysctl.h>
 
-#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 544f33607af0138af041b9735d6f5c8a9102bcef..22f02a5e5a1764774cb5d7f029fa5b5d6b34d178 100644 (file)
--- 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 (file)
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 <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);
+    }
+}
similarity index 97%
rename from copy.sh
rename to scripts/copy.sh
index 8a6d398b34e4cffb4eb2975e6149100cf9963133..efdd1dba160f3d47c714f54099f14b45cbdaf44d 100755 (executable)
--- a/copy.sh
@@ -7,7 +7,6 @@ lodev=`losetup -f --show $HDIMG`
 partx -a ${lodev}
 
 PART=${lodev}p1
-#sleep 1
 
 mount $PART /mnt/
 
index 5a47fb63d7e4bff00a6478e948ee1fbe8945ea2b..4095c8a6ddb9d0a8f01cdc92efba7737dddd2f20 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 OUTPUT_FORMAT("elf32-i386")
-ENTRY(_start)
+ENTRY(kernel_entry)
 kernel_loaded_physic_addr = 0x100000;
 kernel_virtual_addr_start = 0xC0000000;
 kernel_begin = kernel_virtual_addr_start + kernel_loaded_physic_addr;