Skip to content

Commit

Permalink
gdt: Remove the segment selector for GS
Browse files Browse the repository at this point in the history
It turns out that we don't need to have a segment selector for GS
to be able to use the GSBase MSR, as discussed in rust-osdev/x86_64#333.
I suspect this is because we now have a kernel data segment selector
for SS, which we didn't when I first added support for CPU-local
data. Removing the extra selector frees up a slot in the GDT.

Signed-off-by: SlyMarbo <the.sly.marbo@googlemail.com>
  • Loading branch information
SlyMarbo committed Jan 16, 2022
1 parent ae242fe commit a186f04
Showing 2 changed files with 2 additions and 8 deletions.
6 changes: 1 addition & 5 deletions kernel/src/gdt.rs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
//! have no effect.
use lazy_static::lazy_static;
use x86_64::instructions::segmentation::{Segment, CS, GS, SS};
use x86_64::instructions::segmentation::{Segment, CS, SS};
use x86_64::instructions::tables::load_tss;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment;
@@ -33,7 +33,6 @@ pub fn init() {
unsafe {
CS::set_reg(GDT.1.kernel_code_selector);
SS::set_reg(GDT.1.kernel_stack_selector);
GS::set_reg(GDT.1.cpu_local_selector);
load_tss(GDT.1.tss_selector);
}
}
@@ -44,14 +43,12 @@ lazy_static! {
let kernel_code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
let kernel_stack_selector = gdt.add_entry(Descriptor::kernel_data_segment());
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
let cpu_local_selector = gdt.add_entry(Descriptor::kernel_data_segment());
(
gdt,
Selectors {
kernel_code_selector,
kernel_stack_selector,
tss_selector,
cpu_local_selector,
},
)
};
@@ -61,7 +58,6 @@ struct Selectors {
kernel_code_selector: SegmentSelector,
kernel_stack_selector: SegmentSelector,
tss_selector: SegmentSelector,
cpu_local_selector: SegmentSelector,
}

// Set up the task state segment with a safe
4 changes: 1 addition & 3 deletions kernel/src/multitasking/cpu_local.rs
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ use core::mem::size_of;
use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use x86_64::addr::align_up;
use x86_64::instructions::interrupts;
use x86_64::instructions::segmentation::{Segment, CS, GS, SS};
use x86_64::instructions::segmentation::{Segment, CS, SS};
use x86_64::instructions::tables::load_tss;
use x86_64::registers::model_specific::GsBase;
use x86_64::registers::model_specific::Msr;
@@ -161,7 +161,6 @@ pub fn init(cpu_id: CpuId, stack_space: &VirtAddrRange) {
let kernel_code_selector = data.gdt.add_entry(Descriptor::kernel_code_segment());
let kernel_stack_selector = data.gdt.add_entry(Descriptor::kernel_data_segment());
let tss_selector = data.gdt.add_entry(Descriptor::tss_segment(tss_ref));
let cpu_local_selector = data.gdt.add_entry(Descriptor::kernel_data_segment());
let user_code_selector = data.gdt.add_entry(Descriptor::user_code_segment());
let user_stack_selector = data.gdt.add_entry(Descriptor::user_data_segment());

@@ -170,7 +169,6 @@ pub fn init(cpu_id: CpuId, stack_space: &VirtAddrRange) {
unsafe {
CS::set_reg(kernel_code_selector);
SS::set_reg(kernel_stack_selector);
GS::set_reg(cpu_local_selector);
GsBase::write(start); // Set the GS base again now we've updated GS.
load_tss(tss_selector);

0 comments on commit a186f04

Please sign in to comment.