fixing some gdt shit
All checks were successful
Continuous Integration / Check (push) Successful in 45s
Continuous Integration / Clippy (push) Successful in 45s

This commit is contained in:
August 2026-07-03 00:43:01 +00:00
parent bf0e319198
commit 58d684564f
Signed by: shibedrill
SSH Key Fingerprint: SHA256:M0m3JW1s38BgO2t0fG146Yxd9OJ2IOqkvCAsuRHQ6Pw
7 changed files with 1825 additions and 27 deletions

BIN
.tlb.txt.kate-swp Normal file

Binary file not shown.

View File

@ -150,7 +150,7 @@ args = ["-drive", "file=build/gila.iso,format=raw,index=0,media=disk"]
[tasks.monitor_run]
dependencies = ["iso"]
command = "${QEMUCOMMAND}"
args = ["-display", "none", "-drive", "file=build/gila.iso,format=raw,index=0,media=disk", "-monitor", "stdio"]
args = ["-d", "int", "-display", "none", "-drive", "file=build/gila.iso,format=raw,index=0,media=disk", "-monitor", "stdio"]
[tasks.run]
dependencies = ["iso"]

46
log.txt Normal file
View File

@ -0,0 +1,46 @@
check_exception old: 0xffffffff new 0xe // Page Fault
0: v=0e e=0015 i=0 cpl=3 IP=001b:ffffffff800175b0 pc=ffffffff800175b0 SP=0023:ffffffff8f000000 CR2=ffffffff800175b0 // Correct address
RAX=0000000000000200 RBX=0000000000000000 RCX=ffffffff800175b0 RDX=ffffffff8f000000
RSI=000000000000001b RDI=ffffffff8f000023 RBP=0000000000000000 RSP=ffffffff8f000000
R8 =ffffffff8f000000 R9 =ffffffff800175b0 R10=0000000000000000 R11=0000000000000000
R12=0000000000000000 R13=0000000000000000 R14=0000000000000000 R15=0000000000000000
RIP=ffffffff800175b0 RFL=00000202 [-------] CPL=3 II=0 A20=1 SMM=0 HLT=0
ES =0000 0000000000000000 00000000 00001300
CS =001b 0000000000000000 ffffffff 00affb00 DPL=3 CS64 [-RA]
SS =0023 0000000000000000 ffffffff 00aff300 DPL=3 DS [-WA]
DS =0000 0000000000000000 00000000 00001300
FS =0000 0000000000000000 00000000 00001300
GS =0000 0000000000000000 00000000 00001300
LDT=0000 0000000000000000 00000000 00008200 DPL=0 LDT
TR =0038 0000000000000000 00000000 00008900 DPL=0 TSS64-avl
GDT= ffffffff80044498 00000037
IDT= ffffffff800433d0 00000fff
CR0=80010011 CR2=ffffffff800175b0 CR3=0000000007d9e000 CR4=00000020
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=0000000000000000 CCD=ffff800007dad588 CCO=EFLAGS
EFER=0000000000000d00
check_exception old: 0xe new 0xd // General Protection Fault
1: v=08 e=0000 i=0 cpl=3 IP=001b:ffffffff800175b0 pc=ffffffff800175b0 SP=0023:ffffffff8f000000 env->regs[R_EAX]=0000000000000200
RAX=0000000000000200 RBX=0000000000000000 RCX=ffffffff800175b0 RDX=ffffffff8f000000
RSI=000000000000001b RDI=ffffffff8f000023 RBP=0000000000000000 RSP=ffffffff8f000000
R8 =ffffffff8f000000 R9 =ffffffff800175b0 R10=0000000000000000 R11=0000000000000000
R12=0000000000000000 R13=0000000000000000 R14=0000000000000000 R15=0000000000000000
RIP=ffffffff800175b0 RFL=00000202 [-------] CPL=3 II=0 A20=1 SMM=0 HLT=0
ES =0000 0000000000000000 00000000 00001300
CS =001b 0000000000000000 ffffffff 00affb00 DPL=3 CS64 [-RA]
SS =0023 0000000000000000 ffffffff 00aff300 DPL=3 DS [-WA]
DS =0000 0000000000000000 00000000 00001300
FS =0000 0000000000000000 00000000 00001300
GS =0000 0000000000000000 00000000 00001300
LDT=0000 0000000000000000 00000000 00008200 DPL=0 LDT
TR =0038 0000000000000000 00000000 00008900 DPL=0 TSS64-avl
GDT= ffffffff80044498 00000037
IDT= ffffffff800433d0 00000fff
CR0=80010011 CR2=ffffffff800175b0 CR3=0000000007d9e000 CR4=00000020
DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000
DR6=00000000ffff0ff0 DR7=0000000000000400
CCS=0000000000000000 CCD=ffff800007dad588 CCO=EFLAGS
EFER=0000000000000d00
check_exception old: 0x8 new 0xd // Second General Protection Fault
// Instead of calling the GPF handler again it's just a triple fault

