aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / tests / nvme_identify.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Oracle D2: NVMe Identify (CNS=1 controller, CNS=0 namespace) integration tests. |
| 17 | //! |
| 18 | //! Self-contained external test crate. It implements its own `Registers`, |
| 19 | //! `DmaMemory`, and `Delay` fakes (OracleRegs/OracleDma/OracleDelay) that model |
| 20 | //! an in-memory controller answering admin Identify commands by writing the |
| 21 | //! 4096-byte identify payload into the PRP1 buffer. No hardware, no sleeps. |
| 22 | |
| 23 | use std::cell::RefCell; |
| 24 | use std::collections::BTreeMap; |
| 25 | use std::rc::Rc; |
| 26 | |
| 27 | use aienos_kernel::block::BlockDevice; |
| 28 | use aienos_kernel::nvme::driver::{ |
| 29 | Delay, DmaMemory, DmaRegion, NamespaceInfo, NvmeController, NvmeError, |
| 30 | }; |
| 31 | use aienos_kernel::nvme::{Registers, CC_EN, CSTS_RDY, REG_ACQ, REG_ASQ, REG_CC, REG_CSTS}; |
| 32 | |
| 33 | const ADMIN_DEPTH: usize = 8; |
| 34 | const IO_DEPTH: usize = 8; |
| 35 | |
| 36 | // Doorbell offsets for CAP.DSTRD = 0 (stride 4): doorbell_offset() yields these. |
| 37 | const ADMIN_SQ_DB: u32 = 0x1000; // qid 0, submission |
| 38 | const IO_SQ_DB: u32 = 0x1008; // qid 1, submission |
| 39 | |
| 40 | type Store = Rc<RefCell<BTreeMap<u64, Vec<u8>>>>; |
| 41 | |
| 42 | // --------------------------------------------------------------------------- |
| 43 | // DMA fake: page-aligned bump allocator over a shared physical->bytes map. |
| 44 | // --------------------------------------------------------------------------- |
| 45 | |
| 46 | struct OracleDma { |
| 47 | store: Store, |
| 48 | next: u64, |
| 49 | } |
| 50 | |
| 51 | impl DmaMemory for OracleDma { |
| 52 | fn allocate(&mut self, size: usize, alignment: usize) -> Result<DmaRegion, NvmeError> { |
| 53 | let mask = alignment as u64 - 1; |
| 54 | let physical = (self.next + mask) & !mask; |
| 55 | self.next = physical + size as u64; |
| 56 | self.store.borrow_mut().insert(physical, vec![0; size]); |
| 57 | Ok(DmaRegion { |
| 58 | physical, |
| 59 | bytes: vec![0; size], |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | fn read(&self, physical: u64, output: &mut [u8]) -> Result<(), NvmeError> { |
| 64 | let store = self.store.borrow(); |
| 65 | let (base, bytes) = store.range(..=physical).next_back().ok_or(NvmeError::Dma)?; |
| 66 | let offset = (physical - base) as usize; |
| 67 | output.copy_from_slice( |
| 68 | bytes |
| 69 | .get(offset..offset + output.len()) |
| 70 | .ok_or(NvmeError::Dma)?, |
| 71 | ); |
| 72 | Ok(()) |
| 73 | } |
| 74 | |
| 75 | fn write(&mut self, physical: u64, input: &[u8]) -> Result<(), NvmeError> { |
| 76 | let mut store = self.store.borrow_mut(); |
| 77 | let (base, bytes) = store |
| 78 | .range_mut(..=physical) |
| 79 | .next_back() |
| 80 | .ok_or(NvmeError::Dma)?; |
| 81 | let offset = (physical - *base) as usize; |
| 82 | bytes |
| 83 | .get_mut(offset..offset + input.len()) |
| 84 | .ok_or(NvmeError::Dma)? |
| 85 | .copy_from_slice(input); |
| 86 | Ok(()) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // --------------------------------------------------------------------------- |
| 91 | // Registers fake: executes admin/IO commands when the matching doorbell is rung. |
| 92 | // --------------------------------------------------------------------------- |
| 93 | |
| 94 | struct OracleRegs { |
| 95 | store: Store, |
| 96 | cc: u32, |
| 97 | writes: Vec<(u32, u32)>, |
| 98 | |
| 99 | // Canned Identify responses. |
| 100 | namespace_size: u64, |
| 101 | flbas: u8, |
| 102 | lbads: u8, |
| 103 | mdts: u8, |
| 104 | nsid_zero_error: bool, |
| 105 | |
| 106 | command_error: u16, |
| 107 | |
| 108 | admin_sq: u64, |
| 109 | admin_cq: u64, |
| 110 | admin_sq_tail: usize, |
| 111 | admin_cq_tail: usize, |
| 112 | admin_phase: bool, |
| 113 | |
| 114 | io_sq: u64, |
| 115 | io_cq: u64, |
| 116 | io_sq_tail: usize, |
| 117 | io_cq_tail: usize, |
| 118 | io_phase: bool, |
| 119 | io_depth: usize, |
| 120 | } |
| 121 | |
| 122 | impl Registers for OracleRegs { |
| 123 | fn read32(&mut self, offset: u32) -> u32 { |
| 124 | match offset { |
| 125 | 0 => 0x0100_03ff, // CAP.TO = 1 (500 ms units), DSTRD = 0, MQES = 1023 |
| 126 | 4 => 0, |
| 127 | REG_CC => self.cc, |
| 128 | REG_CSTS if self.cc & CC_EN != 0 => CSTS_RDY, |
| 129 | REG_CSTS => 0, |
| 130 | _ => 0, |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn write32(&mut self, offset: u32, value: u32) { |
| 135 | self.writes.push((offset, value)); |
| 136 | |
| 137 | match offset { |
| 138 | REG_CC => self.cc = value, |
| 139 | REG_ASQ => self.admin_sq = (self.admin_sq & !0xffff_ffff) | value as u64, |
| 140 | x if x == REG_ASQ + 4 => { |
| 141 | self.admin_sq = (self.admin_sq & 0xffff_ffff) | ((value as u64) << 32) |
| 142 | } |
| 143 | REG_ACQ => self.admin_cq = (self.admin_cq & !0xffff_ffff) | value as u64, |
| 144 | x if x == REG_ACQ + 4 => { |
| 145 | self.admin_cq = (self.admin_cq & 0xffff_ffff) | ((value as u64) << 32) |
| 146 | } |
| 147 | _ => {} |
| 148 | } |
| 149 | |
| 150 | if offset == ADMIN_SQ_DB { |
| 151 | self.run_admin(); |
| 152 | } else if offset == IO_SQ_DB { |
| 153 | self.run_io(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | impl OracleRegs { |
| 159 | fn run_admin(&mut self) { |
| 160 | let admin_sq = self.admin_sq; |
| 161 | let admin_cq = self.admin_cq; |
| 162 | let mut memory = self.store.borrow_mut(); |
| 163 | |
| 164 | let at = self.admin_sq_tail * 64; |
| 165 | let command = memory |
| 166 | .get(&admin_sq) |
| 167 | .expect("admin SQ allocated") |
| 168 | .get(at..at + 64) |
| 169 | .expect("admin SQE in range") |
| 170 | .to_vec(); |
| 171 | |
| 172 | let opcode = command[0]; |
| 173 | let cid = u16::from_le_bytes(command[2..4].try_into().unwrap()); |
| 174 | let nsid = u32::from_le_bytes(command[4..8].try_into().unwrap()); |
| 175 | let prp = u64::from_le_bytes(command[24..32].try_into().unwrap()); |
| 176 | let cdw10 = u32::from_le_bytes(command[40..44].try_into().unwrap()); |
| 177 | let cdw11 = u32::from_le_bytes(command[44..48].try_into().unwrap()); |
| 178 | |
| 179 | self.command_error = 0; |
| 180 | |
| 181 | match opcode { |
| 182 | 0x06 => { |
| 183 | // Identify: CNS in cdw10; answer into the PRP1 buffer. |
| 184 | let cns = cdw10; |
| 185 | let data = memory.get_mut(&prp).expect("identify buffer allocated"); |
| 186 | data.fill(0); |
| 187 | if cns == 1 { |
| 188 | data[77] = self.mdts; |
| 189 | } else if self.nsid_zero_error && nsid == 0 { |
| 190 | self.command_error = 1 << 1; |
| 191 | } else { |
| 192 | data[0..8].copy_from_slice(&self.namespace_size.to_le_bytes()); |
| 193 | // Descriptor 0 decoy: must be ignored when FLBAS selects another. |
| 194 | data[130] = 9; |
| 195 | let flbas = (self.flbas & 0x0f) as usize; |
| 196 | data[26] = flbas as u8; |
| 197 | let descriptor = 128 + flbas * 4; |
| 198 | data[descriptor + 2] = self.lbads; |
| 199 | } |
| 200 | } |
| 201 | 0x05 => { |
| 202 | // Create I/O CQ: remember buffer + depth (QID 1). |
| 203 | self.io_cq = prp; |
| 204 | self.io_depth = ((cdw10 >> 16) & 0xffff) as usize + 1; |
| 205 | } |
| 206 | 0x01 if cdw10 & 0xffff == 1 && cdw11 & 0xffff == 1 => { |
| 207 | // Create I/O SQ: remember buffer (QID 1). |
| 208 | self.io_sq = prp; |
| 209 | } |
| 210 | _ => {} |
| 211 | } |
| 212 | |
| 213 | { |
| 214 | let cqe = memory.get_mut(&admin_cq).expect("admin CQ allocated"); |
| 215 | let at = self.admin_cq_tail * 16; |
| 216 | cqe[at + 12..at + 14].copy_from_slice(&cid.to_le_bytes()); |
| 217 | cqe[at + 14..at + 16] |
| 218 | .copy_from_slice(&((self.admin_phase as u16) | self.command_error).to_le_bytes()); |
| 219 | } |
| 220 | self.admin_cq_tail = (self.admin_cq_tail + 1) % ADMIN_DEPTH; |
| 221 | if self.admin_cq_tail == 0 { |
| 222 | self.admin_phase = !self.admin_phase; |
| 223 | } |
| 224 | self.admin_sq_tail = (self.admin_sq_tail + 1) % ADMIN_DEPTH; |
| 225 | } |
| 226 | |
| 227 | fn run_io(&mut self) { |
| 228 | let io_sq = self.io_sq; |
| 229 | let io_cq = self.io_cq; |
| 230 | let mut memory = self.store.borrow_mut(); |
| 231 | |
| 232 | let at = self.io_sq_tail * 64; |
| 233 | let command = memory |
| 234 | .get(&io_sq) |
| 235 | .expect("I/O SQ allocated") |
| 236 | .get(at..at + 64) |
| 237 | .expect("I/O SQE in range") |
| 238 | .to_vec(); |
| 239 | let cid = u16::from_le_bytes(command[2..4].try_into().unwrap()); |
| 240 | |
| 241 | self.command_error = 0; |
| 242 | |
| 243 | let cqe = memory.get_mut(&io_cq).expect("I/O CQ allocated"); |
| 244 | let cq_at = self.io_cq_tail * 16; |
| 245 | cqe[cq_at + 12..cq_at + 14].copy_from_slice(&cid.to_le_bytes()); |
| 246 | cqe[cq_at + 14..cq_at + 16] |
| 247 | .copy_from_slice(&((self.io_phase as u16) | self.command_error).to_le_bytes()); |
| 248 | |
| 249 | self.io_cq_tail = (self.io_cq_tail + 1) % self.io_depth; |
| 250 | if self.io_cq_tail == 0 { |
| 251 | self.io_phase = !self.io_phase; |
| 252 | } |
| 253 | self.io_sq_tail = (self.io_sq_tail + 1) % self.io_depth; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #[derive(Default)] |
| 258 | struct OracleDelay { |
| 259 | waited_us: u64, |
| 260 | } |
| 261 | |
| 262 | impl Delay for OracleDelay { |
| 263 | fn delay_us(&mut self, us: u32) { |
| 264 | self.waited_us += u64::from(us); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | fn oracle(namespace_size: u64) -> (OracleRegs, OracleDma) { |
| 269 | let store: Store = Rc::new(RefCell::new(BTreeMap::new())); |
| 270 | ( |
| 271 | OracleRegs { |
| 272 | store: store.clone(), |
| 273 | cc: 0, |
| 274 | writes: Vec::new(), |
| 275 | namespace_size, |
| 276 | flbas: 0, |
| 277 | lbads: 9, |
| 278 | mdts: 0, |
| 279 | nsid_zero_error: false, |
| 280 | command_error: 0, |
| 281 | admin_sq: 0, |
| 282 | admin_cq: 0, |
| 283 | admin_sq_tail: 0, |
| 284 | admin_cq_tail: 0, |
| 285 | admin_phase: true, |
| 286 | io_sq: 0, |
| 287 | io_cq: 0, |
| 288 | io_sq_tail: 0, |
| 289 | io_cq_tail: 0, |
| 290 | io_phase: true, |
| 291 | io_depth: IO_DEPTH, |
| 292 | }, |
| 293 | OracleDma { |
| 294 | store, |
| 295 | next: 0x1000, |
| 296 | }, |
| 297 | ) |
| 298 | } |
| 299 | |
| 300 | type Ctrl = NvmeController<OracleRegs, OracleDma, OracleDelay>; |
| 301 | |
| 302 | fn io_submissions(c: &Ctrl) -> usize { |
| 303 | c.registers |
| 304 | .writes |
| 305 | .iter() |
| 306 | .filter(|(offset, _)| *offset == IO_SQ_DB) |
| 307 | .count() |
| 308 | } |
| 309 | |
| 310 | // --------------------------------------------------------------------------- |
| 311 | // MDTS (Identify Controller, CNS=1): observable via transfer chunk count. |
| 312 | // MDTS is not publicly readable, so byte 77 is asserted through chunking. |
| 313 | // --------------------------------------------------------------------------- |
| 314 | |
| 315 | #[test] |
| 316 | fn mdts_byte_77_controls_transfer_chunking() { |
| 317 | // MDTS = 0 -> 128 KiB cap -> a 32 KiB write is one command. |
| 318 | let (regs, dma) = oracle(12345); |
| 319 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 320 | c.create_io_queues(IO_DEPTH as u16).unwrap(); |
| 321 | let data = vec![0u8; 32 * 1024]; |
| 322 | c.write_blocks(0, &data).unwrap(); |
| 323 | assert_eq!( |
| 324 | io_submissions(&c), |
| 325 | 1, |
| 326 | "MDTS=0 should allow one 32 KiB command" |
| 327 | ); |
| 328 | |
| 329 | // MDTS = 1 -> PAGE_SIZE << 1 = 8 KiB per command -> four commands. |
| 330 | let (mut regs, dma) = oracle(12345); |
| 331 | regs.mdts = 1; |
| 332 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 333 | c.create_io_queues(IO_DEPTH as u16).unwrap(); |
| 334 | c.write_blocks(0, &data).unwrap(); |
| 335 | assert_eq!( |
| 336 | io_submissions(&c), |
| 337 | 4, |
| 338 | "MDTS=1 should split 32 KiB into 8 KiB chunks" |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | // --------------------------------------------------------------------------- |
| 343 | // Capacity round-trip after init (init runs Identify Controller then Namespace). |
| 344 | // --------------------------------------------------------------------------- |
| 345 | |
| 346 | #[test] |
| 347 | fn init_identifies_namespace_and_capacity_round_trips() { |
| 348 | let (regs, dma) = oracle(4096); |
| 349 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 350 | assert_eq!( |
| 351 | c.identify_namespace(1).unwrap(), |
| 352 | NamespaceInfo { |
| 353 | block_count: 4096, |
| 354 | block_size: 512, |
| 355 | } |
| 356 | ); |
| 357 | assert_eq!(c.block_count(), 4096, "BlockDevice capacity after init"); |
| 358 | assert_eq!(c.block_size(), 512); |
| 359 | } |
| 360 | |
| 361 | // --------------------------------------------------------------------------- |
| 362 | // Invalid namespace geometries. |
| 363 | // --------------------------------------------------------------------------- |
| 364 | |
| 365 | #[test] |
| 366 | fn zero_block_count_is_invalid_identify() { |
| 367 | let (regs, dma) = oracle(12345); |
| 368 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 369 | c.registers.namespace_size = 0; |
| 370 | assert_eq!(c.identify_namespace(1), Err(NvmeError::InvalidIdentify)); |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn lbads_at_or_above_32_is_invalid_identify() { |
| 375 | let (regs, dma) = oracle(12345); |
| 376 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 377 | for bad in [32u8, 33, 64, 255] { |
| 378 | c.registers.lbads = bad; |
| 379 | assert_eq!( |
| 380 | c.identify_namespace(1), |
| 381 | Err(NvmeError::InvalidIdentify), |
| 382 | "lbads {bad} must be rejected" |
| 383 | ); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | #[test] |
| 388 | fn lbads_9_and_12_map_to_block_sizes() { |
| 389 | let (regs, dma) = oracle(12345); |
| 390 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 391 | |
| 392 | c.registers.lbads = 9; |
| 393 | assert_eq!(c.identify_namespace(1).unwrap().block_size, 512); |
| 394 | |
| 395 | c.registers.lbads = 12; |
| 396 | assert_eq!(c.identify_namespace(1).unwrap().block_size, 4096); |
| 397 | } |
| 398 | |
| 399 | // --------------------------------------------------------------------------- |
| 400 | // FLBAS selects a nonzero LBA-format descriptor. |
| 401 | // --------------------------------------------------------------------------- |
| 402 | |
| 403 | #[test] |
| 404 | fn nonzero_flbas_selects_its_own_descriptor() { |
| 405 | let (regs, dma) = oracle(12345); |
| 406 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 407 | |
| 408 | // Descriptor 0 is a decoy (lbads 9 -> 512). Selecting index 2 with lbads 12 |
| 409 | // must yield 4096, proving descriptor 128 + flbas*4 + 2 is read. |
| 410 | c.registers.flbas = 2; |
| 411 | c.registers.lbads = 12; |
| 412 | assert_eq!( |
| 413 | c.identify_namespace(1).unwrap(), |
| 414 | NamespaceInfo { |
| 415 | block_count: 12345, |
| 416 | block_size: 4096, |
| 417 | } |
| 418 | ); |
| 419 | |
| 420 | // Back to descriptor 0. |
| 421 | c.registers.flbas = 0; |
| 422 | c.registers.lbads = 9; |
| 423 | assert_eq!(c.identify_namespace(1).unwrap().block_size, 512); |
| 424 | } |
| 425 | |
| 426 | #[test] |
| 427 | fn maximum_flbas_descriptor_offset_stays_in_bounds() { |
| 428 | // FLBAS is masked with 0x0f, so the largest descriptor is 128 + 15*4 + 2 = 190, |
| 429 | // always inside the 4096-byte identify buffer. Assert the boundary is safe. |
| 430 | let (regs, dma) = oracle(12345); |
| 431 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 432 | c.registers.flbas = 0xff; // low nibble 15 |
| 433 | c.registers.lbads = 12; |
| 434 | assert_eq!(c.identify_namespace(1).unwrap().block_size, 4096); |
| 435 | } |
| 436 | |
| 437 | // --------------------------------------------------------------------------- |
| 438 | // Unsupported geometry must not panic. |
| 439 | // --------------------------------------------------------------------------- |
| 440 | |
| 441 | #[test] |
| 442 | fn nsid_zero_identify_does_not_panic() { |
| 443 | let (regs, dma) = oracle(12345); |
| 444 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 445 | |
| 446 | // NSID 0 is not a valid target namespace; surface an error, never panic. |
| 447 | c.registers.nsid_zero_error = true; |
| 448 | assert!(matches!( |
| 449 | c.identify_namespace(0), |
| 450 | Err(NvmeError::CompletionStatus { sc: 1, .. }) |
| 451 | )); |
| 452 | } |
| 453 | |
| 454 | #[test] |
| 455 | fn mdts_255_shift_does_not_panic() { |
| 456 | // MDTS = 255 would make PAGE_SIZE.checked_shl(255) overflow; the driver must |
| 457 | // fall back (128 KiB) and complete the transfer without panicking. |
| 458 | let (mut regs, dma) = oracle(12345); |
| 459 | regs.mdts = 255; |
| 460 | let mut c: Ctrl = NvmeController::init(regs, dma, OracleDelay::default()).unwrap(); |
| 461 | c.create_io_queues(IO_DEPTH as u16).unwrap(); |
| 462 | let mut buffer = vec![0u8; 512]; |
| 463 | c.read_blocks(0, &mut buffer).unwrap(); |
| 464 | } |