# xOS
**A multi-architecture operating system built from scratch targeting x86, ARM, and RISC-V**

![System Overview][system_overview]

---

## What This Is

An operating system implemented entirely from scratch (no existing kernel), spanning kernel development, driver architecture, filesystems, userland applications, and UI frameworks.

### Technical Scope

- **Multi-architecture kernel**: x86/x86-64, ARMv7, Arm64, RISC-V support with unified codebase
- **Custom bootloaders** for each architecture with ELF validation and secure boot integration
- **Full userspace stack**: C/C++ standard libraries, Objective-C runtime, UI framework, window server
- **Production OS features**: Ext2 filesystem, virtual filesystem layer, process management, POSIX signals, TTY subsystem

---

## Core Technical Implementation

### Kernel Architecture

**Preemptive Multitasking & Scheduling**
- Priority-based round-robin scheduler with 12 priority levels
- Per-CPU runqueues for SMP support (multi-core)
- Conditional kernel preemption (`PREEMPT_KERNEL` flag for real-time responsiveness)
- Interrupt depth counter preventing race conditions during context switches
- Idle threads for zero-allocation interrupt handling

**Memory Management**
- Virtual memory manager with per-process address spaces
- Demand paging and swap support
- Kernel/userspace memory isolation with access validation
- Shared buffers for zero-copy IPC

**System Call Interface**
- 60+ system calls following POSIX conventions (`fork`, `exec`, `read`, `write`, `mmap`, etc.)
- Architecture-specific syscall dispatch (software interrupts on x86, `svc` on ARM)
- Kernel-mode syscalls (`ksys1`-`ksys4`) for internal operations

### Boot Process & Security

**Custom Bootloader Chain**
- **Stage 1** (x86): Real-mode to protected-mode transition, memory map collection
- **Stage 2/Prekernel**:  ELF loader, device tree initialization, MMU setup
- **Kernel validation**: SHA256 + RSA signature verification before execution
- Passes boot arguments (`boot_args_t`) with memory layout, framebuffer, device tree

### Filesystem & Device Management

- **Ext2 filesystem** with full read/write support
- **Virtual filesystem (VFS)** layer abstracting file operations
- `/dev` (device nodes) and `/proc` (process/system introspection) filesystems
- Device tree parsing for ARM/RISC-V hardware discovery
- Device driver framework with pluggable driver registration

### Userland & UI

**Window Server** (compositing manager)
- Client-server architecture using IPC
- Desktop and mobile window management modes
- Hardware-accelerated rendering via LibG
- Menu bar, dock, popup system

**Libraries**
- **LibC/LibCxx**:  POSIX-compliant C library and C++ STL
- **LibObjC**: Objective-C runtime (message dispatch, ARC)
- **LibFoundation**: Event loop, shared buffers, collections (Cocoa-inspired)
- **LibG**: 2D graphics (pixel buffers, fonts, PNG/BMP loaders)
- **LibUI**: High-level UI widgets (buttons, text fields, windows)

---

## Key Engineering Decisions

**1. Multi-Architecture Support**  
Abstracted platform-specific code (`kernel/platform/{x86,arm32,arm64,riscv}`) with common interfaces for MMU, interrupts, context switching.  Enables same kernel codebase across all targets.

**2. Build System**  
GN (Generate Ninja) meta-build system for fast incremental builds. Compiles for multiple architectures from single source tree.

**3. Bootloader Security**  
Validates kernel ELF binary cryptographically before loading—prevents tampered kernel execution (verified boot).

**4. Preemptive vs Cooperative**  
Kernel preemption is compile-time optional (`-DPREEMPT_KERNEL`). Enables low-latency scheduling when enabled, or simpler non-preemptive model for easier debugging.

**5. Window Server Architecture**  
Separates compositing from applications—crash in user app doesn't take down entire UI. Similar to X11/Wayland model.

---

## Features

### Kernel

* x86/x86-64, ARMv7 and Arm64 kernel with pre-emptive multi-threading
* Ext2 filesystem
* /dev and /proc filesystems
* Local sockets
* POSIX signals
* TTY
* [learn more](https://code.ayaantunio.me/ayaan/Custom-Operating-System/src/branch/master/docs/kernel.md)

### Libraries

* Runtime & support: LibC, LibCxx, LibObjC
* Rich functionality: LibFoundation
* UI functionality & rendering: LibG, LibUI
* [learn more](https://code.ayaantunio.me/ayaan/Custom-Operating-System/src/branch/master/docs/libs.md)

### Userland

* Composing windows manager for desktop and mobile
* Simple UI apps
* Terminal

### Boot

* Custom bootloaders
* Kernel validation during the boot proccess
* Custom device tree
* [learn more](https://code.ayaantunio.me/ayaan/Custom-Operating-System/src/branch/master/docs/boot.md)

## How to build and run xOS?

See the [build instructions](https://code.ayaantunio.me/ayaan/Custom-Operating-System/src/branch/master/docs/build.md)

[system_overview]: /Images/sysarch.svg "System Architecture Diagram"