Skip to content

Conversation

@haowuge
Copy link
Contributor

@haowuge haowuge commented Dec 5, 2024

This is the initial set up for LoongArch64.
The current plan is for this arch to remain experimental for FreeBSD 15.
This is our amateur learning project, and we will gradually improve it over time.

Regarding the modifications related to the contrib/llvm-project section,
my colleague will submit a pull request to the llvm-project later.
A simple GitHub Actions build test: https://github.com/haowuge/freebsd-src/actions/runs/12178365925/job/33968197600

If you have better suggestions for the code, please let me know,
and we are happy to improve it until compliant code is provided.

This is the initial set up for LoongArch64.
The current plan is for this arch to remain experimental for FreeBSD 15.
This is our amateur learning project, and we will gradually improve it over time.

(More to follow momentarily.)

Co-authored-by: yu shan wei <mpysw@vip.163.com>
Co-authored-by: xiaoqiang zhao <zxq_yx_007@163.com>
Signed-off-by: Haowu Ge <gehaowu@bitmoe.com>
@haowuge haowuge requested review from bapt and bsdimp as code owners December 5, 2024 11:21
@bsdimp
Copy link
Member

bsdimp commented Dec 5, 2024

So I've taken a quick look. Generally I like it.
However, it's done all as one big commit. There's at least two commits here. (1) all the Makefile related stuff and (2) Adding Loongson target support to clang/lld/etc.
I'll look at it in more detail to see if there's improvements I can suggest. Nothing stood out in my first, admittedly hasity, pass through the patch which is a good sign.

@brooksdavis
Copy link
Contributor

Somewhat echoing @bsdimp, I'd like to see a PR that adds general support and works with CROSS_TOOLCHAIN before we add build time to the default toolchain build. IMO in-tree compiler support ranks close to last in bringup priorities. Once we're a bit further along I'd be happy to add LoongArch to FREEBSD_BACKENDS in devel/llvm19.

@haowuge
Copy link
Contributor Author

haowuge commented Dec 7, 2024

Somewhat echoing @bsdimp, I'd like to see a PR that adds general support and works with CROSS_TOOLCHAIN before we add build time to the default toolchain build. IMO in-tree compiler support ranks close to last in bringup priorities. Once we're a bit further along I'd be happy to add LoongArch to FREEBSD_BACKENDS in devel/llvm19.

We originally hoped to merge into the basic system to reduce the pressure of tracking the main branch.

I will try to create LoongArch ports for LLVM 19, and this pull request will temporarily be turned into a draft,
thank you both.

@haowuge haowuge marked this pull request as draft December 7, 2024 14:11
@brooksdavis
Copy link
Contributor

I will try to create LoongArch ports for LLVM 19, and this pull request will temporarily be turned into a draft, thank you both.

You shouldn't need to do anything to make the existing devel/llvm19 package work as it includes all non-experimental backends. If you wanted llvm19-lite to work I'd need to add LoongArch to the list of FreeBSD arches, but that's mostly a long term CI thing.

@haowuge
Copy link
Contributor Author

haowuge commented Dec 12, 2024

I will try to create LoongArch ports for LLVM 19, and this pull request will temporarily be turned into a draft, thank you both.

You shouldn't need to do anything to make the existing devel/llvm19 package work as it includes all non-experimental backends. If you wanted llvm19-lite to work I'd need to add LoongArch to the list of FreeBSD arches, but that's mostly a long term CI thing.

We have several patches for loongarch FreeBSD, so the devel/llvm19 package needs to be patched.
we also made a simple devel/llvm19 ports patch:
0001-add-clang-LoongArch-Add-FreeBSD-targets-119191-patch.patch

We attempted to submit the patches to the LLVM project llvm/llvm-project#119191 , and the upstream suggested that FreeBSD should have more specialized personnel review it. Could you please help check if the patches meet the requirements?

Thank you.

@bsdimp
Copy link
Member

bsdimp commented Jun 11, 2025

So this issue came up at the Developer summit today. There was some interest in this port, but we had two questions: (1) is there a loongson qemu recipe we can use to test the loongson stuff? and (2) what's the loongsoon ecosystem look like in China? How many companies make this chip? How many sell the servers? Is it available in the US and Europe?

@bsdimp
Copy link
Member

bsdimp commented Jun 12, 2025

We attempted to submit the patches to the LLVM project llvm/llvm-project#119191 , and the upstream suggested that FreeBSD should have more specialized personnel review it. Could you please help check if the patches meet the requirements?

It looks like we were too slow on this, and things were merged... My apologies for not suggesting someone...

We're thinking that if we can create a Loongson qemu recipe we can test things with, and we test boot in qemu, we can land this patch, once it's updated. We'll do that just after the 15 branch and reevaluate where we are with the architecture and it's ecosystem when 16 is branched in a couple of years... this might be a good compromise to give the port a chance to mature before re@ starts producing releases for it. We can do daily snapshots to shake out installer bugs. If it's super popular, we can maybe MFC it...

@MingcongBai
Copy link

So this issue came up at the Developer summit today. There was some interest in this port, but we had two questions: (1) is there a loongson qemu recipe we can use to test the loongson stuff? and (2) what's the loongsoon ecosystem look like in China? How many companies make this chip? How many sell the servers? Is it available in the US and Europe?

Hi @bsdimp, happy to assist you with the questions above...

  1. It is possible to test LoongArch via QEMU, it now emulates a Loongson 3A5000 machine with vector instructions. Though this would be very slow on say an x86 machine. I'm happy to help you with SSH access (via the Loongson Community or possibly purchasing said hardware.

  2. Loongson for the past decade had been widely used in the government and state-owned enterprise space, but is now gaining strong community attention since 3A6000. What's more, LoongArch's software ecosystem has been by and large upstream-first. I'm writing to you from a Loongson machine running mainline Linux kernel and applications built from mostly upstream sources.

图片

For the state of community support and basic information on this architecture, I recommend taking a look at Areweloongyet?.

@haowuge
Copy link
Contributor Author

haowuge commented Jun 12, 2025

questions: (1) is there a loongson qemu recipe we can use to test the loongson stuff?

Hi @bsdimp, The LoongArchLinux ported by employees of Loongson (previously maintained internally and now handed over to the community, so it may not be the latest version) provides a QEMU image version. It can be started using the following methods:

BIOS: /usr/local/share/qemu/edk2-loongarch64-code.fd
wget https://mirror.iscas.ac.cn/loongarch/archlinux/images/archlinux-minimal-2024.04.23-loong64.qcow2.zst
or
wget https://mirror.iscas.ac.cn/loongarch/archlinux/images/archlinux-xfce4-2024.04.23-loong64.qcow2.zst

zstd -d archlinux-xfce4-2024.04.23-loong64.qcow2.zst

qemu-system-loongarch64 \
    -m 4G \
    -cpu la464-loongarch-cpu \
    -machine virt \
    -smp 4 \
    -bios /usr/local/share/qemu/edk2-loongarch64-code.fd \
    -serial stdio \
    -device virtio-gpu-pci \
    -net nic -net user \
    -device nec-usb-xhci,id=xhci,addr=0x1b \
    -device usb-tablet,id=tablet,bus=xhci.0,port=1 \
    -device usb-kbd,id=keyboard,bus=xhci.0,port=2 \
    -hda archlinux-xfce4-2024.04.23-loong64.qcow2

reference:
https://mirror.iscas.ac.cn/loongarch/archlinux/images/README.html

build command line

# cat /etc/src.conf
WITHOUT_SYSTEM_COMPILER=
WITHOUT_SYSTEM_LINKER=
WITHOUT_TESTS=
WITHOUT_RESCUE=


git clone -b hitmoon-RELEASE-p4 https://github.com/haowuge/freebsd-src
cd freebsd-src/
make -j6 TARGET=loongarch TARGET_ARCH=loongarch64 kernel-toolchain
make buildenv TARGET=loongarch TARGET_ARCH=loongarch64

cd stand
make 
mkdir -p /loongarch_rootfs/usr/share/man/man3 /loongarch_rootfs/usr/share/man/man5 /loongarch_rootfs/usr/share/man/man8
make install DESTDIR=/loongarch_rootfs
cd ..

make TARGET=loongarch TARGET_ARCH=loongarch64 buildkernel
make TARGET=loongarch TARGET_ARCH=loongarch64 DESTDIR=/loongarch_rootfs installkernel

The FreeBSD loader_simp.efi boot method:

zfs create -V 5g rpool/LoongFreeBSD0
gpart create -s GPT /dev/zvol/rpool/LoongFreeBSD0
gpart add -a 4k -s 512M -t efi /dev/zvol/rpool/LoongFreeBSD0
newfs_msdos -F 32 -c 1 /dev/zvol/rpool/LoongBSD0p1
mount_msdosfs /dev/zvol/rpool/LoongFreeBSD0p1 /mnt/LoongFreeBSD
download localer_simp.efi file to /mnt/LoongFreeBSD
umount /mnt/LoongFreeBSD

Start loader:

qemu-system-loongarch64 -m 1G -smp 4 \
 -bios /usr/local/share/qemu/edk2-loongarch64-code.fd \
 -serial stdio \
 -device virtio-gpu-pci \
 -device nec-usb-xhci,id=xhci,addr=0x1b \
 -device usb-tablet,id=tablet,bus=xhci.0,port=1 \
 -device usb-kbd,id=keyboard,bus=xhci.0,port=2 \
 -device virtio-blk-pci,drive=hd0 \
 -drive id=hd0,file=/dev/zvol/rpool/LoongFreeBSD0,if=none \
 --machine virt

loader

---<<BOOT>>---
Copyright (c) 1992-2023 The FreeBSD Project.
Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
       The Regents of the University of California. All rights reserved.
FreeBSD is a registered trademark of The FreeBSD Foundation.
FreeBSD 14.0-RELEASE-p4 #0 hitmoon-RELEASE-p4-n265421-e39eef800738: Fri Jun 13 17:05:57 CST 2025
   root@gehaowu.com:/usr/obj/home/ghw/CNPROJ/freebsd-src/loongarch.loongarch64/sys/GENERIC loongarch
FreeBSD clang version 17.0.6 (https://github.com/llvm/llvm-project.git llvmorg-17.0.6-0-g6009708b4367)
VT: init without driver.
CPU 0  : Vendor=Loongson Core=Loongson-64bit  
 marchid=0x2
real memory  = 1073672192 (1023 MB)
avail memory = 1035546624 (987 MB)
secondary cpu entry: 0x52042a0
secondary cpu entry: 0x52042a0
secondary cpu entry: 0x52042a0
FreeBSD/SMP: Multiprocessor System Detected: 4 CPUs
CPU 1  : Vendor=Loongson Core=Loongson-64bit  
CPU 2  : Vendor=Loongson Core=Loongson-64bit  
CPU 3  : Vendor=Loongson Core=Loongson-64bit  
arc4random: WARNING: initial seeding bypassed the cryptographic random device because it was not yet seeded and the knob 'bypass_befor
e_seeding' was enabled.

reference:
Source code repository: https://github.com/haowuge/freebsd-src/tree/hitmoon-RELEASE-p4
Binary image: https://github.com/haowuge/LoongArchFreeBSDRaw

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.

4 participants