Skip to content

Conversation

carl-tud
Copy link
Contributor

@carl-tud carl-tud commented Jun 10, 2024

Update 10 April 2025:
#21283 is merged.

Update 10 March 2025:
This PR now builds upon #21283. The next PR will revive macOS support which this PR is the basis for.
You may look at the first approach here: native64-arm-old

Contribution Description.

This PR adds support for the native64 board on arm64/aarch641 Linux platforms (e.g., Raspberry Pi, Mac)

  • ISR functionality has been extended to work on 64-bit ARMv82
  • Since gcc cannot be compiled to support the -m32 and -m64 flags on ARM (?) (hence gcc-multilib is unavailable on ARM), -mXX flags are omitted when they would match getconf LONG_BIT

Testing procedure (GNU/Linux)

Compile and run on an ARM 64-bit CPU on Linux.

Footnotes

  1. Why? E.g., with 64-bit ARM support, you can use the native board on Apple Silicon Macs in a Linux-based virtualized environment (i.e., multipass (or qemu directly), Docker, Virtualization.framework or Hypervisor.framework), without having to resort to emulating a 32-bit ARM or x86 CPU.

  2. For anyone else also wondering how the PC can be set on ARM 64-bit, v8, while preserving all general-purpose registers: You simply can’t. On ARMv8, PC cannot be written to anymore, and branch instructions (and friends) either require a static address (which isn't available to us (stored in _native_saved_eip) or a register containing the destination address. If you look at the glibc implementation of setcontext (which lets you set the instruction pointer) in glibc's setcontext.S, you'll notice x16 is used as the register for the br instruction. This PR adopts the same technique. Edit 10 March 2025: You can set LR and ret there, so actually don't even need x16. This is also consistent with _setcontext.s in libplatform.

@github-actions github-actions bot added Platform: native Platform: This PR/issue effects the native platform Area: build system Area: Build system Area: cpu Area: CPU/MCU ports labels Jun 10, 2024
@Teufelchen1 Teufelchen1 self-requested a review June 11, 2024 11:08
@Teufelchen1
Copy link
Contributor

Cool! Let me know when you need a review. I can also aid in testing as I have an Apple Silicon ARM PC at hand.

@carl-tud carl-tud changed the title [DRAFT] Support native64 Board on ARM Support native64 Board on ARM Jun 27, 2024
@carl-tud carl-tud changed the title Support native64 Board on ARM cpu/native: Support native64 Board on ARM Jun 27, 2024
@carl-tud carl-tud marked this pull request as ready for review June 27, 2024 13:39
@carl-tud
Copy link
Contributor Author

@Teufelchen1 PR is ready for review :)

@carl-tud carl-tud requested a review from Teufelchen1 June 27, 2024 13:39
@Teufelchen1
Copy link
Contributor

Thanks for waiting.

I noticed a few things, maybe I have an invalid setup for testing or this PR need more work.
My setup:

  • MacOS Ventura 13.2.1
  • ARM64 / Apple M2
  • Docker
    • Ubuntu (latest) for --platform linux/arm64
  • Within docker, running BOARD=native64 make all term

Does that sound reasonable to you? Would you expect everything to work with the given setup?

Following things did not work as expected:

  • The ipc_pingpong example, indefinitely hangs after both threads are created.
  • The default example, crashes immediately after hitting an assertion within gnrc.
  • Building for 32 bit native does not work (the issue marked in your PR description). I think we should handle that case more gracefully.

If needed, I can also arrange a plain linux on ARM64 environment.

@carl-tud
Copy link
Contributor Author

carl-tud commented Jul 8, 2024

Thank‘s for testing. These issues definitely need to be resolved first, I’m going to try and figure out what’s going on there with these non-functional examples..

On ARM 64 bit, building for 32 bit platforms with gcc seems to be inherently impossible due to lacking gcc multilib support, at least to my knowledge.. Your setup seems fine and similar to mine.
(edit: haven’t tried compiling with clang)

What is it exactly you had in mind when you said »we should handle that case more gracefully«? A pretty error message with a reference to RIOT‘s doc perhaps..

carl

@carl-tud carl-tud force-pushed the native64-arm branch 2 times, most recently from 0419f5e to 8beb57c Compare August 12, 2024 14:06
@chrysn
Copy link
Member

chrysn commented Sep 3, 2024

