Skip to content

Commit 066ebdb

Browse files
committed
update starry-vm to 0.2
1 parent 3ccbc31 commit 066ebdb

File tree

7 files changed

+117
-34
lines changed

7 files changed

+117
-34
lines changed

Cargo.lock

Lines changed: 10 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ slab = { version = "0.4.9", default-features = false }
6969
spin = "0.10"
7070
starry-process = "0.2"
7171
starry-signal = "0.2"
72-
starry-vm = "0.1"
72+
starry-vm = "0.2"
7373

7474
starry-core = { path = "./core" }
7575
starry-api = { path = "./api" }
@@ -154,8 +154,8 @@ axpoll = { git = "https://github.com/Starry-OS/axpoll.git", rev = "258cb5f" }
154154
page_table_entry = { git = "https://github.com/Starry-OS/page_table_multiarch.git", rev = "3f697c8" }
155155
page_table_multiarch = { git = "https://github.com/Starry-OS/page_table_multiarch.git", rev = "3f697c8" }
156156
starry-process = { git = "https://github.com/Starry-OS/starry-process.git", rev = "6327455" }
157-
starry-signal = { git = "https://github.com/Starry-OS/starry-signal.git", rev = "6cc32f2" }
158-
starry-vm = { git = "https://github.com/Starry-OS/starry-vm.git", rev = "e2a50e7" }
157+
starry-signal = { git = "https://github.com/Starry-OS/starry-signal.git", rev = "5b23dec" }
158+
starry-vm = { git = "https://github.com/Starry-OS/starry-vm.git", rev = "4c91a6b" }
159159

160160
[package.metadata.vendor-filter]
161161
platforms = ["riscv64gc-unknown-none-elf", "loongarch64-unknown-none-softfloat"]

api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ slab.workspace = true
5757
spin.workspace = true
5858
starry-process.workspace = true
5959
starry-signal.workspace = true
60-
starry-vm = { workspace = true, features = ["axio"] }
60+
starry-vm.workspace = true
6161
syscalls = { git = "https://github.com/jasonwhite/syscalls.git", rev = "92624de", default-features = false }
6262
zerocopy = { version = "0.8.26", features = ["derive"] }
6363

api/src/file/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ use inherit_methods_macro::inherit_methods;
1919
use linux_raw_sys::general::{RLIMIT_NOFILE, stat, statx, statx_timestamp};
2020
use spin::RwLock;
2121
use starry_core::{resources::AX_FILE_LIMIT, task::AsThread};
22-
use starry_vm::{VmBytes, VmBytesMut};
2322

2423
pub use self::{
2524
fs::{Directory, File, ResolveAtResult, metadata_to_kstat, resolve_at, with_fs},
2625
net::Socket,
2726
pidfd::PidFd,
2827
pipe::Pipe,
2928
};
30-
use crate::io::IoVectorBufIo;
29+
use crate::{
30+
io::IoVectorBufIo,
31+
mm::{VmBytes, VmBytesMut},
32+
};
3133

