-
Notifications
You must be signed in to change notification settings - Fork 214
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
[RFC] Provide intrinsics necessary to get atomics working on the thumbv6m-none-eabi target #114
Comments
There's a problem with this implementation. This would always work with Cortex-M0 devices but Cortex-M0+ processors have this concept of privileged vs unprivileged execution mode. The instruction that disables/enables interrupts doesn't work when the processor is in unprivileged mode. That means that atomics based on enabling/disabling interrupts won't work (won't be actually atomic) if the processor is in unprivileged mode. cc @whitequark |
The libs team decided awhile back that when adding the various atomic types that if the platform didn't have support for them the standard library wouldn't export them. That is, we always felt pretty uncomfortable falling back to compiler-rt intrinsics to implement critical operations like atomics which can have serious effects on runtime functionality. That being said, however, if you do want the fallbacks as you've verified they work for you, then we don't have a great story for that. We perhaps definitely need to improve there! |
@alexcrichton My understanding of the lib team decision was that falling back to compiler-rt intrinsics was bad because those don't necessarily guarantee lock freedom. This means that they can result in incorrect behavior if mixed with signals for example. However if compiler-rt can provide atomic operations while still guaranteeing lock freedom then IMO this should be acceptable. One specific case I have in mind is atomic support for pre-ARMv6 Linux, where the kernel provides a |
For others targets that need a lock based solution, it seems that we could use a lang item. Perhaps add a This certainly needs a proper RFC. |
I don't think that lock-based solutions should be provided by the standard library. I have a crate which provides a generic |
Or this could be a stopgap perma-unstable solution until we come up with a better one. It depends on how much demand there is for this. An alternative for the actual problem at hand, people want to cross compile alloc / collections for the thumbv6m target, is to add an option to the alloc crate to not depend on any atomic; that would imply getting replacing the current OOM handling code with something else (I don't know with what tough) |
Yes, what I want to do is compile liballoc for Cortex-M0 (a Qualcomm/NXP/Freescale Kinetis KE06Z). |
@japaric It's not just the OOM handling code, liballoc also contains the implementation of |
@Amanieu yeah, |
I was curious, so I did a quick check with arm-none-eabi-gcc 5.2 and the _Atomic int type. #include <stdatomic.h>
_Atomic int g_lock = 0;
void test_function(void)
{
g_lock++;
} With a Cortex-M4 target, gcc emits |
@japaric my preference here would be to just add the appropriate cfg to the liballoc crate for now to get it to compile out |
@alexcrichton Alright. Sent rust-lang/rust#37492 implementing this approach. alloc and collections can be compiled for the thumbv6m with that change. |
This is quite an old bug at this point and I believe it's been sorted out, so closing. |
See https://reviews.llvm.org/D61052 for a related discussion. |
Digging this one up. I also need |
The built-in
thumbv6m-none-eabi
hasmax_atomic_width
set to0
because LLVM doesn't know how to lower atomic operations to actual instructions instead it lowers atomic operations to intrinsics like__sync_fetch_and_add_4
. The result is thatcore
doesn't expose theAtomic*
structs so thealloc
crate and any other crate that depends on it can't be compiled for this target.I propose we implement those intrinsics in this crate (
libcompiler-rt.a
provides these intrinsics on other architectures) and then change the definition of the thumbv6m target (max_atomic_width = 32
) to provide atomics incore
; that wayalloc
,collections
and other crates would become compilable for this target.This is the (incomplete) list of intrinsics that would need to be implemented:
__sync_fetch_and_add_4
__sync_lock_test_and_set_4
Their implementation would likely use locking by temporarily disabling the interrupts.
The alternative is to do the change in the target definition without implement the intrinsics. This pushes the task of implementing the intrinsics to the downstream users.
cc @alexcrichton @Amanieu @thejpster @whitequark
The text was updated successfully, but these errors were encountered: