A minimal x86 operating system written in C and Assembly.
Oslab is a minimal operating system for x86 architecture, developed using C and Assembly languages. The project aims to provide a simple and efficient operating system that can be used for educational purposes and experimentation in low-level system programming.
This OS implements core operating system functionalities including:
- A custom bootloader that loads the kernel into memory
- Protected mode operation with GDT implementation
- Interrupt handling system with IDT
- Basic device drivers for screen, keyboard, and disk
- Memory management utilities
- Real-time clock functionality
graph TD
A[BIOS] --> B[Bootloader]
B --> C[Kernel Initialization]
C --> D[Interrupt System]
C --> E[Device Drivers]
C --> F[Memory Management]
D --> G[Exception Handlers]
D --> H[IRQ Handlers]
E --> I[Screen Driver]
E --> J[Keyboard Driver]
E --> K[ATA Driver]
E --> L[Timer]
- Boot Process: Custom bootloader that transitions from 16-bit real mode to 32-bit protected mode
- Kernel: Main kernel written in C with Assembly entry point
- Interrupt Handling: Complete interrupt descriptor table (IDT) implementation with handlers for CPU exceptions and hardware interrupts
- Memory Management: Basic memory allocation and string manipulation utilities
- Device Drivers:
- VGA text mode screen driver
- PS/2 keyboard driver with scancode translation
- ATA PIO mode disk driver
- Programmable Interval Timer (PIT) driver
- Real-time clock (RTC) implementation
- User Interface: Simple command-line interface for interacting with the system
.
├── boot/ # Bootloader assembly code
├── cpu/ # CPU-specific code (interrupts, ports, timer)
├── drivers/ # Device drivers (screen, keyboard, ATA, sound)
├── kernel/ # Main kernel implementation
├── libc/ # Basic C library functions (memory, string operations)
├── Makefile # Build configuration
├── linker.ld # Linker script
└── README.md # Project documentation
The bootloader is responsible for loading the kernel into memory and transitioning the CPU from 16-bit real mode to 32-bit protected mode. It includes:
bootsect.asm
: Main bootloader code that loads the kernel from diskgdt.asm
: Global Descriptor Table implementation for protected modeswitch_pm.asm
: Code to switch to protected modedisk.asm
: Disk reading functions using BIOS interruptsprint.asm
: Text printing functions for real mode32bit_print.asm
: Text printing functions for protected modeprint_hex.asm
: Hexadecimal number printing utility
The main kernel implementation that runs in 32-bit protected mode:
kernel.c
: Entry point and main kernel functionskernel.h
: Kernel header file
CPU-specific functionality for low-level system operations:
idt.c/h
: Interrupt Descriptor Table implementationisr.c/h
: Interrupt Service Routinesinterrupt.asm
: Assembly interrupt handlersports.c/h
: Hardware port I/O functionstimer.c/h
: Timer and real-time clock implementationtype.h
: Custom type definitions
Hardware device drivers for system peripherals:
screen.c/h
: VGA text mode display driverkeyboard.c/h
: PS/2 keyboard driverata.c/h
: ATA disk driver for reading/writing sectorssound.c/h
: PC speaker sound driverpci.c
: PCI bus detection (incomplete)
Basic C library functions for kernel use:
mem.c/h
: Memory manipulation functionsstring.c/h
: String manipulation functionsfunction.h
: Utility macros
- NASM (Netwide Assembler)
- GCC cross-compiler for i386 (i386-elf-gcc)
- GNU Make
- QEMU (for testing)
make
This will generate:
bootsect.bin
: The compiled bootloaderkernel.bin
: The compiled kernelos-image.bin
: The complete OS image ready to run
To run the OS in QEMU:
make run
To debug with GDB:
make debug
To clean build artifacts:
make clean
After booting, the system will display a prompt:
martin@root:
Available commands:
HALT
: Halts the CPUSEC
: Displays current date and timeS
: Sleeps for 5 secondsW1
: Reads a sector from the disk (ATA driver test)
- BIOS loads the bootloader (bootsect.bin) into memory at 0x7C00
- Bootloader sets up the stack and prints status messages
- Bootloader loads the kernel from disk to memory address 0x1000
- Bootloader sets up the Global Descriptor Table (GDT)
- Bootloader switches CPU to 32-bit protected mode
- Control is transferred to the kernel entry point
- Bootloader: Loaded at 0x7C00
- Kernel: Loaded at 0x1000
- Stack: Set up at 0x90000
- Video Memory: 0xB8000 (VGA text mode)
The OS implements a complete interrupt handling system:
- 32 CPU exceptions (0-31)
- 16 Hardware IRQs (32-47)
- Custom handlers for keyboard input (IRQ1) and timer (IRQ0)
- Direct VGA memory access at 0xB8000
- Text mode output (80x25 characters)
- Color attribute support
- Cursor positioning and scrolling
- PS/2 keyboard scancode translation
- Support for basic ASCII characters
- Backspace and enter key handling
- Input buffering
- PIO mode disk access
- Sector reading functionality
- Status register polling
- Programmable Interval Timer (PIT) configuration
- Real-time clock using CMOS
- Sleep functionality
Contributions are welcome! Please feel free to submit a Pull Request.
Areas for improvement:
- File system implementation
- Memory management improvements
- Additional device drivers
- Process scheduling
- Virtual memory support
This project is licensed under the MIT License - see the LICENSE file for details.
- This project is based on various OS development tutorials and resources
- Inspired by the work of the open-source community in low-level system programming