diff --git a/rmm/src/arch/aarch64.rs b/rmm/src/arch/aarch64.rs index 15b74a1ec9..425a6dd2b1 100644 --- a/rmm/src/arch/aarch64.rs +++ b/rmm/src/arch/aarch64.rs @@ -34,7 +34,9 @@ impl Arch for AArch64Arch { const ENTRY_FLAG_EXEC: usize = 0; const ENTRY_FLAG_GLOBAL: usize = 0; const ENTRY_FLAG_NO_GLOBAL: usize = 1 << 11; - const ENTRY_FLAG_WRITE_COMBINING: usize = 0; + const ENTRY_FLAG_DEVICE_MEMORY: usize = MEM_ATTR_DEVICE_nGnRnE << 2; + const ENTRY_FLAG_UNCACHEABLE: usize = MEM_ATTR_NC << 2; + const ENTRY_FLAG_WRITE_COMBINING: usize = MEM_ATTR_NC << 2; const PHYS_OFFSET: usize = 0xFFFF_8000_0000_0000; @@ -99,13 +101,6 @@ impl Arch for AArch64Arch { } } -bitflags::bitflags! { - pub struct EntryFlags: usize { - const NO_CACHE = MEM_ATTR_NC << 2; - const DEV_MEM = MEM_ATTR_DEVICE_nGnRnE << 2; - } -} - #[cfg_attr(not(target_arch = "aarch64"), allow(unused))] const MEM_ATTR_WB: usize = 0; const MEM_ATTR_NC: usize = 1; diff --git a/rmm/src/arch/mod.rs b/rmm/src/arch/mod.rs index 28977f73e4..e08377714f 100644 --- a/rmm/src/arch/mod.rs +++ b/rmm/src/arch/mod.rs @@ -40,6 +40,8 @@ pub trait Arch: Clone + Copy { const ENTRY_FLAG_EXEC: usize; const ENTRY_FLAG_GLOBAL: usize; const ENTRY_FLAG_NO_GLOBAL: usize; + const ENTRY_FLAG_DEVICE_MEMORY: usize; + const ENTRY_FLAG_UNCACHEABLE: usize; const ENTRY_FLAG_WRITE_COMBINING: usize; const PHYS_OFFSET: usize; diff --git a/rmm/src/arch/riscv64/mod.rs b/rmm/src/arch/riscv64/mod.rs index bcb65af052..894ef349e4 100644 --- a/rmm/src/arch/riscv64/mod.rs +++ b/rmm/src/arch/riscv64/mod.rs @@ -3,10 +3,3 @@ pub use sv48::RiscV64Sv48Arch; mod sv39; mod sv48; - -bitflags::bitflags! { - pub struct EntryFlags: usize { - const NO_CACHE = 1 << 4; - const DEV_MEM = 0; - } -} diff --git a/rmm/src/arch/riscv64/sv39.rs b/rmm/src/arch/riscv64/sv39.rs index 0ac4265751..8bb5083944 100644 --- a/rmm/src/arch/riscv64/sv39.rs +++ b/rmm/src/arch/riscv64/sv39.rs @@ -30,7 +30,9 @@ impl Arch for RiscV64Sv39Arch { const ENTRY_FLAG_EXEC: usize = 1 << 3; const ENTRY_FLAG_GLOBAL: usize = 1 << 5; const ENTRY_FLAG_NO_GLOBAL: usize = 0; - const ENTRY_FLAG_WRITE_COMBINING: usize = 0; + const ENTRY_FLAG_DEVICE_MEMORY: usize = 0; // FIXME use Svpbmt + const ENTRY_FLAG_UNCACHEABLE: usize = 0; // FIXME use Svpbmt + const ENTRY_FLAG_WRITE_COMBINING: usize = 0; // FIXME use Svpbmt const PHYS_OFFSET: usize = 0xFFFF_FFC0_0000_0000; diff --git a/rmm/src/arch/riscv64/sv48.rs b/rmm/src/arch/riscv64/sv48.rs index 527aed80ee..fd421d0e44 100644 --- a/rmm/src/arch/riscv64/sv48.rs +++ b/rmm/src/arch/riscv64/sv48.rs @@ -26,7 +26,9 @@ impl Arch for RiscV64Sv48Arch { const ENTRY_FLAG_EXEC: usize = 1 << 3; const ENTRY_FLAG_GLOBAL: usize = 1 << 5; const ENTRY_FLAG_NO_GLOBAL: usize = 0; - const ENTRY_FLAG_WRITE_COMBINING: usize = 0; + const ENTRY_FLAG_DEVICE_MEMORY: usize = 0; // FIXME use Svpbmt + const ENTRY_FLAG_UNCACHEABLE: usize = 0; // FIXME use Svpbmt + const ENTRY_FLAG_WRITE_COMBINING: usize = 0; // FIXME use Svpbmt const PHYS_OFFSET: usize = 0xFFFF_8000_0000_0000; diff --git a/rmm/src/arch/x86.rs b/rmm/src/arch/x86.rs index a880492b6c..6f64deea4a 100644 --- a/rmm/src/arch/x86.rs +++ b/rmm/src/arch/x86.rs @@ -25,6 +25,8 @@ impl Arch for X86Arch { const ENTRY_FLAG_NO_GLOBAL: usize = 0; const ENTRY_FLAG_NO_EXEC: usize = 0; // NOT AVAILABLE UNLESS PAE IS USED! const ENTRY_FLAG_EXEC: usize = 0; + const ENTRY_FLAG_DEVICE_MEMORY: usize = 1 << 4; + const ENTRY_FLAG_UNCACHEABLE: usize = 1 << 4; const ENTRY_FLAG_WRITE_COMBINING: usize = 1 << 7; const PHYS_OFFSET: usize = 0x8000_0000; diff --git a/rmm/src/arch/x86_64.rs b/rmm/src/arch/x86_64.rs index b6d2338701..eafe7c06c1 100644 --- a/rmm/src/arch/x86_64.rs +++ b/rmm/src/arch/x86_64.rs @@ -24,6 +24,8 @@ impl Arch for X8664Arch { const ENTRY_FLAG_NO_GLOBAL: usize = 0; const ENTRY_FLAG_NO_EXEC: usize = 1 << 63; const ENTRY_FLAG_EXEC: usize = 0; + const ENTRY_FLAG_DEVICE_MEMORY: usize = 1 << 4; + const ENTRY_FLAG_UNCACHEABLE: usize = 1 << 4; const ENTRY_FLAG_WRITE_COMBINING: usize = 1 << 7; const PHYS_OFFSET: usize = Self::PAGE_NEGATIVE_MASK + (Self::PAGE_ADDRESS_SIZE >> 1) as usize; // PML4 slot 256 and onwards diff --git a/rmm/src/arch/x86_shared.rs b/rmm/src/arch/x86_shared.rs index b05c4f3339..003a79085e 100644 --- a/rmm/src/arch/x86_shared.rs +++ b/rmm/src/arch/x86_shared.rs @@ -1,10 +1,3 @@ -bitflags::bitflags! { - pub struct EntryFlags: usize { - const NO_CACHE = 1 << 4; - const DEV_MEM = 0; - } -} - /// Setup page attribute table #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] #[inline(always)] diff --git a/rmm/src/page/flags.rs b/rmm/src/page/flags.rs index 3d90691d60..0d83d60d59 100644 --- a/rmm/src/page/flags.rs +++ b/rmm/src/page/flags.rs @@ -56,6 +56,18 @@ impl PageFlags { self } + #[must_use] + #[inline(always)] + pub fn device_memory(self, value: bool) -> Self { + self.custom_flag(A::ENTRY_FLAG_DEVICE_MEMORY, value) + } + + #[must_use] + #[inline(always)] + pub fn uncacheable(self, value: bool) -> Self { + self.custom_flag(A::ENTRY_FLAG_UNCACHEABLE, value) + } + #[must_use] #[inline(always)] pub fn write_combining(self, value: bool) -> Self { diff --git a/src/acpi/hpet.rs b/src/acpi/hpet.rs index d25f4a5d1f..88b768816d 100644 --- a/src/acpi/hpet.rs +++ b/src/acpi/hpet.rs @@ -60,7 +60,7 @@ impl Hpet { unsafe { use crate::{ memory::{Frame, KernelMapper}, - paging::{EntryFlags, Page, VirtualAddress}, + paging::{Page, VirtualAddress}, }; use rmm::PageFlags; @@ -71,9 +71,7 @@ impl Hpet { .map_phys( page.start_address(), frame.base(), - PageFlags::new() - .write(true) - .custom_flag(EntryFlags::NO_CACHE.bits(), true), + PageFlags::new().write(true).device_memory(true), ) .expect("failed to map memory for GenericAddressStructure") .flush(); diff --git a/src/arch/aarch64/paging/mod.rs b/src/arch/aarch64/paging/mod.rs index ff42fdd688..fb1395c682 100644 --- a/src/arch/aarch64/paging/mod.rs +++ b/src/arch/aarch64/paging/mod.rs @@ -2,9 +2,7 @@ //! Some code was borrowed from [Phil Opp's Blog](http://os.phil-opp.com/modifying-page-tables.html) pub use super::CurrentRmmArch as RmmA; -pub use rmm::{ - aarch64::EntryFlags, Arch as RmmArch, PageFlags, PhysicalAddress, TableKind, VirtualAddress, -}; +pub use rmm::{Arch as RmmArch, PageFlags, PhysicalAddress, TableKind, VirtualAddress}; pub type PageMapper = rmm::PageMapper; diff --git a/src/arch/riscv64/paging/mod.rs b/src/arch/riscv64/paging/mod.rs index 0ac3e5dba5..993f4b445b 100644 --- a/src/arch/riscv64/paging/mod.rs +++ b/src/arch/riscv64/paging/mod.rs @@ -1,9 +1,7 @@ #![allow(unused)] pub use super::CurrentRmmArch as RmmA; -pub use rmm::{ - aarch64::EntryFlags, Arch as RmmArch, PageFlags, PhysicalAddress, TableKind, VirtualAddress, -}; +pub use rmm::{Arch as RmmArch, PageFlags, PhysicalAddress, TableKind, VirtualAddress}; pub type PageMapper = rmm::PageMapper; pub use crate::rmm::KernelMapper; diff --git a/src/arch/x86_shared/device/ioapic.rs b/src/arch/x86_shared/device/ioapic.rs index f0503a0c8d..2d6c96e26e 100644 --- a/src/arch/x86_shared/device/ioapic.rs +++ b/src/arch/x86_shared/device/ioapic.rs @@ -9,7 +9,7 @@ use crate::acpi::madt::{self, Madt, MadtEntry, MadtIntSrcOverride, MadtIoApic}; use crate::{ arch::interrupt::irq, memory::{Frame, KernelMapper}, - paging::{EntryFlags, Page, PageFlags, PhysicalAddress}, + paging::{Page, PageFlags, PhysicalAddress}, }; use super::{local_apic::ApicId, pic}; @@ -253,9 +253,7 @@ pub unsafe fn handle_ioapic(mapper: &mut KernelMapper, madt_ioapic: &'stat .map_phys( page.start_address(), frame.base(), - PageFlags::new() - .write(true) - .custom_flag(EntryFlags::NO_CACHE.bits(), true), + PageFlags::new().write(true).device_memory(true), ) .expect("failed to map I/O APIC") .flush(); diff --git a/src/arch/x86_shared/paging/mod.rs b/src/arch/x86_shared/paging/mod.rs index 42d5588c4f..4217f7a333 100644 --- a/src/arch/x86_shared/paging/mod.rs +++ b/src/arch/x86_shared/paging/mod.rs @@ -8,11 +8,6 @@ pub use rmm::{Arch as RmmArch, PageFlags, PhysicalAddress, TableKind, VirtualAdd pub type PageMapper = rmm::PageMapper; -#[cfg(target_arch = "x86")] -pub use rmm::x86::EntryFlags; -#[cfg(target_arch = "x86_64")] -pub use rmm::x86_64::EntryFlags; - pub mod mapper; /// Size of pages diff --git a/src/arch/x86_shared/pti.rs b/src/arch/x86_shared/pti.rs index 24866c31ad..0ecaf79854 100644 --- a/src/arch/x86_shared/pti.rs +++ b/src/arch/x86_shared/pti.rs @@ -4,8 +4,6 @@ use core::ptr; #[cfg(feature = "pti")] use crate::memory::Frame; #[cfg(feature = "pti")] -use crate::paging::entry::EntryFlags; -#[cfg(feature = "pti")] use crate::paging::ActivePageTable; #[cfg(feature = "pti")] diff --git a/src/memory/mod.rs b/src/memory/mod.rs index ce18237ecd..27145a4732 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -20,7 +20,7 @@ use crate::{ memory::{AccessMode, PfError}, }, kernel_executable_offsets::{__usercopy_end, __usercopy_start}, - paging::{EntryFlags, Page, PageFlags}, + paging::{Page, PageFlags}, sync::CleanLockToken, syscall::error::{Error, ENOMEM}, }; @@ -246,9 +246,7 @@ pub unsafe fn map_device_memory(addr: PhysicalAddress, len: usize) -> VirtualAdd let (_, flush) = mapper .map_linearly( base.add(page_idx * crate::memory::PAGE_SIZE), - PageFlags::new() - .write(true) - .custom_flag(EntryFlags::NO_CACHE.bits(), true), + PageFlags::new().write(true).device_memory(true), ) .expect("failed to linearly map SDT"); flush.flush(); diff --git a/src/scheme/memory.rs b/src/scheme/memory.rs index 4415aa95d1..c64e8f7324 100644 --- a/src/scheme/memory.rs +++ b/src/scheme/memory.rs @@ -9,7 +9,7 @@ use crate::{ memory::{handle_notify_files, AddrSpace, AddrSpaceWrapper, Grant, PageSpan}, }, memory::{free_frames, used_frames, Frame, PAGE_SIZE}, - paging::{EntryFlags, VirtualAddress}, + paging::VirtualAddress, sync::CleanLockToken, syscall::{ data::{Map, StatVfs}, @@ -150,19 +150,8 @@ impl MemoryScheme { MemoryType::Writeback => (), MemoryType::WriteCombining => page_flags = page_flags.write_combining(true), - - MemoryType::Uncacheable => { - page_flags = page_flags.custom_flag(EntryFlags::NO_CACHE.bits(), true) - } - - MemoryType::DeviceMemory => { - // MemoryType::DeviceMemory doesn't exist on x86 && x86_64, which instead support - // uncacheable, write-combining, write-through, write-protect, and write-back. - #[cfg(target_arch = "aarch64")] - { - page_flags = page_flags.custom_flag(EntryFlags::DEV_MEM.bits(), true); - } - } + MemoryType::Uncacheable => page_flags = page_flags.uncacheable(true), + MemoryType::DeviceMemory => page_flags = page_flags.device_memory(true), } Grant::physmap( diff --git a/src/startup/memory.rs b/src/startup/memory.rs index 24fc8f56c6..25d89edadf 100644 --- a/src/startup/memory.rs +++ b/src/startup/memory.rs @@ -1,5 +1,5 @@ use crate::{ - arch::{consts::KERNEL_OFFSET, paging::EntryFlags, rmm::page_flags, CurrentRmmArch}, + arch::{consts::KERNEL_OFFSET, rmm::page_flags, CurrentRmmArch}, memory::PAGE_SIZE, startup::{memory::BootloaderMemoryKind::Null, KernelArgs}, }; @@ -349,10 +349,7 @@ unsafe fn map_memory(areas: &[MemoryArea], mut bump_allocator: &mut Bum for i in 0..size / PAGE_SIZE { let phys = PhysicalAddress::new(base + i * PAGE_SIZE); let virt = A::phys_to_virt(phys); - // use the same mair_el1 value with bootloader, - // mair_el1 == 0x00000000000044FF - // set mem_attr == device memory - let flags = page_flags::(virt).custom_flag(EntryFlags::DEV_MEM.bits(), true); + let flags = page_flags::(virt).device_memory(true); let flush = mapper .map_phys(virt, phys, flags) .expect("failed to map frame");