Working keyboard interrupt, better int_handler

This commit is contained in:
Furkan Mudanyali 2022-07-19 17:01:31 +03:00
parent 036a7423fe
commit 5b2b4d05f1
7 changed files with 90 additions and 127 deletions

View File

@ -16,7 +16,7 @@
.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
.skip 32768 # 32 KiB
stack_top:
# kernel entry point
@ -30,6 +30,7 @@ _start:
call terminal_initialize
call idt_init # initialize the IDT
call PIC_remap
sti
call kernel_main # transfer control to main kernel
# Hang if kernel_main unexpectedly returns
1: hlt

View File

@ -31,11 +31,11 @@ void gdt_init(void) {
__asm__ volatile("lgdt %0" : : "m"(gdtptr)); // Load new GDT
__asm__ volatile(
"jmp $0x0008, $fix_cs\n"
"fix_cs:\n"
"mov %cr0, %eax\n"
"or $1, %al\n" // set PE (Protection Enable) bit in CR0 (Control Register 0)
"mov %eax, %cr0\n"
"jmp $0x0008, $fix_cs\n"
"fix_cs:\n"
"mov $0x0010, %ax\n"
"mov %ax, %ds\n"
"mov %ax, %es\n"

View File

@ -1,104 +1,17 @@
#include <kernel/idt.h>
#include <stdio.h>
__attribute__((interrupt)) void int_handler(void* frame, unsigned int number) {
unsigned int err = 0;
idt_frame* idt_ptr;
idt_frame_err* idt_ptr_e;
switch(number) {
case 8:
case 10:
case 11:
case 12:
case 13:
case 14:
case 17:
case 21:
idt_ptr_e = (idt_frame_err*)frame;
err = 1;
break;
default:
idt_ptr = (idt_frame*)frame;
break;
}
switch(number) {
case 0:
printf("#DE Divide Error!\n");
break;
case 1:
printf("#DB Debug Exception!\n");
break;
case 2:
printf("NMI Interrupt!\n");
break;
case 3:
printf("#BP Breakpoint!\n");
break;
case 4:
printf("#OF Overflow!\n");
break;
case 5:
printf("#BR Bound Range Exceeded!\n");
break;
case 6:
printf("#Invalid Opcode (Undefined Opcode)!\n");
break;
case 7:
printf("#NM Device Not Available (No Math Coprocessor)!\n");
break;
case 9:
printf("Coprocessor Segment Overrun (reserved)!\n");
break;
case 16:
printf("#MF x87 FPU Floating-Point Error (Math Fault)!\n");
break;
case 18:
printf("#MC Machine Check!\n");
break;
case 19:
printf("#XM SIMD Floating-Point Exception!\n");
break;
case 20:
printf("#VE Virtualization Exception!\n");
break;
////////
case 8:
printf("#DF Double Fault!\n");
break;
case 10:
printf("#TS Invalid TSS!\n");
break;
case 11:
printf("#NP Segment Not Present!\n");
break;
case 12:
printf("#SS Stack-Segment Fault!\n");
break;
case 13:
printf("#GP General Protection!\n");
break;
case 14:
printf("#PF Page Fault!\n");
break;
case 17:
printf("#AC Alignment Check!\n");
break;
case 21:
printf("#CP Control Protection Exception!\n");
break;
}
if(err){
printf("err: 0x%X, ip: 0x%X, cs: 0x%X, flags: 0x%X, sp: 0x%X, ss: 0x%X\n",
idt_ptr_e->err, idt_ptr_e->ip, idt_ptr_e->cs, idt_ptr_e->flags, idt_ptr_e->sp, idt_ptr_e->ss);
} else {
printf("ip: 0x%X, cs: 0x%X, flags: 0x%X, sp: 0x%X, ss: 0x%X\n",
idt_ptr->ip, idt_ptr->cs, idt_ptr->flags, idt_ptr->sp, idt_ptr->ss);
}
__attribute__((interrupt)) void keyboard_handler(void* frame) {
return;
}
__attribute__((interrupt)) void int_handler(void* frame, unsigned int number) {
if (number <= 21) {
printf("%s\n", err_msg[number]);
}
asm("cli;hlt");
}
#define INT_WRAPPER(NUM) __attribute__((interrupt)) void int##NUM##_wrapper(void* frame) { \
int_handler(frame, NUM); \
}
@ -113,17 +26,29 @@ INT_WRAPPER(6)
INT_WRAPPER(7)
INT_WRAPPER(8)
INT_WRAPPER(9)
INT_WRAPPER(10)
INT_WRAPPER(11)
INT_WRAPPER(12)
INT_WRAPPER(13)
INT_WRAPPER(14)
INT_WRAPPER(10) // Invalid TSS (IRQ2)
INT_WRAPPER(11) // Segment not Present (IRQ3)
INT_WRAPPER(12) // Stack Fault (IRQ4)
INT_WRAPPER(13) // GP Fault (IRQ5)
INT_WRAPPER(14) // Page Fault (IRQ6)
INT_WRAPPER(15) // Reserved (IRQ7)
INT_WRAPPER(16)
INT_WRAPPER(17)
INT_WRAPPER(18)
INT_WRAPPER(19)
INT_WRAPPER(20)
INT_WRAPPER(21)
// Reserved from now on
INT_WRAPPER(22)
INT_WRAPPER(23)
INT_WRAPPER(24)
INT_WRAPPER(25)
INT_WRAPPER(26)
INT_WRAPPER(27)
INT_WRAPPER(28)
INT_WRAPPER(29)
INT_WRAPPER(30)
INT_WRAPPER(31)
static __attribute__((aligned(0x10))) idt_entry_t idt[IDT_MAX_DESCRIPTORS];
static idtr_t idtptr;
@ -156,13 +81,26 @@ void idt_init(void) {
idt_set_descriptor(12, int12_wrapper, 0x8E);
idt_set_descriptor(13, int13_wrapper, 0x8E);
idt_set_descriptor(14, int14_wrapper, 0x8E);
idt_set_descriptor(15, int15_wrapper, 0x8E); // Reserved
idt_set_descriptor(16, int16_wrapper, 0x8E);
idt_set_descriptor(17, int17_wrapper, 0x8E);
idt_set_descriptor(18, int18_wrapper, 0x8E);
idt_set_descriptor(19, int19_wrapper, 0x8E);
idt_set_descriptor(20, int20_wrapper, 0x8E);
idt_set_descriptor(21, int21_wrapper, 0x8E);
// Reserved
idt_set_descriptor(22, int22_wrapper, 0x8E);
idt_set_descriptor(23, int23_wrapper, 0x8E);
idt_set_descriptor(24, int24_wrapper, 0x8E);
idt_set_descriptor(25, int25_wrapper, 0x8E);
idt_set_descriptor(26, int26_wrapper, 0x8E);
idt_set_descriptor(27, int27_wrapper, 0x8E);
idt_set_descriptor(28, int28_wrapper, 0x8E);
idt_set_descriptor(29, int29_wrapper, 0x8E);
idt_set_descriptor(30, int30_wrapper, 0x8E);
idt_set_descriptor(31, int31_wrapper, 0x8E);
idt_set_descriptor(33, keyboard_handler, 0x8E);
__asm__ volatile("lidt %0" : : "m"(idtptr)); // Load new IDT
__asm__ volatile("sti"); // Set interrupt flag
}

View File

@ -1,6 +1,9 @@
#include <kernel/pic.h>
#include <stdio.h>
void PIC_remap() {
printf("Beginning PIC initialization.\n");
unsigned char a1, a2;
a1 = inb(PIC1_DATA); // save masks
@ -25,6 +28,9 @@ void PIC_remap() {
outb(PIC2_DATA, ICW4_8086);
io_wait();
outb(PIC1_DATA, a1); // restore saved masks
outb(PIC2_DATA, a2);
outb(PIC1_DATA, 0xFF); // restore saved masks
outb(PIC2_DATA, 0xFF);
outb(PIC1_DATA, 0xFD); // Keyboard IRQ
//outb(PIC2_DATA, 0xFF);
}

View File

@ -18,23 +18,30 @@ typedef struct {
uint32_t base;
} __attribute__((packed)) idtr_t;
typedef struct {
uint32_t ip;
uint32_t cs;
uint32_t flags;
uint32_t sp;
uint32_t ss;
} __attribute__((packed)) idt_frame;
typedef struct {
uint32_t err;
uint32_t ip;
uint32_t cs;
uint32_t flags;
uint32_t sp;
uint32_t ss;
} __attribute__((packed)) idt_frame_err;
static idtr_t idtptr;
static char* err_msg[] = {
"#DE Divide Error!",
"#DB Debug Exception!",
"NMI Interrupt!",
"#BP Breakpoint!",
"#OF Overflow!",
"#BR Bound Range Exceeded!",
"#Invalid Opcode (Undefined Opcode)!",
"#NM Device Not Available (No Math Coprocessor)!",
"#DF Double Fault!",
"Coprocessor Segment Overrun (reserved)!",
"#TS Invalid TSS!",
"#NP Segment Not Present!",
"#SS Stack-Segment Fault!",
"#GP General Protection!",
"#PF Page Fault!",
"Unknown Reserved Fault 15!",
"#MF x87 FPU Floating-Point Error (Math Fault)!",
"#AC Alignment Check!",
"#MC Machine Check!",
"#XM SIMD Floating-Point Exception!",
"#VE Virtualization Exception!",
"#CP Control Protection Exception!"
};
#endif

View File

@ -4,13 +4,13 @@
#include <stdint.h>
#define PIC1 0x20
#define PIC1_COMMAND PIC1
#define PIC1_DATA PIC1+1
#define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21
#define PIC1_OFFSET 0x20
#define PIC2 0xA0
#define PIC2_COMMAND PIC2
#define PIC2_DATA PIC2+1
#define PIC2_COMMAND 0xA0
#define PIC2_DATA 0xA1
#define PIC2_OFFSET 0x28
#define PIC_EOI 0x20

View File

@ -3,6 +3,7 @@
#include <kernel/tty.h>
#include <kernel/gdt.h>
#include <kernel/paging.h>
#include <kernel/pic.h>
#define PAGE_ENTRIES 1024
#define PAGE_SIZE 4 * PAGE_ENTRIES
@ -37,5 +38,15 @@ void kernel_main(void) {
printf("Int test: %d\n", 747474);
printf("Hex test: 0x%x\n", 0xDEADBEEF);
printf("And now for 0.1 + 0.2...... which is: %.17f\n", 0.1 + 0.2);
unsigned char c, cold;
for(;;){
cold = c;
c = inb(0x60);
if (cold != c)
putchar(c);
outb(0x20, 0x20);
}
asm("cli; hlt");
}