feat: irq interrupts; basic kb driver

This commit is contained in:
Karina
2025-12-27 20:54:19 +04:00
parent 664bf2e869
commit e644bc5577
10 changed files with 112 additions and 51 deletions
+1
View File
@@ -23,6 +23,7 @@ add_executable(kernel
src/modules/console.c src/modules/console.c
src/modules/rand.c src/modules/rand.c
src/modules/panic.c src/modules/panic.c
src/modules/keyboard.c
src/modules/kfetch.c src/modules/kfetch.c
data/font8x16.c data/font8x16.c
) )
+5
View File
@@ -4,6 +4,11 @@
#ifndef IO_H #ifndef IO_H
#define IO_H #define IO_H
#define MASTER_COMMAND 0x20
#define MASTER_DATA 0x21
#define SLAVE_COMMAND 0xA0
#define SLAVE_DATA 0xA1
static inline void outb(unsigned short port, unsigned char val) { static inline void outb(unsigned short port, unsigned char val) {
__asm__ volatile( __asm__ volatile(
"outb %0, %1" "outb %0, %1"
+8
View File
@@ -0,0 +1,8 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "types.h"
void kb_handler(Registers *regs);
#endif
+4
View File
@@ -4,4 +4,8 @@
#ifndef PIC_H #ifndef PIC_H
#define PIC_H #define PIC_H
#include "types.h"
u16 pic_remap();
#endif #endif
+61 -31
View File
@@ -4,6 +4,7 @@
bits 64 bits 64
extern isr_handler_c extern isr_handler_c
extern irq_handler_c
section .text section .text
@@ -22,6 +23,60 @@ isr%1:
jmp isr_common_stub jmp isr_common_stub
%endmacro %endmacro
%macro PUSHALL 0
push r15
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rbp
push rdi
push rsi
push rdx
push rcx
push rbx
push rax
%endmacro
%macro POPALL 0
pop rax
pop rbx
pop rcx
pop rdx
pop rsi
pop rdi
pop rbp
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
%endmacro
%macro IRQ_HANDLER 2
global %2
%2:
push 0 ; dummy err code
push %1
PUSHALL
mov rdi, rsp
cld
call irq_handler_c
POPALL
add rsp, 16
iretq
%endmacro
ISR_NOERRCODE 0 ; Divide by zero ISR_NOERRCODE 0 ; Divide by zero
ISR_NOERRCODE 1 ; Debug ISR_NOERRCODE 1 ; Debug
ISR_NOERRCODE 2 ; NMI ISR_NOERRCODE 2 ; NMI
@@ -56,40 +111,12 @@ ISR_ERRCODE 30 ; Security Exception
ISR_NOERRCODE 31 ; Reserved ISR_NOERRCODE 31 ; Reserved
isr_common_stub: isr_common_stub:
push r15 PUSHALL
push r14
push r13
push r12
push r11
push r10
push r9
push r8
push rdi
push rsi
push rbp
push rdx
push rcx
push rbx
push rax
mov rdi, rsp mov rdi, rsp
call isr_handler_c call isr_handler_c
pop rax POPALL
pop rbx
pop rcx
pop rdx
pop rbp
pop rsi
pop rdi
pop r8
pop r9
pop r10
pop r11
pop r12
pop r13
pop r14
pop r15
add rsp, 16 add rsp, 16
iretq iretq
@@ -97,4 +124,7 @@ isr_common_stub:
global idt_load global idt_load
idt_load: idt_load:
lidt [rdi] lidt [rdi]
ret ret
IRQ_HANDLER 32, irq0
IRQ_HANDLER 33, irq1
+3 -19
View File
@@ -11,19 +11,13 @@
#include "gdt.h" #include "gdt.h"
#include "idt.h" #include "idt.h"
#include "pic.h"
#include "pmm.h" #include "pmm.h"
#include "vmm.h" #include "vmm.h"
#include "../data/logo.h" #include "../data/logo.h"
#include "vmm.h" #include "vmm.h"
int rectest(int a) {
volatile int b = a + 1;
kprintf("%d", b);
return rectest(b * 2);
}
extern u64 _kernel_end; extern u64 _kernel_end;
extern u8* bitmap; extern u8* bitmap;
extern u64 bitmap_size_g; extern u64 bitmap_size_g;
@@ -49,6 +43,7 @@ void kmain(Bootinfo* info) {
gdt_init(); gdt_init();
idt_init(); idt_init();
pic_remap();
SG_Point logo_point = {0, 10}; SG_Point logo_point = {0, 10};
sg_put_img(&sg_ctx, &logo_point, &logo_img); sg_put_img(&sg_ctx, &logo_point, &logo_img);
@@ -68,18 +63,7 @@ void kmain(Bootinfo* info) {
vmm_init(info); vmm_init(info);
kprintf("\nIM ALIVE :D"); kprintf("\nIM ALIVE :D");
__asm__ volatile ("sti");
// kprintf("\nSetting up guard page test");
// u64* new_stack_phys = pmm_alloc_page();
// u64 stack_top = 0x40000000;
// vmm_map_page(pml4_kernel, (u64)new_stack_phys, stack_top, PTE_PRESENT | PTE_RW);
// __asm__ volatile (
// "mov %0, %%rsp \n"
// :: "r"(stack_top + 4096)
// );
// rectest(0);
while (1) { __asm__("hlt"); } while (1) { __asm__("hlt"); }
} }
+4
View File
@@ -15,6 +15,7 @@ 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 isr20(); extern void isr21(); extern void isr22(); extern void isr23();
extern void isr24(); extern void isr25(); extern void isr26(); extern void isr27(); 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 isr28(); extern void isr29(); extern void isr30(); extern void isr31();
extern void irq0(void); extern void irq1(void);
extern void idt_load(u64); // asm: lidt [rdi] / ret extern void idt_load(u64); // asm: lidt [rdi] / ret
@@ -74,5 +75,8 @@ void idt_init() {
idt[13].ist = 1; idt[13].ist = 1;
idt[14].ist = 1; idt[14].ist = 1;
idt_set_gate(32, (u64)irq0, selector, flags);
idt_set_gate(33, (u64)irq1, selector, flags);
__asm__ volatile ("lidt %0" : : "m"(idt_ptr)); __asm__ volatile ("lidt %0" : : "m"(idt_ptr));
} }
+13
View File
@@ -1,9 +1,22 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2025 0xKarinyash // Copyright (c) 2025 0xKarinyash
#include "io.h"
#include "panic.h" #include "panic.h"
#include "keyboard.h"
#include "types.h" #include "types.h"
void isr_handler_c(Registers *regs) { void isr_handler_c(Registers *regs) {
panic_exception(regs); panic_exception(regs);
}
void irq_handler_c(Registers *regs) {
switch (regs->int_no) {
case 32: return outb(MASTER_COMMAND, 0x20);
case 33: return kb_handler(regs);
default: outb(SLAVE_COMMAND, 0x20); break;
}
outb(MASTER_COMMAND, 0x20);
} }
+10
View File
@@ -0,0 +1,10 @@
#include "keyboard.h"
#include "console.h"
#include "io.h"
#include "types.h"
void kb_handler(Registers *regs) {
u8 scancode = inb(0x60);
outb(MASTER_COMMAND, 0x20);
kprintf("Key: %x\n", scancode);
}
+3 -1
View File
@@ -47,7 +47,9 @@ const char* fun_messages[] = {
"Pro tip: dont crash", "Pro tip: dont crash",
"Stop ricing the kernel you loonixtard, TermOS is NOT Loonix", "Stop ricing the kernel you loonixtard, TermOS is NOT Loonix",
"Attaboy, Jack. He's good. Really good.", "Attaboy, Jack. He's good. Really good.",
"" "Zhenih priehal",
"YOUR PC WAS BLOCKED BY FBI FOR WATCHING PORNOGRAPHY SEND 20 US DOLLARS",
"fuck off"
}; };