From 16da268f7d1e33d81086037fea371ec7211fb8dd Mon Sep 17 00:00:00 2001 From: kosumi Date: Sat, 4 Nov 2023 19:59:17 -0600 Subject: [PATCH] Enable mman feature of Nix crate --- Cargo.lock | 1 + kernel-hal/Cargo.toml | 2 +- kernel-hal/src/libos/mock_mem.rs | 38 +++++++++++++++++++++----------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01900beba..826a155cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -785,6 +785,7 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" [[package]] name = "executor" version = "0.1.0" +source = "git+https://github.com/KaminariOS/PreemptiveScheduler?rev=d558a02af5269dec0c4a9ac4197974577e02e165#d558a02af5269dec0c4a9ac4197974577e02e165" dependencies = [ "aarch64-cpu", "bit-iter", diff --git a/kernel-hal/Cargo.toml b/kernel-hal/Cargo.toml index 6f3b9d44d..906411834 100644 --- a/kernel-hal/Cargo.toml +++ b/kernel-hal/Cargo.toml @@ -61,7 +61,7 @@ smoltcp = { git = "https://github.com/smoltcp-rs/smoltcp", rev = "35e833e3", def "socket-icmp", "async", ] } -nix = { version = "0.27", optional = true, features = ["fs"]} +nix = { version = "0.27", optional = true, features = ["fs", "mman"]} tempfile = { version = "3", optional = true } async-std = { version = "1.10", optional = true } bitmap-allocator = { git = "https://github.com/rcore-os/bitmap-allocator.git", rev = "88e871a5", optional = true } diff --git a/kernel-hal/src/libos/mock_mem.rs b/kernel-hal/src/libos/mock_mem.rs index ff0d08735..118ed729c 100644 --- a/kernel-hal/src/libos/mock_mem.rs +++ b/kernel-hal/src/libos/mock_mem.rs @@ -1,8 +1,10 @@ -use std::os::unix::io::RawFd; - +use core::convert::TryInto; +use core::mem; use nix::fcntl::{self, OFlag}; use nix::sys::mman::{self, MapFlags, ProtFlags}; use nix::{sys::stat::Mode, unistd}; +use std::os::fd::FromRawFd; +use std::os::unix::io::RawFd; use super::mem::PMEM_MAP_VADDR; use crate::{MMUFlags, PhysAddr, VirtAddr}; @@ -23,8 +25,9 @@ impl MockMemory { Mode::S_IRWXU, ) .expect("faild to open"); - unistd::ftruncate(fd, size as _).expect("failed to set size of shared memory!"); - + let mut f = unsafe { std::fs::File::from_raw_fd(fd) }; + unistd::ftruncate(&mut f, size as _).expect("failed to set size of shared memory!"); + core::mem::forget(f); let mem = Self { size, fd }; mem.mmap(PMEM_MAP_VADDR, size, 0, MMUFlags::READ | MMUFlags::WRITE); mem @@ -55,15 +58,24 @@ impl MockMemory { vaddr, prot, ); - - unsafe { mman::mmap(vaddr as _, len, prot_noexec, flags, fd, offset) }.unwrap_or_else( - |err| { - panic!( - "failed to mmap: fd={}, offset={:#x}, len={:#x}, vaddr={:#x}, prot={:?}: {:?}", - fd, offset, len, vaddr, prot, err - ) - }, - ); + let mut f = unsafe { std::fs::File::from_raw_fd(fd) }; + unsafe { + mman::mmap( + vaddr.try_into().ok(), + len.try_into().unwrap(), + prot_noexec, + flags, + Some(&mut f), + offset, + ) + } + .unwrap_or_else(|err| { + panic!( + "failed to mmap: fd={}, offset={:#x}, len={:#x}, vaddr={:#x}, prot={:?}: {:?}", + fd, offset, len, vaddr, prot, err + ) + }); + mem::forget(f); if prot.contains(MMUFlags::EXECUTE) { self.mprotect(vaddr, len, prot); }