diff --git a/acpid/src/acpi.rs b/acpid/src/acpi.rs index 417cd920fa..77ed3b9d8f 100644 --- a/acpid/src/acpi.rs +++ b/acpid/src/acpi.rs @@ -126,7 +126,7 @@ impl Deref for PhysmapGuard { impl Drop for PhysmapGuard { fn drop(&mut self) { unsafe { - let _ = syscall::physunmap(self.virt as usize); + let _ = syscall::funmap(self.virt as usize, self.size); } } } diff --git a/acpid/src/acpi/dmar/drhd.rs b/acpid/src/acpi/dmar/drhd.rs index ee6254f636..67625974d7 100644 --- a/acpid/src/acpi/dmar/drhd.rs +++ b/acpid/src/acpi/dmar/drhd.rs @@ -35,7 +35,7 @@ impl DerefMut for DrhdPage { impl Drop for DrhdPage { fn drop(&mut self) { unsafe { - let _ = syscall::physunmap(self.virt as usize); + let _ = syscall::funmap(self.virt as usize, crate::acpi::PAGE_SIZE); } } } diff --git a/acpid/src/aml_physmem.rs b/acpid/src/aml_physmem.rs index ffc6798511..953b0ac911 100644 --- a/acpid/src/aml_physmem.rs +++ b/acpid/src/aml_physmem.rs @@ -29,8 +29,8 @@ impl MappedPage { impl Drop for MappedPage { fn drop(&mut self) { log::trace!("Drop page {:#x}", self.phys_page); - if let Err(e) = unsafe { syscall::physunmap(self.virt_page) } { - log::error!("physunmap: {:?}", e); + if let Err(e) = unsafe { syscall::funmap(self.virt_page, PAGE_SIZE) } { + log::error!("funmap (phys): {:?}", e); } } } diff --git a/ahcid/src/main.rs b/ahcid/src/main.rs index c19c60106d..05ed80fb8a 100644 --- a/ahcid/src/main.rs +++ b/ahcid/src/main.rs @@ -1,4 +1,5 @@ #![cfg_attr(target_arch = "aarch64", feature(stdsimd))] // Required for yield instruction +#![feature(int_roundings)] extern crate syscall; extern crate byteorder; @@ -8,6 +9,7 @@ use std::fs::File; use std::io::{ErrorKind, Read, Write}; use std::os::unix::io::{FromRawFd, RawFd}; +use syscall::PAGE_SIZE; use syscall::error::{Error, ENODEV}; use syscall::data::{Event, Packet}; use syscall::flag::{EVENT_READ, PHYSMAP_NO_CACHE, PHYSMAP_WRITE}; @@ -89,7 +91,7 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { info!(" + AHCI {} on: {:X} size: {} IRQ: {}", name, bar, bar_size, irq); let address = unsafe { - syscall::physmap(bar, (bar_size+4095)/4096*4096, PHYSMAP_WRITE | PHYSMAP_NO_CACHE) + syscall::physmap(bar, bar_size.next_multiple_of(PAGE_SIZE), PHYSMAP_WRITE | PHYSMAP_NO_CACHE) .expect("ahcid: failed to map address") }; { @@ -200,7 +202,6 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { } } } - unsafe { let _ = syscall::physunmap(address); } std::process::exit(0); } diff --git a/alxd/src/main.rs b/alxd/src/main.rs index f0877eacd4..6f125a855f 100644 --- a/alxd/src/main.rs +++ b/alxd/src/main.rs @@ -141,7 +141,6 @@ fn main() { }).expect("alxd: failed to write event"); } } - unsafe { let _ = syscall::physunmap(address); } std::process::exit(0); }).expect("alxd: failed to daemonize"); } diff --git a/e1000d/src/main.rs b/e1000d/src/main.rs index 67f8a4012a..e0353af679 100644 --- a/e1000d/src/main.rs +++ b/e1000d/src/main.rs @@ -193,9 +193,6 @@ fn main() { send_events(event_count); } } - unsafe { - let _ = syscall::physunmap(address); - } process::exit(0); }).expect("e1000d: failed to create daemon"); } diff --git a/ihdad/src/hda/stream.rs b/ihdad/src/hda/stream.rs index c6f3753e59..dbdc3ad79c 100644 --- a/ihdad/src/hda/stream.rs +++ b/ihdad/src/hda/stream.rs @@ -1,4 +1,4 @@ -use syscall::{PHYSMAP_WRITE, PHYSMAP_NO_CACHE}; +use syscall::{PHYSMAP_WRITE, PHYSMAP_NO_CACHE, PAGE_SIZE}; use syscall::error::{Error, EIO, Result}; use syscall::io::{Mmio, Io}; use std::result; @@ -289,8 +289,10 @@ pub struct StreamBuffer { impl StreamBuffer { pub fn new(block_length: usize, block_count: usize) -> result::Result { + let page_aligned_size = (block_length * block_count).next_multiple_of(PAGE_SIZE); + let phys = match unsafe { - syscall::physalloc(block_length * block_count) + syscall::physalloc(page_aligned_size) } { Ok(phys) => phys, Err(_err) => { @@ -299,17 +301,18 @@ impl StreamBuffer { }; let addr = match unsafe { - syscall::physmap(phys, block_length * block_count, PHYSMAP_WRITE | PHYSMAP_NO_CACHE) + syscall::physmap(phys, page_aligned_size, PHYSMAP_WRITE | PHYSMAP_NO_CACHE) } { Ok(addr) => addr, Err(_err) => { unsafe { - syscall::physfree(phys, block_length * block_count); + syscall::physfree(phys, page_aligned_size); } return Err("Could not map physical memory for buffer."); } }; + // TODO: Already zeroed by kernel? unsafe { ptr::write_bytes(addr as *mut u8, 0, block_length * block_count); } @@ -370,8 +373,10 @@ impl Drop for StreamBuffer { fn drop(&mut self) { unsafe { log::debug!("IHDA: Deallocating buffer."); - if syscall::physunmap(self.addr).is_ok() { - let _ = syscall::physfree(self.phys, self.block_len * self.block_cnt); + let page_aligned_size = (self.block_len * self.block_cnt).next_multiple_of(PAGE_SIZE); + + if syscall::funmap(self.addr, page_aligned_size).is_ok() { + let _ = syscall::physfree(self.phys, page_aligned_size); } } } diff --git a/ihdad/src/main.rs b/ihdad/src/main.rs index 1fac624ae0..f77b2c278e 100755 --- a/ihdad/src/main.rs +++ b/ihdad/src/main.rs @@ -1,4 +1,5 @@ //#![deny(warnings)] +#![feature(int_roundings)] extern crate bitflags; extern crate spin; @@ -300,7 +301,6 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { } } - unsafe { let _ = syscall::physunmap(address); } std::process::exit(0); } diff --git a/ixgbed/src/main.rs b/ixgbed/src/main.rs index ae8be8d5b7..7bbd3a42fb 100644 --- a/ixgbed/src/main.rs +++ b/ixgbed/src/main.rs @@ -195,9 +195,6 @@ fn main() { send_events(event_count); } } - unsafe { - let _ = syscall::physunmap(address); - } std::process::exit(0); }).expect("ixgbed: failed to daemonize"); diff --git a/nvmed/src/main.rs b/nvmed/src/main.rs index f88a1db9e9..420742704a 100644 --- a/nvmed/src/main.rs +++ b/nvmed/src/main.rs @@ -1,4 +1,5 @@ #![cfg_attr(target_arch = "aarch64", feature(stdsimd))] // Required for yield instruction +#![feature(int_roundings)] use std::convert::TryInto; use std::fs::File; @@ -11,7 +12,7 @@ use std::{slice, usize}; use pcid_interface::{PciBar, PciFeature, PciFeatureInfo, PciFunction, PcidServerHandle}; use syscall::{ Event, Mmio, Packet, Result, SchemeBlockMut, PHYSMAP_NO_CACHE, - PHYSMAP_WRITE, + PHYSMAP_WRITE, PAGE_SIZE, }; use redox_log::{OutputBuilder, RedoxLogger}; @@ -42,7 +43,7 @@ impl Bar { impl Drop for Bar { fn drop(&mut self) { - let _ = unsafe { syscall::physunmap(self.physical) }; + let _ = unsafe { syscall::funmap(self.physical, self.bar_size.next_multiple_of(PAGE_SIZE)) }; } } diff --git a/pcid/src/pcie/mod.rs b/pcid/src/pcie/mod.rs index 1e1da08a25..7c73d84767 100644 --- a/pcid/src/pcie/mod.rs +++ b/pcid/src/pcie/mod.rs @@ -2,6 +2,7 @@ use std::{fmt, fs, io, mem, ptr, slice}; use std::collections::BTreeMap; use std::sync::{Arc, Mutex}; +use syscall::PAGE_SIZE; use syscall::flag::PhysmapFlags; use smallvec::SmallVec; @@ -172,7 +173,7 @@ impl Pcie { }; let mut maps_lock = self.maps.lock().unwrap(); let virt_pointer = maps_lock.entry((bus, dev, func)).or_insert_with(|| { - syscall::physmap(base_address_phys as usize + Self::addr_offset_in_bytes(starting_bus, bus, dev, func, 0), 4096, PhysmapFlags::PHYSMAP_NO_CACHE | PhysmapFlags::PHYSMAP_WRITE).unwrap_or_else(|error| panic!("failed to physmap pcie configuration space for {:2x}:{:2x}.{:2x}: {:?}", bus, dev, func, error)) as *mut u32 + syscall::physmap(base_address_phys as usize + Self::addr_offset_in_bytes(starting_bus, bus, dev, func, 0), PAGE_SIZE, PhysmapFlags::PHYSMAP_NO_CACHE | PhysmapFlags::PHYSMAP_WRITE).unwrap_or_else(|error| panic!("failed to physmap pcie configuration space for {:2x}:{:2x}.{:2x}: {:?}", bus, dev, func, error)) as *mut u32 }); f(Some(&mut *virt_pointer.offset((offset as usize / mem::size_of::()) as isize))) } @@ -207,7 +208,7 @@ impl CfgAccess for Pcie { impl Drop for Pcie { fn drop(&mut self) { for address in self.maps.lock().unwrap().values().copied() { - let _ = unsafe { syscall::physunmap(address as usize) }; + let _ = unsafe { syscall::funmap(address as usize, PAGE_SIZE) }; } } } diff --git a/rtl8168d/src/main.rs b/rtl8168d/src/main.rs index dbfe97aa8b..dae8c8a62e 100644 --- a/rtl8168d/src/main.rs +++ b/rtl8168d/src/main.rs @@ -459,9 +459,6 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { send_events(event_count); } } - unsafe { - let _ = syscall::physunmap(address); - } process::exit(0); } diff --git a/vboxd/src/main.rs b/vboxd/src/main.rs index 0ed4a1975d..4ad50c5598 100644 --- a/vboxd/src/main.rs +++ b/vboxd/src/main.rs @@ -296,7 +296,6 @@ fn main() { event_queue.run().expect("vboxd: failed to run event loop"); } - unsafe { let _ = syscall::physunmap(address); } std::process::exit(0); }).expect("vboxd: failed to daemonize"); diff --git a/vesad/src/main.rs b/vesad/src/main.rs index 1fb6a9ef39..10ae4df44a 100644 --- a/vesad/src/main.rs +++ b/vesad/src/main.rs @@ -1,3 +1,4 @@ +#![feature(int_roundings)] extern crate orbclient; extern crate syscall; diff --git a/vesad/src/scheme.rs b/vesad/src/scheme.rs index acfdaa76d6..d50c12902b 100644 --- a/vesad/src/scheme.rs +++ b/vesad/src/scheme.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; use std::{mem, ptr, slice, str}; use orbclient::{Event, EventOption}; -use syscall::{Error, EventFlags, EACCES, EBADF, EINVAL, ENOENT, Map, O_NONBLOCK, physmap, physunmap, PHYSMAP_WRITE, PHYSMAP_WRITE_COMBINE, Result, SchemeMut}; +use syscall::{Error, EventFlags, EACCES, EBADF, EINVAL, ENOENT, Map, O_NONBLOCK, physmap, PHYSMAP_WRITE, PHYSMAP_WRITE_COMBINE, Result, SchemeMut, PAGE_SIZE}; use crate::{ display::Display, @@ -102,7 +102,8 @@ impl DisplayScheme { // Unmap old onscreen unsafe { - physunmap(self.onscreens[fb_i].as_mut_ptr() as usize).expect("vesad: failed to unmap framebuffer"); + let slice = mem::take(&mut self.onscreens[fb_i]); + syscall::funmap(slice.as_mut_ptr() as usize, (slice.len() * 4).next_multiple_of(PAGE_SIZE)).expect("vesad: failed to unmap framebuffer"); } // Map new onscreen diff --git a/xhcid/src/main.rs b/xhcid/src/main.rs index f12b28c2bd..8ccfd50653 100644 --- a/xhcid/src/main.rs +++ b/xhcid/src/main.rs @@ -333,8 +333,5 @@ fn daemon(daemon: redox_daemon::Daemon) -> ! { event_queue.run().expect("xhcid: failed to handle events"); - unsafe { - let _ = syscall::physunmap(address); - } std::process::exit(0); }