Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix small mapping issues, update toolchain #48

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"xtask",
"check",
"--json"
]
],
"lldb.displayFormat": "auto",
"lldb.showDisassembly": "auto",
"lldb.dereferencePointers": true,
"lldb.consoleMode": "commands"
}
20 changes: 9 additions & 11 deletions aarch64/lib/kernel.ld
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@ SECTIONS {
boottext = .;
.text.boot : ALIGN(4096) {
*(.boottext .bootdata)
. = ALIGN(4096);
eboottext = .;
. = ALIGN(2097152);
esys = .;
}
. = ALIGN(4096);
eboottext = .;

text = .;
.text : ALIGN(4096) {
*(.text* .stub .gnu.linkonce.t.*)
. = ALIGN(2097152);
etext = .;
}
. = ALIGN(4096);
etext = .;

rodata = .;
.rodata : ALIGN(4096) {
*(.rodata* .gnu.linkonce.r.*)
. = ALIGN(2097152);
erodata = .;
}
. = ALIGN(4096);
erodata = .;

data = .;
.data : ALIGN(4096) {
Expand All @@ -41,14 +39,15 @@ SECTIONS {
.got.plt : ALIGN(4096) {
*(.got.plt)
}
. = ALIGN(4096);
edata = .;

bss = .;
.bss : ALIGN(4096) {
*(.bss*)
*(COMMON)
. = ALIGN(2097152);
}
. = ALIGN(4096);
ebss = .;

/* Reserve section for early pagetables. Align to 2MiB to allow us to map
Expand All @@ -59,9 +58,8 @@ SECTIONS {
early_pagetables = .;
. += 2 * 1024 * 1024;
eearly_pagetables = .;

end = .;
PROVIDE(end = .);

/DISCARD/ : {
*(.eh_frame .note.GNU-stack)
Expand Down
71 changes: 65 additions & 6 deletions aarch64/src/kmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,96 @@ use port::mem::{PhysAddr, PhysRange};

// These map to definitions in kernel.ld
extern "C" {
static eboottext: [u64; 0];
static text: [u64; 0];
static etext: [u64; 0];
static rodata: [u64; 0];
static erodata: [u64; 0];
static data: [u64; 0];
static edata: [u64; 0];
static bss: [u64; 0];
static ebss: [u64; 0];
static end: [u64; 0];
static early_pagetables: [u64; 0];
static eearly_pagetables: [u64; 0];
}

pub fn text_addr() -> usize {
fn base_addr() -> usize {
0xffff_8000_0000_0000
}

pub fn etext_addr() -> usize {
fn eboottext_addr() -> usize {
unsafe { eboottext.as_ptr().addr() }
}

fn text_addr() -> usize {
unsafe { text.as_ptr().addr() }
}

fn etext_addr() -> usize {
unsafe { etext.as_ptr().addr() }
}

pub fn erodata_addr() -> usize {
fn rodata_addr() -> usize {
unsafe { rodata.as_ptr().addr() }
}

fn erodata_addr() -> usize {
unsafe { erodata.as_ptr().addr() }
}

pub fn ebss_addr() -> usize {
fn data_addr() -> usize {
unsafe { data.as_ptr().addr() }
}

fn edata_addr() -> usize {
unsafe { edata.as_ptr().addr() }
}

fn bss_addr() -> usize {
unsafe { bss.as_ptr().addr() }
}

fn ebss_addr() -> usize {
unsafe { ebss.as_ptr().addr() }
}

pub fn early_pagetables_addr() -> usize {
fn end_addr() -> usize {
unsafe { end.as_ptr().addr() }
}

fn early_pagetables_addr() -> usize {
unsafe { early_pagetables.as_ptr().addr() }
}

pub fn eearly_pagetables_addr() -> usize {
fn eearly_pagetables_addr() -> usize {
unsafe { eearly_pagetables.as_ptr().addr() }
}

pub fn boottext_range() -> PhysRange {
PhysRange(from_virt_to_physaddr(base_addr())..from_virt_to_physaddr(eboottext_addr()))
}

pub fn text_range() -> PhysRange {
PhysRange(from_virt_to_physaddr(text_addr())..from_virt_to_physaddr(etext_addr()))
}

pub fn rodata_range() -> PhysRange {
PhysRange(from_virt_to_physaddr(rodata_addr())..from_virt_to_physaddr(erodata_addr()))
}

pub fn data_range() -> PhysRange {
PhysRange(from_virt_to_physaddr(data_addr())..from_virt_to_physaddr(edata_addr()))
}

pub fn bss_range() -> PhysRange {
PhysRange(from_virt_to_physaddr(bss_addr())..from_virt_to_physaddr(ebss_addr()))
}

pub fn total_kernel_range() -> PhysRange {
PhysRange(from_virt_to_physaddr(base_addr())..from_virt_to_physaddr(end_addr()))
}

pub const fn physaddr_as_virt(pa: PhysAddr) -> usize {
(pa.addr() as usize).wrapping_add(KZERO)
}
Expand Down
44 changes: 18 additions & 26 deletions aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![cfg_attr(not(test), no_main)]
#![feature(alloc_error_handler)]
#![feature(core_intrinsics)]
#![feature(strict_provenance)]
#![feature(sync_unsafe_cell)]
#![forbid(unsafe_op_in_unsafe_fn)]

Expand All @@ -22,8 +21,8 @@ mod vm;

use crate::kmem::from_virt_to_physaddr;
use crate::vm::kernel_root;
use core::ffi::c_void;
use core::ptr;
use kmem::{boottext_range, bss_range, data_range, rodata_range, text_range, total_kernel_range};
use port::fdt::DeviceTree;
use port::mem::PhysRange;
use port::println;
Expand All @@ -34,35 +33,20 @@ core::arch::global_asm!(include_str!("l.S"));

static mut KPGTBL: PageTable = PageTable::empty();

unsafe fn print_memory_range(name: &str, start: &*const c_void, end: &*const c_void) {
let start = start as *const _ as u64;
let end = end as *const _ as u64;
let size = end - start;
println!(" {name}{start:#x}..{end:#x} ({size:#x})");
unsafe fn print_memory_range(name: &str, range: &PhysRange) {
let size = range.size();
println!(" {name}{range} ({size:#x})");
}

fn print_binary_sections() {
extern "C" {
static boottext: *const c_void;
static eboottext: *const c_void;
static text: *const c_void;
static etext: *const c_void;
static rodata: *const c_void;
static erodata: *const c_void;
static data: *const c_void;
static edata: *const c_void;
static bss: *const c_void;
static end: *const c_void;
}

println!("Binary sections:");
unsafe {
print_memory_range("boottext:\t", &boottext, &eboottext);
print_memory_range("text:\t\t", &text, &etext);
print_memory_range("rodata:\t", &rodata, &erodata);
print_memory_range("data:\t\t", &data, &edata);
print_memory_range("bss:\t\t", &bss, &end);
print_memory_range("total:\t", &boottext, &end);
print_memory_range("boottext:\t", &boottext_range());
print_memory_range("text:\t\t", &text_range());
print_memory_range("rodata:\t", &rodata_range());
print_memory_range("data:\t\t", &data_range());
print_memory_range("bss:\t\t", &bss_range());
print_memory_range("total:\t", &total_kernel_range());
}
}

Expand Down Expand Up @@ -141,6 +125,14 @@ pub extern "C" fn main9(dtb_va: usize) {

print_memory_info();

if let Ok(page) = pagealloc::allocate() {
println!("page addr: {:#016x}", page.data().as_ptr() as *const _ as u64);

//let mapped_range =
// let kpgtable = unsafe { &mut *ptr::addr_of_mut!(KPGTBL) };
// kpgtable.map_phys_range(range, *flags, *page_size).expect("dynamic mapping failed");
}

kernel_root().print_recursive_tables();

println!("looping now");
Expand Down
38 changes: 14 additions & 24 deletions aarch64/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::{
kmem::{
ebss_addr, erodata_addr, etext_addr, from_ptr_to_physaddr, from_virt_to_physaddr,
physaddr_as_ptr_mut, physaddr_as_virt, text_addr,
boottext_range, bss_range, data_range, from_ptr_to_physaddr, physaddr_as_ptr_mut,
physaddr_as_virt, rodata_range, text_range,
},
pagealloc,
registers::rpi_mmio,
Expand Down Expand Up @@ -48,6 +48,10 @@ impl Page4K {
core::intrinsics::volatile_set_memory(&mut self.0, 0u8, 1);
}
}

pub fn data(&mut self) -> &mut [u8] {
&mut self.0
}
}

#[derive(Debug, IntoPrimitive, FromPrimitive)]
Expand Down Expand Up @@ -485,24 +489,16 @@ pub unsafe fn init(kpage_table: &mut PageTable, dtb_range: PhysRange, available_

// TODO leave the first page unmapped to catch null pointer dereferences in unsafe code
let custom_map = {
let text_range =
PhysRange(from_virt_to_physaddr(text_addr())..from_virt_to_physaddr(etext_addr()));
let data_range = PhysRange::with_len(
from_virt_to_physaddr(etext_addr()).addr(),
erodata_addr() - etext_addr(),
);
let bss_range = PhysRange::with_len(
from_virt_to_physaddr(erodata_addr()).addr(),
ebss_addr() - erodata_addr(),
);

let text_range = boottext_range().add(&text_range());
let data_range = rodata_range().add(&data_range());
let bss_range = bss_range();
let mmio_range = rpi_mmio().expect("mmio base detect failed");

let mut map = [
("DTB", dtb_range, Entry::ro_kernel_data(), PageSize::Page4K),
("Kernel Text", text_range, Entry::ro_kernel_text(), PageSize::Page2M),
("Kernel Data", data_range, Entry::ro_kernel_data(), PageSize::Page2M),
("Kernel BSS", bss_range, Entry::rw_kernel_data(), PageSize::Page2M),
("Kernel Text", text_range, Entry::ro_kernel_text(), PageSize::Page4K),
("Kernel Data", data_range, Entry::rw_kernel_data(), PageSize::Page4K),
("Kernel BSS", bss_range, Entry::rw_kernel_data(), PageSize::Page4K),
("MMIO", mmio_range, Entry::ro_kernel_device(), PageSize::Page2M),
];
map.sort_by_key(|a| a.1.start());
Expand All @@ -515,14 +511,8 @@ pub unsafe fn init(kpage_table: &mut PageTable, dtb_range: PhysRange, available_
kpage_table.map_phys_range(range, *flags, *page_size).expect("init mapping failed");

println!(
" {:14}{:#018x}..{:#018x} to {:#018x}..{:#018x} flags: {:?} page_size: {:?}",
name,
range.start().addr(),
range.end().addr(),
mapped_range.0,
mapped_range.1,
flags,
page_size
" {:14}{} to {:#018x}..{:#018x} flags: {:?} page_size: {:?}",
name, range, mapped_range.0, mapped_range.1, flags, page_size
);
}

Expand Down
1 change: 0 additions & 1 deletion port/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![feature(allocator_api)]
#![feature(maybe_uninit_slice)]
#![feature(step_trait)]
#![feature(strict_provenance)]
#![forbid(unsafe_op_in_unsafe_fn)]

extern crate alloc;
Expand Down
5 changes: 5 additions & 0 deletions port/src/mem.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::fdt::RegBlock;
use core::{
cmp::{max, min},
fmt,
iter::{Step, StepBy},
ops::{self, Range},
Expand Down Expand Up @@ -145,6 +146,10 @@ impl PhysRange {
let endpa = self.end().round_up(step_size as u64);
(startpa..endpa).step_by(step_size)
}

pub fn add(&self, other: &PhysRange) -> Self {
Self(min(self.0.start, other.0.start)..max(self.0.end, other.0.end))
}
}

impl fmt::Display for PhysRange {
Expand Down
6 changes: 3 additions & 3 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[toolchain]
channel = "nightly-2024-10-15"
components = [ "rustfmt", "rust-src", "clippy", "llvm-tools" ]
channel = "nightly-2024-10-23"
components = ["rustfmt", "rust-src", "clippy", "llvm-tools"]
targets = [
"aarch64-unknown-none",
"riscv64gc-unknown-none-elf",
"x86_64-unknown-none"
"x86_64-unknown-none",
]
Loading