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

fixing segmentation print #153

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ pub mod cpuid {

#[cfg(not(test))]
mod std {
pub use core::fmt;
pub use core::ops;
pub use core::option;



}

#[cfg(all(test, feature = "vmtest"))]
Expand Down
42 changes: 20 additions & 22 deletions src/segmentation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Functionality to manipulate segment registers, build segement
//! descriptors and selectors.

use bitflags::*;

use core::arch::asm;
Expand Down Expand Up @@ -44,6 +45,22 @@
self.bits >> 3
}

/// Returns the requested privilege level of the selector.
pub fn rpl(&self) -> Ring {
match self.bits & 0b11 {
0b00 => Ring::Ring0,
0b01 => Ring::Ring1,
0b10 => Ring::Ring2,
0b11 => Ring::Ring3,
_ => unreachable!(),
}
}

/// Returns the table indicator (TI) bit.
pub fn ti(&self) -> u16 {
(self.bits >> 2) & 1
}

/// Make a new segment selector from a untyped u16 value.
pub const fn from_raw(bits: u16) -> SegmentSelector {
SegmentSelector { bits }
Expand All @@ -52,36 +69,17 @@

impl fmt::Display for SegmentSelector {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let r0 = match self.contains(SegmentSelector::RPL_0) {
false => "",
true => "Ring 0 segment selector.",
};
let r1 = match self.contains(SegmentSelector::RPL_1) {
false => "",
true => "Ring 1 segment selector.",
};
let r2 = match self.contains(SegmentSelector::RPL_2) {
false => "",
true => "Ring 2 segment selector.",
};
let r3 = match self.contains(SegmentSelector::RPL_3) {
false => "",
true => "Ring 3 segment selector.",
};
let tbl = match self.contains(SegmentSelector::TI_LDT) {
false => "GDT Table",
true => "LDT Table",
};

write!(
f,
"Index {} in {}, {}{}{}{}",
self.bits >> 3,
"Index {} in {}, {:?} segment selector.",
self.index(),
tbl,
r0,
r1,
r2,
r3
self.rpl()
)
}
}
Expand Down Expand Up @@ -437,7 +435,7 @@
(self.upper as u64) << 32 | self.lower as u64
}

pub(crate) fn apply_builder_settings(&mut self, builder: &DescriptorBuilder) {

Check failure on line 438 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
if let Some(ring) = builder.dpl {
self.set_dpl(ring)
}
Expand Down Expand Up @@ -467,7 +465,7 @@

/// Create a new segment, TSS or LDT descriptor
/// by setting the three base and two limit fields.
pub fn set_base_limit(&mut self, base: u32, limit: u32) {

Check failure on line 468 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
// Clear the base and limit fields in Descriptor
self.lower = 0;
self.upper &= 0x00F0FF00;
Expand All @@ -485,7 +483,7 @@

/// Creates a new descriptor with selector and offset (for IDT Gate descriptors,
/// e.g. Trap, Interrupts and Task gates)
pub fn set_selector_offset(&mut self, selector: SegmentSelector, offset: u32) {

Check failure on line 486 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
// Clear the selector and offset
self.lower = 0;
self.upper &= 0x0000ffff;
Expand All @@ -502,18 +500,18 @@
/// Indicates the segment or gate type and specifies the kinds of access that can be made to the
/// segment and the direction of growth. The interpretation of this field depends on whether the descriptor
/// type flag specifies an application (code or data) descriptor or a system descriptor.
pub fn set_type(&mut self, typ: u8) {

Check failure on line 503 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
self.upper &= !(0x0f << 8); // clear
self.upper |= (typ as u32 & 0x0f) << 8;
}

/// Specifies whether the segment descriptor is for a system segment (S flag is clear) or a code or data segment (S flag is set).
pub fn set_s(&mut self) {

Check failure on line 509 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
self.upper |= bit!(12);
}

/// Specifies the privilege level of the segment. The DPL is used to control access to the segment.
pub fn set_dpl(&mut self, ring: Ring) {

Check failure on line 514 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
assert!(ring as u32 <= 0b11);
self.upper &= !(0b11 << 13);
self.upper |= (ring as u32) << 13;
Expand All @@ -523,12 +521,12 @@
/// Indicates whether the segment is present in memory (set) or not present (clear).
/// If this flag is clear, the processor generates a segment-not-present exception (#NP) when a segment selector
/// that points to the segment descriptor is loaded into a segment register.
pub fn set_p(&mut self) {

Check failure on line 524 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
self.upper |= bit!(15);
}

/// Set AVL bit. System software can use this bit to store information.
pub fn set_avl(&mut self) {

Check failure on line 529 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
self.upper |= bit!(20);
}

Expand All @@ -537,14 +535,14 @@
/// code segment contains native 64-bit code. A value of 1 indicates instructions in this code
/// segment are executed in 64-bit mode. A value of 0 indicates the instructions in this code segment
/// are executed in compatibility mode. If L-bit is set, then D-bit must be cleared.
pub fn set_l(&mut self) {

Check failure on line 538 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
self.upper |= bit!(21);
}

/// Set D/B.
/// Performs different functions depending on whether the segment descriptor is an executable code segment,
/// an expand-down data segment, or a stack segment.
pub fn set_db(&mut self) {

Check failure on line 545 in src/segmentation.rs

View workflow job for this annotation

GitHub Actions / ci (nightly)

mutable references are not allowed in constant functions
self.upper |= bit!(22);
}

Expand Down
Loading