]> Zhao Yanbai Git Server - kernel.git/commitdiff
add init_paging.
authorAceVest <zhaoyanbai@126.com>
Fri, 4 Apr 2014 09:55:19 +0000 (17:55 +0800)
committerAceVest <zhaoyanbai@126.com>
Fri, 4 Apr 2014 09:55:19 +0000 (17:55 +0800)
.bochsrc [new file with mode: 0644]
.gitignore [new file with mode: 0644]
boot/boot.c
boot/multiboot.S
include/boot/boot.h [moved from include/boot/bootparam.h with 75% similarity]
include/mm.h
include/page.h
mm/mm.c
mm/page.c
setup/setup.c

diff --git a/.bochsrc b/.bochsrc
new file mode 100644 (file)
index 0000000..22d3edc
--- /dev/null
+++ b/.bochsrc
@@ -0,0 +1,53 @@
+# configuration file generated by Bochs
+megs: 128
+plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, iodebug=1
+config_interface: textconfig
+display_library: x
+memory: host=128, guest=128
+romimage: file="/usr/local/share/bochs/BIOS-bochs-latest"
+vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest"
+boot: disk
+floppy_bootsig_check: disabled=1
+# no floppya
+# no floppyb
+ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
+ata0-master: type=disk, mode=flat, translation=auto, path="./HD.IMG", cylinders=48, heads=16, spt=63, biosdetect=auto, model="Generic 1234"
+ata0-slave: type=cdrom, path="grub.iso", status=inserted, biosdetect=auto, model="Generic 1234"
+ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
+ata2: enabled=0
+ata3: enabled=0
+parport1: enabled=1, file=""
+parport2: enabled=0
+com1: enabled=1, mode=null, dev=""
+com2: enabled=0
+com3: enabled=0
+com4: enabled=0
+cpuid: vendor_string="GenuineIntel"
+cpuid: brand_string="              Intel(R) Pentium(R) 4 CPU        "
+
+usb_uhci: enabled=0
+usb_ohci: enabled=0
+print_timestamps: enabled=0
+debugger_log: -
+magic_break: enabled=1
+port_e9_hack: enabled=0
+private_colormap: enabled=0
+clock: sync=none, time0=local
+# no cmosimage
+ne2k: enabled=0
+pnic: enabled=0
+sb16: enabled=0
+# no loader
+log: -
+logprefix: %t%e%d
+panic: action=ask
+error: action=report
+info: action=report
+debug: action=ignore
+keyboard_type: mf
+keyboard_serial_delay: 250
+keyboard_paste_delay: 100000
+keyboard_mapping: enabled=1, map=/usr/local/share/bochs/keymaps/x11-pc-us.map
+user_shortcut: keys=none
+mouse: enabled=0
+#mouse: enabled=0, type=ps2, toggle=ctrl+mbutton
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..af33667
--- /dev/null
@@ -0,0 +1,26 @@
+# Object files
+*.o
+*.ko
+
+# Libraries
+*.lib
+*.a
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+
+*.IMG
+*.img
+*build*
+*.BIN
+*.map
+bin/hw
+bin/sh
index fe64161f9ea2318eb0833f3077b67604346b571e..69bff433c9d58e4a3d77df313f2b048bebf655b3 100644 (file)
@@ -12,8 +12,7 @@
 
 
 #include<system.h>
-#include<boot/multiboot.h>
-#include<boot/bootparam.h>
+#include<boot/boot.h>
 #include<page.h>
 #include<bits.h>
 #include<assert.h>
@@ -47,8 +46,6 @@ void init_boot_params(multiboot_info_t *p)
     }
 }
 
-extern void init_bootmem();
-
 void CheckKernel(unsigned long addr, unsigned long magic)
 {
        if(magic != MULTIBOOT_BOOTLOADER_MAGIC)
@@ -67,9 +64,6 @@ void CheckKernel(unsigned long addr, unsigned long magic)
 
     init_boot_params(mbi);
 
-    init_bootmem();
-
-    while(1);
 }
 
 #if 0
