asterinas/services/libs/aster-std/src/util/mod.rs

39 lines
1.4 KiB
Rust
Raw Normal View History

2023-07-31 11:22:33 +00:00
use crate::prelude::*;
2023-12-25 03:12:25 +00:00
use aster_frame::vm::VmIo;
2023-05-31 02:46:51 +00:00
pub mod net;
/// copy bytes from user space of current process. The bytes len is the len of dest.
pub fn read_bytes_from_user(src: Vaddr, dest: &mut [u8]) -> Result<()> {
let current = current!();
2022-12-20 06:12:22 +00:00
let root_vmar = current.root_vmar();
Ok(root_vmar.read_bytes(src, dest)?)
}
/// copy val (Plain of Data type) from user space of current process.
pub fn read_val_from_user<T: Pod>(src: Vaddr) -> Result<T> {
let current = current!();
2022-12-20 06:12:22 +00:00
let root_vmar = current.root_vmar();
Ok(root_vmar.read_val(src)?)
}
/// write bytes from user space of current process. The bytes len is the len of src.
pub fn write_bytes_to_user(dest: Vaddr, src: &[u8]) -> Result<()> {
let current = current!();
2022-12-20 06:12:22 +00:00
let root_vmar = current.root_vmar();
Ok(root_vmar.write_bytes(dest, src)?)
}
/// write val (Plain of Data type) to user space of current process.
pub fn write_val_to_user<T: Pod>(dest: Vaddr, val: &T) -> Result<()> {
let current = current!();
2022-12-20 06:12:22 +00:00
let root_vmar = current.root_vmar();
Ok(root_vmar.write_val(dest, val)?)
}
/// read a cstring from user, the length of cstring should not exceed max_len(include null byte)
pub fn read_cstring_from_user(addr: Vaddr, max_len: usize) -> Result<CString> {
let mut buffer = vec![0u8; max_len];
read_bytes_from_user(addr, &mut buffer)?;
Ok(CString::from(CStr::from_bytes_until_nul(&buffer)?))
}