feat: proper IPC-based boot — init loads servers from storage via IPC, VirtIO GPU driver

Complete microkernel boot pipeline: adapter loads only init + storage + display
from disk. Init bootstraps all other servers by reading binaries from the
storage server via IPC, parsing ELF, creating processes, mapping pages, and
spawning threads.

New syscalls:
- CreateEndpoint: userspace endpoint creation with full capability grant
- DebugPutchar: debug output to UART from userspace
- MapPage: cross-process page mapping (init loads binaries into children)
- SpawnThreadIn: spawn thread in another process via capability

Key fixes:
- Per-process L1 page tables for VPN[2]=0 (prevents cross-process corruption)
- Separate kernel_pt from page_tables[0] (init pid=0 overwrote kernel PT ref)
- Thread Running state set in switch_to_next (syscalls rejected without it)
- Pending IPC results applied during context switch
- SIE disabled during boot (timer can't preempt into unready processes)
- User heap VA starts at 0x40000 (avoids shared buffer at 0x30000)
- RequestPages maps at user VAs via per-process heap allocator

VirtIO GPU:
- Adapter probes for GPU device, initializes resource/scanout/backing
- Display server writes to mapped framebuffer, flushes via GPU virtqueue
- 640x480 BGRA framebuffer with transfer-to-host and resource-flush

Storage server:
- Userspace VirtIO block driver using pre-mapped MMIO and DMA pages
- FAT16 filesystem reader serving file-read requests via IPC
- Shared buffer protocol: init writes filename, storage writes file data

Echo app for IPC testing (adapter-loaded echo proved the pipeline end-to-end).
This commit is contained in:
2026-10-18 13:02:30 +02:00
parent ffde4065ba
commit 38f20cc3c4
41 changed files with 1676 additions and 808 deletions

View File

@@ -1,34 +1,123 @@
use crate::framebuffer::Framebuffer;
const VIRTIO_GPU_BASE: usize = 0x10008000;
const FB_WIDTH: u16 = 640;
const FB_HEIGHT: u16 = 480;
const GPU_MMIO_VA: usize = 0x20000;
const VQUEUE_VA: usize = 0x21000;
const CMDBUF_VA: usize = 0x23000;
const PHYSINFO_VA: usize = 0x24000;
const FB_VA: usize = 0x100000;
const FB_BPP: usize = 4;
const QUEUE_SIZE: usize = 8;
struct PhysInfo {
cmd_pa: u64,
resp_pa: u64,
fb_pa: u64,
width: u64,
height: u64,
}
fn read_phys_info() -> PhysInfo {
unsafe {
let p = PHYSINFO_VA as *const u64;
PhysInfo {
cmd_pa: core::ptr::read_volatile(p),
resp_pa: core::ptr::read_volatile(p.add(1)),
fb_pa: core::ptr::read_volatile(p.add(2)),
width: core::ptr::read_volatile(p.add(3)),
height: core::ptr::read_volatile(p.add(4)),
}
}
}
fn mmio_write(offset: usize, val: u32) {
unsafe { core::ptr::write_volatile((GPU_MMIO_VA + offset) as *mut u32, val) }
}
pub struct VirtioFramebuffer {
buffer: &'static mut [u8],
width: u16,
height: u16,
stride: usize,
phys: PhysInfo,
}
impl VirtioFramebuffer {
pub fn new(buffer_addr: usize) -> Self {
let stride = FB_WIDTH as usize * FB_BPP;
let size = stride * FB_HEIGHT as usize;
let buffer = unsafe { core::slice::from_raw_parts_mut(buffer_addr as *mut u8, size) };
pub fn new(_buffer_addr: usize) -> Self {
let phys = read_phys_info();
let width = phys.width as u16;
let height = phys.height as u16;
let stride = width as usize * FB_BPP;
for byte in buffer.iter_mut() {
*byte = 0;
}
Self { width, height, stride, phys }
}
Self {
buffer,
width: FB_WIDTH,
height: FB_HEIGHT,
stride,
fn send_gpu_cmd(&self, cmd_len: u32, resp_len: u32) {
unsafe {
let desc0 = VQUEUE_VA as *mut u8;
core::ptr::write_volatile(desc0 as *mut u64, self.phys.cmd_pa);
core::ptr::write_volatile(desc0.add(8) as *mut u32, cmd_len);
core::ptr::write_volatile(desc0.add(12) as *mut u16, 1); // NEXT
core::ptr::write_volatile(desc0.add(14) as *mut u16, 1);
let desc1 = desc0.add(16);
core::ptr::write_volatile(desc1 as *mut u64, self.phys.resp_pa);
core::ptr::write_volatile(desc1.add(8) as *mut u32, resp_len);
core::ptr::write_volatile(desc1.add(12) as *mut u16, 2); // WRITE
core::ptr::write_volatile(desc1.add(14) as *mut u16, 0);
let avail = VQUEUE_VA + QUEUE_SIZE * 16;
let avail_idx = core::ptr::read_volatile((avail + 2) as *const u16);
core::ptr::write_volatile(
(avail + 4 + (avail_idx as usize % QUEUE_SIZE) * 2) as *mut u16,
0,
);
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
core::ptr::write_volatile((avail + 2) as *mut u16, avail_idx.wrapping_add(1));
core::sync::atomic::fence(core::sync::atomic::Ordering::SeqCst);
mmio_write(0x050, 0);
let used = VQUEUE_VA + 4096;
let expected = avail_idx.wrapping_add(1);
let mut timeout = 10_000_000u32;
while core::ptr::read_volatile((used + 2) as *const u16) != expected {
core::hint::spin_loop();
timeout -= 1;
if timeout == 0 { return; }
}
}
}
fn transfer_and_flush(&self, x: u32, y: u32, w: u32, h: u32) {
// TRANSFER_TO_HOST_2D
unsafe {
let p = CMDBUF_VA as *mut u8;
core::ptr::write_bytes(p, 0, 512);
core::ptr::write_volatile(p as *mut u32, 0x0105); // TRANSFER_TO_HOST_2D
// rect at offset 24
core::ptr::write_volatile(p.add(24) as *mut u32, x);
core::ptr::write_volatile(p.add(28) as *mut u32, y);
core::ptr::write_volatile(p.add(32) as *mut u32, w);
core::ptr::write_volatile(p.add(36) as *mut u32, h);
// offset at 40
core::ptr::write_volatile(p.add(40) as *mut u64, 0);
// resource_id at 48
core::ptr::write_volatile(p.add(48) as *mut u32, 1);
}
self.send_gpu_cmd(56, 24);
// RESOURCE_FLUSH
unsafe {
let p = CMDBUF_VA as *mut u8;
core::ptr::write_bytes(p, 0, 512);
core::ptr::write_volatile(p as *mut u32, 0x0106); // RESOURCE_FLUSH
core::ptr::write_volatile(p.add(24) as *mut u32, x);
core::ptr::write_volatile(p.add(28) as *mut u32, y);
core::ptr::write_volatile(p.add(32) as *mut u32, w);
core::ptr::write_volatile(p.add(36) as *mut u32, h);
core::ptr::write_volatile(p.add(40) as *mut u32, 1); // resource_id
}
self.send_gpu_cmd(48, 24);
}
}
impl Framebuffer for VirtioFramebuffer {
@@ -41,15 +130,12 @@ impl Framebuffer for VirtioFramebuffer {
}
fn write_pixel(&mut self, x: u16, y: u16, color: u32) {
if x >= self.width || y >= self.height {
return;
}
if x >= self.width || y >= self.height { return; }
let offset = (y as usize) * self.stride + (x as usize) * FB_BPP;
let bytes = color.to_le_bytes();
self.buffer[offset] = bytes[0];
self.buffer[offset + 1] = bytes[1];
self.buffer[offset + 2] = bytes[2];
self.buffer[offset + 3] = bytes[3];
let buf = FB_VA + offset;
unsafe {
core::ptr::write_volatile(buf as *mut u32, color);
}
}
fn fill_rect(&mut self, x: u16, y: u16, w: u16, h: u16, color: u32) {
@@ -61,21 +147,14 @@ impl Framebuffer for VirtioFramebuffer {
}
fn clear(&mut self, color: u32) {
let bytes = color.to_le_bytes();
for y in 0..self.height as usize {
for x in 0..self.width as usize {
let offset = y * self.stride + x * FB_BPP;
self.buffer[offset] = bytes[0];
self.buffer[offset + 1] = bytes[1];
self.buffer[offset + 2] = bytes[2];
self.buffer[offset + 3] = bytes[3];
}
let total = self.width as usize * self.height as usize;
let buf = FB_VA as *mut u32;
for i in 0..total {
unsafe { core::ptr::write_volatile(buf.add(i), color); }
}
}
fn flush(&mut self) {
// VirtIO GPU flush would send TRANSFER_TO_HOST_2D + RESOURCE_FLUSH
// For now, the buffer is memory-mapped — no explicit flush needed
// when using ramfb or a simple scanout
self.transfer_and_flush(0, 0, self.width as u32, self.height as u32);
}
}