use turkos_percpu_data::percpu::*; fn registry(id: u64) -> Registry { unsafe { Registry::new(id, 0x2025, &[(0, 0x4fff0000), (1, 0x4fff1000), (2, 0x4fff2000), (3, 0x4fff3000)], 0x40000000, 0x50000000, 0x40090000) }.unwrap() } fn worked(mut area: Area, prior: u64, active: u64) -> Area { area.counter = area.updates; area.sum = area.updates * (area.updates + 1) / 2; area.prior_tpidr = prior; area.active = active; area } #[test] fn os_percpu_data_positive() { let mut r = registry(1); let old = r.prepare(0, 17).unwrap(); let stale = unsafe { core::ptr::read(&old.ticket) }; unsafe { r.abort_unstarted_from_kernel(old.ticket) }.unwrap(); for cpu in 0..4 { let p = r.prepare(cpu, (cpu as u64 + 1) * 17).unwrap(); let before = p.area.bytes(); assert_eq!(u64::from_le_bytes(before[32..40].try_into().unwrap()), cpu as u64); assert_eq!(u64::from_le_bytes(before[40..48].try_into().unwrap()), 0x4fff0000 + cpu as u64 * 4096); assert_eq!(r.close(), Err(Error::Busy)); let area = worked(p.area, 0x77 + cpu as u64, 1); let bound = unsafe { r.bind_from_kernel(p.ticket, &area.bytes(), cpu as u64, area.physical, area.prior_tpidr) }.unwrap(); assert_eq!(r.close(), Err(Error::Busy)); let detached = worked(area, area.prior_tpidr, 0); unsafe { r.detach_from_kernel(bound, &detached.bytes(), cpu as u64, area.prior_tpidr) }.unwrap(); } let next = r.prepare(0, 1).unwrap(); let area = worked(next.area, 0, 1); assert_eq!(unsafe { r.bind_from_kernel(stale, &area.bytes(), 0, area.physical, 0) }.err(), Some(Error::Ticket)); unsafe { r.abort_unstarted_from_kernel(next.ticket) }.unwrap(); let s = r.snapshot(); assert_eq!((s.state.published, s.state.bound, s.state.detached, s.state.aborted), (6, 4, 4, 2)); r.close().unwrap(); assert_eq!(r.prepare(0, 1).err().map(|e| e), Some(Error::State)); assert_eq!(r.close(), Err(Error::State)); } unsafe fn ticket_copy(ticket: &Ticket) -> Ticket { unsafe { core::ptr::read(ticket) } } unsafe fn bound_copy(bound: &Bound) -> Bound { unsafe { core::ptr::read(bound) } } #[test] fn os_percpu_data_negative() { let mut r = registry(2); let p = r.prepare(1, 34).unwrap(); let area = worked(p.area, 0x99, 1); let bytes = area.bytes(); let before = r.snapshot(); let mut other = registry(3); let foreign = other.prepare(1, 34).unwrap(); assert_eq!(unsafe { r.bind_from_kernel(foreign.ticket, &bytes, 1, area.physical, 0x99) }.err(), Some(Error::Ticket)); assert_eq!(unsafe { r.bind_from_kernel(ticket_copy(&p.ticket), &bytes, 2, area.physical, 0x99) }.err(), Some(Error::Cpu)); assert_eq!(unsafe { r.bind_from_kernel(ticket_copy(&p.ticket), &bytes, 1, area.physical + 4096, 0x99) }.err(), Some(Error::Register)); assert_eq!(unsafe { r.bind_from_kernel(ticket_copy(&p.ticket), &bytes, 1, area.physical, area.physical) }.err(), Some(Error::Register)); assert_eq!(unsafe { r.bind_from_kernel(ticket_copy(&p.ticket), &bytes, 1, area.physical, 0x4fff2000) }.err(), Some(Error::Register)); for i in 0..128 { let mut bad = bytes; bad[i] ^= 1; assert_eq!(unsafe { r.bind_from_kernel(ticket_copy(&p.ticket), &bad, 1, area.physical, 0x99) }.err(), Some(Error::Payload)); assert_eq!(r.snapshot(), before); } for n in [0, 1, 127] { assert_eq!(unsafe { r.bind_from_kernel(ticket_copy(&p.ticket), &bytes[..n], 1, area.physical, 0x99) }.err(), Some(Error::Payload)); } let bound = unsafe { r.bind_from_kernel(p.ticket, &bytes, 1, area.physical, 0x99) }.unwrap(); let before = r.snapshot(); let detached = worked(area, 0x99, 0).bytes(); assert_eq!(unsafe { r.detach_from_kernel(bound_copy(&bound), &detached, 2, 0x99) }, Err(Error::Cpu)); assert_eq!(unsafe { r.detach_from_kernel(bound_copy(&bound), &detached, 1, area.physical) }, Err(Error::Register)); assert_eq!(unsafe { r.detach_from_kernel(bound_copy(&bound), &bytes, 1, 0x99) }, Err(Error::Payload)); assert_eq!(r.snapshot(), before); unsafe { r.detach_from_kernel(bound, &detached, 1, 0x99) }.unwrap(); } #[test] fn os_percpu_data_boundaries() { let mut r = registry(4); let before = r.snapshot(); for cpu in [4, 8, usize::MAX] { assert_eq!(r.prepare(cpu, 1).err(), Some(Error::Cpu)); } for n in [0, 1025, u64::MAX] { assert_eq!(r.prepare(0, n).err(), Some(Error::Updates)); } assert_eq!(r.snapshot(), before); let p = r.prepare(0, MAX_UPDATES).unwrap(); assert_eq!(r.prepare(0, 1).err(), Some(Error::Busy)); let area = worked(p.area, 0, 1); assert_eq!(area.sum, 524800); let bound = unsafe { r.bind_from_kernel(p.ticket, &area.bytes(), 0x80000000, area.physical, 0) }.unwrap(); let detached = worked(area, 0, 0); unsafe { r.detach_from_kernel(bound, &detached.bytes(), 0, 0) }.unwrap(); for cpus in [&[(0, 0x4fff0000), (0, 0x4fff1000)][..], &[(0, 0x4fff0000), (1, 0x4fff0000)][..]] { assert_eq!(unsafe { Registry::new(1, 1, cpus, 0x40000000, 0x50000000, 0x40090000) }.err(), Some(Error::Duplicate)); } for physical in [0, 0x50000000, 0x4ffff001, 0x40090000] { assert_eq!(unsafe { Registry::new(1, 1, &[(0, physical)], 0x40000000, 0x50000000, 0x40090000) }.err(), Some(Error::Address)); } assert_eq!(unsafe { Registry::new(1, 1, &[(AFFINITY_MASK + 1, 0x4fff0000)], 0x40000000, 0x50000000, 0x40090000) }.err(), Some(Error::Cpu)); let cpus: Vec<_> = (0..8).map(|i| (i, 0x4fff0000 + i * 4096)).collect(); let mut full = unsafe { Registry::new(1, 1, &cpus, 0x40000000, 0x50000000, 0x40090000) }.unwrap(); let last = full.prepare(7, 1).unwrap(); unsafe { full.abort_unstarted_from_kernel(last.ticket) }.unwrap(); full.close().unwrap(); let nine: Vec<_> = (0..9).map(|i| (i, 0x4fff0000 + i * 4096)).collect(); for cpus in [&[][..], &nine[..]] { assert_eq!(unsafe { Registry::new(1, 1, cpus, 0x40000000, 0x50000000, 0x40090000) }.err(), Some(Error::Configuration)); } }