ahrightos-kernel 0.1.0

RISC-V 64 Rust teaching kernel for AhRightOS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! VirtIO Block Device Driver (MMIO Transport, Legacy Mode)
//!
//! Implements the BlockDevice trait for real QEMU virtio-blk devices
//! discovered via the MMIO transport on RISC-V virt machines.
//!
//! ## Device Discovery
//! QEMU RISC-V virt places virtio MMIO devices at 0x10001000+ (stride 0x1000).
//! We scan each slot for MagicValue (0x74726976) and DeviceID 2 (block).
//!
//! ## Virtqueue Architecture
//! Single virtqueue (index 0) for both read and write. Each I/O request uses
//! a 3-descriptor chain: request header → data buffer → status byte.

use alloc::vec;
use alloc::vec::Vec;
use core::ptr::{read_volatile, write_volatile};
use core::sync::atomic::{fence, AtomicBool, Ordering};
use spin::Mutex;

use super::super::fs::block_dev::BlockDevice;
use super::super::fs::file::FileError;

// ============================================================
// MMIO Register Offsets
// ============================================================
const MAGIC_VALUE: usize = 0x00;
const VERSION: usize = 0x04;
const DEVICE_ID: usize = 0x08;
const DEVICE_FEATURES: usize = 0x10;
const DEVICE_FEATURES_SEL: usize = 0x14;
const DRIVER_FEATURES: usize = 0x20;
const DRIVER_FEATURES_SEL: usize = 0x24;
const GUEST_PAGE_SIZE: usize = 0x28;
const QUEUE_SEL: usize = 0x30;
const QUEUE_NUM_MAX: usize = 0x34;
const QUEUE_NUM: usize = 0x38;
const QUEUE_ALIGN: usize = 0x3C;
const QUEUE_PFN: usize = 0x40;
const QUEUE_NOTIFY: usize = 0x50;
const INTERRUPT_STATUS: usize = 0x60;
const INTERRUPT_ACK: usize = 0x64;
const STATUS: usize = 0x70;
const DEVICE_CONFIG: usize = 0x100;

// Status register bits
const STATUS_ACKNOWLEDGE: u32 = 1;
const STATUS_DRIVER: u32 = 2;
const STATUS_DRIVER_OK: u32 = 4;
const STATUS_FEATURES_OK: u32 = 8;

// MMIO discovery
const VIRTIO_MMIO_BASE: usize = 0x1000_1000;
const VIRTIO_MMIO_STRIDE: usize = 0x1000;
const VIRTIO_MMIO_SLOTS: usize = 8;
const VIRTIO_MAGIC: u32 = 0x7472_6976;
const VIRTIO_DEV_BLOCK: u32 = 2;

// Virtqueue
const VIRTQ_SIZE: usize = 8;

// Virtio-blk request codes
const VIRTIO_BLK_T_IN: u32 = 0;
const VIRTIO_BLK_T_OUT: u32 = 1;
const VIRTIO_BLK_S_OK: u8 = 0;

// ============================================================
// MMIO access helpers
// ============================================================

struct MmioRegion {
    pub base: usize,
}

impl MmioRegion {
    fn new(base: usize) -> Self {
        Self { base }
    }

    #[inline]
    fn read32(&self, offset: usize) -> u32 {
        unsafe { read_volatile((self.base + offset) as *const u32) }
    }

    #[inline]
    fn write32(&self, offset: usize, val: u32) {
        unsafe {
            write_volatile((self.base + offset) as *mut u32, val);
        }
    }
}

// ============================================================
// Virtqueue structures
// ============================================================

#[repr(C, align(16))]
struct VirtqDesc {
    addr: u64,
    len: u32,
    flags: u16,
    next: u16,
}

const VIRTQ_DESC_F_NEXT: u16 = 1;
const VIRTQ_DESC_F_WRITE: u16 = 2;

