Skip to content

Commit

Permalink
fix: Fix debug build caused by kcontext_switch_pt
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock committed Sep 18, 2024
1 parent e296b1b commit 5b89010
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 183 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
all:

test-build:
cargo build --release --all-features --target riscv64gc-unknown-none-elf
cargo build --release --all-features --target aarch64-unknown-none-softfloat
cargo build --release --all-features --target x86_64-unknown-none
cargo build --release --all-features --target loongarch64-unknown-none
cargo build --all-features --target riscv64gc-unknown-none-elf
cargo build --all-features --target aarch64-unknown-none-softfloat
cargo build --all-features --target x86_64-unknown-none
cargo build --all-features --target loongarch64-unknown-none

test-clippy:
cargo clippy --all-features --target riscv64gc-unknown-none-elf
Expand Down
2 changes: 1 addition & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ define_entry!(main);
fn panic(info: &PanicInfo) -> ! {
if let Some(location) = info.location() {
log::error!(
"[kernel] Panicked at {}:{} {}",
"[kernel] Panicked at {}:{} \n\t{}",
location.file(),
location.line(),
info.message().unwrap()
Expand Down
108 changes: 57 additions & 51 deletions src/components/kcontext/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ use crate::PageTable;

use crate::components::kcontext::KContextArgs;

/// Save the task context registers.
macro_rules! save_callee_regs {
() => {
"
mrs x3, tpidr_el1
mov x4, sp
stp x4, x3, [x0]
stp x19, x20, [x0, 2 * 8]
stp x21, x22, [x0, 4 * 8]
stp x23, x24, [x0, 6 * 8]
stp x25, x26, [x0, 8 * 8]
stp x27, x28, [x0, 10 * 8]
stp x27, x28, [x0, 10 * 8]
stp x29, x30, [x0, 12 * 8]
"
};
}

/// Restore the task context registers.
macro_rules! restore_callee_regs {
() => {
"
ldp x4, x3, [x1]
ldp x19, x20, [x1, 2 * 8]
ldp x21, x22, [x1, 4 * 8]
ldp x23, x24, [x1, 6 * 8]
ldp x25, x26, [x1, 8 * 8]
ldp x27, x28, [x1, 10 * 8]
ldp x27, x28, [x1, 10 * 8]
ldp x29, x30, [x1, 12 * 8]
msr tpidr_el1, x3
mov sp, x4
"
};
}

/// Kernel Context
///
/// Kernel Context is used to switch context between kernel task.
Expand Down Expand Up @@ -91,59 +127,39 @@ impl IndexMut<KContextArgs> for KContext {
pub unsafe extern "C" fn context_switch(from: *mut KContext, to: *const KContext) {
core::arch::asm!(
// Save Kernel Context.
"
mrs x3, tpidr_el1
mov x4, sp
stp x4, x3, [x0]
stp x19, x20, [x0, 2 * 8]
stp x21, x22, [x0, 4 * 8]
stp x23, x24, [x0, 6 * 8]
stp x25, x26, [x0, 8 * 8]
stp x27, x28, [x0, 10 * 8]
stp x27, x28, [x0, 10 * 8]
stp x29, x30, [x0, 12 * 8]
",
save_callee_regs!(),
// Restore Kernel Context.
"
ldp x4, x3, [x1]
ldp x19, x20, [x1, 2 * 8]
ldp x21, x22, [x1, 4 * 8]
ldp x23, x24, [x1, 6 * 8]
ldp x25, x26, [x1, 8 * 8]
ldp x27, x28, [x1, 10 * 8]
ldp x27, x28, [x1, 10 * 8]
ldp x29, x30, [x1, 12 * 8]
msr tpidr_el1, x3
mov sp, x4
ret
",
restore_callee_regs!(),
// Return to the caller.
"ret",
options(noreturn)
)
}

/// Context Switch With Page Table
///
/// Save the context of current task and switch to new task.
#[naked]
#[inline]
pub unsafe extern "C" fn context_switch_pt(
from: *mut KContext,
to: *const KContext,
pt_token: PageTable,
) {
context_switch_pt_impl(from, to, pt_token.0.0);
}

/// Context Switch With Page Table Implement
///
/// The detail implementation of [context_switch_pt].
#[naked]
unsafe extern "C" fn context_switch_pt_impl(
from: *mut KContext,
to: *const KContext,
pt_token: usize,
) {
core::arch::asm!(
// Save Kernel Context.
"
mrs x3, tpidr_el1
mov x4, sp
stp x4, x3, [x0]
stp x19, x20, [x0, 2 * 8]
stp x21, x22, [x0, 4 * 8]
stp x23, x24, [x0, 6 * 8]
stp x25, x26, [x0, 8 * 8]
stp x27, x28, [x0, 10 * 8]
stp x27, x28, [x0, 10 * 8]
stp x29, x30, [x0, 12 * 8]
",
save_callee_regs!(),
// Switch to new page table.
"
msr ttbr0_el1, x2
Expand All @@ -152,19 +168,9 @@ pub unsafe extern "C" fn context_switch_pt(
isb
",
// Restore Kernel Context.
"
ldp x4, x3, [x1]
ldp x19, x20, [x1, 2 * 8]
ldp x21, x22, [x1, 4 * 8]
ldp x23, x24, [x1, 6 * 8]
ldp x25, x26, [x1, 8 * 8]
ldp x27, x28, [x1, 10 * 8]
ldp x27, x28, [x1, 10 * 8]
ldp x29, x30, [x1, 12 * 8]
msr tpidr_el1, x3
mov sp, x4
ret
",
restore_callee_regs!(),
// Return to the caller.
"ret",
options(noreturn)
)
}
Expand Down
126 changes: 63 additions & 63 deletions src/components/kcontext/loongarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,48 @@ use core::{

use crate::{components::kcontext::KContextArgs, components::pagetable::PageTable};

/// Save the task context registers.
macro_rules! save_callee_regs {
() => {
"
st.d $sp, $a0, 0*8
st.d $tp, $a0, 1*8
st.d $s9, $a0, 2*8
st.d $s0, $a0, 3*8
st.d $s1, $a0, 4*8
st.d $s2, $a0, 5*8
st.d $s3, $a0, 6*8
st.d $s4, $a0, 7*8
st.d $s5, $a0, 8*8
st.d $s6, $a0, 9*8
st.d $s7, $a0, 10*8
st.d $s8, $a0, 11*8
st.d $ra, $a0, 12*8
"
};
}

/// Restore the task context registers.
macro_rules! restore_callee_regs {
() => {
"
ld.d $sp, $a1, 0*8
ld.d $tp, $a1, 1*8
ld.d $s9, $a1, 2*8
ld.d $s0, $a1, 3*8
ld.d $s1, $a1, 4*8
ld.d $s2, $a1, 5*8
ld.d $s3, $a1, 6*8
ld.d $s4, $a1, 7*8
ld.d $s5, $a1, 8*8
ld.d $s6, $a1, 9*8
ld.d $s7, $a1, 10*8
ld.d $s8, $a1, 11*8
ld.d $ra, $a1, 12*8
"
};
}

/// Kernel Context
///
/// Kernel Context is used to switch context between kernel task.
Expand Down Expand Up @@ -92,68 +134,39 @@ impl IndexMut<KContextArgs> for KContext {
pub unsafe extern "C" fn context_switch(from: *mut KContext, to: *const KContext) {
core::arch::asm!(
// Save Kernel Context.
"
st.d $sp, $a0, 0*8
st.d $tp, $a0, 1*8
st.d $s9, $a0, 2*8
st.d $s0, $a0, 3*8
st.d $s1, $a0, 4*8
st.d $s2, $a0, 5*8
st.d $s3, $a0, 6*8
st.d $s4, $a0, 7*8
st.d $s5, $a0, 8*8
st.d $s6, $a0, 9*8
st.d $s7, $a0, 10*8
st.d $s8, $a0, 11*8
st.d $ra, $a0, 12*8
",
save_callee_regs!(),
// Restore Kernel Context.
"
ld.d $sp, $a1, 0*8
ld.d $tp, $a1, 1*8
ld.d $s9, $a1, 2*8
ld.d $s0, $a1, 3*8
ld.d $s1, $a1, 4*8
ld.d $s2, $a1, 5*8
ld.d $s3, $a1, 6*8
ld.d $s4, $a1, 7*8
ld.d $s5, $a1, 8*8
ld.d $s6, $a1, 9*8
ld.d $s7, $a1, 10*8
ld.d $s8, $a1, 11*8
ld.d $ra, $a1, 12*8
ret
",
restore_callee_regs!(),
// Return to the caller.
"ret",
options(noreturn)
)
}

/// Context Switch With Page Table
///
/// Save the context of current task and switch to new task.
#[naked]
#[inline]
pub unsafe extern "C" fn context_switch_pt(
from: *mut KContext,
to: *const KContext,
pt_token: PageTable,
) {
context_switch_pt_impl(from, to, pt_token.0.0);
}

/// Context Switch With Page Table Implement
///
/// The detail implementation of [context_switch_pt].
#[naked]
unsafe extern "C" fn context_switch_pt_impl(
from: *mut KContext,
to: *const KContext,
pt_token: usize,
) {
core::arch::asm!(
// Save Kernel Context.
"
st.d $sp, $a0, 0*8
st.d $tp, $a0, 1*8
st.d $s9, $a0, 2*8
st.d $s0, $a0, 3*8
st.d $s1, $a0, 4*8
st.d $s2, $a0, 5*8
st.d $s3, $a0, 6*8
st.d $s4, $a0, 7*8
st.d $s5, $a0, 8*8
st.d $s6, $a0, 9*8
st.d $s7, $a0, 10*8
st.d $s8, $a0, 11*8
st.d $ra, $a0, 12*8
",
save_callee_regs!(),
// Switch to new page table.
// Write PageTable to pgdl(CSR 0x19)
"
Expand All @@ -162,22 +175,9 @@ pub unsafe extern "C" fn context_switch_pt(
invtlb 0x00, $r0, $r0
",
// Restore Kernel Context.
"
ld.d $sp, $a1, 0*8
ld.d $tp, $a1, 1*8
ld.d $s9, $a1, 2*8
ld.d $s0, $a1, 3*8
ld.d $s1, $a1, 4*8
ld.d $s2, $a1, 5*8
ld.d $s3, $a1, 6*8
ld.d $s4, $a1, 7*8
ld.d $s5, $a1, 8*8
ld.d $s6, $a1, 9*8
ld.d $s7, $a1, 10*8
ld.d $s8, $a1, 11*8
ld.d $ra, $a1, 12*8
ret
",
restore_callee_regs!(),
// Return to the caller.
"ret",
options(noreturn)
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/kcontext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//!
use crate::pub_use_arch;

super::define_arch_mods!();

/// Kernel Context Arg Type.
Expand All @@ -16,3 +18,5 @@ pub enum KContextArgs {
/// Kernel Program Counter
KPC,
}

pub_use_arch!(context_switch, context_switch_pt);
Loading

0 comments on commit 5b89010

Please sign in to comment.