Skip to content

Commit ec4e49f

Browse files
Merge pull request #112 from rust-osdev/rewrite-fix-gdt-limit
Add a flat limit to all GDT segments
2 parents 8c7791c + df73047 commit ec4e49f

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/v86/src/gdt.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ impl GlobalDescriptorTable {
4848
limit: (self.table.len() * size_of::<u64>() - 1) as u16,
4949
};
5050

51-
llvm_asm!("lgdt ($0)" :: "r" (&ptr) : "memory");
51+
unsafe {
52+
llvm_asm!("lgdt ($0)" :: "r" (&ptr) : "memory");
53+
}
5254
}
5355

5456
#[inline]
@@ -87,6 +89,9 @@ bitflags! {
8789

8890
/// The DPL for this descriptor is Ring 3
8991
const DPL_RING_3 = 3 << 45;
92+
93+
/// If set, limit is in 4k pages
94+
const GRANULARITY = 1 << 55;
9095
}
9196
}
9297

@@ -98,7 +103,7 @@ impl Descriptor {
98103

99104
let flags =
100105
Flags::USER_SEGMENT | Flags::PRESENT | Flags::EXECUTABLE | Flags::READABLE_WRITABLE;
101-
Descriptor(flags.bits())
106+
Descriptor(flags.bits()).with_flat_limit()
102107
}
103108

104109
/// Creates a segment descriptor for a protected mode kernel data segment.
@@ -107,7 +112,7 @@ impl Descriptor {
107112
use self::DescriptorFlags as Flags;
108113

109114
let flags = Flags::USER_SEGMENT | Flags::PRESENT | Flags::READABLE_WRITABLE;
110-
Descriptor(flags.bits())
115+
Descriptor(flags.bits()).with_flat_limit()
111116
}
112117

113118
/// Creates a segment descriptor for a protected mode ring 3 data segment.
@@ -117,7 +122,7 @@ impl Descriptor {
117122

118123
let flags =
119124
Flags::USER_SEGMENT | Flags::PRESENT | Flags::READABLE_WRITABLE | Flags::DPL_RING_3;
120-
Descriptor(flags.bits())
125+
Descriptor(flags.bits()).with_flat_limit()
121126
}
122127

123128
/// Creates a segment descriptor for a protected mode ring 3 code segment.
@@ -130,7 +135,7 @@ impl Descriptor {
130135
| Flags::EXECUTABLE
131136
| Flags::DPL_RING_3
132137
| Flags::READABLE_WRITABLE;
133-
Descriptor(flags.bits())
138+
Descriptor(flags.bits()).with_flat_limit()
134139
}
135140

136141
/// Creates a TSS system descriptor for the given TSS.
@@ -152,6 +157,17 @@ impl Descriptor {
152157

153158
Descriptor(val)
154159
}
160+
161+
fn with_flat_limit(mut self) -> Self {
162+
// limit_low
163+
self.0.set_bits(0..16, 0xffff);
164+
// limit high
165+
self.0.set_bits(48..52, 0xff);
166+
// granularity
167+
self.0 |= DescriptorFlags::GRANULARITY.bits();
168+
169+
self
170+
}
155171
}
156172

157173
#[derive(Debug, Clone, Copy)]
@@ -224,7 +240,7 @@ pub struct Stack {
224240
}
225241

226242
impl Stack {
227-
fn zero() -> Self {
243+
const fn zero() -> Self {
228244
Stack { esp: 0, ss: 0 }
229245
}
230246
}

src/v86/src/idt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::marker::PhantomData;
2+
use bit_field::BitField;
23

34
/// An Interrupt Descriptor Table with 32 entries.
45
#[derive(Clone)]

0 commit comments

Comments
 (0)