refactor(architecture): adopt canonical Linux enterprise directory layout (arch, drivers, fs, mm, kernel, lib, security, scripts, tools)

This commit is contained in:
Adam Eehan committed 2026-10-23 18:23:16 +05:30
1 parent 67ed4de23f
commit 2bbeb3f345
17 files changed
+217 -9

No files matched your search

-175
View File
@@ -1,175 +0,0 @@
#include <airlys/heap.h>
#include <airlys/vga.h>
#include <airlys/kernel.h>
#define HEAP_MAGIC 0xA18A3644ULL
typedef struct heap_block {
size_t size; /* Payload size in bytes */
bool is_free; /* True if block is free */
uint32_t magic; /* Integrity guard */
struct heap_block* next; /* Next block in list */
struct heap_block* prev; /* Previous block */
} __attribute__((aligned(16))) heap_block_t;
static uintptr_t heap_start = 0;
static size_t heap_total_size = 0;
static heap_block_t* heap_head = NULL;
static inline size_t align16(size_t size) {
return (size + 15) & ~15ULL;
}
void heap_init(uintptr_t start_addr, size_t size) {
heap_start = (start_addr + 15) & ~15ULL;
heap_total_size = size;
heap_head = (heap_block_t*)heap_start;
heap_head->size = size - sizeof(heap_block_t);
heap_head->is_free = true;
heap_head->magic = HEAP_MAGIC;
heap_head->next = NULL;
heap_head->prev = NULL;
}
void* kmalloc(size_t size) {
if (size == 0 || heap_head == NULL) {
return NULL;
}
size_t aligned_size = align16(size);
heap_block_t* curr = heap_head;
while (curr) {
if (curr->magic != HEAP_MAGIC) {
kernel_panic("Kernel Heap Corruption detected: invalid magic token!");
}
if (curr->is_free && curr->size >= aligned_size) {
/* Check if block can be split */
if (curr->size >= aligned_size + sizeof(heap_block_t) + 32) {
uintptr_t new_block_addr = (uintptr_t)curr + sizeof(heap_block_t) + aligned_size;
heap_block_t* new_block = (heap_block_t*)new_block_addr;
new_block->size = curr->size - aligned_size - sizeof(heap_block_t);
new_block->is_free = true;
new_block->magic = HEAP_MAGIC;
new_block->next = curr->next;
new_block->prev = curr;
if (curr->next) {
curr->next->prev = new_block;
}
curr->next = new_block;
curr->size = aligned_size;
}
curr->is_free = false;
return (void*)((uint8_t*)curr + sizeof(heap_block_t));
}
curr = curr->next;
}
return NULL; /* Out of heap memory */
}
void kfree(void* ptr) {
if (!ptr) return;
heap_block_t* block = (heap_block_t*)((uint8_t*)ptr - sizeof(heap_block_t));
if (block->magic != HEAP_MAGIC) {
kernel_panic("Kernel kfree error: double free or invalid memory pointer!");
}
block->is_free = true;
/* Coalesce with next block if free */
if (block->next && block->next->is_free) {
block->size += sizeof(heap_block_t) + block->next->size;
block->next = block->next->next;
if (block->next) {
block->next->prev = block;
}
}
/* Coalesce with prev block if free */
if (block->prev && block->prev->is_free) {
block->prev->size += sizeof(heap_block_t) + block->size;
block->prev->next = block->next;
if (block->next) {
block->next->prev = block->prev;
}
}
}
void* kcalloc(size_t num, size_t size) {
size_t total = num * size;
void* ptr = kmalloc(total);
if (ptr) {
uint8_t* p = (uint8_t*)ptr;
for (size_t i = 0; i < total; i++) {
p[i] = 0;
}
}
return ptr;
}
void* krealloc(void* ptr, size_t new_size) {
if (!ptr) {
return kmalloc(new_size);
}
if (new_size == 0) {
kfree(ptr);
return NULL;
}
heap_block_t* block = (heap_block_t*)((uint8_t*)ptr - sizeof(heap_block_t));
if (block->magic != HEAP_MAGIC) {
kernel_panic("Kernel krealloc error: invalid magic header!");
}
if (block->size >= new_size) {
return ptr; /* Existing block is already large enough */
}
void* new_ptr = kmalloc(new_size);
if (!new_ptr) return NULL;
uint8_t* src = (uint8_t*)ptr;
uint8_t* dst = (uint8_t*)new_ptr;
for (size_t i = 0; i < block->size; i++) {
dst[i] = src[i];
}
kfree(ptr);
return new_ptr;
}
size_t heap_get_total_bytes(void) {
return heap_total_size;
}
size_t heap_get_used_bytes(void) {
size_t used = 0;
heap_block_t* curr = heap_head;
while (curr) {
if (!curr->is_free) {
used += curr->size + sizeof(heap_block_t);
}
curr = curr->next;
}
return used;
}
size_t heap_get_free_bytes(void) {
size_t free_mem = 0;
heap_block_t* curr = heap_head;
while (curr) {
if (curr->is_free) {
free_mem += curr->size;
}
curr = curr->next;
}
return free_mem;
}
-76
View File
@@ -1,76 +0,0 @@
#include <airlys/keyboard.h>
#include <airlys/idt.h>
#include <airlys/io.h>
#include <airlys/vga.h>
#include <airlys/pic.h>
#include <airlys/shell.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) {
shell_handle_char(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);
}
-77
View File
@@ -1,77 +0,0 @@
#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);
}
-136
View File
@@ -1,136 +0,0 @@
#include <airlys/pmm.h>
#include <airlys/vga.h>
extern uint8_t _kernel_start[];
extern uint8_t _kernel_end[];
static uint8_t* pmm_bitmap = NULL;
static size_t pmm_total_frames = 0;
static size_t pmm_used_frames = 0;
static inline void pmm_set_bit(size_t frame) {
pmm_bitmap[frame / 8] |= (1 << (frame % 8));
}
static inline void pmm_clear_bit(size_t frame) {
pmm_bitmap[frame / 8] &= ~(1 << (frame % 8));
}
static inline bool pmm_test_bit(size_t frame) {
return (pmm_bitmap[frame / 8] & (1 << (frame % 8))) != 0;
}
void pmm_init(uintptr_t mem_size) {
if (mem_size > PMM_MAX_MEMORY || mem_size == 0) {
mem_size = PMM_MAX_MEMORY;
}
pmm_total_frames = mem_size / PAGE_SIZE;
pmm_used_frames = pmm_total_frames; /* Initially mark all as used */
/* Place bitmap directly at _kernel_end (page aligned) */
uintptr_t bitmap_addr = ((uintptr_t)_kernel_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
pmm_bitmap = (uint8_t*)bitmap_addr;
/* Fill bitmap with 1s (all used) */
size_t bitmap_bytes = pmm_total_frames / 8;
for (size_t i = 0; i < bitmap_bytes; i++) {
pmm_bitmap[i] = 0xFF;
}
/* Usable memory starts immediately after the bitmap */
uintptr_t free_mem_start = bitmap_addr + bitmap_bytes;
free_mem_start = (free_mem_start + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
size_t start_frame = free_mem_start / PAGE_SIZE;
for (size_t frame = start_frame; frame < pmm_total_frames; frame++) {
pmm_clear_bit(frame);
pmm_used_frames--;
}
}
void* pmm_alloc_frame(void) {
size_t bitmap_bytes = pmm_total_frames / 8;
for (size_t i = 0; i < bitmap_bytes; i++) {
if (pmm_bitmap[i] != 0xFF) {
for (int bit = 0; bit < 8; bit++) {
if (!(pmm_bitmap[i] & (1 << bit))) {
size_t frame = i * 8 + bit;
if (frame < pmm_total_frames) {
pmm_set_bit(frame);
pmm_used_frames++;
return (void*)(frame * PAGE_SIZE);
}
}
}
}
}
return NULL; /* Out of physical memory */
}
void pmm_free_frame(void* frame_addr) {
uintptr_t addr = (uintptr_t)frame_addr;
size_t frame = addr / PAGE_SIZE;
if (frame < pmm_total_frames && pmm_test_bit(frame)) {
pmm_clear_bit(frame);
pmm_used_frames--;
}
}
void* pmm_alloc_frames(size_t count) {
if (count == 0) return NULL;
if (count == 1) return pmm_alloc_frame();
size_t free_run = 0;
size_t start_frame = 0;
for (size_t frame = 0; frame < pmm_total_frames; frame++) {
if (!pmm_test_bit(frame)) {
if (free_run == 0) {
start_frame = frame;
}
free_run++;
if (free_run == count) {
for (size_t j = start_frame; j < start_frame + count; j++) {
pmm_set_bit(j);
pmm_used_frames++;
}
return (void*)(start_frame * PAGE_SIZE);
}
} else {
free_run = 0;
}
}
return NULL;
}
void pmm_free_frames(void* frame_addr, size_t count) {
uintptr_t addr = (uintptr_t)frame_addr;
size_t start_frame = addr / PAGE_SIZE;
for (size_t i = 0; i < count; i++) {
size_t frame = start_frame + i;
if (frame < pmm_total_frames && pmm_test_bit(frame)) {
pmm_clear_bit(frame);
pmm_used_frames--;
}
}
}
size_t pmm_get_total_frames(void) {
return pmm_total_frames;
}
size_t pmm_get_used_frames(void) {
return pmm_used_frames;
}
size_t pmm_get_free_frames(void) {
return pmm_total_frames - pmm_used_frames;
}
uint64_t pmm_get_free_memory_kb(void) {
return (uint64_t)(pmm_get_free_frames() * PAGE_SIZE) / 1024;
}
-211
View File
@@ -1,211 +0,0 @@
#include <airlys/ramfs.h>
#include <airlys/heap.h>
static uint32_t ramfs_next_inode = 1;
static size_t kstrlen(const char* str) {
size_t len = 0;
while (str && str[len] != '\0') {
len++;
}
return len;
}
static void kstrncpy(char* dest, const char* src, size_t n) {
size_t i = 0;
while (i < n - 1 && src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
static int kstrcmp(const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
static uint64_t ramfs_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) {
if (!node || !buffer || !node->internal_data) return 0;
if (offset >= node->size) return 0;
if (offset + size > node->size) {
size = node->size - offset;
}
const uint8_t* src = (const uint8_t*)node->internal_data + offset;
for (uint64_t i = 0; i < size; i++) {
buffer[i] = src[i];
}
return size;
}
static struct vfs_node* ramfs_finddir(vfs_node_t* node, const char* name) {
if (!node || !name) return NULL;
for (uint32_t i = 0; i < node->num_children; i++) {
if (kstrcmp(node->children[i]->name, name) == 0) {
return node->children[i];
}
}
return NULL;
}
static struct vfs_node* ramfs_readdir(vfs_node_t* node, uint32_t index) {
if (!node || index >= node->num_children) return NULL;
return node->children[index];
}
vfs_node_t* ramfs_create_dir(vfs_node_t* parent, const char* name) {
vfs_node_t* dir = (vfs_node_t*)kmalloc(sizeof(vfs_node_t));
if (!dir) return NULL;
kstrncpy(dir->name, name, sizeof(dir->name));
dir->flags = VFS_DIRECTORY;
dir->size = 0;
dir->inode = ramfs_next_inode++;
dir->read = NULL;
dir->write = NULL;
dir->finddir = ramfs_finddir;
dir->readdir = ramfs_readdir;
dir->parent = parent;
dir->num_children = 0;
dir->internal_data = NULL;
for (size_t i = 0; i < VFS_MAX_CHILDREN; i++) {
dir->children[i] = NULL;
}
if (parent && parent->num_children < VFS_MAX_CHILDREN) {
parent->children[parent->num_children++] = dir;
}
return dir;
}
vfs_node_t* ramfs_create_file(vfs_node_t* parent, const char* name, const char* content) {
vfs_node_t* file = (vfs_node_t*)kmalloc(sizeof(vfs_node_t));
if (!file) return NULL;
kstrncpy(file->name, name, sizeof(file->name));
file->flags = VFS_FILE;
file->inode = ramfs_next_inode++;
file->read = ramfs_read;
file->write = NULL;
file->finddir = NULL;
file->readdir = NULL;
file->parent = parent;
file->num_children = 0;
size_t len = content ? kstrlen(content) : 0;
file->size = len;
if (len > 0) {
char* data = (char*)kmalloc(len + 1);
if (data) {
for (size_t i = 0; i < len; i++) {
data[i] = content[i];
}
data[len] = '\0';
file->internal_data = data;
} else {
file->internal_data = NULL;
}
} else {
file->internal_data = NULL;
}
for (size_t i = 0; i < VFS_MAX_CHILDREN; i++) {
file->children[i] = NULL;
}
if (parent && parent->num_children < VFS_MAX_CHILDREN) {
parent->children[parent->num_children++] = file;
}
return file;
}
bool ramfs_delete_node(vfs_node_t* parent, const char* name) {
if (!parent || !name) return false;
for (uint32_t i = 0; i < parent->num_children; i++) {
if (kstrcmp(parent->children[i]->name, name) == 0) {
vfs_node_t* target = parent->children[i];
/* If directory, ensure it has no children */
if ((target->flags & VFS_DIRECTORY) && target->num_children > 0) {
return false; /* Directory not empty */
}
if (target->internal_data) {
kfree(target->internal_data);
target->internal_data = NULL;
}
kfree(target);
/* Shift remaining children left */
for (uint32_t j = i; j < parent->num_children - 1; j++) {
parent->children[j] = parent->children[j + 1];
}
parent->children[parent->num_children - 1] = NULL;
parent->num_children--;
return true;
}
}
return false;
}
vfs_node_t* ramfs_init(void) {
/* Create Root "/" directory */
vfs_node_t* root = ramfs_create_dir(NULL, "/");
vfs_set_root(root);
/* Standard filesystem directory tree */
vfs_node_t* etc = ramfs_create_dir(root, "etc");
vfs_node_t* bin = ramfs_create_dir(root, "bin");
vfs_node_t* home = ramfs_create_dir(root, "home");
vfs_node_t* user = ramfs_create_dir(home, "user");
vfs_node_t* dev = ramfs_create_dir(root, "dev");
(void)bin;
(void)dev;
/* System configuration & metadata files */
ramfs_create_file(etc, "os-release",
"NAME=\"Airlys Sovereign OS\"\n"
"VERSION=\"0.2.0-genesis\"\n"
"ID=\"airlys\"\n"
"ARCH=\"x86_64\"\n"
"LEAD_ARCHITECT=\"Er. Adam Eehan\"\n"
"ORGANIZATION=\"Aira Group of Technology\"\n"
"COVENANT=\"100% Free Lifetime Compute for Students Worldwide\"\n");
ramfs_create_file(etc, "covenant.txt",
"=================================================================\n"
" THE SOVEREIGN OPEN-SOURCE COVENANT \n"
" Aira Group of Technology & Er. Adam Eehan \n"
"=================================================================\n"
"Pledge: 100% Free Lifetime Sovereign OS, Software & Cloud Compute\n"
"for students, underprivileged youth, and open-source creators \n"
"worldwide. Funded by ethical enterprise adoption in the West. \n"
"Knowledge and computational power are birthrights, not luxury! \n");
ramfs_create_file(user, "welcome.txt",
"Welcome to Airlys Sovereign Terminal!\n"
"Engineered from first principles by Er. Adam Eehan.\n\n"
"Useful Commands:\n"
" help - Show all commands\n"
" ls - List directory contents\n"
" cat <f> - Display file content\n"
" whoami - Display current user & authority\n"
" ps - Display active running tasks\n"
" free - Memory statistics\n"
" covenant - Read our public open-source pledge\n"
" aira <cmd>- Sovereign root privilege execution\n");
return root;
}
-47
View File
@@ -1,47 +0,0 @@
#include <airlys/timer.h>
#include <airlys/idt.h>
#include <airlys/io.h>
#include <airlys/pic.h>
#include <airlys/sched.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++;
sched_timer_tick();
}
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");
}
}
-82
View File
@@ -1,82 +0,0 @@
#include <airlys/vfs.h>
#include <airlys/heap.h>
static vfs_node_t* vfs_root = NULL;
static int kstrcmp(const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
void vfs_init(void) {
vfs_root = NULL;
}
vfs_node_t* vfs_get_root(void) {
return vfs_root;
}
void vfs_set_root(vfs_node_t* root) {
vfs_root = root;
}
uint64_t vfs_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) {
if (node && node->read) {
return node->read(node, offset, size, buffer);
}
return 0;
}
uint64_t vfs_write(vfs_node_t* node, uint64_t offset, uint64_t size, const uint8_t* buffer) {
if (node && node->write) {
return node->write(node, offset, size, buffer);
}
return 0;
}
vfs_node_t* vfs_finddir(vfs_node_t* node, const char* name) {
if (node && node->finddir) {
return node->finddir(node, name);
}
return NULL;
}
vfs_node_t* vfs_readdir(vfs_node_t* node, uint32_t index) {
if (node && node->readdir) {
return node->readdir(node, index);
}
return NULL;
}
vfs_node_t* vfs_resolve_path(const char* path) {
if (!path || !vfs_root) return NULL;
if (kstrcmp(path, "/") == 0) return vfs_root;
const char* ptr = path;
if (*ptr == '/') ptr++; /* Skip leading slash */
vfs_node_t* curr = vfs_root;
char token[VFS_MAX_NAME];
while (*ptr != '\0') {
size_t idx = 0;
while (*ptr != '/' && *ptr != '\0' && idx < VFS_MAX_NAME - 1) {
token[idx++] = *ptr++;
}
token[idx] = '\0';
if (*ptr == '/') ptr++;
if (idx > 0) {
vfs_node_t* next = vfs_finddir(curr, token);
if (!next) {
return NULL; /* Path component not found */
}
curr = next;
}
}
return curr;
}
-180
View File
@@ -1,180 +0,0 @@
#include <airlys/vga.h>
static size_t vga_row;
static size_t vga_column;
static uint8_t vga_current_color;
static volatile uint16_t* const vga_buffer = (volatile uint16_t*)VGA_BUFFER_PHYSICAL;
/* Outport byte to I/O port (used for hardware cursor updates) */
static inline void outb(uint16_t port, uint8_t val) {
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
static void vga_update_cursor(void) {
uint16_t pos = (uint16_t)(vga_row * VGA_WIDTH + vga_column);
outb(0x3D4, 0x0F);
outb(0x3D5, (uint8_t)(pos & 0xFF));
outb(0x3D4, 0x0E);
outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF));
}
static void vga_scroll(void) {
for (size_t y = 0; y < VGA_HEIGHT - 1; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
vga_buffer[y * VGA_WIDTH + x] = vga_buffer[(y + 1) * VGA_WIDTH + x];
}
}
for (size_t x = 0; x < VGA_WIDTH; x++) {
vga_buffer[(VGA_HEIGHT - 1) * VGA_WIDTH + x] = vga_entry(' ', vga_current_color);
}
vga_row = VGA_HEIGHT - 1;
}
void vga_init(void) {
vga_row = 0;
vga_column = 0;
vga_current_color = vga_entry_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
vga_clear();
}
void vga_set_color(vga_color_t fg, vga_color_t bg) {
vga_current_color = vga_entry_color(fg, bg);
}
void vga_clear(void) {
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
vga_buffer[y * VGA_WIDTH + x] = vga_entry(' ', vga_current_color);
}
}
vga_row = 0;
vga_column = 0;
vga_update_cursor();
}
void vga_putchar(char c) {
if (c == '\b') {
if (vga_column > 12) {
vga_column--;
const size_t index = vga_row * VGA_WIDTH + vga_column;
vga_buffer[index] = vga_entry(' ', vga_current_color);
vga_update_cursor();
}
return;
}
if (c == '\n') {
vga_column = 0;
if (++vga_row == VGA_HEIGHT) {
vga_scroll();
}
vga_update_cursor();
return;
}
if (c == '\r') {
vga_column = 0;
vga_update_cursor();
return;
}
if (c == '\t') {
vga_column = (vga_column + 4) & ~3;
if (vga_column >= VGA_WIDTH) {
vga_column = 0;
if (++vga_row == VGA_HEIGHT) {
vga_scroll();
}
}
vga_update_cursor();
return;
}
const size_t index = vga_row * VGA_WIDTH + vga_column;
vga_buffer[index] = vga_entry((unsigned char)c, vga_current_color);
if (++vga_column == VGA_WIDTH) {
vga_column = 0;
if (++vga_row == VGA_HEIGHT) {
vga_scroll();
}
}
vga_update_cursor();
}
void vga_write(const char* data, size_t size) {
for (size_t i = 0; i < size; i++) {
vga_putchar(data[i]);
}
}
void vga_puts(const char* str) {
if (!str) return;
while (*str) {
vga_putchar(*str++);
}
}
void vga_puthex(uint64_t val) {
const char hex_chars[] = "0123456789ABCDEF";
vga_puts("0x");
for (int i = 60; i >= 0; i -= 4) {
vga_putchar(hex_chars[(val >> i) & 0xF]);
}
}
void vga_putdec(uint64_t val) {
if (val == 0) {
vga_putchar('0');
return;
}
char buf[24];
int i = 0;
while (val > 0) {
buf[i++] = (char)('0' + (val % 10));
val /= 10;
}
while (--i >= 0) {
vga_putchar(buf[i]);
}
}
static char vga_prompt_path[64] = "/home/user";
void vga_set_prompt_path(const char* path) {
if (!path) return;
size_t i = 0;
while (path[i] != '\0' && i < sizeof(vga_prompt_path) - 1) {
vga_prompt_path[i] = path[i];
i++;
}
vga_prompt_path[i] = '\0';
}
void vga_print_prompt(void) {
/* Line 1: ┌──(root@airlys-os)-[current_path] */
vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
vga_putchar((char)0xDA); /* CP437 box top-left: ┌ */
vga_putchar((char)0xC4); /* CP437 horizontal: ─ */
vga_putchar((char)0xC4); /* CP437 horizontal: ─ */
vga_puts("(");
vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK);
vga_puts("root");
vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
vga_puts("@airlys-os)-[");
vga_set_color(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_BLACK);
vga_puts(vga_prompt_path);
vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
vga_puts("]\n");
/* Line 2: └─Airlys >> */
vga_putchar((char)0xC0); /* CP437 box bottom-left: └ */
vga_putchar((char)0xC4); /* CP437 horizontal: ─ */
vga_puts("Airlys >> ");
vga_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
}
-38
View File
@@ -1,38 +0,0 @@
#include <airlys/vmm.h>
#include <airlys/vga.h>
#include <airlys/kernel.h>
static void page_fault_handler(registers_t* regs) {
uintptr_t faulting_addr = read_cr2();
bool present = (regs->err_code & 0x1) != 0;
bool write = (regs->err_code & 0x2) != 0;
bool user = (regs->err_code & 0x4) != 0;
bool reserved = (regs->err_code & 0x8) != 0;
bool ifetch = (regs->err_code & 0x10) != 0;
vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK);
vga_puts("\n\n [FATAL] PAGE FAULT (#PF) DETECTED!\n");
vga_puts(" ----------------------------------------------------\n");
vga_puts(" Faulting Virtual Address (CR2) : ");
vga_puthex(faulting_addr);
vga_puts("\n Error Code : ");
vga_puthex(regs->err_code);
vga_puts("\n Flags: ");
if (!present) vga_puts("[Not Present] ");
else vga_puts("[Protection Violation] ");
if (write) vga_puts("[Write] ");
else vga_puts("[Read] ");
if (user) vga_puts("[User Ring3] ");
else vga_puts("[Kernel Ring0] ");
if (reserved) vga_puts("[Reserved Bit] ");
if (ifetch) vga_puts("[Instruction Fetch] ");
vga_puts("\n ----------------------------------------------------\n");
kernel_panic("Kernel unhandled page fault access violation");
}
void vmm_init(void) {
/* Register Page Fault handler on CPU Exception Vector 14 */
register_interrupt_handler(14, page_fault_handler);
}