Skip to content

Commit

Permalink
task ok
Browse files Browse the repository at this point in the history
  • Loading branch information
ZR233 committed Jan 21, 2025
1 parent 54f75ce commit ac26122
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions crates/sparreal-kernel/src/globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::{
platform::{self, cpu_list},
};

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

mod percpu;

pub struct GlobalVal {
Expand Down
12 changes: 12 additions & 0 deletions crates/sparreal-kernel/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ pub fn kstack_size() -> usize {
PlatformImpl::kstack_size()
}

pub fn page_size() -> usize {
#[cfg(feature = "mmu")]
{
MMUImpl::page_size()
}

#[cfg(not(feature = "mmu"))]
{
0x1000
}
}

pub fn app_main() {
unsafe extern "C" {
fn __sparreal_rt_main();
Expand Down
22 changes: 14 additions & 8 deletions crates/sparreal-kernel/src/task/tcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ use core::{
use alloc::{boxed::Box, string::String};
use log::debug;

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

use super::{TaskConfig, TaskError};

Expand Down Expand Up @@ -59,7 +65,8 @@ impl TaskControlBlock {

let buffer = NonNull::new(unsafe {
alloc::alloc::alloc_zeroed(
Layout::from_size_align(Self::tcb_size(config.stack_size), TCB_ALIGN).unwrap(),
Layout::from_size_align(Self::tcb_size(config.stack_size), platform::page_size())
.unwrap(),
)
})
.ok_or(TaskError::NoMemory)?;
Expand Down Expand Up @@ -94,7 +101,7 @@ impl TaskControlBlock {

let buffer = NonNull::new(unsafe {
alloc::alloc::alloc_zeroed(
Layout::from_size_align(Self::tcb_size(0), TCB_ALIGN).unwrap(),
Layout::from_size_align(Self::tcb_size(0), platform::page_size()).unwrap(),
)
})
.ok_or(TaskError::NoMemory)
Expand Down Expand Up @@ -126,15 +133,14 @@ impl TaskControlBlock {
unsafe { self.stack_bottom().add(self.stack_size) }
}

pub(super) fn stack(&self) -> &[u8] {
unsafe { core::slice::from_raw_parts_mut(self.stack_bottom(), self.stack_size) }
}

pub(super) unsafe fn drop(self) {
let size = Self::tcb_size(self.stack_size);

unsafe {
alloc::alloc::dealloc(self.0, Layout::from_size_align_unchecked(size, TCB_ALIGN))
alloc::alloc::dealloc(
self.0,
Layout::from_size_align_unchecked(size, platform::page_size()),
)
};
}

Expand Down

0 comments on commit ac26122

Please sign in to comment.