From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12045 Path: news.gmane.org!.POSTED!not-for-mail From: Michael Clark Newsgroups: gmane.linux.lib.musl.general Subject: RISC-V musl port and toolchain builder Date: Sun, 29 Oct 2017 18:14:39 +1300 Message-ID: <8B64E4E2-F2DA-431A-9F9E-474772A99073@mac.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 (Mac OS X Mail 11.0 \(3445.1.7\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1509254118 17389 195.159.176.226 (29 Oct 2017 05:15:18 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 29 Oct 2017 05:15:18 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-12058-gllmg-musl=m.gmane.org@lists.openwall.com Sun Oct 29 06:15:08 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1e8fvx-00027k-HM for gllmg-musl@m.gmane.org; Sun, 29 Oct 2017 06:14:53 +0100 Original-Received: (qmail 21576 invoked by uid 550); 29 Oct 2017 05:14:57 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 21533 invoked from network); 29 Oct 2017 05:14:56 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mac.com; s=04042017; t=1509254083; bh=DYFk2mZDCxaCPyXZjE7XMUtf9QqmjVl/DnkuO7+0DyM=; h=From:Content-type:MIME-version:Subject:Message-id:Date:To; b=bgaOdl8+DiWQvcv3fZKvuFCFRfbqUpYvy3/aqGNNkzwX1e3gXIpjlFbXOFeXuvEEa os9sPViNWCnB3d1Y4Wn/ApY5FU0ais6EuKT+l78NkkskDMNuZ556krZU2qwY19f5xc 3MyevyoiyZn41XXIxOR3s+BVSC/s7lDB/UYnP+I2k1HNBls4ww8a0j5c/KV+H7UM++ rDbm+EbsfeqLByfGb+aSs4zQe7TYXyzEUlmGHjlAbD4NDHmnG/Ps+Xi2lHyIBfAH8Q x9jG7g2h/fsmCVbfLZqZ1dMqU7CtNt4FOksuAZGMpo1sxmvpprSHdxY0CvmtnI8pHM bQSzQA/qj1bWg== X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-10-29_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1011 suspectscore=93 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1710290074 X-Mailer: Apple Mail (2.3445.1.7) Xref: news.gmane.org gmane.linux.lib.musl.general:12045 Archived-At: Hi Folks, A quick status update on the RISC-V musl port and building musl = toolchains for RISC-V. I posted parts of this update earlier to the RISC-V software development = mailing list but have since tagged a new toolchain builder with an = undocumented =E2=80=9Cnative-cross=E2=80=9D feature that allows building = native toolchains which are dynamically linked against the target=E2=80=99= s musl libc using the target=E2=80=99s cross compiler: - https://github.com/rv8-io/musl-riscv-toolchain/ - https://github.com/rv8-io/musl-riscv/ Some background: I picked up maintenance of a branch of musl-riscv from = a GSoC project. I did this to use musl libc in a toolchain for = cross-platform emulator benchmarking across 6 architectures: riscv32, = riscv64, i386, x86_64, arm and aarch64 [1] [2]. The bulk of the = musl-riscv porting was completed during the GSoC project however the = port needed some testing and a few updates to match ABI and toolchain = changes done during binutils and gcc upstreaming earlier in the year. = The port is now in a mostly usable state, with the exception of ELF = thread local storage (which I could use some help with). Shared = libraries are working and have been tested both in a full system = emulator running riscv-linux and in a user mode simulator. I=E2=80=99ve tagged a pre-release of musl-riscv along with a toolchain = build script that uses a snapshot of riscv-linux for installation of = linux headers: =E2=80=A2 = https://github.com/rv8-io/musl-riscv/releases/tag/v1.1.17-riscv-a5 =E2=80=A2 = https://github.com/rv8-io/musl-riscv-toolchain/releases/tag/v7.2.0-6 =E2=80=A2 = https://github.com/rv8-io/riscv-linux/releases/tag/linux-riscv-4.12-v7_0 We could switch to musl-cross-make however at the time I needed to use = gcc-7.1, latest binutils 2.28 for riscv support along with snapshots of = musl-riscv and linux-riscv and none of these were supported by = musl-cross-make at that time and I happened to have a toolchain build = script I was using privately. The other 4 architectures were added so = that I could compare code gen in compiler explorer [3]. The x86 32-bit = and 64-bit compilers are configured to tune for core2. The arm target is = armv7-a and the aarch64 target is armv8-a. I recently added a = =E2=80=9Cnative-cross=E2=80=9D feature to test native riscv gcc in = user-mode simulators such as qemu-riscv and rv8. The native cross build = needs some fine tuning as it currently shares the sysroot with the cross = toolchain but installs untransformed native tools into the = SYSROOT/usr/bin directory (where SYSROOT=3DPREFIX/TUPLE). I=E2=80=99d = like to figure out how to build the native toolchains with / as the = sysroot so the sysroot can be used as a chroot for use with a user-mode = simulator. I haven=E2=80=99t yet figured out how to make the run-time = sysroot differ from the build-time sysroot? Installation # tested on Debian Stretch - /opt/riscv is writable $ sudo apt-get install -y build-essential bison flex $ git clone https://github.com/rv8-io/musl-riscv-toolchain.git $ cd musl-riscv-toolchain $ sh bootstrap.sh riscv64 $ export PATH=3D$PATH:/opt/riscv/musl-riscv-toolchain-7.2.0-6/bin $ riscv64-linux-musl-gcc --version riscv64-linux-musl-gcc (GCC) 7.2.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. = There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A = PARTICULAR PURPOSE.=20 Alliteratively you can build all supported architectures like so: $ time ( for i in riscv32 riscv64 i386 x86_64 arm aarch64; do sh = bootstrap.sh $i ; done ) | tee build.log Note: the bootstrap script default to -j8 and building the 6 supported = architectures requires 17GB of space and will take about a couple hours = on an 8 core machine. Issues fixed since picking up GSoC musl-riscv branch: =E2=80=A2 gcc patch to set the musl dynamic linker name (ELF = interp) =E2=80=A2 patch is applied automatically by the = toolchain build script =E2=80=A2 /lib/ld-musl-riscv32.so.1 (-mabi=3Dilp32d, = default, hard float) =E2=80=A2 /lib/ld-musl-riscv64.so.1 (-mabi=3Dlp64d, = default, hard float) =E2=80=A2 /lib/ld-musl-riscv32-sf.so.1 (-mabi=3Dilp32, = soft float) =E2=80=A2 /lib/ld-musl-riscv64-sf.so.1 (-mabi=3Dlp64, = soft float) =E2=80=A2 /lib/ld-musl-riscv32-sp.so.1 (-mabi=3Dilp32f, = single precision) =E2=80=A2 /lib/ld-musl-riscv64-sp.so.1 (-mabi=3Dlp64f, = single precision) =E2=80=A2 fixed failing pthread tests. =E2=80=A2 a_cas was deadlocking (updated a_cas in = atomic_a.h, fixed missing inline asm constraint) =E2=80=A2 defined the minimal set of atomics required by = the musl library =E2=80=A2 fixed failing sigaltstack tests (update sigaltstack = and ucontext in signal.h) =E2=80=A2 fixed failing ipc_sem tests (added struct semid_ds in = sem.h) =E2=80=A2 fixed failing stat tests (defined blksize_t and = nlink_t in alltypes.h.in) =E2=80=A2 rename sigcontext __regs to gregs so that gcc would = compile =E2=80=A2 rename _gp to __global_pointer$ in the crt to work = with current binutils =E2=80=A2 change definition of long double to quadruple = precision =E2=80=A2 update syscalls.h.in to use asm-generic syscall = definitions =E2=80=A2 update stat.h to use asm-generic stat definition Remaining issues: =E2=80=A2 rebase to current musl-libc =E2=80=A2 check results of tests that are expected to fail = (compare with other architectures) =E2=80=A2 ELF thread local variables are not being initialised =E2=80=A2 tls_init test is failing =E2=80=A2 lower priority - atomics require fallback to syscall = if A extension is not present - need to check latest kernel ABI It looks like RISC-V might make it into linux 4.15 [4] I suspect we = should wait until we are in mainline linux before considering adding the = riscv arch directories to musl? I would also like to know how a new port should be divided up into = patches for submission. The history will need squashing as there is some = churn in the history. I don=E2=80=99t believe there are any changes = outside of arch/riscv32 and arch/riscv64 so its pretty low risk. We=E2=80=99ll need to address the ELF TLS issue. I have debugged it to = the point of confirming that it appears to be finding the TLS ELF = segments (i see calls to __copy_tls, memcpy) but new threads are not = having their thread locals initialised. tp is being set correctly = otherwise pthread_self and the other thread tests would not be working. = I have an additional patch to set DTP_OFFSET to use the same value as = glibc but that doesn=E2=80=99t appear to help. It needs more time with a = debugger to find out why the thread initialisation code is not copying = initial state for thread locals from .tdata --- a/arch/riscv64/pthread_arch.h +++ b/arch/riscv64/pthread_arch.h @@ -8,4 +8,6 @@ static inline struct pthread *__pthread_self() #define TLS_ABOVE_TP #define TP_ADJ(p) ((char *)p + sizeof(struct pthread) - 16) =20 +#define DTP_OFFSET 0x800 + #define MC_PC gregs[0] Michael. [1] https://carrv.github.io/papers/clark-rv8-carrv2017.pdf [2] https://rv8.io/bench [3] https://cx.rv8.io/ [4] https://www.phoronix.com/scan.php?page=3Dnews_item&px=3DRISC-V-Linux-V= 9=