Skip to content

Commit

Permalink
Kernel: Jump to userspace and back!
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 15, 2025
1 parent f7e3b42 commit 6e79673
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/arch/src/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl TaskStateSegmentPtr {
task.set_base(tss as *const _ as u64);
task.set_limit(size_of::<crate::tss64::TaskStateSegment>() as u32);

task.set_accessed_flag(true);
task.set_present_flag(true);
task.set_code_segment_flag(true);
task.set_accessed_flag(true);
Expand Down
11 changes: 7 additions & 4 deletions kernel/src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ use lldebug::{log, logln, sync::Mutex};
static INTERRUPT_TABLE: Mutex<InterruptDescTable> = Mutex::new(InterruptDescTable::new());
static IRQ_HANDLERS: Mutex<[Option<fn(&InterruptInfo)>; 32]> = Mutex::new([None; 32]);

#[interrupt(0..48)]
#[interrupt(0..50)]
fn main_handler(args: InterruptInfo) {
match args.flags {
// IRQ
InterruptFlags::Irq(irq_num) if irq_num - PIC_IRQ_OFFSET <= 16 => {
call_attached_irq(irq_num - PIC_IRQ_OFFSET, &args);
unsafe { pic_eoi(irq_num - PIC_IRQ_OFFSET) };
}
InterruptFlags::Irq(irq_num) if irq_num == 49 => unsafe {
task_start();
},
InterruptFlags::Debug => (),
exception => {
panic!("UNHANDLED FAULT\n{:#016x?}", args)
Expand Down Expand Up @@ -104,7 +107,8 @@ pub fn attach_syscall() {
gate.set_privilege(arch::CpuPrivilege::Ring3);
gate.set_offset(syscall_entry as u64);
gate.set_gate_kind(arch::idt64::GateKind::InterruptGate);
gate.set_code_segment(Segment::new(3, arch::CpuPrivilege::Ring3));
gate.set_code_segment(Segment::new(1, arch::CpuPrivilege::Ring0));
// gate.set_code_segment(Segment::new(3, arch::CpuPrivilege::Ring3));

idt.attach_raw(0x80, gate);
unsafe { idt.submit_table().load() };
Expand All @@ -127,7 +131,7 @@ unsafe extern "C" {

#[unsafe(no_mangle)]
extern "C" fn syscall_handler() {
todo!()
logln!("Hello from userspace!");
}

global_asm!(
Expand Down Expand Up @@ -170,7 +174,6 @@ global_asm!(
r#"
.global task_start
task_start:
cli
# Save our state to the stack
push rax
push rbx
Expand Down
8 changes: 4 additions & 4 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ fn main(kbh: &KernelBootHeader) {
);

gdt::init_kernel_gdt();
gdt::set_stack_for_privl(0x200000000000 as *mut u8, Ring0);
// gdt::set_stack_for_privl(0x200000000000 as *mut u8, Ring0);
gdt::set_stack_for_privl(0x3000 as *mut u8, Ring0);
unsafe { gdt::load_tss() };
int::attach_interrupts();
int::attach_syscall();
Expand Down Expand Up @@ -136,9 +137,8 @@ fn main(kbh: &KernelBootHeader) {
})
.unwrap();

unsafe {
task_start();
}
logln!("Attempting to jump to userspace!");
unsafe { core::arch::asm!("int 0x31") };
loop {}

logln!("Finished in {}ms", kernel_ticks());

Check warning on line 144 in kernel/src/main.rs

View workflow job for this annotation

GitHub Actions / Build OS

unreachable statement
Expand Down
1 change: 1 addition & 0 deletions user/dummy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ extern "C" fn _start() {
unsafe {
test_syscall();
}
loop {}
}

0 comments on commit 6e79673

Please sign in to comment.