#[repr(C, align(2))]
struct VirtqAvail {
    flags: u16,
    idx: u16,
    ring: [u16; VIRTQ_SIZE],
}

#[repr(C, align(4))]
#[derive(Clone, Copy)]
struct VirtqUsedElem {
    id: u32,
    len: u32,
}

#[repr(C, align(4))]
struct VirtqUsed {
    flags: u16,
    idx: u16,
    ring: [VirtqUsedElem; VIRTQ_SIZE],
}

struct VirtqMemory {
    desc: *mut VirtqDesc,
    avail: *mut VirtqAvail,
    used: *mut VirtqUsed,
    desc_phys: u64,
    _backing: Vec<u8>,
}

impl VirtqMemory {
    /// Allocate contiguous page-aligned memory for legacy virtqueue layout:
    ///   Page 0: Descriptor Table | Available Ring
    ///   Page 1: Used Ring (must be page-aligned per QUEUE_ALIGN)
    fn new() -> Option<Self> {
        let desc_size = core::mem::size_of::<VirtqDesc>() * VIRTQ_SIZE;
        let page_size = 4096usize;
        let total_size = page_size * 2;
        let mut backing = vec![0u8; total_size + page_size];
        let base = backing.as_mut_ptr() as usize;
        let aligned = (base + page_size - 1) & !(page_size - 1);

        let desc = aligned as *mut VirtqDesc;
        let avail = (aligned + desc_size) as *mut VirtqAvail;
        let used = (aligned + page_size) as *mut VirtqUsed;

        unsafe {
            for i in 0..VIRTQ_SIZE {
                (*desc.add(i)) = VirtqDesc {
                    addr: 0,
                    len: 0,
                    flags: 0,
                    next: 0,
                };
            }
            (*avail) = VirtqAvail {
                flags: 0,
                idx: 0,
                ring: [0u16; VIRTQ_SIZE],
            };
            (*used) = VirtqUsed {
                flags: 0,
                idx: 0,
                ring: [VirtqUsedElem { id: 0, len: 0 }; VIRTQ_SIZE],
            };
        }

        Some(Self {
            desc,
            avail,
            used,
            desc_phys: aligned as u64,
            _backing: backing,
        })
    }
}

#[repr(C)]
struct BlkReq {
    req_type: u32,
    reserved: u32,
    sector: u64,
}

// ============================================================
// Global device reference for interrupt handler
// ============================================================

static VIRTIO_DEV_PTR: Mutex<Option<usize>> = Mutex::new(None);
static VIRTIO_IO_COMPLETE: AtomicBool = AtomicBool::new(false);

pub fn try_handle_interrupt() {
    let ptr = *VIRTIO_DEV_PTR.lock();
    if let Some(mmio_base) = ptr {
        let mmio = MmioRegion::new(mmio_base);
        let status = mmio.read32(INTERRUPT_STATUS);
        if status & 1 != 0 {
            mmio.write32(INTERRUPT_ACK, status);
            fence(Ordering::SeqCst);
            VIRTIO_IO_COMPLETE.store(true, Ordering::Release);
        }
    }
}

// ============================================================
// VirtIO Block Device
// ============================================================

pub struct VirtioBlkDev {
    mmio: MmioRegion,
    vq: Mutex<VirtqMemory>,
    capacity: usize,
    last_used_idx: Mutex<u16>,
}

impl VirtioBlkDev {
    /// Scan MMIO region for a virtio-blk device and initialize it.
    pub fn probe() -> Option<Self> {
        for slot in 0..VIRTIO_MMIO_SLOTS {
            let base = VIRTIO_MMIO_BASE + slot * VIRTIO_MMIO_STRIDE;
            let mmio = MmioRegion::new(base);

            if mmio.read32(MAGIC_VALUE) != VIRTIO_MAGIC {
                continue;
            }
            if mmio.read32(DEVICE_ID) != VIRTIO_DEV_BLOCK {
                continue;
            }

            crate::serial_println!(
                "[VIRTIO] Found virtio-blk at {:#x}, version={}",
                base,
                mmio.read32(VERSION)
            );

            return Self::init_device(mmio);
        }
        crate::serial_println!("[VIRTIO] No virtio-blk device found");
        None
    }

