@@ -47,7 +47,7 @@ use crate::registers::segmentation::{Segment, CS, SS};
47
47
#[ derive( Debug , Clone ) ]
48
48
pub struct GlobalDescriptorTable {
49
49
table : [ u64 ; 8 ] ,
50
- next_free : usize ,
50
+ len : usize ,
51
51
}
52
52
53
53
impl GlobalDescriptorTable {
@@ -56,7 +56,7 @@ impl GlobalDescriptorTable {
56
56
pub const fn new ( ) -> GlobalDescriptorTable {
57
57
GlobalDescriptorTable {
58
58
table : [ 0 ; 8 ] ,
59
- next_free : 1 ,
59
+ len : 1 ,
60
60
}
61
61
}
62
62
@@ -68,29 +68,29 @@ impl GlobalDescriptorTable {
68
68
/// * The provided slice **must not be larger than 8 items** (only up to the first 8 will be observed.)
69
69
#[ inline]
70
70
pub const unsafe fn from_raw_slice ( slice : & [ u64 ] ) -> GlobalDescriptorTable {
71
- let next_free = slice. len ( ) ;
71
+ let len = slice. len ( ) ;
72
72
let mut table = [ 0 ; 8 ] ;
73
73
let mut idx = 0 ;
74
74
75
75
assert ! (
76
- next_free <= 8 ,
76
+ len <= 8 ,
77
77
"initializing a GDT from a slice requires it to be **at most** 8 elements."
78
78
) ;
79
79
80
- while idx != next_free {
80
+ while idx < len {
81
81
table[ idx] = slice[ idx] ;
82
82
idx += 1 ;
83
83
}
84
84
85
- GlobalDescriptorTable { table, next_free }
85
+ GlobalDescriptorTable { table, len }
86
86
}
87
87
88
88
/// Get a reference to the internal table.
89
89
///
90
90
/// The resulting slice may contain system descriptors, which span two `u64`s.
91
91
#[ inline]
92
92
pub fn as_raw_slice ( & self ) -> & [ u64 ] {
93
- & self . table [ ..self . next_free ]
93
+ & self . table [ ..self . len ]
94
94
}
95
95
96
96
/// Adds the given segment descriptor to the GDT, returning the segment selector.
@@ -101,13 +101,13 @@ impl GlobalDescriptorTable {
101
101
pub fn add_entry ( & mut self , entry : Descriptor ) -> SegmentSelector {
102
102
let index = match entry {
103
103
Descriptor :: UserSegment ( value) => {
104
- if self . next_free > self . table . len ( ) - 1 {
104
+ if self . len > self . table . len ( ) - 1 {
105
105
panic ! ( "GDT full" )
106
106
}
107
107
self . push ( value)
108
108
}
109
109
Descriptor :: SystemSegment ( value_low, value_high) => {
110
- if self . next_free > self . table . len ( ) - 2 {
110
+ if self . len > self . table . len ( ) - 2 {
111
111
panic ! ( "GDT requires two free spaces to hold a SystemSegment" )
112
112
}
113
113
let index = self . push ( value_low) ;
@@ -165,9 +165,9 @@ impl GlobalDescriptorTable {
165
165
#[ inline]
166
166
#[ cfg_attr( feature = "const_fn" , rustversion:: attr( all( ) , const ) ) ]
167
167
fn push ( & mut self , value : u64 ) -> usize {
168
- let index = self . next_free ;
168
+ let index = self . len ;
169
169
self . table [ index] = value;
170
- self . next_free += 1 ;
170
+ self . len += 1 ;
171
171
index
172
172
}
173
173
@@ -178,7 +178,7 @@ impl GlobalDescriptorTable {
178
178
use core:: mem:: size_of;
179
179
super :: DescriptorTablePointer {
180
180
base : crate :: VirtAddr :: new ( self . table . as_ptr ( ) as u64 ) ,
181
- limit : ( self . next_free * size_of :: < u64 > ( ) - 1 ) as u16 ,
181
+ limit : ( self . len * size_of :: < u64 > ( ) - 1 ) as u16 ,
182
182
}
183
183
}
184
184
}
@@ -361,6 +361,7 @@ mod tests {
361
361
gdt. add_entry ( Descriptor :: UserSegment ( DescriptorFlags :: USER_CODE32 . bits ( ) ) ) ;
362
362
gdt. add_entry ( Descriptor :: user_data_segment ( ) ) ;
363
363
gdt. add_entry ( Descriptor :: user_code_segment ( ) ) ;
364
+ assert_eq ! ( gdt. len, 6 ) ;
364
365
gdt
365
366
}
366
367
@@ -369,6 +370,7 @@ mod tests {
369
370
fn make_full_gdt ( ) -> GlobalDescriptorTable {
370
371
let mut gdt = make_six_entry_gdt ( ) ;
371
372
gdt. add_entry ( Descriptor :: tss_segment ( & TSS ) ) ;
373
+ assert_eq ! ( gdt. len, 8 ) ;
372
374
gdt
373
375
}
374
376
@@ -377,7 +379,9 @@ mod tests {
377
379
// Make sure we don't panic with user segments
378
380
let mut gdt = make_six_entry_gdt ( ) ;
379
381
gdt. add_entry ( Descriptor :: user_data_segment ( ) ) ;
382
+ assert_eq ! ( gdt. len, 7 ) ;
380
383
gdt. add_entry ( Descriptor :: user_data_segment ( ) ) ;
384
+ assert_eq ! ( gdt. len, 8 ) ;
381
385
// Make sure we don't panic with system segments
382
386
let _ = make_full_gdt ( ) ;
383
387
}
0 commit comments