Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc: add Label and swtch #23

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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