|
| 1 | +--- |
| 2 | +title: Exploring x32 psABI for Erlang/OTP |
| 3 | +authors: [joaohf] |
| 4 | +tags: [meta-erlang, x32] |
| 5 | +--- |
| 6 | + |
| 7 | +According to [Wikipedia X32 ABI](https://en.wikipedia.org/wiki/X32_ABI) page: |
| 8 | + |
| 9 | +> The x32 ABI is an application binary interface (ABI) and one of the interfaces |
| 10 | +> of the Linux kernel. The x32 ABI provides 32-bit integers, long and pointers |
| 11 | +> (ILP32) on Intel and AMD 64-bit hardware. The ABI allows programs to take |
| 12 | +> advantage of the benefits of x86-64 instruction set (larger number of CPU |
| 13 | +> registers, better floating-point performance, faster position-independent |
| 14 | +> code, shared libraries, function parameters passed via registers, faster |
| 15 | +> syscall instruction) while using 32-bit pointers and thus avoiding the |
| 16 | +> overhead of 64-bit pointers. |
| 17 | +
|
| 18 | +So, I'm wondering if it would be possible to enable x32 support in Erlang/OTP |
| 19 | +build. That way, I could make a Yocto image for x32 that runs on x86-64 |
| 20 | +machines. |
| 21 | + |
| 22 | +Here is some references about the subject: |
| 23 | + |
| 24 | +- [Yocto, Using x32 psABI](https://docs.yoctoproject.org/dev-manual/x32-psabi.html) |
| 25 | +- [x32-abi](https://sites.google.com/site/x32abi/home?authuser=0) |
| 26 | +- [X32 – A Native 32bit ABI For X86-64](http://linuxplumbersconf.org/2011/ocw//system/presentations/531/original/x32-LPC-2011-0906.pptx) |
| 27 | +- [Debian x32 port](https://wiki.debian.org/X32Port) |
| 28 | +- [System V Application Binary Interface AMD64 Architecture Processor Supplement (With LP64 and ILP32 Programming Models](https://raw.githubusercontent.com/wiki/hjl-tools/x86-psABI/x86-64-psABI-1.0.pdf) |
| 29 | + |
| 30 | +In fact, x32 seems to be around since 2011/2012 and has been integrated in many |
| 31 | +platforms. Like Ubuntu, Debian, Gentoo. |
| 32 | + |
| 33 | +Enabling it for Yocto is simple, just adding the follow snippet in your |
| 34 | +local.conf file: |
| 35 | + |
| 36 | +``` |
| 37 | +MACHINE = "qemux86-64" |
| 38 | +DEFAULTTUNE = "x86-64-x32" |
| 39 | +baselib = "${@d.getVar('BASE_LIB:tune-' + (d.getVar('DEFAULTTUNE') or 'INVALID')) or 'lib'}" |
| 40 | +``` |
| 41 | + |
| 42 | +Then, building erlang: |
| 43 | + |
| 44 | +``` |
| 45 | +bitbake erlang |
| 46 | +``` |
| 47 | + |
| 48 | +:::note |
| 49 | + |
| 50 | +For those that want to check all configure and compiler flags, I'm including the |
| 51 | +links to those logs: |
| 52 | + |
| 53 | +- [Configure log output](log.do_configure) |
| 54 | +- [Compile log output](log.do_compile) |
| 55 | + |
| 56 | +::: |
| 57 | + |
| 58 | +The build failed in one point related to ASM code in |
| 59 | +[erts/lib_src/pthread/ethread.c](https://github.com/erlang/otp/blob/maint-26/erts/lib_src/pthread/ethread.c#L193). |
| 60 | +Looks like an ASM incompatibility issue. In order to address it here is |
| 61 | +[a patch](https://github.com/joaohf/otp/commit/6cd15d5888a536af97f5d8e26b2db2e379fa7eab) |
| 62 | +that just adds one more compiler check to pick up the correct ifdef branch. |
| 63 | + |
| 64 | +Afer that, the build runs as expected. And testing it using QEMU shows exactly |
| 65 | +what I had in mind: |
| 66 | + |
| 67 | +``` |
| 68 | +runqemu core-image-minimal-qemux86-64.ext4 slirp nographic serialstdio |
| 69 | +``` |
| 70 | + |
| 71 | +- check the current kernel |
| 72 | + |
| 73 | +``` |
| 74 | +root@qemux86-64:~# uname -a |
| 75 | +Linux qemux86-64 6.1.32-yocto-standard #1 SMP PREEMPT_DYNAMIC Mon Jun 5 13:43:33 UTC 2023 x86_64 GNU/Linux |
| 76 | +``` |
| 77 | + |
| 78 | +- check /proc/cpuinfo to see the 'lm' (long mode) |
| 79 | + |
| 80 | +``` |
| 81 | +root@qemux86-64:~# grep -o -w 'lm' /proc/cpuinfo |
| 82 | +lm |
| 83 | +``` |
| 84 | + |
| 85 | +- check Erlang shell |
| 86 | + |
| 87 | +``` |
| 88 | +Erlang/OTP 26 [erts-14.0.2] [source] [32-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] |
| 89 | +
|
| 90 | +Eshell V14.0.2 (press Ctrl+G to abort, type help(). for help) |
| 91 | +1> application:ensure_all_started(crypto). |
| 92 | +{ok,[crypto]} |
| 93 | +``` |
| 94 | + |
| 95 | +Well, looks like we are running Erlang/OTP 32-bits in a x86_64 machine. Also, it |
| 96 | +was able to correct load the crypto (with ssl libraries compiled for x32 too). |
| 97 | +By the way, there is a second |
| 98 | +[patch need to proper compile the crypto application](https://github.com/joaohf/otp/commit/e63b5b703ffa0005bf6a8f4d3bcec18f786bda92). |
| 99 | + |
| 100 | +Some raised questions for further investigations: |
| 101 | + |
| 102 | +- What tests are necessary to prove that the x32 Erlang build is safe ? |
| 103 | +- Are there any other code change in order to fit the x32 build ? |
| 104 | +- Would [BeamAsm](https://www.erlang.org/doc/apps/erts/beamasm#faq) be available |
| 105 | + for x32 ? |
0 commit comments