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

asm/inline: explicitly use asm macro #372

Merged
merged 8 commits into from
Dec 19, 2021

Conversation

jordens
Copy link
Contributor

@jordens jordens commented Dec 16, 2021

asm!() removed from prelude in current nightly
rust-lang/rust#91728

close #371

This is also a good candidate for the cortex-m v0.7 series.

removed from prelude in current nightly
rust-lang/rust#91728

close rust-embedded#371

Signed-off-by: Robert Jördens <[email protected]>
@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @therealprof (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cortex-m labels Dec 16, 2021
@adamgreig
Copy link
Member

LGTM, any reason it's still marked as draft?

@jordens jordens marked this pull request as ready for review December 16, 2021 17:57
@jordens jordens requested a review from a team as a code owner December 16, 2021 17:57
@jordens
Copy link
Contributor Author

jordens commented Dec 16, 2021

Just that I haven't actually been able to test it with the affected new nightly...

@TDHolmes
Copy link
Contributor

Looks like it should be core::asm?

error[E0432]: unresolved import `core::arch::asm`
149
  --> asm/inline.rs:10:5
150
   |
151
10 | use core::arch::asm;
152
   |     ^^^^^^^^^^^^^^^ no `asm` in `arch`
153
   |
154
   = note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined
155
help: a macro with this name exists at the root of the crate
156
   |
157
10 | use core::asm;
158
   |     ^^^^^^^^^

@jordens
Copy link
Contributor Author

jordens commented Dec 16, 2021

@adamgreig good to go now.

adamgreig
adamgreig previously approved these changes Dec 17, 2021
Copy link
Member

@adamgreig adamgreig left a comment

Choose a reason for hiding this comment

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

Thanks!

bors merge

bors bot added a commit that referenced this pull request Dec 17, 2021
372: asm/inline: explicitly use asm macro r=adamgreig a=jordens

`asm!()` removed from prelude in current nightly
rust-lang/rust#91728

close #371

This is also a good candidate for the cortex-m v0.7 series.

Co-authored-by: Robert Jördens <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 17, 2021

Build failed:

@adamgreig
Copy link
Member

Ah, the pinned nightly we use to build the pre-built binaries is too old to know about core::arch::asm. Probably fine to simply bump the version in rust-toolchain.toml but it would be good to check and understand any differences in the generated asm.

@TDHolmes
Copy link
Contributor

I think every user of cortex_m::asm::inline will need to do use core::arch::asm with this approach. Should we just change all calls to asm! to core::arch::asm! in inline.rs?

@jordens
Copy link
Contributor Author

jordens commented Dec 17, 2021

@adamgreig do you prefer a minimal bump or a maximal bump to 2021-12-16?
It doesn't look good.

--- 2020-08-20-lib      2021-12-17 08:21:37.685375537 +0100
+++ 2021-12-16-lib      2021-12-17 08:26:04.931123275 +0100
@@ -6,229 +6,291 @@
 Disassembly of section .text.__bkpt:

 00000000 <__bkpt>:
-   0:  be00            bkpt    0x0000
-   2:  4770            bx      lr
+   0:  b580            push    {r7, lr}
+   2:  466f            mov     r7, sp
+   4:  be00            bkpt    0x0000
+   6:  bd80            pop     {r7, pc}

 Disassembly of section .text.__control_r:

 00000000 <__control_r>:
-   0:  f3ef 8014       mrs     r0, CONTROL
-   4:  4770            bx      lr
+   0:  b580            push    {r7, lr}
+   2:  466f            mov     r7, sp
+   4:  f3ef 8014       mrs     r0, CONTROL
+   8:  bd80            pop     {r7, pc}

 Disassembly of section .text.__control_w:

 00000000 <__control_w>:
-   0:  f380 8814       msr     CONTROL, r0
-   4:  f3bf 8f6f       isb     sy
-   8:  4770            bx      lr
+   0:  b580            push    {r7, lr}
+   2:  466f            mov     r7, sp
+   4:  f380 8814       msr     CONTROL, r0
+   8:  f3bf 8f6f       isb     sy
+   c:  bd80            pop     {r7, pc}

 Disassembly of section .text.__cpsid:

 00000000 <__cpsid>:
-   0:  b672            cpsid   i
-   2:  4770            bx      lr
+   0:  b580            push    {r7, lr}
+   2:  466f            mov     r7, sp
+   4:  b672            cpsid   i
+   6:  bd80            pop     {r7, pc}

...

@jordens
Copy link
Contributor Author

jordens commented Dec 17, 2021

I'll try a minimal bump.

@adamgreig
Copy link
Member

Oh jeez, the new assembly doesn't look good indeed. I wonder what changed there.

@jordens
Copy link
Contributor Author

jordens commented Dec 17, 2021

There is no nightly that has asm!() in the location that is in now but does not do that stack dance by default.
options(nostack) helps and looks like it's the right approach. But somebody with deeper ARM/Thumb machine knowledge needs to make the analysis where that can actually be used safely. It's a bunch of functions.

@adamgreig
Copy link
Member

Thanks for checking, I'd just been going down that path myself. Adding suitable nomem and nostack options to the asm calls gets us identical disassembly output so looks like it should be OK and we can then bump to the latest nightly. I've made the changes but will just check them over now. OK for me to push a commit to this PR for review?

@jordens
Copy link
Contributor Author

jordens commented Dec 17, 2021

Absolutely! Thanks!

@adamgreig
Copy link
Member

Hm, github is suggesting I can add more commits by pushing to quartiq/cortex-m, but I get permission denied when I try to actually do that. Anyway, it's this commit.

@rust-embedded/cortex-m, anyone else able to check over the new nostack, nomem, preserves_flags options? I think it's mostly straightforward (nomem means we don't read/write memory, but almost all our asm routines are just modifying registers or special registers; nostack means we don't read/write stack which again is almost always the case, preserves_flags means no status-modifying instructions (like the 'S' variants 'ADDS' etc)).

The reference for what the options mean is here.

With this change the disassembly output is identical to what we have now (phew).

@jordens
Copy link
Contributor Author

jordens commented Dec 17, 2021

Feel free to hijack this (close, reopen with your branch). Alternatively, I can just pull your changes.
I went ahead and pulled them in here.

@jordens
Copy link
Contributor Author

jordens commented Dec 19, 2021

I went through the functions and checked their behavior w.r.t. the options constraints. Looks correct AFAICT.
Also and explicitly "Unless the nostack option is set ... The stack pointer must be restored to its original value before leaving the asm block." implying that to mess with the stack pointer nostack is required.

Copy link
Member

@adamgreig adamgreig left a comment

Choose a reason for hiding this comment

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

I took that to mean "without nostack, you may use the stack, but you must restore SP to its original value before finishing", but without the further implication that "if you do specify nostack, it's fine to mess with the stack pointer".

Anyway, good enough for now, thanks for the PR and checking the options!

bors merge

@bors
Copy link
Contributor

bors bot commented Dec 19, 2021

Build succeeded:

@bors bors bot merged commit 441cb87 into rust-embedded:master Dec 19, 2021
@jordens
Copy link
Contributor Author

jordens commented Dec 19, 2021

It just says that with nostack it may be fine to mess with the stack pointer. Without it you have to undo what you did.

@jordens jordens deleted the fix-inline-asm branch December 19, 2021 21:14
adamgreig added a commit that referenced this pull request Dec 31, 2021
372: asm/inline: explicitly use asm macro r=adamgreig a=jordens

`asm!()` removed from prelude in current nightly
rust-lang/rust#91728

close #371

This is also a good candidate for the cortex-m v0.7 series.

Co-authored-by: Robert Jördens <[email protected]>
Co-authored-by: Adam Greig <[email protected]>
@adamgreig adamgreig mentioned this pull request Dec 31, 2021
bors bot added a commit that referenced this pull request Jan 2, 2022
375: Prepare v0.7.4 r=thejpster a=adamgreig

I've created a new branch, `v0.7.x`, which is currently at the latest non-breaking commit (so includes #346 #349 #347 #351 #339 #352 #348 #363 #362 #361 but does not include #342), to track the 0.7 series since master now contains breaking changes for v0.8.

This PR (which targets the new branch) cherry-picks #372 #369 #374 and bumps the version to v0.7.4 (and updates CHANGELOG) ready for a new v0.7.4 release. Once complete I'll also backport the changelog entries and bump the version in master to 0.7.4.

I think this is everything that should be in 0.7 -- the only excluded PRs from master are #342 and #367 I believe, and I don't think we have any open PRs targeting 0.7 either.

Any other thoughts on items for inclusion in 0.7.4 (or other changelog entries I missed)?

Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
Co-authored-by: Adam Greig <[email protected]>
yvt added a commit to r3-os/r3 that referenced this pull request Jan 3, 2022
This release includes [rust-embedded/cortex-m#372][1], which is
necessary for compatibility with the current nightly compiler.

[1]: rust-embedded/cortex-m#372
@jeffy1009
Copy link

If compiler_fence prevents compiler from moving memory load/stores across it, and nomem tells the compiler that the asm block doesn't perform memory operations, isn't it theoretically possible that the asm block is moved across the compiler_fence?

  1. Is it impossible to happen?
  2. or is it possible (there is no such checks in the compiler), but the compiler doesn't really reorder asm and compiler_fence in such ways according to how it's implemented?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cortex-m
Projects
None yet
Development

Successfully merging this pull request may close these issues.

inline-asm broken on recent nightly
6 participants