Skip to content

Commit 144de74

Browse files
WhatAmISupposedToPutHerejannau
authored andcommitted
rust: io: Add helper for memcpy_toio
Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com> Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 87ae7db commit 144de74

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

rust/helpers/io.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ void rust_helper_memcpy_fromio(void *to, const void __iomem *from, long count)
2323
memcpy_fromio(to, from, count);
2424
}
2525

26+
void rust_helper_memcpy_toio(void __iomem *to, const void *from, size_t count)
27+
{
28+
memcpy_toio(to, from, count);
29+
}
30+
2631
u8 rust_helper_readb(const void __iomem *addr)
2732
{
2833
return readb(addr);

rust/kernel/io.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,27 @@ impl<const SIZE: usize> Io<SIZE> {
272272
Ok(())
273273
}
274274

275+
/// Copy memory block to i/o memory from the specified buffer.
276+
pub fn try_memcpy_toio(&self, offset: usize, buffer: &[u8]) -> Result {
277+
if buffer.len() == 0 || !Self::length_valid(offset, buffer.len(), self.maxsize()) {
278+
return Err(EINVAL);
279+
}
280+
// no need to check since offset + buffer.len() - 1 is valid
281+
let addr = self.io_addr::<crate::ffi::c_char>(offset)?;
282+
283+
// SAFETY:
284+
// - The type invariants guarantee that `adr` is a valid pointer.
285+
// - The bounds of `buffer` are checked with a call to `length_valid`.
286+
unsafe {
287+
bindings::memcpy_toio(
288+
addr as *mut _,
289+
buffer.as_ptr() as *const _,
290+
buffer.len() as _,
291+
)
292+
};
293+
Ok(())
294+
}
295+
275296
define_read!(read8, try_read8, readb -> u8);
276297
define_read!(read16, try_read16, readw -> u16);
277298
define_read!(read32, try_read32, readl -> u32);

0 commit comments

Comments
 (0)