mirror of
https://github.com/iceHtwoO/novaOS.git
synced 2026-06-01 08:29:45 +00:00
Compare commits
2 Commits
master
...
refactor_mmu
| Author | SHA1 | Date | |
|---|---|---|---|
| 778b3ed80c | |||
| cba7073ae5 |
@@ -4,14 +4,15 @@ SECTIONS {
|
||||
.text ALIGN(4) : {
|
||||
KEEP(*(.text._start))
|
||||
*(.text .text.*)
|
||||
. = ALIGN(4K);
|
||||
__text_end = .;
|
||||
}
|
||||
|
||||
.rodata : {
|
||||
*(.rodata .rodata.*)
|
||||
}
|
||||
|
||||
.data ALIGN(2M) : {
|
||||
_data = .;
|
||||
.data : {
|
||||
*(.data .data.*)
|
||||
}
|
||||
|
||||
@@ -21,6 +22,10 @@ SECTIONS {
|
||||
__bss_end = .;
|
||||
}
|
||||
|
||||
. = ALIGN(2M);
|
||||
|
||||
__share_end = .;
|
||||
|
||||
.vector_table ALIGN(2K) : {
|
||||
KEEP(*(.vector_table))
|
||||
}
|
||||
@@ -36,15 +41,6 @@ SECTIONS {
|
||||
. = ALIGN(2M);
|
||||
|
||||
__kernel_end = .;
|
||||
|
||||
.stack_el0 : {
|
||||
__stack_start_el0 = .;
|
||||
. += 10K; #10kB stack
|
||||
__stack_end_el0 = .;
|
||||
}
|
||||
|
||||
. = ALIGN(2M);
|
||||
_end = .;
|
||||
}
|
||||
|
||||
__bss_size = (__bss_end - __bss_start) >> 3;
|
||||
|
||||
+80
-222
@@ -1,16 +1,12 @@
|
||||
use core::panic;
|
||||
|
||||
use core::mem::size_of;
|
||||
use nova_error::NovaError;
|
||||
|
||||
use crate::get_current_el;
|
||||
|
||||
unsafe extern "C" {
|
||||
static mut __translation_table_l2_start: u64;
|
||||
static __stack_start_el0: u64;
|
||||
static __kernel_end: u64;
|
||||
static _data: u64;
|
||||
}
|
||||
use crate::{
|
||||
aarch64::mmu::physical_mapping::{
|
||||
reserve_block, reserve_block_explicit, reserve_page, reserve_page_explicit,
|
||||
},
|
||||
get_current_el,
|
||||
};
|
||||
|
||||
const BLOCK: u64 = 0b01;
|
||||
const TABLE: u64 = 0b11;
|
||||
@@ -51,6 +47,16 @@ pub const KERNEL_VIRTUAL_MEM_SPACE: usize = 0xFFFF_FF80_0000_0000;
|
||||
|
||||
pub const STACK_START_ADDR: usize = !KERNEL_VIRTUAL_MEM_SPACE & (!0xF);
|
||||
|
||||
mod physical_mapping;
|
||||
|
||||
type VirtAddr = usize;
|
||||
type PhysAddr = usize;
|
||||
|
||||
pub enum PhysSource {
|
||||
Any,
|
||||
Explicit(PhysAddr),
|
||||
}
|
||||
|
||||
#[repr(align(4096))]
|
||||
pub struct PageTable([u64; TABLE_ENTRY_COUNT]);
|
||||
|
||||
@@ -59,122 +65,85 @@ pub static mut TRANSLATIONTABLE_TTBR0: PageTable = PageTable([0; 512]);
|
||||
#[no_mangle]
|
||||
pub static mut TRANSLATIONTABLE_TTBR1: PageTable = PageTable([0; 512]);
|
||||
|
||||
static mut PAGING_BITMAP: [u64; MAX_PAGE_COUNT / 64] = [0; MAX_PAGE_COUNT / 64];
|
||||
|
||||
/// Allocate a memory block of `size` starting at `virtual_address`.
|
||||
pub fn allocate_memory(
|
||||
mut virtual_address: usize,
|
||||
mut size: usize,
|
||||
additional_flags: u64,
|
||||
virtual_address: usize,
|
||||
size_bytes: usize,
|
||||
phys: PhysSource,
|
||||
flags: u64,
|
||||
) -> Result<(), NovaError> {
|
||||
if !virtual_address.is_multiple_of(GRANULARITY) {
|
||||
return Err(NovaError::Misalignment);
|
||||
}
|
||||
|
||||
let level1_blocks = size / LEVEL1_BLOCK_SIZE;
|
||||
size %= LEVEL1_BLOCK_SIZE;
|
||||
let level2_blocks = size / LEVEL2_BLOCK_SIZE;
|
||||
size %= LEVEL2_BLOCK_SIZE;
|
||||
let level3_pages = size / GRANULARITY;
|
||||
if !size.is_multiple_of(GRANULARITY) {
|
||||
if !size_bytes.is_multiple_of(GRANULARITY) {
|
||||
return Err(NovaError::InvalidGranularity);
|
||||
}
|
||||
|
||||
if level1_blocks > 0 {
|
||||
todo!("Currently not supported");
|
||||
}
|
||||
|
||||
let base_table = if virtual_address & KERNEL_VIRTUAL_MEM_SPACE > 0 {
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR1)
|
||||
} else {
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0)
|
||||
};
|
||||
|
||||
for _ in 0..level2_blocks {
|
||||
alloc_block_l2(virtual_address, base_table, additional_flags)?;
|
||||
virtual_address += LEVEL2_BLOCK_SIZE;
|
||||
match phys {
|
||||
PhysSource::Any => map_range_dynamic(virtual_address, size_bytes, base_table, flags),
|
||||
PhysSource::Explicit(phys_addr) => {
|
||||
map_range_explicit(virtual_address, phys_addr, size_bytes, base_table, flags)
|
||||
}
|
||||
}
|
||||
for _ in 0..level3_pages {
|
||||
alloc_page(virtual_address, base_table, additional_flags)?;
|
||||
virtual_address += GRANULARITY;
|
||||
}
|
||||
|
||||
fn map_range_explicit(
|
||||
mut virt: VirtAddr,
|
||||
mut phys: PhysAddr,
|
||||
size_bytes: usize,
|
||||
base: *mut PageTable,
|
||||
flags: u64,
|
||||
) -> Result<(), NovaError> {
|
||||
let mut remaining = size_bytes;
|
||||
|
||||
while virt % LEVEL2_BLOCK_SIZE != 0 {
|
||||
map_page(virt, phys, base, flags)?;
|
||||
(virt, _) = virt.overflowing_add(GRANULARITY);
|
||||
phys += GRANULARITY;
|
||||
remaining -= GRANULARITY;
|
||||
}
|
||||
|
||||
while remaining >= LEVEL2_BLOCK_SIZE {
|
||||
map_l2_block(virt, phys, base, flags)?;
|
||||
(virt, _) = virt.overflowing_add(LEVEL2_BLOCK_SIZE);
|
||||
phys += LEVEL2_BLOCK_SIZE;
|
||||
remaining -= LEVEL2_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
while remaining > 0 {
|
||||
map_page(virt, phys, base, flags)?;
|
||||
(virt, _) = virt.overflowing_add(GRANULARITY);
|
||||
phys += GRANULARITY;
|
||||
remaining -= GRANULARITY;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Allocate a memory block of `size` starting at `virtual_address`,
|
||||
/// with explicit physical_address.
|
||||
///
|
||||
/// Note: This can be used when mapping predefined regions.
|
||||
pub fn allocate_memory_explicit(
|
||||
mut virtual_address: usize,
|
||||
mut size: usize,
|
||||
mut physical_address: usize,
|
||||
additional_flags: u64,
|
||||
fn map_range_dynamic(
|
||||
mut virt: PhysAddr,
|
||||
size_bytes: usize,
|
||||
base: *mut PageTable,
|
||||
flags: u64,
|
||||
) -> Result<(), NovaError> {
|
||||
if !virtual_address.is_multiple_of(GRANULARITY) {
|
||||
return Err(NovaError::Misalignment);
|
||||
}
|
||||
if !physical_address.is_multiple_of(GRANULARITY) {
|
||||
return Err(NovaError::Misalignment);
|
||||
let mut remaining = size_bytes;
|
||||
|
||||
while remaining >= LEVEL2_BLOCK_SIZE {
|
||||
map_l2_block(virt, reserve_block(), base, flags)?;
|
||||
(virt, _) = virt.overflowing_add(LEVEL2_BLOCK_SIZE);
|
||||
remaining -= LEVEL2_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
let level1_blocks = size / LEVEL1_BLOCK_SIZE;
|
||||
size %= LEVEL1_BLOCK_SIZE;
|
||||
let mut level2_blocks = size / LEVEL2_BLOCK_SIZE;
|
||||
size %= LEVEL2_BLOCK_SIZE;
|
||||
let mut level3_pages = size / GRANULARITY;
|
||||
if !size.is_multiple_of(GRANULARITY) {
|
||||
return Err(NovaError::InvalidGranularity);
|
||||
}
|
||||
|
||||
if level1_blocks > 0 {
|
||||
todo!("Currently not supported");
|
||||
}
|
||||
|
||||
let l2_alignment = (physical_address % LEVEL2_BLOCK_SIZE) / GRANULARITY;
|
||||
if l2_alignment != 0 {
|
||||
let l3_diff = LEVEL2_BLOCK_SIZE / GRANULARITY - l2_alignment;
|
||||
if l3_diff > level3_pages {
|
||||
level2_blocks -= 1;
|
||||
level3_pages += TABLE_ENTRY_COUNT;
|
||||
}
|
||||
|
||||
level3_pages -= l3_diff;
|
||||
|
||||
for _ in 0..l3_diff {
|
||||
alloc_page_explicit(
|
||||
virtual_address,
|
||||
physical_address,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
additional_flags,
|
||||
)?;
|
||||
|
||||
virtual_address += GRANULARITY;
|
||||
physical_address += GRANULARITY;
|
||||
}
|
||||
}
|
||||
|
||||
for _ in 0..level2_blocks {
|
||||
alloc_block_l2_explicit(
|
||||
virtual_address,
|
||||
physical_address,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
additional_flags,
|
||||
)?;
|
||||
virtual_address += LEVEL2_BLOCK_SIZE;
|
||||
physical_address += LEVEL2_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
for _ in 0..level3_pages {
|
||||
alloc_page_explicit(
|
||||
virtual_address,
|
||||
physical_address,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
additional_flags,
|
||||
)?;
|
||||
virtual_address += GRANULARITY;
|
||||
physical_address += GRANULARITY;
|
||||
while remaining > 0 {
|
||||
map_page(virt, reserve_page(), base, flags)?;
|
||||
(virt, _) = virt.overflowing_add(GRANULARITY);
|
||||
remaining -= GRANULARITY;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -210,7 +179,7 @@ pub fn alloc_page_explicit(
|
||||
)
|
||||
}
|
||||
|
||||
fn map_page(
|
||||
pub fn map_page(
|
||||
virtual_address: usize,
|
||||
physical_address: usize,
|
||||
base_table_ptr: *mut PageTable,
|
||||
@@ -232,20 +201,6 @@ fn map_page(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Allocate a level 2 block.
|
||||
pub fn alloc_block_l2(
|
||||
virtual_addr: usize,
|
||||
base_table_ptr: *mut PageTable,
|
||||
additional_flags: u64,
|
||||
) -> Result<(), NovaError> {
|
||||
map_l2_block(
|
||||
virtual_addr,
|
||||
reserve_block(),
|
||||
base_table_ptr,
|
||||
additional_flags,
|
||||
)
|
||||
}
|
||||
|
||||
// Allocate a level 2 block, at a explicit `physical_address`.
|
||||
pub fn alloc_block_l2_explicit(
|
||||
virtual_addr: usize,
|
||||
@@ -290,10 +245,10 @@ pub fn map_l2_block(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reserve_range_explicit(
|
||||
start_physical_address: usize,
|
||||
end_physical_address: usize,
|
||||
) -> Result<(), NovaError> {
|
||||
pub fn reserve_range(
|
||||
start_physical_address: PhysAddr,
|
||||
end_physical_address: PhysAddr,
|
||||
) -> Result<PhysAddr, NovaError> {
|
||||
let mut size = end_physical_address - start_physical_address;
|
||||
let l1_blocks = size / LEVEL1_BLOCK_SIZE;
|
||||
size %= LEVEL1_BLOCK_SIZE;
|
||||
@@ -320,57 +275,7 @@ pub fn reserve_range_explicit(
|
||||
addr += GRANULARITY;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reserve_page() -> usize {
|
||||
if let Some(address) = find_unallocated_page() {
|
||||
let page = address / GRANULARITY;
|
||||
let word_index = page / 64;
|
||||
unsafe { PAGING_BITMAP[word_index] |= 1 << (page % 64) };
|
||||
return address;
|
||||
}
|
||||
panic!("Out of Memory!");
|
||||
}
|
||||
|
||||
fn reserve_page_explicit(physical_address: usize) -> Result<(), NovaError> {
|
||||
let page = physical_address / GRANULARITY;
|
||||
let word_index = page / 64;
|
||||
|
||||
if unsafe { PAGING_BITMAP[word_index] } & (1 << (page % 64)) > 0 {
|
||||
return Err(NovaError::Paging);
|
||||
}
|
||||
|
||||
unsafe { PAGING_BITMAP[word_index] |= 1 << (page % 64) };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reserve_block() -> usize {
|
||||
if let Some(start) = find_contiguous_free_bitmap_words(L2_BLOCK_BITMAP_WORDS) {
|
||||
for j in 0..L2_BLOCK_BITMAP_WORDS {
|
||||
unsafe { PAGING_BITMAP[start + j] = u64::MAX };
|
||||
}
|
||||
return start * 64 * GRANULARITY;
|
||||
}
|
||||
|
||||
panic!("Out of Memory!");
|
||||
}
|
||||
|
||||
fn reserve_block_explicit(physical_address: usize) -> Result<(), NovaError> {
|
||||
let page = physical_address / GRANULARITY;
|
||||
for i in 0..L2_BLOCK_BITMAP_WORDS {
|
||||
unsafe {
|
||||
if PAGING_BITMAP[(page / 64) + i] != 0 {
|
||||
return Err(NovaError::Paging);
|
||||
}
|
||||
};
|
||||
}
|
||||
for i in 0..L2_BLOCK_BITMAP_WORDS {
|
||||
unsafe {
|
||||
PAGING_BITMAP[(page / 64) + i] = u64::MAX;
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
Ok(start_physical_address)
|
||||
}
|
||||
|
||||
fn create_block_descriptor_entry(physical_address: usize, additional_flags: u64) -> u64 {
|
||||
@@ -401,18 +306,6 @@ fn virtual_address_to_table_offset(virtual_addr: usize) -> (usize, usize, usize)
|
||||
(l1_off, l2_off, l3_off)
|
||||
}
|
||||
|
||||
/// Debugging function to navigate the translation tables.
|
||||
#[allow(unused_variables)]
|
||||
pub fn sim_l3_access(addr: usize) {
|
||||
unsafe {
|
||||
let entry1 = TRANSLATIONTABLE_TTBR0.0[addr / LEVEL1_BLOCK_SIZE];
|
||||
let table2 = &mut *(entry_phys(entry1 as usize) as *mut PageTable);
|
||||
let entry2 = table2.0[(addr % LEVEL1_BLOCK_SIZE) / LEVEL2_BLOCK_SIZE];
|
||||
let table3 = &mut *(entry_phys(entry2 as usize) as *mut PageTable);
|
||||
let _entry3 = table3.0[(addr % LEVEL2_BLOCK_SIZE) / GRANULARITY];
|
||||
}
|
||||
}
|
||||
|
||||
/// Navigate the table tree, by following given offsets. This function
|
||||
/// allocates new tables if required.
|
||||
fn navigate_table(
|
||||
@@ -451,49 +344,14 @@ fn next_table(table_ptr: *mut PageTable, offset: usize) -> Result<*mut PageTable
|
||||
}
|
||||
}
|
||||
|
||||
fn find_unallocated_page() -> Option<usize> {
|
||||
for (i, entry) in unsafe { PAGING_BITMAP }.iter().enumerate() {
|
||||
if *entry != u64::MAX {
|
||||
for offset in 0..64 {
|
||||
if entry >> offset & 0b1 == 0 {
|
||||
return Some((i * 64 + offset) * GRANULARITY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn find_contiguous_free_bitmap_words(required_words: usize) -> Option<usize> {
|
||||
let mut run_start = 0;
|
||||
let mut run_len = 0;
|
||||
|
||||
for (i, entry) in unsafe { PAGING_BITMAP }.iter().enumerate() {
|
||||
if *entry == 0 {
|
||||
if run_len == 0 {
|
||||
run_start = i;
|
||||
}
|
||||
run_len += 1;
|
||||
|
||||
if run_len == required_words {
|
||||
return Some(run_start);
|
||||
}
|
||||
} else {
|
||||
run_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Extracts the physical address out of an table entry.
|
||||
#[inline]
|
||||
fn entry_phys(entry: usize) -> usize {
|
||||
fn entry_phys(entry: usize) -> PhysAddr {
|
||||
entry & 0x0000_FFFF_FFFF_F000
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn entry_table_addr(entry: usize) -> usize {
|
||||
fn entry_table_addr(entry: usize) -> VirtAddr {
|
||||
if get_current_el() == 1 {
|
||||
phys_table_to_kernel_space(entry_phys(entry))
|
||||
} else {
|
||||
@@ -503,6 +361,6 @@ fn entry_table_addr(entry: usize) -> usize {
|
||||
|
||||
/// Extracts the physical address out of an table entry.
|
||||
#[inline]
|
||||
fn phys_table_to_kernel_space(entry: usize) -> usize {
|
||||
fn phys_table_to_kernel_space(entry: usize) -> VirtAddr {
|
||||
entry | TRANSLATION_TABLE_BASE_ADDR
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
use crate::aarch64::mmu::{PhysAddr, GRANULARITY, L2_BLOCK_BITMAP_WORDS, MAX_PAGE_COUNT};
|
||||
use nova_error::NovaError;
|
||||
|
||||
static mut PAGING_BITMAP: [u64; MAX_PAGE_COUNT / 64] = [0; MAX_PAGE_COUNT / 64];
|
||||
|
||||
pub fn reserve_page() -> PhysAddr {
|
||||
if let Some(address) = find_unallocated_page() {
|
||||
let page = address / GRANULARITY;
|
||||
let word_index = page / 64;
|
||||
unsafe { PAGING_BITMAP[word_index] |= 1 << (page % 64) };
|
||||
return address;
|
||||
}
|
||||
panic!("Out of Memory!");
|
||||
}
|
||||
|
||||
pub fn reserve_page_explicit(physical_address: usize) -> Result<PhysAddr, NovaError> {
|
||||
let page = physical_address / GRANULARITY;
|
||||
let word_index = page / 64;
|
||||
|
||||
if unsafe { PAGING_BITMAP[word_index] } & (1 << (page % 64)) > 0 {
|
||||
return Err(NovaError::Paging);
|
||||
}
|
||||
|
||||
unsafe { PAGING_BITMAP[word_index] |= 1 << (page % 64) };
|
||||
Ok(physical_address)
|
||||
}
|
||||
|
||||
pub fn reserve_block() -> usize {
|
||||
if let Some(start) = find_contiguous_free_bitmap_words(L2_BLOCK_BITMAP_WORDS) {
|
||||
for j in 0..L2_BLOCK_BITMAP_WORDS {
|
||||
unsafe { PAGING_BITMAP[start + j] = u64::MAX };
|
||||
}
|
||||
return start * 64 * GRANULARITY;
|
||||
}
|
||||
|
||||
panic!("Out of Memory!");
|
||||
}
|
||||
|
||||
pub fn reserve_block_explicit(physical_address: usize) -> Result<(), NovaError> {
|
||||
let page = physical_address / GRANULARITY;
|
||||
for i in 0..L2_BLOCK_BITMAP_WORDS {
|
||||
unsafe {
|
||||
if PAGING_BITMAP[(page / 64) + i] != 0 {
|
||||
return Err(NovaError::Paging);
|
||||
}
|
||||
};
|
||||
}
|
||||
for i in 0..L2_BLOCK_BITMAP_WORDS {
|
||||
unsafe {
|
||||
PAGING_BITMAP[(page / 64) + i] = u64::MAX;
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_unallocated_page() -> Option<usize> {
|
||||
for (i, entry) in unsafe { PAGING_BITMAP }.iter().enumerate() {
|
||||
if *entry != u64::MAX {
|
||||
for offset in 0..64 {
|
||||
if entry >> offset & 0b1 == 0 {
|
||||
return Some((i * 64 + offset) * GRANULARITY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn find_contiguous_free_bitmap_words(required_words: usize) -> Option<usize> {
|
||||
let mut run_start = 0;
|
||||
let mut run_len = 0;
|
||||
|
||||
for (i, entry) in unsafe { PAGING_BITMAP }.iter().enumerate() {
|
||||
if *entry == 0 {
|
||||
if run_len == 0 {
|
||||
run_start = i;
|
||||
}
|
||||
run_len += 1;
|
||||
|
||||
if run_len == required_words {
|
||||
return Some(run_start);
|
||||
}
|
||||
} else {
|
||||
run_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
+3
-2
@@ -87,8 +87,9 @@ el1_to_el0:
|
||||
msr ELR_EL1, x0
|
||||
|
||||
// Set SP_EL1 to stack base
|
||||
ldr x0, =__stack_end_el0
|
||||
msr SP_EL0, x0
|
||||
adrp x0, EL0_STACK_TOP
|
||||
ldr x1, [x0, :lo12:EL0_STACK_TOP]
|
||||
msr SP_EL0, x1
|
||||
|
||||
isb
|
||||
|
||||
|
||||
+57
-38
@@ -32,79 +32,98 @@ const AS: u64 = 0b1 << 36; // configure an ASID size of 16 bits
|
||||
pub static TCR_EL1_CONF: u64 = IPS | TG0 | TG1 | T0SZ | T1SZ | SH0 | SH1 | AS;
|
||||
|
||||
pub mod mmu {
|
||||
|
||||
use crate::{
|
||||
aarch64::mmu::{
|
||||
alloc_block_l2, alloc_block_l2_explicit, map_l2_block, reserve_range_explicit,
|
||||
DEVICE_MEM, EL0_ACCESSIBLE, KERNEL_VIRTUAL_MEM_SPACE, LEVEL1_BLOCK_SIZE,
|
||||
LEVEL2_BLOCK_SIZE, NORMAL_MEM, PXN, READ_ONLY, STACK_START_ADDR,
|
||||
TRANSLATIONTABLE_TTBR0, TRANSLATIONTABLE_TTBR1, UXN, WRITABLE,
|
||||
alloc_block_l2_explicit, allocate_memory, map_l2_block, map_page, reserve_range,
|
||||
PhysSource, DEVICE_MEM, EL0_ACCESSIBLE, GRANULARITY, KERNEL_VIRTUAL_MEM_SPACE,
|
||||
LEVEL1_BLOCK_SIZE, LEVEL2_BLOCK_SIZE, NORMAL_MEM, PXN, READ_ONLY, STACK_START_ADDR,
|
||||
TRANSLATIONTABLE_TTBR0, UXN, WRITABLE,
|
||||
},
|
||||
PERIPHERAL_BASE,
|
||||
};
|
||||
|
||||
#[no_mangle]
|
||||
static EL1_STACK_TOP: usize = STACK_START_ADDR | KERNEL_VIRTUAL_MEM_SPACE;
|
||||
const EL1_STACK_BOTTOM: usize = EL1_STACK_TOP - LEVEL2_BLOCK_SIZE * 2;
|
||||
|
||||
const EL1_STACK_SIZE: usize = LEVEL2_BLOCK_SIZE * 2;
|
||||
#[no_mangle]
|
||||
static EL0_STACK_TOP: usize = STACK_START_ADDR;
|
||||
const EL0_STACK_SIZE: usize = LEVEL2_BLOCK_SIZE * 2;
|
||||
extern "C" {
|
||||
static _data: u64;
|
||||
static _end: u64;
|
||||
static __text_end: u64;
|
||||
static __share_end: u64;
|
||||
static __kernel_end: u64;
|
||||
}
|
||||
|
||||
pub fn initialize_mmu_translation_tables() {
|
||||
let shared_segment_end = unsafe { &_data } as *const _ as usize;
|
||||
let text_end = unsafe { &__text_end } as *const _ as usize;
|
||||
let shared_segment_end = unsafe { &__share_end } as *const _ as usize;
|
||||
let kernel_end = unsafe { &__kernel_end } as *const _ as usize;
|
||||
let user_space_end = unsafe { &_end } as *const _ as usize;
|
||||
|
||||
reserve_range_explicit(0x0, user_space_end).unwrap();
|
||||
reserve_range(0x0, kernel_end).unwrap();
|
||||
|
||||
for addr in (0..shared_segment_end).step_by(LEVEL2_BLOCK_SIZE) {
|
||||
let _ = map_l2_block(
|
||||
for addr in (0..text_end).step_by(GRANULARITY) {
|
||||
map_page(
|
||||
addr,
|
||||
addr,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
EL0_ACCESSIBLE | READ_ONLY | NORMAL_MEM,
|
||||
);
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
for addr in (text_end..shared_segment_end).step_by(GRANULARITY) {
|
||||
map_page(
|
||||
addr,
|
||||
addr,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
EL0_ACCESSIBLE | WRITABLE | NORMAL_MEM,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
for addr in (shared_segment_end..kernel_end).step_by(LEVEL2_BLOCK_SIZE) {
|
||||
let _ = map_l2_block(
|
||||
map_l2_block(
|
||||
addr,
|
||||
addr,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
WRITABLE | UXN | NORMAL_MEM,
|
||||
);
|
||||
}
|
||||
|
||||
for addr in (kernel_end..user_space_end).step_by(LEVEL2_BLOCK_SIZE) {
|
||||
let _ = map_l2_block(
|
||||
addr,
|
||||
addr,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
EL0_ACCESSIBLE | WRITABLE | PXN | NORMAL_MEM,
|
||||
);
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
for addr in (PERIPHERAL_BASE..LEVEL1_BLOCK_SIZE).step_by(LEVEL2_BLOCK_SIZE) {
|
||||
let _ = alloc_block_l2_explicit(
|
||||
alloc_block_l2_explicit(
|
||||
addr,
|
||||
addr,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR0),
|
||||
EL0_ACCESSIBLE | WRITABLE | UXN | PXN | DEVICE_MEM,
|
||||
);
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
for addr in (EL1_STACK_BOTTOM..EL1_STACK_TOP)
|
||||
.rev()
|
||||
.step_by(LEVEL2_BLOCK_SIZE)
|
||||
{
|
||||
let _ = alloc_block_l2(
|
||||
addr,
|
||||
core::ptr::addr_of_mut!(TRANSLATIONTABLE_TTBR1),
|
||||
WRITABLE | NORMAL_MEM,
|
||||
);
|
||||
}
|
||||
// Frame Buffer memory range
|
||||
allocate_memory(
|
||||
0x3c100000,
|
||||
1080 * 1920 * 4,
|
||||
PhysSource::Explicit(0x3c100000),
|
||||
NORMAL_MEM | PXN | UXN | WRITABLE | EL0_ACCESSIBLE,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
allocate_memory(
|
||||
EL1_STACK_TOP - EL1_STACK_SIZE + 0x10,
|
||||
EL1_STACK_SIZE,
|
||||
PhysSource::Any,
|
||||
WRITABLE | NORMAL_MEM,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
allocate_memory(
|
||||
EL0_STACK_TOP - EL0_STACK_SIZE + 0x10,
|
||||
EL0_STACK_SIZE,
|
||||
PhysSource::Any,
|
||||
WRITABLE | EL0_ACCESSIBLE | NORMAL_MEM,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -14,7 +14,8 @@ use heap::Heap;
|
||||
|
||||
use crate::{
|
||||
aarch64::mmu::{
|
||||
allocate_memory, KERNEL_VIRTUAL_MEM_SPACE, LEVEL2_BLOCK_SIZE, NORMAL_MEM, UXN, WRITABLE,
|
||||
allocate_memory, PhysSource, KERNEL_VIRTUAL_MEM_SPACE, LEVEL2_BLOCK_SIZE, NORMAL_MEM, UXN,
|
||||
WRITABLE,
|
||||
},
|
||||
interrupt_handlers::initialize_interrupt_handler,
|
||||
logger::DefaultLogger,
|
||||
@@ -33,7 +34,7 @@ pub unsafe fn init_kernel_heap() {
|
||||
let start = core::ptr::addr_of_mut!(__kernel_end) as usize | KERNEL_VIRTUAL_MEM_SPACE;
|
||||
let size = LEVEL2_BLOCK_SIZE * 2;
|
||||
|
||||
allocate_memory(start, size, NORMAL_MEM | UXN | WRITABLE).unwrap();
|
||||
allocate_memory(start, size, PhysSource::Any, NORMAL_MEM | UXN | WRITABLE).unwrap();
|
||||
let heap = core::ptr::addr_of_mut!(GLOBAL_ALLOCATOR);
|
||||
(*heap).init(start, start + size);
|
||||
}
|
||||
|
||||
+20
-26
@@ -11,10 +11,7 @@ extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use nova::{
|
||||
aarch64::{
|
||||
mmu::{allocate_memory_explicit, EL0_ACCESSIBLE, NORMAL_MEM, PXN, UXN, WRITABLE},
|
||||
registers::{daif, read_id_aa64mmfr0_el1},
|
||||
},
|
||||
aarch64::registers::{daif, read_id_aa64mmfr0_el1},
|
||||
configuration::mmu::initialize_mmu_translation_tables,
|
||||
framebuffer::{FrameBuffer, BLUE, GREEN, RED},
|
||||
get_current_el,
|
||||
@@ -26,13 +23,14 @@ use nova::{
|
||||
},
|
||||
uart::uart_init,
|
||||
},
|
||||
pi3::mailbox,
|
||||
println,
|
||||
};
|
||||
|
||||
global_asm!(include_str!("vector.S"));
|
||||
global_asm!(include_str!("config.S"));
|
||||
|
||||
static mut FRAMEBUFFER: Option<FrameBuffer> = None;
|
||||
|
||||
extern "C" {
|
||||
fn el2_to_el1();
|
||||
fn el1_to_el0();
|
||||
@@ -74,6 +72,8 @@ pub extern "C" fn main() -> ! {
|
||||
|
||||
println!("Register: AA64MMFR0_EL1: {:064b}", read_id_aa64mmfr0_el1());
|
||||
println!("Moving El2->EL1");
|
||||
unsafe { FRAMEBUFFER = Some(FrameBuffer::default()) };
|
||||
|
||||
unsafe {
|
||||
el2_to_el1();
|
||||
}
|
||||
@@ -100,15 +100,6 @@ pub extern "C" fn kernel_main() -> ! {
|
||||
}
|
||||
println!("heap allocation test: {:?}", test_vector);
|
||||
|
||||
// Frame Buffer memory range
|
||||
// TODO: this is just temporary
|
||||
allocate_memory_explicit(
|
||||
0x3c100000,
|
||||
1080 * 1920 * 4,
|
||||
0x3c100000,
|
||||
NORMAL_MEM | PXN | UXN | WRITABLE | EL0_ACCESSIBLE,
|
||||
)
|
||||
.unwrap();
|
||||
println!("Exception Level: {}", get_current_el());
|
||||
daif::unmask_all();
|
||||
|
||||
@@ -132,22 +123,25 @@ pub extern "C" fn el0() -> ! {
|
||||
|
||||
enable_irq_source(IRQSource::UartInt);
|
||||
|
||||
let fb = FrameBuffer::default();
|
||||
if let Some(fb) = unsafe { FRAMEBUFFER.as_mut() } {
|
||||
for i in 0..1080 {
|
||||
fb.draw_pixel(50, i, BLUE);
|
||||
}
|
||||
fb.draw_square(500, 500, 600, 700, RED);
|
||||
fb.draw_square_fill(800, 800, 900, 900, GREEN);
|
||||
fb.draw_square_fill(1000, 800, 1200, 700, BLUE);
|
||||
fb.draw_square_fill(900, 100, 800, 150, RED | BLUE);
|
||||
fb.draw_string("Hello World! :D\nTest next Line", 500, 5, 3, BLUE);
|
||||
|
||||
for i in 0..1080 {
|
||||
fb.draw_pixel(50, i, BLUE);
|
||||
fb.draw_function(cos, 0, 101, RED);
|
||||
}
|
||||
fb.draw_square(500, 500, 600, 700, RED);
|
||||
fb.draw_square_fill(800, 800, 900, 900, GREEN);
|
||||
fb.draw_square_fill(1000, 800, 1200, 700, BLUE);
|
||||
fb.draw_square_fill(900, 100, 800, 150, RED | BLUE);
|
||||
fb.draw_string("Hello World! :D\nTest next Line", 500, 5, 3, BLUE);
|
||||
|
||||
fb.draw_function(cos, 0, 101, RED);
|
||||
|
||||
loop {
|
||||
let temp = mailbox::read_soc_temp([0]).unwrap();
|
||||
println!("{} °C", temp[1] / 1000);
|
||||
// TODO: Mailbox requires a physical address. The stack is now in VA space causing an issue.
|
||||
// Fix with SVCs ?
|
||||
|
||||
// let temp = mailbox::read_soc_temp([0]).unwrap();
|
||||
// println!("{} °C", temp[1] / 1000);
|
||||
|
||||
blink_gpio(SpecificGpio::OnboardLed as u8, 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user