    fn init_device(mmio: MmioRegion) -> Option<Self> {
        // Reset device
        mmio.write32(STATUS, 0);
        fence(Ordering::SeqCst);

        // Acknowledge
        mmio.write32(STATUS, STATUS_ACKNOWLEDGE);
        fence(Ordering::SeqCst);

        // Driver
        mmio.write32(STATUS, STATUS_ACKNOWLEDGE | STATUS_DRIVER);
        fence(Ordering::SeqCst);

        // Negotiate features (none)
        mmio.write32(DEVICE_FEATURES_SEL, 0);
        mmio.read32(DEVICE_FEATURES);
        mmio.write32(DEVICE_FEATURES_SEL, 1);
        mmio.read32(DEVICE_FEATURES);
        mmio.write32(DRIVER_FEATURES_SEL, 0);
        mmio.write32(DRIVER_FEATURES, 0);
        mmio.write32(DRIVER_FEATURES_SEL, 1);
        mmio.write32(DRIVER_FEATURES, 0);

        // Features OK
        mmio.write32(
            STATUS,
            STATUS_ACKNOWLEDGE | STATUS_DRIVER | STATUS_FEATURES_OK,
        );
        fence(Ordering::SeqCst);

        let status = mmio.read32(STATUS);
        if status & STATUS_FEATURES_OK == 0 {
            crate::serial_println!("[VIRTIO] Feature negotiation failed, status={:#x}", status);
            return None;
        }

        // Setup virtqueue (legacy mode via QUEUE_PFN)
        let vq = VirtqMemory::new()?;

        mmio.write32(QUEUE_SEL, 0);
        fence(Ordering::SeqCst);

        let max_size = mmio.read32(QUEUE_NUM_MAX);
        if max_size == 0 {
            return None;
        }

        let queue_size = core::cmp::min(VIRTQ_SIZE as u32, max_size);
        mmio.write32(QUEUE_NUM, queue_size);
        fence(Ordering::SeqCst);

        let page_size = 4096u32;
        mmio.write32(GUEST_PAGE_SIZE, page_size);
        mmio.write32(QUEUE_ALIGN, page_size);
        mmio.write32(QUEUE_PFN, (vq.desc_phys as u32) / page_size);
        fence(Ordering::SeqCst);

        // Driver OK
        mmio.write32(
            STATUS,
            STATUS_ACKNOWLEDGE | STATUS_DRIVER | STATUS_FEATURES_OK | STATUS_DRIVER_OK,
        );
        fence(Ordering::SeqCst);

        // Read capacity from config space
        let capacity_lo = mmio.read32(DEVICE_CONFIG);
        let capacity_hi = mmio.read32(DEVICE_CONFIG + 4);
        let capacity = ((capacity_hi as u64) << 32 | capacity_lo as u64) as usize;

        // Enable interrupts on the device
        mmio.write32(INTERRUPT_ACK, 0x3); // clear any pending interrupts
        fence(Ordering::SeqCst);

        // Store MMIO base for the external interrupt handler
        *VIRTIO_DEV_PTR.lock() = Some(mmio.base);

        crate::serial_println!(
            "[VIRTIO] Device ready: {} sectors ({} MB), interrupts enabled",
            capacity,
            capacity * 512 / (1024 * 1024)
        );

        Some(Self {
            mmio,
            vq: Mutex::new(vq),
            capacity,
            last_used_idx: Mutex::new(0),
        })
    }

