oxidecomputer / oxidecomputer/hubris

Consider replacing `#[pre_init]` with `global_asm!`

Open
#2,572 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.6k
Forks
239
Avg merge
1d 12h
Merged PRs (30d)
23

Description

https://docs.rs/cortex-m-rt/0.7.5/cortex_m_rt/#features says:

A #[pre_init] macro is also provided to run a function before RAM initialisation, but its use is deprecated as it is not defined behaviour to execute Rust code before initialisation. It is still possible to create a custom pre_init function using assembly.

The macro has also been marked as deprecated in the upstream project. In https://github.com/oxidecomputer/hubris/pull/2571, I added a larger warning about this, but the main sticking point is that pre_init runs before memory is initialized, including zero-initialized .bss statics, as well as flash-copied .data statics. The Rust Abstract Machine requires that statics have been initialized prior to executing any Rust code, otherwise we would be able to observe uninitialized memory (in the form of statics) using only safe code.

Unfortunately, we DO need to run some custom code before the standard cortex-m-rt initialization code runs: we have to turn on the necessary RAM sections BEFORE they can be initialized! This is what the pre_init hook always existed to enable. Right now, we are very careful to avoid interacting with statics in this function, but it still leaves us in a somewhat grey area with regards to soundness, as we are running Rust code without statics being actually initialized.

The currently recommended behavior is to replace this "life before main" code with some form of assembly, as assembly does not have any soundness concerns of its own to worry about. As our current code only needs to peek and poke at a few registers, this should be fairly manageable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the current #[pre_init] hook and trace the startup entry point it uses before RAM initialization. Review how the hook enables RAM sections and accesses registers, then determine how that work can be represented with global_asm! while preserving initialization order and behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.