From 0d0c06ab09e6c4058d0da247e9cb6452a9cc3546 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Wed, 12 May 2021 23:48:06 +0100 Subject: [PATCH] Working on PIC --- src/kernel/arch/i386/boot.s | 300 ++++++++++++++++++++++++------------ src/kernel/idt.cpp | 85 ++++++---- src/kernel/idt.h | 50 ++++-- src/kernel/io.cpp | 10 +- src/kernel/io.h | 3 +- src/kernel/kernel.cpp | 49 +++++- src/kernel/kernel.h | 3 +- 7 files changed, 342 insertions(+), 158 deletions(-) diff --git a/src/kernel/arch/i386/boot.s b/src/kernel/arch/i386/boot.s index 91c47df..5f1b4e8 100644 --- a/src/kernel/arch/i386/boot.s +++ b/src/kernel/arch/i386/boot.s @@ -20,103 +20,13 @@ stack_bottom: .skip 16384 # 16 KiB stack_top: -.text -.globl idt_flush -idt_flush: - mov 4(%esp), %eax - lidt (%eax) - ret - -.text -.globl enablePaging -enablePaging: - push %ebp - mov %esp, %ebp - mov %cr0, %eax - or $0x80000000, %eax - mov %eax, %cr0 - mov %ebp, %esp - pop %ebp - ret - -.text -.globl loadPageDirectory -loadPageDirectory: - push %ebp - mov %esp, %ebp - mov 8(%esp), %eax - mov %eax, %cr3 - mov %ebp, %esp - pop %ebp - ret - - .section .text -.global _start -.type _start, @function -_start: - /*Setup the stack pointer to point to the beginning of our stack */ - /* I believe its a hight address growing down to lower adress for the stack on x86*/ - mov $stack_top, %esp - call early_main - - load_gdt: - lgdt gdt - - # set the segment selecters - - movw $0x10, %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs - movw %ax, %ss - ljmp $0x08, $flush - - flush: - #load idt - call init_idt - - # Try enable A20 - # mov $0x2401, %ax - # int $0x15 - - - # enable protected mode - mov %cr0, %eax - or $1, %eax - mov %eax, %cr0 - - - call kernel_main - - - cli - 1: hlt - jmp 1b - - - - - /* Tell processor to use our gdt*/ - gdt: - .word (gdt_end - gdt_start -1) # Size of the GDT in bytes minus 1 for math reasons - .int gdt_start # linear address of our GDT - - - - -.att_syntax - - -.size _start, . - _start - /* * Interupt handlers */ - -.text +# NOTE: I have no clue how I should use these macros. +# Have tried to use them in a myriad of ways, none would actually work .macro irq_NoErrCode code:req .globl irq\code irq\code: @@ -126,7 +36,6 @@ _start: jmp irq_common .endm -.text .macro irq_ErrCode code .globl irq\code irq\code: @@ -135,8 +44,6 @@ _start: jmp irq_common .endm - - .globl irq0 irq0: cli @@ -362,11 +269,120 @@ irq31: jmp irq_common +.globl irq0 +irq0: + cli + push $0 + push $0 + jmp irq_common + +.globl irq1 +irq1: + cli + push $0 + push $1 + jmp irq_common + +.globl irq2 +irq2: + cli + push $0 + push $2 + jmp irq_common + +.globl irq3 +irq3: + cli + push $0 + push $3 + jmp irq_common + +.globl irq4 +irq4: + cli + push $0 + push $4 + jmp irq_common + +.globl irq5 +irq5: + cli + push $0 + push $5 + jmp irq_common + +.globl irq6 +irq6: + cli + push $0 + push $6 + jmp irq_common + +.globl irq7 +irq7: + cli + push $0 + push $7 + jmp irq_common + +.globl irq8 +irq8: + cli + push $0 + push $8 + jmp irq_common + +.globl irq9 +irq9: + cli + push $0 + push $9 + jmp irq_common + +.globl irq10 +irq10: + cli + push $0 + push $10 + jmp irq_common + +.globl irq11 +irq11: + cli + push $0 + push $11 + jmp irq_common + +.globl irq12 +irq12: + cli + push $0 + push $12 + jmp irq_common + +.globl irq13 +irq13: + cli + push $0 + push $13 + jmp irq_common + +.globl irq14 +irq14: + cli + push $0 + push $14 + jmp irq_common + +.globl irq15 +irq15: + cli + push $0 + push $15 + jmp irq_common - -.text irq_common: pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax @@ -396,6 +412,96 @@ irq_common: sti iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP +.globl idt_flush +idt_flush: + mov 4(%esp), %eax + lidt (%eax) + ret + +.globl enablePaging +enablePaging: + push %ebp + mov %esp, %ebp + mov %cr0, %eax + or $0x80000000, %eax + mov %eax, %cr0 + mov %ebp, %esp + pop %ebp + ret + +.globl loadPageDirectory +loadPageDirectory: + push %ebp + mov %esp, %ebp + mov 8(%esp), %eax + mov %eax, %cr3 + mov %ebp, %esp + pop %ebp + ret + +.global _start +.type _start, @function +_start: + /*Setup the stack pointer to point to the beginning of our stack */ + /* I believe its a hight address growing down to lower adress for the stack on x86*/ + mov $stack_top, %esp + call early_main + cli + load_gdt: + lgdt gdt + + # set the segment selecters + + movw $0x10, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs + movw %ax, %ss + ljmp $0x08, $flush + + flush: + + + #load idt + call init_idt + sti + + # Try enable A20 + # mov $0x2401, %ax + # int $0x15 + + + # enable protected mode + mov %cr0, %eax + or $1, %eax + mov %eax, %cr0 + + + call kernel_main + + + cli + 1: hlt + jmp 1b + + + + + /* Tell processor to use our gdt*/ + gdt: + .word (gdt_end - gdt_start -1) # Size of the GDT in bytes minus 1 for math reasons + .int gdt_start # linear address of our GDT + + + + +.att_syntax + + +.size _start, . - _start + + /* * Create the GDT */ diff --git a/src/kernel/idt.cpp b/src/kernel/idt.cpp index 2ba7165..abe1c4d 100644 --- a/src/kernel/idt.cpp +++ b/src/kernel/idt.cpp @@ -14,7 +14,7 @@ void set_id_entry (uint8_t num , uint32_t base, uint16_t sel, uint8_t flags){ void irq_handler (registers regs) { - kterm_writestring("received interrupt!"); + kterm_writestring("received interrupt!\n"); } @@ -26,39 +26,58 @@ void init_idt(){ // TODO: Set everything to zero first - set_id_entry(0, (uint32_t) irq0 , 0x08, 0x8E); - set_id_entry(1, (uint32_t) irq1 , 0x08, 0x8E); - set_id_entry(2, (uint32_t) irq2 , 0x08, 0x8E); - set_id_entry(3, (uint32_t) irq3 , 0x08, 0x8E); - set_id_entry(4, (uint32_t) irq4 , 0x08, 0x8E); - set_id_entry(5, (uint32_t) irq5 , 0x08, 0x8E); - set_id_entry(6, (uint32_t) irq6 , 0x08, 0x8E); - set_id_entry(7, (uint32_t) irq7 , 0x08, 0x8E); - set_id_entry(8, (uint32_t) irq8 , 0x08, 0x8E); - set_id_entry(9, (uint32_t) irq9 , 0x08, 0x8E); - set_id_entry(10, (uint32_t) irq10 , 0x08, 0x8E); - set_id_entry(11, (uint32_t) irq11 , 0x08, 0x8E); - set_id_entry(12, (uint32_t) irq12 , 0x08, 0x8E); - set_id_entry(13, (uint32_t) irq13 , 0x08, 0x8E); - set_id_entry(14, (uint32_t) irq14 , 0x08, 0x8E); - set_id_entry(15, (uint32_t) irq15 , 0x08, 0x8E); - set_id_entry(16, (uint32_t) irq16 , 0x08, 0x8E); - set_id_entry(17, (uint32_t) irq17 , 0x08, 0x8E); - set_id_entry(18, (uint32_t) irq18 , 0x08, 0x8E); - set_id_entry(19, (uint32_t) irq19 , 0x08, 0x8E); - set_id_entry(20, (uint32_t) irq20 , 0x08, 0x8E); - set_id_entry(21, (uint32_t) irq21 , 0x08, 0x8E); - set_id_entry(22, (uint32_t) irq22 , 0x08, 0x8E); - set_id_entry(23, (uint32_t) irq23 , 0x08, 0x8E); - set_id_entry(24, (uint32_t) irq24 , 0x08, 0x8E); - set_id_entry(25, (uint32_t) irq25 , 0x08, 0x8E); - set_id_entry(26, (uint32_t) irq26 , 0x08, 0x8E); - set_id_entry(27, (uint32_t) irq27 , 0x08, 0x8E); - set_id_entry(28, (uint32_t) irq28 , 0x08, 0x8E); - set_id_entry(29, (uint32_t) irq29 , 0x08, 0x8E); - set_id_entry(30, (uint32_t) irq30 , 0x08, 0x8E); - set_id_entry(31, (uint32_t) irq31 , 0x08, 0x8E); + set_id_entry(0, (uint32_t) irs0 , 0x08, 0x8E); + set_id_entry(1, (uint32_t) irs1 , 0x08, 0x8E); + set_id_entry(2, (uint32_t) irs2 , 0x08, 0x8E); + set_id_entry(3, (uint32_t) irs3 , 0x08, 0x8E); + set_id_entry(4, (uint32_t) irs4 , 0x08, 0x8E); + set_id_entry(5, (uint32_t) irs5 , 0x08, 0x8E); + set_id_entry(6, (uint32_t) irs6 , 0x08, 0x8E); + set_id_entry(7, (uint32_t) irs7 , 0x08, 0x8E); + set_id_entry(8, (uint32_t) irs8 , 0x08, 0x8E); + set_id_entry(9, (uint32_t) irs9 , 0x08, 0x8E); + set_id_entry(10, (uint32_t) irs10 , 0x08, 0x8E); + set_id_entry(11, (uint32_t) irs11 , 0x08, 0x8E); + set_id_entry(12, (uint32_t) irs12 , 0x08, 0x8E); + set_id_entry(13, (uint32_t) irs13 , 0x08, 0x8E); + set_id_entry(14, (uint32_t) irs14 , 0x08, 0x8E); + set_id_entry(15, (uint32_t) irs15 , 0x08, 0x8E); + set_id_entry(16, (uint32_t) irs16 , 0x08, 0x8E); + set_id_entry(17, (uint32_t) irs17 , 0x08, 0x8E); + set_id_entry(18, (uint32_t) irs18 , 0x08, 0x8E); + set_id_entry(19, (uint32_t) irs19 , 0x08, 0x8E); + set_id_entry(20, (uint32_t) irs20 , 0x08, 0x8E); + set_id_entry(21, (uint32_t) irs21 , 0x08, 0x8E); + set_id_entry(22, (uint32_t) irs22 , 0x08, 0x8E); + set_id_entry(23, (uint32_t) irs23 , 0x08, 0x8E); + set_id_entry(24, (uint32_t) irs24 , 0x08, 0x8E); + set_id_entry(25, (uint32_t) irs25 , 0x08, 0x8E); + set_id_entry(26, (uint32_t) irs26 , 0x08, 0x8E); + set_id_entry(27, (uint32_t) irs27 , 0x08, 0x8E); + set_id_entry(28, (uint32_t) irs28 , 0x08, 0x8E); + set_id_entry(29, (uint32_t) irs29 , 0x08, 0x8E); + set_id_entry(30, (uint32_t) irs30 , 0x08, 0x8E); + set_id_entry(31, (uint32_t) irs31 , 0x08, 0x8E); + + // pic IRQ Table + set_id_entry(32, (uint32_t)irq0, 0x08, 0x8E); + set_id_entry(33, (uint32_t)irq1, 0x08, 0x8E); + set_id_entry(34, (uint32_t)irq2, 0x08, 0x8E); + set_id_entry(35, (uint32_t)irq3, 0x08, 0x8E); + set_id_entry(36, (uint32_t)irq4, 0x08, 0x8E); + set_id_entry(37, (uint32_t)irq5, 0x08, 0x8E); + set_id_entry(38, (uint32_t)irq6, 0x08, 0x8E); + set_id_entry(39, (uint32_t)irq7, 0x08, 0x8E); + set_id_entry(40, (uint32_t)irq8, 0x08, 0x8E); + set_id_entry(41, (uint32_t)irq9, 0x08, 0x8E); + set_id_entry(42, (uint32_t)irq10, 0x08, 0x8E); + set_id_entry(43, (uint32_t)irq11, 0x08, 0x8E); + set_id_entry(44, (uint32_t)irq12, 0x08, 0x8E); + set_id_entry(45, (uint32_t)irq13, 0x08, 0x8E); + set_id_entry(46, (uint32_t)irq14, 0x08, 0x8E); + set_id_entry(47, (uint32_t)irq15, 0x08, 0x8E); + idt_flush((uint32_t)&idt_ptr); } diff --git a/src/kernel/idt.h b/src/kernel/idt.h index 78b9a3d..d239f43 100644 --- a/src/kernel/idt.h +++ b/src/kernel/idt.h @@ -31,6 +31,39 @@ extern "C" { void irq_handler (registers regs); + extern void irs0 (); + extern void irs1 (); + extern void irs2 (); + extern void irs3 (); + extern void irs4 (); + extern void irs5 (); + extern void irs6 (); + extern void irs7 (); + extern void irs8 (); + extern void irs9 (); + extern void irs10 (); + extern void irs11 (); + extern void irs12 (); + extern void irs13 (); + extern void irs14 (); + extern void irs15 (); + extern void irs16 (); + extern void irs17 (); + extern void irs18 (); + extern void irs19 (); + extern void irs20 (); + extern void irs21 (); + extern void irs22 (); + extern void irs23 (); + extern void irs24 (); + extern void irs25 (); + extern void irs26 (); + extern void irs27 (); + extern void irs28 (); + extern void irs29 (); + extern void irs30 (); + extern void irs31 (); + extern void irq0 (); extern void irq1 (); extern void irq2 (); @@ -47,23 +80,6 @@ extern "C" { extern void irq13 (); extern void irq14 (); extern void irq15 (); - extern void irq16 (); - extern void irq17 (); - extern void irq18 (); - extern void irq19 (); - extern void irq20 (); - extern void irq21 (); - extern void irq22 (); - extern void irq23 (); - extern void irq24 (); - extern void irq25 (); - extern void irq26 (); - extern void irq27 (); - extern void irq28 (); - extern void irq29 (); - extern void irq30 (); - extern void irq31 (); - diff --git a/src/kernel/io.cpp b/src/kernel/io.cpp index 11dbc3b..f9ed07d 100644 --- a/src/kernel/io.cpp +++ b/src/kernel/io.cpp @@ -56,4 +56,12 @@ void outsw(unsigned short , const void *, void outsl(unsigned short , const void *, unsigned long ){ - } \ No newline at end of file + } + +void io_wait(void) +{ + /* TODO: This is probably fragile. */ + asm volatile ( "jmp 1f\n\t" + "1:jmp 2f\n\t" + "2:" ); +} \ No newline at end of file diff --git a/src/kernel/io.h b/src/kernel/io.h index 8b729f9..c7fd561 100644 --- a/src/kernel/io.h +++ b/src/kernel/io.h @@ -40,4 +40,5 @@ void outsb(unsigned short port, const void *addr, void outsw(unsigned short port, const void *addr, unsigned long count); void outsl(unsigned short port, const void *addr, - unsigned long count); \ No newline at end of file + unsigned long count); +void io_wait(); \ No newline at end of file diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index ddb9402..4360aac 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -107,22 +107,37 @@ extern "C" { init_serial(); print_serial("\033[31;42mEarly main called!\n"); + print_serial("Remapping PIC\n"); + // remap the PIC IRQ table + outb(0x20, 0x11); + outb(0xA0, 0x11); + outb(0x21, 0x20); + outb(0xA1, 0x28); + outb(0x21, 0x04); + outb(0xA1, 0x02); + outb(0x21, 0x01); + outb(0xA1, 0x01); + outb(0x21, 0x0); + outb(0xA1, 0x0); + + + print_serial("done... \n"); } void kernel_main (void) { - - print_serial("\033[31;42mKernel main called!\n"); + + print_serial("Kernel main called!\n"); /** initialize terminal interface */ kterm_init(); /** Setup the MMU **/ - kterm_writestring("Starting MMU...\n"); - auto mmu = MMU(); - mmu.enable(); - kterm_writestring("MMU enabled!\n"); + //kterm_writestring("Starting MMU...\n"); + //auto mmu = MMU(); + //mmu.enable(); + //kterm_writestring("MMU enabled!\n"); @@ -143,11 +158,29 @@ extern "C" { auto testObject = Test(); testObject.printMe(); + IRQ_set_mask(0); + IRQ_set_mask(1); + IRQ_set_mask(2); + IRQ_set_mask(3); + IRQ_set_mask(4); + IRQ_set_mask(5); + IRQ_set_mask(6); + IRQ_set_mask(7); + IRQ_set_mask(8); + IRQ_set_mask(9); + IRQ_set_mask(10); + IRQ_set_mask(11); + IRQ_set_mask(12); + IRQ_set_mask(13); + IRQ_set_mask(14); + IRQ_set_mask(15); + + /** test interrupt handlers **/ - //asm volatile ("int $0x3"); + asm volatile ("int $0x03"); - //asm volatile ("int $0x4"); + //asm volatile ("int $4"); /** Lets start using the serial port for debugging .. **/ // Hopefully once we go into realmode or do something that diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 4b2b306..50fc89b 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -5,4 +5,5 @@ extern "C" { } #include "MMU.h" #include "io.h" -#include "idt.h" \ No newline at end of file +#include "idt.h" +#include "pic.h" \ No newline at end of file