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

Add Host MacOS support #2804

Merged
merged 21 commits into from
Jun 24, 2024
Merged

Add Host MacOS support #2804

merged 21 commits into from
Jun 24, 2024

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Jun 16, 2024

This PR adds support for the Host Emulator to MacOS.

See #2773.

64-bit only

Standard compiler is 'Apple Clang', supports only 64-bit builds: no 32-bit compatibility.
GCC can be installed via brew install gcc and compiles as usual with the -m32 flag.
The GNU linker is absent from brew's coreutils package (see Homebrew/homebrew-core#17794) so the standard system linker must be used. This only supports 64-bit targets.
Apple have removed 32-bit application support from their ecosystem.

Advantages
No need for 32-bit compatibibility libraries in GNU/Linux.
For MacOS build and run host emulator using standard Apple tools, no GCC required.
General improvements in portability, less tolerant of incorrect type usage.

Disadvantages
Types such as long, size_t now 64-bit, don't match target hardware
Pointers are 64-bit so cannot be cast to 32-bit intrinsics. Code must use correct intptr_t types.
Must use ENABLE_STORAGE_SIZE64=1 as emulated flash addresses must be 64-bit.

Symbol wrapping

MacOS linker doesn't support symbol wrapping which is used by malloc_count Component to hook malloc, etc.
Don't currently have a solution for this so malloc_count is disabled for MacOS.

CI builds

Added MacOS builds plus running tests. For esp32 only IDF 5.2 is tested.

Notes

MacOS identified in makefiles with UNAME=Darwin.
In code, use #ifdef __APPLE__ when running on apple toolchains.
Also requires #ifdef __aarch64__ for Apple Silicon.
The rbpf library requires bpf target with clang. Not supported by apple clang - use brew clang.

I managed to get MacOS Ventura running in Qemu via https://github.com/kholia/OSX-KVM.
This runs in x86_64 mode, adequate for dev/testing.
The github actions runner macos-latest uses arm64, so introduces a few extra quirks.

Tools

  • Compiler. Standard compiler is 'Apple Clang', supports only 64-bit builds: no 32-bit compatibility. Doesn't support alias attribute so use linker to do that instead.

  • Archive. Standard ar does not support

  • Linker. GNU Linker not available. MacOS linker does not support symbol wrapping.

  • GDB. Can be installed with brew but requires some additional steps for security reasons. Separate lldb target added.

  • sed. GNU 'sed' tool has extensions which MacOS version does not support. Added SED build variable which should always point to GNU version gsed, but only for Darwin.

TODO:

  • Pull out fixes into separate PR, merge first
  • Make GNU/Linux Host build type selectable between 32/64-bit
  • Is there a solution to symbol wrapping for MacOS linker? Nope.
  • Fix interrupt simulation code - requires rewrite of signal layer
  • Add lldb support to framework
  • Fix/check macos installer
  • Reinstate full CI builds
  • How to create virtual TAP for MacOS? Document.
  • Review MacOS documentation

@slaff slaff added this to the 5.2.0 milestone Jun 17, 2024
@slaff
Copy link
Contributor

slaff commented Jun 18, 2024

How to create virtual TAP for MacOS? Document.

Take a look here:
https://groups.google.com/g/tunnelblick-discuss/c/v5wnQCRZ8HY/m/Q8nhFBx1BgAJ

@mikee47 mikee47 force-pushed the feature/macos branch 2 times, most recently from 78ecdc8 to 5c7e58d Compare June 19, 2024 14:52
@mikee47
Copy link
Contributor Author

mikee47 commented Jun 19, 2024

The networking works as follows. First, install tap extension:

sudo cp -r "$SMING_HOME/Arch/Host/Tools/macos/tap.kext" /Library/Extensions
sudo kextload /Library/Extensions/tap.kext

There's some security guff to manually deal with, then a reboot is required. (NB. this doesn't work in CI environment but gracefully fails.)

The Sming application must be run as sudo also. Changing permissions for /dev/tap0 doesn't help, still get network write failures. I've tested this with the HttpServer_ConfigNetwork sample, running

sudo out/Host/debug/firmware/app

Then browsing to 192.168.13.10.

This kernel extension creates The /dev/tap0 device. However, the actual tap0 interface is created dynamically when the device is opened, as can be illustrated below. This post was most helpful https://stackoverflow.com/questions/87442/virtual-network-interface-in-mac-os-x/31259641#31259641.