1709
log1.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,9 @@ use core::ptr::addr_of_mut;
use intbits::Bits;
use lazy_static::lazy_static;
use x86_64::PrivilegeLevel::{Ring0, Ring3};
use x86_64::structures::gdt;
use x86_64::structures::gdt::*;
use x86_64::structures::tss::TaskStateSegment;
use crate::log::*;
use crate::memory::alloc::format;
@ -27,40 +27,52 @@ impl ModifyFlags for Descriptor {
Descriptor::UserSegment(a.with_bits(52..=55, flags.bits().bits(0..=3)))
}
Descriptor::SystemSegment(a, b) => {
log_trace!("0b{:b}", flags.bits().bits(0..=3));
Descriptor::SystemSegment(a.with_bits(52..=55, flags.bits().bits(0..=3)), b)
}
}
}
}
pub struct Selectors {
pub sel_kernel_code: SegmentSelector,
pub sel_kernel_stack: SegmentSelector,
pub sel_user_code: SegmentSelector,
pub sel_user_stack: SegmentSelector,
#[allow(unused)]
pub sel_tss: SegmentSelector,
pub gdt: GlobalDescriptorTable<48>,
}
lazy_static! {
pub static ref TSS: x86_64::structures::tss::TaskStateSegment =
x86_64::structures::tss::TaskStateSegment::new();
pub static ref GDT: x86_64::structures::gdt::GlobalDescriptorTable<64> = {
let mut g = GlobalDescriptorTable::empty();
g.append(Descriptor::kernel_code_segment());
g.append(
pub static ref TSS: TaskStateSegment = TaskStateSegment::new();
pub static ref SELECTORS: Selectors = {
let mut gdt = GlobalDescriptorTable::empty();
// DO NOT REORDER
let sel_k_cs = gdt.append(Descriptor::kernel_code_segment());
// DO NOT REORDER
let sel_k_ss = gdt.append(
Descriptor::kernel_data_segment().set_flags(DescriptorFlags::from_bits_retain(0b1010)),
);
g.append(Descriptor::user_code_segment());
g.append(
// DO NOT REORDER
let sel_u_ss = gdt.append(
Descriptor::user_data_segment().set_flags(DescriptorFlags::from_bits_retain(0b1010)),
);
g.append(
// DO NOT REORDER
let sel_u_cs = gdt.append(Descriptor::user_code_segment());
// DO NOT REORDER
let sel_tss = gdt.append(
Descriptor::tss_segment(&TSS).set_flags(DescriptorFlags::from_bits_retain(0b1010)),
);
g
Selectors {
sel_kernel_code: sel_k_cs,
sel_kernel_stack: sel_k_ss,
sel_user_code: sel_u_cs,
sel_user_stack: sel_u_ss,
sel_tss,
gdt,
}
};
}
#[allow(unused)]
pub const GDT_SELECTOR_KERNEL_CODE: SegmentSelector = SegmentSelector::new(1, Ring0);
#[allow(unused)]
pub const GDT_SELECTOR_KERNEL_DATA: SegmentSelector = SegmentSelector::new(2, Ring0);
pub const GDT_SELECTOR_USER_CODE: SegmentSelector = SegmentSelector::new(3, Ring3);
pub const GDT_SELECTOR_USER_DATA: SegmentSelector = SegmentSelector::new(4, Ring3);
#[allow(unused)]
pub const GDT_SELECTOR_TSS: SegmentSelector = SegmentSelector::new(5, Ring0);
#[allow(dead_code)]
pub trait GdtEntryRead {

View File

@ -32,7 +32,7 @@ use limine::firmware::{
};
use spin::mutex::Mutex;
use x86_64::{
PhysAddr, VirtAddr,
VirtAddr,
registers::rflags::RFlags,
structures::{
idt::InterruptStackFrameValue,
@ -45,7 +45,7 @@ use crate::{
asm,
x86_64::{
cpuid::{CPUID, virt_supported},
gdt::{GDT, GDT_SELECTOR_USER_CODE, GDT_SELECTOR_USER_DATA, dump_gdt},
gdt::{SELECTORS, dump_gdt},
interrupts::get_lapic_addr,
},
},
@ -106,7 +106,7 @@ unsafe extern "C" fn main() -> ! {
// Ensure IDT exists
IDT.load();
log_info!("IDT loaded!");
GDT.load();
SELECTORS.gdt.load();
log_info!("GDT loaded!");
log_info!(
@ -227,11 +227,13 @@ unsafe extern "C" fn main() -> ! {
{
let ub = modules_list.modules()[0];
log_info!("Found userboot: {} {}", ub.path(), ub.cmdline());
let start_addr = VirtAddr::from_ptr(ub.data() as *const [u8]);
//let start_addr = VirtAddr::from_ptr(ub.data() as *const [u8]);
let start_addr = VirtAddr::from_ptr(usermode_fn as *const fn());
log_info!("Start address: 0x{:x}", start_addr);
// This does not work :/
unsafe {
/*unsafe {
InterruptStackFrameValue::new(
start_addr,
GDT_SELECTOR_USER_CODE,
@ -240,7 +242,29 @@ unsafe extern "C" fn main() -> ! {
GDT_SELECTOR_USER_DATA,
)
.iretq()
};
};*/
log_trace!("Segment offsets:");
log_trace!("GDT_SELECTOR_USER_CODE: {}", SELECTORS.sel_user_code.0);
log_trace!("GDT_SELECTOR_USER_DATA: {}", SELECTORS.sel_user_stack.0);
log_trace!("GDT_SELECTOR_KERNEL_CODE: {}", SELECTORS.sel_kernel_code.0);
log_trace!("GDT_SELECTOR_KERNEL_DATA: {}", SELECTORS.sel_kernel_stack.0);
x86_64::registers::model_specific::Star::write(
SELECTORS.sel_user_code,
SELECTORS.sel_user_stack,
SELECTORS.sel_kernel_code,
SELECTORS.sel_kernel_stack,
)
.expect("fuck");
unsafe {
InterruptStackFrameValue::new(
VirtAddr::from_ptr(usermode_fn as *const fn()),
SELECTORS.sel_user_code,
RFlags::INTERRUPT_FLAG,
VirtAddr::new(0xffffffff8f000000),
SELECTORS.sel_user_stack,
)
.iretq();
}
}
// TODO:
@ -249,3 +273,10 @@ unsafe extern "C" fn main() -> ! {
panic!("Finished boot, but cannot start init because processes not implemented!");
}
fn usermode_fn() -> ! {
log_info!("In usermode!");
loop {
arch::asm::nop();
}
}

0
tlb.txt Normal file
View File