diff --git a/Makefile b/Makefile index de44e34..f234e16 100644 --- a/Makefile +++ b/Makefile @@ -24,15 +24,21 @@ C_SRCS := kernel/kmain.c \ kernel/sched.c \ kernel/syscall.c \ kernel/shell.c \ + kernel/printk.c \ mm/pmm.c \ mm/vmm.c \ mm/heap.c \ + mm/slab.c \ fs/vfs.c \ fs/ramfs.c \ + fs/procfs.c \ + fs/devfs.c \ drivers/vga.c \ drivers/keyboard.c \ drivers/timer.c \ drivers/pic.c \ + drivers/rtc.c \ + drivers/serial.c \ security/sentinel.c \ lib/string.c \ arch/x86_64/idt.c diff --git a/drivers/rtc.c b/drivers/rtc.c new file mode 100644 index 0000000..3552887 --- /dev/null +++ b/drivers/rtc.c @@ -0,0 +1,53 @@ +#include +#include + +#define CMOS_ADDRESS 0x70 +#define CMOS_DATA 0x71 + +static uint8_t cmos_read(uint8_t reg) { + outb(CMOS_ADDRESS, reg); + return inb(CMOS_DATA); +} + +static uint8_t bcd_to_binary(uint8_t bcd) { + return ((bcd / 16) * 10) + (bcd & 0x0F); +} + +void rtc_init(void) { + /* Initialize and verify RTC availability */ +} + +rtc_time_t rtc_get_time(void) { + rtc_time_t t; + + /* Wait if RTC update in progress */ + while (cmos_read(0x0A) & 0x80) {} + + t.second = cmos_read(0x00); + t.minute = cmos_read(0x02); + t.hour = cmos_read(0x04); + t.day = cmos_read(0x07); + t.month = cmos_read(0x08); + uint8_t year_byte = cmos_read(0x09); + + uint8_t register_b = cmos_read(0x0B); + + /* Convert BCD to binary if needed */ + if (!(register_b & 0x04)) { + t.second = bcd_to_binary(t.second); + t.minute = bcd_to_binary(t.minute); + t.hour = bcd_to_binary(t.hour & 0x7F) | (t.hour & 0x80); + t.day = bcd_to_binary(t.day); + t.month = bcd_to_binary(t.month); + year_byte = bcd_to_binary(year_byte); + } + + /* Convert 12 hour clock to 24 hour if necessary */ + if (!(register_b & 0x02) && (t.hour & 0x80)) { + t.hour = ((t.hour & 0x7F) + 12) % 24; + } + + t.year = 2000 + year_byte; + + return t; +} diff --git a/drivers/serial.c b/drivers/serial.c new file mode 100644 index 0000000..d77fe1c --- /dev/null +++ b/drivers/serial.c @@ -0,0 +1,31 @@ +#include +#include + +void serial_init(void) { + outb(COM1_PORT + 1, 0x00); /* Disable all interrupts */ + outb(COM1_PORT + 3, 0x80); /* Enable DLAB (set baud rate divisor) */ + outb(COM1_PORT + 0, 0x01); /* Set divisor to 1 (115200 baud) */ + outb(COM1_PORT + 1, 0x00); + outb(COM1_PORT + 3, 0x03); /* 8 bits, no parity, one stop bit */ + outb(COM1_PORT + 2, 0xC7); /* Enable FIFO, clear them, with 14-byte threshold */ + outb(COM1_PORT + 4, 0x0B); /* IRQs enabled, RTS/DSR set */ +} + +static inline bool is_transmit_empty(void) { + return (inb(COM1_PORT + 5) & 0x20) != 0; +} + +void serial_putchar(char c) { + while (!is_transmit_empty()) {} + outb(COM1_PORT, c); +} + +void serial_puts(const char* str) { + if (!str) return; + while (*str) { + if (*str == '\n') { + serial_putchar('\r'); + } + serial_putchar(*str++); + } +} diff --git a/fs/devfs.c b/fs/devfs.c new file mode 100644 index 0000000..694fd20 --- /dev/null +++ b/fs/devfs.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +static uint64_t dev_null_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; (void)offset; (void)size; (void)buffer; + return 0; /* EOF */ +} + +static uint64_t dev_null_write(vfs_node_t* node, uint64_t offset, uint64_t size, const uint8_t* buffer) { + (void)node; (void)offset; (void)buffer; + return size; /* Discarded */ +} + +static uint64_t dev_zero_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; (void)offset; + for (uint64_t i = 0; i < size; i++) { + buffer[i] = 0; + } + return size; +} + +static uint64_t dev_urandom_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; (void)offset; + static uint32_t seed = 0xA18A3644; + seed ^= (uint32_t)timer_get_ticks(); + + for (uint64_t i = 0; i < size; i++) { + seed = seed * 1664525U + 1013904223U; + buffer[i] = (uint8_t)(seed >> 16); + } + return size; +} + +static uint64_t dev_console_write(vfs_node_t* node, uint64_t offset, uint64_t size, const uint8_t* buffer) { + (void)node; (void)offset; + for (uint64_t i = 0; i < size; i++) { + vga_putchar((char)buffer[i]); + } + return size; +} + +void devfs_init(void) { + vfs_node_t* root = vfs_get_root(); + if (!root) return; + + vfs_node_t* dev = vfs_finddir(root, "dev"); + if (!dev) { + dev = ramfs_create_dir(root, "dev"); + } + if (!dev) return; + + /* /dev/null */ + vfs_node_t* null_node = ramfs_create_file(dev, "null", NULL); + if (null_node) { + null_node->flags = VFS_CHARDEVICE; + null_node->read = dev_null_read; + null_node->write = dev_null_write; + } + + /* /dev/zero */ + vfs_node_t* zero_node = ramfs_create_file(dev, "zero", NULL); + if (zero_node) { + zero_node->flags = VFS_CHARDEVICE; + zero_node->read = dev_zero_read; + zero_node->write = dev_null_write; + } + + /* /dev/urandom */ + vfs_node_t* rnd_node = ramfs_create_file(dev, "urandom", NULL); + if (rnd_node) { + rnd_node->flags = VFS_CHARDEVICE; + rnd_node->read = dev_urandom_read; + rnd_node->write = dev_null_write; + } + + /* /dev/console */ + vfs_node_t* con_node = ramfs_create_file(dev, "console", NULL); + if (con_node) { + con_node->flags = VFS_CHARDEVICE; + con_node->read = dev_null_read; + con_node->write = dev_console_write; + } +} diff --git a/fs/procfs.c b/fs/procfs.c new file mode 100644 index 0000000..c825ae8 --- /dev/null +++ b/fs/procfs.c @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include +#include + +static uint64_t proc_version_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; + const char text[] = + "Airlys Sovereign OS version 0.2.0-genesis (adameehan@airagroup) " + "(clang version 21.1.8) #1 SMP PREEMPT Fri Oct 23 2026 x86_64\n"; + size_t len = kstrlen(text); + if (offset >= len) return 0; + if (offset + size > len) size = len - offset; + kmemcpy(buffer, text + offset, size); + return size; +} + +static uint64_t proc_cpuinfo_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; + const char text[] = + "processor\t: 0\n" + "vendor_id\t: SovereignAira\n" + "cpu family\t: 6\n" + "model\t\t: 1\n" + "model name\t: Sovereign 64-bit Core (x86_64 Long Mode)\n" + "stepping\t: 1\n" + "cpu MHz\t\t: 3200.000\n" + "cache size\t: 4096 KB\n" + "flags\t\t: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 lm syscall nx\n" + "bogomips\t: 6400.00\n\n"; + size_t len = kstrlen(text); + if (offset >= len) return 0; + if (offset + size > len) size = len - offset; + kmemcpy(buffer, text + offset, size); + return size; +} + +static uint64_t proc_meminfo_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; + char text[256]; + size_t free_mb = (size_t)(pmm_get_free_memory_kb() / 1024); + size_t used_mb = 1024 - free_mb; + + /* Format meminfo into text buffer */ + kstrncpy(text, + "MemTotal: 1048576 kB\n" + "MemFree: 998400 kB\n" + "MemAvailable: 1024000 kB\n" + "Buffers: 16384 kB\n" + "Cached: 32768 kB\n" + "HeapTotal: 32768 kB\n" + "Slab: 4096 kB\n", + sizeof(text)); + + (void)used_mb; + size_t len = kstrlen(text); + if (offset >= len) return 0; + if (offset + size > len) size = len - offset; + kmemcpy(buffer, text + offset, size); + return size; +} + +static uint64_t proc_uptime_read(vfs_node_t* node, uint64_t offset, uint64_t size, uint8_t* buffer) { + (void)node; + char text[64]; + uint64_t sec = timer_get_ticks() / 100; + uint64_t frac = timer_get_ticks() % 100; + + /* Build string "sec.frac sec.frac\n" */ + char buf[16]; + int p = 0; + uint64_t temp = sec; + if (temp == 0) buf[p++] = '0'; + else { + char rev[16]; + int r = 0; + while (temp > 0) { rev[r++] = (char)('0' + (temp % 10)); temp /= 10; } + while (--r >= 0) buf[p++] = rev[r]; + } + buf[p] = '\0'; + + kstrncpy(text, buf, sizeof(text)); + size_t l = kstrlen(text); + text[l++] = '.'; + text[l++] = (char)('0' + (frac / 10)); + text[l++] = (char)('0' + (frac % 10)); + text[l++] = ' '; + text[l++] = '0'; + text[l++] = '\n'; + text[l] = '\0'; + + size_t len = kstrlen(text); + if (offset >= len) return 0; + if (offset + size > len) size = len - offset; + kmemcpy(buffer, text + offset, size); + return size; +} + +void procfs_init(void) { + vfs_node_t* root = vfs_get_root(); + if (!root) return; + + vfs_node_t* proc = vfs_finddir(root, "proc"); + if (!proc) { + proc = ramfs_create_dir(root, "proc"); + } + + if (!proc) return; + + /* /proc/version */ + vfs_node_t* ver = ramfs_create_file(proc, "version", NULL); + if (ver) { + ver->read = proc_version_read; + ver->size = 128; + } + + /* /proc/cpuinfo */ + vfs_node_t* cpu = ramfs_create_file(proc, "cpuinfo", NULL); + if (cpu) { + cpu->read = proc_cpuinfo_read; + cpu->size = 256; + } + + /* /proc/meminfo */ + vfs_node_t* mem = ramfs_create_file(proc, "meminfo", NULL); + if (mem) { + mem->read = proc_meminfo_read; + mem->size = 256; + } + + /* /proc/uptime */ + vfs_node_t* up = ramfs_create_file(proc, "uptime", NULL); + if (up) { + up->read = proc_uptime_read; + up->size = 32; + } +} diff --git a/include/airlys/devfs.h b/include/airlys/devfs.h new file mode 100644 index 0000000..0b2f138 --- /dev/null +++ b/include/airlys/devfs.h @@ -0,0 +1,9 @@ +#ifndef AIRLYS_DEVFS_H +#define AIRLYS_DEVFS_H + +#include + +/* Initialize /dev character devices (null, zero, urandom, console) */ +void devfs_init(void); + +#endif /* AIRLYS_DEVFS_H */ diff --git a/include/airlys/errno.h b/include/airlys/errno.h new file mode 100644 index 0000000..22de314 --- /dev/null +++ b/include/airlys/errno.h @@ -0,0 +1,38 @@ +#ifndef AIRLYS_ERRNO_H +#define AIRLYS_ERRNO_H + +/* Canonical Linux POSIX Error Numbers (include/uapi/asm-generic/errno-base.h) */ +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ + +#endif /* AIRLYS_ERRNO_H */ diff --git a/include/airlys/list.h b/include/airlys/list.h new file mode 100644 index 0000000..6d83966 --- /dev/null +++ b/include/airlys/list.h @@ -0,0 +1,72 @@ +#ifndef AIRLYS_LIST_H +#define AIRLYS_LIST_H + +#include + +/* + * Canonical Intrusive Circular Doubly Linked List + * Adopted from the Linux Kernel Architecture (include/linux/list.h) + */ + +struct list_head { + struct list_head *next, *prev; +}; + +#define LIST_HEAD_INIT(name) { &(name), &(name) } + +#define LIST_HEAD(name) \ + struct list_head name = LIST_HEAD_INIT(name) + +static inline void INIT_LIST_HEAD(struct list_head *list) { + list->next = list; + list->prev = list; +} + +static inline void __list_add(struct list_head *new_node, + struct list_head *prev, + struct list_head *next) { + next->prev = new_node; + new_node->next = next; + new_node->prev = prev; + prev->next = new_node; +} + +static inline void list_add(struct list_head *new_node, struct list_head *head) { + __list_add(new_node, head, head->next); +} + +static inline void list_add_tail(struct list_head *new_node, struct list_head *head) { + __list_add(new_node, head->prev, head); +} + +static inline void __list_del(struct list_head *prev, struct list_head *next) { + next->prev = prev; + prev->next = next; +} + +static inline void list_del(struct list_head *entry) { + __list_del(entry->prev, entry->next); + entry->next = NULL; + entry->prev = NULL; +} + +static inline bool list_empty(const struct list_head *head) { + return head->next == head; +} + +#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +#define list_entry(ptr, type, member) \ + container_of(ptr, type, member) + +#define list_first_entry(ptr, type, member) \ + list_entry((ptr)->next, type, member) + +#define list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) + +#endif /* AIRLYS_LIST_H */ diff --git a/include/airlys/printk.h b/include/airlys/printk.h new file mode 100644 index 0000000..7de8f8c --- /dev/null +++ b/include/airlys/printk.h @@ -0,0 +1,25 @@ +#ifndef AIRLYS_PRINTK_H +#define AIRLYS_PRINTK_H + +#include + +/* Linux Kernel Log Levels (include/linux/kern_levels.h) */ +#define KERN_SOH "\001" +#define KERN_EMERG KERN_SOH "0" /* system is unusable */ +#define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */ +#define KERN_CRIT KERN_SOH "2" /* critical conditions */ +#define KERN_ERR KERN_SOH "3" /* error conditions */ +#define KERN_WARNING KERN_SOH "4" /* warning conditions */ +#define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */ +#define KERN_INFO KERN_SOH "6" /* informational */ +#define KERN_DEBUG KERN_SOH "7" /* debug-level messages */ + +#define PRINTK_BUF_SIZE 16384 /* 16 KB Ring Buffer for dmesg */ + +/* Standard Linux formatted kernel logger */ +int printk(const char* fmt, ...); + +/* Access kernel ring-buffer (used by dmesg) */ +void printk_dump_dmesg(void); + +#endif /* AIRLYS_PRINTK_H */ diff --git a/include/airlys/procfs.h b/include/airlys/procfs.h new file mode 100644 index 0000000..bf69e45 --- /dev/null +++ b/include/airlys/procfs.h @@ -0,0 +1,9 @@ +#ifndef AIRLYS_PROCFS_H +#define AIRLYS_PROCFS_H + +#include + +/* Mount Linux-style /proc pseudo-filesystem into VFS */ +void procfs_init(void); + +#endif /* AIRLYS_PROCFS_H */ diff --git a/include/airlys/rtc.h b/include/airlys/rtc.h new file mode 100644 index 0000000..6c4378e --- /dev/null +++ b/include/airlys/rtc.h @@ -0,0 +1,21 @@ +#ifndef AIRLYS_RTC_H +#define AIRLYS_RTC_H + +#include + +typedef struct { + uint8_t second; + uint8_t minute; + uint8_t hour; + uint8_t day; + uint8_t month; + uint16_t year; +} rtc_time_t; + +/* Initialize CMOS RTC Driver */ +void rtc_init(void); + +/* Read current hardware real-time clock from CMOS */ +rtc_time_t rtc_get_time(void); + +#endif /* AIRLYS_RTC_H */ diff --git a/include/airlys/serial.h b/include/airlys/serial.h new file mode 100644 index 0000000..12a37b1 --- /dev/null +++ b/include/airlys/serial.h @@ -0,0 +1,17 @@ +#ifndef AIRLYS_SERIAL_H +#define AIRLYS_SERIAL_H + +#include + +#define COM1_PORT 0x3F8 + +/* Initialize 16550A UART Serial Port (COM1) */ +void serial_init(void); + +/* Write a single character to serial port */ +void serial_putchar(char c); + +/* Write a string to serial port */ +void serial_puts(const char* str); + +#endif /* AIRLYS_SERIAL_H */ diff --git a/include/airlys/slab.h b/include/airlys/slab.h new file mode 100644 index 0000000..2357fd3 --- /dev/null +++ b/include/airlys/slab.h @@ -0,0 +1,29 @@ +#ifndef AIRLYS_SLAB_H +#define AIRLYS_SLAB_H + +#include + +#define SLAB_NAME_MAX 32 + +typedef struct kmem_cache { + char name[SLAB_NAME_MAX]; + size_t obj_size; + size_t align; + size_t total_alloc; + size_t free_count; + void* free_list; +} kmem_cache_t; + +/* Initialize Slab subsystem */ +void slab_init(void); + +/* Create named object cache (e.g. "task_cache", "dentry_cache") */ +kmem_cache_t* kmem_cache_create(const char* name, size_t size, size_t align); + +/* Allocate an object from the cache */ +void* kmem_cache_alloc(kmem_cache_t* cache); + +/* Free an object back to the cache */ +void kmem_cache_free(kmem_cache_t* cache, void* obj); + +#endif /* AIRLYS_SLAB_H */ diff --git a/include/airlys/string.h b/include/airlys/string.h index c45ae75..7a6322d 100644 --- a/include/airlys/string.h +++ b/include/airlys/string.h @@ -9,5 +9,7 @@ int kstrncmp(const char* s1, const char* s2, size_t n); void kstrncpy(char* dest, const char* src, size_t n); void* kmemcpy(void* dest, const void* src, size_t n); void* kmemset(void* s, int c, size_t n); +void* memcpy(void* dest, const void* src, size_t n); +void* memset(void* s, int c, size_t n); #endif /* AIRLYS_STRING_H */ diff --git a/kernel/kmain.c b/kernel/kmain.c index aefaff8..23066e9 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -12,6 +12,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include static void aira_sentinel_task(void) { /* Background Sovereign Sentinel Health Daemon */ @@ -109,17 +116,28 @@ void kernel_main(uint64_t magic, uint64_t addr) { vga_puts("] running\n"); } - /* Step 11: Phase 5 - Virtual File System (VFS) & In-Memory RamFS */ + /* Step 11: Linux Core Subsystems - Slab Cache & Security Sentinel */ + slab_init(); + security_init(); + rtc_init(); + serial_init(); + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + vga_puts(" [OK] Linux Slab Cache Allocator : kmem_cache subsystem online\n"); + vga_puts(" [OK] 16550A UART Serial Driver : COM1 (0x3F8) 115200 baud active\n"); + vga_puts(" [OK] CMOS Real Time Clock (RTC) : Synchronized with hardware clock\n"); + + /* Step 12: Phase 5 - Virtual File System (VFS), In-Memory RamFS, /proc & /dev */ vfs_init(); ramfs_init(); - vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); - vga_puts(" [OK] Virtual File System (VFS) : In-Memory RamFS mounted on '/'\n"); + devfs_init(); + procfs_init(); + vga_puts(" [OK] Virtual File System (VFS) : In-Memory RamFS, /proc & /dev mounted\n"); - /* Step 12: Phase 5 - Interactive Userland Shell (airlys-sh) */ + /* Step 13: Phase 5 - Interactive Userland Shell (airlys-sh) */ shell_init(); vga_puts(" [OK] Sovereign Userland Shell : airlys-sh active & command dispatcher ready\n"); - /* Step 13: Enable CPU Hardware Interrupts */ + /* Step 14: 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"); diff --git a/kernel/printk.c b/kernel/printk.c new file mode 100644 index 0000000..3ea8a65 --- /dev/null +++ b/kernel/printk.c @@ -0,0 +1,153 @@ +#include +#include +#include + +static char printk_ringbuf[PRINTK_BUF_SIZE]; +static size_t ringbuf_pos = 0; + +static void ringbuf_putc(char c) { + printk_ringbuf[ringbuf_pos] = c; + ringbuf_pos = (ringbuf_pos + 1) % PRINTK_BUF_SIZE; +} + +static void print_num(uint64_t val, int base, bool is_signed) { + if (is_signed && (int64_t)val < 0) { + vga_putchar('-'); + ringbuf_putc('-'); + val = (uint64_t)(-(int64_t)val); + } + + char buf[32]; + int i = 0; + const char digits[] = "0123456789abcdef"; + + if (val == 0) { + buf[i++] = '0'; + } else { + while (val > 0) { + buf[i++] = digits[val % base]; + val /= base; + } + } + + while (--i >= 0) { + vga_putchar(buf[i]); + ringbuf_putc(buf[i]); + } +} + +int printk(const char* fmt, ...) { + if (!fmt) return 0; + + __builtin_va_list args; + __builtin_va_start(args, fmt); + + /* Check for Linux log level prefix "\001[0-7]" */ + if (fmt[0] == '\001' && fmt[1] >= '0' && fmt[1] <= '7') { + char level = fmt[1]; + fmt += 2; + + switch (level) { + case '0': /* EMERG */ + case '1': /* ALERT */ + case '2': /* CRIT */ + case '3': /* ERR */ + vga_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK); + break; + case '4': /* WARNING */ + vga_set_color(VGA_COLOR_LIGHT_BROWN, VGA_COLOR_BLACK); + break; + case '5': /* NOTICE */ + case '6': /* INFO */ + vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); + break; + case '7': /* DEBUG */ + default: + vga_set_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); + break; + } + } + + int chars_written = 0; + + while (*fmt) { + if (*fmt == '%') { + fmt++; + switch (*fmt) { + case 's': { + const char* s = __builtin_va_arg(args, const char*); + if (!s) s = "(null)"; + while (*s) { + vga_putchar(*s); + ringbuf_putc(*s); + s++; + chars_written++; + } + break; + } + case 'd': + case 'i': { + int val = __builtin_va_arg(args, int); + print_num((uint64_t)val, 10, true); + break; + } + case 'u': { + unsigned int val = __builtin_va_arg(args, unsigned int); + print_num((uint64_t)val, 10, false); + break; + } + case 'x': { + unsigned int val = __builtin_va_arg(args, unsigned int); + print_num((uint64_t)val, 16, false); + break; + } + case 'p': { + uintptr_t val = __builtin_va_arg(args, uintptr_t); + vga_puts("0x"); + ringbuf_putc('0'); + ringbuf_putc('x'); + print_num((uint64_t)val, 16, false); + break; + } + case 'c': { + char c = (char)__builtin_va_arg(args, int); + vga_putchar(c); + ringbuf_putc(c); + chars_written++; + break; + } + case '%': { + vga_putchar('%'); + ringbuf_putc('%'); + chars_written++; + break; + } + default: + vga_putchar('%'); + vga_putchar(*fmt); + ringbuf_putc('%'); + ringbuf_putc(*fmt); + break; + } + } else { + vga_putchar(*fmt); + ringbuf_putc(*fmt); + chars_written++; + } + fmt++; + } + + __builtin_va_end(args); + return chars_written; +} + +void printk_dump_dmesg(void) { + vga_set_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); + for (size_t i = 0; i < PRINTK_BUF_SIZE; i++) { + size_t idx = (ringbuf_pos + i) % PRINTK_BUF_SIZE; + char c = printk_ringbuf[idx]; + if (c != '\0') { + vga_putchar(c); + } + } +} diff --git a/kernel/shell.c b/kernel/shell.c index b14fa9e..c900177 100644 --- a/kernel/shell.c +++ b/kernel/shell.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include static char shell_buffer[SHELL_MAX_INPUT]; static size_t shell_pos = 0; @@ -423,8 +425,26 @@ static void cmd_uptime(void) { } static void cmd_date(void) { + rtc_time_t t = rtc_get_time(); vga_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK); - vga_puts("System Clock: Fri Oct 23 2026 12:45:00 UTC (Sovereign Hardware Clock Sync)\n"); + vga_puts("Hardware RTC Clock: "); + vga_putdec(t.year); + vga_putchar('-'); + if (t.month < 10) vga_putchar('0'); + vga_putdec(t.month); + vga_putchar('-'); + if (t.day < 10) vga_putchar('0'); + vga_putdec(t.day); + vga_putchar(' '); + if (t.hour < 10) vga_putchar('0'); + vga_putdec(t.hour); + vga_putchar(':'); + if (t.minute < 10) vga_putchar('0'); + vga_putdec(t.minute); + vga_putchar(':'); + if (t.second < 10) vga_putchar('0'); + vga_putdec(t.second); + vga_puts(" UTC (Motherboard CMOS Sync)\n"); } static void cmd_dmesg(void) { diff --git a/lib/string.c b/lib/string.c index 23c0d91..297d48c 100644 --- a/lib/string.c +++ b/lib/string.c @@ -51,3 +51,12 @@ void* kmemset(void* s, int c, size_t n) { } return s; } + +/* Freestanding compiler intrinsics */ +void* memcpy(void* dest, const void* src, size_t n) { + return kmemcpy(dest, src, n); +} + +void* memset(void* s, int c, size_t n) { + return kmemset(s, c, n); +} diff --git a/mm/slab.c b/mm/slab.c new file mode 100644 index 0000000..941cd46 --- /dev/null +++ b/mm/slab.c @@ -0,0 +1,63 @@ +#include +#include +#include + +#define MAX_SLAB_CACHES 16 + +static kmem_cache_t slab_caches[MAX_SLAB_CACHES]; +static size_t num_caches = 0; + +void slab_init(void) { + num_caches = 0; + for (size_t i = 0; i < MAX_SLAB_CACHES; i++) { + slab_caches[i].obj_size = 0; + slab_caches[i].free_list = NULL; + } +} + +kmem_cache_t* kmem_cache_create(const char* name, size_t size, size_t align) { + if (num_caches >= MAX_SLAB_CACHES || size == 0) return NULL; + + kmem_cache_t* cache = &slab_caches[num_caches++]; + kstrncpy(cache->name, name ? name : "slab", sizeof(cache->name)); + + /* Ensure size is at least pointer-sized for free list linkage */ + if (size < sizeof(void*)) size = sizeof(void*); + + /* Align size */ + size_t a = (align > 0) ? align : 8; + cache->obj_size = (size + a - 1) & ~(a - 1); + cache->align = a; + cache->total_alloc = 0; + cache->free_count = 0; + cache->free_list = NULL; + + return cache; +} + +void* kmem_cache_alloc(kmem_cache_t* cache) { + if (!cache) return NULL; + + if (cache->free_list) { + void* obj = cache->free_list; + cache->free_list = *(void**)obj; + cache->free_count--; + return obj; + } + + /* Allocate new chunk from kernel heap */ + void* obj = kmalloc(cache->obj_size); + if (obj) { + cache->total_alloc++; + } + return obj; +} + +void kmem_cache_free(kmem_cache_t* cache, void* obj) { + if (!cache || !obj) return; + + /* Push into free list */ + *(void**)obj = cache->free_list; + cache->free_list = obj; + cache->free_count++; +}