A refresh of a copy on the card sends the dwords title memory changed, and no others (VICTORY_SEND_CHANGED, on)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0b3af7939c
commit
61cbfe3ea0
3 files changed
+127
-208
No files matched your search
@@ -75,46 +75,6 @@ std::arch::global_asm!(
|
||||
"vzeroupper",
|
||||
"xor eax, eax",
|
||||
"ret",
|
||||
".globl victory_safe_update",
|
||||
".globl victory_safe_update_at",
|
||||
".globl victory_safe_update_after",
|
||||
".globl victory_safe_update_failed",
|
||||
// rcx destination, rdx source, r8 bytes (whole 64s, not none), r9 where
|
||||
// the offset of the first 64 bytes that differed goes (-1: none did) and
|
||||
// behind it the offset of the end of the last: stores the 64s that
|
||||
// differ. eax: whether all could be read.
|
||||
"victory_safe_update:",
|
||||
"xor r10d, r10d",
|
||||
"mov r11, -1",
|
||||
"xor eax, eax",
|
||||
"2:",
|
||||
"victory_safe_update_at:",
|
||||
"vmovdqu ymm0, ymmword ptr [rdx + r10]",
|
||||
"vmovdqu ymm1, ymmword ptr [rdx + r10 + 32]",
|
||||
"victory_safe_update_after:",
|
||||
"vpxor ymm2, ymm0, ymmword ptr [rcx + r10]",
|
||||
"vpxor ymm3, ymm1, ymmword ptr [rcx + r10 + 32]",
|
||||
"vpor ymm2, ymm2, ymm3",
|
||||
"vptest ymm2, ymm2",
|
||||
"jz 3f",
|
||||
"vmovdqu ymmword ptr [rcx + r10], ymm0",
|
||||
"vmovdqu ymmword ptr [rcx + r10 + 32], ymm1",
|
||||
"cmp r11, -1",
|
||||
"cmove r11, r10",
|
||||
"lea rax, [r10 + 64]",
|
||||
"3:",
|
||||
"add r10, 64",
|
||||
"cmp r10, r8",
|
||||
"jb 2b",
|
||||
"mov qword ptr [r9], r11",
|
||||
"mov qword ptr [r9 + 8], rax",
|
||||
"vzeroupper",
|
||||
"mov eax, 1",
|
||||
"ret",
|
||||
"victory_safe_update_failed:",
|
||||
"vzeroupper",
|
||||
"xor eax, eax",
|
||||
"ret",
|
||||
".globl victory_safe_merge",
|
||||
".globl victory_safe_merge_at",
|
||||
".globl victory_safe_merge_after",
|
||||
@@ -185,63 +145,12 @@ unsafe extern "system" {
|
||||
fn victory_safe_put_at();
|
||||
fn victory_safe_put_after();
|
||||
fn victory_safe_put_failed();
|
||||
fn victory_safe_update(dst: *mut u8, src: *const u8, len: usize, changed: *mut [usize; 2]) -> u32;
|
||||
fn victory_safe_update_at();
|
||||
fn victory_safe_update_after();
|
||||
fn victory_safe_update_failed();
|
||||
fn victory_safe_merge(title: *mut u8, now: *const u8, held: *mut u8, blocks: usize, counts: *mut [u64; 2]) -> usize;
|
||||
fn victory_safe_merge_at();
|
||||
fn victory_safe_merge_after();
|
||||
fn victory_safe_merge_failed();
|
||||
}
|
||||
|
||||
/// What [`update`] found.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Updated {
|
||||
/// Every byte was the same already; nothing was stored.
|
||||
Same,
|
||||
/// The bytes that changed lie in these offsets.
|
||||
Changed(usize, usize),
|
||||
/// Not all of the source could be read; some may have been copied.
|
||||
Failed,
|
||||
}
|
||||
|
||||
/// Makes `dst` hold what `src` holds, storing only what differs. The source
|
||||
/// may change meanwhile: what is compared is what is stored, read once.
|
||||
///
|
||||
/// # Safety
|
||||
/// As [`copy`]'s, and the destination is memory of ours that nothing else
|
||||
/// writes.
|
||||
pub(crate) unsafe fn update(dst: *mut u8, src: *const u8, len: usize) -> Updated {
|
||||
let whole = len / 64 * 64;
|
||||
let (mut from, mut to) = (usize::MAX, 0);
|
||||
if whole > 0 {
|
||||
let mut changed = [usize::MAX, 0];
|
||||
// SAFETY: the source's faults leave through the failing exit; the
|
||||
// rest is the caller's.
|
||||
if unsafe { victory_safe_update(dst, src, whole, &mut changed) } == 0 {
|
||||
return Updated::Failed;
|
||||
}
|
||||
[from, to] = changed;
|
||||
}
|
||||
if whole < len {
|
||||
let mut rest = [0u8; 64];
|
||||
let rest = &mut rest[..len - whole];
|
||||
// SAFETY: as above; `rest` is ours.
|
||||
unsafe {
|
||||
if !copy(rest.as_mut_ptr(), src.add(whole), rest.len()) {
|
||||
return Updated::Failed;
|
||||
}
|
||||
let held = std::slice::from_raw_parts_mut(dst.add(whole), rest.len());
|
||||
if held != rest {
|
||||
held.copy_from_slice(rest);
|
||||
(from, to) = (from.min(whole), len);
|
||||
}
|
||||
}
|
||||
}
|
||||
if from >= to { Updated::Same } else { Updated::Changed(from, to) }
|
||||
}
|
||||
|
||||
/// What [`merge`] did.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub(crate) struct Merged {
|
||||
@@ -445,10 +354,6 @@ pub(crate) fn failing_exit(at: usize) -> Option<usize> {
|
||||
if (from..to).contains(&at) {
|
||||
return Some(victory_safe_put_failed as *const () as usize);
|
||||
}
|
||||
let (from, to) = (victory_safe_update_at as *const () as usize, victory_safe_update_after as *const () as usize);
|
||||
if (from..to).contains(&at) {
|
||||
return Some(victory_safe_update_failed as *const () as usize);
|
||||
}
|
||||
let (from, to) = (victory_safe_merge_at as *const () as usize, victory_safe_merge_after as *const () as usize);
|
||||
(from..to).contains(&at).then_some(victory_safe_merge_failed as *const () as usize)
|
||||
}
|
||||
@@ -504,52 +409,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// An update makes the two the same, says where they differed (to 64
|
||||
/// bytes), stores nothing where they did not, and fails at a page that
|
||||
/// is not there.
|
||||
#[test]
|
||||
fn updates_store_what_differs() {
|
||||
assert!(on());
|
||||
let (src, dst) = (Pages::new(1 << 14), Pages::new(1 << 14));
|
||||
for len in [0usize, 1, 63, 64, 65, 200, 4096, 5000] {
|
||||
for (da, sa) in [(0usize, 0usize), (3, 0), (0, 5)] {
|
||||
for changed in [vec![], vec![0usize], vec![len / 2], vec![len.saturating_sub(1)], vec![1, len / 3, len / 2]] {
|
||||
src.fill();
|
||||
// SAFETY: inside the two allocations.
|
||||
unsafe {
|
||||
std::ptr::write_bytes(dst.0, 0xEE, dst.1);
|
||||
std::ptr::copy_nonoverlapping(src.0.add(sa), dst.0.add(da), len);
|
||||
let at: std::collections::BTreeSet<usize> = changed.iter().copied().filter(|&i| i < len).collect();
|
||||
for &i in &at {
|
||||
*src.0.add(sa + i) ^= 0x40;
|
||||
}
|
||||
let found = update(dst.0.add(da), src.0.add(sa), len);
|
||||
match (at.iter().min(), at.iter().max()) {
|
||||
(Some(&first), Some(&last)) => {
|
||||
let Updated::Changed(from, to) = found else {
|
||||
panic!("{len} {at:?}: {found:?}")
|
||||
};
|
||||
assert_eq!(from, first / 64 * 64);
|
||||
assert_eq!(to, ((last / 64 + 1) * 64).min(len));
|
||||
}
|
||||
_ => assert_eq!(found, Updated::Same),
|
||||
}
|
||||
let (to, from) = (std::slice::from_raw_parts(dst.0, dst.1), std::slice::from_raw_parts(src.0, src.1));
|
||||
assert_eq!(to[da..da + len], from[sa..sa + len]);
|
||||
assert!(to[..da].iter().chain(&to[da + len..]).all(|&x| x == 0xEE));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// SAFETY: our allocation.
|
||||
unsafe {
|
||||
VirtualFree(src.0.add(4096).cast(), 4096, MEM_DECOMMIT);
|
||||
assert_eq!(update(dst.0, src.0, 8192), Updated::Failed);
|
||||
assert_eq!(update(dst.0, src.0.add(4096), 10), Updated::Failed);
|
||||
assert_ne!(update(dst.0, src.0, 4096), Updated::Failed);
|
||||
}
|
||||
}
|
||||
|
||||
/// The dwords that changed are stored, and no byte of any other, at
|
||||
/// every alignment and length; a page that is not there fails.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user