Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new PteFlags to replace old EntryFlags #708

Merged
merged 10 commits into from
Dec 5, 2022
21 changes: 9 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions applications/bm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use heapfile::HeapFile;
use path::Path;
use fs_node::{DirRef, FileOrDir, FileRef};
use libtest::*;
use memory::{create_mapping, EntryFlags};
use memory::{create_mapping, PteFlags};
use getopts::Options;
use mod_mgmt::crate_name_from_path;

Expand Down Expand Up @@ -514,7 +514,7 @@ fn do_memory_map_inner(overhead_ct: u64, th: usize, nr: usize) -> Result<u64, &'
start_hpet = hpet.get_counter();

for _ in 0..ITERATIONS{
let mapping = create_mapping(MAPPING_SIZE, EntryFlags::WRITABLE)?;
let mapping = create_mapping(MAPPING_SIZE, PteFlags::new().writable(true))?;
// write 0xFF to the first byte as lmbench does
unsafe{ *(mapping.start_address().value() as *mut u8) = 0xFF; }
drop(mapping);
Expand Down
18 changes: 12 additions & 6 deletions applications/loadc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use core::{
};
use alloc::{collections::BTreeSet, string::{String, ToString}, sync::Arc, vec::Vec};
use getopts::{Matches, Options};
use memory::{Page, MappedPages, VirtualAddress, EntryFlags};
use memory::{Page, MappedPages, VirtualAddress, PteFlagsArch, PteFlags};
use mod_mgmt::{CrateNamespace, StrongDependency, find_symbol_table, RelocationEntry, write_relocation};
use rustc_demangle::demangle;
use path::Path;
Expand Down Expand Up @@ -140,7 +140,7 @@ pub struct LoadedSegment {
/// (may be a subset)
bounds: Range<VirtualAddress>,
/// The proper flags for this segment specified by the ELF file.
flags: EntryFlags,
flags: PteFlagsArch,
/// The indices of the sections in the ELF file
/// that were grouped ("mapped") into this segment by the linker.
section_ndxs: BTreeSet<usize>,
Expand Down Expand Up @@ -276,11 +276,11 @@ fn parse_and_load_elf_executable<'f>(
// debug!("Successfully split pages into {:?} and {:?}", this_ap, all_pages);
// debug!("Adjusted segment vaddr: {:#X}, size: {:#X}, {:?}", start_vaddr, memory_size_in_bytes, this_ap.start_address());

let initial_flags = EntryFlags::from_elf_program_flags(prog_hdr.flags());
let initial_flags = convert_to_pte_flags(prog_hdr.flags());
let mmi = task::with_current_task(|t| t.mmi.clone()).unwrap();
// Must initially map the memory as writable so we can copy the segment data to it later.
let mut mp = mmi.lock().page_table
.map_allocated_pages(this_ap, initial_flags | EntryFlags::WRITABLE)
.map_allocated_pages(this_ap, initial_flags.writable(true))
.map_err(String::from)?;

// Copy data from this section into the correct offset into our newly-mapped pages
Expand Down Expand Up @@ -318,7 +318,7 @@ fn parse_and_load_elf_executable<'f>(
mapped_segments.push(LoadedSegment {
mp,
bounds: segment_bounds,
flags: initial_flags,
flags: initial_flags.into(),
section_ndxs,
sections_i_depend_on: Vec::new(), // this is populated later in `overwrite_relocations()`
});
Expand Down Expand Up @@ -496,11 +496,17 @@ fn overwrite_relocations(
Ok(())
}

/// Converts the given ELF program flags into `PteFlags`.
fn convert_to_pte_flags(prog_flags: xmas_elf::program::Flags) -> PteFlags {
PteFlags::new()
.valid(prog_flags.is_read())
.writable(prog_flags.is_read())
.executable(prog_flags.is_execute())
}

fn print_usage(opts: Options) {
println!("{}", opts.usage(USAGE));
}


const USAGE: &'static str = "Usage: loadc [ARGS] PATH
Loads C language ELF executables on Theseus.";
3 changes: 1 addition & 2 deletions applications/test_filerw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ fn test_filerw() -> Result<(), &'static str> {
// we'll allocate the buffer length plus the offset because that's guranteed to be the most bytes we
// need (because it entered this conditional statement)
let pages = memory::allocate_pages_by_bytes(1).ok_or("could not allocate pages")?;
// the default flag is that the MappedPages are not writable
let mapped_pages = kernel_mmi_ref.lock().page_table.map_allocated_pages(pages, Default::default())?;
let mapped_pages = kernel_mmi_ref.lock().page_table.map_allocated_pages(pages, memory::PteFlags::new())?;

let non_writable_file = MemFile::from_mapped_pages(mapped_pages, "non-writable testfile".to_string(), 1, &parent)?;
match non_writable_file.lock().write_at(&mut string_slice_as_bytes, 0) {
Expand Down
8 changes: 4 additions & 4 deletions kernel/acpi/acpi_table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate sdt;
extern crate zerocopy;

use alloc::collections::BTreeMap;
use memory::{MappedPages, allocate_pages, allocate_frames_at, PageTable, EntryFlags, PhysicalAddress, Frame, FrameRange};
use memory::{MappedPages, allocate_pages, allocate_frames_at, PageTable, PteFlags, PhysicalAddress, Frame, FrameRange};
use sdt::Sdt;
use core::ops::Add;
use zerocopy::FromBytes;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl AcpiTables {
let new_mapped_pages = page_table.map_allocated_pages_to(
new_pages,
af,
EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE,
PteFlags::new().valid(true).writable(true),
)?;

self.adjust_mapping_offsets(new_frames, new_mapped_pages);
Expand All @@ -114,7 +114,7 @@ impl AcpiTables {
let new_mapped_pages = page_table.map_allocated_pages_to(
new_pages,
af,
EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE,
PteFlags::new().valid(true).writable(true),
)?;

self.adjust_mapping_offsets(new_frames, new_mapped_pages);
Expand Down Expand Up @@ -142,7 +142,7 @@ impl AcpiTables {
let new_mapped_pages = page_table.map_allocated_pages_to(
new_pages,
af,
EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE,
PteFlags::new().valid(true).writable(true),
)?;
// No real need to adjust mapping offsets here, since we've only appended frames (not prepended);
// we call this just to set the new frames and new mapped pages
Expand Down
6 changes: 3 additions & 3 deletions kernel/acpi/hpet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use log::debug;
use volatile::{Volatile, ReadOnly};
use zerocopy::FromBytes;
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
use memory::{allocate_pages, allocate_frames_by_bytes_at, PageTable, PhysicalAddress, EntryFlags, BorrowedMappedPages, Mutable};
use memory::{allocate_pages, allocate_frames_by_bytes_at, PageTable, PhysicalAddress, PteFlags, BorrowedMappedPages, Mutable};
use sdt::{Sdt, GenericAddressStructure};
use acpi_table::{AcpiTables, AcpiSignature};
use static_assertions::const_assert_eq;
Expand Down Expand Up @@ -175,8 +175,8 @@ impl HpetAcpiTable {
.ok_or("Couldn't allocate pages for HPET")?;
let hpet_mp = page_table.map_allocated_pages_to(
pages,
frames,
EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_CACHE | EntryFlags::NO_EXECUTE,
frames,
PteFlags::new().valid(true).writable(true).device_memory(true),
)?;

let mut hpet = hpet_mp.into_borrowed_mut::<Hpet>(phys_addr.frame_offset())
Expand Down
4 changes: 2 additions & 2 deletions kernel/acpi/rsdp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern crate zerocopy;
#[macro_use] extern crate static_assertions;

use core::mem;
use memory::{PageTable, MappedPages, PhysicalAddress, allocate_pages_by_bytes, allocate_frames_by_bytes_at, EntryFlags, BorrowedMappedPages};
use memory::{PageTable, MappedPages, PhysicalAddress, allocate_pages_by_bytes, allocate_frames_by_bytes_at, PteFlags, BorrowedMappedPages};
use zerocopy::FromBytes;

/// The starting physical address of the region of memory where the RSDP table exists.
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Rsdp {
let pages = allocate_pages_by_bytes(size).ok_or("couldn't allocate pages")?;
let frames_to_search = allocate_frames_by_bytes_at(PhysicalAddress::new_canonical(RSDP_SEARCH_START), size)
.map_err(|_e| "Couldn't allocate physical frames when searching for RSDP")?;
let mapped_pages = page_table.map_allocated_pages_to(pages, frames_to_search, EntryFlags::PRESENT)?;
let mapped_pages = page_table.map_allocated_pages_to(pages, frames_to_search, PteFlags::new().valid(true))?;
Rsdp::search(mapped_pages)
}

Expand Down
4 changes: 2 additions & 2 deletions kernel/apic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use spin::Once;
use raw_cpuid::CpuId;
use msr::*;
use irq_safety::RwLockIrqSafe;
use memory::{PageTable, PhysicalAddress, EntryFlags, MappedPages, allocate_pages, allocate_frames_at, AllocatedFrames, BorrowedMappedPages, Mutable};
use memory::{PageTable, PhysicalAddress, PteFlags, MappedPages, allocate_pages, allocate_frames_at, AllocatedFrames, BorrowedMappedPages, Mutable};
use kernel_config::time::CONFIG_TIMESLICE_PERIOD_MICROSECONDS;
use atomic_linked_list::atomic_map::AtomicMap;
use crossbeam_utils::atomic::AtomicCell;
Expand Down Expand Up @@ -158,7 +158,7 @@ fn map_apic(page_table: &mut PageTable) -> Result<MappedPages, &'static str> {
page_table,
new_page,
frame,
EntryFlags::WRITABLE | EntryFlags::NO_CACHE | EntryFlags::NO_EXECUTE,
PteFlags::new().valid(true).writable(true).device_memory(true),
)
}
}
Expand Down
1 change: 1 addition & 0 deletions kernel/crate_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
spin = "0.9.0"
xmas-elf = { version = "0.6.2", git = "https://github.com/theseus-os/xmas-elf.git" }
qp-trie = "0.8.0"
static_assertions = "1.1.0"

[dependencies.str_ref]
path = "../../libs/str_ref"
Expand Down
37 changes: 25 additions & 12 deletions kernel/crate_metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ use alloc::{
sync::{Arc, Weak},
vec::Vec,
};
use memory::{MappedPages, VirtualAddress, EntryFlags};
#[cfg(internal_deps)]
use memory::PageTable;
use memory::{MappedPages, VirtualAddress, PteFlags};
use cow_arc::{CowArc, CowWeak};
use fs_node::{FileRef, WeakFileRef};
use hashbrown::HashMap;
use goblin::elf::reloc::*;
use static_assertions::const_assert;

pub use str_ref::StrRef;
pub use crate_metadata_serde::{
Expand All @@ -90,11 +89,25 @@ pub type StrongSectionRef = Arc<LoadedSection>;
pub type WeakSectionRef = Weak<LoadedSection>;

/// `.text` sections are read-only and executable.
pub const TEXT_SECTION_FLAGS: EntryFlags = EntryFlags::PRESENT;
pub const TEXT_SECTION_FLAGS: PteFlags = PteFlags::from_bits_truncate(
(PteFlags::new().bits() | PteFlags::VALID.bits())
& !PteFlags::NOT_EXECUTABLE.bits() // clear the no-exec bits
);
/// `.rodata` sections are read-only and non-executable.
pub const RODATA_SECTION_FLAGS: EntryFlags = EntryFlags::from_bits_truncate(EntryFlags::PRESENT.bits() | EntryFlags::NO_EXECUTE.bits());
pub const RODATA_SECTION_FLAGS: PteFlags = PteFlags::from_bits_truncate(
(PteFlags::new().bits() | PteFlags::VALID.bits())
& !PteFlags::WRITABLE.bits()
);
/// `.data` and `.bss` sections are read-write and non-executable.
pub const DATA_BSS_SECTION_FLAGS: EntryFlags = EntryFlags::from_bits_truncate(EntryFlags::PRESENT.bits() | EntryFlags::NO_EXECUTE.bits() | EntryFlags::WRITABLE.bits());
pub const DATA_BSS_SECTION_FLAGS: PteFlags = PteFlags::from_bits_truncate(
(PteFlags::new().bits() | PteFlags::VALID.bits())
| PteFlags::WRITABLE.bits()
);

// Double-check section flags were defined correctly.
const_assert!(TEXT_SECTION_FLAGS.is_executable() && !TEXT_SECTION_FLAGS.is_writable());
const_assert!(!RODATA_SECTION_FLAGS.is_writable() && !RODATA_SECTION_FLAGS.is_executable());
const_assert!(DATA_BSS_SECTION_FLAGS.is_writable() && !DATA_BSS_SECTION_FLAGS.is_executable());


/// The Theseus Makefile appends prefixes onto bootloader module names,
Expand Down Expand Up @@ -379,19 +392,19 @@ impl LoadedCrate {
#[cfg(internal_deps)]
pub fn deep_copy(
&self,
page_table: &mut PageTable,
page_table: &mut memory::PageTable,
) -> Result<StrongCrateRef, &'static str> {

// This closure deep copies the given mapped_pages (mapping them as WRITABLE)
// and recalculates the the range of addresses covered by the new mapping.
let mut deep_copy_mp = |old_mp_range: &(Arc<Mutex<MappedPages>>, Range<VirtualAddress>), flags: EntryFlags|
let mut deep_copy_mp = |old_mp_range: &(Arc<Mutex<MappedPages>>, Range<VirtualAddress>), flags: PteFlags|
-> Result<(Arc<Mutex<MappedPages>>, Range<VirtualAddress>), &'static str>
{
let old_mp_locked = old_mp_range.0.lock();
let old_start_address = old_mp_range.1.start.value();
let size = old_mp_range.1.end.value() - old_start_address;
let offset = old_start_address - old_mp_locked.start_address().value();
let new_mp = old_mp_range.0.lock().deep_copy(Some(flags | EntryFlags::WRITABLE), page_table)?;
let new_mp = old_mp_range.0.lock().deep_copy(page_table, Some(flags.writable(true)))?;
let new_start_address = new_mp.start_address() + offset;
Ok((Arc::new(Mutex::new(new_mp)), new_start_address .. (new_start_address + size)))
};
Expand Down Expand Up @@ -523,7 +536,7 @@ impl LoadedCrate {
strong_dep.relocation,
new_sec_slice,
new_sec_mapped_pages_offset,
source_sec.start_address(),
source_sec.virt_addr,
true
)?;

Expand All @@ -549,10 +562,10 @@ impl LoadedCrate {
// to ensure that we don't cause deadlock by trying to lock the same section twice.
let source_sec_vaddr = if Arc::ptr_eq(source_sec, new_sec) {
// here: the source_sec and new_sec are the same, so just use the already-locked new_sec
new_sec.start_address()
new_sec.virt_addr
} else {
// here: the source_sec and new_sec are different, so we can go ahead and safely lock the source_sec
source_sec.start_address()
source_sec.virt_addr
};
write_relocation(
internal_dep.relocation,
Expand Down
6 changes: 3 additions & 3 deletions kernel/crate_swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ use core::{
fmt,
ops::Deref,
};
use spin::Mutex;
use alloc::{
borrow::Cow,
collections::BTreeSet,
string::{String, ToString},
sync::Arc,
vec::Vec,
};
use spin::Mutex;
use hashbrown::HashMap;
use memory::{EntryFlags, MmiRef};
use memory::MmiRef;
use fs_node::{FsNode, FileOrDir, FileRef, DirRef};
use mod_mgmt::{
CrateNamespace,
Expand Down Expand Up @@ -428,7 +428,7 @@ pub fn swap_crates(
let mut target_sec_mapped_pages = target_sec.mapped_pages.lock();
let target_sec_initial_flags = target_sec_mapped_pages.flags();
if !target_sec_initial_flags.is_writable() {
target_sec_mapped_pages.remap(&mut kernel_mmi_ref.lock().page_table, target_sec_initial_flags | EntryFlags::WRITABLE)?;
target_sec_mapped_pages.remap(&mut kernel_mmi_ref.lock().page_table, target_sec_initial_flags.writable(true))?;
}

write_relocation(
Expand Down
Loading