From 2835072483c1c4bfe6b546bed353c4e1623573da Mon Sep 17 00:00:00 2001 From: TwoUnderscorez Date: Sun, 12 Nov 2017 19:28:07 +0200 Subject: [PATCH] POC FINAL --- os.iso | Bin 567296 -> 567296 bytes run.sh | 3 + src/boot/boot.asm | 11 +- src/kernel/descriptors.c | 303 +-------------------------------------- src/kernel/descriptors.h | 303 +++++++++++++++++++++++++++++++++++++++ src/kernel/gdt.asm | 2 +- src/kernel/isr.h | 7 +- src/kernel/kernel.c | 9 +- src/kernel/kernel.h | 21 +++ src/kernel/screen.c | 6 +- 10 files changed, 348 insertions(+), 317 deletions(-) create mode 100644 run.sh create mode 100644 src/kernel/kernel.h diff --git a/os.iso b/os.iso index d7a6c76f8b5104f5ed316da233aa091780e53ab9..18b74c8fca2c0b8135688680382701466ab1da38 100644 GIT binary patch delta 282 zcmZp;pww_dX@haIq@j_arIDGjv4H`Hk%6JPp&?9QvQM)V2aK~hulXoH(P}3b#j@}S zODRr%@*z+J#Na69My84;Gyag@obmB8I~z9_m!Uq#WXGSd#eG-hP h*ukRhF$#=8%ml>DK+FQftU$~L#O&K+6gZZ&0|3;uLEQiV delta 251 zcmZp;pww_dX@haIq=AK@xv_zFcwZzH2A+Z4tJU}L5}VGP}T_@yn|M1iht9E=ehVA1U{0*rbfrmV{LJ_*KC>|oLM c7zIWkW&&bnAZ7t#Rv=~rV)pGZ3LHz?0e?h7-2eap diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..42f9946 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +export DISPLAY=:0 +printf "\n###Running QEMU...###\n" +qemu-system-i386 -cdrom os.iso \ No newline at end of file diff --git a/src/boot/boot.asm b/src/boot/boot.asm index 773c6f0..966cadd 100644 --- a/src/boot/boot.asm +++ b/src/boot/boot.asm @@ -2,6 +2,7 @@ global loader extern kmain +; Prepare GRUB header MODULEALIGN equ 1<<0 MEMINFO equ 1<<1 FLAGS equ MODULEALIGN | MEMINFO @@ -9,7 +10,8 @@ MAGIC equ 0x1BADB002 CHECKSUM equ -(MAGIC + FLAGS) section .text - + +; Define GRUB Header align 4 dd MAGIC dd FLAGS @@ -18,10 +20,11 @@ align 4 STACKSIZE equ 0x4000 loader: + ; Setup the stack mov esp, stack + STACKSIZE - push eax - push ebx - + + push eax ; Magic number + push ebx ; Multiboot information call kmain cli diff --git a/src/kernel/descriptors.c b/src/kernel/descriptors.c index 43a9451..43c26f7 100644 --- a/src/kernel/descriptors.c +++ b/src/kernel/descriptors.c @@ -1,306 +1,5 @@ #include "descriptors.h" -extern void gdt_write(unsigned int); -extern void idt_write(unsigned int); - -#pragma region isrs -extern void isr0(); -extern void isr1(); -extern void isr2(); -extern void isr3(); -extern void isr4(); -extern void isr5(); -extern void isr6(); -extern void isr7(); -extern void isr8(); -extern void isr9(); -extern void isr10(); -extern void isr11(); -extern void isr12(); -extern void isr13(); -extern void isr14(); -extern void isr15(); -extern void isr16(); -extern void isr17(); -extern void isr18(); -extern void isr19(); -extern void isr20(); -extern void isr21(); -extern void isr22(); -extern void isr23(); -extern void isr24(); -extern void isr25(); -extern void isr26(); -extern void isr27(); -extern void isr28(); -extern void isr29(); -extern void isr30(); -extern void isr31(); -extern void isr32(); -extern void isr33(); -extern void isr34(); -extern void isr35(); -extern void isr36(); -extern void isr37(); -extern void isr38(); -extern void isr39(); -extern void isr40(); -extern void isr41(); -extern void isr42(); -extern void isr43(); -extern void isr44(); -extern void isr45(); -extern void isr46(); -extern void isr47(); -extern void isr48(); -extern void isr49(); -extern void isr50(); -extern void isr51(); -extern void isr52(); -extern void isr53(); -extern void isr54(); -extern void isr55(); -extern void isr56(); -extern void isr57(); -extern void isr58(); -extern void isr59(); -extern void isr60(); -extern void isr61(); -extern void isr62(); -extern void isr63(); -extern void isr64(); -extern void isr65(); -extern void isr66(); -extern void isr67(); -extern void isr68(); -extern void isr69(); -extern void isr70(); -extern void isr71(); -extern void isr72(); -extern void isr73(); -extern void isr74(); -extern void isr75(); -extern void isr76(); -extern void isr77(); -extern void isr78(); -extern void isr79(); -extern void isr80(); -extern void isr81(); -extern void isr82(); -extern void isr83(); -extern void isr84(); -extern void isr85(); -extern void isr86(); -extern void isr87(); -extern void isr88(); -extern void isr89(); -extern void isr90(); -extern void isr91(); -extern void isr92(); -extern void isr93(); -extern void isr94(); -extern void isr95(); -extern void isr96(); -extern void isr97(); -extern void isr98(); -extern void isr99(); -extern void isr100(); -extern void isr101(); -extern void isr102(); -extern void isr103(); -extern void isr104(); -extern void isr105(); -extern void isr106(); -extern void isr107(); -extern void isr108(); -extern void isr109(); -extern void isr110(); -extern void isr111(); -extern void isr112(); -extern void isr113(); -extern void isr114(); -extern void isr115(); -extern void isr116(); -extern void isr117(); -extern void isr118(); -extern void isr119(); -extern void isr120(); -extern void isr121(); -extern void isr122(); -extern void isr123(); -extern void isr124(); -extern void isr125(); -extern void isr126(); -extern void isr127(); -extern void isr128(); -extern void isr129(); -extern void isr130(); -extern void isr131(); -extern void isr132(); -extern void isr133(); -extern void isr134(); -extern void isr135(); -extern void isr136(); -extern void isr137(); -extern void isr138(); -extern void isr139(); -extern void isr140(); -extern void isr141(); -extern void isr142(); -extern void isr143(); -extern void isr144(); -extern void isr145(); -extern void isr146(); -extern void isr147(); -extern void isr148(); -extern void isr149(); -extern void isr150(); -extern void isr151(); -extern void isr152(); -extern void isr153(); -extern void isr154(); -extern void isr155(); -extern void isr156(); -extern void isr157(); -extern void isr158(); -extern void isr159(); -extern void isr160(); -extern void isr161(); -extern void isr162(); -extern void isr163(); -extern void isr164(); -extern void isr165(); -extern void isr166(); -extern void isr167(); -extern void isr168(); -extern void isr169(); -extern void isr170(); -extern void isr171(); -extern void isr172(); -extern void isr173(); -extern void isr174(); -extern void isr175(); -extern void isr176(); -extern void isr177(); -extern void isr178(); -extern void isr179(); -extern void isr180(); -extern void isr181(); -extern void isr182(); -extern void isr183(); -extern void isr184(); -extern void isr185(); -extern void isr186(); -extern void isr187(); -extern void isr188(); -extern void isr189(); -extern void isr190(); -extern void isr191(); -extern void isr192(); -extern void isr193(); -extern void isr194(); -extern void isr195(); -extern void isr196(); -extern void isr197(); -extern void isr198(); -extern void isr199(); -extern void isr200(); -extern void isr201(); -extern void isr202(); -extern void isr203(); -extern void isr204(); -extern void isr205(); -extern void isr206(); -extern void isr207(); -extern void isr208(); -extern void isr209(); -extern void isr210(); -extern void isr211(); -extern void isr212(); -extern void isr213(); -extern void isr214(); -extern void isr215(); -extern void isr216(); -extern void isr217(); -extern void isr218(); -extern void isr219(); -extern void isr220(); -extern void isr221(); -extern void isr222(); -extern void isr223(); -extern void isr224(); -extern void isr225(); -extern void isr226(); -extern void isr227(); -extern void isr228(); -extern void isr229(); -extern void isr230(); -extern void isr231(); -extern void isr232(); -extern void isr233(); -extern void isr234(); -extern void isr235(); -extern void isr236(); -extern void isr237(); -extern void isr238(); -extern void isr239(); -extern void isr240(); -extern void isr241(); -extern void isr242(); -extern void isr243(); -extern void isr244(); -extern void isr245(); -extern void isr246(); -extern void isr247(); -extern void isr248(); -extern void isr249(); -extern void isr250(); -extern void isr251(); -extern void isr252(); -extern void isr253(); -extern void isr254(); -extern void isr255(); -#pragma endregion - -// A struct that represents a line in the GDT -struct gdt_entry_struct { - unsigned short limit_low; - unsigned short base_low; - unsigned char base_middle; - unsigned char access; - unsigned char granularity; // 4 bits of the limit and 4 bits which are flags - unsigned char base_high; -} __attribute__((packed)); //No padding - -typedef struct gdt_entry_struct gdt_entry_t; - -// A struct that represents the pointer passed to lgdt (a.k.a. GDT descriptor) -struct gdt_ptr_struct { - unsigned short limit; - unsigned int base; -} __attribute__((packed)); - -typedef struct gdt_ptr_struct gdt_ptr_t; - -// A struct that represents a line in the IDT -struct idt_entry_struct{ - unsigned short base_low; - unsigned short selector; - unsigned char always0; - unsigned char flags; - unsigned short base_high; -} __attribute__((packed)); - -typedef struct idt_entry_struct idt_entry_t; - -// A struct that represents the pointer passed to lidt (a.k.a. IDT descriptor) -struct idt_ptr_struct{ - unsigned short limit; - unsigned int base; -} __attribute__((packed)); - -typedef struct idt_ptr_struct idt_ptr_t; - // Define GDT gdt_entry_t gdt_entries[5]; // Define IDT @@ -338,7 +37,7 @@ void idt_set_gate(int index,unsigned int base, unsigned short selector, unsigned idt_entries[index].base_high = (base >> 16) & 0xFFFF; idt_entries[index].selector = selector; idt_entries[index].always0 = 0; - idt_entries[index].flags = flags; + idt_entries[index].flags = flags; } // Setup the IDT diff --git a/src/kernel/descriptors.h b/src/kernel/descriptors.h index 617e206..cc04fbc 100644 --- a/src/kernel/descriptors.h +++ b/src/kernel/descriptors.h @@ -1,7 +1,310 @@ #ifndef DESCRIPTORS_H #define DESCRIPTORS_H +extern void gdt_write(unsigned int); +extern void idt_write(unsigned int); +#pragma region isrs +extern void isr0(); +extern void isr1(); +extern void isr2(); +extern void isr3(); +extern void isr4(); +extern void isr5(); +extern void isr6(); +extern void isr7(); +extern void isr8(); +extern void isr9(); +extern void isr10(); +extern void isr11(); +extern void isr12(); +extern void isr13(); +extern void isr14(); +extern void isr15(); +extern void isr16(); +extern void isr17(); +extern void isr18(); +extern void isr19(); +extern void isr20(); +extern void isr21(); +extern void isr22(); +extern void isr23(); +extern void isr24(); +extern void isr25(); +extern void isr26(); +extern void isr27(); +extern void isr28(); +extern void isr29(); +extern void isr30(); +extern void isr31(); +extern void isr32(); +extern void isr33(); +extern void isr34(); +extern void isr35(); +extern void isr36(); +extern void isr37(); +extern void isr38(); +extern void isr39(); +extern void isr40(); +extern void isr41(); +extern void isr42(); +extern void isr43(); +extern void isr44(); +extern void isr45(); +extern void isr46(); +extern void isr47(); +extern void isr48(); +extern void isr49(); +extern void isr50(); +extern void isr51(); +extern void isr52(); +extern void isr53(); +extern void isr54(); +extern void isr55(); +extern void isr56(); +extern void isr57(); +extern void isr58(); +extern void isr59(); +extern void isr60(); +extern void isr61(); +extern void isr62(); +extern void isr63(); +extern void isr64(); +extern void isr65(); +extern void isr66(); +extern void isr67(); +extern void isr68(); +extern void isr69(); +extern void isr70(); +extern void isr71(); +extern void isr72(); +extern void isr73(); +extern void isr74(); +extern void isr75(); +extern void isr76(); +extern void isr77(); +extern void isr78(); +extern void isr79(); +extern void isr80(); +extern void isr81(); +extern void isr82(); +extern void isr83(); +extern void isr84(); +extern void isr85(); +extern void isr86(); +extern void isr87(); +extern void isr88(); +extern void isr89(); +extern void isr90(); +extern void isr91(); +extern void isr92(); +extern void isr93(); +extern void isr94(); +extern void isr95(); +extern void isr96(); +extern void isr97(); +extern void isr98(); +extern void isr99(); +extern void isr100(); +extern void isr101(); +extern void isr102(); +extern void isr103(); +extern void isr104(); +extern void isr105(); +extern void isr106(); +extern void isr107(); +extern void isr108(); +extern void isr109(); +extern void isr110(); +extern void isr111(); +extern void isr112(); +extern void isr113(); +extern void isr114(); +extern void isr115(); +extern void isr116(); +extern void isr117(); +extern void isr118(); +extern void isr119(); +extern void isr120(); +extern void isr121(); +extern void isr122(); +extern void isr123(); +extern void isr124(); +extern void isr125(); +extern void isr126(); +extern void isr127(); +extern void isr128(); +extern void isr129(); +extern void isr130(); +extern void isr131(); +extern void isr132(); +extern void isr133(); +extern void isr134(); +extern void isr135(); +extern void isr136(); +extern void isr137(); +extern void isr138(); +extern void isr139(); +extern void isr140(); +extern void isr141(); +extern void isr142(); +extern void isr143(); +extern void isr144(); +extern void isr145(); +extern void isr146(); +extern void isr147(); +extern void isr148(); +extern void isr149(); +extern void isr150(); +extern void isr151(); +extern void isr152(); +extern void isr153(); +extern void isr154(); +extern void isr155(); +extern void isr156(); +extern void isr157(); +extern void isr158(); +extern void isr159(); +extern void isr160(); +extern void isr161(); +extern void isr162(); +extern void isr163(); +extern void isr164(); +extern void isr165(); +extern void isr166(); +extern void isr167(); +extern void isr168(); +extern void isr169(); +extern void isr170(); +extern void isr171(); +extern void isr172(); +extern void isr173(); +extern void isr174(); +extern void isr175(); +extern void isr176(); +extern void isr177(); +extern void isr178(); +extern void isr179(); +extern void isr180(); +extern void isr181(); +extern void isr182(); +extern void isr183(); +extern void isr184(); +extern void isr185(); +extern void isr186(); +extern void isr187(); +extern void isr188(); +extern void isr189(); +extern void isr190(); +extern void isr191(); +extern void isr192(); +extern void isr193(); +extern void isr194(); +extern void isr195(); +extern void isr196(); +extern void isr197(); +extern void isr198(); +extern void isr199(); +extern void isr200(); +extern void isr201(); +extern void isr202(); +extern void isr203(); +extern void isr204(); +extern void isr205(); +extern void isr206(); +extern void isr207(); +extern void isr208(); +extern void isr209(); +extern void isr210(); +extern void isr211(); +extern void isr212(); +extern void isr213(); +extern void isr214(); +extern void isr215(); +extern void isr216(); +extern void isr217(); +extern void isr218(); +extern void isr219(); +extern void isr220(); +extern void isr221(); +extern void isr222(); +extern void isr223(); +extern void isr224(); +extern void isr225(); +extern void isr226(); +extern void isr227(); +extern void isr228(); +extern void isr229(); +extern void isr230(); +extern void isr231(); +extern void isr232(); +extern void isr233(); +extern void isr234(); +extern void isr235(); +extern void isr236(); +extern void isr237(); +extern void isr238(); +extern void isr239(); +extern void isr240(); +extern void isr241(); +extern void isr242(); +extern void isr243(); +extern void isr244(); +extern void isr245(); +extern void isr246(); +extern void isr247(); +extern void isr248(); +extern void isr249(); +extern void isr250(); +extern void isr251(); +extern void isr252(); +extern void isr253(); +extern void isr254(); +extern void isr255(); +#pragma endregion + +// A struct that represents a line in the GDT +struct gdt_entry_struct { + unsigned short limit_low; + unsigned short base_low; + unsigned char base_middle; + unsigned char access; + unsigned char granularity; // 4 bits of the limit and 4 bits which are flags + unsigned char base_high; +} __attribute__((packed)); //No padding + +typedef struct gdt_entry_struct gdt_entry_t; + +// A struct that represents the pointer passed to lgdt (a.k.a. GDT descriptor) +struct gdt_ptr_struct { + unsigned short limit; + unsigned int base; +} __attribute__((packed)); + +typedef struct gdt_ptr_struct gdt_ptr_t; + +// A struct that represents a line in the IDT +struct idt_entry_struct { + unsigned short base_low; + unsigned short selector; + unsigned char always0; + unsigned char flags; + unsigned short base_high; +} __attribute__((packed)); + +typedef struct idt_entry_struct idt_entry_t; + +// A struct that represents the pointer passed to lidt (a.k.a. IDT descriptor) +struct idt_ptr_struct{ + unsigned short limit; + unsigned int base; +} __attribute__((packed)); + +typedef struct idt_ptr_struct idt_ptr_t; + + +gdt_entry_t gdt_set_gate(unsigned int base, unsigned int limit, unsigned char access, unsigned char granularity); void gdt_setup(); +void idt_set_gate(int index,unsigned int base, unsigned short selector, unsigned char flags); void idt_setup(); #endif diff --git a/src/kernel/gdt.asm b/src/kernel/gdt.asm index 54e5c4c..eaaf885 100644 --- a/src/kernel/gdt.asm +++ b/src/kernel/gdt.asm @@ -1,7 +1,7 @@ global gdt_write gdt_write: - mov eax, [esp+4] + mov eax, [esp+4]; 1st param is the addr of the GDT descriptor in memory lgdt [eax] mov ax, 0x10 ; Kernel DataSeg mov ds, ax diff --git a/src/kernel/isr.h b/src/kernel/isr.h index 83ea8f1..2f98b4c 100644 --- a/src/kernel/isr.h +++ b/src/kernel/isr.h @@ -1,12 +1,15 @@ #ifndef ISR_H #define ISR_H -typedef struct registers +// A struct that represents the data structure of what PUSHA pushes onto the stack +struct registers { unsigned int ds; unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax; unsigned int int_no, err_code; unsigned int eip, cs, eflags, useresp, ss; -} registers_t; +}; + +typedef struct registers registers_t; #endif diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 3148ac2..f870040 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,10 +1,10 @@ +#include "kernel.h" #include "screen.h" #include "descriptors.h" #include "keyboard.h" +#include "asmio.h" -void free_write(); - -int kmain(void *mbd, unsigned int magic){ +int kmain(multiboot_information_t * mbd, unsigned int magic){ int i = 0; while(i++<50000000); screen_clear(); @@ -38,5 +38,4 @@ void free_write() { while(i++<100000); i=0; } -} - +} \ No newline at end of file diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h new file mode 100644 index 0000000..ed1be59 --- /dev/null +++ b/src/kernel/kernel.h @@ -0,0 +1,21 @@ +#ifndef KERNEL_H +#define KERNEL_H + +void free_write(); + +struct multiboot_information_struct { + unsigned char flags_mem_lower; + unsigned char mem_upper_bootdev; + unsigned char cmdline_mod_count; + unsigned short mods_addr_syms; + unsigned char mmap_len_addr; + unsigned char drives_len_addr; + unsigned char cfgtbl_bloader_name; + unsigned char amptbl_vbe_ctrlinfo; + unsigned char vbe_mode; + +} __attribute__((packed)); + +typedef struct multiboot_information_struct multiboot_information_t; + +#endif \ No newline at end of file diff --git a/src/kernel/screen.c b/src/kernel/screen.c index 04196e8..4d858b9 100644 --- a/src/kernel/screen.c +++ b/src/kernel/screen.c @@ -23,7 +23,7 @@ void screen_scroll(){ for(i=160;i<4000;i++){ vidmem[i-160]=vidmem[i]; } - for(i= 160*24;i<4000;i+=2) { + for(i=160*24;i<4000;i+=2) { vidmem[i]=0x20; vidmem[i+1]=0x07; } @@ -81,14 +81,14 @@ void screen_print(char *string){ // Sets the cursor position void set_cursor_position(int row, int col) { - unsigned short position=(row*80) + col; //calc posotion and save it in a WORD + unsigned short position = ( row*80) + col; //calc posotion and save it in a WORD // cursor LOW port to vga INDEX register out_byte(0x3D4, 0x0F); out_byte(0x3D5, (unsigned char)(position&0xFF)); //get low BYTE of WORD // cursor HIGH port to vga INDEX register out_byte(0x3D4, 0x0E); - out_byte(0x3D5, (unsigned char )((position>>8)&0xFF)); //get high BYTE of WORD + out_byte(0x3D5, (unsigned char)((position>>8)&0xFF)); //get high BYTE of WORD } // int to ASCII