This branch also works for the Raspberry Pi devices used by for the exercises. Skimming the discussion I'm afraid we won't get this merged by tomorrow so it could go into this summit's tutorials :-)

Could you add a branch to cpu/native/Makefile.include already in this PR so that on matching platforms, the RUST_TARGET can be aarch64-unknown-linux-gnu? (You can verify that by building and running examples/rust-gcoap or any other Rust example).

@benpicco benpicco added the State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet label Sep 24, 2024
@OlegHahm
Copy link
Member

OlegHahm commented Dec 4, 2024

Any update on this? I think it would be super cool to have native support on a RPi.

@carl-tud carl-tud marked this pull request as draft December 14, 2024 10:57
@carl-tud
Copy link
Contributor Author

carl-tud commented Dec 14, 2024

Any update on this? I think it would be super cool to have native support on a RPi.

There‘s still some investigating needed to find out why some of the examples fail.. also, networking with gnrc does not work for me (maybe a threading issue?). Perhaps the ISR logic can‘t be that naively ported to arm from x86? I still want to get arm64 support working eventually, but I don’t have the time at the moment. If somebody wants to dig into this or has some insights to share as to why this isn’t working as it should, your help would be greatly appreciated 🚀.

@carl-tud carl-tud changed the title cpu/native: Support native64 Board on ARM cpu/native: Support native CPU on arm64 Linux Mar 10, 2025
@github-actions github-actions bot added the Area: sys Area: System label Mar 10, 2025
@carl-tud carl-tud marked this pull request as ready for review March 10, 2025 12:16
@carl-tud carl-tud requested a review from kaspar030 as a code owner March 10, 2025 12:16
@carl-tud carl-tud changed the title cpu/native: Support native CPU on arm64 Linux cpu/native: DNM: Support native CPU on arm64 Linux Mar 10, 2025
@carl-tud
Copy link
Contributor Author

carl-tud commented Mar 10, 2025

Figured it out last week: ucontext_t has a PC field which is ignored on arm64 (see glibc and libplatform). This makes sense given PC cannot even be set in assembly. Instead, you need to set LR. That's the C part. Given the freedom of assembly, you can decide between blr/br x16 (x16 contains addr) and ret (lr=x30 contains addr).

Also, when setting ints from assembly, you'll need to use w0 (32-bit reg view) instead of x0 (64-bit register) or otherwise you'll corrupt the 3 bytes following the first 32-bit integer value.

@Teufelchen1 can you check, please?

@github-actions github-actions bot added the Area: drivers Area: Device drivers label Mar 10, 2025
@carl-tud
Copy link
Contributor Author

If Murdock looks at this PR, the result will actually just indicate nothing has been broken but won't tell us if native on arm64 works, right? Because the CI currently does not know about arm64, does it?

@carl-tud
Copy link
Contributor Author

@chrysn why does

ifneq (,$(filter arch_32bit,$(FEATURES_USED)))

use arch_32bit and FEATURES_USED instead of NATIVE_ARCH_BIT (which is either 32 or 64)?

That's the part that sets the Rust target triple

@Teufelchen1
Copy link
Contributor

Looks super promising! Overall review is very cluttered, I will go through it by commits then. When do you need my review? Are you blocked or can I take my time?

@Teufelchen1
Copy link
Contributor

Ah no, 21283 has been merged, if you rebase, the PR should shrink and look rather clean! Neat!

@github-actions github-actions bot removed Area: drivers Area: Device drivers Area: sys Area: System labels Mar 24, 2025
@carl-tud carl-tud changed the title cpu/native: DNM: Support native CPU on arm64 Linux cpu/native: Support native CPU on arm64 Linux Mar 24, 2025
@carl-tud
Copy link
Contributor Author

Rebased. #21289 builds on this, but does not block me.

@broglep
Copy link

broglep commented Jul 25, 2025

I have successfully tested this on a Raspberry Pi Zero 2 W (with few examples & driver tests, gnrc_networking, spi, sx127x)

@crasbe crasbe added Type: new feature The issue requests / The PR implemements a new feature for RIOT CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jul 28, 2025
@riot-ci
Copy link

riot-ci commented Jul 28, 2025

Murdock results

✔️ PASSED

df84d12 cpu/native: add support for aarch64/arm64 on Linux

Success Failures Total Runtime
10547 0 10547 12m:14s

Artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: cpu Area: CPU/MCU ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: native Platform: This PR/issue effects the native platform State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants