Skip to content

Commit

Permalink
pass the tcb set space function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiyuanSue committed Dec 5, 2024
1 parent fb7346e commit 1fdcd7c
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 7 deletions.
14 changes: 14 additions & 0 deletions kernel/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,25 @@ pub const RISCVLoadPageFault: usize = 13;
pub const RISCVStorePageFault: usize = 15;
pub const RISCVSupervisorTimer: usize = 9223372036854775813;

pub const thread_control_caps_update_ipc_buffer: usize = 0x1;
pub const thread_control_caps_update_space: usize = 0x2;
pub const thread_control_caps_update_fault: usize = 0x4;
pub const thread_control_caps_update_timeout: usize = 0x8;

pub const thread_control_sched_update_priority: usize = 0x1;
pub const thread_control_sched_update_mcp: usize = 0x2;
pub const thread_control_sched_update_sc: usize = 0x4;
pub const thread_control_sched_update_fault: usize = 0x8;

pub const thread_control_update_priority: usize = 0x1;
pub const thread_control_update_ipc_buffer: usize = 0x2;
pub const thread_control_update_space: usize = 0x4;
pub const thread_control_update_mcp: usize = 0x8;

pub const thread_control_update_sc: usize = 0x10;
pub const thread_control_update_fault: usize = 0x20;
pub const thread_control_update_timeout: usize = 0x40;

pub const seL4_WordBits: usize = 64;

pub const seL4_UserTop: usize = 0x00007fffffffffff;
Expand Down
147 changes: 147 additions & 0 deletions kernel/src/syscall/invocation/decode/decode_tcb_invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ use sel4_cspace::capability::cap_func;
use sel4_cspace::interface::cte_t;
use sel4_task::{get_currenct_thread, set_thread_state, tcb_t, ThreadState};

