Skip to content

Commit 3374828

Browse files
foxcobphil-opp
authored andcommitted
Add slice and slice_mut methods to IDT (#95)
1 parent 70ea324 commit 3374828

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/structures/idt.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use bit_field::BitField;
1414
use bitflags::bitflags;
1515
use core::fmt;
1616
use core::marker::PhantomData;
17-
use core::ops::{Deref, Index, IndexMut};
17+
use core::ops::Bound::{Excluded, Included, Unbounded};
18+
use core::ops::{Deref, Index, IndexMut, RangeBounds};
1819

1920
/// An Interrupt Descriptor Table with 256 entries.
2021
///
@@ -442,6 +443,49 @@ impl InterruptDescriptorTable {
442443

443444
unsafe { lidt(&ptr) };
444445
}
446+
447+
/// Returns a normalized and ranged check slice range from a RangeBounds trait object
448+
///
449+
/// Panics if range is outside the range of user interrupts (i.e. greater than 255) or if the entry is an
450+
/// exception
451+
fn condition_slice_bounds(&self, bounds: impl RangeBounds<usize>) -> (usize, usize) {
452+
let lower_idx = match bounds.start_bound() {
453+
Included(start) => *start,
454+
Excluded(start) => *start + 1,
455+
Unbounded => 0,
456+
};
457+
let upper_idx = match bounds.end_bound() {
458+
Included(end) => *end + 1,
459+
Excluded(end) => *end,
460+
Unbounded => 256,
461+
};
462+
463+
if lower_idx > 256 || upper_idx > 256 {
464+
panic!("Index out of range [{}..{}]", lower_idx, upper_idx);
465+
}
466+
if lower_idx < 32 {
467+
panic!("Cannot return slice from traps, faults, and exception handlers");
468+
}
469+
(lower_idx, upper_idx)
470+
}
471+
472+
/// Returns slice of IDT entries with the specified range.
473+
///
474+
/// Panics if range is outside the range of user interrupts (i.e. greater than 255) or if the entry is an
475+
/// exception
476+
pub fn slice(&self, bounds: impl RangeBounds<usize>) -> &[Entry<HandlerFunc>] {
477+
let (lower_idx, upper_idx) = self.condition_slice_bounds(bounds);
478+
&self.interrupts[(lower_idx - 32)..(upper_idx - 32)]
479+
}
480+
481+
/// Returns a mutable slice of IDT entries with the specified range.
482+
///
483+
/// Panics if range is outside the range of user interrupts (i.e. greater than 255) or if the entry is an
484+
/// exception
485+
pub fn slice_mut(&mut self, bounds: impl RangeBounds<usize>) -> &mut [Entry<HandlerFunc>] {
486+
let (lower_idx, upper_idx) = self.condition_slice_bounds(bounds);
487+
&mut self.interrupts[(lower_idx - 32)..(upper_idx - 32)]
488+
}
445489
}
446490

447491
impl Index<usize> for InterruptDescriptorTable {

0 commit comments

Comments
 (0)