diff --git a/Copy of kernel.c b/Copy of kernel.c new file mode 100644 index 0000000..6d74789 --- /dev/null +++ b/Copy of kernel.c @@ -0,0 +1,98 @@ +//kernel.c +//08/13/03 Josh Holtrop +//Holtrop's Operating System +//Version: 0.12 +//Modified: 11/12/03 + +#include "k_defines.h" //#DEFINE's for kernel + +#include "lib/string.h" //library string functions +#include "lib/io.h" //library input/output functions + +#include "functions.h" //general functions +#include "video.h" //video functions +#include "mm.h" //physical memory management functions +#include "vmm.h" //virtual memory management & paging functions +#include "keyboard.h" //generic keyboard driver & functions +#include "mouse.h" //generic ps/2 mouse driver & functions +#include "fdc.h" //Floppy Disk Controller functions +#include "stdfont.h" //Standard font bitmask array + +void isr(dword num); +void k_init(); +extern dword write_cr0(dword cr0); +extern dword read_cr0(); +extern dword write_cr3(dword cr3); +extern dword read_cr3(); + +#include "fdc.c" +#include "mouse.c" +#include "keyboard.c" +#include "mm.c" +#include "vmm.c" +#include "functions.c" +#include "video.c" + +dword timer = 0; +dword *videoMode; + +//Main kernel initialization method +void k_init() +{ + // ===== Initialization + fdc_sendDOR(0x0C); //turn off floppy motor!! + console_cls(); + remap_pics(0x20, 0x28); + init_timer(); + mm_init(); + videoMode = (dword *)0x90002; + if (*videoMode) + video_init((ModeInfoBlock *) 0x90306); + vmm_init(); + mouse_init(); + vmm_enable_paging(); + pic1_mask(0); //unmask IRQ's 0-7 + pic2_mask(0); //unmask IRQ's 8-15 + enable_ints(); + kbd_resetLEDs(); //after enabling interrupts!! + + printf("HOS 0.12 - Kernel Size: %d kb\n", kernel_size()/1024); + printf("Memory available to OS: %d MB (Bytes: %d)\n", mm_totalmem/0x100000, mm_totalmem); + printf("Free memory: %d bytes\n", mm_freemem()); + + dword key = 0; + for (;;) + { + key = kbdWaitKey(); + if ((key & 0xFF) > 2) //key is not a control key + putc(key); + } +} + +// main Interrupt Service Routine - handles all interrupts unless caught by kernel.asm +void isr(dword num) +{ + switch(num) + { + case 0x20: // IRQ0 - timer interrupt + timer++; + (*(byte *)(0xb8000))++; + eoi(); + break; + case 0x21: // IRQ1 - keyboard interrupt + isr_keyboard(); //isr_keybard() takes care of calling eoi() + break; + case 0x2C: // IRQ12 - PS/2 mouse + isr_mouse(); + eoi2(); + break; + default: + printf("Interrupt %d (0x%x) Unhandled!!\n", num, num); + break; + } +} + + + + + diff --git a/bootdef.inc b/bootdef.inc index fc8d6b2..504c7f6 100644 --- a/bootdef.inc +++ b/bootdef.inc @@ -6,7 +6,7 @@ %define BOOT_KERNEL_SEG 0x0AC0 ;right after ROOT_DIR %define BOOT_STAGE2_SEG 0x0B00 ;right after KERNEL_SEG %define BOOT_STAGE2_ADD 0xB000 ;address of stage2 to jump to, org at -%define BOOT_KERNEL_ADD 0x104000 ;final pmode kernel destination - physical +%define BOOT_KERNEL_ADD 0x106000 ;final pmode kernel destination - physical %define BOOT_RD_ADD 0x200000 ;2mb for ram disk %define BOOT_DATA_SEG 0x9000 ;data gathered by stage2 loader goes here diff --git a/gdt.inc b/gdt.inc index 2c04c58..63481f3 100644 --- a/gdt.inc +++ b/gdt.inc @@ -5,7 +5,7 @@ gdtr: dw gdt_end-gdt-1 - dd GDT + dd GDT_P gdt: dd 0 dd 0 diff --git a/idt.inc b/idt.inc index 0efb45b..e12b2f6 100644 --- a/idt.inc +++ b/idt.inc @@ -5,7 +5,7 @@ idtr: dw 50*8-1 ;size of idt - dd IDT ;address of idt + dd IDT_P ;address of idt %macro isr_label 1 diff --git a/k.bin b/k.bin deleted file mode 100644 index ae472d1..0000000 Binary files a/k.bin and /dev/null differ diff --git a/kernel.asm b/kernel.asm index 0ad7697..4d8a3eb 100644 --- a/kernel.asm +++ b/kernel.asm @@ -2,10 +2,16 @@ ;Author: Josh Holtrop ;Modified: 10/30/03 -%include "bootdef.inc" - -%define GDT 0x100000 -%define IDT 0x102000 +%define GDT_P 0x100000; ;1mb physical - Global Descriptor Table space +%define GDT_V GDT_P+0xC0000000 +%define IDT_P 0x102000 ;1mb+8kb - Interrupt Descriptor Table space +%define IDT_V IDT_P+0xC0000000 +%define PDBR_P 0x104000 ;1mb+16kb - Page Directory Base Register (first PD) +%define PDBR_V PDBR_P+0xC0000000 +%define LOPT_P 0x105000 ;1mb+20kb - LOw Page Table for mapping first 4mb +%define LOPT_V LOPT_P+0xC0000000 +%define KERNEL_P 0x106000 ;1mb+24kb - the kernel's physical address +%define KERNEL_V KERNEL_P+0xC0000000 ;3gb+1mb+24kb, the virtual address of the kernel [global start] [extern _isr] @@ -14,9 +20,36 @@ bits 32 ;This is where the kernel begins execution +;At this point, the temporary gdt is set up to "map" 0xC000_0000 to 0x0. +;We must enable paging with the first 4mb mapped 1:1 virtual:physical +; and with the 4mb starting at 0xC000_0000 mapped to the first 4mb physical. +;Then we can start using our "real" gdt, then unmap the lower 4mb. start: - cli ;if they weren't already off - mov edi, GDT + cli ;if they weren't already off + + xor eax, eax + mov edi, PDBR_V + mov ecx, 1024 ;clear the PDBR + rep stosd + mov [PDBR_V], dword LOPT_P|0x03 ;store the physical address of the LOw Page Table (read/write, present) + mov [PDBR_V+0xC00], dword LOPT_P|0x03 ;store the physical address of the LOw Page Table (read/write, present) + + mov edi, LOPT_V + mov ecx, 1024 + mov eax, 0x03 ;starting physical address = 0x0 (read/write, present flags) +fill_lopt_loop: ;fill the page table + stosd + add eax, 4096 ;increment next phsyical address by 4kb + loop fill_lopt_loop + + mov eax, PDBR_P + mov cr3, eax ;store the Page Directory Base Address + mov eax, cr0 + or eax, 0x80000000 ;set page enable bit + mov cr0, eax ;now paging is active! + + + mov edi, GDT_V mov esi, gdt mov ecx, gdt_end-gdt copy_gdt: @@ -24,7 +57,7 @@ copy_gdt: stosb loop copy_gdt - mov edi, IDT ;destination + mov edi, IDT_V ;destination mov esi, isr_0 ;address of isr0 mov edx, isr_1-isr_0 ;distance between isr labels mov ecx, 50 ;number of isrlabels @@ -42,7 +75,7 @@ fill_idt: mov esi, ebx add esi, edx loop fill_idt - mov word [IDT+0x30*8+4], 0xEE00 ;interrupt 0x30 has user priviledges + mov word [IDT_V+0x30*8+4], 0xEE00 ;interrupt 0x30 has user priviledges lgdt [gdtr] ;load gdt jmp KERNEL_CODE:newgdtcontinue @@ -53,7 +86,7 @@ newgdtcontinue: mov gs, ax mov fs, ax mov ss, ax - mov esp, 0x1ffffc ;stack just under 2mb, moves downward + mov esp, 0xc01ffffc ;stack just under 2mb, moves downward lidt [idtr] ;load idt call _k_init diff --git a/kernel.c b/kernel.c index 15cc7ac..ddd8aff 100644 --- a/kernel.c +++ b/kernel.c @@ -1,111 +1,21 @@ -//kernel.c -//08/13/03 Josh Holtrop -//Holtrop's Operating System -//Version: 0.12 -//Modified: 11/12/03 -#include "k_defines.h" //#DEFINE's for kernel - -#include "lib/string.h" //library string functions -#include "lib/io.h" //library input/output functions - -#include "functions.h" //general functions -#include "video.h" //video functions -#include "mm.h" //physical memory management functions -#include "vmm.h" //virtual memory management & paging functions -#include "keyboard.h" //generic keyboard driver & functions -#include "mouse.h" //generic ps/2 mouse driver & functions -#include "fdc.h" //Floppy Disk Controller functions -#include "stdfont.h" //Standard font bitmask array +#include "k_defines.h" void isr(dword num); void k_init(); -extern dword write_cr0(dword cr0); -extern dword read_cr0(); -extern dword write_cr3(dword cr3); -extern dword read_cr3(); - -#include "fdc.c" -#include "mouse.c" -#include "keyboard.c" -#include "mm.c" -#include "vmm.c" -#include "functions.c" -#include "video.c" - -dword timer = 0; -dword *videoMode; //Main kernel initialization method void k_init() { - // ===== Initialization - fdc_sendDOR(0x0C); //turn off floppy motor!! - console_cls(); - remap_pics(0x20, 0x28); - init_timer(); - mm_init(); - videoMode = (dword *)0x90002; - if (*videoMode) - video_init((ModeInfoBlock *) 0x90306); - vmm_init(); - mouse_init(); - vmm_enable_paging(); - pic1_mask(0); //unmask IRQ's 0-7 - pic2_mask(0); //unmask IRQ's 8-15 - enable_ints(); - kbd_resetLEDs(); //after enabling interrupts!! - - char *bmpptr = (char *)0x108000+54; - int a = 0; - int x = 0; - int y = 479; - for (a=0; a < 640*480; a++) - { - video_pset(x, y, *(dword *)bmpptr); - bmpptr += 3; - x++; - if (x == 640) - { - x = 0; - y--; - } - } - - printf("HOS 0.12 - Kernel Size: %d kb\n", kernel_size()/1024); - printf("Memory available to OS: %d MB (Bytes: %d)\n", mm_totalmem/0x100000, mm_totalmem); - printf("Free memory: %d bytes\n", mm_freemem()); - - dword key = 0; for (;;) { - key = kbdWaitKey(); - if ((key & 0xFF) > 2) //key is not a control key - putc(key); + (*(byte *)0xc00b8000)++; } } -// main Interrupt Service Routine - handles all interrupts unless caught by kernel.asm void isr(dword num) { - switch(num) - { - case 0x20: // IRQ0 - timer interrupt - timer++; - (*(byte *)(0xb8000))++; - eoi(); - break; - case 0x21: // IRQ1 - keyboard interrupt - isr_keyboard(); //isr_keybard() takes care of calling eoi() - break; - case 0x2C: // IRQ12 - PS/2 mouse - isr_mouse(); - eoi2(); - break; - default: - printf("Interrupt %d (0x%x) Unhandled!!\n", num, num); - break; - } + } diff --git a/link.ld b/link.ld index 310c93e..c3e78f5 100644 --- a/link.ld +++ b/link.ld @@ -2,7 +2,7 @@ OUTPUT_FORMAT("binary") ENTRY(start) SECTIONS { - .text 0x104000 : { + .text 0xC0106000 : { code = .; _code = .; __code = .; *(.text) . = ALIGN(4096); diff --git a/rivercity.bmp b/rivercity.bmp deleted file mode 100644 index 1fbc411..0000000 Binary files a/rivercity.bmp and /dev/null differ diff --git a/stage2.asm b/stage2.asm index 32c2c06..b1edfd3 100644 --- a/stage2.asm +++ b/stage2.asm @@ -890,11 +890,43 @@ KERNEL_DATA equ $-gdt db 0x00 db 0x00 db 0x92 - db 0xcf ;cf + db 0xcf db 0x00 gdt_end: + +;------------------------------------------------------- +gdtr32: + dw gdt_end32-gdt32-1 + dd gdt32 +gdt32: + dd 0 + dd 0 + + ;a base of 0x4000_0000, when added to 0xC000_0000 will produce 0x0000_0000 physical before paging in effect +KERNEL_CODE32 equ $-gdt32 + db 0xff ;limit 7:0 + db 0xff ;limit 15:8 + db 0x00 ;base 7:0 + db 0x00 ;base 15:8 + db 0x00 ;base 23:16 + db 0x9a ;access + db 0xcf ;flags / limit 19:16 + db 0x40 ;base 31:24 + +KERNEL_DATA32 equ $-gdt32 + db 0xff ;segment 16 = 4gb data + db 0xff + db 0x00 + db 0x00 + db 0x00 + db 0x92 + db 0xcf + db 0x40 + +gdt_end32: + ;------------------------------------------------------ go_pm: xor ax, ax @@ -909,12 +941,16 @@ go_pm: bits 32 pmode: - mov ax, KERNEL_DATA + lgdt [gdtr32] + jmp KERNEL_CODE32:pmode_offsetted+0xC0000000 + +pmode_offsetted: + mov ax, KERNEL_DATA32 mov es, ax mov ds, ax mov fs, ax mov gs, ax - jmp KERNEL_CODE:BOOT_KERNEL_ADD + jmp KERNEL_CODE:BOOT_KERNEL_ADD+0xC0000000 kernel: db "KERNEL BIN", 0