diff --git a/Makefile b/Makefile index f4e41c8..727a601 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,16 @@ BIN_DIR := bin KERNEL_BIN := $(BIN_DIR)/airlys.elf # Sources -C_SRCS := kernel/kmain.c kernel/vga.c kernel/panic.c -ASM_SRCS := arch/x86_64/boot.s +C_SRCS := kernel/kmain.c \ + kernel/vga.c \ + kernel/panic.c \ + kernel/pic.c \ + kernel/timer.c \ + kernel/keyboard.c \ + arch/x86_64/idt.c + +ASM_SRCS := arch/x86_64/boot.s \ + arch/x86_64/interrupts.s # Objects OBJS := $(patsubst %.c, $(BUILD_DIR)/%.o, $(C_SRCS)) \ diff --git a/README.md b/README.md index 071e18d..51971be 100644 --- a/README.md +++ b/README.md @@ -116,11 +116,12 @@ qemu-system-x86_64 -kernel bin/airlys.elf - [x] Failsafe Kernel Panic screen - [x] Clean zero-warning freestanding build system (`clang` + `ld.lld`) -- [ ] **Phase 2: CPU Interrupts & Hardware Timing** - - [ ] Interrupt Descriptor Table (IDT64) & ISR gates - - [ ] 8259 PIC initialization & remapping - - [ ] Programmable Interval Timer (PIT) frequency driver - - [ ] PS/2 Keyboard hardware scan-code decoder +- [x] **Phase 2: CPU Interrupts & Hardware Timing** + - [x] Interrupt Descriptor Table (IDT64) & 256 gates (CPU exceptions 0..31) + - [x] Dual 8259 PIC remapping (Master 32..39, Slave 40..47) with EOI handling + - [x] Programmable Interval Timer (PIT 8254) running at 100 Hz (10ms preemptive ticks) + - [x] PS/2 Hardware Keyboard driver with Scan Code Set 1 decoding + - [x] CPU hardware interrupts enabled (`sti`) with live interactive keyboard typing! - [ ] **Phase 3: Sovereign Memory Subsystem** - [ ] Physical Memory Manager (Bitmap Page Allocator) diff --git a/arch/x86_64/idt.c b/arch/x86_64/idt.c new file mode 100644 index 0000000..9787507 --- /dev/null +++ b/arch/x86_64/idt.c @@ -0,0 +1,207 @@ +#include +#include +#include +#include + +static idt_entry_t idt_entries[256]; +static idt_ptr_t idt_ptr; +static isr_handler_t interrupt_handlers[256]; + +/* External assembly ISR declarations */ +extern void isr0(void); +extern void isr1(void); +extern void isr2(void); +extern void isr3(void); +extern void isr4(void); +extern void isr5(void); +extern void isr6(void); +extern void isr7(void); +extern void isr8(void); +extern void isr9(void); +extern void isr10(void); +extern void isr11(void); +extern void isr12(void); +extern void isr13(void); +extern void isr14(void); +extern void isr15(void); +extern void isr16(void); +extern void isr17(void); +extern void isr18(void); +extern void isr19(void); +extern void isr20(void); +extern void isr21(void); +extern void isr22(void); +extern void isr23(void); +extern void isr24(void); +extern void isr25(void); +extern void isr26(void); +extern void isr27(void); +extern void isr28(void); +extern void isr29(void); +extern void isr30(void); +extern void isr31(void); + +/* External assembly IRQ declarations */ +extern void irq0(void); +extern void irq1(void); +extern void irq2(void); +extern void irq3(void); +extern void irq4(void); +extern void irq5(void); +extern void irq6(void); +extern void irq7(void); +extern void irq8(void); +extern void irq9(void); +extern void irq10(void); +extern void irq11(void); +extern void irq12(void); +extern void irq13(void); +extern void irq14(void); +extern void irq15(void); + +static const char* exception_messages[32] = { + "Division By Zero", + "Debug", + "Non Maskable Interrupt", + "Breakpoint", + "Into Detected Overflow", + "Out of Bounds", + "Invalid Opcode", + "No Coprocessor", + "Double Fault", + "Coprocessor Segment Overrun", + "Bad TSS", + "Segment Not Present", + "Stack Fault", + "General Protection Fault", + "Page Fault", + "Unknown Interrupt", + "Coprocessor Fault", + "Alignment Check", + "Machine Check", + "SIMD Floating-Point Exception", + "Virtualization Exception", + "Control Protection Exception", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Hypervisor Injection Exception", + "VMM Communication Exception", + "Security Exception", + "Reserved" +}; + +void idt_set_gate(uint8_t num, uint64_t base, uint16_t sel, uint8_t flags) { + idt_entries[num].offset_low = (uint16_t)(base & 0xFFFF); + idt_entries[num].selector = sel; + idt_entries[num].ist = 0; + idt_entries[num].type_attr = flags; + idt_entries[num].offset_mid = (uint16_t)((base >> 16) & 0xFFFF); + idt_entries[num].offset_high = (uint32_t)((base >> 32) & 0xFFFFFFFF); + idt_entries[num].zero = 0; +} + +void register_interrupt_handler(uint8_t n, isr_handler_t handler) { + interrupt_handlers[n] = handler; +} + +void idt_init(void) { + idt_ptr.limit = sizeof(idt_entries) - 1; + idt_ptr.base = (uint64_t)&idt_entries; + + /* Zero out all handlers */ + for (int i = 0; i < 256; i++) { + idt_set_gate((uint8_t)i, 0, 0, 0); + interrupt_handlers[i] = NULL; + } + + /* 0x8E = Present (0x80) | 64-bit Interrupt Gate (0x0E) */ + uint8_t flags = 0x8E; + uint16_t cs = 0x08; /* Kernel 64-bit code segment */ + + /* Install CPU Exceptions */ + idt_set_gate(0, (uint64_t)isr0, cs, flags); + idt_set_gate(1, (uint64_t)isr1, cs, flags); + idt_set_gate(2, (uint64_t)isr2, cs, flags); + idt_set_gate(3, (uint64_t)isr3, cs, flags); + idt_set_gate(4, (uint64_t)isr4, cs, flags); + idt_set_gate(5, (uint64_t)isr5, cs, flags); + idt_set_gate(6, (uint64_t)isr6, cs, flags); + idt_set_gate(7, (uint64_t)isr7, cs, flags); + idt_set_gate(8, (uint64_t)isr8, cs, flags); + idt_set_gate(9, (uint64_t)isr9, cs, flags); + idt_set_gate(10, (uint64_t)isr10, cs, flags); + idt_set_gate(11, (uint64_t)isr11, cs, flags); + idt_set_gate(12, (uint64_t)isr12, cs, flags); + idt_set_gate(13, (uint64_t)isr13, cs, flags); + idt_set_gate(14, (uint64_t)isr14, cs, flags); + idt_set_gate(15, (uint64_t)isr15, cs, flags); + idt_set_gate(16, (uint64_t)isr16, cs, flags); + idt_set_gate(17, (uint64_t)isr17, cs, flags); + idt_set_gate(18, (uint64_t)isr18, cs, flags); + idt_set_gate(19, (uint64_t)isr19, cs, flags); + idt_set_gate(20, (uint64_t)isr20, cs, flags); + idt_set_gate(21, (uint64_t)isr21, cs, flags); + idt_set_gate(22, (uint64_t)isr22, cs, flags); + idt_set_gate(23, (uint64_t)isr23, cs, flags); + idt_set_gate(24, (uint64_t)isr24, cs, flags); + idt_set_gate(25, (uint64_t)isr25, cs, flags); + idt_set_gate(26, (uint64_t)isr26, cs, flags); + idt_set_gate(27, (uint64_t)isr27, cs, flags); + idt_set_gate(28, (uint64_t)isr28, cs, flags); + idt_set_gate(29, (uint64_t)isr29, cs, flags); + idt_set_gate(30, (uint64_t)isr30, cs, flags); + idt_set_gate(31, (uint64_t)isr31, cs, flags); + + /* Install Hardware IRQs (32..47) */ + idt_set_gate(32, (uint64_t)irq0, cs, flags); + idt_set_gate(33, (uint64_t)irq1, cs, flags); + idt_set_gate(34, (uint64_t)irq2, cs, flags); + idt_set_gate(35, (uint64_t)irq3, cs, flags); + idt_set_gate(36, (uint64_t)irq4, cs, flags); + idt_set_gate(37, (uint64_t)irq5, cs, flags); + idt_set_gate(38, (uint64_t)irq6, cs, flags); + idt_set_gate(39, (uint64_t)irq7, cs, flags); + idt_set_gate(40, (uint64_t)irq8, cs, flags); + idt_set_gate(41, (uint64_t)irq9, cs, flags); + idt_set_gate(42, (uint64_t)irq10, cs, flags); + idt_set_gate(43, (uint64_t)irq11, cs, flags); + idt_set_gate(44, (uint64_t)irq12, cs, flags); + idt_set_gate(45, (uint64_t)irq13, cs, flags); + idt_set_gate(46, (uint64_t)irq14, cs, flags); + idt_set_gate(47, (uint64_t)irq15, cs, flags); + + /* Load IDTR register into the CPU */ + __asm__ volatile ("lidt %0" : : "m"(idt_ptr)); +} + +void isr_handler(registers_t* regs) { + if (regs->int_no < 32) { + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + vga_puts("\n [CPU EXCEPTION] Vector: "); + vga_putdec(regs->int_no); + vga_puts(" -> "); + vga_puts(exception_messages[regs->int_no]); + vga_puts(" | ErrCode: "); + vga_puthex(regs->err_code); + vga_puts(" | RIP: "); + vga_puthex(regs->rip); + vga_puts("\n"); + + kernel_panic(exception_messages[regs->int_no]); + } +} + +void irq_handler(registers_t* regs) { + /* If a custom driver handler is registered, call it */ + if (interrupt_handlers[regs->int_no] != NULL) { + isr_handler_t handler = interrupt_handlers[regs->int_no]; + handler(regs); + } + + /* Send End Of Interrupt signal to 8259 PIC */ + pic_send_eoi((uint8_t)(regs->int_no - 32)); +} diff --git a/arch/x86_64/interrupts.s b/arch/x86_64/interrupts.s new file mode 100644 index 0000000..4bc5165 --- /dev/null +++ b/arch/x86_64/interrupts.s @@ -0,0 +1,173 @@ +/* + * Airlys 64-Bit Interrupt Service Routine (ISR) & IRQ Stubs + * Architecture: x86_64 Long Mode + * Aira Group of Technology + */ + +.code64 +.section .text + +.extern isr_handler +.extern irq_handler + +/* Macro for ISRs that do NOT push an error code (we push dummy 0) */ +.macro ISR_NOERRCODE num +.global isr\num +isr\num: + pushq $0 /* Dummy error code */ + pushq $\num /* Interrupt number */ + jmp isr_common_stub +.endm + +/* Macro for ISRs that DO push an error code by hardware */ +.macro ISR_ERRCODE num +.global isr\num +isr\num: + pushq $\num /* Interrupt number */ + jmp isr_common_stub +.endm + +/* Macro for IRQ stubs (remapped to 32..47) */ +.macro IRQ_STUB irq_num, int_num +.global irq\irq_num +irq\irq_num: + pushq $0 /* Dummy error code */ + pushq $\int_num /* Interrupt number (32 + irq_num) */ + jmp irq_common_stub +.endm + +/* CPU Exceptions (0-31) */ +ISR_NOERRCODE 0 +ISR_NOERRCODE 1 +ISR_NOERRCODE 2 +ISR_NOERRCODE 3 +ISR_NOERRCODE 4 +ISR_NOERRCODE 5 +ISR_NOERRCODE 6 +ISR_NOERRCODE 7 +ISR_ERRCODE 8 +ISR_NOERRCODE 9 +ISR_ERRCODE 10 +ISR_ERRCODE 11 +ISR_ERRCODE 12 +ISR_ERRCODE 13 +ISR_ERRCODE 14 +ISR_NOERRCODE 15 +ISR_NOERRCODE 16 +ISR_ERRCODE 17 +ISR_NOERRCODE 18 +ISR_NOERRCODE 19 +ISR_NOERRCODE 20 +ISR_ERRCODE 21 +ISR_NOERRCODE 22 +ISR_NOERRCODE 23 +ISR_NOERRCODE 24 +ISR_NOERRCODE 25 +ISR_NOERRCODE 26 +ISR_NOERRCODE 27 +ISR_NOERRCODE 28 +ISR_ERRCODE 29 +ISR_ERRCODE 30 +ISR_NOERRCODE 31 + +/* Hardware IRQs (0-15 mapped to 32-47) */ +IRQ_STUB 0, 32 +IRQ_STUB 1, 33 +IRQ_STUB 2, 34 +IRQ_STUB 3, 35 +IRQ_STUB 4, 36 +IRQ_STUB 5, 37 +IRQ_STUB 6, 38 +IRQ_STUB 7, 39 +IRQ_STUB 8, 40 +IRQ_STUB 9, 41 +IRQ_STUB 10, 42 +IRQ_STUB 11, 43 +IRQ_STUB 12, 44 +IRQ_STUB 13, 45 +IRQ_STUB 14, 46 +IRQ_STUB 15, 47 + +/* Common ISR stub for exceptions */ +isr_common_stub: + /* Push general purpose registers */ + pushq %rax + pushq %rbx + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %rbp + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + pushq %r12 + pushq %r13 + pushq %r14 + pushq %r15 + + /* First argument in System V AMD64 ABI is %rdi (pointer to registers_t) */ + movq %rsp, %rdi + call isr_handler + + /* Restore registers */ + popq %r15 + popq %r14 + popq %r13 + popq %r12 + popq %r11 + popq %r10 + popq %r9 + popq %r8 + popq %rbp + popq %rdi + popq %rsi + popq %rdx + popq %rcx + popq %rbx + popq %rax + + /* Clean up interrupt number and error code */ + addq $16, %rsp + iretq + +/* Common IRQ stub for hardware interrupts */ +irq_common_stub: + pushq %rax + pushq %rbx + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %rbp + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + pushq %r12 + pushq %r13 + pushq %r14 + pushq %r15 + + movq %rsp, %rdi + call irq_handler + + popq %r15 + popq %r14 + popq %r13 + popq %r12 + popq %r11 + popq %r10 + popq %r9 + popq %r8 + popq %rbp + popq %rdi + popq %rsi + popq %rdx + popq %rcx + popq %rbx + popq %rax + + addq $16, %rsp + iretq diff --git a/assets/airlys-eagle.png b/assets/airlys-eagle.png new file mode 100644 index 0000000..b01be9a Binary files /dev/null and b/assets/airlys-eagle.png differ diff --git a/assets/airlys-kali-eagle.jpg b/assets/airlys-kali-eagle.jpg new file mode 100644 index 0000000..499c24c Binary files /dev/null and b/assets/airlys-kali-eagle.jpg differ diff --git a/assets/airlys-kali-style.svg b/assets/airlys-kali-style.svg new file mode 100644 index 0000000..44a51b1 --- /dev/null +++ b/assets/airlys-kali-style.svg @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AIRLYS + THE SOVEREIGN KERNEL + diff --git a/include/airlys/idt.h b/include/airlys/idt.h new file mode 100644 index 0000000..87b4b98 --- /dev/null +++ b/include/airlys/idt.h @@ -0,0 +1,47 @@ +#ifndef AIRLYS_IDT_H +#define AIRLYS_IDT_H + +#include + +/* 64-Bit Interrupt Descriptor Table Entry */ +typedef struct __attribute__((packed)) { + uint16_t offset_low; /* Target address bits 0..15 */ + uint16_t selector; /* Kernel code segment selector (0x08) */ + uint8_t ist; /* Interrupt Stack Table index (bits 0..2) */ + uint8_t type_attr; /* Type and attributes (flags) */ + uint16_t offset_mid; /* Target address bits 16..31 */ + uint32_t offset_high; /* Target address bits 32..63 */ + uint32_t zero; /* Reserved (must be zero) */ +} idt_entry_t; + +/* IDTR register pointer */ +typedef struct __attribute__((packed)) { + uint16_t limit; + uint64_t base; +} idt_ptr_t; + +/* Register state saved on the stack by assembly ISR stubs */ +typedef struct __attribute__((packed)) { + /* Pushed by common assembly stub (reverse order of push) */ + uint64_t r15, r14, r13, r12, r11, r10, r9, r8; + uint64_t rbp, rdi, rsi, rdx, rcx, rbx, rax; + + /* Pushed by individual ISR stub */ + uint64_t int_no; + uint64_t err_code; + + /* Pushed automatically by CPU hardware on interrupt */ + uint64_t rip; + uint64_t cs; + uint64_t rflags; + uint64_t rsp; + uint64_t ss; +} registers_t; + +typedef void (*isr_handler_t)(registers_t*); + +void idt_init(void); +void idt_set_gate(uint8_t num, uint64_t base, uint16_t sel, uint8_t flags); +void register_interrupt_handler(uint8_t n, isr_handler_t handler); + +#endif /* AIRLYS_IDT_H */ diff --git a/include/airlys/io.h b/include/airlys/io.h new file mode 100644 index 0000000..2183319 --- /dev/null +++ b/include/airlys/io.h @@ -0,0 +1,33 @@ +#ifndef AIRLYS_IO_H +#define AIRLYS_IO_H + +#include + +/* Direct Hardware I/O Port operations */ + +static inline void outb(uint16_t port, uint8_t val) { + __asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port)); +} + +static inline uint8_t inb(uint16_t port) { + uint8_t ret; + __asm__ volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +static inline void outw(uint16_t port, uint16_t val) { + __asm__ volatile ("outw %0, %1" : : "a"(val), "Nd"(port)); +} + +static inline uint16_t inw(uint16_t port) { + uint16_t ret; + __asm__ volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} + +/* Wait for I/O operations to complete (useful for PIC remapping) */ +static inline void io_wait(void) { + outb(0x80, 0); +} + +#endif /* AIRLYS_IO_H */ diff --git a/include/airlys/keyboard.h b/include/airlys/keyboard.h new file mode 100644 index 0000000..7d75c03 --- /dev/null +++ b/include/airlys/keyboard.h @@ -0,0 +1,8 @@ +#ifndef AIRLYS_KEYBOARD_H +#define AIRLYS_KEYBOARD_H + +#include + +void keyboard_init(void); + +#endif /* AIRLYS_KEYBOARD_H */ diff --git a/include/airlys/pic.h b/include/airlys/pic.h new file mode 100644 index 0000000..37adab0 --- /dev/null +++ b/include/airlys/pic.h @@ -0,0 +1,21 @@ +#ifndef AIRLYS_PIC_H +#define AIRLYS_PIC_H + +#include + +#define PIC1_COMMAND 0x20 +#define PIC1_DATA 0x21 +#define PIC2_COMMAND 0xA0 +#define PIC2_DATA 0xA1 + +#define PIC_EOI 0x20 + +#define PIC1_OFFSET 32 +#define PIC2_OFFSET 40 + +void pic_init(void); +void pic_send_eoi(uint8_t irq); +void pic_mask_irq(uint8_t irq); +void pic_unmask_irq(uint8_t irq); + +#endif /* AIRLYS_PIC_H */ diff --git a/include/airlys/timer.h b/include/airlys/timer.h new file mode 100644 index 0000000..0541122 --- /dev/null +++ b/include/airlys/timer.h @@ -0,0 +1,10 @@ +#ifndef AIRLYS_TIMER_H +#define AIRLYS_TIMER_H + +#include + +void timer_init(uint32_t frequency_hz); +uint64_t timer_get_ticks(void); +void timer_sleep(uint32_t ticks); + +#endif /* AIRLYS_TIMER_H */ diff --git a/kernel/keyboard.c b/kernel/keyboard.c new file mode 100644 index 0000000..61c1ef4 --- /dev/null +++ b/kernel/keyboard.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include + +#define KBD_DATA_PORT 0x60 + +static bool shift_pressed = false; + +/* Scan Code Set 1 mapping (lower-case / normal) */ +static const char kbd_scancode_normal[128] = { + 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', + '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', + 0, /* Control */ + 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', + 0, /* Left Shift */ + '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', + 0, /* Right Shift */ + '*', + 0, /* Alt */ + ' ', /* Space */ + 0, /* Caps Lock */ +}; + +/* Scan Code Set 1 mapping (Shifted / upper-case) */ +static const char kbd_scancode_shift[128] = { + 0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', + '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', + 0, /* Control */ + 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '\"', '~', + 0, /* Left Shift */ + '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', + 0, /* Right Shift */ + '*', + 0, /* Alt */ + ' ', /* Space */ + 0, /* Caps Lock */ +}; + +static void keyboard_callback(registers_t* regs) { + (void)regs; + uint8_t scancode = inb(KBD_DATA_PORT); + + /* Check for Shift key press (0x2A left, 0x36 right) */ + if (scancode == 0x2A || scancode == 0x36) { + shift_pressed = true; + return; + } + /* Check for Shift key release (0xAA left, 0xB6 right) */ + if (scancode == 0xAA || scancode == 0xB6) { + shift_pressed = false; + return; + } + + /* Key release has bit 7 set (scancode >= 0x80) */ + if (scancode & 0x80) { + return; + } + + if (scancode < 128) { + char c = shift_pressed ? kbd_scancode_shift[scancode] : kbd_scancode_normal[scancode]; + if (c != 0) { + vga_putchar(c); + } + } +} + +void keyboard_init(void) { + /* Register keyboard on IRQ1 (Vector 33) */ + register_interrupt_handler(33, keyboard_callback); + + /* Unmask IRQ1 on Master PIC */ + pic_unmask_irq(1); +} diff --git a/kernel/kmain.c b/kernel/kmain.c index 5d2c68f..9d3ba11 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -1,8 +1,12 @@ #include #include +#include +#include +#include +#include void kernel_main(uint64_t magic, uint64_t addr) { - /* Initialize hardware VGA terminal console */ + /* Step 1: Initialize hardware VGA terminal console */ vga_init(); /* Banner & Sovereign Branding */ @@ -12,14 +16,13 @@ void kernel_main(uint64_t magic, uint64_t addr) { vga_puts(" / \\ |_ _| _ \\| | \\ \\ / / ___| \n"); vga_puts(" / _ \\ | || |_) | | \\ V /\\___ \\ \n"); vga_puts(" / ___ \\ | || _ <| |___| | ___) |\n"); - vga_puts(" /_/ \\_\\___|_| \\_\\_____|_| |____/ SOVEREIGN OS KERNEL v0.1.0\n"); + vga_puts(" /_/ \\_\\___|_| \\_\\_____|_| |____/ SOVEREIGN OS KERNEL v0.2.0\n"); vga_puts("================================================================================\n"); vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK); - vga_puts(" [OK] Airlys Kernel Genesis initialized successfully!\n"); - vga_puts(" [OK] Architecture : x86_64 (64-Bit Long Mode)\n"); - vga_puts(" [OK] Creator : Er. Adam Eehan (Founder & CEO, Aira Group)\n"); - vga_puts(" [OK] Co-Pilot : Sovereign AI Aira 3 Ultra (Engine v3.6)\n"); + vga_puts(" [OK] Airlys Kernel Architecture : x86_64 Long Mode (64-Bit)\n"); + vga_puts(" [OK] Lead Systems Architect : Er. Adam Eehan (Founder & CEO, Aira Group)\n"); + vga_puts(" [OK] Engineering Co-Pilot : Sovereign AI Aira 3 Ultra (Engine v3.6)\n"); vga_set_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); vga_puts("--------------------------------------------------------------------------------\n"); @@ -27,19 +30,41 @@ void kernel_main(uint64_t magic, uint64_t addr) { vga_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK); vga_puts(" [*] Bootloader Magic : "); vga_puthex(magic); - vga_puts("\n"); - vga_puts(" [*] Multiboot Info : "); + vga_puts(" | Multiboot Info: "); vga_puthex(addr); vga_puts("\n"); + /* Step 2: Initialize 64-bit Interrupt Descriptor Table (IDT) */ + idt_init(); vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); - vga_puts(" [*] Physical VGA Memory Map: 0x000B8000 (80x25 Text Mode)\n"); - vga_puts(" [*] Preemptive Subsystems: Ready for Stage 2 (GDT & IDT Interrupts)\n"); + vga_puts(" [OK] CPU IDT Loaded : 256 Gates (Vectors 0..255 active)\n"); + + /* Step 3: Remap 8259 Dual Programmable Interrupt Controllers */ + pic_init(); + vga_puts(" [OK] Dual 8259 PIC Remapped : Master IRQ 32..39, Slave IRQ 40..47\n"); + + /* Step 4: Initialize Programmable Interval Timer (PIT) at 100 Hz */ + timer_init(100); + vga_puts(" [OK] Programmable Interval Timer: 100 Hz (10ms preemptive tick interval)\n"); + + /* Step 5: Initialize PS/2 Keyboard Driver */ + keyboard_init(); + vga_puts(" [OK] PS/2 Hardware Keyboard : Scan Code Set 1 Driver active\n"); + + /* Step 6: Enable CPU Hardware Interrupts */ + __asm__ volatile ("sti"); + vga_set_color(VGA_COLOR_GREEN, VGA_COLOR_BLACK); + vga_puts(" [OK] Hardware Interrupts Active : STI executed (IF=1)\n"); vga_set_color(VGA_COLOR_LIGHT_MAGENTA, VGA_COLOR_BLACK); - vga_puts("\n >>> Airlys System Idle Loop Active. CPU Halted awaiting hardware ticks.\n"); + vga_puts("\n ------------------------------------------------------------------------------\n"); + vga_puts(" >>> Interactive Console Active. Type on your keyboard to test hardware input:\n"); + vga_puts(" ------------------------------------------------------------------------------\n\n"); - /* Infinite safe idle loop */ + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts("airlys> "); + + /* Infinite safe idle loop awaiting hardware interrupts */ for (;;) { __asm__ volatile ("hlt"); } diff --git a/kernel/pic.c b/kernel/pic.c new file mode 100644 index 0000000..ef11d1c --- /dev/null +++ b/kernel/pic.c @@ -0,0 +1,77 @@ +#include +#include + +#define ICW1_INIT 0x11 +#define ICW4_8086 0x01 + +void pic_init(void) { + uint8_t a1, a2; + + /* Save masks */ + a1 = inb(PIC1_DATA); + a2 = inb(PIC2_DATA); + + /* Start initialization sequence in cascade mode (ICW1) */ + outb(PIC1_COMMAND, ICW1_INIT); + io_wait(); + outb(PIC2_COMMAND, ICW1_INIT); + io_wait(); + + /* ICW2: Master PIC vector offset (32) and Slave PIC vector offset (40) */ + outb(PIC1_DATA, PIC1_OFFSET); + io_wait(); + outb(PIC2_DATA, PIC2_OFFSET); + io_wait(); + + /* ICW3: Tell Master PIC there is a Slave PIC at IRQ2 (0000 0100) */ + outb(PIC1_DATA, 4); + io_wait(); + /* Tell Slave PIC its cascade identity (0000 0010) */ + outb(PIC2_DATA, 2); + io_wait(); + + /* ICW4: Set 8086 mode */ + outb(PIC1_DATA, ICW4_8086); + io_wait(); + outb(PIC2_DATA, ICW4_8086); + io_wait(); + + /* Restore masks (or mask all except cascade) */ + outb(PIC1_DATA, a1); + outb(PIC2_DATA, a2); +} + +void pic_send_eoi(uint8_t irq) { + if (irq >= 8) { + outb(PIC2_COMMAND, PIC_EOI); + } + outb(PIC1_COMMAND, PIC_EOI); +} + +void pic_mask_irq(uint8_t irq) { + uint16_t port; + uint8_t value; + + if (irq < 8) { + port = PIC1_DATA; + } else { + port = PIC2_DATA; + irq -= 8; + } + value = inb(port) | (uint8_t)(1 << irq); + outb(port, value); +} + +void pic_unmask_irq(uint8_t irq) { + uint16_t port; + uint8_t value; + + if (irq < 8) { + port = PIC1_DATA; + } else { + port = PIC2_DATA; + irq -= 8; + } + value = inb(port) & (uint8_t)~(1 << irq); + outb(port, value); +} diff --git a/kernel/timer.c b/kernel/timer.c new file mode 100644 index 0000000..a70eb0e --- /dev/null +++ b/kernel/timer.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#define PIT_CHANNEL0 0x40 +#define PIT_COMMAND 0x43 +#define PIT_BASE_FREQ 1193182 + +static volatile uint64_t timer_ticks = 0; + +static void timer_callback(registers_t* regs) { + (void)regs; + timer_ticks++; +} + +void timer_init(uint32_t frequency_hz) { + if (frequency_hz == 0) frequency_hz = 100; + + uint32_t divisor = PIT_BASE_FREQ / frequency_hz; + if (divisor == 0) divisor = 1; + if (divisor > 65535) divisor = 65535; + + /* Register callback on IRQ0 (Vector 32) */ + register_interrupt_handler(32, timer_callback); + + /* 0x36 = Channel 0, lobyte/hibyte, Mode 3 (Square wave generator), binary 16-bit */ + outb(PIT_COMMAND, 0x36); + outb(PIT_CHANNEL0, (uint8_t)(divisor & 0xFF)); + outb(PIT_CHANNEL0, (uint8_t)((divisor >> 8) & 0xFF)); + + /* Unmask IRQ0 on Master PIC */ + pic_unmask_irq(0); +} + +uint64_t timer_get_ticks(void) { + return timer_ticks; +} + +void timer_sleep(uint32_t ticks) { + uint64_t start = timer_ticks; + while ((timer_ticks - start) < ticks) { + __asm__ volatile ("hlt"); + } +}