Skip to content

Commit

Permalink
optimize some code
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock committed Feb 21, 2024
1 parent ad83c8a commit 9c7691c
Show file tree
Hide file tree
Showing 20 changed files with 37 additions and 1,302 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 编译的目标平台
[build]
# target = 'riscv64imac-unknown-none-elf'
target = 'x86_64-unknown-none'
target = 'riscv64imac-unknown-none-elf'
# target = 'x86_64-unknown-none'

# This flags also can be set from every target.
rustflags = [
Expand Down
38 changes: 2 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ members = [
"crates/timestamp",
"crates/vfscore",
"crates/cv1811-sd",
"crates/page_table",
"crates/page_table_entry",
"crates/memory_addr",
"crates/crate_interface",

Expand Down
15 changes: 15 additions & 0 deletions arch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,18 @@ pub fn clear_bss() {
.fill(0);
}
}

bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MappingFlags: u64 {
const None = 0;
const U = 1 << 0;
const R = 1 << 1;
const W = 1 << 2;
const X = 1 << 3;
const A = 1 << 4;
const D = 1 << 5;
const Device = 1 << 6;
const Cache = 1 << 7;
}
}
20 changes: 8 additions & 12 deletions arch/src/riscv64/page_table/sv39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ pub fn get_pte_list(paddr: PhysAddr) -> &'static mut [PTE] {
unsafe { core::slice::from_raw_parts_mut(paddr.get_mut_ptr::<PTE>(), PAGE_ITEM_COUNT) }
}

fn destory_pte_leaf(paddr: PhysAddr) {
let pte_list = get_pte_list(paddr);
for pte in pte_list {
if pte.is_leaf() {
destory_pte_leaf(pte.to_ppn().into());
ArchInterface::frame_unalloc(pte.to_ppn());
}
}
ArchInterface::frame_unalloc(paddr.into());
}

#[derive(Debug)]
pub struct PageTable(pub(crate) PhysAddr);

