From 15f3c909c75aec9e8499f67720928ca20d555cb4 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Mon, 18 Mar 2024 17:28:43 +0100 Subject: [PATCH] Switch almost fully to libredox. --- Cargo.lock | 14 +++++++++++++- Cargo.toml | 1 + src/bin/mount.rs | 29 +++++++++++++---------------- src/mount/redox/resource.rs | 26 +++++++++++++------------- src/tests.rs | 26 +++++++++++++------------- 5 files changed, 53 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 801412ee94..4754c619ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -213,6 +213,17 @@ dependencies = [ "redox_syscall 0.4.1", ] +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.4.2", + "libc", + "redox_syscall 0.5.0", +] + [[package]] name = "log" version = "0.4.21" @@ -324,6 +335,7 @@ dependencies = [ "fuser", "getrandom", "libc", + "libredox 0.1.3", "log", "range-tree", "redox-path", @@ -409,7 +421,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4648c7def6f2043b2568617b9f9b75eae88ca185dbc1f1fda30e95a85d49d7d" dependencies = [ "libc", - "libredox", + "libredox 0.0.2", "numtoa", "redox_termios", ] diff --git a/Cargo.toml b/Cargo.toml index 3819e5ed6f..59c549d341 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ seahash = { version = "4.1.0", default-features = false } termion = { version = "2", optional = true } uuid = { version = "1.4", default-features = false } redox-path = "0.3.0" +libredox = "0.1.3" # https://github.com/rexlunae/simple-endian-rs/pull/5 [dependencies.redox_simple_endian] diff --git a/src/bin/mount.rs b/src/bin/mount.rs index 479308b428..f80cb5ca5e 100644 --- a/src/bin/mount.rs +++ b/src/bin/mount.rs @@ -69,11 +69,12 @@ fn bootloader_password() -> Option> { #[cfg(target_os = "redox")] fn capability_mode() { - syscall::setrens(0, 0).expect("redoxfs: failed to enter null namespace"); + libredox::call::setrens(0, 0).expect("redoxfs: failed to enter null namespace"); } #[cfg(target_os = "redox")] fn bootloader_password() -> Option> { + use libredox::call::MmapArgs; use syscall::MapFlags; let addr_env = env::var_os("REDOXFS_PASSWORD_ADDR")?; @@ -95,27 +96,23 @@ fn bootloader_password() -> Option> { unsafe { let aligned_size = size.next_multiple_of(syscall::PAGE_SIZE); - let fd = syscall::open("memory:physical", syscall::O_CLOEXEC) + let fd = libredox::Fd::open("memory:physical", libredox::flag::O_CLOEXEC, 0) .expect("failed to open physical memory file"); - let password_map = syscall::fmap( - fd, - &syscall::Map { - offset: addr, - size: aligned_size, - flags: MapFlags::PROT_READ | MapFlags::MAP_SHARED, - address: 0, // ignored - }, - ) - .expect("failed to map REDOXFS_PASSWORD"); - - let _ = syscall::close(fd); + let password_map = libredox::call::mmap(MmapArgs { + addr: core::ptr::null_mut(), + length: aligned_size, + prot: libredox::flag::PROT_READ, + flags: libredox::flag::MAP_SHARED, + fd: fd.raw(), + offset: addr as u64, + }).expect("failed to map REDOXFS_PASSWORD").cast::(); for i in 0..size { - password.push(*((password_map + i) as *const u8)); + password.push(password_map.add(i).read()); } - let _ = syscall::funmap(password_map, aligned_size); + let _ = libredox::call::munmap(password_map.cast(), aligned_size); } Some(password) } diff --git a/src/mount/redox/resource.rs b/src/mount/redox/resource.rs index 5c5f5afc0e..7012d5615a 100644 --- a/src/mount/redox/resource.rs +++ b/src/mount/redox/resource.rs @@ -3,6 +3,7 @@ use std::slice; use std::time::{SystemTime, UNIX_EPOCH}; use alloc::collections::BTreeMap; +use libredox::call::MmapArgs; use range_tree::RangeTree; use syscall::data::{Map, Stat, TimeSpec}; @@ -293,7 +294,7 @@ impl Fmap { { Ok(ok) => ok, Err(err) => { - let _ = syscall::funmap(address as usize, aligned_size); + let _ = libredox::call::munmap(address.cast(), aligned_size); return Err(err); } }; @@ -487,19 +488,18 @@ impl Resource for FileResource { if new_size > fmap_info.size { fmap_info.base = if fmap_info.base.is_null() { unsafe { - syscall::fmap( - !0, - &Map { - size: new_size, - // PRIVATE/SHARED doesn't matter once the pages are passed in the fmap - // handler. - flags: MapFlags::PROT_READ - | MapFlags::PROT_WRITE - | MapFlags::MAP_PRIVATE, + libredox::call::mmap(MmapArgs { + length: new_size, + // PRIVATE/SHARED doesn't matter once the pages are passed in the fmap + // handler. + prot: libredox::flag::PROT_READ + | libredox::flag::PROT_WRITE, + flags: libredox::flag::MAP_PRIVATE, - offset: 0, - address: 0, - }, + offset: 0, + fd: !0, + addr: core::ptr::null_mut(), + } )? as *mut u8 } } else { diff --git a/src/tests.rs b/src/tests.rs index 2fd0ca619e..bf90242ed9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -84,30 +84,30 @@ fn mmap() { let path = dbg!(path.join("test")); let mmap_inner = |write: bool| { - let fd = dbg!(syscall::open( + let fd = dbg!(libredox::call::open( path.to_str().unwrap(), - syscall::O_CREAT | syscall::O_RDWR | syscall::O_CLOEXEC + libredox::flag::O_CREAT | libredox::flag::O_RDWR | libredox::flag::O_CLOEXEC, + 0, )) .unwrap(); let map = unsafe { slice::from_raw_parts_mut( - dbg!(syscall::fmap( + dbg!(libredox::call::mmap(libredox::call::MmapArgs { fd, - &syscall::Map { - offset: 0, - size: 128, - flags: syscall::PROT_READ | syscall::PROT_WRITE, - address: 0, - } - )) + offset: 0, + length: 128, + prot: libredox::flag::PROT_READ | libredox::flag::PROT_WRITE, + flags: libredox::flag::MAP_SHARED, + addr: core::ptr::null_mut(), + })) .unwrap() as *mut u8, 128, ) }; // Maps should be available after closing - assert_eq!(dbg!(syscall::close(fd)), Ok(0)); + assert_eq!(dbg!(libredox::call::close(fd)), Ok(())); for i in 0..128 { if write { @@ -119,8 +119,8 @@ fn mmap() { //TODO: add msync unsafe { assert_eq!( - dbg!(syscall::funmap(map.as_mut_ptr() as usize, map.len())), - Ok(0) + dbg!(libredox::call::munmap(map.as_mut_ptr().cast(), map.len())), + Ok(()) ); } };