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

Support for atomic bitwise operations in MSP430 PAC API #407

Closed
wants to merge 16 commits into from

Conversation

YuhanLiin
Copy link
Contributor

Typically, when setting or clearing bits of a register, RMW in the form of reg.modify(|r, w| ...) is needed, which can be interrupted. On the MSP430 architecture however there are instructions such as bis and xor that can modify specific bits of a memory location atomically. This change adds APIs to the register proxies to take advantage of those instructions. I also updated the MSP430 regression test cases, though they're still broken because MSP430 doesn't compile on stable.

As discussed on matrix, we don't want to merge this until after the MSP430 improvement patch goes in, so I'm uploading the PR for feedback.

@rust-highfive
Copy link

r? @reitermarkus

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-tools labels Dec 30, 2019
@cr1901
Copy link
Contributor

cr1901 commented Dec 31, 2019

FWIW, this looks fine to me. Will take a closer look after the msp430 improvements are merged.

Please keep in mind that msp430_atomics will never be stable because of inline asm (unless somehow we can get them into libcore...)

Additionally, why are your set_bits, clear_bits, and toggle_bits methods unsafe?

@YuhanLiin
Copy link
Contributor Author

The new API is unsafe because the bit patterns specified in the write proxy may not be equal to the bits in the actual register after the operation. For example, calling set_bits with write proxy of 11000000 on a register with value 00110000 results in a register value of 11110000. The resulting bit pattern may not be valid for the register, so the method is unsafe (same reason why reg.bits() is unsafe).
The stability thing is an issue. Hopefully we get libcore intrinsic support for the required instructions before building MSP430 is stabilized in its entirety.

@therealprof
Copy link
Contributor

What's the status of this?

@cr1901
Copy link
Contributor

cr1901 commented Apr 22, 2020

@therealprof No further movement on my end, and I just realized this PR as-is would conflict with getting msp430 to work completely on stable (since msp430-atomic uses feature(asm)), which is on my radar.

I would like to hear from @YuhanLiin before closing (and resubmitting) later, though.

@YuhanLiin
Copy link
Contributor Author

Haven't looked into this PR in a while, so it's probably out of date right now. The stability issue still stands, since there're no ASM intrinsics in MSP430 right now, and I don't see any workarounds at all. Closing with the intention of reopening later makes sense.

@burrbull
Copy link
Member

Is there any progress?

@burrbull burrbull marked this pull request as draft April 17, 2021 04:15
@YuhanLiin
Copy link
Contributor Author

The PR still relies on msp430-atomic, which requires the unstable llvm_asm feature. If that's still a concern then I don't see any way forward, since asm isn't going to be available on MSP430 anytime soon. Paging @cr1901 for more info.

@burrbull
Copy link
Member

The PR still relies on msp430-atomic, which requires the unstable llvm_asm feature. If that's still a concern then I don't see any way forward, since asm isn't going to be available on MSP430 anytime soon. Paging @cr1901 for more info.

As said one guy llvm_asm never be stable.
But you can put this under feature to hide.

@cr1901
Copy link
Contributor

cr1901 commented Apr 17, 2021

If that's still a concern then I don't see any way forward, since asm isn't going to be available on MSP430 anytime soon.

I think it's more accurate to say there are other blockers on getting msp430 stabilized (I have a list somewhere) that makes it low priority to swap from llvm_asm to asm across the core msp430 crates. That said, there is relatively little asm in the msp430 ecosystem:

  • msp430-atomic is one, because it's copied from libcore with changes that should be upstreamed eventually.
  • msp430-rt sidesteps the problem by compiling assembly to a static lib.
  • msp430 has llvm_asm because I considered the overhead of function calls for intrinsics unacceptable. -Clinker-plugin-lto can't be used because we're stuck with binutils.

I could change this all to asm if necessary, though I think we should hold off on this change for now personally, as I don't quite have the bandwidth to change even that much assembly.

In addition, I thought the "new" asm syntax was also unstable (but eventually will be stabilized)? Did that change?

@YuhanLiin
Copy link
Contributor Author

asm is still unstable. It also doesn't support the MSP430 architecture. The real question is, do we want code generated by svd2rust to rely on msp430-atomic (and by extension, llvm_asm)? If we do, then should that dependency be gated behind a feature flag?

@cr1901
Copy link
Contributor

cr1901 commented Apr 17, 2021

It also doesn't support the MSP430 architecture.

Oh great, that means it's gonna be involved to add, doesn't it?

The real question is, do we want code generated by svd2rust to rely on msp430-atomic (and by extension, llvm_asm)?

PACs generated with svd2rust already rely on nightly, unfortunately. So at first glance, it seems like it makes little difference to depend on more unstable features.

That said, just because I accept the consequences of using nightly doesn't mean other people need to. I still intend to create a stable path for msp430 code:

  • In principle, until msp430-interrupt is stabilized, it is possible to get rid of this dependency using a carefully coded interrupt handlers in asm that we link to. But I haven't implemented this yet.
  • The other asm can be removed using analogous object files/static archives we link to.
  • AtomicBool, strictly speaking, isn't necessary- Mutex<Cell<bool>> will do the same thing, with more code :).

If we do, then should that dependency be gated behind a feature flag?

I think so, as unstable features can allow extremely useful optimizations at the cost of being tied to nightly. I can put msp430-abi-interrupt behind a feature as well once a stable path exist. Call the feature atomic and maybe create an unstable feature that encompasses all the features?

@burrbull
Copy link
Member

unstable feature that encompasses all the features

I have the same opinion

@YuhanLiin YuhanLiin marked this pull request as ready for review April 18, 2021 16:07
@burrbull
Copy link
Member

cc @therealprof What do you think about?

@burrbull
Copy link
Member

burrbull commented May 8, 2021

@YuhanLiin instead of using compile time feature, you can use runtime --nightly option already present in svd2rust

@YuhanLiin
Copy link
Contributor Author

So should the atomics be gated purely by the nightly runtime flag or should there be a compile-time feature gating it as well, as per the previous discussions?

@burrbull
Copy link
Member

burrbull commented May 9, 2021

I think runtime flag is enough for this.

@burrbull
Copy link
Member

LGTM. Please, use rebase instead of merge and update changelog.

@YuhanLiin
Copy link
Contributor Author

How should I undo my previous merge?

@burrbull
Copy link
Member

bors bot added a commit that referenced this pull request May 10, 2021
520: Support for atomic bitwise operations in MSP430 PAC API  r=burrbull a=YuhanLiin

Rebased version of #407 with updated changelog.

Co-authored-by: YuhanLiin <linyuhan0315@hotmail.com>
Co-authored-by: YuhanLiin <yuhanliin+github@protonmail.com>
@YuhanLiin YuhanLiin closed this May 10, 2021
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-tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants