From 03f2b2b4c9dcfc6d88d95f2c9e6a8b806d5e578e Mon Sep 17 00:00:00 2001 From: Dan Cross Date: Wed, 23 Aug 2023 20:05:47 +0000 Subject: [PATCH] proc: add Label and swtch Add a `Label` structure that encapsulates kernel thread register state, and a `swtch` to jump between threads (as defined by labels). Signed-off-by: Dan Cross --- x86_64/src/dat.rs | 1 + x86_64/src/main.rs | 28 ++++++++++++++++++++++++++ x86_64/src/proc.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++ xtask/src/main.rs | 1 + 4 files changed, 80 insertions(+) create mode 100644 x86_64/src/dat.rs create mode 100644 x86_64/src/proc.rs diff --git a/x86_64/src/dat.rs b/x86_64/src/dat.rs new file mode 100644 index 0000000..ab0c014 --- /dev/null +++ b/x86_64/src/dat.rs @@ -0,0 +1 @@ +// \ No newline at end of file diff --git a/x86_64/src/main.rs b/x86_64/src/main.rs index db7f7ec..f0a312a 100644 --- a/x86_64/src/main.rs +++ b/x86_64/src/main.rs @@ -1,25 +1,53 @@ #![feature(alloc_error_handler)] #![feature(asm_const)] +#![feature(naked_functions)] #![cfg_attr(not(any(test, feature = "cargo-clippy")), no_std)] #![cfg_attr(not(test), no_main)] #![allow(clippy::upper_case_acronyms)] #![forbid(unsafe_op_in_unsafe_fn)] +mod dat; mod devcons; mod pio; +mod proc; mod uart16550; +use proc::{swtch, Label}; + #[cfg(not(test))] core::arch::global_asm!(include_str!("l.S"), options(att_syntax)); use port::println; +static mut THRSTACK: [u64; 1024] = [0; 1024]; +static mut CTX: u64 = 0; +static mut THR: u64 = 0; + +fn jumpback() { + println!("in a thread"); + unsafe { + let thr = &mut *(THR as *mut Label); + let ctx = &mut *(CTX as *mut Label); + swtch(thr, ctx); + } +} + #[no_mangle] pub extern "C" fn main9() { devcons::init(); println!(); println!("r9 from the Internet"); println!("looping now"); + let mut ctx = Label::new(); + let mut thr = Label::new(); + thr.pc = jumpback as u64; + unsafe { + thr.sp = &mut THRSTACK[1023] as *mut _ as u64; + CTX = &mut ctx as *mut _ as u64; + THR = &mut thr as *mut _ as u64; + swtch(&mut ctx, &mut thr); + } + println!("came out the other side of a context switch"); #[allow(clippy::empty_loop)] loop {} } diff --git a/x86_64/src/proc.rs b/x86_64/src/proc.rs new file mode 100644 index 0000000..6d5d204 --- /dev/null +++ b/x86_64/src/proc.rs @@ -0,0 +1,50 @@ +use core::arch::asm; + +#[repr(C)] +pub struct Label { + pub pc: u64, + pub sp: u64, + pub fp: u64, + rbx: u64, + r12: u64, + r13: u64, + r14: u64, + r15: u64, +} + +impl Label { + pub const fn new() -> Label { + Label { pc: 0, sp: 0, fp: 0, rbx: 0, r12: 0, r13: 0, r14: 0, r15: 0 } + } +} + +#[naked] +pub unsafe extern "C" fn swtch(save: &mut Label, next: &mut Label) { + unsafe { + asm!( + r#" + movq (%rsp), %rax + movq %rax, 0(%rdi) + movq %rsp, 8(%rdi) + movq %rbp, 16(%rdi) + movq %rbx, 24(%rdi) + movq %r12, 32(%rdi) + movq %r13, 40(%rdi) + movq %r14, 48(%rdi) + movq %r15, 56(%rdi) + + movq 0(%rsi), %rax + movq 8(%rsi), %rsp + movq 16(%rsi), %rbp + movq 24(%rsi), %rbx + movq 32(%rsi), %r12 + movq 40(%rsi), %r13 + movq 48(%rsi), %r14 + movq 56(%rsi), %r15 + movq %rax, (%rsp) + xorl %eax, %eax + ret"#, + options(att_syntax, noreturn) + ); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index b8442d9..a241593 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -517,6 +517,7 @@ fn run(build_params: &BuildParams) -> Result<()> { cmd.arg("qemu64,pdpe1gb,xsaveopt,fsgsbase,apic,msr"); cmd.arg("-smp"); cmd.arg("8"); + cmd.arg("-s"); cmd.arg("-m"); cmd.arg("8192"); if build_params.wait_for_gdb {