Skip to content

Commit 22f5cf9

Browse files
committed
aarch64: map framebuffer & set mem attr in bootloader
1 parent 2237d2e commit 22f5cf9

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed

bootloader/src/arch/aarch64/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use aarch64::paging::{memory_attribute::*, PageTable, PageTableAttribute, PageTa
33
use aarch64::paging::{Page, PhysFrame, Size1GiB, Size2MiB, Size4KiB};
44
use aarch64::{asm::*, barrier, regs::*};
55
use bcm2837::addr::{phys_to_virt, virt_to_phys};
6-
use bcm2837::addr::{PHYSICAL_IO_START, PHYSICAL_IO_END, PHYSICAL_MEMORY_OFFSET};
6+
use bcm2837::addr::{PHYSICAL_IO_END, PHYSICAL_IO_START, PHYSICAL_MEMORY_OFFSET};
77
use bcm2837::atags::Atags;
88
use bootinfo::BootInfo;
99
use core::ptr;
@@ -15,6 +15,15 @@ const ALIGN_2MB: u64 = 0x200000;
1515

1616
global_asm!(include_str!("boot.S"));
1717

18+
fn map_2mib(p2: &mut PageTable, start: usize, end: usize, flag: EF, attr: PageTableAttribute) {
19+
for frame in PhysFrame::<Size2MiB>::range_of(start as u64, end as u64) {
20+
let paddr = frame.start_address();
21+
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64() as usize) as u64);
22+
let page = Page::<Size2MiB>::containing_address(vaddr);
23+
p2[page.p2_index()].set_block::<Size2MiB>(paddr, flag, attr);
24+
}
25+
}
26+
1827
// TODO: set segments permission
1928
fn create_page_table(start_paddr: usize, end_paddr: usize) {
2029
#[repr(align(4096))]
@@ -35,19 +44,11 @@ fn create_page_table(start_paddr: usize, end_paddr: usize) {
3544

3645
let block_flags = EF::default_block() | EF::UXN;
3746
// normal memory
38-
for frame in PhysFrame::<Size2MiB>::range_of(start_paddr as u64, end_paddr as u64) {
39-
let paddr = frame.start_address();
40-
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64() as usize) as u64);
41-
let page = Page::<Size2MiB>::containing_address(vaddr);
42-
p2[page.p2_index()].set_block::<Size2MiB>(paddr, block_flags, MairNormal::attr_value());
43-
}
47+
map_2mib(p2, start_paddr, end_paddr, block_flags, MairNormal::attr_value());
48+
// normal non-cacheable memory
49+
map_2mib(p2, end_paddr, PHYSICAL_IO_START, block_flags | EF::PXN, MairNormalNonCacheable::attr_value());
4450
// device memory
45-
for frame in PhysFrame::<Size2MiB>::range_of(PHYSICAL_IO_START as u64, PHYSICAL_IO_END as u64) {
46-
let paddr = frame.start_address();
47-
let vaddr = VirtAddr::new(phys_to_virt(paddr.as_u64() as usize) as u64);
48-
let page = Page::<Size2MiB>::containing_address(vaddr);
49-
p2[page.p2_index()].set_block::<Size2MiB>(paddr, block_flags | EF::PXN, MairDevice::attr_value());
50-
}
51+
map_2mib(p2, PHYSICAL_IO_START, PHYSICAL_IO_END, block_flags | EF::PXN, MairDevice::attr_value());
5152

5253
p3[0].set_frame(frame_lvl2, EF::default_table(), PageTableAttribute::new(0, 0, 0));
5354
p3[1].set_block::<Size1GiB>(PhysAddr::new(PHYSICAL_IO_END as u64), block_flags | EF::PXN, MairDevice::attr_value());

kernel/src/drivers/console/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ lazy_static! {
1313

1414
/// Initialize console driver
1515
pub fn init() {
16-
if let Some(fb) = FRAME_BUFFER.lock().take() {
17-
// FIXME: now take FrameBuffer out of global variable, then move into Console
18-
let console = Console::on_frame_buffer(fb.fb_info.xres, fb.fb_info.yres, fb);
19-
*CONSOLE.lock() = Some(console);
20-
info!("console: init end");
21-
} else {
22-
warn!("console: init failed");
16+
if cfg!(feature = "consolegraphic") {
17+
if let Some(fb) = FRAME_BUFFER.lock().take() {
18+
// FIXME: now take FrameBuffer out of global variable, then move into Console
19+
let console = Console::on_frame_buffer(fb.fb_info.xres, fb.fb_info.yres, fb);
20+
*CONSOLE.lock() = Some(console);
21+
info!("console: init end");
22+
} else {
23+
warn!("console: init failed");
24+
}
2325
}
2426
}

kernel/src/drivers/gpu/fb.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Framebuffer
22
3-
use crate::fs::vga::{fb_bitfield, fb_fix_screeninfo, fb_var_screeninfo};
3+
use crate::fs::vga::{fb_fix_screeninfo, fb_var_screeninfo};
44
use alloc::string::String;
55
use core::fmt;
66
use lazy_static::lazy_static;
@@ -55,7 +55,6 @@ pub enum ColorConfig {
5555
BGRA8888, // RPi3 B+ uses BGRA
5656
VgaPalette,
5757
}
58-
use self::ColorConfig::*;
5958

6059
pub type FramebufferResult = Result<(FramebufferInfo, ColorConfig, usize), String>;
6160

kernel/src/memory.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ lazy_static! {
5555
}
5656

5757
/// Convert physical address to virtual address
58+
// #[inline]
5859
pub const fn phys_to_virt(paddr: usize) -> usize {
5960
PHYSICAL_MEMORY_OFFSET + paddr
6061
}
6162

6263
/// Convert virtual address to physical address
64+
// #[inline]
6365
pub const fn virt_to_phys(vaddr: usize) -> usize {
6466
vaddr - PHYSICAL_MEMORY_OFFSET
6567
}

kernel/src/syscall/mem.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ impl Syscall<'_> {
5656
match &*file.path {
5757
"/dev/fb0" => {
5858
use crate::arch::board::fb::FRAME_BUFFER;
59+
let attr = prot.to_attr();
60+
#[cfg(feature = "board_raspi3")]
61+
let attr = attr.mmio(crate::arch::paging::MMIOType::NormalNonCacheable as u8);
62+
5963
if let Some(fb) = FRAME_BUFFER.lock().as_mut() {
6064
self.vm().push(
6165
addr,
6266
addr + len,
63-
prot.to_attr(),
64-
Linear::new((fb.bus_addr() - addr) as isize),
67+
attr,
68+
Linear::new((crate::memory::virt_to_phys(fb.base_addr()) - addr) as isize),
6569
"mmap_file",
6670
);
6771
info!("mmap for /dev/fb0");

0 commit comments

Comments
 (0)