Skip to content

Cleanup const_fn! #255

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

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Unreleased

- The following methods no longer require the `nightly` feature to be `const fn`s`:
- `PageTable::new`
- `GlobalDescriptorTable::from_raw_slice`
- `MappedFrame::{start_address, size}`
- `Page<Size4KiB>::p1_index`
- Add `Debug` implementation for `InterruptDescriptorTable` ([#253](https://github.com/rust-osdev/x86_64/pull/253))
- Improve `Debug` implementations for `Entry` and `EntryOptions`

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! and access to various system registers.

#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "const_fn", feature(const_panic))]
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))]
#![cfg_attr(feature = "const_fn", feature(const_fn_fn_ptr_basics))]
#![cfg_attr(feature = "const_fn", feature(const_fn_trait_bound))]
#![cfg_attr(feature = "const_fn", feature(const_panic))] // Better panic messages
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))] // GDT add_entry()
#![cfg_attr(feature = "const_fn", feature(const_fn_fn_ptr_basics))] // IDT new()
#![cfg_attr(feature = "const_fn", feature(const_fn_trait_bound))] // PageSize marker trait
#![cfg_attr(feature = "inline_asm", feature(asm))]
#![cfg_attr(feature = "abi_x86_interrupt", feature(abi_x86_interrupt))]
#![cfg_attr(docsrs, feature(doc_cfg))]
Expand Down
14 changes: 8 additions & 6 deletions src/structures/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,19 @@ impl GlobalDescriptorTable {
/// * The user must make sure that the entries are well formed
/// * The provided slice **must not be larger than 8 items** (only up to the first 8 will be observed.)
#[inline]
#[cfg(feature = "const_fn")]
pub const unsafe fn from_raw_slice(slice: &[u64]) -> GlobalDescriptorTable {
assert!(
slice.len() <= 8,
"initializing a GDT from a slice requires it to be **at most** 8 elements."
);
let next_free = slice.len();

let mut table = [0; 8];
let mut idx = 0;

#[cfg(feature = "const_fn")]
assert!(
next_free <= 8,
"initializing a GDT from a slice requires it to be **at most** 8 elements."
);
#[cfg(not(feature = "const_fn"))]
table[next_free]; // Will fail if slice.len() > 8

Comment on lines +120 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we use assert! in every case? Also, table[next_free] panics if slice.len() >= 8, not slice.len() > 8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert! doesn't work as const panics are not yet stable. See: rust-lang/rust#85194

Good pont on the off-by-one error though, i'll fix it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josephlr Did we end up fixing this yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I forgot about this. PR for fix: #269

while idx != next_free {
table[idx] = slice[idx];
idx += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct PhysFrame<S: PageSize = Size4KiB> {
start_address: PhysAddr,
pub(crate) start_address: PhysAddr, // TODO: remove when start_address() is const
size: PhantomData<S>,
}

Expand Down
28 changes: 12 additions & 16 deletions src/structures/paging/mapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,21 @@ pub enum MappedFrame {
}

impl MappedFrame {
const_fn! {
/// Returns the start address of the frame.
pub fn start_address(&self) -> PhysAddr {
match self {
MappedFrame::Size4KiB(frame) => frame.start_address(),
MappedFrame::Size2MiB(frame) => frame.start_address(),
MappedFrame::Size1GiB(frame) => frame.start_address(),
}
/// Returns the start address of the frame.
pub const fn start_address(&self) -> PhysAddr {
match self {
MappedFrame::Size4KiB(frame) => frame.start_address,
MappedFrame::Size2MiB(frame) => frame.start_address,
MappedFrame::Size1GiB(frame) => frame.start_address,
}
}

const_fn! {
/// Returns the size the frame (4KB, 2MB or 1GB).
pub fn size(&self) -> u64 {
match self {
MappedFrame::Size4KiB(frame) => frame.size(),
MappedFrame::Size2MiB(frame) => frame.size(),
MappedFrame::Size1GiB(frame) => frame.size(),
}
/// Returns the size the frame (4KB, 2MB or 1GB).
pub const fn size(&self) -> u64 {
match self {
MappedFrame::Size4KiB(_) => Size4KiB::SIZE,
MappedFrame::Size2MiB(_) => Size2MiB::SIZE,
MappedFrame::Size1GiB(_) => Size1GiB::SIZE,
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,10 @@ impl Page<Size4KiB> {
Page::containing_address(VirtAddr::new(addr))
}

const_fn! {
/// Returns the level 1 page table index of this page.
#[inline]
pub fn p1_index(self) -> PageTableIndex {
self.start_address().p1_index()
}
/// Returns the level 1 page table index of this page.
#[inline]
pub const fn p1_index(self) -> PageTableIndex {
self.start_address.p1_index()
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/structures/paging/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ pub struct PageTable {

impl PageTable {
/// Creates an empty page table.
#[cfg(feature = "const_fn")]
#[inline]
pub const fn new() -> Self {
const EMPTY: PageTableEntry = PageTableEntry::new();
Expand All @@ -198,16 +197,6 @@ impl PageTable {
}
}

/// Creates an empty page table.
#[cfg(not(feature = "const_fn"))]
#[inline]
pub fn new() -> Self {
const EMPTY: PageTableEntry = PageTableEntry::new();
PageTable {
entries: [EMPTY; ENTRY_COUNT],
}
}

/// Clears all entries.
#[inline]
pub fn zero(&mut self) {
Expand Down