3234
#[derive(Debug, Clone, Copy)]
3335
pub struct Kstat {

api/src/mm.rs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
use alloc::string::String;
2-
use core::{alloc::Layout, ffi::c_char, hint::unlikely, mem::transmute, ptr, slice, str};
2+
use core::{
3+
alloc::Layout,
4+
ffi::c_char,
5+
hint::unlikely,
6+
mem::{MaybeUninit, transmute},
7+
ptr, slice, str,
8+
};
39

410
use axerrno::{AxError, AxResult};
511
use axhal::{
612
paging::MappingFlags,
713
trap::{PAGE_FAULT, register_trap_handler},
814
};
15+
use axio::{Buf, BufMut, Read, Write};
916
use axtask::current;
1017
use memory_addr::{MemoryAddr, PAGE_SIZE_4K, VirtAddr};
1118
use starry_core::{
1219
mm::{access_user_memory, is_accessing_user_memory},
1320
task::AsThread,
1421
};
15-
use starry_vm::vm_load_until_nul;
22+
use starry_vm::{vm_load_until_nul, vm_read_slice, vm_write_slice};
1623

1724
fn check_region(start: VirtAddr, layout: Layout, access_flags: MappingFlags) -> AxResult<()> {
1825
let align = layout.align();
@@ -256,3 +263,90 @@ pub fn vm_load_string(ptr: *const c_char) -> AxResult<String> {
256263
let bytes = vm_load_until_nul(ptr as *const u8)?;
257264
String::from_utf8(bytes).map_err(|_| AxError::IllegalBytes)
258265
}
266+
267+
/// A read-only buffer in the VM's memory.
268+
///
269+
/// It implements the `axio::Read` trait, allowing it to be used with other I/O
270+
/// operations.
271+
pub struct VmBytes {
272+
/// The pointer to the start of the buffer in the VM's memory.
273+
pub ptr: *const u8,
274+
/// The length of the buffer.
275+
pub len: usize,
276+
}
277+
278+
impl VmBytes {
279+
/// Creates a new `VmBytes` from a raw pointer and a length.
280+
pub fn new(ptr: *const u8, len: usize) -> Self {
281+
Self { ptr, len }
282+
}
283+
284+
/// Casts the `VmBytes` to a mutable `VmBytesMut`.
285+
pub fn cast_mut(&self) -> VmBytesMut {
286+
VmBytesMut::new(self.ptr as *mut u8, self.len)
287+
}
288+
}
289+
290+
impl Read for VmBytes {
291+
/// Reads bytes from the VM's memory into the provided buffer.
292+
fn read(&mut self, buf: &mut [u8]) -> axio::Result<usize> {
293+
let len = self.len.min(buf.len());
294+
vm_read_slice(self.ptr, unsafe {
295+
transmute::<&mut [u8], &mut [MaybeUninit<u8>]>(&mut buf[..len])
296+
})?;
297+
self.ptr = self.ptr.wrapping_add(len);
298+
self.len -= len;
299+
Ok(len)
300+
}
301+
}
302+
303+
impl Buf for VmBytes {
304+
fn remaining(&self) -> usize {
305+
self.len
306+
}
307+
}
308+
309+
/// A mutable buffer in the VM's memory.
310+
///
311+
/// It implements the `axio::Write` trait, allowing it to be used with other I/O
312+
/// operations.
313+
pub struct VmBytesMut {
314+
/// The pointer to the start of the buffer in the VM's memory.
315+
pub ptr: *mut u8,
316+
/// The length of the buffer.
317+
pub len: usize,
318+
}
319+
320+
impl VmBytesMut {
321+
/// Creates a new `VmBytesMut` from a raw pointer and a length.
322+
pub fn new(ptr: *mut u8, len: usize) -> Self {
323+
Self { ptr, len }
324+
}
325+
326+
/// Casts the `VmBytesMut` to a read-only `VmBytes`.
327+
pub fn cast_const(&self) -> VmBytes {
328+
VmBytes::new(self.ptr, self.len)
329+
}
330+
}
331+
332+
impl Write for VmBytesMut {
333+
/// Writes bytes from the provided buffer into the VM's memory.
334+
fn write(&mut self, buf: &[u8]) -> axio::Result<usize> {
335+
let len = self.len.min(buf.len());
336+
vm_write_slice(self.ptr, &buf[..len])?;
337+
self.ptr = self.ptr.wrapping_add(len);
338+
self.len -= len;
339+
Ok(len)
340+
}
341+
342+
/// Flushes the buffer. This is a no-op for `VmBytesMut`.
343+
fn flush(&mut self) -> axio::Result {
344+
Ok(())
345+
}
346+
}
347+
348+
impl BufMut for VmBytesMut {
349+
fn remaining_mut(&self) -> usize {
350+
self.len
351+
}
352+
}

api/src/syscall/fs/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use axio::{Seek, SeekFrom};
1010
use axpoll::{IoEvents, Pollable};
1111
use axtask::current;
1212
use linux_raw_sys::general::__kernel_off_t;
13-
use starry_vm::{VmBytes, VmBytesMut, VmMutPtr, VmPtr};
13+
use starry_vm::{VmMutPtr, VmPtr};
1414
use syscalls::Sysno;
1515

1616
use crate::{
1717
file::{File, FileLike, Pipe, SealedBuf, SealedBufMut, get_file_like},
1818
io::{IoVec, IoVectorBuf},
19-
mm::UserConstPtr,
19+
mm::{UserConstPtr, VmBytes, VmBytesMut},
2020
};
2121

2222
struct DummyFd;

api/src/syscall/net/io.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ use axnet::{CMsgData, RecvFlags, RecvOptions, SendFlags, SendOptions, SocketAddr
77
use linux_raw_sys::net::{
88
MSG_PEEK, MSG_TRUNC, SCM_RIGHTS, SOL_SOCKET, cmsghdr, msghdr, sockaddr, socklen_t,
99
};
10-
use starry_vm::{VmBytes, VmBytesMut};
1110

1211
use crate::{
1312
file::{FileLike, Socket, add_file_like},
1413
io::{IoVec, IoVectorBuf},
15-
mm::{UserConstPtr, UserPtr},
14+
mm::{UserConstPtr, UserPtr, VmBytes, VmBytesMut},
1615
socket::SocketAddrExt,
1716
syscall::net::{CMsg, CMsgBuilder},
1817
};

0 commit comments

Comments
 (0)