From 238cc575c369c338c1c040dc0a720076df3cac7c Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 19 Apr 2017 12:09:27 +0200 Subject: [PATCH] Add a Gdt::add_entry method --- src/interrupts/gdt.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/interrupts/gdt.rs b/src/interrupts/gdt.rs index 50e73e5..a341dae 100644 --- a/src/interrupts/gdt.rs +++ b/src/interrupts/gdt.rs @@ -1,4 +1,6 @@ use x86_64::structures::tss::TaskStateSegment; +use x86_64::structures::gdt::SegmentSelector; +use x86_64::PrivilegeLevel; pub struct Gdt { table: [u64; 8], @@ -12,6 +14,29 @@ impl Gdt { next_free: 1, } } + + pub fn add_entry(&mut self, entry: Descriptor) -> SegmentSelector { + let index = match entry { + Descriptor::UserSegment(value) => self.push(value), + Descriptor::SystemSegment(value_low, value_high) => { + let index = self.push(value_low); + self.push(value_high); + index + } + }; + SegmentSelector::new(index as u16, PrivilegeLevel::Ring0) + } + + fn push(&mut self, value: u64) -> usize { + if self.next_free < self.table.len() { + let index = self.next_free; + self.table[index] = value; + self.next_free += 1; + index + } else { + panic!("GDT full"); + } + } } pub enum Descriptor {