Skip to content

riscv-rt: Leave __pre_init under a feature gate #282

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

Merged
merged 2 commits into from
May 7, 2025
Merged

Conversation

romancardenas
Copy link
Contributor

Still working on #247

The idea is to remove __pre_init from the default riscv-rt crate. If users want it, they must enable the pre-init feature.
If enabled, no default __pre_init symbol is provided, as we assume that users will develop their routine.

Additionally, the riscv_rt::pre_init attribute macro is now marked as deprecated, as running Rust code before RAM initialization is unsound, and assembly code is preferred.

@romancardenas romancardenas requested a review from a team as a code owner April 29, 2025 15:04
@romancardenas romancardenas force-pushed the pre-init branch 5 times, most recently from 69b56a2 to f7c74f8 Compare April 29, 2025 16:08
@@ -197,6 +204,7 @@ fn is_correct_type(ty: &Type, name: &str) -> bool {
///
/// # fn main() {}
/// ```
#[deprecated(note = "Use global_asm! to define the __pre_init function instead")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Will we be removing this in the next (or another future) point release?

I wasn't aware that using Rust code before RAM init was unsound. Is this true even for code that only uses the stack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about how Rust's ABI can break something. I guess that, if you do not use static variables, it should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From cortex-m documentation:

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

We also discussed this on Monday's meeting. I think the consensus was that there were too many preconditions + invariants to justify keeping a pre_init implementation in Rust.

I think it may still be helpful to point to the feature-gated __pre_init function in the riscv-rt docs as an example for users that would want to implement their own. Maybe in the place currently held by #[pre_init] or above the _pre_init_trap?

@romancardenas romancardenas added this pull request to the merge queue May 7, 2025
Merged via the queue into master with commit d7fd706 May 7, 2025
138 checks passed
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