@@ -14,7 +14,8 @@ use bit_field::BitField;
14
14
use bitflags:: bitflags;
15
15
use core:: fmt;
16
16
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 } ;
18
19
19
20
/// An Interrupt Descriptor Table with 256 entries.
20
21
///
@@ -442,6 +443,49 @@ impl InterruptDescriptorTable {
442
443
443
444
unsafe { lidt ( & ptr) } ;
444
445
}
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
+ }
445
489
}
446
490
447
491
impl Index < usize > for InterruptDescriptorTable {
0 commit comments