Skip to content

Hardware Drivers

ghartrid edited this page Feb 9, 2026 · 1 revision

Hardware Drivers

PhantomOS includes drivers for graphics, USB, networking, storage, and power management, with a hardware abstraction layer for GPU backends.


GPU HAL (kernel/gpu_hal.h)

The GPU Hardware Abstraction Layer provides a unified interface for multiple graphics backends. At boot, all backends register themselves, and the highest-priority available backend is selected.

Backends

Backend Priority Source Description
Intel BLT 100 intel_gpu.c Intel integrated GPU BLT engine
VirtIO GPU 80 virtio_gpu.c QEMU VirtIO GPU device
VMware SVGA 60 vmware_svga.c VMware SVGA II protocol
Bochs VGA 40 bochs_vga.c Bochs/QEMU VGA extensions
Software 0 (built-in) CPU-based fallback

Operations

gpu_hal_fill_rect(x, y, w, h, color);   // Hardware-accelerated rectangle fill
gpu_hal_clear(color);                     // Full screen clear
gpu_hal_copy_region(dx, dy, sx, sy, w, h); // Screen-to-screen copy
gpu_hal_flip();                           // Backbuffer to frontbuffer
gpu_hal_sync();                           // Drain pending operations

If the active backend doesn't support an operation, it falls back to software rendering. Statistics track GPU vs. software fallback counts.

VMware SVGA Notes

  • QEMU uses SVGA_IO_MUL=1: value port at BAR0+1 (not +4)
  • QEMU doesn't implement RECT_FILL or RECT_COPY FIFO commands
  • Uses CPU backbuffer + SVGA_CMD_UPDATE for compatibility

PCI Bus (kernel/pci.c)

Full PCI bus enumeration via I/O ports 0xCF8/0xCFC:

void pci_init(void);                     // Enumerate all buses/devices/functions
uint32_t pci_read(uint8_t bus, uint8_t dev, uint8_t func, uint8_t offset);
void pci_write(uint8_t bus, uint8_t dev, uint8_t func, uint8_t offset, uint32_t val);

Discovers VirtIO devices, USB controllers, GPU devices, and storage controllers.


USB HID Stack

UHCI Host Controller (kernel/usb.c)

USB 1.1 Universal Host Controller Interface:

  • Discovered via PCI (class 0x0C, subclass 0x03, prog-if 0x00)
  • I/O port base from PCI BAR4
  • 1024-entry frame list for scheduled transfers
  • Transfer Descriptors (TDs) and Queue Heads (QHs) for control/interrupt transfers

HID Boot Protocol (kernel/usb_hid.c)

Supports boot-protocol keyboard and mouse:

  • Device enumeration via USB SET_ADDRESS, GET_DESCRIPTOR
  • SET_PROTOCOL to boot mode (simplified 8-byte reports)
  • Interrupt IN polling via frame list scheduled TDs
  • Keyboard: scancode translation, press/release detection via report diff
  • Mouse: relative X/Y movement, button state
void usb_hid_init(void);
void usb_hid_poll(void);        // Called from main loop
int usb_hid_device_count(void);

VirtIO Networking (kernel/virtio_net.c)

VirtIO-net PCI device driver with ARP/ICMP stack:

Configuration

  • Static IP: 10.0.2.15/24
  • Gateway: 10.0.2.2 (QEMU user-mode networking)
  • MAC address read from VirtIO config space

Protocol Support

  • ARP: Responds to ARP requests, sends ARP for gateway resolution
  • ICMP: Echo reply (responds to pings) and echo request (sends pings)

API

int virtio_net_init(void);
int virtio_net_ping(uint32_t dest_ip, uint16_t seq);
int virtio_net_ping_check(void);
void virtio_net_poll(void);
const struct net_stats *virtio_net_get_stats(void);

Network Window

Live desktop window showing: MAC address, IP, link status, TX/RX packet counts, ping results.


ACPI Power Management (kernel/acpi.c)

PIIX4 PM device support for QEMU i440fx platform:

  • Detects PIIX4 PM via PCI (vendor 0x8086, device 0x7113)
  • SCI interrupt on IRQ9 for power button events
  • Graceful shutdown via S5 sleep state
  • System reset via port 0xCF9
int acpi_init(void);
void acpi_request_shutdown(void);   // Request graceful shutdown
void acpi_poweroff(void);           // Execute S5 power-off
void acpi_reboot(void);             // System reset

VM Optimizations (kernel/vm_detect.c)

Detects hypervisor via CPUID leaf 0x40000000:

  • KVM: Paravirtualized clock via MSR (kernel/kvm_clock.c)
  • QEMU: Dirty-rect tile tracking for efficient screen updates
  • VMware: SVGA II driver activation
  • General: Timer-based frame limiting to avoid busy-loop rendering

VirtIO Console (kernel/virtio_console.c)

VirtIO serial port for kernel log output when serial port unavailable. Used as debug output channel in VirtIO-enabled VMs.


Input Drivers

PS/2 Keyboard (kernel/keyboard.c)

  • IRQ1 handler
  • Scancode set 1 translation
  • Modifier key tracking (shift, ctrl, alt)
  • Key buffer for asynchronous reading

PS/2 Mouse (kernel/mouse.c)

  • IRQ12 handler (requires PIC cascade on IRQ2)
  • 3-byte packet protocol (buttons, delta-X, delta-Y)
  • Cursor position tracking with screen bounds clamping