@@ -14,10 +14,13 @@ use crate::registers::segmentation::{Segment, CS, SS};
14
14
/// In 64-bit mode, segmentation is not supported. The GDT is used nonetheless, for example for
15
15
/// switching between user and kernel mode or for loading a TSS.
16
16
///
17
- /// The GDT has a fixed size of 8 entries, trying to add more entries will panic.
17
+ /// The GDT has a fixed maximum size given by the `MAX` const generic parameter.
18
+ /// Trying to add more entries than this maximum via [`GlobalDescriptorTable::add_entry`]
19
+ /// will panic.
18
20
///
19
21
/// You do **not** need to add a null segment descriptor yourself - this is already done
20
- /// internally.
22
+ /// internally. This means you can add up to `MAX - 1` additional [`Descriptor`]s to
23
+ /// this table.
21
24
///
22
25
/// Data segment registers in ring 0 can be loaded with the null segment selector. When running in
23
26
/// ring 3, the `ss` register must point to a valid data segment which can be obtained through the
@@ -45,17 +48,24 @@ use crate::registers::segmentation::{Segment, CS, SS};
45
48
/// ```
46
49
47
50
#[ derive( Debug , Clone ) ]
48
- pub struct GlobalDescriptorTable {
49
- table : [ u64 ; 8 ] ,
51
+ pub struct GlobalDescriptorTable < const MAX : usize = 8 > {
52
+ table : [ u64 ; MAX ] ,
50
53
next_free : usize ,
51
54
}
52
55
53
56
impl GlobalDescriptorTable {
54
- /// Creates an empty GDT.
57
+ /// Creates an empty GDT with the default length of 8.
58
+ pub const fn new ( ) -> Self {
59
+ Self :: empty ( )
60
+ }
61
+ }
62
+
63
+ impl < const MAX : usize > GlobalDescriptorTable < MAX > {
64
+ /// Creates an empty GDT which can hold `MAX` number of [`Descriptor`]s.
55
65
#[ inline]
56
- pub const fn new ( ) -> GlobalDescriptorTable {
57
- GlobalDescriptorTable {
58
- table : [ 0 ; 8 ] ,
66
+ pub const fn empty ( ) -> Self {
67
+ Self {
68
+ table : [ 0 ; MAX ] ,
59
69
next_free : 1 ,
60
70
}
61
71
}
@@ -67,13 +77,13 @@ impl GlobalDescriptorTable {
67
77
/// * The user must make sure that the entries are well formed
68
78
/// * The provided slice **must not be larger than 8 items** (only up to the first 8 will be observed.)
69
79
#[ inline]
70
- pub const unsafe fn from_raw_slice ( slice : & [ u64 ] ) -> GlobalDescriptorTable {
80
+ pub const unsafe fn from_raw_slice ( slice : & [ u64 ] ) -> Self {
71
81
let next_free = slice. len ( ) ;
72
- let mut table = [ 0 ; 8 ] ;
82
+ let mut table = [ 0 ; MAX ] ;
73
83
let mut idx = 0 ;
74
84
75
85
assert ! (
76
- next_free <= 8 ,
86
+ next_free <= MAX ,
77
87
"initializing a GDT from a slice requires it to be **at most** 8 elements."
78
88
) ;
79
89
@@ -82,7 +92,7 @@ impl GlobalDescriptorTable {
82
92
idx += 1 ;
83
93
}
84
94
85
- GlobalDescriptorTable { table, next_free }
95
+ Self { table, next_free }
86
96
}
87
97
88
98
/// Get a reference to the internal table.
0 commit comments