From: AceVest Date: Fri, 4 Apr 2014 09:55:19 +0000 (+0800) Subject: add init_paging. X-Git-Tag: 0.3.0~92 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=a3f5acfd83cfb197880071e643180d286f24e3d0;p=kernel.git add init_paging. --- diff --git a/.bochsrc b/.bochsrc new file mode 100644 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 index 0000000..af33667 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/boot/boot.c b/boot/boot.c index fe64161..69bff43 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -12,8 +12,7 @@ #include -#include -#include +#include #include #include #include @@ -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 diff --git a/boot/multiboot.S b/boot/multiboot.S index b64d8fc..f8ad7e7 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -1,125 +1,139 @@ /* *-------------------------------------------------------------------------- - * 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 diff --git a/include/boot/bootparam.h b/include/boot/boot.h similarity index 75% rename from include/boot/bootparam.h rename to include/boot/boot.h index 494df90..4fbef9e 100644 --- a/include/boot/bootparam.h +++ b/include/boot/boot.h @@ -1,6 +1,6 @@ /* * ------------------------------------------------------------------------ - * File Name: bootparam.h + * File Name: boot.h * Author: Zhao Yanbai * Sat Mar 29 09:02:21 2014 * Description: none @@ -11,13 +11,17 @@ #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 diff --git a/include/mm.h b/include/mm.h index ef81104..dbf8596 100644 --- a/include/mm.h +++ b/include/mm.h @@ -9,5 +9,9 @@ #pragma once +#include void *alloc_bootmem(unsigned long size, unsigned long align); + +#define bootmem_alloc_pages(n) alloc_bootmem((n)*PAGE_SIZE, PAGE_SIZE) + diff --git a/include/page.h b/include/page.h index d45d9a8..3fa1038 100644 --- a/include/page.h +++ b/include/page.h @@ -35,7 +35,13 @@ #include -#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 c169b2f..7e11b7d 100644 --- a/mm/mm.c +++ b/mm/mm.c @@ -19,7 +19,8 @@ #include #include #include -#include +#include +#include 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> 22); + + for(di=BOOT_INIT_PAGETBL_CNT; 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 #include #include - -#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 #define get_page_from_list(pList) list_entry(pList, Page, list) diff --git a/setup/setup.c b/setup/setup.c index 3507ec6..d7d5d63 100644 --- a/setup/setup.c +++ b/setup/setup.c @@ -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 * @@ -17,20 +17,20 @@ #include #include -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(); }