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

Add support for qemu machine raspi4b #39

Merged
merged 2 commits into from
Aug 28, 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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ check to ensure `qemu` or `qemu-kvm` is installed and the

R9 can be run using qemu for the various supported architectures:

|Arch|Commandline|
|----|-----------|
|aarch64|cargo xtask qemu --arch aarch64 --verbose|
|x86-64|cargo xtask qemu --arch x86-64 --verbose|
|x86-64 (with kvm)|cargo xtask qemu --arch x86-64 --kvm --verbose|
|riscv|cargo xtask qemu --arch riscv64 --verbose|
|Arch|Platform|Commandline|
|----|--------|-----------|
|aarch64|raspi3b|cargo xtask qemu --arch aarch64 --verbose|
|aarch64|raspi4b|cargo xtask qemu --arch aarch64 --config raspi4b --verbose|
|x86-64|q35|cargo xtask qemu --arch x86-64 --verbose|
|x86-64 (with kvm)|q35|cargo xtask qemu --arch x86-64 --kvm --verbose|
|riscv|virt|cargo xtask qemu --arch riscv64 --verbose|

## Running on Real Hardware™️

Expand Down
8 changes: 5 additions & 3 deletions aarch64/lib/config_default.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[build]
target = "lib/aarch64-unknown-none-elf.json"
buildflags = [
"-Z", "build-std=core,alloc"
]
buildflags = ["-Z", "build-std=core,alloc"]

[link]
# linker script to use
script = 'aarch64/lib/kernel.ld'

# kernel load address to insert into kernel.ld
load-address = '0xffff800000100000 - 0x80000'

[qemu]
machine = "raspi3b"
dtb = "aarch64/lib/bcm2710-rpi-3-b.dtb"
14 changes: 14 additions & 0 deletions aarch64/lib/config_raspi4b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build]
target = "lib/aarch64-unknown-none-elf.json"
buildflags = ["-Z", "build-std=core,alloc"]

[link]
# linker script to use
script = 'aarch64/lib/kernel.ld'

# kernel load address to insert into kernel.ld
load-address = '0xffff800000100000 - 0x80000'

[qemu]
machine = "raspi4b"
dtb = "aarch64/lib/bcm2711-rpi-4-b.dtb"
27 changes: 16 additions & 11 deletions aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ fn print_binary_sections() {
}
}

