Skip to content

Commit

Permalink
task重构
Browse files Browse the repository at this point in the history
  • Loading branch information
ZR233 committed Jan 21, 2025
1 parent ac26122 commit 665d38d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 383 deletions.
2 changes: 1 addition & 1 deletion app/helloworld/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![no_main]
extern crate alloc;

use core::{hint::spin_loop, time::Duration};
use core::time::Duration;

use alloc::string::ToString;
use log::info;
Expand Down
2 changes: 0 additions & 2 deletions crates/sparreal-kernel/src/globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use crate::{
platform::{self, cpu_list},
};

pub const STACK_SIZE: usize = 32 * 1024 * 1024;

mod percpu;

pub struct GlobalVal {
Expand Down
2 changes: 0 additions & 2 deletions crates/sparreal-kernel/src/platform_if/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::ffi::c_void;

pub use driver_interface::DriverRegisterListRef;
use sparreal_macros::api_trait;

Expand Down
10 changes: 1 addition & 9 deletions crates/sparreal-kernel/src/task/tcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ use core::{
use alloc::{boxed::Box, string::String};
use log::debug;

use crate::{
globals::{STACK_SIZE, global_val},
mem::VirtAddr,
platform,
platform_if::PlatformImpl,
task::schedule::*,
};
use crate::{platform, platform_if::PlatformImpl, task::schedule::*};

use super::{TaskConfig, TaskError};

const TCB_ALIGN: usize = 16;

#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct TaskControlBlock(*mut u8);
Expand Down
96 changes: 83 additions & 13 deletions crates/sparreal-rt/src/arch/aarch64/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use core::{
fmt::{self, Debug},
};

use log::debug;
use sparreal_kernel::task::TaskControlBlock;

#[repr(C, align(0x10))]
Expand Down Expand Up @@ -99,8 +98,8 @@ extern "C" fn __store_context() {
}
}

#[inline(never)]
pub fn tcb_switch(prev: &mut TaskControlBlock, next: &mut TaskControlBlock) {
#[inline(always)]
fn __ctx_store_x_q() {
unsafe {
asm!(
"
Expand All @@ -120,8 +119,12 @@ pub fn tcb_switch(prev: &mut TaskControlBlock, next: &mut TaskControlBlock) {
stp X3,X4, [sp,#-0x10]!
stp X1,X2, [sp,#-0x10]!
mrs x9, SPSR_EL1
stp x9, x0, [sp,#-0x10]!
stp x9, x0, [sp,#-0x10]!"
);

#[cfg(hard_float)]
asm!(
"
stp q30, q31, [sp,#-0x20]!
stp q28, q29, [sp,#-0x20]!
stp q26, q27, [sp,#-0x20]!
Expand All @@ -141,21 +144,74 @@ pub fn tcb_switch(prev: &mut TaskControlBlock, next: &mut TaskControlBlock) {
mrs x9, fpcr
mrs x10, fpsr
stp x9, x10, [sp,#-0x10]!
"
);
}
}

/// return `x9` is sp, `x10` is lr
#[inline(always)]
fn store_pc_is_lr() {
__ctx_store_x_q();
unsafe {
asm!(
"
mov x10, lr
mov x9, sp
sub x9, x9, #0x10
stp x9, x10, [sp,#-0x10]!"
stp x9, x10, [sp,#-0x10]!
"
);
asm!("mov {0}, x9", out(reg) prev.sp, options(nostack, nomem));
let sp = next.sp;
}
}

asm!("mov sp, {0}", in(reg) sp, options(nostack, nomem));
#[inline(always)]
pub(super) fn store_pc_is_elr() {
__ctx_store_x_q();
unsafe {
asm!(
"
mrs x10, ELR_EL1
mov x9, sp
sub x9, x9, #0x10
stp x9, x10, [sp,#-0x10]!
"
);
}
}

/// return `x9` is sp, `x10` is lr
#[inline(always)]
fn restore_pc_is_lr() {
unsafe {
asm!(
"
ldp x9, lr, [sp], #0x10
"
);
}
__ctx_restore_x_q();
}

#[inline(always)]
pub(super) fn restore_pc_is_elr() {
unsafe {
asm!(
"
ldp x9, lr, [sp], #0x10
ldp X0, X10, [sp], #0x10
msr ELR_EL1, X10
"
);
}
__ctx_restore_x_q();
}

#[inline(always)]
fn __ctx_restore_x_q() {
unsafe {
#[cfg(hard_float)]
asm!(
"
ldp x9, x10, [sp], #0x10
msr fpcr, x9
msr fpsr, x10
Expand All @@ -175,7 +231,11 @@ pub fn tcb_switch(prev: &mut TaskControlBlock, next: &mut TaskControlBlock) {
ldp q26, q27, [sp], #0x20
ldp q28, q29, [sp], #0x20
ldp q30, q31, [sp], #0x20
"
);

asm!(
"
ldp X9,X0, [sp], #0x10
msr SPSR_EL1, X9
ldp X1,X2, [sp], #0x10
Expand All @@ -193,9 +253,19 @@ pub fn tcb_switch(prev: &mut TaskControlBlock, next: &mut TaskControlBlock) {
ldp X25,X26, [sp], #0x10
ldp X27,X28, [sp], #0x10
ldp X29,X30, [sp], #0x10
ret
",
options(nostack, nomem)
)
"
);
}
}

#[inline(never)]
pub fn tcb_switch(prev: &mut TaskControlBlock, next: &mut TaskControlBlock) {
store_pc_is_lr();

unsafe {
asm!("mov {0}, x9", out(reg) prev.sp, options(nostack, nomem));
asm!("mov sp, {0}", in(reg) next.sp, options(nostack, nomem));
}

restore_pc_is_lr();
}
4 changes: 1 addition & 3 deletions crates/sparreal-rt/src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{arch::asm, ffi::c_void};
use core::arch::asm;

use aarch64_cpu::registers::*;
use context::{Context, tcb_switch};
Expand Down Expand Up @@ -87,8 +87,6 @@ impl Platform for PlatformImpl {

debug!("next: {:?}", next_ctx);



tcb_switch(&mut prev, &mut next);
}

Expand Down
Loading

0 comments on commit 665d38d

Please sign in to comment.