sudo su
exec 4<>/dev/tap0
ifconfig

So two questions remain:

  1. How to do this without requiring sudo? Slaff's post above suggests that the tunnelblick daemon does this sort of stuff, but that adds way more complexity than I'd like so haven't spend time on that.
  2. How to configure routing so we can access internet?

Ref. ntop/n2n#773

@mikee47 mikee47 force-pushed the feature/macos branch 8 times, most recently from a308f49 to dc0633c Compare June 21, 2024 12:01
@mikee47 mikee47 changed the title [WIP] Add Host MacOS support Add Host MacOS support Jun 21, 2024
@mikee47 mikee47 requested a review from slaff June 21, 2024 12:04
``Sming/Arch/Host/Tools/setup-network-linux.sh``. Here is the manual approach::
first, and requires root privilege.

For Linux, uou can use ``Sming/Arch/Host/Tools/setup-network-linux.sh``. Here is the manual approach::
Copy link
Contributor

Choose a reason for hiding this comment

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

uou can use -> you can use


Environment Variables
---------------------

Open with a text editor the ``.profile`` file in your home directory, and add these lines::

export ESP_HOME=/opt/esp-quick-toolchain
export SMING_HOME=<your-favourite-development-folder>/Sming/Sming
export SMING_HOME=opt/Sming/Sming
Copy link
Contributor

Choose a reason for hiding this comment

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

export SMING_HOME=opt/Sming/Sming

Either change this command to export SMING_HOME=/opt/Sming/Sming or revert this line.


Environment Variables
---------------------

Open with a text editor the ``.profile`` file in your home directory, and add these lines::

export ESP_HOME=/opt/esp-quick-toolchain
export SMING_HOME=<your-favourite-development-folder>/Sming/Sming
export SMING_HOME=opt/Sming/Sming

Make sure to replace ``<your-favourite-development-folder>`` in the
Copy link
Contributor

Choose a reason for hiding this comment

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

If you change it to /opt/Sming/Sming this sentence can be removed.

@mikee47 mikee47 force-pushed the feature/macos branch 2 times, most recently from b457ad5 to 6b456e8 Compare June 21, 2024 12:58
@mikee47
Copy link
Contributor Author

mikee47 commented Jun 21, 2024

@slaff There's still a couple of APPVEYOR references in Tools/ci/install.sh and Sming/Arch/Host/Tools/install.sh. The latter can go but the first one relates to deployment, which is your area... separate PR?

@slaff
Copy link
Contributor

slaff commented Jun 21, 2024

The latter can go but the first one relates to deployment, which is your area... separate PR?

I will take care of this next week in a separate PR.

mikee47 and others added 15 commits June 24, 2024 06:33
Fix stat64, lseek64, ftruncate64
Define _DARWIN_C_SOURCE
Only run disk image checks in Linux
Fix pthreads
Use SIGUSRn
Main loop signal support requires work (interrupt faking)
Use hard-coded IP settings
tap0 interface is created dynamically when /dev/tap0 is opened.
Still need to run with sudo permissions
How to configure routing for internet access?
If this function fails then flashmem fails to initialise and the emulator fails without explanation.
Therefore, add a suitable error message if this happens.
Implement gdb_do_break for arm64
Add lldb target
Update documentation
Detects arm64 and tries to use NEON optimisations.
No point in this since we want to exercise code which runs on actual devices.
du command doesn't have -b option in MacOS, but wc/awk is fine
installation of clang-format requires 'optional' option
ninja installed at top level
fix esp32 install
Fix shellcheck globbing warnings
Use arrays consistently
Make non-apple CLANG available for RBPF library
Don't change permissions on ``opt`` as the actual directory could be different (as per environment variables).
@slaff slaff removed the 3 - Review label Jun 24, 2024
@slaff slaff merged commit 9a097d8 into SmingHub:develop Jun 24, 2024
48 checks passed
@mikee47 mikee47 deleted the feature/macos branch June 24, 2024 10:46
@slaff slaff mentioned this pull request Jul 4, 2024
5 tasks
mikee47 added a commit to mikee47/Sming that referenced this pull request Jul 5, 2024
slaff pushed a commit that referenced this pull request Jul 5, 2024
Bad Rp2040 cyw43 firmware build bug, introduced in #2804.

Also ensure builds work with previous 10.3.1 toolchain.
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