fn print_memory_info() {
fn print_physical_memory_info() {
println!("Physical memory map:");
let arm_mem = mailbox::get_arm_memory();
println!(" Memory:\t{arm_mem} ({:#x})", arm_mem.size());
let vc_mem = mailbox::get_vc_memory();
println!(" Video:\t{vc_mem} ({:#x})", vc_mem.size());
}

println!("Memory usage::");
fn print_memory_info() {
println!("Memory usage:");
let (used, total) = pagealloc::usage_bytes();
println!(" Used:\t\t{used:#016x}");
println!(" Total:\t{total:#016x}");
Expand All @@ -84,25 +86,26 @@ fn print_pi_name(board_revision: u32) {
let name = match board_revision {
0xa21041 => "Raspberry Pi 2B",
0xa02082 => "Raspberry Pi 3B",
0xb03115 => "Raspberry Pi 4B",
0xa220a0 => "Raspberry Compute Module 3",
_ => "Unknown",
_ => "Unrecognised",
};
println!(" Board Name: {name}");
println!(" Board Name:\t{name}");
}

fn print_board_info() {
println!("Board information:");
let board_revision = mailbox::get_board_revision();
print_pi_name(board_revision);
println!(" Board Revision: {board_revision:#010x}");
println!(" Board Rev:\t{board_revision:#010x}");
let model = mailbox::get_board_model();
println!(" Board Model: {model:#010x}");
println!(" Board Model:\t{model:#010x}");
let serial = mailbox::get_board_serial();
println!(" Serial Number: {serial:#010x}");
println!(" Serial Num:\t{serial:#010x}");
let mailbox::MacAddress { a, b, c, d, e, f } = mailbox::get_board_macaddr();
println!(" MAC Address: {a:02x}:{b:02x}:{c:02x}:{d:02x}:{e:02x}:{f:02x}");
println!(" MAC Address:\t{a:02x}:{b:02x}:{c:02x}:{d:02x}:{e:02x}:{f:02x}");
let fw_revision = mailbox::get_firmware_revision();
println!(" Firmware Revision: {fw_revision:#010x}");
println!(" Firmware Rev:\t{fw_revision:#010x}");
}

/// dtb_va is the virtual address of the DTB structure. The physical address is
Expand All @@ -123,6 +126,10 @@ pub extern "C" fn main9(dtb_va: usize) {
println!("DTB found at: {:#x}", dtb_va);
println!("midr_el1: {:?}", registers::MidrEl1::read());

print_binary_sections();
print_physical_memory_info();
print_board_info();

// Map address space accurately using rust VM code to manage page tables
unsafe {
let dtb_range = PhysRange::with_len(from_virt_to_physaddr(dtb_va).addr(), dt.size());
Expand All @@ -132,9 +139,7 @@ pub extern "C" fn main9(dtb_va: usize) {

// From this point we can use the global allocator

print_binary_sections();
print_memory_info();
print_board_info();

kernel_root().print_recursive_tables();

Expand Down
4 changes: 2 additions & 2 deletions aarch64/src/pagealloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use port::{
};

/// Set up bitmap page allocator assuming everything is allocated.
static PAGE_ALLOC: Lock<BitmapPageAlloc<16, PAGE_SIZE_4K>> = Lock::new(
static PAGE_ALLOC: Lock<BitmapPageAlloc<32, PAGE_SIZE_4K>> = Lock::new(
"page_alloc",
const { BitmapPageAlloc::<16, PAGE_SIZE_4K>::new_all_allocated(PAGE_SIZE_4K) },
const { BitmapPageAlloc::<32, PAGE_SIZE_4K>::new_all_allocated(PAGE_SIZE_4K) },
);

/// The bitmap allocator has all pages marked as allocated initially. We'll
Expand Down
4 changes: 3 additions & 1 deletion aarch64/src/uartmini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ pub struct MiniUart {
#[allow(dead_code)]
impl MiniUart {
pub fn new(dt: &DeviceTree, mmio_virt_offset: usize) -> MiniUart {
// TODO use aliases?
// Bcm2835 and bcm2711 are essentially the same for our needs here.
// If fdt.rs supported aliases well, we could try to just look up 'gpio'.
let gpio_range = VirtRange::from(
&dt.find_compatible("brcm,bcm2835-gpio")
.next()
.or_else(|| dt.find_compatible("brcm,bcm2711-gpio").next())
.and_then(|uart| dt.property_translated_reg_iter(uart).next())
.and_then(|reg| reg.regblock())
.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion port/src/bitmapalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<const SIZE_BYTES: usize> Bitmap<SIZE_BYTES> {

#[derive(Debug, PartialEq)]
pub enum BitmapPageAllocError {
NotEnoughBitmaps,
OutOfBounds,
MisalignedAddr,
OutOfSpace,
Expand Down Expand Up @@ -215,7 +216,7 @@ impl<const NUM_BITMAPS: usize, const BITMAP_SIZE_BYTES: usize>
check_end: bool,
) -> Result<(), BitmapPageAllocError> {
if check_end && range.0.end > self.end {
return Err(BitmapPageAllocError::OutOfBounds);
return Err(BitmapPageAllocError::NotEnoughBitmaps);
}

for pa in range.step_by_rounded(self.alloc_page_size) {
Expand Down
3 changes: 1 addition & 2 deletions port/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ impl PhysRange {

impl fmt::Display for PhysRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#016x}..{:#016x}", self.0.start.addr(), self.0.end.addr())?;
Ok(())
write!(f, "{:#016x}..{:#016x}", self.0.start.addr(), self.0.end.addr())
}
}

Expand Down
Loading
Loading