Skip to content

Commit

Permalink
try to fix more bugs of convert
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiyuanSue committed Dec 12, 2024
1 parent 184c9c0 commit 49ee9ca
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 38 deletions.
2 changes: 1 addition & 1 deletion kernel/src/arch/aarch64/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn handleVMFaultEvent(vm_faultType: usize) -> exception_t {
if status != exception_t::EXCEPTION_NONE {
handle_fault(get_currenct_thread());
}
// sel4_common::println!("handle vm fault event");
// sel4_common::println!("handle vm fault event");
schedule();
activateThread();
exception_t::EXCEPTION_NONE
Expand Down
51 changes: 46 additions & 5 deletions kernel/src/interfaces_impl/cspace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::usize;

use crate::config::CONFIG_MAX_NUM_WORK_UNITS_PER_PREEMPTION;
// use crate::ffi::tcbDebugRemove;
use crate::interrupt::{deletingIRQHandler, isIRQPending, setIRQState, IRQState};
Expand All @@ -6,12 +8,16 @@ use crate::syscall::safe_unbind_notification;
use sel4_common::sel4_config::{tcbCNodeEntries, tcbCTable, tcbVTable};
use sel4_common::structures::exception_t;
use sel4_common::structures_gen::{cap, cap_null_cap, cap_tag, endpoint, notification};
use sel4_common::utils::convert_to_mut_type_ref;
use sel4_common::utils::{
convert_to_mut_type_ref, convert_to_option_mut_type_ref, convert_to_option_type_ref,
};
use sel4_cspace::capability::cap_func;
use sel4_cspace::compatibility::{ZombieType_ZombieTCB, Zombie_new};
use sel4_cspace::interface::finaliseCap_ret;
use sel4_ipc::{endpoint_func, notification_func, Transfer};
use sel4_task::{get_currenct_thread, ksWorkUnitsCompleted, tcb_t};
use sel4_task::reply::reply_t;
use sel4_task::sched_context::sched_context_t;
use sel4_task::{get_currenct_thread, ksWorkUnitsCompleted, tcb_t, ThreadState};
#[cfg(target_arch = "riscv64")]
use sel4_vspace::find_vspace_for_asid;
#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -190,6 +196,9 @@ pub fn finaliseCap(capability: &cap, _final: bool, _exposed: bool) -> finaliseCa
let ntfn = convert_to_mut_type_ref::<notification>(
cap::cap_notification_cap(capability).get_capNtfnPtr() as usize,
);
#[cfg(feature = "KERNEL_MCS")]
convert_to_mut_type_ref::<sched_context_t>(ntfn.get_ntfnSchedContext() as usize)
.schedContext_unbindNtfn();
ntfn.safe_unbind_tcb();
ntfn.cacncel_all_signal();
}
Expand All @@ -198,9 +207,31 @@ pub fn finaliseCap(capability: &cap, _final: bool, _exposed: bool) -> finaliseCa
return fc_ret;
}
cap_tag::cap_reply_cap => {
// TODO: MCS
}
cap_tag::cap_null_cap | cap_tag::cap_domain_cap => {
// TODO: MCS
if _final {
if let Some(reply) = convert_to_option_mut_type_ref::<reply_t>(
cap::cap_reply_cap(capability).get_capReplyPtr() as usize,
) {
if reply.replyTCB != 0 {
match convert_to_mut_type_ref::<tcb_t>(reply.replyTCB).get_state() {
ThreadState::ThreadStateBlockedOnReply => {
reply.remove(convert_to_mut_type_ref::<tcb_t>(reply.replyTCB));
}
ThreadState::ThreadStateBlockedOnReceive => {
convert_to_mut_type_ref::<tcb_t>(reply.replyTCB).cancel_ipc();
}
_ => {
panic!("invalid tcb state");
}
}
}
}
}
fc_ret.remainder = cap_null_cap::new().unsplay();
fc_ret.cleanupInfo = cap_null_cap::new().unsplay();
return fc_ret;
}
cap_tag::cap_null_cap | cap_tag::cap_domain_cap => {
fc_ret.remainder = cap_null_cap::new().unsplay();
fc_ret.cleanupInfo = cap_null_cap::new().unsplay();
return fc_ret;
Expand Down Expand Up @@ -239,6 +270,16 @@ pub fn finaliseCap(capability: &cap, _final: bool, _exposed: bool) -> finaliseCa
};
let cte_ptr = tcb.get_cspace_mut_ref(tcbCTable);
safe_unbind_notification(tcb);
#[cfg(feature = "KERNEL_MCS")]
if let Some(sc) =
convert_to_option_mut_type_ref::<sched_context_t>(tcb.tcbSchedContext)
{
sc.schedContext_unbindTCB(tcb);
if sc.scYieldFrom != 0 {
convert_to_mut_type_ref::<tcb_t>(sc.scYieldFrom)
.schedContext_completeYieldTo();
}
}
tcb.cancel_ipc();
tcb.suspend();
// #[cfg(feature="DEBUG_BUILD")]
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/syscall/invocation/invoke_sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn invokeSchedControl_ConfigureFlags(
convert_to_mut_type_ref::<tcb_t>(target.scTcb).Release_Remove();
convert_to_mut_type_ref::<tcb_t>(target.scTcb).sched_dequeue();
/* bill the current consumed amount before adjusting the params */
if unsafe { ksCurSC } == target.get_ptr() {
if target.is_current() {
assert!(checkBudget());
commitTime();
}
Expand Down
4 changes: 3 additions & 1 deletion kernel/src/syscall/invocation/invoke_tcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ pub fn invoke_tcb_suspend(thread: &mut tcb_t) -> exception_t {
#[inline]
pub fn invoke_tcb_resume(thread: &mut tcb_t) -> exception_t {
// cancel_ipc(thread);
thread.cancel_ipc();
if thread.is_stopped() {
thread.cancel_ipc();
}
thread.restart();
exception_t::EXCEPTION_NONE
}
Expand Down
18 changes: 7 additions & 11 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ use sel4_common::structures_gen::{
};
use sel4_common::utils::{convert_to_mut_type_ref, ptr_to_mut};
use sel4_ipc::{endpoint_func, notification_func, Transfer};
#[cfg(feature = "KERNEL_MCS")]
use sel4_task::{mcs_preemption_point, chargeBudget, ksConsumed, ksCurSC};
use sel4_task::{
activateThread, get_currenct_thread, rescheduleRequired,
schedule, set_thread_state, tcb_t, ThreadState,
activateThread, get_currenct_thread, rescheduleRequired, schedule, set_thread_state, tcb_t,
ThreadState,
};
#[cfg(feature = "KERNEL_MCS")]
use sel4_task::{chargeBudget, get_current_sc, ksConsumed, ksCurSC, mcs_preemption_point};
pub use utils::*;

use crate::arch::restore_user_context;
Expand Down Expand Up @@ -539,13 +539,9 @@ fn handle_yield() {
#[cfg(feature = "KERNEL_MCS")]
{
unsafe {
let consumed =
convert_to_mut_type_ref::<sched_context_t>(ksCurSC).scConsumed + ksConsumed;
chargeBudget(
(*convert_to_mut_type_ref::<sched_context_t>(ksCurSC).refill_head()).rAmount,
false,
);
convert_to_mut_type_ref::<sched_context_t>(ksCurSC).scConsumed = consumed;
let consumed = get_current_sc().scConsumed + ksConsumed;
chargeBudget((*get_current_sc().refill_head()).rAmount, false);
get_current_sc().scConsumed = consumed;
}
}
#[cfg(not(feature = "KERNEL_MCS"))]
Expand Down
77 changes: 68 additions & 9 deletions sel4_ipc/src/notification.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::transfer::Transfer;
use sel4_common::arch::ArchReg;
use sel4_common::structures_gen::notification;
use sel4_common::structures_gen::{notification, notification_t};
use sel4_common::utils::{convert_to_mut_type_ref, convert_to_option_mut_type_ref};
#[cfg(feature = "KERNEL_MCS")]
use sel4_task::sched_context::sched_context_t;
use sel4_task::{
possible_switch_to, rescheduleRequired, set_thread_state, tcb_queue_t, tcb_t, ThreadState,
};
Expand Down Expand Up @@ -141,7 +143,31 @@ impl notification_func for notification {
tcb.cancel_ipc();
set_thread_state(tcb, ThreadState::ThreadStateRunning);
tcb.tcbArch.set_register(ArchReg::Badge, badge);
#[cfg(feature = "KERNEL_MCS")]
{
maybeDonateSchedContext(tcb, self);
if tcb.is_schedulable() {
possible_switch_to(tcb);
}
}
#[cfg(not(feature = "KERNEL_MCS"))]
possible_switch_to(tcb);
#[cfg(feature = "KERNEL_MCS")]
if let Some(tcbsc) =
convert_to_option_mut_type_ref::<sched_context_t>(tcb.tcbSchedContext)
{
if tcbsc.sc_active() {
let sc = convert_to_mut_type_ref::<sched_context_t>(
self.get_ntfnSchedContext() as usize,
);
if tcbsc.get_ptr() == sc.get_ptr()
&& sc.sc_sporadic()
&& !tcbsc.is_current()
{
tcbsc.refill_unblock_check();
}
}
}
} else {
self.active(badge);
}
Expand All @@ -159,7 +185,26 @@ impl notification_func for notification {
}
set_thread_state(dest, ThreadState::ThreadStateRunning);
dest.tcbArch.set_register(ArchReg::Badge, badge);
#[cfg(feature = "KERNEL_MCS")]
{
maybeDonateSchedContext(dest, self);
if dest.is_schedulable() {
possible_switch_to(dest);
}
}
#[cfg(not(feature = "KERNEL_MCS"))]
possible_switch_to(dest);
#[cfg(feature = "KERNEL_MCS")]
if let Some(sc) =
convert_to_option_mut_type_ref::<sched_context_t>(dest.tcbSchedContext)
{
if sc.sc_sporadic() {
assert!(!sc.is_current());
if !sc.is_current() {
sc.refill_unblock_check();
}
}
}
} else {
panic!("queue is empty!")
}
Expand Down Expand Up @@ -214,14 +259,28 @@ impl notification_func for notification {
#[cfg(feature = "KERNEL_MCS")]
#[inline]
fn maybeReturnSchedContext(&mut self, thread: &mut tcb_t) {
use sel4_task::sched_context::sched_context_t;

let sc = convert_to_mut_type_ref::<sched_context_t>(self.get_ntfnSchedContext() as usize);
if sc.get_ptr() != 0 && sc.get_ptr() == thread.tcbSchedContext {
thread.tcbSchedContext = 0;
sc.scTcb = 0;
if thread.is_current() {
rescheduleRequired();
if let Some(sc) =
convert_to_option_mut_type_ref::<sched_context_t>(self.get_ntfnSchedContext() as usize)
{
if sc.get_ptr() == thread.tcbSchedContext {
thread.tcbSchedContext = 0;
sc.scTcb = 0;
if thread.is_current() {
rescheduleRequired();
}
}
}
}
}
#[cfg(feature = "KERNEL_MCS")]
pub fn maybeDonateSchedContext(tcb: &mut tcb_t, ntfnptr: &notification_t) {
if tcb.tcbSchedContext == 0 {
if let Some(sc) = convert_to_option_mut_type_ref::<sched_context_t>(
ntfnptr.get_ntfnSchedContext() as usize,
) {
if sc.scTcb == 0 {
sc.schedContext_donate(tcb);
sc.schedContext_resume();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions sel4_ipc/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl Transfer for tcb_t {
#[cfg(feature = "KERNEL_MCS")]
fn do_reply(&mut self, reply: &mut reply_t, grant: bool) {
use sel4_common::{ffi::current_fault, structures_gen::seL4_Fault_Timeout};
use sel4_task::{handleTimeout, ksCurSC, sched_context::sched_context_t};
use sel4_task::{handleTimeout, sched_context::sched_context_t};

if reply.replyTCB == 0
|| convert_to_mut_type_ref::<tcb_t>(reply.replyTCB)
Expand All @@ -358,7 +358,7 @@ impl Transfer for tcb_t {
assert!(reply.replyTCB == 0);

let sc = convert_to_mut_type_ref::<sched_context_t>(receiver.tcbSchedContext);
if sc.sc_sporadic() && sc.get_ptr() != unsafe { ksCurSC } {
if sc.sc_sporadic() && !sc.is_current() {
sc.refill_unblock_check();
}
assert_eq!(receiver.get_state(), ThreadState::ThreadStateBlockedOnReply);
Expand Down
6 changes: 5 additions & 1 deletion sel4_task/src/sched_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ impl sched_context {
self.scPeriod == 0
}
#[inline]
pub fn is_current(&self) -> bool {
self.get_ptr() == unsafe { ksCurSC }
}
#[inline]
pub fn sc_released(&mut self) -> bool {
if self.sc_active() {
assert!(self.refill_sufficient(0));
Expand Down Expand Up @@ -295,7 +299,7 @@ impl sched_context {
assert!(tcb.tcbSchedContext == 0);
tcb.tcbSchedContext = self.get_ptr();
self.scTcb = tcb.get_ptr();
if self.sc_sporadic() && self.sc_active() && self.get_ptr() != unsafe { ksCurSC } {
if self.sc_sporadic() && self.sc_active() && !self.is_current() {
self.refill_unblock_check()
}
self.schedContext_resume();
Expand Down
20 changes: 13 additions & 7 deletions sel4_task/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,18 @@ fn chooseThread() {
}
};
assert_ne!(thread, 0);
assert!(convert_to_mut_type_ref::<tcb_t>(thread).is_schedulable());
#[cfg(feature="KERNEL_MCS")]
{
assert!(convert_to_mut_type_ref::<sched_context_t>(convert_to_mut_type_ref::<tcb_t>(thread).tcbSchedContext).refill_sufficient(0));
assert!(convert_to_mut_type_ref::<sched_context_t>(convert_to_mut_type_ref::<tcb_t>(thread).tcbSchedContext).refill_ready());
}
assert!(convert_to_mut_type_ref::<tcb_t>(thread).is_schedulable());
#[cfg(feature = "KERNEL_MCS")]
{
assert!(convert_to_mut_type_ref::<sched_context_t>(
convert_to_mut_type_ref::<tcb_t>(thread).tcbSchedContext
)
.refill_sufficient(0));
assert!(convert_to_mut_type_ref::<sched_context_t>(
convert_to_mut_type_ref::<tcb_t>(thread).tcbSchedContext
)
.refill_ready());
}
convert_to_mut_type_ref::<tcb_t>(thread).switch_to_this();
} else {
#[cfg(target_arch = "aarch64")]
Expand Down Expand Up @@ -883,7 +889,7 @@ pub fn activateThread() {
ThreadState::ThreadStateRestart => {
let pc = thread.tcbArch.get_register(ArchReg::FaultIP);
// setNextPC(thread, pc);
// sel4_common::println!("restart pc is {:x}",pc);
// sel4_common::println!("restart pc is {:x}",pc);
thread.tcbArch.set_register(ArchReg::NextIP, pc);
// setThreadState(thread, ThreadStateRunning);
set_thread_state(thread, ThreadState::ThreadStateRunning);
Expand Down

0 comments on commit 49ee9ca

Please sign in to comment.