    /// Submit a block I/O request using a 3-descriptor chain.
    /// Polls for completion.
    fn submit_io(&self, sector: u64, buf: &mut [u8], is_write: bool) -> Result<(), FileError> {
        if buf.len() != 512 {
            return Err(FileError::InvalidOperation);
        }
        if sector >= self.capacity as u64 && self.capacity > 0 {
            return Err(FileError::InvalidOperation);
        }

        let req = BlkReq {
            req_type: if is_write {
                VIRTIO_BLK_T_OUT
            } else {
                VIRTIO_BLK_T_IN
            },
            reserved: 0,
            sector,
        };

        let mut status: u8 = 0xFF;
        let req_phys = &req as *const BlkReq as u64;
        let buf_phys = buf.as_ptr() as u64;
        let status_phys = &mut status as *mut u8 as u64;

        let vq = self.vq.lock();
        let desc = vq.desc;

        // Build 3-descriptor chain
        unsafe {
            (*desc.add(0)).addr = req_phys;
            (*desc.add(0)).len = core::mem::size_of::<BlkReq>() as u32;
            (*desc.add(0)).flags = VIRTQ_DESC_F_NEXT;
            (*desc.add(0)).next = 1;

            (*desc.add(1)).addr = buf_phys;
            (*desc.add(1)).len = 512;
            (*desc.add(1)).flags = VIRTQ_DESC_F_NEXT;
            if !is_write {
                (*desc.add(1)).flags |= VIRTQ_DESC_F_WRITE;
            }
            (*desc.add(1)).next = 2;

            (*desc.add(2)).addr = status_phys;
            (*desc.add(2)).len = 1;
            (*desc.add(2)).flags = VIRTQ_DESC_F_WRITE;
            (*desc.add(2)).next = 0;
        }

        fence(Ordering::SeqCst);

        // Add to available ring
        let avail = vq.avail;
        let used_ptr = vq.used;
        unsafe {
            let idx = (*avail).idx;
            let ring_idx = (idx as usize) % VIRTQ_SIZE;
            (*avail).ring[ring_idx] = 0;
            fence(Ordering::SeqCst);
            (*avail).idx = idx.wrapping_add(1);
        }

        // Notify device
        self.mmio.write32(QUEUE_NOTIFY, 0);
        fence(Ordering::SeqCst);
        drop(vq);

        // Wait for interrupt-driven completion
        let mut last = self.last_used_idx.lock();
        let used = unsafe { &*used_ptr };
        VIRTIO_IO_COMPLETE.store(false, Ordering::Release);
        loop {
            fence(Ordering::SeqCst);
            let used_idx = unsafe { read_volatile(&used.idx) };
            if used_idx != *last {
                *last = used_idx;
                break;
            }
            drop(last);
            unsafe {
                riscv::register::sstatus::set_sie();
            }
            riscv::asm::wfi();
            unsafe {
                riscv::register::sstatus::clear_sie();
            }
            last = self.last_used_idx.lock();
        }
        VIRTIO_IO_COMPLETE.store(false, Ordering::Release);

        fence(Ordering::SeqCst);

        if status != VIRTIO_BLK_S_OK {
            crate::serial_println!("[VIRTIO] I/O error: status={:#x}", status);
            return Err(FileError::IoError);
        }

        Ok(())
    }
}

// ============================================================
// BlockDevice trait implementation
// ============================================================

impl BlockDevice for VirtioBlkDev {
    fn read_block(&self, block_no: usize, buf: &mut [u8]) -> Result<(), FileError> {
        if buf.len() != 512 {
            return Err(FileError::InvalidOperation);
        }
        self.submit_io(block_no as u64, buf, false)
    }

    fn write_block(&self, block_no: usize, buf: &[u8]) -> Result<(), FileError> {
        if buf.len() != 512 {
            return Err(FileError::InvalidOperation);
        }
        let mut tmp = [0u8; 512];
        tmp.copy_from_slice(buf);
        self.submit_io(block_no as u64, &mut tmp, true)
    }

    fn block_count(&self) -> usize {
        self.capacity
    }
}

unsafe impl Send for VirtioBlkDev {}
unsafe impl Sync for VirtioBlkDev {}