From 044e7b108957fb17665f8d0933af2ad14f7c49c8 Mon Sep 17 00:00:00 2001 From: ZhiyuanSue <108735103+ZhiyuanSue@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:04:47 +0800 Subject: [PATCH] Update kernel (#16) * almost done all the data structure changes and sync (#11) * update the sel4_cspace/src/arch/aarch64/mod.rs with some definations * add some funcitons * update the objecttype definations * clean the pgde/pude/pde structures definetions * change the definetions of lookup and update 4 todos * Update kernel 3 (#12) * update the sel4_cspace/src/arch/aarch64/mod.rs with some definations * add some funcitons * update the objecttype definations * clean the pgde/pude/pde structures definetions * change the definetions of lookup and update 4 todos * try to fix some functions * fix some errors with more errors generated * clean some unused functions * update and pass the complier, but still have unimplement * Update kernel 4 (#13) * fill in the unimplement code * clean the warnings * add the 4k page * FIX:the page entry problem of map kernel window * Update kernel 5 (#14) * fix the word the vs used * fix the page bug, and let the root task work * Update kernel 6 (#15) * fix make user pte todo * fix the unmap assert * fix the bug of the unmap page * fix another bug * seems fix bug --- src/arch/aarch64/exception.rs | 12 +- src/arch/aarch64/pg.rs | 2 +- src/arch/riscv/exception.rs | 14 +- src/boot/root_server.rs | 2 +- src/interfaces_impl/cspace.rs | 53 +- src/interrupt/handler.rs | 4 +- src/syscall/invocation/arch/aarch64.rs | 29 +- src/syscall/invocation/arch/riscv64.rs | 6 +- src/syscall/invocation/decode/arch/aarch64.rs | 690 +++++++++--------- src/syscall/invocation/decode/arch/riscv64.rs | 12 +- .../decode/decode_tcb_invocation.rs | 24 +- src/syscall/invocation/invoke_mmu_op.rs | 211 +++--- src/syscall/invocation/mod.rs | 2 +- src/utils.rs | 26 +- 14 files changed, 544 insertions(+), 543 deletions(-) diff --git a/src/arch/aarch64/exception.rs b/src/arch/aarch64/exception.rs index a82ad96..5ea1155 100644 --- a/src/arch/aarch64/exception.rs +++ b/src/arch/aarch64/exception.rs @@ -15,8 +15,8 @@ use aarch64_cpu::registers::Readable; use aarch64_cpu::registers::TTBR0_EL1; use log::debug; use sel4_common::arch::ArchReg::*; -use sel4_common::print; use sel4_common::fault::seL4_Fault_t; +use sel4_common::print; use sel4_common::sel4_config::seL4_MsgMaxLength; use sel4_common::structures::exception_t; use sel4_common::utils::global_read; @@ -27,23 +27,23 @@ use super::instruction::*; #[no_mangle] pub fn handleUnknownSyscall(w: isize) -> exception_t { - let thread = get_currenct_thread(); + let thread = get_currenct_thread(); if w == SysDebugPutChar { - print!("{}",thread.tcbArch.get_register(Cap) as u8 as char); + print!("{}", thread.tcbArch.get_register(Cap) as u8 as char); return exception_t::EXCEPTION_NONE; } if w == SysDebugDumpScheduler { - // unimplement debug + // unimplement debug // println!("debug dump scheduler"); return exception_t::EXCEPTION_NONE; } if w == SysDebugHalt { - // unimplement debug + // unimplement debug // println!("debug halt"); return exception_t::EXCEPTION_NONE; } if w == SysDebugSnapshot { - // unimplement debug + // unimplement debug // println!("debug snapshot"); return exception_t::EXCEPTION_NONE; } diff --git a/src/arch/aarch64/pg.rs b/src/arch/aarch64/pg.rs index 464f5b0..fd87e64 100644 --- a/src/arch/aarch64/pg.rs +++ b/src/arch/aarch64/pg.rs @@ -67,7 +67,7 @@ extern "C" fn decodeARMMMUInvocation( pub fn set_vm_root_for_flush(vspace: usize, asid: asid_t) -> bool { let thread_root = get_currenct_thread().get_cspace(tcbVTable).cap; - if thread_root.is_valid_native_root() && thread_root.get_pgd_base_ptr() == vspace as usize { + if thread_root.is_valid_native_root() && thread_root.get_vs_base_ptr() == vspace as usize { return false; } diff --git a/src/arch/riscv/exception.rs b/src/arch/riscv/exception.rs index 643ee31..630c592 100644 --- a/src/arch/riscv/exception.rs +++ b/src/arch/riscv/exception.rs @@ -13,31 +13,31 @@ use crate::syscall::{ use log::debug; use sel4_common::arch::ArchReg::*; use sel4_common::fault::seL4_Fault_t; +use sel4_common::print; use sel4_common::sel4_config::seL4_MsgMaxLength; use sel4_common::structures::exception_t; use sel4_cspace::arch::CapTag; -use sel4_common::print; use sel4_task::{activateThread, get_currenct_thread, schedule}; #[no_mangle] pub fn handleUnknownSyscall(w: isize) -> exception_t { - let thread = get_currenct_thread(); + let thread = get_currenct_thread(); if w == SysDebugPutChar { - print!("{}",thread.tcbArch.get_register(Cap) as u8 as char); + print!("{}", thread.tcbArch.get_register(Cap) as u8 as char); return exception_t::EXCEPTION_NONE; } if w == SysDebugDumpScheduler { - // unimplement debug + // unimplement debug // println!("debug dump scheduler"); return exception_t::EXCEPTION_NONE; } if w == SysDebugHalt { - // unimplement debug + // unimplement debug // println!("debug halt"); return exception_t::EXCEPTION_NONE; } if w == SysDebugSnapshot { - // unimplement debug + // unimplement debug // println!("debug snap shot"); return exception_t::EXCEPTION_NONE; } @@ -68,7 +68,7 @@ pub fn handleUnknownSyscall(w: isize) -> exception_t { debug!("SysDebugNameThread: Name too long, halting"); halt(); } - + // setThreadName(TCB_PTR(cap_thread_cap_get_capTCBPtr(lu_ret.cap)), name); return exception_t::EXCEPTION_NONE; } diff --git a/src/boot/root_server.rs b/src/boot/root_server.rs index 9378f6c..427d05d 100644 --- a/src/boot/root_server.rs +++ b/src/boot/root_server.rs @@ -442,7 +442,7 @@ unsafe fn rust_create_it_address_space(root_cnode_cap: &cap_t, it_v_reg: v_regio #[cfg(target_arch = "aarch64")] unsafe fn rust_create_it_address_space(root_cnode_cap: &cap_t, it_v_reg: v_region_t) -> cap_t { // create the PGD - let vspace_cap = cap_t::new_page_global_directory_cap(IT_ASID, rootserver.vspace, 1); + let vspace_cap = cap_t::new_vspace_cap(IT_ASID, rootserver.vspace, 1); let ptr = root_cnode_cap.get_cap_ptr() as *mut cte_t; let slot_pos_before = ndks_boot.slot_pos_cur; write_slot(ptr.add(seL4_CapInitThreadVspace), vspace_cap.clone()); diff --git a/src/interfaces_impl/cspace.rs b/src/interfaces_impl/cspace.rs index 03409e4..c71921c 100644 --- a/src/interfaces_impl/cspace.rs +++ b/src/interfaces_impl/cspace.rs @@ -12,11 +12,9 @@ use sel4_ipc::{endpoint_t, notification_t, Transfer}; use sel4_task::{get_currenct_thread, ksWorkUnitsCompleted, tcb_t}; #[cfg(target_arch = "riscv64")] use sel4_vspace::find_vspace_for_asid; -use sel4_vspace::{asid_pool_t, asid_t, delete_asid, delete_asid_pool, unmapPage, PTE}; #[cfg(target_arch = "aarch64")] -use sel4_vspace::{ - unmap_page_directory, unmap_page_table, unmap_page_upper_directory, PDE, PGDE, PUDE, -}; +use sel4_vspace::unmap_page_table; +use sel4_vspace::{asid_pool_t, asid_t, delete_asid, delete_asid_pool, unmapPage, PTE}; #[cfg(target_arch = "riscv64")] #[no_mangle] @@ -89,27 +87,32 @@ pub fn Arch_finaliseCap(cap: &cap_t, final_: bool) -> finaliseCap_ret { } } } - CapTag::CapPageGlobalDirectoryCap => { - if final_ && cap.get_pgd_is_mapped() == 1 { - deleteASID(cap.get_pgd_is_mapped(), cap.get_pgd_base_ptr() as _); - } - } - CapTag::CapPageUpperDirectoryCap => { - if final_ && cap.get_pud_is_mapped() == 1 { - let pud = ptr_to_mut(cap.get_pt_base_ptr() as *mut PUDE); - unmap_page_upper_directory( - cap.get_pud_mapped_asid(), - cap.get_pud_mapped_address(), - pud, - ); - } - } - CapTag::CapPageDirectoryCap => { - if final_ && cap.get_pd_is_mapped() == 1 { - let pd = ptr_to_mut(cap.get_pt_base_ptr() as *mut PDE); - unmap_page_directory(cap.get_pd_mapped_asid(), cap.get_pd_mapped_address(), pd); + CapTag::CapVspaceCap => { + if final_ && cap.get_vs_is_mapped() == 1 { + deleteASID(cap.get_vs_is_mapped(), cap.get_vs_base_ptr() as _); } } + // CapTag::CapPageGlobalDirectoryCap => { + // if final_ && cap.get_pgd_is_mapped() == 1 { + // deleteASID(cap.get_pgd_is_mapped(), cap.get_pgd_base_ptr() as _); + // } + // } + // CapTag::CapPageUpperDirectoryCap => { + // if final_ && cap.get_pud_is_mapped() == 1 { + // let pud = ptr_to_mut(cap.get_pt_base_ptr() as *mut PUDE); + // unmap_page_upper_directory( + // cap.get_pud_mapped_asid(), + // cap.get_pud_mapped_address(), + // pud, + // ); + // } + // } + // CapTag::CapPageDirectoryCap => { + // if final_ && cap.get_pd_is_mapped() == 1 { + // let pd = ptr_to_mut(cap.get_pt_base_ptr() as *mut PDE); + // unmap_page_directory(cap.get_pd_mapped_asid(), cap.get_pd_mapped_address(), pd); + // } + // } CapTag::CapPageTableCap => { if final_ && cap.get_pt_is_mapped() == 1 { let pte = ptr_to_mut(cap.get_pt_base_ptr() as *mut PTE); @@ -259,7 +262,6 @@ pub fn preemptionPoint() -> exception_t { } } - #[no_mangle] #[cfg(target_arch = "riscv64")] pub fn deleteASID(asid: asid_t, vspace: *mut PTE) { @@ -276,7 +278,7 @@ pub fn deleteASID(asid: asid_t, vspace: *mut PTE) { #[no_mangle] #[cfg(target_arch = "aarch64")] -pub fn deleteASID(asid: asid_t, vspace: *mut PGDE) { +pub fn deleteASID(asid: asid_t, vspace: *mut PTE) { unsafe { if let Err(lookup_fault) = delete_asid( asid, @@ -288,7 +290,6 @@ pub fn deleteASID(asid: asid_t, vspace: *mut PGDE) { } } - #[no_mangle] pub fn deleteASIDPool(asid_base: asid_t, pool: *mut asid_pool_t) { unsafe { diff --git a/src/interrupt/handler.rs b/src/interrupt/handler.rs index 06b34ad..efdd868 100644 --- a/src/interrupt/handler.rs +++ b/src/interrupt/handler.rs @@ -1,12 +1,12 @@ use crate::arch::resetTimer; -use crate::config::{irqInvalid,maxIRQ}; +use crate::config::{irqInvalid, maxIRQ}; use crate::interrupt::*; use core::intrinsics::unlikely; use log::debug; use sel4_common::structures::exception_t; use sel4_cspace::interface::CapTag; use sel4_ipc::notification_t; -use sel4_task::{activateThread, schedule,timerTick}; +use sel4_task::{activateThread, schedule, timerTick}; #[no_mangle] pub fn handleInterruptEntry() -> exception_t { diff --git a/src/syscall/invocation/arch/aarch64.rs b/src/syscall/invocation/arch/aarch64.rs index 0f84415..d46cb77 100644 --- a/src/syscall/invocation/arch/aarch64.rs +++ b/src/syscall/invocation/arch/aarch64.rs @@ -1,4 +1,7 @@ -use sel4_common::{arch::{vm_rights_t, ObjectType}, sel4_config::{asidInvalid, ARM_Large_Page, ARM_Small_Page}}; +use sel4_common::{ + arch::{vm_rights_t, ObjectType}, + sel4_config::{asidInvalid, ARM_Huge_Page, ARM_Large_Page, ARM_Small_Page}, +}; use sel4_cspace::arch::cap_t; use sel4_vspace::pptr_t; @@ -17,18 +20,6 @@ pub fn arch_create_object( asidInvalid, region_base, ), - ObjectType::seL4_ARM_PageUpperDirectoryObject => { - cap_t::new_page_upper_directory_cap(asidInvalid, region_base, 0, 0) - } - ObjectType::seL4_ARM_PageDirectoryObject => { - cap_t::new_page_directory_cap(asidInvalid, region_base, 0, 0) - } - ObjectType::seL4_ARM_PageTableObject => { - cap_t::new_page_table_cap(asidInvalid, region_base, 0, 0) - } - ObjectType::seL4_ARM_PageGlobalDirectoryObject => { - cap_t::new_page_global_directory_cap(asidInvalid, region_base, 0) - } ObjectType::seL4_ARM_LargePageObject => cap_t::new_frame_cap( device_mem, vm_rights_t::VMReadWrite as _, @@ -37,6 +28,18 @@ pub fn arch_create_object( asidInvalid, region_base, ), + ObjectType::seL4_ARM_HugePageObject => cap_t::new_frame_cap( + device_mem, + vm_rights_t::VMReadWrite as _, + 0, + ARM_Huge_Page, + asidInvalid, + region_base, + ), + ObjectType::seL4_ARM_VSpaceObject => cap_t::new_vspace_cap(asidInvalid, region_base, 0), + ObjectType::seL4_ARM_PageTableObject => { + cap_t::new_page_table_cap(asidInvalid, region_base, 0, 0) + } _ => { unimplemented!( "create object: {:?} region: {:#x} - {:#x}", diff --git a/src/syscall/invocation/arch/riscv64.rs b/src/syscall/invocation/arch/riscv64.rs index 7804abd..792e3b7 100644 --- a/src/syscall/invocation/arch/riscv64.rs +++ b/src/syscall/invocation/arch/riscv64.rs @@ -1,8 +1,10 @@ -use sel4_common::{arch::{vm_rights_t, ObjectType}, sel4_config::asidInvalid}; +use sel4_common::{ + arch::{vm_rights_t, ObjectType}, + sel4_config::asidInvalid, +}; use sel4_cspace::arch::cap_t; use sel4_vspace::pptr_t; - pub fn arch_create_object( obj_type: ObjectType, region_base: pptr_t, diff --git a/src/syscall/invocation/decode/arch/aarch64.rs b/src/syscall/invocation/decode/arch/aarch64.rs index 7e55388..79f329f 100644 --- a/src/syscall/invocation/decode/arch/aarch64.rs +++ b/src/syscall/invocation/decode/arch/aarch64.rs @@ -5,19 +5,17 @@ use crate::syscall::invocation::decode::current_syscall_error; use crate::syscall::ThreadState; use crate::syscall::{current_lookup_fault, get_syscall_arg, set_thread_state, unlikely}; use crate::syscall::{ensure_empty_slot, get_currenct_thread, lookup_slot_for_cnode_op}; -use crate::utils::clear_memory_pt; use log::debug; use sel4_common::arch::maskVMRights; use sel4_common::cap_rights::seL4_CapRights_t; use sel4_common::fault::lookup_fault_t; use sel4_common::sel4_config::{ - asidInvalid, asidLowBits, nASIDPools, seL4_AlignmentError, seL4_FailedLookup, seL4_RangeError, - ARM_Huge_Page, ARM_Large_Page, ARM_Small_Page, PAGE_BITS, PGD_INDEX_OFFSET, PUD_INDEX_OFFSET, + asidInvalid, asidLowBits, nASIDPools, seL4_AlignmentError, seL4_FailedLookup, seL4_PageBits, + seL4_RangeError, }; use sel4_common::sel4_config::{seL4_DeleteFirst, seL4_InvalidArgument}; use sel4_common::sel4_config::{ seL4_IllegalOperation, seL4_InvalidCapability, seL4_RevokeFirst, seL4_TruncatedMessage, - PD_INDEX_OFFSET, }; use sel4_common::utils::{ convert_ref_type_to_usize, convert_to_mut_type_ref, global_ops, pageBitsForSize, ptr_to_mut, @@ -33,13 +31,12 @@ use sel4_cspace::interface::{cap_t, cte_insert, cte_t, CapTag}; use sel4_vspace::{ asid_map_t, asid_pool_t, asid_t, clean_by_va_pou, doFlush, find_vspace_for_asid, - get_asid_pool_by_index, makeUser3rdLevel, make_user_1st_level, make_user_2nd_level, - pptr_to_paddr, set_asid_pool_by_index, vm_attributes_t, vptr_t, PDE, PGDE, PTE, PUDE, + get_asid_pool_by_index, pptr_to_paddr, pte_tag_t, set_asid_pool_by_index, vm_attributes_t, + vptr_t, PTE, }; use crate::syscall::invocation::invoke_mmu_op::{ - invoke_huge_page_map, invoke_large_page_map, invoke_page_get_address, invoke_page_table_unmap, - invoke_page_unmap, invoke_small_page_map, + invoke_page_get_address, invoke_page_map, invoke_page_table_unmap, invoke_page_unmap, }; use crate::{ config::maxIRQ, @@ -55,15 +52,7 @@ pub fn decode_mmu_invocation( buffer: &seL4_IPCBuffer, ) -> exception_t { match slot.cap.get_cap_type() { - CapTag::CapPageGlobalDirectoryCap => { - decode_vspace_root_invocation(label, length, slot, buffer) - } - CapTag::CapPageUpperDirectoryCap => { - decode_page_upper_directory_invocation(label, length, slot, buffer) - } - CapTag::CapPageDirectoryCap => { - decode_page_directory_invocation(label, length, slot, buffer) - } + CapTag::CapVspaceCap => decode_vspace_root_invocation(label, length, slot, buffer), CapTag::CapPageTableCap => decode_page_table_invocation(label, length, slot, buffer), CapTag::CapFrameCap => decode_frame_invocation(label, length, slot, call, buffer), CapTag::CapASIDControlCap => decode_asid_control(label, length, buffer), @@ -116,7 +105,7 @@ fn decode_page_table_invocation( return exception_t::EXCEPTION_SYSCALL_ERROR; } - let vaddr = get_syscall_arg(0, buffer) & !(MASK!(PD_INDEX_OFFSET)); + let vaddr = get_syscall_arg(0, buffer); let vspace_root_cap = convert_to_mut_type_ref::(global_ops!(current_extra_caps.excaprefs[0])); @@ -126,8 +115,8 @@ fn decode_page_table_invocation( return exception_t::EXCEPTION_SYSCALL_ERROR; } - let vspace_root = vspace_root_cap.get_pgd_base_ptr(); - let asid = vspace_root_cap.get_pgd_mapped_asid(); + let vspace_root = vspace_root_cap.get_vs_base_ptr(); + let asid = vspace_root_cap.get_vs_mapped_asid(); if unlikely(vaddr > USER_TOP) { global_ops!(current_syscall_error._type = seL4_InvalidArgument); @@ -148,31 +137,27 @@ fn decode_page_table_invocation( return exception_t::EXCEPTION_SYSCALL_ERROR; } - let pd_slot = PGDE::new_from_pte(vspace_root).lookup_pd_slot(vaddr); + let pd_slot = PTE(vspace_root).lookup_pt_slot(vaddr); - if pd_slot.status != exception_t::EXCEPTION_NONE { - global_ops!(current_syscall_error._type = seL4_FailedLookup); - global_ops!(current_syscall_error.failedLookupWasSource = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } if unlikely( - ptr_to_ref(pd_slot.pdSlot).get_present() || ptr_to_ref(pd_slot.pdSlot).is_larger_page(), + pd_slot.ptBitsLeft == seL4_PageBits + || (ptr_to_ref(pd_slot.ptSlot).get_type() != (pte_tag_t::pte_invalid) as usize), ) { global_ops!(current_syscall_error._type = seL4_DeleteFirst); return exception_t::EXCEPTION_SYSCALL_ERROR; } - - let pde = PDE::new_page(pptr_to_paddr(cte.cap.get_pt_base_ptr()), 0x3); + let pte = PTE::pte_new_table(pptr_to_paddr(cte.cap.get_pt_base_ptr())); cte.cap.set_pt_is_mapped(1); cte.cap.set_pt_mapped_asid(asid); - cte.cap.set_pt_mapped_address(vaddr); + cte.cap + .set_pt_mapped_address(vaddr & !(MASK!(pd_slot.ptBitsLeft))); get_currenct_thread().set_state(ThreadState::ThreadStateRestart); - *ptr_to_mut(pd_slot.pdSlot) = pde; + *ptr_to_mut(pd_slot.ptSlot) = pte; // log::warn!("Need to clean D-Cache using cleanByVA_PoU"); clean_by_va_pou( - convert_ref_type_to_usize(ptr_to_mut(pd_slot.pdSlot)), - pptr_to_paddr(convert_ref_type_to_usize(ptr_to_mut(pd_slot.pdSlot))), + convert_ref_type_to_usize(ptr_to_mut(pd_slot.ptSlot)), + pptr_to_paddr(convert_ref_type_to_usize(ptr_to_mut(pd_slot.ptSlot))), ); exception_t::EXCEPTION_NONE } @@ -296,11 +281,7 @@ fn decode_frame_invocation( } } -fn decode_asid_control( - label: MessageLabel, - length: usize, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_asid_control(label: MessageLabel, length: usize, buffer: &seL4_IPCBuffer) -> exception_t { if unlikely(label != MessageLabel::ARMASIDControlMakePool) { global_ops!(current_syscall_error._type = seL4_IllegalOperation); return exception_t::EXCEPTION_SYSCALL_ERROR; @@ -386,7 +367,7 @@ fn decode_asid_pool(label: MessageLabel, cte: &mut cte_t) -> exception_t { let vspace_cap_slot = global_ops!(current_extra_caps.excaprefs[0]); let vspace_cap = convert_to_mut_type_ref::(vspace_cap_slot); - if unlikely(!vspace_cap.is_vtable_root() || vspace_cap.get_pgd_is_mapped() == 1) { + if unlikely(!vspace_cap.is_vtable_root() || vspace_cap.get_vs_is_mapped() == 1) { log::debug!("is not a valid vtable root"); global_ops!(current_syscall_error._type = seL4_InvalidCapability); global_ops!(current_syscall_error.invalidArgumentNumber = 1); @@ -429,18 +410,14 @@ fn decode_asid_pool(label: MessageLabel, cte: &mut cte_t) -> exception_t { asid += i; get_currenct_thread().set_state(ThreadState::ThreadStateRestart); - vspace_cap.set_pgd_mapped_asid(asid); - vspace_cap.set_pgd_is_mapped(1); - let asid_map = asid_map_t::new_vspace(vspace_cap.get_pgd_base_ptr()); + vspace_cap.set_vs_mapped_asid(asid); + vspace_cap.set_vs_is_mapped(1); + let asid_map = asid_map_t::new_vspace(vspace_cap.get_vs_base_ptr()); pool[asid & MASK!(asidLowBits)] = asid_map; exception_t::EXCEPTION_NONE } -fn decode_frame_map( - length: usize, - frame_slot: &mut cte_t, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_frame_map(length: usize, frame_slot: &mut cte_t, buffer: &seL4_IPCBuffer) -> exception_t { if length < 3 || get_extra_cap_by_index(0).is_none() { debug!("ARMPageMap: Truncated message."); global_ops!(current_syscall_error._type = seL4_TruncatedMessage); @@ -460,8 +437,8 @@ fn decode_frame_map( global_ops!(current_syscall_error.invalidCapNumber = 1); return exception_t::EXCEPTION_SYSCALL_ERROR; } - let vspace_root = vspace_root_cap.get_pgd_base_ptr(); - let asid = vspace_root_cap.get_pgd_mapped_asid(); + let vspace_root = vspace_root_cap.get_vs_base_ptr(); + let asid = vspace_root_cap.get_vs_mapped_asid(); let find_ret = find_vspace_for_asid(asid); if unlikely(find_ret.status != exception_t::EXCEPTION_NONE) { global_ops!(current_syscall_error._type = seL4_FailedLookup); @@ -499,72 +476,88 @@ fn decode_frame_map( return exception_t::EXCEPTION_SYSCALL_ERROR; } } - // TODO: copy cap in the here. Not write slot when the address is not need to write. - // frame_slot.cap.set_frame_mapped_asid(asid); - // frame_slot.cap.set_frame_mapped_address(vaddr); - - let vspace_root = PGDE::new_from_pte(vspace_root); + let mut vspace_root_pte = PTE::new_from_pte(vspace_root); let base = pptr_to_paddr(frame_slot.cap.get_frame_base_ptr()); - match frame_size { - ARM_Small_Page => { - let lu_ret = vspace_root.lookup_pt_slot(vaddr); - if lu_ret.status != exception_t::EXCEPTION_NONE { - unsafe { - current_syscall_error._type = seL4_FailedLookup; - current_syscall_error.failedLookupWasSource = 0; - } - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - let ptSlot = convert_to_mut_type_ref::(lu_ret.ptSlot as usize); - invoke_small_page_map( - vaddr, - asid, - frame_slot, - makeUser3rdLevel(base, vm_rights, attr), - ptSlot, - ) - } - ARM_Large_Page => { - let lu_ret = vspace_root.lookup_pd_slot(vaddr); - if lu_ret.status != exception_t::EXCEPTION_NONE { - unsafe { - current_syscall_error._type = seL4_FailedLookup; - current_syscall_error.failedLookupWasSource = 0; - } - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - let pdSlot = convert_to_mut_type_ref::(lu_ret.pdSlot as usize); - invoke_large_page_map( - vaddr, - asid, - frame_slot, - make_user_2nd_level(base, vm_rights, attr), - pdSlot, - ) - } - ARM_Huge_Page => { - let lu_ret = vspace_root.lookup_pud_slot(vaddr); - if lu_ret.status != exception_t::EXCEPTION_NONE { - unsafe { - current_syscall_error._type = seL4_FailedLookup; - current_syscall_error.failedLookupWasSource = 0; - } - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); - let pudSlot = convert_to_mut_type_ref::(lu_ret.pudSlot as usize); - invoke_huge_page_map( - vaddr, - asid, - frame_slot, - make_user_1st_level(base, vm_rights, attr), - pudSlot, - ) + let lu_ret = vspace_root_pte.lookup_pt_slot(vaddr); + if unlikely(lu_ret.ptBitsLeft != pageBitsForSize(frame_size)) { + unsafe { + current_lookup_fault = lookup_fault_t::new_missing_cap(lu_ret.ptBitsLeft); + current_syscall_error._type = seL4_FailedLookup; + current_syscall_error.failedLookupWasSource = 0; + return exception_t::EXCEPTION_SYSCALL_ERROR; } - _ => exception_t::EXCEPTION_SYSCALL_ERROR, } + let pt_slot = convert_to_mut_type_ref::(lu_ret.ptSlot as usize); + set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); + frame_slot.cap.set_frame_mapped_asid(asid); + frame_slot.cap.set_frame_mapped_address(vaddr); + return invoke_page_map( + asid, + frame_slot.cap.clone(), + frame_slot, + PTE::make_user_pte(base, vm_rights, attr, frame_size), + pt_slot, + ); + // match frame_size { + // ARM_Small_Page => { + // let lu_ret = vspace_root.lookup_pt_slot(vaddr); + // if lu_ret.status != exception_t::EXCEPTION_NONE { + // unsafe { + // current_syscall_error._type = seL4_FailedLookup; + // current_syscall_error.failedLookupWasSource = 0; + // } + // return exception_t::EXCEPTION_SYSCALL_ERROR; + // } + // set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); + // let ptSlot = convert_to_mut_type_ref::(lu_ret.ptSlot as usize); + // invoke_small_page_map( + // vaddr, + // asid, + // frame_slot, + // makeUser3rdLevel(base, vm_rights, attr), + // ptSlot, + // ) + // } + // ARM_Large_Page => { + // let lu_ret = vspace_root.lookup_pd_slot(vaddr); + // if lu_ret.status != exception_t::EXCEPTION_NONE { + // unsafe { + // current_syscall_error._type = seL4_FailedLookup; + // current_syscall_error.failedLookupWasSource = 0; + // } + // return exception_t::EXCEPTION_SYSCALL_ERROR; + // } + // set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); + // let pdSlot = convert_to_mut_type_ref::(lu_ret.pdSlot as usize); + // invoke_large_page_map( + // vaddr, + // asid, + // frame_slot, + // make_user_2nd_level(base, vm_rights, attr), + // pdSlot, + // ) + // } + // ARM_Huge_Page => { + // let lu_ret = vspace_root.lookup_pud_slot(vaddr); + // if lu_ret.status != exception_t::EXCEPTION_NONE { + // unsafe { + // current_syscall_error._type = seL4_FailedLookup; + // current_syscall_error.failedLookupWasSource = 0; + // } + // return exception_t::EXCEPTION_SYSCALL_ERROR; + // } + // set_thread_state(get_currenct_thread(), ThreadState::ThreadStateRestart); + // let pudSlot = convert_to_mut_type_ref::(lu_ret.pudSlot as usize); + // invoke_huge_page_map( + // vaddr, + // asid, + // frame_slot, + // make_user_1st_level(base, vm_rights, attr), + // pudSlot, + // ) + // } + // _ => exception_t::EXCEPTION_SYSCALL_ERROR, + // } // if length < 3 || get_extra_cap_by_index(0).is_none() { // debug!("ARMPageMap: Truncated message."); // unsafe { @@ -688,7 +681,7 @@ fn decode_frame_map( #[allow(unused)] fn decode_page_table_unmap(pt_cte: &mut cte_t) -> exception_t { if !pt_cte.is_final_cap() { - debug!("RISCVPageTableUnmap: cannot unmap if more than once cap exists"); + debug!("PageTableUnmap: cannot unmap if more than once cap exists"); global_ops!(current_syscall_error._type = seL4_RevokeFirst); return exception_t::EXCEPTION_SYSCALL_ERROR; } @@ -699,32 +692,32 @@ fn decode_page_table_unmap(pt_cte: &mut cte_t) -> exception_t { return invoke_page_table_unmap(cap); } -fn decode_upper_page_directory_unmap(ctSlot: &mut cte_t) -> exception_t { - let cap = &mut ctSlot.cap; - if cap.get_pud_is_mapped() != 0 { - let pud = &mut PUDE(cap.get_pud_base_ptr()); - // TODO:: llh implement unmap_page_upper_directory as PUDE's method , but below two lines code both will cause sel4test end panic - pud.unmap_page_upper_directory(cap.get_pud_mapped_asid(), cap.get_pud_mapped_address()); - // unmap_page_upper_directory(cap.get_pud_mapped_asid(), cap.get_pud_mapped_address(), pud); - clear_memory_pt(pud.self_addr() as *mut u8, cap.get_cap_size_bits()); - } - cap.set_pud_is_mapped(0); - exception_t::EXCEPTION_NONE -} - -fn decode_page_directory_unmap(ctSlot: &mut cte_t) -> exception_t { - let cap = &mut ctSlot.cap; - if cap.get_pd_is_mapped() != 0 { - let pd = &mut PDE(cap.get_pud_base_ptr()); - // clear_memory(ptr, bits); - // TODO:: llh implement unmap_page_upper_directory as PUDE's method , but below two lines code both will cause sel4test end panic - pd.unmap_page_directory(cap.get_pd_mapped_asid(), cap.get_pd_mapped_address()); - // unmap_page_directory(cap.get_pd_mapped_asid(), cap.get_pd_mapped_address(), pd); - clear_memory_pt(pd.self_addr() as *mut u8, cap.get_cap_size_bits()); - } - cap.set_pud_is_mapped(0); - exception_t::EXCEPTION_NONE -} +// fn decode_upper_page_directory_unmap(ctSlot: &mut cte_t) -> exception_t { +// let cap = &mut ctSlot.cap; +// if cap.get_pud_is_mapped() != 0 { +// let pud = &mut PUDE(cap.get_pud_base_ptr()); +// // TODO:: llh implement unmap_page_upper_directory as PUDE's method , but below two lines code both will cause sel4test end panic +// pud.unmap_page_upper_directory(cap.get_pud_mapped_asid(), cap.get_pud_mapped_address()); +// // unmap_page_upper_directory(cap.get_pud_mapped_asid(), cap.get_pud_mapped_address(), pud); +// clear_memory_pt(pud.self_addr() as *mut u8, cap.get_cap_size_bits()); +// } +// cap.set_pud_is_mapped(0); +// exception_t::EXCEPTION_NONE +// } + +// fn decode_page_directory_unmap(ctSlot: &mut cte_t) -> exception_t { +// let cap = &mut ctSlot.cap; +// if cap.get_pd_is_mapped() != 0 { +// let pd = &mut PDE(cap.get_pud_base_ptr()); +// // clear_memory(ptr, bits); +// // TODO:: llh implement unmap_page_upper_directory as PUDE's method , but below two lines code both will cause sel4test end panic +// pd.unmap_page_directory(cap.get_pd_mapped_asid(), cap.get_pd_mapped_address()); +// // unmap_page_directory(cap.get_pd_mapped_asid(), cap.get_pd_mapped_address(), pd); +// clear_memory_pt(pd.self_addr() as *mut u8, cap.get_cap_size_bits()); +// } +// cap.set_pud_is_mapped(0); +// exception_t::EXCEPTION_NONE +// } fn decode_vspace_root_invocation( label: MessageLabel, @@ -766,7 +759,7 @@ fn decode_vspace_root_invocation( }; return exception_t::EXCEPTION_SYSCALL_ERROR; } - let vspace_root = PGDE::new_from_pte(cte.cap.get_pgd_base_ptr()); + let vspace_root = cte.cap.get_vs_base_ptr() as *mut PTE; let asid = cte.cap.get_asid_base(); let find_ret = find_vspace_for_asid(asid); if find_ret.status != exception_t::EXCEPTION_NONE { @@ -777,7 +770,7 @@ fn decode_vspace_root_invocation( return exception_t::EXCEPTION_SYSCALL_ERROR; } } - if find_ret.vspace_root.unwrap() as usize != vspace_root.get_ptr() { + if find_ret.vspace_root.unwrap() as usize != ptr_to_ref(vspace_root).get_ptr() { debug!("VSpaceRoot Flush: Invalid VSpace Cap"); unsafe { current_syscall_error._type = seL4_InvalidCapability; @@ -785,24 +778,25 @@ fn decode_vspace_root_invocation( } return exception_t::EXCEPTION_SYSCALL_ERROR; } - let resolve_ret = vspace_root.lookup_frame(start); - if !resolve_ret.valid { + let resolve_ret = ptr_to_mut(vspace_root).lookup_pt_slot(start); + let pte = resolve_ret.ptSlot; + if ptr_to_ref(pte).get_type() != (pte_tag_t::pte_page) as usize { get_currenct_thread().set_state(ThreadState::ThreadStateRestart); return exception_t::EXCEPTION_NONE; } - let page_base_start = start & !MASK!(pageBitsForSize(resolve_ret.frameSize)); - let page_base_end = (end - 1) & !MASK!(pageBitsForSize(resolve_ret.frameSize)); + let page_base_start = start & !MASK!(pageBitsForSize(resolve_ret.ptBitsLeft)); + let page_base_end = (end - 1) & !MASK!(pageBitsForSize(resolve_ret.ptBitsLeft)); if page_base_start != page_base_end { unsafe { current_syscall_error._type = seL4_RangeError; current_syscall_error.rangeErrorMin = start; current_syscall_error.rangeErrorMax = - page_base_start + MASK!(pageBitsForSize(resolve_ret.frameSize)); + page_base_start + MASK!(pageBitsForSize(resolve_ret.ptBitsLeft)); } return exception_t::EXCEPTION_SYSCALL_ERROR; } - let pstart = - resolve_ret.frameBase + start & MASK!(pageBitsForSize(resolve_ret.frameSize)); + let pstart = ptr_to_ref(pte).get_page_base_address() + start + & MASK!(pageBitsForSize(resolve_ret.ptBitsLeft)); get_currenct_thread().set_state(ThreadState::ThreadStateRestart); return decode_vspace_flush_invocation( label, @@ -838,206 +832,206 @@ fn decode_vspace_flush_invocation( exception_t::EXCEPTION_NONE } -fn decode_page_upper_directory_invocation( - label: MessageLabel, - length: usize, - cte: &mut cte_t, - buffer: &seL4_IPCBuffer, -) -> exception_t { - /* - lookupPGDSlot_ret_t pgdSlot; - findVSpaceForASID_ret_t find_ret; - if (invLabel == ARMPageUpperDirectoryUnmap) { - if (unlikely(!isFinalCapability(cte))) { - current_syscall_error.type = seL4_RevokeFirst; - return EXCEPTION_SYSCALL_ERROR; - } - setThreadState(NODE_STATE(ksCurThread), ThreadState_Restart); - return performUpperPageDirectoryInvocationUnmap(cap, cte); - } - */ - if label == MessageLabel::ARMPageUpperDirectoryUnmap { - // log::warn!("Need to check is FinalCapability here"); - if unlikely(!cte.is_final_cap()) { - global_ops!(current_syscall_error._type = seL4_RevokeFirst); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - get_currenct_thread().set_state(ThreadState::ThreadStateRestart); - // unimplemented!("performUpperPageDirectoryInvocationUnmap"); - return decode_upper_page_directory_unmap(cte); - } - - // Return SYSCALL_ERROR if message is not ARMPageUpperDirectoryUnmap - if unlikely(label != MessageLabel::ARMPageUpperDirectoryMap) { - global_ops!(current_syscall_error._type = seL4_IllegalOperation); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - if unlikely(length < 2 || unsafe { current_extra_caps.excaprefs[0] == 0 }) { - global_ops!(current_syscall_error._type = seL4_TruncatedMessage); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - if unlikely(cte.cap.get_pud_is_mapped() == 1) { - global_ops!(current_syscall_error._type = seL4_InvalidCapability); - global_ops!(current_syscall_error.invalidCapNumber = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - let vaddr = get_syscall_arg(0, buffer) & (!MASK!(PGD_INDEX_OFFSET)); - let pgd_cap = convert_to_mut_type_ref::(global_ops!(current_extra_caps.excaprefs[0])); - - if unlikely(!pgd_cap.is_valid_native_root()) { - global_ops!(current_syscall_error._type = seL4_InvalidCapability); - global_ops!(current_syscall_error.invalidCapNumber = 1); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - let pgd = pgd_cap.get_pgd_base_ptr(); - let asid = pgd_cap.get_pgd_mapped_asid(); - - if unlikely(vaddr > USER_TOP) { - global_ops!(current_syscall_error._type = seL4_InvalidArgument); - global_ops!(current_syscall_error.failedLookupWasSource = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - let find_ret = find_vspace_for_asid(asid); - - if unlikely(find_ret.status != exception_t::EXCEPTION_NONE) { - global_ops!(current_syscall_error._type = seL4_FailedLookup); - global_ops!(current_syscall_error.failedLookupWasSource = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - // vspace_root is Some(_) when Exception is NONE - if unlikely(find_ret.vspace_root.unwrap() as usize != pgd) { - global_ops!(current_syscall_error._type = seL4_InvalidCapability); - global_ops!(current_syscall_error.invalidCapNumber = 1); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - // Ensure that pgd is aligned 4K. - assert!(pgd & MASK!(PAGE_BITS) == 0); - - let pgd_slot = PGDE::new_from_pte(pgd).lookup_pgd_slot(vaddr); - - if unlikely(ptr_to_ref(pgd_slot.pgdSlot).get_present()) { - global_ops!(current_syscall_error._type = seL4_DeleteFirst); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - // TODO: make 0x3 in a pagetable-specific position - let pgde = PGDE::new_page(pptr_to_paddr(cte.cap.get_pud_base_ptr()), 0x3); - cte.cap.set_pud_is_mapped(1); - cte.cap.set_pud_mapped_asid(asid); - cte.cap.set_pud_mapped_address(vaddr); - - get_currenct_thread().set_state(ThreadState::ThreadStateRestart); - *ptr_to_mut(pgd_slot.pgdSlot) = pgde; - clean_by_va_pou( - convert_ref_type_to_usize(ptr_to_mut(pgd_slot.pgdSlot)), - pptr_to_paddr(convert_ref_type_to_usize(ptr_to_mut(pgd_slot.pgdSlot))), - ); - exception_t::EXCEPTION_NONE -} -fn decode_page_directory_invocation( - label: MessageLabel, - length: usize, - cte: &mut cte_t, - buffer: &seL4_IPCBuffer, -) -> exception_t { - /* - if (invLabel == ARMPageDirectoryUnmap) { - if (unlikely(!isFinalCapability(cte))) { - current_syscall_error.type = seL4_RevokeFirst; - return EXCEPTION_SYSCALL_ERROR; - } - setThreadState(NODE_STATE(ksCurThread), ThreadState_Restart); - return performPageDirectoryInvocationUnmap(cap, cte); - } - */ - // Call performPageDirectoryInvocationUnmap if message is unmap - if label == MessageLabel::ARMPageDirectoryUnmap { - // log::warn!("Need to check is FinalCapability here"); - if unlikely(!cte.is_final_cap()) { - global_ops!(current_syscall_error._type = seL4_RevokeFirst); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - get_currenct_thread().set_state(ThreadState::ThreadStateRestart); - // unimplemented!("performPageDirectoryInvocationUnmap"); - return decode_page_directory_unmap(cte); - } - - // Return SYSCALL_ERROR if message is not ARMPageDirectoryUnmap - if unlikely(label != MessageLabel::ARMPageDirectoryMap) { - global_ops!(current_syscall_error._type = seL4_IllegalOperation); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - if unlikely(length < 2 || global_ops!(current_extra_caps.excaprefs[0] == 0)) { - global_ops!(current_syscall_error._type = seL4_TruncatedMessage); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - if unlikely(cte.cap.get_pd_is_mapped() == 1) { - global_ops!(current_syscall_error._type = seL4_InvalidCapability); - global_ops!(current_syscall_error.invalidCapNumber = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - let vaddr = get_syscall_arg(0, buffer) & (!MASK!(PUD_INDEX_OFFSET)); - let vspace_root_cap = - convert_to_mut_type_ref::(global_ops!(current_extra_caps.excaprefs[0])); - - if unlikely(!vspace_root_cap.is_valid_native_root()) { - global_ops!(current_syscall_error._type = seL4_InvalidCapability); - global_ops!(current_syscall_error.invalidCapNumber = 1); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - let vspace_root = vspace_root_cap.get_pgd_base_ptr(); - let asid = vspace_root_cap.get_pgd_mapped_asid(); - - if unlikely(vaddr > USER_TOP) { - global_ops!(current_syscall_error._type = seL4_InvalidArgument); - global_ops!(current_syscall_error.failedLookupWasSource = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - let find_ret = find_vspace_for_asid(asid); - - if unlikely(find_ret.status != exception_t::EXCEPTION_NONE) { - global_ops!(current_syscall_error._type = seL4_FailedLookup); - global_ops!(current_syscall_error.failedLookupWasSource = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - if unlikely(find_ret.vspace_root.unwrap() as usize != vspace_root) { - global_ops!(current_syscall_error._type = seL4_InvalidCapability); - global_ops!(current_syscall_error.invalidCapNumber = 1); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - - let pud_slot = PGDE::new_from_pte(vspace_root).lookup_pud_slot(vaddr); - - if pud_slot.status != exception_t::EXCEPTION_NONE { - global_ops!(current_syscall_error._type = seL4_FailedLookup); - global_ops!(current_syscall_error.failedLookupWasSource = 0); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - if unlikely( - ptr_to_ref(pud_slot.pudSlot).get_present() || ptr_to_ref(pud_slot.pudSlot).is_1g_page(), - ) { - global_ops!(current_syscall_error._type = seL4_DeleteFirst); - return exception_t::EXCEPTION_SYSCALL_ERROR; - } - // TODO: make 0x3 in a pagetable-specific position - let pude = PUDE::new_page(pptr_to_paddr(cte.cap.get_pd_base_ptr()), 0x3); - cte.cap.set_pd_is_mapped(1); - cte.cap.set_pd_mapped_asid(asid); - cte.cap.set_pd_mapped_address(vaddr); - get_currenct_thread().set_state(ThreadState::ThreadStateRestart); - *ptr_to_mut(pud_slot.pudSlot) = pude; - // log::warn!("Need to clean D-Cache using cleanByVA_PoU"); - clean_by_va_pou( - convert_ref_type_to_usize(ptr_to_mut(pud_slot.pudSlot)), - pptr_to_paddr(convert_ref_type_to_usize(ptr_to_mut(pud_slot.pudSlot))), - ); - exception_t::EXCEPTION_NONE -} +// fn decode_page_upper_directory_invocation( +// label: MessageLabel, +// length: usize, +// cte: &mut cte_t, +// buffer: &seL4_IPCBuffer, +// ) -> exception_t { +// /* +// lookupPGDSlot_ret_t pgdSlot; +// findVSpaceForASID_ret_t find_ret; +// if (invLabel == ARMPageUpperDirectoryUnmap) { +// if (unlikely(!isFinalCapability(cte))) { +// current_syscall_error.type = seL4_RevokeFirst; +// return EXCEPTION_SYSCALL_ERROR; +// } +// setThreadState(NODE_STATE(ksCurThread), ThreadState_Restart); +// return performUpperPageDirectoryInvocationUnmap(cap, cte); +// } +// */ +// if label == MessageLabel::ARMPageUpperDirectoryUnmap { +// // log::warn!("Need to check is FinalCapability here"); +// if unlikely(!cte.is_final_cap()) { +// global_ops!(current_syscall_error._type = seL4_RevokeFirst); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// get_currenct_thread().set_state(ThreadState::ThreadStateRestart); +// // unimplemented!("performUpperPageDirectoryInvocationUnmap"); +// return decode_upper_page_directory_unmap(cte); +// } + +// // Return SYSCALL_ERROR if message is not ARMPageUpperDirectoryUnmap +// if unlikely(label != MessageLabel::ARMPageUpperDirectoryMap) { +// global_ops!(current_syscall_error._type = seL4_IllegalOperation); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// if unlikely(length < 2 || unsafe { current_extra_caps.excaprefs[0] == 0 }) { +// global_ops!(current_syscall_error._type = seL4_TruncatedMessage); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// if unlikely(cte.cap.get_pud_is_mapped() == 1) { +// global_ops!(current_syscall_error._type = seL4_InvalidCapability); +// global_ops!(current_syscall_error.invalidCapNumber = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// let vaddr = get_syscall_arg(0, buffer) & (!MASK!(PGD_INDEX_OFFSET)); +// let pgd_cap = convert_to_mut_type_ref::(global_ops!(current_extra_caps.excaprefs[0])); + +// if unlikely(!pgd_cap.is_valid_native_root()) { +// global_ops!(current_syscall_error._type = seL4_InvalidCapability); +// global_ops!(current_syscall_error.invalidCapNumber = 1); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// let pgd = pgd_cap.get_pgd_base_ptr(); +// let asid = pgd_cap.get_pgd_mapped_asid(); + +// if unlikely(vaddr > USER_TOP) { +// global_ops!(current_syscall_error._type = seL4_InvalidArgument); +// global_ops!(current_syscall_error.failedLookupWasSource = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// let find_ret = find_vspace_for_asid(asid); + +// if unlikely(find_ret.status != exception_t::EXCEPTION_NONE) { +// global_ops!(current_syscall_error._type = seL4_FailedLookup); +// global_ops!(current_syscall_error.failedLookupWasSource = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// // vspace_root is Some(_) when Exception is NONE +// if unlikely(find_ret.vspace_root.unwrap() as usize != pgd) { +// global_ops!(current_syscall_error._type = seL4_InvalidCapability); +// global_ops!(current_syscall_error.invalidCapNumber = 1); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// // Ensure that pgd is aligned 4K. +// assert!(pgd & MASK!(PAGE_BITS) == 0); + +// let pgd_slot = PGDE::new_from_pte(pgd).lookup_pgd_slot(vaddr); + +// if unlikely(ptr_to_ref(pgd_slot.pgdSlot).get_present()) { +// global_ops!(current_syscall_error._type = seL4_DeleteFirst); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// // TODO: make 0x3 in a pagetable-specific position +// let pgde = PGDE::new_page(pptr_to_paddr(cte.cap.get_pud_base_ptr()), 0x3); +// cte.cap.set_pud_is_mapped(1); +// cte.cap.set_pud_mapped_asid(asid); +// cte.cap.set_pud_mapped_address(vaddr); + +// get_currenct_thread().set_state(ThreadState::ThreadStateRestart); +// *ptr_to_mut(pgd_slot.pgdSlot) = pgde; +// clean_by_va_pou( +// convert_ref_type_to_usize(ptr_to_mut(pgd_slot.pgdSlot)), +// pptr_to_paddr(convert_ref_type_to_usize(ptr_to_mut(pgd_slot.pgdSlot))), +// ); +// exception_t::EXCEPTION_NONE +// } +// fn decode_page_directory_invocation( +// label: MessageLabel, +// length: usize, +// cte: &mut cte_t, +// buffer: &seL4_IPCBuffer, +// ) -> exception_t { +// /* +// if (invLabel == ARMPageDirectoryUnmap) { +// if (unlikely(!isFinalCapability(cte))) { +// current_syscall_error.type = seL4_RevokeFirst; +// return EXCEPTION_SYSCALL_ERROR; +// } +// setThreadState(NODE_STATE(ksCurThread), ThreadState_Restart); +// return performPageDirectoryInvocationUnmap(cap, cte); +// } +// */ +// // Call performPageDirectoryInvocationUnmap if message is unmap +// if label == MessageLabel::ARMPageDirectoryUnmap { +// // log::warn!("Need to check is FinalCapability here"); +// if unlikely(!cte.is_final_cap()) { +// global_ops!(current_syscall_error._type = seL4_RevokeFirst); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// get_currenct_thread().set_state(ThreadState::ThreadStateRestart); +// // unimplemented!("performPageDirectoryInvocationUnmap"); +// return decode_page_directory_unmap(cte); +// } + +// // Return SYSCALL_ERROR if message is not ARMPageDirectoryUnmap +// if unlikely(label != MessageLabel::ARMPageDirectoryMap) { +// global_ops!(current_syscall_error._type = seL4_IllegalOperation); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// if unlikely(length < 2 || global_ops!(current_extra_caps.excaprefs[0] == 0)) { +// global_ops!(current_syscall_error._type = seL4_TruncatedMessage); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// if unlikely(cte.cap.get_pd_is_mapped() == 1) { +// global_ops!(current_syscall_error._type = seL4_InvalidCapability); +// global_ops!(current_syscall_error.invalidCapNumber = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// let vaddr = get_syscall_arg(0, buffer) & (!MASK!(PUD_INDEX_OFFSET)); +// let vspace_root_cap = +// convert_to_mut_type_ref::(global_ops!(current_extra_caps.excaprefs[0])); + +// if unlikely(!vspace_root_cap.is_valid_native_root()) { +// global_ops!(current_syscall_error._type = seL4_InvalidCapability); +// global_ops!(current_syscall_error.invalidCapNumber = 1); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// let vspace_root = vspace_root_cap.get_pgd_base_ptr(); +// let asid = vspace_root_cap.get_pgd_mapped_asid(); + +// if unlikely(vaddr > USER_TOP) { +// global_ops!(current_syscall_error._type = seL4_InvalidArgument); +// global_ops!(current_syscall_error.failedLookupWasSource = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// let find_ret = find_vspace_for_asid(asid); + +// if unlikely(find_ret.status != exception_t::EXCEPTION_NONE) { +// global_ops!(current_syscall_error._type = seL4_FailedLookup); +// global_ops!(current_syscall_error.failedLookupWasSource = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// if unlikely(find_ret.vspace_root.unwrap() as usize != vspace_root) { +// global_ops!(current_syscall_error._type = seL4_InvalidCapability); +// global_ops!(current_syscall_error.invalidCapNumber = 1); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } + +// let pud_slot = PGDE::new_from_pte(vspace_root).lookup_pud_slot(vaddr); + +// if pud_slot.status != exception_t::EXCEPTION_NONE { +// global_ops!(current_syscall_error._type = seL4_FailedLookup); +// global_ops!(current_syscall_error.failedLookupWasSource = 0); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// if unlikely( +// ptr_to_ref(pud_slot.pudSlot).get_present() || ptr_to_ref(pud_slot.pudSlot).is_1g_page(), +// ) { +// global_ops!(current_syscall_error._type = seL4_DeleteFirst); +// return exception_t::EXCEPTION_SYSCALL_ERROR; +// } +// // TODO: make 0x3 in a pagetable-specific position +// let pude = PUDE::new_page(pptr_to_paddr(cte.cap.get_pd_base_ptr()), 0x3); +// cte.cap.set_pd_is_mapped(1); +// cte.cap.set_pd_mapped_asid(asid); +// cte.cap.set_pd_mapped_address(vaddr); +// get_currenct_thread().set_state(ThreadState::ThreadStateRestart); +// *ptr_to_mut(pud_slot.pudSlot) = pude; +// // log::warn!("Need to clean D-Cache using cleanByVA_PoU"); +// clean_by_va_pou( +// convert_ref_type_to_usize(ptr_to_mut(pud_slot.pudSlot)), +// pptr_to_paddr(convert_ref_type_to_usize(ptr_to_mut(pud_slot.pudSlot))), +// ); +// exception_t::EXCEPTION_NONE +// } pub(crate) fn check_irq(irq: usize) -> exception_t { if irq > maxIRQ { diff --git a/src/syscall/invocation/decode/arch/riscv64.rs b/src/syscall/invocation/decode/arch/riscv64.rs index 2624159..cfa6b57 100644 --- a/src/syscall/invocation/decode/arch/riscv64.rs +++ b/src/syscall/invocation/decode/arch/riscv64.rs @@ -108,11 +108,7 @@ fn decode_frame_invocation( } } -fn decode_asid_control( - label: MessageLabel, - length: usize, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_asid_control(label: MessageLabel, length: usize, buffer: &seL4_IPCBuffer) -> exception_t { if label != MessageLabel::RISCVASIDControlMakePool { unsafe { current_syscall_error._type = seL4_IllegalOperation; @@ -249,11 +245,7 @@ fn decode_asid_pool(label: MessageLabel, cte: &mut cte_t) -> exception_t { } } -fn decode_frame_map( - length: usize, - frame_slot: &mut cte_t, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_frame_map(length: usize, frame_slot: &mut cte_t, buffer: &seL4_IPCBuffer) -> exception_t { if length < 3 || get_extra_cap_by_index(0).is_none() { debug!("RISCVPageMap: Truncated message."); unsafe { diff --git a/src/syscall/invocation/decode/decode_tcb_invocation.rs b/src/syscall/invocation/decode/decode_tcb_invocation.rs index 0f0a24d..8d2fba9 100644 --- a/src/syscall/invocation/decode/decode_tcb_invocation.rs +++ b/src/syscall/invocation/decode/decode_tcb_invocation.rs @@ -157,11 +157,7 @@ fn decode_read_registers( invoke_tcb_read_registers(thread, flags & BIT!(ReadRegisters_suspend), n, 0, call) } -fn decode_write_registers( - cap: &cap_t, - length: usize, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_write_registers(cap: &cap_t, length: usize, buffer: &seL4_IPCBuffer) -> exception_t { if length < 2 { unsafe { debug!("TCB CopyRegisters: Truncated message."); @@ -196,11 +192,7 @@ fn decode_write_registers( invoke_tcb_write_registers(thread, flags & BIT!(0), w, 0, buffer) } -fn decode_copy_registers( - cap: &cap_t, - _length: usize, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_copy_registers(cap: &cap_t, _length: usize, buffer: &seL4_IPCBuffer) -> exception_t { let flags = get_syscall_arg(0, buffer); let source_cap = get_extra_cap_by_index(0).unwrap().cap; @@ -369,11 +361,7 @@ fn decode_set_priority(cap: &cap_t, length: usize, buffer: &seL4_IPCBuffer) -> e ) } -fn decode_set_mc_priority( - cap: &cap_t, - length: usize, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_set_mc_priority(cap: &cap_t, length: usize, buffer: &seL4_IPCBuffer) -> exception_t { if length < 1 || get_extra_cap_by_index(0).is_none() { debug!("TCB SetMCPPriority: Truncated message."); unsafe { @@ -405,11 +393,7 @@ fn decode_set_mc_priority( invoke_tcb_set_mcp(convert_to_mut_type_ref::(cap.get_tcb_ptr()), new_mcp) } -fn decode_set_sched_params( - cap: &cap_t, - length: usize, - buffer: &seL4_IPCBuffer, -) -> exception_t { +fn decode_set_sched_params(cap: &cap_t, length: usize, buffer: &seL4_IPCBuffer) -> exception_t { if length < 2 || get_extra_cap_by_index(0).is_some() { debug!("TCB SetSchedParams: Truncated message."); unsafe { diff --git a/src/syscall/invocation/invoke_mmu_op.rs b/src/syscall/invocation/invoke_mmu_op.rs index 18a4a56..428e157 100644 --- a/src/syscall/invocation/invoke_mmu_op.rs +++ b/src/syscall/invocation/invoke_mmu_op.rs @@ -1,8 +1,11 @@ #[cfg(target_arch = "aarch64")] -use core::arch::asm; +use core::intrinsics::unlikely; use sel4_common::arch::ArchReg; #[cfg(target_arch = "aarch64")] use sel4_common::BIT; + +#[cfg(target_arch = "aarch64")] +use sel4_common::utils::convert_ref_type_to_usize; #[cfg(target_arch = "riscv64")] use sel4_common::{ arch::maskVMRights, @@ -24,7 +27,7 @@ use sel4_vspace::{ PTEFlags, }; #[cfg(target_arch = "aarch64")] -use sel4_vspace::{invalidate_tlb_by_asid_va, PDE, PUDE}; +use sel4_vspace::{clean_by_va_pou, invalidate_tlb_by_asid_va, pte_tag_t}; use sel4_vspace::{pptr_to_paddr, unmapPage, unmap_page_table, PTE}; use crate::{kernel::boot::current_lookup_fault, utils::clear_memory}; @@ -54,29 +57,29 @@ pub fn invoke_page_table_map( sfence(); exception_t::EXCEPTION_NONE } -#[allow(unused)] -#[cfg(target_arch = "aarch64")] -pub fn invoke_page_table_map( - pt_cap: &mut cap_t, - pd_slot: &mut PDE, - asid: usize, - vaddr: usize, -) -> exception_t { - let paddr = pptr_to_paddr(pt_cap.get_pt_base_ptr()); - let pde = PDE::new_small(paddr >> seL4_PageBits); - *pd_slot = pde; - pt_cap.set_pt_is_mapped(1); - pt_cap.set_pt_mapped_asid(asid); - pt_cap.set_pt_mapped_address(vaddr); - unsafe { - asm!( - "dc cvau, {}", - "dmb sy", - in(reg) pd_slot, - ); - } - exception_t::EXCEPTION_NONE -} +// #[allow(unused)] +// #[cfg(target_arch = "aarch64")] +// pub fn invoke_page_table_map( +// pt_cap: &mut cap_t, +// pd_slot: &mut PDE, +// asid: usize, +// vaddr: usize, +// ) -> exception_t { +// let paddr = pptr_to_paddr(pt_cap.get_pt_base_ptr()); +// let pde = PDE::new_small(paddr >> seL4_PageBits); +// *pd_slot = pde; +// pt_cap.set_pt_is_mapped(1); +// pt_cap.set_pt_mapped_asid(asid); +// pt_cap.set_pt_mapped_address(vaddr); +// unsafe { +// asm!( +// "dc cvau, {}", +// "dmb sy", +// in(reg) pd_slot, +// ); +// } +// exception_t::EXCEPTION_NONE +// } pub fn invoke_page_get_address(vbase_ptr: usize, call: bool) -> exception_t { let thread = get_currenct_thread(); @@ -108,7 +111,7 @@ pub fn invoke_page_unmap(frame_slot: &mut cte_t) -> exception_t { } } frame_slot.cap.set_frame_mapped_address(0); - frame_slot.cap.set_pt_mapped_asid(asidInvalid); + frame_slot.cap.set_frame_mapped_asid(asidInvalid); exception_t::EXCEPTION_NONE } @@ -138,82 +141,104 @@ pub fn invoke_page_map( exception_t::EXCEPTION_NONE } #[cfg(target_arch = "aarch64")] -pub fn invoke_huge_page_map( - vaddr: usize, +pub fn invoke_page_map( asid: usize, + cap: cap_t, frame_slot: &mut cte_t, - pude: PUDE, - pudSlot: &mut PUDE, + pte: PTE, + pt_slot: &mut PTE, ) -> exception_t { - frame_slot.cap.set_frame_mapped_address(vaddr); - frame_slot.cap.set_frame_mapped_asid(asid); - *pudSlot = pude; - unsafe { - asm!( - "dc cvau, {}", - "dmb sy", - in(reg) pudSlot, - ); - } - let tlbflush_required = pudSlot.get_pude_type() == 1; - if tlbflush_required { - assert!(asid < BIT!(16)); - invalidate_tlb_by_asid_va(asid, vaddr); - } - exception_t::EXCEPTION_NONE -} + let tlbflush_required: bool = pt_slot.get_type() != (pte_tag_t::pte_invalid) as usize; + // frame_slot.cap = cap; + pt_slot.update(pte); -#[cfg(target_arch = "aarch64")] -pub fn invoke_large_page_map( - vaddr: usize, - asid: usize, - frame_slot: &mut cte_t, - pde: PDE, - pdSlot: &mut PDE, -) -> exception_t { - frame_slot.cap.set_frame_mapped_address(vaddr); - frame_slot.cap.set_frame_mapped_asid(asid); - *pdSlot = pde; - unsafe { - asm!( - "dc cvau, {}", - "dmb sy", - in(reg) pdSlot, - ); - } - let tlbflush_required = pdSlot.get_pde_type() == 1; - if tlbflush_required { + clean_by_va_pou( + convert_ref_type_to_usize(pt_slot), + pptr_to_paddr(convert_ref_type_to_usize(pt_slot)), + ); + if unlikely(tlbflush_required) { assert!(asid < BIT!(16)); - invalidate_tlb_by_asid_va(asid, vaddr); + invalidate_tlb_by_asid_va(asid, cap.get_frame_mapped_address()); } exception_t::EXCEPTION_NONE } +// #[cfg(target_arch = "aarch64")] +// pub fn invoke_huge_page_map( +// vaddr: usize, +// asid: usize, +// frame_slot: &mut cte_t, +// pude: PUDE, +// pudSlot: &mut PUDE, +// ) -> exception_t { +// frame_slot.cap.set_frame_mapped_address(vaddr); +// frame_slot.cap.set_frame_mapped_asid(asid); +// *pudSlot = pude; +// unsafe { +// asm!( +// "dc cvau, {}", +// "dmb sy", +// in(reg) pudSlot, +// ); +// } +// let tlbflush_required = pudSlot.get_pude_type() == 1; +// if tlbflush_required { +// assert!(asid < BIT!(16)); +// invalidate_tlb_by_asid_va(asid, vaddr); +// } +// exception_t::EXCEPTION_NONE +// } -#[cfg(target_arch = "aarch64")] -pub fn invoke_small_page_map( - vaddr: usize, - asid: usize, - frame_slot: &mut cte_t, - pte: PTE, - ptSlot: &mut PTE, -) -> exception_t { - frame_slot.cap.set_frame_mapped_address(vaddr); - frame_slot.cap.set_frame_mapped_asid(asid); - *ptSlot = pte; - unsafe { - asm!( - "dc cvau, {}", - "dmb sy", - in(reg) ptSlot, - ); - } - let tlbflush_required = ptSlot.is_present(); - if tlbflush_required { - assert!(asid < BIT!(16)); - invalidate_tlb_by_asid_va(asid, vaddr); - } - exception_t::EXCEPTION_NONE -} +// #[cfg(target_arch = "aarch64")] +// pub fn invoke_large_page_map( +// vaddr: usize, +// asid: usize, +// frame_slot: &mut cte_t, +// pde: PDE, +// pdSlot: &mut PDE, +// ) -> exception_t { +// frame_slot.cap.set_frame_mapped_address(vaddr); +// frame_slot.cap.set_frame_mapped_asid(asid); +// *pdSlot = pde; +// unsafe { +// asm!( +// "dc cvau, {}", +// "dmb sy", +// in(reg) pdSlot, +// ); +// } +// let tlbflush_required = pdSlot.get_pde_type() == 1; +// if tlbflush_required { +// assert!(asid < BIT!(16)); +// invalidate_tlb_by_asid_va(asid, vaddr); +// } +// exception_t::EXCEPTION_NONE +// } + +// #[cfg(target_arch = "aarch64")] +// pub fn invoke_small_page_map( +// vaddr: usize, +// asid: usize, +// frame_slot: &mut cte_t, +// pte: PTE, +// ptSlot: &mut PTE, +// ) -> exception_t { +// frame_slot.cap.set_frame_mapped_address(vaddr); +// frame_slot.cap.set_frame_mapped_asid(asid); +// *ptSlot = pte; +// unsafe { +// asm!( +// "dc cvau, {}", +// "dmb sy", +// in(reg) ptSlot, +// ); +// } +// let tlbflush_required = ptSlot.is_present(); +// if tlbflush_required { +// assert!(asid < BIT!(16)); +// invalidate_tlb_by_asid_va(asid, vaddr); +// } +// exception_t::EXCEPTION_NONE +// } #[cfg(target_arch = "riscv64")] pub fn invoke_asid_control( diff --git a/src/syscall/invocation/mod.rs b/src/syscall/invocation/mod.rs index 8d5f7b4..56a4859 100644 --- a/src/syscall/invocation/mod.rs +++ b/src/syscall/invocation/mod.rs @@ -1,10 +1,10 @@ +pub(crate) mod arch; pub mod decode; mod invoke_cnode; pub mod invoke_irq; mod invoke_mmu_op; mod invoke_tcb; mod invoke_untyped; -pub(crate) mod arch; use core::intrinsics::unlikely; diff --git a/src/utils.rs b/src/utils.rs index 8996159..7f7d74a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -46,20 +46,20 @@ pub fn clear_memory(ptr: *mut u8, bits: usize) { // addrFromPPtr(ptr)); // } -#[cfg(target_arch = "aarch64")] -#[inline] -pub fn clear_memory_pt(ptr: *mut u8, bits: usize) { - use sel4_vspace::{clean_cache_range_pou, pptr_to_paddr}; +// #[cfg(target_arch = "aarch64")] +// #[inline] +// pub fn clear_memory_pt(ptr: *mut u8, bits: usize) { +// use sel4_vspace::{clean_cache_range_pou, pptr_to_paddr}; - unsafe { - core::slice::from_raw_parts_mut(ptr, BIT!(bits)).fill(0); - clean_cache_range_pou( - ptr as usize, - ptr.add(BIT!(bits) - 1) as usize, - pptr_to_paddr(ptr as usize), - ); - } -} +// unsafe { +// core::slice::from_raw_parts_mut(ptr, BIT!(bits)).fill(0); +// clean_cache_range_pou( +// ptr as usize, +// ptr.add(BIT!(bits) - 1) as usize, +// pptr_to_paddr(ptr as usize), +// ); +// } +// } #[inline] #[cfg(target_arch = "aarch64")]