From 6d132345ee2eb2f780702a2ff7b3e5ee53840fb6 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sun, 2 Jan 2022 11:57:34 +0100 Subject: [PATCH 01/10] WIP: Replace fexec with exec. --- src/call.rs | 17 +++++++++++++---- src/data.rs | 30 ++++++++++++++++++++++++++++++ src/number.rs | 2 +- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/call.rs b/src/call.rs index f6eb89c..8cf2876 100644 --- a/src/call.rs +++ b/src/call.rs @@ -1,5 +1,5 @@ use super::arch::*; -use super::data::{Map, SigAction, Stat, StatVfs, TimeSpec}; +use super::data::{ExecMemRange, Map, SigAction, Stat, StatVfs, TimeSpec}; use super::error::Result; use super::flag::*; use super::number::*; @@ -85,9 +85,18 @@ pub fn fcntl(fd: usize, cmd: usize, arg: usize) -> Result { unsafe { syscall3(SYS_FCNTL, fd, cmd, arg) } } -/// Replace the current process with a new executable -pub fn fexec(fd: usize, args: &[[usize; 2]], vars: &[[usize; 2]]) -> Result { - unsafe { syscall5(SYS_FEXEC, fd, args.as_ptr() as usize, args.len(), vars.as_ptr() as usize, vars.len()) } +// TODO: Support specifying FDs to keep/close (for FDs not included in that list, it would take +// into account O_CLOEXEC). +// TODO: Allow setting all registers of the target process? +/// Replace the current process with a new executable, allowing the user to specify memory ranges +/// to either keep, move, or add. It will then jump to [`instruction_ptr`] in the new process +/// memory specified by the range map, with the stack pointer set accordingly. This syscall does +/// not support setuid/setgid; instead, privilege escalation must be done by a higher-privileged +/// process performing these actions via ptrace. +// TODO: never type +pub fn exec(memranges: &[ExecMemRange], instruction_ptr: usize, stack_ptr: usize) -> Result { + unsafe { syscall4(SYS_EXEC, memranges.as_ptr() as usize, memranges.len(), instruction_ptr, stack_ptr)?; } + panic!("SYS_EXEC should only return in case of an error"); } /// Map a file into memory, but with the ability to set the address to map into, either as a hint diff --git a/src/data.rs b/src/data.rs index 45d2dd8..efd1bc2 100644 --- a/src/data.rs +++ b/src/data.rs @@ -295,3 +295,33 @@ macro_rules! ptrace_event { } } } + +#[repr(C)] +pub struct ExecMemRange { + /// The address where the range is intended to be. + pub address: usize, + /// The size of the memory range. + pub size: usize, + /// Flags describing permissions (i.e. R/W/X) + pub flags: MapFlags, + /// If this equals [`address`], the range is kept untouched although flags can change, and the + /// range can be shortened or partly zeroed. If it is different, it will move `[old_address, + /// old_address+size)` to `[address, address+size]`. + pub old_address: usize, +} +impl Deref for ExecMemRange { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + slice::from_raw_parts(self as *const ExecMemRange as *const u8, mem::size_of::()) + } + } +} + +impl DerefMut for ExecMemRange { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut(self as *mut ExecMemRange as *mut u8, mem::size_of::()) + } + } +} diff --git a/src/number.rs b/src/number.rs index 1f037eb..1d6f17b 100644 --- a/src/number.rs +++ b/src/number.rs @@ -26,7 +26,6 @@ pub const SYS_FCHMOD: usize = SYS_CLASS_FILE | 94; pub const SYS_FCHOWN: usize = SYS_CLASS_FILE | 207; pub const SYS_FCNTL: usize = SYS_CLASS_FILE | 55; pub const SYS_FEVENT: usize = SYS_CLASS_FILE | 927; -pub const SYS_FEXEC: usize = SYS_CLASS_FILE | 11; pub const SYS_FMAP_OLD: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 90; pub const SYS_FMAP: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 900; pub const SYS_FUNMAP_OLD: usize = SYS_CLASS_FILE | 91; @@ -39,6 +38,7 @@ pub const SYS_FSYNC: usize = SYS_CLASS_FILE | 118; pub const SYS_FTRUNCATE: usize = SYS_CLASS_FILE | 93; pub const SYS_FUTIMENS: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 320; +pub const SYS_EXEC: usize = 101; pub const SYS_CHDIR: usize = 12; pub const SYS_CLOCK_GETTIME: usize = 265; pub const SYS_CLONE: usize = 120; From a88e2f72cbbb49c480393a00396c1925341a9863 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 21 Jan 2022 21:29:21 +0100 Subject: [PATCH 02/10] Derive traits for ExecMemRange, change flags type. --- src/data.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/data.rs b/src/data.rs index efd1bc2..51d9a05 100644 --- a/src/data.rs +++ b/src/data.rs @@ -297,13 +297,14 @@ macro_rules! ptrace_event { } #[repr(C)] +#[derive(Clone, Copy, Debug)] pub struct ExecMemRange { /// The address where the range is intended to be. pub address: usize, /// The size of the memory range. pub size: usize, /// Flags describing permissions (i.e. R/W/X) - pub flags: MapFlags, + pub flags: usize, /// If this equals [`address`], the range is kept untouched although flags can change, and the /// range can be shortened or partly zeroed. If it is different, it will move `[old_address, /// old_address+size)` to `[address, address+size]`. From a16a1ef95cef734bfa02515023bee838f8bacabb Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 22 Jan 2022 13:31:20 +0100 Subject: [PATCH 03/10] Add the CloneInfo struct to extend SYS_CLONE. --- src/data.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/data.rs b/src/data.rs index 51d9a05..683a4be 100644 --- a/src/data.rs +++ b/src/data.rs @@ -326,3 +326,29 @@ impl DerefMut for ExecMemRange { } } } +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub struct CloneInfo { + /// The newly allocated stack to use for the child process when [`CLONE_VM`] is set. Otherwise + /// it is ignored. + pub target_stack: usize, + /// The newly allocated signal stack, when [`CLONE_VM`] is set, otherwise ignored. If no signal + /// stack is desired (i.e. all sigactions are SIG_DFL), then this may be set to `usize::MAX`. + pub target_sigstack: usize, +} +impl Deref for CloneInfo { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + slice::from_raw_parts(self as *const Self as *const u8, mem::size_of::()) + } + } +} + +impl DerefMut for CloneInfo { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut(self as *mut Self as *mut u8, mem::size_of::()) + } + } +} From 8e4529ae7ca91b00aeedb49eec6a661f6514c557 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 22 Jan 2022 13:32:01 +0100 Subject: [PATCH 04/10] Make DMA code use whole pages in syscalls. --- src/io/dma.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/io/dma.rs b/src/io/dma.rs index b356c8a..e16ffba 100644 --- a/src/io/dma.rs +++ b/src/io/dma.rs @@ -13,12 +13,26 @@ pub struct PhysBox { size: usize } +#[cfg(target_arch = "x86_64")] +const PAGE_SIZE: usize = 4096; + +const fn round_up(x: usize) -> usize { + (x + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE +} +fn assert_aligned(x: usize) { + assert_eq!(x % PAGE_SIZE, 0); +} + impl PhysBox { - /// Construct a PhysBox from an address and a size. + /// Construct a PhysBox from an address and a size. The address must be page-aligned, and the + /// size must similarly be a multiple of the page size. /// /// # Safety /// This function is unsafe because when dropping, Self has to a valid allocation. pub unsafe fn from_raw_parts(address: usize, size: usize) -> Self { + assert_aligned(address); + assert_aligned(size); + Self { address, size, @@ -42,12 +56,10 @@ impl PhysBox { pub fn new_with_flags(size: usize, flags: PhysallocFlags) -> Result { assert!(!flags.contains(PhysallocFlags::PARTIAL_ALLOC)); + assert_aligned(size); let address = unsafe { crate::physalloc2(size, flags.bits())? }; - Ok(Self { - address, - size, - }) + Ok(unsafe { Self::from_raw_parts(address, size) }) } /// "Partially" allocate physical memory, in the sense that the allocation may be smaller than @@ -57,21 +69,18 @@ impl PhysBox { /// that first allocation only returns half the size, the driver can do another allocation /// and then let the device use both buffers. pub fn new_partial_allocation(size: usize, flags: PhysallocFlags, strategy: Option, mut min: usize) -> Result { + assert_aligned(size); debug_assert!(!(flags.contains(PhysallocFlags::PARTIAL_ALLOC) && strategy.is_none())); - let address = unsafe { crate::physalloc3(size, flags.bits() | strategy.map(|s| s as usize).unwrap_or(0), &mut min)? }; - Ok(Self { - address, - size: min, - }) + let address = unsafe { crate::physalloc3(size, flags.bits() | strategy.map_or(0, |s| s as usize), &mut min)? }; + Ok(unsafe { Self::from_raw_parts(address, size) }) } pub fn new(size: usize) -> Result { + assert_aligned(size); + let address = unsafe { crate::physalloc(size)? }; - Ok(Self { - address, - size, - }) + Ok(unsafe { Self::from_raw_parts(address, size) }) } } @@ -111,11 +120,11 @@ impl Dma { } pub fn new(value: T) -> Result { - let phys = PhysBox::new(mem::size_of::())?; + let phys = PhysBox::new(round_up(mem::size_of::()))?; Self::from_physbox(phys, value) } pub fn zeroed() -> Result>> { - let phys = PhysBox::new(mem::size_of::())?; + let phys = PhysBox::new(round_up(mem::size_of::()))?; Self::from_physbox_zeroed(phys) } } @@ -163,7 +172,7 @@ impl Dma<[T]> { /// * `T` must be properly aligned. /// * `T` must be valid as zeroed (i.e. no NonNull pointers). pub unsafe fn zeroed_unsized(count: usize) -> Result { - let phys = PhysBox::new(mem::size_of::() * count)?; + let phys = PhysBox::new(round_up(mem::size_of::() * count))?; Ok(Self::from_physbox_zeroed_unsized(phys, count)?.assume_init()) } } @@ -195,6 +204,6 @@ impl DerefMut for Dma { impl Drop for Dma { fn drop(&mut self) { unsafe { ptr::drop_in_place(self.virt) } - let _ = unsafe { crate::physunmap(self.virt as *mut u8 as usize) }; + let _ = unsafe { crate::funmap(self.virt as *mut u8 as usize, self.phys.size) }; } } From 67cf0997dc710707977c0075854c1410a7afcf22 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Thu, 7 Jul 2022 10:05:22 +0200 Subject: [PATCH 05/10] Remove clone() and Daemon abstraction. --- src/call.rs | 5 ----- src/daemon.rs | 62 --------------------------------------------------- src/lib.rs | 4 ---- src/number.rs | 1 - 4 files changed, 72 deletions(-) delete mode 100644 src/daemon.rs diff --git a/src/call.rs b/src/call.rs index 8cf2876..f42eaf9 100644 --- a/src/call.rs +++ b/src/call.rs @@ -38,11 +38,6 @@ pub fn chmod>(path: T, mode: usize) -> Result { unsafe { syscall3(SYS_CHMOD, path.as_ref().as_ptr() as usize, path.as_ref().len(), mode) } } -/// Produce a fork of the current process, or a new process thread -pub unsafe fn clone(flags: CloneFlags) -> Result { - syscall1(SYS_CLONE, flags.bits()) -} - /// Close a file pub fn close(fd: usize) -> Result { unsafe { syscall1(SYS_CLOSE, fd) } diff --git a/src/daemon.rs b/src/daemon.rs deleted file mode 100644 index 6433bcd..0000000 --- a/src/daemon.rs +++ /dev/null @@ -1,62 +0,0 @@ -use core::convert::Infallible; - -use super::{ - clone, - CloneFlags, - close, - EIO, - Error, - exit, - pipe2, - read, - Result, - write, -}; - -#[must_use = "Daemon::ready must be called"] -pub struct Daemon { - write_pipe: usize, -} - -impl Daemon { - pub fn new Infallible>(f: F) -> Result { - let mut pipes = [0; 2]; - pipe2(&mut pipes, 0)?; - - let [read_pipe, write_pipe] = pipes; - - if unsafe { clone(CloneFlags::empty())? } == 0 { - let _ = close(read_pipe); - - f(Daemon { - write_pipe, - }); - // TODO: Replace Infallible with the never type once it is stabilized. - unreachable!(); - } else { - let _ = close(write_pipe); - - let mut data = [0]; - let res = read(read_pipe, &mut data); - let _ = close(read_pipe); - - if res? == 1 { - exit(data[0] as usize)?; - unreachable!(); - } else { - Err(Error::new(EIO)) - } - } - } - - pub fn ready(self) -> Result<()> { - let res = write(self.write_pipe, &[0]); - let _ = close(self.write_pipe); - - if res? == 1 { - Ok(()) - } else { - Err(Error::new(EIO)) - } - } -} diff --git a/src/lib.rs b/src/lib.rs index 9c59398..3f6d884 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,6 @@ extern crate core; pub use self::arch::*; pub use self::call::*; -pub use self::daemon::*; pub use self::data::*; pub use self::error::*; pub use self::flag::*; @@ -43,9 +42,6 @@ pub mod call; /// Complex structures that are used for some system calls pub mod data; -/// Wrapper to make daemons easier to write -pub mod daemon; - /// All errors that can be generated by a system call pub mod error; diff --git a/src/number.rs b/src/number.rs index 1d6f17b..5cf50f7 100644 --- a/src/number.rs +++ b/src/number.rs @@ -41,7 +41,6 @@ pub const SYS_FUTIMENS: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 320; pub const SYS_EXEC: usize = 101; pub const SYS_CHDIR: usize = 12; pub const SYS_CLOCK_GETTIME: usize = 265; -pub const SYS_CLONE: usize = 120; pub const SYS_EXIT: usize = 1; pub const SYS_FUTEX: usize = 240; pub const SYS_GETCWD: usize = 183; From f407c6b70cc2e2a5c0c9fb53acc80b3ae63b6bd5 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Thu, 7 Jul 2022 12:11:20 +0200 Subject: [PATCH 06/10] Remove CloneFlags. --- src/flag.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/flag.rs b/src/flag.rs index 9788884..d1eb105 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -32,18 +32,6 @@ macro_rules! bitflags { } } -bitflags! { - pub struct CloneFlags: usize { - const CLONE_VM = 0x100; - const CLONE_FS = 0x200; - const CLONE_FILES = 0x400; - const CLONE_SIGHAND = 0x800; - const CLONE_VFORK = 0x4000; - const CLONE_THREAD = 0x10000; - const CLONE_STACK = 0x1000_0000; - } -} - pub const CLOCK_REALTIME: usize = 1; pub const CLOCK_MONOTONIC: usize = 4; From ab5c685978ec6452b0b7a9ec2e0308311b929360 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Thu, 21 Jul 2022 11:13:57 +0200 Subject: [PATCH 07/10] Remove SYS_EXEC from existence --- src/call.rs | 16 +-------------- src/data.rs | 57 --------------------------------------------------- src/number.rs | 1 - 3 files changed, 1 insertion(+), 73 deletions(-) diff --git a/src/call.rs b/src/call.rs index f42eaf9..1e8eac6 100644 --- a/src/call.rs +++ b/src/call.rs @@ -1,5 +1,5 @@ use super::arch::*; -use super::data::{ExecMemRange, Map, SigAction, Stat, StatVfs, TimeSpec}; +use super::data::{Map, SigAction, Stat, StatVfs, TimeSpec}; use super::error::Result; use super::flag::*; use super::number::*; @@ -80,20 +80,6 @@ pub fn fcntl(fd: usize, cmd: usize, arg: usize) -> Result { unsafe { syscall3(SYS_FCNTL, fd, cmd, arg) } } -// TODO: Support specifying FDs to keep/close (for FDs not included in that list, it would take -// into account O_CLOEXEC). -// TODO: Allow setting all registers of the target process? -/// Replace the current process with a new executable, allowing the user to specify memory ranges -/// to either keep, move, or add. It will then jump to [`instruction_ptr`] in the new process -/// memory specified by the range map, with the stack pointer set accordingly. This syscall does -/// not support setuid/setgid; instead, privilege escalation must be done by a higher-privileged -/// process performing these actions via ptrace. -// TODO: never type -pub fn exec(memranges: &[ExecMemRange], instruction_ptr: usize, stack_ptr: usize) -> Result { - unsafe { syscall4(SYS_EXEC, memranges.as_ptr() as usize, memranges.len(), instruction_ptr, stack_ptr)?; } - panic!("SYS_EXEC should only return in case of an error"); -} - /// Map a file into memory, but with the ability to set the address to map into, either as a hint /// or as a requirement of the map. /// diff --git a/src/data.rs b/src/data.rs index 683a4be..45d2dd8 100644 --- a/src/data.rs +++ b/src/data.rs @@ -295,60 +295,3 @@ macro_rules! ptrace_event { } } } - -#[repr(C)] -#[derive(Clone, Copy, Debug)] -pub struct ExecMemRange { - /// The address where the range is intended to be. - pub address: usize, - /// The size of the memory range. - pub size: usize, - /// Flags describing permissions (i.e. R/W/X) - pub flags: usize, - /// If this equals [`address`], the range is kept untouched although flags can change, and the - /// range can be shortened or partly zeroed. If it is different, it will move `[old_address, - /// old_address+size)` to `[address, address+size]`. - pub old_address: usize, -} -impl Deref for ExecMemRange { - type Target = [u8]; - fn deref(&self) -> &[u8] { - unsafe { - slice::from_raw_parts(self as *const ExecMemRange as *const u8, mem::size_of::()) - } - } -} - -impl DerefMut for ExecMemRange { - fn deref_mut(&mut self) -> &mut [u8] { - unsafe { - slice::from_raw_parts_mut(self as *mut ExecMemRange as *mut u8, mem::size_of::()) - } - } -} -#[repr(C)] -#[derive(Clone, Copy, Debug)] -pub struct CloneInfo { - /// The newly allocated stack to use for the child process when [`CLONE_VM`] is set. Otherwise - /// it is ignored. - pub target_stack: usize, - /// The newly allocated signal stack, when [`CLONE_VM`] is set, otherwise ignored. If no signal - /// stack is desired (i.e. all sigactions are SIG_DFL), then this may be set to `usize::MAX`. - pub target_sigstack: usize, -} -impl Deref for CloneInfo { - type Target = [u8]; - fn deref(&self) -> &[u8] { - unsafe { - slice::from_raw_parts(self as *const Self as *const u8, mem::size_of::()) - } - } -} - -impl DerefMut for CloneInfo { - fn deref_mut(&mut self) -> &mut [u8] { - unsafe { - slice::from_raw_parts_mut(self as *mut Self as *mut u8, mem::size_of::()) - } - } -} diff --git a/src/number.rs b/src/number.rs index 5cf50f7..a35f469 100644 --- a/src/number.rs +++ b/src/number.rs @@ -38,7 +38,6 @@ pub const SYS_FSYNC: usize = SYS_CLASS_FILE | 118; pub const SYS_FTRUNCATE: usize = SYS_CLASS_FILE | 93; pub const SYS_FUTIMENS: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 320; -pub const SYS_EXEC: usize = 101; pub const SYS_CHDIR: usize = 12; pub const SYS_CLOCK_GETTIME: usize = 265; pub const SYS_EXIT: usize = 1; From c7b84541dc4a04cb1cbc52c517eb74ea371de5a0 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Thu, 21 Jul 2022 11:14:14 +0200 Subject: [PATCH 08/10] Improve general flags for userspace_fexec. First, auxiliary vectors are no longer left here in syscall, but remain in relibc. The rationale behind removing them was that they are no longer used by the kernel in any way, as exec is moved to userspace. Second, a ptrace event for address space switches was added. This is to allow tracers to reopen their mem file when that mem file points to different memory (otherwise mem can be decoupled from address spaces and simply use the one that the context uses at the time). --- src/flag.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/flag.rs b/src/flag.rs index d1eb105..b200079 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -201,8 +201,10 @@ bitflags! { /// If you don't catch this, the child is started as normal. const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100; - const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00; + /// Sent when current-addrspace is changed, allowing the tracer to reopen the memory file. + const PTRACE_EVENT_ADDRSPACE_SWITCH = 0x0000_0000_0000_0200; + const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00; /// Special meaning, depending on the event. Usually, when fired before /// an action, it will skip performing that action. @@ -280,13 +282,6 @@ bitflags! { } } -// Auxiliery vector types -pub const AT_NULL: usize = 0; -pub const AT_PHDR: usize = 3; -pub const AT_PHENT: usize = 4; -pub const AT_PHNUM: usize = 5; -pub const AT_ENTRY: usize = 9; - bitflags! { pub struct WaitFlags: usize { const WNOHANG = 0x01; From d6af266119e7b4a3b0e9a04c63b3cfcfac94781a Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Tue, 26 Jul 2022 15:45:07 +0200 Subject: [PATCH 09/10] Add "opcodes", for e.g. mmap on other addr spaces. --- src/flag.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/flag.rs b/src/flag.rs index b200079..3411048 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -290,6 +290,11 @@ bitflags! { } } +pub const ADDRSPACE_OP_MMAP: usize = 0; +pub const ADDRSPACE_OP_MUNMAP: usize = 1; +pub const ADDRSPACE_OP_MPROTECT: usize = 2; +pub const ADDRSPACE_OP_TRANSFER: usize = 3; + /// True if status indicates the child is stopped. pub fn wifstopped(status: usize) -> bool { (status & 0xff) == 0x7f From 300b5e4a8066019c0b6d009cabd8ec4aef6b94a8 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Wed, 27 Jul 2022 17:24:15 +0200 Subject: [PATCH 10/10] Add PAGE_SIZE for all archs, make it public. --- src/arch/aarch64.rs | 2 ++ src/arch/nonredox.rs | 3 +++ src/arch/x86.rs | 2 ++ src/arch/x86_64.rs | 2 ++ src/io/dma.rs | 4 +--- 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/arch/aarch64.rs b/src/arch/aarch64.rs index e771396..63c64cf 100644 --- a/src/arch/aarch64.rs +++ b/src/arch/aarch64.rs @@ -3,6 +3,8 @@ use core::ops::{Deref, DerefMut}; use super::error::{Error, Result}; +pub const PAGE_SIZE: usize = 4096; + macro_rules! syscall { ($($name:ident($a:ident, $($b:ident, $($c:ident, $($d:ident, $($e:ident, $($f:ident, )?)?)?)?)?);)+) => { $( diff --git a/src/arch/nonredox.rs b/src/arch/nonredox.rs index f99a714..65c44fc 100644 --- a/src/arch/nonredox.rs +++ b/src/arch/nonredox.rs @@ -1,5 +1,8 @@ use super::error::{Error, Result, ENOSYS}; +// Doesn't really matter, but since we will most likely run on an x86_64 host, why not 4096? +pub const PAGE_SIZE: usize = 4096; + pub unsafe fn syscall0(_a: usize) -> Result { Err(Error::new(ENOSYS)) } diff --git a/src/arch/x86.rs b/src/arch/x86.rs index 2f9301e..a97ba81 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -4,6 +4,8 @@ use core::ops::{Deref, DerefMut}; use super::error::{Error, Result}; +pub const PAGE_SIZE: usize = 4096; + macro_rules! syscall { ($($name:ident($a:ident, $($b:ident, $($c:ident, $($d:ident, $($e:ident, $($f:ident, )?)?)?)?)?);)+) => { $( diff --git a/src/arch/x86_64.rs b/src/arch/x86_64.rs index f71898e..2ff57bb 100644 --- a/src/arch/x86_64.rs +++ b/src/arch/x86_64.rs @@ -4,6 +4,8 @@ use core::ops::{Deref, DerefMut}; use super::error::{Error, Result}; +pub const PAGE_SIZE: usize = 4096; + macro_rules! syscall { ($($name:ident($a:ident, $($b:ident, $($c:ident, $($d:ident, $($e:ident, $($f:ident, )?)?)?)?)?);)+) => { $( diff --git a/src/io/dma.rs b/src/io/dma.rs index e16ffba..f496a1e 100644 --- a/src/io/dma.rs +++ b/src/io/dma.rs @@ -4,6 +4,7 @@ use core::{ptr, slice}; use crate::Result; use crate::{PartialAllocStrategy, PhysallocFlags}; +use crate::arch::PAGE_SIZE; /// An RAII guard of a physical memory allocation. Currently all physically allocated memory are /// page-aligned and take up at least 4k of space (on x86_64). @@ -13,9 +14,6 @@ pub struct PhysBox { size: usize } -#[cfg(target_arch = "x86_64")] -const PAGE_SIZE: usize = 4096; - const fn round_up(x: usize) -> usize { (x + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE }