Skip to content

Commit

Permalink
Clean ffi 1 (#8)
Browse files Browse the repository at this point in the history
* clean the tcb debug

* add the map_kernel_devices of aarch64,but need fix the riscv64

* clean warnings and fmt

* add the riscv64 map_kernel_devices
  • Loading branch information
ZhiyuanSue committed Aug 28, 2024
1 parent 86b93d6 commit 2f69158
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/arch/aarch64/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use sel4_common::{
config::{PADDR_BASE, PADDR_TOP, PPTR_BASE, PPTR_TOP},
vm_rights_t,
},
ffi_call,
sel4_config::{seL4_LargePageBits, ARM_Large_Page, ARM_Small_Page, PUD_INDEX_BITS},
utils::convert_to_mut_type_ref,
BIT,
Expand All @@ -18,7 +17,7 @@ use crate::{
vptr_t, GET_KPT_INDEX, GET_PD_INDEX, GET_PT_INDEX, GET_PUD_INDEX, PDE, PGDE, PTE, PUDE,
};

use super::page_slice;
use super::{map_kernel_devices, page_slice};

#[derive(PartialEq, Eq, Debug)]
enum find_type {
Expand Down Expand Up @@ -124,8 +123,8 @@ pub fn rust_map_kernel_window() {
BIT!(PUD_INDEX_BITS) - 1,
PDE::new_small(kpptr_to_paddr(get_kernel_page_table_base())),
);

ffi_call!(map_kernel_devices());
map_kernel_devices();
// ffi_call!(map_kernel_devices());
}

#[no_mangle]
Expand Down
70 changes: 70 additions & 0 deletions src/arch/aarch64/device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use super::boot::map_kernel_frame;
use crate::{paddr_t, pptr_t, vm_attributes_t};
use sel4_common::arch::vm_rights_t::VMKernelOnly;
use sel4_common::{sel4_config::PAGE_BITS, BIT};

pub const KDEV_BASE: usize = 0xFFFFFFFFC0000000;
pub(crate) const NUM_KERNEL_DEVICE_FRAMES: usize = 3;
pub(crate) const UART_PPTR: usize = KDEV_BASE + 0x0;
pub(crate) const GIC_V2_DISTRIBUTOR_PPTR: usize = KDEV_BASE + 0x1000;
pub(crate) const GIC_V2_CONTROLLER_PPTR: usize = KDEV_BASE + 0x2000;
#[derive(Copy, Clone)]
struct kernel_frame_t {
paddr: paddr_t,
pptr: pptr_t,
armExecuteNever: isize,
userAvailable: isize,
}

#[derive(Copy, Clone)]
#[repr(C)]
struct p_region_t {
pub start: usize,
pub end: usize,
}
extern "C" {
pub(self) fn reserve_region(reg: p_region_t) -> bool;
}

#[no_mangle]
#[link_section = ".boot.text"]
pub(self) static mut kernel_device_frames: [kernel_frame_t; NUM_KERNEL_DEVICE_FRAMES] = [
kernel_frame_t {
paddr: paddr_t(0x9000000),
pptr: UART_PPTR,
armExecuteNever: 1,
userAvailable: 1,
},
kernel_frame_t {
paddr: paddr_t(0x8000000),
pptr: GIC_V2_DISTRIBUTOR_PPTR,
armExecuteNever: 1,
userAvailable: 0,
},
kernel_frame_t {
paddr: paddr_t(0x8010000),
pptr: GIC_V2_CONTROLLER_PPTR,
armExecuteNever: 1,
userAvailable: 0,
},
];
#[no_mangle]
pub fn map_kernel_devices() {
unsafe {
for kernel_frame in kernel_device_frames {
let vm_attr: vm_attributes_t = vm_attributes_t(kernel_frame.armExecuteNever as usize);
map_kernel_frame(
kernel_frame.paddr.0,
kernel_frame.pptr,
VMKernelOnly,
vm_attr,
);
if kernel_frame.userAvailable == 0 {
reserve_region(p_region_t {
start: kernel_frame.paddr.0,
end: kernel_frame.paddr.0 + BIT!(PAGE_BITS),
});
}
}
}
}
2 changes: 2 additions & 0 deletions src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod asid;
mod boot;
mod device;
mod interface;
mod machine;
mod pagetable;
Expand All @@ -8,6 +9,7 @@ mod structures;
mod utils;
pub use asid::*;
pub use boot::*;
pub use device::*;
pub use interface::*;
pub use machine::*;
pub use pagetable::create_it_pud_cap;
Expand Down
20 changes: 19 additions & 1 deletion src/arch/riscv64/boot.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
use super::{device::KDEV_BASE, utils::RISCV_GET_LVL_PGSIZE_BITS};
use crate::arch::riscv64::pagetable::{KERNEL_LEVEL2_PAGE_TABLE, KERNEL_ROOT_PAGE_TABLE};
use crate::{pptr_t, pptr_to_paddr, sfence, PTEFlags, PTE, RISCV_GET_PT_INDEX};
use sel4_common::{
arch::vm_rights_t,
sel4_config::{seL4_PageBits, RISCVMegaPageBits, RISCVPageBits},
utils::convert_to_mut_type_ref,
ROUND_DOWN,
};
use sel4_cspace::arch::cap_t;

use crate::{pptr_t, pptr_to_paddr, sfence, PTEFlags, PTE};
#[no_mangle]
#[link_section = ".boot.text"]
pub fn map_kernel_frame(paddr: usize, vaddr: usize, _vm_rights: vm_rights_t) {
if vaddr >= KDEV_BASE {
let paddr = ROUND_DOWN!(paddr, RISCV_GET_LVL_PGSIZE_BITS(1));
unsafe {
KERNEL_LEVEL2_PAGE_TABLE.map_next_table(RISCV_GET_PT_INDEX(vaddr, 0), paddr, true);
}
} else {
let paddr = ROUND_DOWN!(paddr, RISCV_GET_LVL_PGSIZE_BITS(0));
unsafe {
KERNEL_ROOT_PAGE_TABLE.map_next_table(RISCV_GET_PT_INDEX(vaddr, 0), paddr, true);
}
}
}

#[no_mangle]
#[link_section = ".boot.text"]
Expand Down
41 changes: 41 additions & 0 deletions src/arch/riscv64/device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use super::boot::map_kernel_frame;
use crate::{paddr_t, pptr_t};
use sel4_common::arch::vm_rights_t::VMKernelOnly;
use sel4_common::{sel4_config::PAGE_BITS, BIT};

pub const KDEV_BASE: usize = 0xFFFFFFFFC0000000;
pub(crate) const NUM_KERNEL_DEVICE_FRAMES: usize = 0;
#[derive(Copy, Clone)]
struct kernel_frame_t {
paddr: paddr_t,
pptr: pptr_t,
userAvailable: isize,
}

#[derive(Copy, Clone)]
#[repr(C)]
struct p_region_t {
pub start: usize,
pub end: usize,
}
extern "C" {
pub(self) fn reserve_region(reg: p_region_t) -> bool;
}

#[no_mangle]
#[link_section = ".boot.text"]
pub(self) static mut kernel_device_frames: [kernel_frame_t; NUM_KERNEL_DEVICE_FRAMES] = [];
#[no_mangle]
pub fn map_kernel_devices() {
unsafe {
for kernel_frame in kernel_device_frames {
map_kernel_frame(kernel_frame.paddr.0, kernel_frame.pptr, VMKernelOnly);
if kernel_frame.userAvailable == 0 {
reserve_region(p_region_t {
start: kernel_frame.paddr.0,
end: kernel_frame.paddr.0 + BIT!(PAGE_BITS),
});
}
}
}
}
2 changes: 2 additions & 0 deletions src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod asid;
mod boot;
mod device;
mod interface;
mod pagetable;
mod pte;
Expand All @@ -8,6 +9,7 @@ mod structures;
mod utils;
pub use asid::*;
pub use boot::*;
pub use device::*;
pub use interface::{set_vm_root, unmap_page_table};
pub use pagetable::{
activate_kernel_vspace, copyGlobalMappings, rust_map_kernel_window, unmapPage,
Expand Down
3 changes: 2 additions & 1 deletion src/arch/riscv64/pagetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use sel4_common::{
};

use super::{
kpptr_to_paddr, setVSpaceRoot,
kpptr_to_paddr, map_kernel_devices, setVSpaceRoot,
utils::{RISCV_GET_LVL_PGSIZE_BITS, RISCV_GET_PT_INDEX},
RISCV_GET_LVL_PGSIZE,
};
Expand Down Expand Up @@ -127,6 +127,7 @@ pub fn rust_map_kernel_window() {
paddr += RISCV_GET_LVL_PGSIZE(1);
index += 1;
}
map_kernel_devices();
}

/// 激活内核页表,将`satp`的值设置为内核页表根页表地址
Expand Down

0 comments on commit 2f69158

Please sign in to comment.