From d56a88947e0d3fb7531dcca572d835a2862bd690 Mon Sep 17 00:00:00 2001 From: equation314 Date: Wed, 28 Aug 2019 01:41:47 +0800 Subject: [PATCH] aarch64: set ttbr0_el1 to null for kernel thread, fixed #56 --- bootloader/Cargo.lock | 8 ++++---- bootloader/Cargo.toml | 2 +- bootloader/src/arch/aarch64/mod.rs | 8 ++++---- kernel/Cargo.lock | 10 +++++----- kernel/Cargo.toml | 2 +- kernel/src/arch/aarch64/memory.rs | 2 ++ kernel/src/arch/aarch64/paging.rs | 11 +++++++---- 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/bootloader/Cargo.lock b/bootloader/Cargo.lock index ff6c78fe..688f4c83 100644 --- a/bootloader/Cargo.lock +++ b/bootloader/Cargo.lock @@ -2,8 +2,8 @@ # It is not intended for manual editing. [[package]] name = "aarch64" -version = "2.6.1" -source = "git+https://github.com/rcore-os/aarch64#65d1453f11f3cc113247352dffa02d8dcdd34769" +version = "2.7.0" +source = "git+https://github.com/rcore-os/aarch64#b2806c9f0fe0120a838cf8afdec8c4899241ef7e" dependencies = [ "bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -104,7 +104,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "rcore-bootloader" version = "0.1.0" dependencies = [ - "aarch64 2.6.1 (git+https://github.com/rcore-os/aarch64)", + "aarch64 2.7.0 (git+https://github.com/rcore-os/aarch64)", "bcm2837 1.0.0 (git+https://github.com/rcore-os/bcm2837)", "cc 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)", "fixedvec 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -211,7 +211,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum aarch64 2.6.1 (git+https://github.com/rcore-os/aarch64)" = "" +"checksum aarch64 2.7.0 (git+https://github.com/rcore-os/aarch64)" = "" "checksum bcm2837 1.0.0 (git+https://github.com/rcore-os/bcm2837)" = "" "checksum bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" diff --git a/bootloader/Cargo.toml b/bootloader/Cargo.toml index cacae222..be48b7ef 100644 --- a/bootloader/Cargo.toml +++ b/bootloader/Cargo.toml @@ -9,7 +9,7 @@ xmas-elf = "0.6.2" fixedvec = "0.2.3" [target.'cfg(target_arch = "aarch64")'.dependencies] -aarch64 = { git = "https://github.com/rcore-os/aarch64", version = "2.6.1" } +aarch64 = { git = "https://github.com/rcore-os/aarch64", version = "2.7.0" } bcm2837 = { git = "https://github.com/rcore-os/bcm2837", version = "1.0.0" } [build-dependencies] diff --git a/bootloader/src/arch/aarch64/mod.rs b/bootloader/src/arch/aarch64/mod.rs index 5a70c326..b250e477 100644 --- a/bootloader/src/arch/aarch64/mod.rs +++ b/bootloader/src/arch/aarch64/mod.rs @@ -1,5 +1,5 @@ use aarch64::addr::{PhysAddr, VirtAddr}; -use aarch64::paging::{memory_attribute::*, Page, PageTable, PageTableFlags as EF, PhysFrame}; +use aarch64::paging::{memory_attribute::*, Page, PageTable, PageTableAttribute, PageTableFlags as EF, PhysFrame}; use aarch64::paging::{Size1GiB, Size2MiB, Size4KiB}; use aarch64::{asm::*, barrier, regs::*}; use bcm2837::consts::RAW_IO_BASE; @@ -42,7 +42,7 @@ fn create_page_table(start_paddr: usize, end_paddr: usize) { p3.zero(); p2.zero(); - let block_flags = EF::VALID | EF::AF | EF::WRITE | EF::UXN; + let block_flags = EF::default_block() | EF::UXN; // normal memory for frame in PhysFrame::::range_of(start_paddr as u64, end_paddr as u64) { let paddr = frame.start_address(); @@ -58,10 +58,10 @@ fn create_page_table(start_paddr: usize, end_paddr: usize) { p2[page.p2_index()].set_block::(paddr, block_flags | EF::PXN, MairDevice::attr_value()); } - p3[0].set_frame(frame_lvl2, EF::default(), MairNormal::attr_value()); + p3[0].set_frame(frame_lvl2, EF::default_table(), PageTableAttribute::new(0, 0, 0)); p3[1].set_block::(PhysAddr::new(0x4000_0000), block_flags | EF::PXN, MairDevice::attr_value()); - p4[0].set_frame(frame_lvl3, EF::default(), MairNormal::attr_value()); + p4[0].set_frame(frame_lvl3, EF::default_table(), PageTableAttribute::new(0, 0, 0)); // the bootloader is still running at the lower virtual address range, // so the TTBR0_EL1 also needs to be set. diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock index 5ecee2ae..01bea7d0 100644 --- a/kernel/Cargo.lock +++ b/kernel/Cargo.lock @@ -2,8 +2,8 @@ # It is not intended for manual editing. [[package]] name = "aarch64" -version = "2.6.1" -source = "git+https://github.com/rcore-os/aarch64#65d1453f11f3cc113247352dffa02d8dcdd34769" +version = "2.7.0" +source = "git+https://github.com/rcore-os/aarch64#b2806c9f0fe0120a838cf8afdec8c4899241ef7e" dependencies = [ "bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -68,7 +68,7 @@ name = "bcm2837" version = "1.0.0" source = "git+https://github.com/rcore-os/bcm2837#bf1e4ec63c52827bc199399f214f39a4fdcbe228" dependencies = [ - "aarch64 2.6.1 (git+https://github.com/rcore-os/aarch64)", + "aarch64 2.7.0 (git+https://github.com/rcore-os/aarch64)", "volatile 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -380,7 +380,7 @@ dependencies = [ name = "rcore" version = "0.2.0" dependencies = [ - "aarch64 2.6.1 (git+https://github.com/rcore-os/aarch64)", + "aarch64 2.7.0 (git+https://github.com/rcore-os/aarch64)", "acpi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "aml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "apic 0.1.0 (git+https://github.com/rcore-os/apic-rs)", @@ -751,7 +751,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum aarch64 2.6.1 (git+https://github.com/rcore-os/aarch64)" = "" +"checksum aarch64 2.7.0 (git+https://github.com/rcore-os/aarch64)" = "" "checksum acpi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2c18d706bdc322dd4f8f7930a5879ad8df3d78d4452a678d5419c72f9f69acea" "checksum aml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b7669e841017880c2710777c46ec654272163379bbe55de6e17a2a2388d44d92" "checksum apic 0.1.0 (git+https://github.com/rcore-os/apic-rs)" = "" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index f5e5c23f..24d2c34b 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -86,7 +86,7 @@ aml = "0.4" riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } [target.'cfg(target_arch = "aarch64")'.dependencies] -aarch64 = { git = "https://github.com/rcore-os/aarch64", version = "2.6.1" } +aarch64 = { git = "https://github.com/rcore-os/aarch64", version = "2.7.0" } bcm2837 = { git = "https://github.com/rcore-os/bcm2837", version = "1.0.0", optional = true } [target.'cfg(target_arch = "mips")'.dependencies] diff --git a/kernel/src/arch/aarch64/memory.rs b/kernel/src/arch/aarch64/memory.rs index 11013404..1fd9c067 100644 --- a/kernel/src/arch/aarch64/memory.rs +++ b/kernel/src/arch/aarch64/memory.rs @@ -2,11 +2,13 @@ use crate::consts::MEMORY_OFFSET; use crate::memory::{init_heap, virt_to_phys, FRAME_ALLOCATOR}; +use aarch64::{paging::frame::PhysFrame, asm::ttbr_el1_write}; use log::*; use rcore_memory::PAGE_SIZE; /// Memory initialization. pub fn init() { + ttbr_el1_write(0, PhysFrame::of_addr(0)); init_frame_allocator(); init_heap(); info!("memory: init end"); diff --git a/kernel/src/arch/aarch64/paging.rs b/kernel/src/arch/aarch64/paging.rs index f10a8840..b956fad9 100644 --- a/kernel/src/arch/aarch64/paging.rs +++ b/kernel/src/arch/aarch64/paging.rs @@ -9,7 +9,7 @@ use aarch64::paging::{ page_table::{PageTable as Aarch64PageTable, PageTableEntry, PageTableFlags as EF}, FrameAllocator, FrameDeallocator, Page as PageAllSizes, Size4KiB, }; -use aarch64::{PhysAddr, VirtAddr}; +use aarch64::PhysAddr; use core::mem::ManuallyDrop; use log::*; use rcore_memory::paging::*; @@ -26,7 +26,7 @@ pub struct PageEntry(&'static mut PageTableEntry, Page); impl PageTable for PageTableImpl { fn map(&mut self, addr: usize, target: usize) -> &mut dyn Entry { - let flags = EF::default(); + let flags = EF::default_page() | EF::PXN | EF::UXN; let attr = MairNormal::attr_value(); unsafe { self.page_table @@ -85,6 +85,7 @@ pub enum MMIOType { Unsupported = 3, } +// TODO: software dirty bit needs to be reconsidered impl Entry for PageEntry { fn update(&mut self) { tlb_invalidate(self.1.start_address()); @@ -161,9 +162,11 @@ impl Entry for PageEntry { } fn set_execute(&mut self, value: bool) { if self.user() { - self.as_flags().set(EF::UXN, !value) + self.as_flags().set(EF::UXN, !value); + self.as_flags().set(EF::PXN, true); } else { - self.as_flags().set(EF::PXN, !value) + self.as_flags().set(EF::PXN, !value); + self.as_flags().set(EF::UXN, true) } } fn mmio(&self) -> u8 {