feat(kernel): implement Phase 2 - IDT64, 8259 PIC, 100Hz PIT timer & PS/2 keyboard driver

This commit is contained in:
Adam Eehan committed 2026-10-23 09:37:54 +05:30
1 parent eec88c14c0
commit 7251770d7f
16 files changed
+863 -19

No files matched your search

+10 -2
View File
@@ -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)) \
+6 -5
View File
@@ -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)
+207
View File
@@ -0,0 +1,207 @@
#include <airlys/idt.h>
#include <airlys/vga.h>
#include <airlys/kernel.h>
#include <airlys/pic.h>
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));
}
+173
View File
@@ -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
Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

+114
View File
@@ -0,0 +1,114 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000" width="100%" height="100%">
<defs>
<!-- Kali-style Vibrant Blue/Cyan Gradient -->
<linearGradient id="kaliBlue" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#00f2fe"/>
<stop offset="40%" stop-color="#00a8ff"/>
<stop offset="100%" stop-color="#0051ff"/>
</linearGradient>
<!-- Subtle Glow for Wallpaper Look -->
<filter id="kaliGlow" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur stdDeviation="8" result="blur"/>
<feMerge>
<feMergeNode in="blur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>
<!-- Deep Pure Black Background like Kali Linux -->
<rect width="1000" height="1000" fill="#06090e"/>
<!-- Kali-Style Minimalist Tribal/Cyber Eagle Silhouette -->
<g transform="translate(100, 120) scale(0.8)" filter="url(#kaliGlow)">
<!-- Left Main Sweeping Wing (Tribal Curves) -->
<path d="M 500 350
C 420 220 280 120 80 100
C 120 180 200 240 280 280
C 180 260 90 320 40 380
C 120 400 220 410 310 410
C 210 420 120 490 80 560
C 160 550 250 530 340 500
C 240 540 180 620 150 700
C 230 660 320 610 400 560
C 320 620 280 710 260 800
C 340 730 420 660 480 580
C 450 660 430 760 430 850
C 480 770 510 680 520 590
Z"
fill="url(#kaliBlue)"/>
<!-- Right Main Sweeping Wing (Mirrored Dynamic Curves) -->
<path d="M 500 350
C 580 220 720 120 920 100
C 880 180 800 240 720 280
C 820 260 910 320 960 380
C 880 400 780 410 690 410
C 790 420 880 490 920 560
C 840 550 750 530 660 500
C 760 540 820 620 850 700
C 770 660 680 610 600 560
C 680 620 720 710 740 800
C 660 730 580 660 520 580
C 550 660 570 760 570 850
C 520 770 490 680 480 590
Z"
fill="url(#kaliBlue)"/>
<!-- Eagle Head, Crown & Hooked Beak (Sleek Tribal Profile) -->
<path d="M 500 180
C 480 120 450 80 420 50
C 460 70 490 100 500 130
C 520 70 560 30 620 10
C 590 60 560 110 550 160
C 580 130 630 110 680 100
C 640 150 590 190 560 230
C 600 240 640 260 670 300
C 610 290 560 300 530 340
C 525 360 515 390 500 420
C 485 390 475 360 470 340
C 440 300 390 290 330 300
C 360 260 400 240 440 230
C 410 190 360 150 320 100
C 370 110 420 130 450 160
Z"
fill="url(#kaliBlue)"/>
<!-- Piercing Diamond Negative Space Eye -->
<polygon points="460,260 480,240 500,260 480,270" fill="#06090e"/>
<polygon points="540,260 520,240 500,260 520,270" fill="#06090e"/>
<!-- Aggressive Center Beak Blade -->
<path d="M 485 280
C 495 320 500 350 500 390
C 500 350 505 320 515 280
Z"
fill="#ffffff"/>
<!-- Center Chest Split Line (Kali-style Negative Space Blade) -->
<path d="M 500 430
L 500 620
L 485 530
Z"
fill="#06090e"/>
<path d="M 500 430
L 500 620
L 515 530
Z"
fill="#06090e"/>
<!-- Dynamic Tail Blade (Kali-style sharp swooping fin) -->
<path d="M 470 650
C 485 750 495 830 500 920
C 505 830 515 750 530 650
L 500 700
Z"
fill="url(#kaliBlue)"/>
</g>
<!-- Sleek Kali-Style Typographic Branding at Bottom -->
<text x="500" y="930" font-family="'Orbitron', 'Inter', monospace, sans-serif" font-size="34" font-weight="900" letter-spacing="16" fill="#ffffff" text-anchor="middle">AIRLYS</text>
<text x="500" y="960" font-family="'Inter', monospace, sans-serif" font-size="11" font-weight="600" letter-spacing="6" fill="#00f2fe" text-anchor="middle" opacity="0.8">THE SOVEREIGN KERNEL</text>
</svg>

After

Width:  |  Height:  |  Size: 4.1 KiB

+47
View File
@@ -0,0 +1,47 @@
#ifndef AIRLYS_IDT_H
#define AIRLYS_IDT_H
#include <airlys/types.h>
/* 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 */
+33
View File
@@ -0,0 +1,33 @@
#ifndef AIRLYS_IO_H
#define AIRLYS_IO_H
#include <airlys/types.h>
/* 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 */
+8
View File
@@ -0,0 +1,8 @@
#ifndef AIRLYS_KEYBOARD_H
#define AIRLYS_KEYBOARD_H
#include <airlys/types.h>
void keyboard_init(void);
#endif /* AIRLYS_KEYBOARD_H */
+21
View File
@@ -0,0 +1,21 @@
#ifndef AIRLYS_PIC_H
#define AIRLYS_PIC_H
#include <airlys/types.h>
#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 */
+10
View File
@@ -0,0 +1,10 @@
#ifndef AIRLYS_TIMER_H
#define AIRLYS_TIMER_H
#include <airlys/types.h>
void timer_init(uint32_t frequency_hz);
uint64_t timer_get_ticks(void);
void timer_sleep(uint32_t ticks);
#endif /* AIRLYS_TIMER_H */
+75
View File
@@ -0,0 +1,75 @@
#include <airlys/keyboard.h>
#include <airlys/idt.h>
#include <airlys/io.h>
#include <airlys/vga.h>
#include <airlys/pic.h>
#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);
}
+37 -12
View File
@@ -1,8 +1,12 @@
#include <airlys/kernel.h>
#include <airlys/vga.h>
#include <airlys/idt.h>
#include <airlys/pic.h>
#include <airlys/timer.h>
#include <airlys/keyboard.h>
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");
}
+77
View File
@@ -0,0 +1,77 @@
#include <airlys/pic.h>
#include <airlys/io.h>
#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);
}
+45
View File
@@ -0,0 +1,45 @@
#include <airlys/timer.h>
#include <airlys/idt.h>
#include <airlys/io.h>
#include <airlys/pic.h>
#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");
}
}