Architecture - cupidthecat/cupid-os GitHub Wiki
Architecture
cupid-os is a monolithic, single-address-space, ring-0 operating system for 32-bit x86. Every component - kernel, drivers, shell, applications - runs in the same flat memory space with full hardware access.
Boot Sequence
BIOS loads boot.asm at 0x7C00 (real mode, 16-bit)
β
βββ Set up stack at 0x7C00
βββ Load kernel from disk (sectors 1+) to 0x10000
βββ Set 640Γ480Γ32bpp via Bochs VBE I/O ports (0x01CE/0x01CF)
βββ Read VBE LFB address from PCI BAR0 β store at 0x0500
βββ Set up GDT (flat model: code + data segments)
βββ Switch to protected mode (CR0 bit 0)
βββ Far jump to kernel entry at 0x10000
β
βββ idt_init() - Interrupt Descriptor Table
βββ pic_init() - PIC remapping (IRQ0β32, IRQ8β40)
βββ irq_init() - IRQ handler registration
βββ pmm_init() - Physical memory manager
βββ paging_init() - Identity-mapped 4KB pages (32MB)
β + maps VBE LFB region from 0x0500
βββ heap_init() - Kernel heap (2MB) with canaries
βββ keyboard_init() - PS/2 keyboard (IRQ1)
βββ pit_init() - PIT at 100Hz (IRQ0)
βββ serial_init() - COM1 at 115200 baud
βββ rtc_init() - CMOS Real-Time Clock
βββ fat16_init() - FAT16 filesystem + block cache
βββ fs_init() - In-memory filesystem
βββ vfs_init() - Virtual File System
β βββ Register ramfs, devfs, fat16 filesystem types
β βββ Mount ramfs at /
β βββ Create /bin, /tmp, /home directories
β βββ Mount devfs at /dev (null, zero, random, serial)
β βββ Mount fat16 at /home (ATA disk)
β βββ Pre-populate /LICENSE.txt, /MOTD.txt
βββ process_init() - Process table, idle process (PID 1)
βββ Register desktop as PID 2
βββ vga_init_vbe() - Init 640Γ480 32bpp framebuffer
βββ mouse_init() - PS/2 mouse (IRQ12)
βββ desktop_run() - Main event loop
Memory Layout
0x00000000 ββββββββββββββββββββββββββββ
β Interrupt Vector Table β (not used - we use IDT)
0x00000500 ββββββββββββββββββββββββββββ€
β VBE LFB address β β Written by bootloader
0x00001000 ββββββββββββββββββββββββββββ€
β Kernel Code + Data β β Loaded by bootloader
β (.text, .data, .bss) β β starts at 0x10000
ββββββββββββββββββββββββββββ€
β Boot Stack β β 0x7C00 (real mode)
0x00110000 ββββββββββββββββββββββββββββ€
β Stack Guard Region β β 512KB kernel stack
0x00190000 ββββββββββββββββββββββββββββ€
β Kernel Heap β β kmalloc/kfree, 2MB
β (canary-protected) β β includes VBE back buffer
ββββββββββββββββββββββββββββ€
β Process Stacks β β kmalloc'd, 4KB+ each
β (canary at bottom) β
0x000A0000 ββββββββββββββββββββββββββββ€
β VGA Text Memory β β Text mode (legacy)
0x00100000 ββββββββββββββββββββββββββββ€
β Extended Memory β β PMM bitmap manages this
β (pages allocated here) β
0x02000000 ββββββββββββββββββββββββββββ€
β End of managed memory β (32MB total)
ββββββββββββββββββββββββββββ
Β·
Β· (unmapped gap)
Β·
0xFD000000 ββββββββββββββββββββββββββββ
β VBE Linear Framebuffer β β 640Γ480Γ4 = 1.2MB
β (identity-mapped by β PCI BAR0, QEMU default
β paging_init) β
0xFD140000 ββββββββββββββββββββββββββββ
Component Architecture
Kernel Core
| Component |
Files |
Purpose |
| Kernel entry |
kernel.c/h |
VGA, init sequence, main print functions |
| IDT |
idt.c/h |
Interrupt descriptor table setup |
| ISR/IRQ |
isr.asm, irq.c/h |
Interrupt/exception dispatching |
| PIC |
pic.c/h |
Programmable interrupt controller |
| Memory |
memory.c/h, paging.c |
PMM, heap, paging, canaries, leak detection |
| VFS |
vfs.c/h |
Virtual filesystem, mount table, path resolution |
| Panic |
panic.c/h, assert.h |
Crash handler, assertions |
| Strings |
string.c/h |
strlen, strcmp, memcpy, memset |
| Math |
math.c/h |
64-bit division, itoa, hex printing |
Drivers
| Driver |
Files |
IRQ |
Purpose |
| Keyboard |
keyboard.c/h |
IRQ1 |
PS/2 input with modifiers |
| Mouse |
mouse.c/h |
IRQ12 |
PS/2 mouse with cursor |
| Timer |
timer.c/h, pit.c/h |
IRQ0 |
100Hz PIT, uptime, sleep |
| VGA |
vga.c/h |
- |
VBE 640Γ480 32bpp, double buffering |
| ATA |
ata.c/h |
- |
PIO disk read/write |
| Serial |
serial.c/h |
- |
COM1 logging |
| Speaker |
speaker.c/h |
- |
PC speaker tones |
| RTC |
rtc.c/h |
- |
CMOS real-time clock |
Subsystems
| Subsystem |
Files |
Purpose |
| Shell |
shell.c/h |
38-command interactive shell with CWD support |
| CupidScript |
cupidscript*.c/h |
Bash-like scripting language |
| Ed Editor |
ed.c/h |
Unix ed(1) line editor |
| VFS |
vfs.c/h |
Virtual File System with mount table and path resolution |
| RamFS |
ramfs.c/h |
In-memory filesystem (root, /bin, /tmp) |
| DevFS |
devfs.c/h |
Device filesystem (/dev/null, zero, random, serial) |
| FAT16 VFS |
fat16_vfs.c/h |
FAT16 VFS wrapper for /home |
| FAT16 |
fat16.c/h, blockdev.c/h, blockcache.c/h |
FAT16 driver with block cache |
| In-Memory FS |
fs.c/h |
Legacy read-only system file table |
| Exec |
exec.c/h |
CUPD program loader |
| Process Mgr |
process.c/h, context_switch.asm |
Scheduler, context switching |
| GUI |
gui.c/h, desktop.c/h, graphics.c/h, font_8x8.c/h |
Window manager, desktop |
| Terminal |
terminal_app.c/h |
GUI terminal application |
| Notepad |
notepad.c/h |
Text editor application (VFS file dialog) |
| Clipboard |
clipboard.c/h |
System clipboard |
| Calendar |
calendar.c/h |
Calendar math, time/date formatting, popup state |
Interrupt Map
| IRQ |
Vector |
Handler |
Purpose |
| IRQ0 |
32 |
timer_callback |
PIT timer tick (100Hz), scheduler flag |
| IRQ1 |
33 |
keyboard_handler |
PS/2 keyboard input |
| IRQ12 |
44 |
mouse_handler |
PS/2 mouse input |
| - |
0 |
division_error |
Divide by zero exception |
| - |
6 |
invalid_opcode |
Invalid opcode exception |
| - |
13 |
general_protection |
GPF |
| - |
14 |
page_fault |
Page fault (with CR2 reporting) |
Execution Model
cupid-os uses deferred preemptive multitasking:
- PIT IRQ0 fires every 10ms β sets
need_reschedule flag
- Flag is checked at safe voluntary points only:
- Desktop main loop (before
HLT)
process_yield() calls
- Idle process loop
- Context switch happens via pure assembly
context_switch():
- Save EBP, EDI, ESI, EBX, EFLAGS on current stack
- Store ESP into old process PCB
- Load new process ESP and jump to new EIP
This avoids the complexity and stack corruption risks of switching inside interrupt handlers.
CupidScript Execution Pipeline
.cup file on disk
β
βΌ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β Lexer βββββΆβ Parser βββββΆβ Interpreter β
β (tokenize) β β (build AST) β β (execute AST)β
ββββββββββββββββ ββββββββββββββββ ββββββββ¬ββββββββ
β
ββββββββββββββββββββββββ€
βΌ βΌ
ββββββββββββββββ ββββββββββββββββ
β Runtime β β Shell β
β (variables, β β (execute_ β
β functions) β β command()) β
ββββββββββββββββ ββββββββββββββββ
See Also