index b64d8fc7884a021ffbbd887a067de89e04918a01..f8ad7e76e8f887130f1102fafbbc733f94ec6a7c 100644 (file)
 /*
  *--------------------------------------------------------------------------
- *   File Name:        reboot.S
+ *   File Name: reboot.S
  * 
- * Description:        none
+ * Description: none
  * 
  * 
- *      Author:        Zhao Yanbai [zhaoyanbai@126.com]
+ *      Author: Zhao Yanbai [zhaoyanbai@126.com]
  * 
- *     Version:        1.0
+ *     Version:    1.0
  * Create Date: Mon Nov 10 15:58:46 2008
  * Last Update: Tue Feb 10 22:39:24 2009
  * 
  *--------------------------------------------------------------------------
  */
 #define ASM
-#include"boot/multiboot.h"
+#include"boot/boot.h"
 #include"system.h"
-.global        _start
+.global _start
 .extern CheckKernel
 .extern SetupKernel
-.extern        KernelEntry
+.extern KernelEntry
 .extern init_pgd
 .extern init_pgt
+.extern kernel_virtual_addr_start
 
 .section .multiboot_header
 .align 32
-       # Multiboot Header
-       # Align 32 bits boundary
-       .align  4
-       # Magic
-       .long   MULTIBOOT_HEADER_MAGIC
-       # Flags
-       .long   MULTIBOOT_HEADER_FLAGS
-       # Checksum
-       .long   -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
+    # Multiboot Header
+    # Align 32 bits boundary
+    .align 4
+    # Magic
+    .long MULTIBOOT_HEADER_MAGIC
+    # Flags
+    .long MULTIBOOT_HEADER_FLAGS
+    # Checksum
+    .long -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
 
 .text
 .code32
 .align 32
 _start:
-       # Reset EFLAGS
-       pushl   $0
-       popf
-
-       # Load GDT's Information To GDTR
-       lgdt    GDTR-KRNLADDR
-
-       mov     $0x10, %dx
-       mov     %dx,%ds
-       mov     %dx,%es
-       mov     %dx,%ss
-       mov     %dx,%fs
-       mov     %dx,%gs
-
-       movl    $(stack + MULTIBOOT_STACK_SIZE - KRNLADDR), %esp
-
-
-       # Save Multiboot Infomation...
-       pushl   %eax
-       addl    $KRNLADDR,%ebx
-       pushl   %ebx
-
-       # Setup Page 
-       movl    $init_pgd-KRNLADDR,%ebx
-       movl    %ebx,%edi
-       movl    $1024,%ecx
-       xorl    %eax,%eax
-       cld
-       rep
-       stosl
-
-       movl    %ebx,%edi
-       movl    %ebx,%eax
-       movl    %ebx,%ecx
-       addl    $7,%eax
-       addl    $0x1000,%eax
-       movl    %eax, 0000(%edi)
-       movl    %eax, 3072(%edi)
-       addl    $0x1000,%eax
-       movl    %eax, 0004(%edi)
-       movl    %eax, 3076(%edi)
-
-       movl    $init_pgt-KRNLADDR,%ebx
-       movl    %ebx,%edi
-       #addl   $0x1000,%edi
-       movl    $2048,%ecx
-       movl    $0x07,%eax
-       cld
+    # Reset EFLAGS
+    pushl   $0
+    popf
+
+    # Load GDT's Information To GDTR
+    lgdt    GDTR-KRNLADDR
+
+    movw    $0x10, %dx
+    movw    %dx,%ds
+    movw    %dx,%es
+    movw    %dx,%ss
+    movw    %dx,%fs
+    movw    %dx,%gs
+
+    movl    $(stack + MULTIBOOT_STACK_SIZE - KRNLADDR), %esp
+
+    # Save Multiboot Infomation...
+    pushl   %eax
+    addl    $KRNLADDR,%ebx
+    pushl   %ebx
+
+    # Setup Paging
+    # Clear Page Directory
+    movl    $init_pgd-KRNLADDR,%ebx
+    movl    %ebx,%edi
+    movl    $1024,%ecx
+    xorl    %eax,%eax
+    cld
+    rep
+    stosl
+
+    # Init Page Directory
+    movl    %ebx,%edi
+    movl    $init_pgt-KRNLADDR,%eax
+    addl    $7,%eax
+    movl    $BOOT_INIT_PAGETBL_CNT,%ecx
 1:
-       stosl
-       addl    $0x1000,%eax
-       loop    1b
-
-       movl    $init_pgd-KRNLADDR,%ebx
-       movl    %ebx,%cr3
-
-       # enable PG
-       movl    %cr0,%eax
-       orl     $0x80000000,%eax
-       movl    %eax,%cr0
-
-
-       jmp     2f
+    stosl
+    addl    $0x1000,%eax
+    loop    1b
+
+    movl    $kernel_virtual_addr_start,%eax
+    shrl    $20,%eax
+    addl    %ebx,%eax
+    movl    %eax,%edi
+
+    movl    $init_pgt-KRNLADDR,%eax
+    addl    $7,%eax
+    movl    $BOOT_INIT_PAGETBL_CNT,%ecx
 2:
