git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/check.rs
//! The self-checks that hold on every architecture: memory and the
//! cryptography instructions. The rest are in `selfcheck`, ARM-only until
//! the PC runs tasks.
use crate::mm::{PAGE, pages};
use crate::{println, timer};
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
/// The page allocator gives aligned blocks of every order and merges them
/// back on free; the heap serves `alloc`'s collections, small and large.
pub fn memory() {
let before = pages::stats();
let mut blocks = [(0, 0); 2 * (pages::MAX_ORDER + 1)];
for (i, block) in blocks.iter_mut().enumerate() {
let order = i / 2;
let address = pages::alloc(order).expect("out of pages");
assert!(
address.is_multiple_of(PAGE << order),
"a block of order {order} is misaligned"
);
*block = (address, order);
}
for &(address, order) in blocks.iter().rev() {
pages::free(address, order);
}
assert!(pages::stats() == before, "freed blocks did not merge back");
let numbers: Vec<u64> = (0..100_000).collect();
assert_eq!(numbers.iter().sum::<u64>(), 99_999 * 100_000 / 2);
let boxes: Vec<Box<[u8; 48]>> = (0..1000).map(|i| Box::new([i as u8; 48])).collect();
assert!(boxes.iter().enumerate().all(|(i, b)| b[47] == i as u8));
let mut names = BTreeMap::new();
for i in 0..1000u32 {
names.insert(i, String::from("task") + &i.to_string());
}
assert_eq!(names[&999], "task999");
drop((numbers, boxes, names));
let (free, largest) = pages::stats();
let total = crate::mm::ram_size() / PAGE;
println!(
"memory: {} of {} MiB free, {largest} free blocks of {} MiB",
(free * PAGE) >> 20,
(total * PAGE) >> 20,
(PAGE << pages::MAX_ORDER) >> 20,
);
println!("memory ok");
}
/// A PC's interrupts: a breakpoint comes back through the table, and the
/// APIC's timer ticks at its rate.
#[cfg(target_arch = "x86_64")]
pub fn interrupts() {
// SAFETY: a breakpoint, which the table's handler counts and returns
// from.
unsafe { core::arch::asm!("int3") };
let breakpoints = crate::exceptions::BREAKPOINTS.load(core::sync::atomic::Ordering::Relaxed);
assert_eq!(breakpoints, 1, "the breakpoint did not come back");
let (start, ticks) = (timer::now_ms(), timer::ticks());
while timer::now_ms() < start + 200 {
crate::arch::wait_for_interrupt();
}
let ticked = timer::ticks() - ticks;
println!(
"interrupts: a breakpoint handled; {ticked} ticks of the APIC timer in 200 ms ({} counts a ms)",
crate::apic::timer_rate()
);
assert!(
(10..=30).contains(&ticked),
"the APIC timer ticked {ticked} times in 200 ms, not about {}",
timer::HZ / 5
);
println!("interrupts ok");
}
/// The random device's interrupt came: what it asked for arrived through
/// the interrupt controller, not only by polling.
pub fn device_interrupt() {
use core::sync::atomic::Ordering;
let start = timer::now_ms();
while crate::drivers::random::INTERRUPTS.load(Ordering::Relaxed) == 0 && timer::now_ms() < start + 500 {
crate::arch::wait_for_interrupt();
}
let taken = crate::drivers::random::INTERRUPTS.load(Ordering::Relaxed);
assert!(taken > 0, "the random device's interrupt never came");
println!("interrupts: the random device's arrived ({taken})");
}
/// SHA-256 on the core's own instructions, when it has them: the same
/// digests as the software's, at the block's edges and over 256 KiB,
/// and how much quicker.
pub fn crypto() {
use pinnacle_crypto::sha2::Sha256;
if !pinnacle_crypto::hw::sha256() {
println!("crypto: no SHA-256 instructions on this core: in software");
println!("crypto ok");
return;
}
let data: Vec<u8> = (0..1u32 << 18).map(|i| (i * 31 + i / 7) as u8).collect();
for len in [0, 1, 55, 56, 63, 64, 65, 119, 120, 1000, 4096, 4097] {
assert_eq!(
Sha256::digest(&data[..len]),
Sha256::digest_in_software(&data[..len]),
"SHA-256 instructions gave another digest for {len} bytes"
);
}
let start = timer::now_ms();
let hardware = Sha256::digest(&data);
let middle = timer::now_ms();
let software = Sha256::digest_in_software(&data);
let end = timer::now_ms();
assert_eq!(
hardware, software,
"SHA-256 instructions gave another digest for 256 KiB"
);
println!(
"crypto: SHA-256 on the core's instructions: 256 KiB in {} ms, in software {} ms",
middle - start,
end - middle
);
aes();
println!("crypto ok");
}
/// AES on the core's instructions, where it has them, against the
/// software, with both key sizes over 1 000 blocks.
fn aes() {
use pinnacle_crypto::aes::Aes;
if !pinnacle_crypto::hw::aes() {
println!("crypto: no AES instructions on this core: in software");
return;
}
for key_len in [16, 32] {
let key: Vec<u8> = (0..key_len).map(|i| (i * 7 + 1) as u8).collect();
let aes = Aes::new(&key).expect("an AES key");
let mut hardware: Vec<u8> = (0..16 * 1000u32).map(|i| (i * 13 + i / 5) as u8).collect();
let mut software = hardware.clone();
aes.encrypt_blocks(&mut hardware);
aes.encrypt_blocks_in_software(&mut software);
assert!(
hardware == software,
"AES instructions gave other blocks, {key_len}-byte key"
);
}
println!("crypto: AES on the core's instructions, AES-128 and AES-256 as the software's");
}