Expand Down Expand Up @@ -244,6 +233,13 @@ impl PageTable {

impl Drop for PageTable {
fn drop(&mut self) {
destory_pte_leaf(self.0);
for root_pte in get_pte_list(self.0)[..0x100].iter().filter(|x| x.is_leaf()) {
get_pte_list(root_pte.to_ppn().into())
.iter()
.filter(|x| x.is_leaf())
.for_each(|x| ArchInterface::frame_unalloc(x.to_ppn()));
ArchInterface::frame_unalloc(root_pte.to_ppn());
}
ArchInterface::frame_unalloc(self.0.into());
}
}
31 changes: 0 additions & 31 deletions arch/src/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,6 @@ use x86_64::instructions::port::PortWriteOnly;

use crate::x86_64::multiboot::use_multiboot;

#[link_section = ".data.prepage.entry"]
static KERNEL_PDPT: PDPT = {
let mut arr: PDPT = [PDPTEntry(0); PAGE_SIZE_ENTRIES];
// 0x00000000_80000000 -> 0x80000000 (1G)
// arr[0] = PDPTEntry::new(PAddr(0x0), PDPTFlags::P | PDPTFlags::RW | PDPTFlags::PS);
// arr[1] = PDPTEntry::new(PAddr(0x40000000), PDPTFlags::P | PDPTFlags::RW | PDPTFlags::PS);
// arr[2] = PDPTEntry::new(PAddr(0x80000000), PDPTFlags::P | PDPTFlags::RW | PDPTFlags::PS);
// arr[3] = PDPTEntry::new(PAddr(0xc0000000), PDPTFlags::P | PDPTFlags::RW | PDPTFlags::PS);
arr[0] = PDPTEntry(0x0 | 0x83);
arr[1] = PDPTEntry(0x40000000 | 0x83);
arr[2] = PDPTEntry(0x80000000 | 0x83);
arr[3] = PDPTEntry(0xc0000000 | 0x83);
arr
};

// #[link_section = ".data.prepage.entry"]
// static PAGE_TABLE: PML4 = {
// let mut arr: PML4 = [PML4Entry(0); PAGE_SIZE_ENTRIES];

// // arr[2] = PTE::from_addr(0x8000_0000, PTEFlags::ADVRWX);
// // arr[0x100] = PTE::from_addr(0x0000_0000, PTEFlags::ADGVRWX);
// // arr[0x101] = PTE::from_addr(0x4000_0000, PTEFlags::ADGVRWX);
// // arr[0x102] = PTE::from_addr(0x8000_0000, PTEFlags::ADGVRWX);
// // arr[0x106] = PTE::from_addr(0x8000_0000, PTEFlags::ADVRWX);
// // arr[0] = PML4Entry::new(PAddr(KERNEL_PDPT.as_ptr() as u64 - VIRT_ADDR_START as u64), PML4Flags::P | PML4Flags::RW);
// let ptr = &KERNEL_PDPT as *const [PDPTEntry; PAGE_SIZE_ENTRIES] as *const PDPTEntry;
// let paddr: u64 = unsafe { transmute(ptr.sub(VIRT_ADDR_START)) };
// arr[0] = PML4Entry(paddr | 3);
// arr
// };

pub fn shutdown() -> ! {
unsafe { PortWriteOnly::new(0x604).write(0x2000u16) };

Expand Down
27 changes: 10 additions & 17 deletions arch/src/x86_64/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{ArchInterface, PhysAddr, PhysPage, VirtAddr, VirtPage, PAGE_ITEM_COU

use super::sigtrx::get_trx_mapping;


#[derive(Copy, Clone, Debug)]
pub struct PTE(usize);
impl PTE {
Expand Down Expand Up @@ -122,17 +121,6 @@ pub fn get_pte_list(paddr: PhysAddr) -> &'static mut [PTE] {
unsafe { core::slice::from_raw_parts_mut(paddr.get_mut_ptr::<PTE>(), PAGE_ITEM_COUNT) }
}

fn destory_pte_leaf(paddr: PhysAddr) {
let pte_list = get_pte_list(paddr);
for pte in pte_list {
if pte.is_leaf() {
destory_pte_leaf(pte.to_ppn().into());
ArchInterface::frame_unalloc(pte.to_ppn());
}
}
ArchInterface::frame_unalloc(paddr.into());
}

#[derive(Debug)]
pub struct PageTable(pub(crate) PhysAddr);

Expand Down Expand Up @@ -169,8 +157,7 @@ impl PageTable {
}

#[inline]
pub fn map(&self, ppn: PhysPage, vpn: VirtPage, flags: PTEFlags, level: usize)
{
pub fn map(&self, ppn: PhysPage, vpn: VirtPage, flags: PTEFlags, level: usize) {
// TODO: Add huge page support.
let mut pte_list = get_pte_list(self.0);
for i in (1..level).rev() {
Expand Down Expand Up @@ -225,9 +212,15 @@ impl PageTable {

impl Drop for PageTable {
fn drop(&mut self) {
destory_pte_leaf(self.0);
for root_pte in get_pte_list(self.0)[..0x100].iter().filter(|x| x.is_leaf()) {
get_pte_list(root_pte.to_ppn().into())
.iter()
.filter(|x| x.is_leaf())
.for_each(|x| ArchInterface::frame_unalloc(x.to_ppn()));
ArchInterface::frame_unalloc(root_pte.to_ppn());
}
ArchInterface::frame_unalloc(self.0.into());
}
}

pub fn switch_to_kernel_page_table() {
}
pub fn switch_to_kernel_page_table() {}
15 changes: 0 additions & 15 deletions crates/page_table/Cargo.toml

This file was deleted.

22 changes: 0 additions & 22 deletions crates/page_table/src/arch/aarch64.rs

This file was deleted.

8 changes: 0 additions & 8 deletions crates/page_table/src/arch/mod.rs

This file was deleted.

30 changes: 0 additions & 30 deletions crates/page_table/src/arch/riscv.rs

This file was deleted.

16 changes: 0 additions & 16 deletions crates/page_table/src/arch/x86_64.rs

This file was deleted.

Loading

0 comments on commit 9c7691c

Please sign in to comment.