-
-       ljmp    $0x08,$Label
+    stosl
+    addl    $0x1000,%eax
+    loop    2b
+    
+    # Init Page Table
+    movl    $init_pgt-KRNLADDR,%ebx
+    movl    %ebx,%edi
+    movl    $(BOOT_INIT_PAGETBL_CNT*1024),%ecx
+    movl    $0x07,%eax
+    cld
+3:
+    stosl
+    addl    $0x1000,%eax
+    loop    3b
+
+    movl    $init_pgd-KRNLADDR,%ebx
+    movl    %ebx,%cr3
+
+    # enable PG
+    movl    %cr0,%eax
+    orl     $0x80000000,%eax
+    movl    %eax,%cr0
+
+
+    jmp     4f
+4:
+
+    ljmp    $0x08,$Label
 Label:
-       call    CheckKernel
-       addl    $8,%esp
-       call    KernelEntry
+    call    CheckKernel
+    addl    $8,%esp
+    call    KernelEntry
 
-Die:   jmp     Die     # Should never come to here.
+Die:
+    jmp     Die    # Should never come to here.
 
 .align 32
 BootGDT:
-EMPT:  .long   0x00000000,0x00000000
-Code:  .long   0x0000FFFF,0x00CF9B00
-Data:  .long   0x0000FFFF,0x00CF9300
+EMPT: .long    0x00000000, 0x00000000
+Code: .long    0x0000FFFF, 0x00CF9B00
+Data: .long    0x0000FFFF, 0x00CF9300
 BootGDTEnd:
 GDTR:
-       GDTRLimit:      .word   BootGDTEnd-BootGDT
-       GDTRBase:       .long   BootGDT-KRNLADDR
+    GDTRLimit: .word BootGDTEnd-BootGDT
+    GDTRBase:  .long BootGDT-KRNLADDR
 
-       .comm   stack, MULTIBOOT_STACK_SIZE
+    .comm stack, MULTIBOOT_STACK_SIZE
similarity index 75%
rename from include/boot/bootparam.h
rename to include/boot/boot.h
index 494df9099c9793fe057248daec925e59b2769a1a..4fbef9eb48d77e7b948fe86a46524da17b5954be 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ------------------------------------------------------------------------
- *   File Name: bootparam.h
+ *   File Name: boot.h
  *      Author: Zhao Yanbai
  *              Sat Mar 29 09:02:21 2014
  * Description: none
 
 #include "multiboot.h"
 
-#define        E820_RAM        1
-#define        E820_RESERVED   2
-#define        E820_ACPI       3
-#define        E820_NVS        4
-#define E820_UNUSABLE 5
+#define BOOT_INIT_PAGETBL_CNT 2
 
