Skip to content

Commit

Permalink
proc: add Label and swtch
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Dan Cross authored and gmacd committed Aug 26, 2023
1 parent eb92556 commit 03f2b2b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions x86_64/src/dat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//
28 changes: 28 additions & 0 deletions x86_64/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
Expand Down
50 changes: 50 additions & 0 deletions x86_64/src/proc.rs
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
1 change: 1 addition & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 03f2b2b

Please sign in to comment.