Skip to content

aarch64: Restrict address mappings in loader.rs#464

Open
bruelc wants to merge 1 commit intoseL4:mainfrom
bruelc:master-mmuloader
Open

aarch64: Restrict address mappings in loader.rs#464
bruelc wants to merge 1 commit intoseL4:mainfrom
bruelc:master-mmuloader

Conversation

@bruelc
Copy link
Copy Markdown

@bruelc bruelc commented Apr 1, 2026

Hello,

This is a rework of the existing support committed in elfloader:
seL4/seL4_tools#244
and discussed here:
https://lists.sel4.systems/hyperkitty/list/devel@sel4.systems/thread/DJNDO5CUQBKGIA4SQHXCNQ3T6SKZEY4A/

This patch limits the 1:1 address mapping in the loader to cover only the necessary blocks using level-2 translation tables with 2MB blocks. The UART MMIO address is now exposed as a global uart_addr variable.

This prevents speculative accesses to device or secure memory, which could otherwise cause faults or unwanted side effects.

This has been tested only on STM32MP2. Testers on other platforms would be greatly appreciated.

Thank you for your review.

@bruelc bruelc mentioned this pull request Apr 1, 2026
Copy link
Copy Markdown
Contributor

@midnightveil midnightveil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine. Just some minor code cleanups to do.

.expect("Could not find 'end' symbol");

if Aarch64::lvl1_index(text_addr) != Aarch64::lvl1_index(end_addr) {
eprintln!("ERROR: We only map 1GiB, but elfloader paddr range covers multiple GiB");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we tend to use panic!("INTERNAL: <message>") here.

I'd also reword this: specifically the issue is the microkit loader (not elfloader) crosses a GiB boundary [or really that it requires multiple level 1 page tables].

It likely makes sense to do this change to both AArch64 and Riscv64, no sense it being different for either.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with the Riscv64 MMU tables, and I do not have access to a testing platform. Is there any volunteer to do the adaptation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. We're planning on changing a few things after these are good to go, anyway, and having to retest.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you. I noticed the CI doesn’t build for riscv64 without uart_addr. I’ll guard it to fix the build before your changes.

Comment on lines +510 to +514
if let Ok((uart_addr, _)) = elf.find_symbol("uart_addr") {
let data = elf
.get_data(uart_addr, 8)
.expect("uart_addr not initialized");
let uart_base = u64::from_le_bytes(data[0..8].try_into().unwrap());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not something you need to fix, just making this comment so I can link to it later):

It'd be nice if the kernel exposed the virtual address of where it wants its UART so we can map that in and get this feature working again.


let mut boot_lvl2_lower: [u8; PAGE_TABLE_SIZE] = [0; PAGE_TABLE_SIZE];

let pt_entry = (boot_lvl2_lower_addr | 3).to_le_bytes();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we be consistent with the other examples and split out the meanings of the 3 flags into the individual bits?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This this is a 2 bit marker for the 1GB L1 table entry here, not individual bits

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, we do |3 in a bunch of places below. I'll change that at a later point.

Copy link
Copy Markdown
Author

@bruelc bruelc Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, better to handle it globally yes. I’ll add a comment since “3” means different things depending on the granule (page or table)

@bruelc bruelc force-pushed the master-mmuloader branch from 12dd82c to 9dbc0d7 Compare April 8, 2026 07:38
let pt_entry: u64 = (entry_idx as u64 + start_addr) |
(1 << 10) | // Access flag
(3 << 8) | // Sharable
(3 << 2) | // MT_NORMAL memory
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this more. Did you intentionally make this 3 << 2? That would correspond to MT_NORMAL_NC, not MT_NORMAL.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh no! That’s an early debugging trace… thanks for noticing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: Note that I also had to update #465 to flush the cache before jumping to the secondary core

Limit the 1:1 address mapping in the loader to cover only the
necessary blocks using level-2 translation tables and with 2MB blocks.
Don't map device memory at all, therefore, printf must not be used in
the elfloader after the MMU is enabled.

This prevents speculative accesses to device or secure memory which
otherwise would cause faults or unwanted side-effects.

Signed-off-by: Christian Bruel <christian.bruel@foss.st.com>
@bruelc bruelc force-pushed the master-mmuloader branch from 9dbc0d7 to b62c440 Compare April 10, 2026 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants