Page MenuHomeFreeBSD

libc: add LoongArch support
Needs ReviewPublic

Authored by zhaoxiaoqiang007_gmail.com on Tue, Sep 22, 2:24 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Oct 13, 3:48 AM
Unknown Object (File)
Sun, Oct 11, 3:47 PM
Unknown Object (File)
Thu, Oct 8, 6:40 PM
Unknown Object (File)
Thu, Oct 8, 7:31 AM
Unknown Object (File)
Wed, Oct 7, 8:42 PM
Unknown Object (File)
Wed, Oct 7, 6:02 PM
Unknown Object (File)
Wed, Oct 7, 3:23 AM
Unknown Object (File)
Tue, Oct 6, 1:22 AM

Details

Summary

Add libc/loongarch (setjmp, makecontext, FP helpers, MD headers) and
wire loongarch into the libc, libcompiler_rt and libgcc_s build glue.

Signed-off-by: Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
Signed-off-by: yu shan wei <mpysw@vip.163.com>

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 77711
Build 74594: arc lint + arc unit

Event Timeline

I would doublecheck with the original copyright holders for some of the code (@br , @kp ) about some of the questions/comments I made in the Differential.

If you could also please add SPDX-License-Identifiers to new code, that would be extremely helpful to committers.

(the contrib/netbsd-tests changes look ok BTW)

contrib/netbsd-tests/lib/libc/sys/t_getcontext.c
131–132

Grammar nit (Oxford comma).

lib/libc/loongarch/gen/_ctx_start.S
2–34

Should the licensing be updated here?

@emaste, @imp: I could be wrong (not a lawyer), but is carrying the DARPA/AFRL contract licensing advertisement forward necessary with the BSD-2-Clause license?

lib/libc/loongarch/gen/fpgetmask.c
6–8

Similar comment here (and elsewhere where this pattern is used) with the advertisement claim.

lib/libc/loongarch/gen/fpsetmask.c
36

"Loongarch" should probably be used here instead of "RISC-V".

lib/libc/loongarch/gen/_ctx_start.S
2–34

This file was basically rewritten from scratch with the same copyright. The elements of this file that are the same have no copyright protection because they are required for the interface. I'd ditch the copyright on this file and use your own. I noticed that in a lot of places, but wanted to see how well this worked before taking the time to give that feedback.

That makes the DARPA and higher ed grant questions moot: they didn't fund any of this...

lib/libc/loongarch/gen/_ctx_start.S
2–34

okay

lib/libc/loongarch/gen/fpsetmask.c
36

Yes

shipujin.t_gmail.com added inline comments.
lib/libc/loongarch/gen/_ctx_start.S
38

comment is not very clear; it is suggested to change it to:

	jirl	$ra, $s0, 0	/* call func; return here on completion */
40–41

bl has a limited branch range (~±128 MB). While ctx_done and abort are likely in range here, call36 is the safer and more conventional choice for LoongArch function calls in libc, and it matches the upstream RISC-V use of call. Suggest replacing both bls with call36.

LLVM's LoongArch support includes the call36 : llvm/lib/Target/LoongArch/Asm/LoongArchAsmParser.cpp

	call36	_C_LABEL(ctx_done)
	call36	_C_LABEL(abort)
lib/libc/loongarch/gen/fabs.S
42–48

This introduces a soft-float branch in libc that will never be compiled, and the ABI assumptions of this dead-code branch conflict with the current compilation environment.

In https://reviews.freebsd.org/D59891, bsd.cpu.mk uses lp64d, which introduces a soft-float branch in libc that will never be compiled.

Suppose the else branch is taken: a double argument would be in $a0. But under the lp64d ABI, a double argument is actually in $fa0, so this code operates on the wrong register.

Suggested change to:

	fabs.d	$fa0, $fa0
	ret
lib/libc/loongarch/gen/flt_rounds.c
54–56

This introduces a non-functional soft-float branch in libc; the soft-float check here should be removed.

58

Remove

lib/libc/loongarch/gen/setjmp.S
86–89

What setjmp/longjmp saves are the callee-saved registers, not the floating-point environment. fcsr0 also contains exception status bits, and restoring the whole register would wipe out floating-point exceptions raised after setjmp, so it is suggested to remove this.

153–156

What setjmp/longjmp saves are the callee-saved registers, not the floating-point environment. fcsr0 also contains exception status bits, and restoring the whole register would wipe out floating-point exceptions raised after setjmp, so it is suggested to remove this.

164

Suggested change to:

ret
contrib/netbsd-tests/lib/libc/sys/t_getcontext.c
131–132

will fix in next reversion

lib/libc/loongarch/gen/fabs.S
42–48

will remove the macro check

lib/libc/loongarch/gen/setjmp.S
86–89

right, thanks!

So I've gone through and tagged a number of copyright issues. There's two reasons for this. First, we prefer that the new SPDX-License-Identifier form be used for the license texts, where possible. And second, in the past we've not been good about requiring that copyrights be completely replaced when the file contents are completely replaced. This makes it hard to trace the actual copyright owner. This causes confusion. And it's factually inaccurate. There's a number of copyrights that have been propagated form either early NetBSD code or the early arm code that no longer contain any code from the original authors. This causes confusion.

contrib/netbsd-tests/lib/libc/gen/t_dir.c
138 ↗(On Diff #187411)

So this should be a list of things we HAVE sbrk on, since it's been obsoleted: armv7, amd64, and powerpc*. And we likely want a #define for HAS_SBRK or similar and test against that here, and below.

lib/libc/loongarch/_fpmath.h
5

Since this file is all new, it would be better to just have Xiaoqiang's copyright.
Also, we're requesting new files use the new format:

/*
  * Copyright (c) 2024 Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
lib/libc/loongarch/arith.h
3

Needs a copyright, similar to what I suggested in _fpmath.h

lib/libc/loongarch/gd_qnan.h
3

Needs a copyright, similar to what I suggested in _fpmath.h

lib/libc/loongarch/gen/_ctx_start.S
2–34

Yes. This should instead use a copyright, similar to what I suggested in _fpmath.h

lib/libc/loongarch/gen/fabs.S
3

This copyright should be replaced with something similar to what I suggested in _fpmath.h.

lib/libc/loongarch/gen/flt_rounds.c
4

This copyright should be replaced with something similar to what I suggested in _fpmath.h. It's replaced all the riscv content.

lib/libc/loongarch/gen/fpgetmask.c
3

I think this copyright should be replaced with something similar to what I suggested in _fpmath.h.
But honestly, there's no copyrightable code left. But better to be safe with a copyright.

lib/libc/loongarch/gen/fpsetmask.c
3

This copyright should be replaced with something similar to what I suggested in _fpmath.h.

lib/libc/loongarch/gen/infinity.c
3

This needs a copyright similar to what I suggested in _fpmath.h.

lib/libc/loongarch/gen/makecontext.c
3

This copyright should be replaced with something similar to what I suggested in _fpmath.h.

lib/libc/loongarch/gen/setjmp.S
3

This copyright should be replaced with something similar to what I suggested in _fpmath.h.

lib/libc/loongarch/gen/sigsetjmp.S
3

This copyright should be replaced with something similar to what I suggested in _fpmath.h.

lib/libc/tests/sys/Makefile
6 ↗(On Diff #187411)

This list should be inclusive (eg all the ports we have sbrk) rather than exclusive.

lib/libc/loongarch/_fpmath.h
5

It's much more clear for me.

  1. rebase main
  2. copyright fixup as suggested by @imp
  3. sbrk test conditional build refinement, as separate differential:

https://reviews.freebsd.org/D60357, https://reviews.freebsd.org/D60358

  1. misc fixes suggested by @shipujin.t_gmail.com