Skip to content

Commit

Permalink
Task context depends on architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf2019 committed Mar 8, 2023
1 parent 4b2eb3c commit 3ee555c
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 21 deletions.
File renamed without changes.
2 changes: 2 additions & 0 deletions kernel/src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub mod mm {
pub use mm_rv::*;
}
mod context;
pub mod timer;
pub mod trap;
pub mod uintr;

pub use context::*;
use mm_rv::*;
use riscv::asm::{sfence_vma, sfence_vma_all};

Expand Down
18 changes: 18 additions & 0 deletions kernel/src/arch/riscv64/trap/trapframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syscall_interface::SyscallNO;
use crate::{
error::{KernelError, KernelResult},
syscall::SyscallArgs,
task::CloneFlags,
};

/// User context is saved in trapframe by trap handler in trampoline.
Expand Down Expand Up @@ -52,6 +53,23 @@ impl TrapFrame {
trapframe
}

/// Copies from the old one when we clone a task and initialize its trap frame.
pub fn copy_from(&mut self, orig: &mut TrapFrame, flags: CloneFlags, stack: usize, tls: usize) {
*self = *orig;

// Child task returns zero
self.set_a0(0);

// Set stack pointer
if stack != 0 {
self.set_sp(stack);
}

if flags.contains(CloneFlags::CLONE_SETTLS) {
self.set_tp(tls);
}
}

/// Get syscall arguments in registers in user trap frame.
///
/// Returns error if syscall number not supported.
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod vma;
use alloc::{collections::BTreeMap, string::String, sync::Arc, vec::Vec};
use core::{fmt, mem::size_of, slice};
use errno::Errno;
use log::{info, trace, warn};
use log::{trace, warn};
use syscall_interface::SyscallResult;

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mm/vma.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;

use alloc::{sync::Arc, vec::Vec};
use log::{info, warn};
use log::warn;

use crate::{
arch::{flush_tlb, mm::*},
Expand Down
17 changes: 4 additions & 13 deletions kernel/src/task/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ use crate::{
arch::{
mm::VirtAddr,
trap::{user_trap_return, TrapFrame},
TaskContext,
},
config::MAIN_TASK,
error::{KernelError, KernelResult},
};

use super::{
context::TaskContext, init_trapframe, kstack_alloc, kstack_dealloc, kstack_vm_alloc, pid_alloc,
schedule::Scheduler, Task, TaskInner, TaskLockedInner, TaskState, PID, TASK_MANAGER,
init_trapframe, kstack_alloc, kstack_dealloc, kstack_vm_alloc, pid_alloc, schedule::Scheduler,
Task, TaskInner, TaskLockedInner, TaskState, PID, TASK_MANAGER,
};

bitflags::bitflags! {
Expand Down Expand Up @@ -111,17 +112,7 @@ pub fn do_clone(
let mut mm = mm.lock();
let trapframe_pa = init_trapframe(&mut mm)?;
let trapframe = TrapFrame::from(trapframe_pa);
// Copy from trapframe of parent task
*trapframe = *TrapFrame::from(task.trapframe_pa);
// Child task returns zero
trapframe.set_a0(0);
// Set stack pointer
if stack != 0 {
trapframe.set_sp(stack);
}
if flags.contains(CloneFlags::CLONE_SETTLS) {
trapframe.set_tp(tls);
}
trapframe.copy_from(TrapFrame::from(task.trapframe_pa), flags, stack, tls);
trapframe_pa
};

Expand Down
5 changes: 2 additions & 3 deletions kernel/src/task/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use alloc::{string::String, sync::Arc, vec::Vec};
use core::cell::SyncUnsafeCell;
use id_alloc::{IDAllocator, RecycleAllocator};
use kernel_sync::{CPUs, SpinLock};
use log::{trace, info};
use log::trace;
use oscomp::{fetch_test, finish_test};
use spin::Lazy;

use crate::{
arch::{get_cpu_id, mm::PAGE_SIZE},
arch::{get_cpu_id, mm::PAGE_SIZE, TaskContext, __switch},
config::{
ADDR_ALIGN, CPU_NUM, INIT_TASK_PATH, IS_TEST_ENV, KERNEL_STACK_SIZE, MAIN_TASK, ROOT_DIR,
TRAMPOLINE_VA,
Expand All @@ -18,7 +18,6 @@ use crate::{
};

use super::{
context::{TaskContext, __switch},
schedule::{QueueScheduler, Scheduler},
task::Task,
TaskState,
Expand Down
1 change: 0 additions & 1 deletion kernel/src/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod context;
pub mod manager;
mod schedule;
mod task;
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use core::{cell::SyncUnsafeCell, fmt};
use errno::Errno;
use id_alloc::{IDAllocator, RecycleAllocator};
use kernel_sync::{SpinLock, SpinLockGuard};
use log::{trace, info};
use log::trace;
use signal_defs::{SigActions, SigPending, SigSet};
use syscall_interface::AT_FDCWD;
use vfs::{File, Path};

use crate::{
arch::{
TaskContext, __switch,
mm::{frame_dealloc, AllocatedFrame, Frame, PTEFlags, Page, PhysAddr, VirtAddr, PAGE_SIZE},
trap::{user_trap_handler, user_trap_return, TrapFrame},
},
Expand All @@ -26,7 +27,6 @@ use crate::{
};

use super::{
context::{TaskContext, __switch},
curr_ctx, curr_task, idle_ctx,
manager::{kstack_dealloc, kstack_vm_alloc, PID},
TASK_MANAGER,
Expand Down

0 comments on commit 3ee555c

Please sign in to comment.