use crate::config::{
thread_control_caps_update_fault, thread_control_caps_update_ipc_buffer,
thread_control_caps_update_space,
};
use crate::{
kernel::boot::{current_syscall_error, get_extra_cap_by_index},
syscall::utils::{check_ipc_buffer_vaild, check_prio, get_syscall_arg},
Expand Down Expand Up @@ -92,6 +96,7 @@ pub fn decode_tcb_invocation(
call: bool,
buffer: &seL4_IPCBuffer,
) -> exception_t {
sel4_common::println!("label is {}", invLabel as usize);
match invLabel {
MessageLabel::TCBReadRegisters => decode_read_registers(capability, length, call, buffer),
MessageLabel::TCBWriteRegisters => decode_write_registers(capability, length, buffer),
Expand Down Expand Up @@ -324,6 +329,7 @@ fn decode_tcb_configure(
}

set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart);
#[cfg(not(feature = "KERNEL_MCS"))]
let status = invoke_tcb_set_space(
target_thread,
target_thread_slot,
Expand All @@ -333,6 +339,20 @@ fn decode_tcb_configure(
vroot_cap,
vroot_slot,
);
#[cfg(feature = "KERNEL_MCS")]
let status = invoke_tcb_set_space(
target_thread,
target_thread_slot,
&cap_null_cap::new().unsplay(),
unsafe { &mut *(0 as *mut cte_t) },
&cap_null_cap::new().unsplay(),
unsafe { &mut *(0 as *mut cte_t) },
croot_cap,
croot_slot,
vroot_cap,
vroot_slot,
thread_control_caps_update_space | thread_control_caps_update_ipc_buffer,
);
if status != exception_t::EXCEPTION_NONE {
return status;
}
Expand Down Expand Up @@ -513,6 +533,7 @@ fn decode_set_ipc_buffer(
)
}

#[cfg(not(feature = "KERNEL_MCS"))]
fn decode_set_space(
capability: &cap_thread_cap,
length: usize,
Expand Down Expand Up @@ -590,6 +611,132 @@ fn decode_set_space(
vroot_slot,
)
}
#[cfg(feature = "KERNEL_MCS")]
pub fn validFaultHandler(capability: &cap) -> bool {
use sel4_common::structures_gen::cap_Splayed;

match capability.clone().splay() {
cap_Splayed::endpoint_cap(data) => {
if data.get_capCanSend() == 0
|| (data.get_capCanGrant() == 0 && data.get_capCanGrantReply() == 0)
{
unsafe {
current_syscall_error._type = seL4_InvalidCapability;
}
return false;
}
return true;
}
cap_Splayed::null_cap(_) => {
return true;
}
_ => {
unsafe {
current_syscall_error._type = seL4_InvalidCapability;
}
return false;
}
}
}
#[cfg(feature = "KERNEL_MCS")]
fn decode_set_space(
capability: &cap_thread_cap,
length: usize,
slot: &mut cte_t,
buffer: &seL4_IPCBuffer,
) -> exception_t {
if length < 2
|| get_extra_cap_by_index(0).is_none()
|| get_extra_cap_by_index(1).is_none()
|| get_extra_cap_by_index(2).is_none()
{
sel4_common::println!("TCB SetSpace: Truncated message. {}", length);
unsafe {
current_syscall_error._type = seL4_TruncatedMessage;
}
return exception_t::EXCEPTION_SYSCALL_ERROR;
}

let croot_data = get_syscall_arg(0, buffer);
let vroot_data = get_syscall_arg(1, buffer);

let fh_slot = get_extra_cap_by_index(0).unwrap();
let fh_cap = &fh_slot.clone().capability;

let croot_slot = get_extra_cap_by_index(1).unwrap();
let mut croot_cap = &croot_slot.capability;

let vroot_slot = get_extra_cap_by_index(2).unwrap();
let mut vroot_cap = &vroot_slot.capability;

let target_thread = convert_to_mut_type_ref::<tcb_t>(capability.get_capTCBPtr() as usize);
if target_thread.get_cspace(tcbCTable).is_long_running_delete()
|| target_thread.get_cspace(tcbVTable).is_long_running_delete()
{
sel4_common::println!("TCB Configure: CSpace or VSpace currently being deleted.");
unsafe {
current_syscall_error._type = seL4_IllegalOperation;
}
return exception_t::EXCEPTION_SYSCALL_ERROR;
}

let decode_croot_cap = decode_set_space_args(croot_data, croot_cap, croot_slot);
let binding = decode_croot_cap.clone().unwrap();
match decode_croot_cap {
Ok(_) => croot_cap = &binding,
Err(status) => return status,
}
if croot_cap.get_tag() != cap_tag::cap_cnode_cap {
sel4_common::println!("TCB Configure: CSpace cap is invalid.");
unsafe {
current_syscall_error._type = seL4_IllegalOperation;
}
return exception_t::EXCEPTION_SYSCALL_ERROR;
}

let decode_vroot_cap_ret = decode_set_space_args(vroot_data, vroot_cap, vroot_slot);
let binding = decode_vroot_cap_ret.clone().unwrap();
match decode_vroot_cap_ret {
Ok(_) => vroot_cap = &binding,
Err(status) => return status,
}
#[cfg(target_arch = "riscv64")]
if !is_valid_vtable_root(&vroot_cap) {
unsafe {
current_syscall_error._type = seL4_IllegalOperation;
}
return exception_t::EXCEPTION_SYSCALL_ERROR;
}
#[cfg(target_arch = "aarch64")]
if !vroot_cap.is_valid_vtable_root() {
unsafe {
current_syscall_error._type = seL4_IllegalOperation;
}
return exception_t::EXCEPTION_SYSCALL_ERROR;
}
if !validFaultHandler(fh_cap) {
sel4_common::println!("TCB SetSpace: fault endpoint cap invalid.");
unsafe {
current_syscall_error.invalidCapNumber = 1;
}
return exception_t::EXCEPTION_SYSCALL_ERROR;
}

set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart);
invoke_tcb_set_space(
target_thread,
slot,
&fh_cap,
fh_slot,
&cap_null_cap::new().unsplay(),
unsafe { &mut *(0 as *mut cte_t) },
&croot_cap,
croot_slot,
&vroot_cap,
vroot_slot,
thread_control_caps_update_space | thread_control_caps_update_fault,
)
}

fn decode_bind_notification(capability: &cap_thread_cap) -> exception_t {
if get_extra_cap_by_index(0).is_none() {
Expand Down
122 changes: 119 additions & 3 deletions kernel/src/syscall/invocation/invoke_tcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn invoke_tcb_set_priority(target: &mut tcb_t, prio: usize) -> exception_t {
target.set_priority(prio);
exception_t::EXCEPTION_NONE
}

#[cfg(not(feature = "KERNEL_MCS"))]
pub fn invoke_tcb_set_space(
target: &mut tcb_t,
slot: &mut cte_t,
Expand Down Expand Up @@ -200,6 +200,122 @@ pub fn invoke_tcb_set_space(
}
exception_t::EXCEPTION_NONE
}
#[cfg(feature = "KERNEL_MCS")]
#[no_mangle]
pub fn installTCBCap(
target: &mut tcb_t,
tCap: &cap,
slot: &mut cte_t,
index: usize,
newCap: &cap,
srcSlot: &mut cte_t,
) -> exception_t {
let mut rootSlot = target.get_cspace_mut_ref(tcbBuffer);
let e = rootSlot.delete_all(true);
if e != exception_t::EXCEPTION_NONE {
return e;
}
if same_object_as(newCap, &srcSlot.capability) && same_object_as(tCap, &slot.capability) {
cte_insert(newCap, srcSlot, &mut rootSlot);
}
return e;
}
#[cfg(feature = "KERNEL_MCS")]
pub fn invoke_tcb_set_space(
target: &mut tcb_t,
slot: &mut cte_t,
fh_newCap: &cap,
fh_srcSlot: &mut cte_t,
th_newCap: &cap,
th_srcSlot: &mut cte_t,
croot_new_cap: &cap,
croot_src_slot: &mut cte_t,
vroot_new_cap: &cap,
vroot_src_slot: &mut cte_t,
updateFlags: usize,
) -> exception_t {
use sel4_common::sel4_config::{tcbFaultHandler, tcbTimeoutHandler};

use crate::config::{
thread_control_caps_update_fault, thread_control_caps_update_space,
thread_control_caps_update_timeout,
};
let target_cap = cap_thread_cap::new(target.get_ptr() as u64).unsplay();
if updateFlags & thread_control_caps_update_fault != 0 {
let e = installTCBCap(
target,
&target_cap,
slot,
tcbFaultHandler,
fh_newCap,
fh_srcSlot,
);
if e != exception_t::EXCEPTION_NONE {
return e;
}
}
if updateFlags & thread_control_caps_update_timeout != 0 {
let e = installTCBCap(
target,
&target_cap,
slot,
tcbTimeoutHandler,
th_newCap,
th_srcSlot,
);
if e != exception_t::EXCEPTION_NONE {
return e;
}
}
if updateFlags & thread_control_caps_update_space != 0 {
let e = installTCBCap(
target,
&target_cap,
slot,
tcbCTable,
croot_new_cap,
croot_src_slot,
);
if e != exception_t::EXCEPTION_NONE {
return e;
}
let e = installTCBCap(
target,
&target_cap,
slot,
tcbVTable,
vroot_new_cap,
vroot_src_slot,
);
if e != exception_t::EXCEPTION_NONE {
return e;
}
}

// target.tcbFaultHandler = fault_ep;
// let root_slot = target.get_cspace_mut_ref(tcbCTable);
// let status = root_slot.delete_all(true);
// if status != exception_t::EXCEPTION_NONE {
// return status;
// }
// if same_object_as(croot_new_cap, &croot_src_slot.capability)
// && same_object_as(&target_cap, &slot.capability)
// {
// cte_insert(croot_new_cap, croot_src_slot, root_slot);
// }

// let root_vslot = target.get_cspace_mut_ref(tcbVTable);
// let status = root_vslot.delete_all(true);
// if status != exception_t::EXCEPTION_NONE {
// return status;
// }
// if same_object_as(vroot_new_cap, &vroot_src_slot.capability)
// && same_object_as(&target_cap, &slot.capability)
// {
// cte_insert(vroot_new_cap, vroot_src_slot, root_vslot);
// }
exception_t::EXCEPTION_NONE
}

pub fn invoke_tcb_set_ipc_buffer(
target: &mut tcb_t,
Expand All @@ -215,11 +331,11 @@ pub fn invoke_tcb_set_ipc_buffer(
return status;
}
target.tcbIPCBuffer = buffer_addr;
if let Some(buffer_src_slot) = buffer_src_slot {
if let Some(mut buffer_src_slot) = buffer_src_slot {
if same_object_as(&buffer_cap, &buffer_src_slot.capability)
&& same_object_as(&target_cap, &slot.capability)
{
cte_insert(&buffer_cap, buffer_src_slot, buffer_slot);
cte_insert(&buffer_cap, &mut buffer_src_slot, buffer_slot);
}
}
if target.is_current() {
Expand Down
4 changes: 0 additions & 4 deletions sel4_task/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ pub extern "C" fn sendIPC() {
unimplemented!("MCS");
}
#[no_mangle]
pub extern "C" fn installTCBCap() {
unimplemented!("MCS");
}
#[no_mangle]
pub extern "C" fn tcbSchedDequeue(tcb: &mut tcb_t) {
(*tcb).sched_dequeue();
}
Expand Down

0 comments on commit 1fdcd7c

Please sign in to comment.