-#define E820_MAP_CNT 128
+#ifndef ASM
+
+#define E820_RAM        1
+#define E820_RESERVED   2
+#define E820_ACPI       3
+#define E820_NVS        4
+#define E820_UNUSABLE   5
+
+#define E820_MAP_CNT    128
 
 struct e820_entry {
     unsigned long addr;
@@ -42,3 +46,5 @@ struct boot_params {
 };
 
 extern struct boot_params boot_params;
+
+#endif
index ef811040b15a886b2faaa9a5c0df7cd88e59a249..dbf85967ddb8a4362060f9d06b624a2f69118371 100644 (file)
@@ -9,5 +9,9 @@
 
 #pragma once
 
+#include <page.h>
 
 void *alloc_bootmem(unsigned long size, unsigned long align);
+
+#define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE)
+
index d45d9a8f8ac8ac25aaa26798e297aaa59625b2ea..3fa10386bba4e6405fb29db33eeee249285c9eb4 100644 (file)
 
 #include<list.h>
 
-#define        PAGE_ITEMS      (PAGE_SIZE/sizeof(unsigned long))
+typedef unsigned long pde_t;
+typedef unsigned long pte_t;
+
+#define        PDECNT_PER_PAGE (PAGE_SIZE/sizeof(pde_t))
+#define PTECNT_PER_PAGE (PAGE_SIZE/sizeof(pte_t))
+
+#define        PAGE_ITEMS (PAGE_SIZE/sizeof(unsigned long))
 #define        PAGE_ALIGN(page)        (page & PAGE_MASK)
 #define        PAGE_UP(page)   (((unsigned long)page + PAGE_SIZE -1) & PAGE_MASK)
 #define        PAGE_DOWN       PAGE_ALIGN
@@ -54,6 +60,9 @@
 #define PFN_UP(addr)     (((addr) + PAGE_SIZE - 1) >> PAGE_SHIFT)
 #define PFN_DW(addr)     ((addr) >> PAGE_SHIFT)
 
+
+#define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde)))
+
 #define        MAX_ORDER       (11)
 
 typedef struct page
diff --git a/mm/mm.c b/mm/mm.c
index c169b2fa06573ec0d96bb8d17076706d79079de6..7e11b7de27ff88631b3b1a364dec82b90fdfb789 100644 (file)
--- a/mm/mm.c
+++ b/mm/mm.c
@@ -19,7 +19,8 @@
 #include<types.h>
 #include<bits.h>
 #include<mm.h>
-#include<boot/bootparam.h>
+#include<init.h>
+#include<boot/boot.h>
 
 
 extern char kernel_begin, kernel_end;
@@ -82,6 +83,7 @@ bootmem_data_t bootmem_data;
 #define pfn2inx(pfn) ((pfn) - bootmem_data.min_pfn)
 #define inx2pfn(inx) ((inx) + bootmem_data.min_pfn)
 
+
 void e820_init_bootmem_data()
 {
     unsigned int i=0;
@@ -166,7 +168,7 @@ void register_bootmem_pages()
 
 void reserve_bootmem(unsigned long bgn_inx, unsigned long end_inx)
 {
-    printk("reserve %d %d\n", bgn_inx, end_inx);
+    //printk("reserve %d %d\n", bgn_inx, end_inx);
 
     int i=0;
     for(i=bgn_inx; i<end_inx; ++i)
@@ -213,12 +215,14 @@ void init_bootmem()
     e820_init_bootmem_data();
     init_bootmem_allocator();
     
+#if 0
     printk("alloc 10 bytes align 8    addr %08x\n", alloc_bootmem(10, 8));
     printk("alloc 40961 bytes align 4096 addr %08x\n", alloc_bootmem(40961, 4096));
     printk("alloc 5  bytes align 4    addr %08x\n", alloc_bootmem(5, 4));
     printk("alloc 10 bytes align 1024 addr %08x\n", alloc_bootmem(10, 1024));
     printk("alloc 123bytes align 2    addr %08x\n", alloc_bootmem(123, 2));
     printk("alloc 123bytes align 2    addr %08x\n", alloc_bootmem(123, 2));
+#endif
 }
 
 unsigned long align_bootmem_index(unsigned long inx, unsigned long align)
@@ -318,10 +322,51 @@ find_block:
     return 0;
 }
 
-void init_mm()
+
+
+pde_t __initdata init_pgd[PDECNT_PER_PAGE]                       __attribute__((__aligned__(PAGE_SIZE)));
+pte_t __initdata init_pgt[PTECNT_PER_PAGE*BOOT_INIT_PAGETBL_CNT] __attribute__((__aligned__(PAGE_SIZE)));
+
+void init_paging()
 {
+    unsigned long di, ti;
+    unsigned long *pde, *pte;
 
+    unsigned long pde_end = (pfn2pa(bootmem_data.max_pfn) >> 22);
+
+    for(di=BOOT_INIT_PAGETBL_CNT; di<pde_end; ++di)
+    {
+        pde = init_pgd + di;
+
+        if(di >= pde_end)
+        {
+            *pde = 0;
+            continue;
+        }
 
+        unsigned long pt_addr = (unsigned long) va2pa(bootmem_alloc_pages(1));
+
+        *pde = pt_addr | 7;
+        *(pde + (PAGE_OFFSET>>20)) = *pde; // for kernel paging
+
+        for(ti=0; ti<PTECNT_PER_PAGE; ++ti)
+        {
+            unsigned long page = (di << 22) | (ti << 12);
+
+            pte = ((pte_t *) pa2va(pt_addr)) + ti;
+
+            *pte = page | 7;
+            //printk("pde %08x pte %08x pt_addr %08x page %08x\n", pde, pte, pt_addr, page);
+        }
+    }
+
+    LOAD_CR3(init_pgd);
+}
+
+void init_mm()
+{
+    init_bootmem();
+    init_paging();
 }
 
 
index e73c0a779da907efd78cacc3842d4f7c85ae4a03..d8af96ad5674b68c5e8c136b80d55c912e84a3d8 100644 (file)
--- a/mm/page.c
+++ b/mm/page.c
 #include<assert.h>
 #include<printk.h>
 #include<init.h>
-
-#define INIT_PAGE_TABLE_CNT 2
-
-char __initdata init_pgd[PAGE_SIZE]                     __attribute__((__aligned__(PAGE_SIZE)));
-char __initdata init_pgt[PAGE_SIZE*INIT_PAGE_TABLE_CNT] __attribute__((__aligned__(PAGE_SIZE)));
-
-
-void init_paging()
-{
-    system.page_dir = (u32 *) init_pgd;
-
-       int i;
-       unsigned long kernel_pde_base = KRNLADDR>>22;
-       for(i=0; i<kernel_pde_base; ++i)
-    {
-               system.page_dir[i] = 0;
-    }
-
-    for(i=kernel_pde_base + INIT_PAGE_TABLE_CNT; i<1024; ++i)
-    {
-        
-    }
-}
+#include<mm.h>
 
 
 #define        get_page_from_list(pList) list_entry(pList, Page, list)
index 3507ec69cfacd7d101609b1f91e8d744d3feef2f..d7d5d638fbbf5e04ab58e1122bf6b3e038286a7c 100644 (file)
@@ -1,13 +1,13 @@
 /*
  *--------------------------------------------------------------------------
- *   File Name:        setup.c
+ *   File Name: setup.c
  * 
- * Description:        none
+ * Description: none
  * 
  * 
- *      Author:        Zhao Yanbai [zhaoyanbai@126.com]
+ *      Author: Zhao Yanbai [zhaoyanbai@126.com]
  * 
- *     Version:        1.0
+ *     Version:    1.0
  * Create Date: Wed Mar  4 20:34:36 2009
  * Last Update: Wed Mar  4 20:34:36 2009
  * 
 #include<printk.h>
 #include<system.h>
 
-extern void    setup_gdt();
-extern void    setup_idt();
-extern void    setup_gate();
-extern void    detect_cpu();
-extern void    setup_sysc();
-extern void    setup_pci();
-extern void    set_tss();
-extern void    show_logo();
-//extern void  setup_tTasks();
-extern void    setup_tasks();
-extern void    setup_root_dev();
-extern void    setup_hd();
-extern void    setup_fs();
-extern void    setup_ext2();
+extern void setup_gdt();
+extern void setup_idt();
+extern void setup_gate();
+extern void detect_cpu();
+extern void setup_sysc();
+extern void setup_pci();
+extern void set_tss();
+extern void show_logo();
+//extern void setup_tTasks();
+extern void setup_tasks();
+extern void setup_root_dev();
+extern void setup_hd();
+extern void setup_fs();
+extern void setup_ext2();
 
 extern unsigned long mb_mm_lower, mb_mm_upper;
 extern unsigned long mb_mmap_addr, mb_mmap_size;
@@ -40,35 +40,37 @@ extern void reboot();
 
 void setup_kernel()
 {
-       extern char _start, _end;
+    extern char _start, _end;
 
-       printk("kernel:\t%08x - %08x\n", &_start,
-                                        &_end);
+    printk("kernel:\t%08x - %08x\n", &_start, &_end);
 
-       setup_gdt();
-       setup_idt();
-       setup_gate();
+    init_mm();
 
-       detect_cpu();
+    while(1);
 
-       //setup_tTasks();
-       init_mm();
+    setup_gdt();
+    setup_idt();
+    setup_gate();
 
-       setup_irqs();
-       set_tss();
-       setup_sysc();
-       setup_pci();
+    detect_cpu();
+
+    //setup_tTasks();
+
+    setup_irqs();
+    set_tss();
+    setup_sysc();
+    setup_pci();
 
 
     while(1); // TODO MODIFY CODE BELOW
 
-       setup_tasks();
+    setup_tasks();
 
-       setup_root_dev();
-       setup_hd();
-       setup_fs();
-       setup_ext2();
+    setup_root_dev();
+    setup_hd();
+    setup_fs();
+    setup_ext2();
 
-       show_logo();
+    show_logo();
 }