Skip to content

Commit c5048c4

Browse files
authored
Merge pull request #109 from nathanchance/loongarch
Initial LoongArch support
2 parents 8ffe148 + 09087cf commit c5048c4

11 files changed

+261
-36
lines changed

boot-qemu.py

+32
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'arm32_v7',
2222
'arm64',
2323
'arm64be',
24+
'loongarch',
2425
'm68k',
2526
'mips',
2627
'mipsel',
@@ -476,6 +477,35 @@ def __init__(self):
476477
self._initrd_arch = 'arm64be'
477478

478479

480+
class LoongArchQEMURunner(QEMURunner):
481+
482+
def __init__(self):
483+
super().__init__()
484+
485+
self.cmdline.append('console=ttyS0,115200')
486+
self._default_kernel_path = Path('arch/loongarch/boot/vmlinuz.efi')
487+
self._initrd_arch = 'loongarch'
488+
489+
bios = Path(utils.BOOT_UTILS, 'images', self._initrd_arch,
490+
'edk2-loongarch64-code.fd')
491+
if not bios.exists():
492+
firmware_url = f"https://github.com/loongson/Firmware/raw/main/LoongArchVirtMachine/{bios.name}"
493+
utils.green(
494+
f"Downloading LoongArch firmware from {firmware_url}...")
495+
curl_cmd = ['curl', '-LSs', '-o', bios, firmware_url]
496+
subprocess.run(curl_cmd, check=True)
497+
498+
self._qemu_arch = 'loongarch64'
499+
self._qemu_args += [
500+
'-M', 'virt',
501+
'-cpu', 'la464',
502+
'-bios', bios,
503+
'-no-reboot',
504+
] # yapf: disable
505+
self._ram = '2G'
506+
self.smp = 2
507+
508+
479509
class M68KQEMURunner(QEMURunner):
480510

481511
def __init__(self):
@@ -699,6 +729,7 @@ def guess_arch(kernel_arg):
699729
'ELF 64-bit MSB executable, ARM aarch64': 'arm64be',
700730
'ELF 64-bit LSB pie executable, ARM aarch64': 'arm64',
701731
'ELF 64-bit MSB pie executable, ARM aarch64': 'arm64be',
732+
'ELF 64-bit LSB executable, LoongArch': 'loongarch',
702733
'ELF 32-bit MSB executable, Motorola m68k, 68020': 'm68k',
703734
'ELF 32-bit MSB executable, MIPS, MIPS32': 'mips',
704735
'ELF 32-bit LSB executable, MIPS, MIPS32': 'mipsel',
@@ -802,6 +833,7 @@ def parse_arguments():
802833
'arm32_v7': ARMV7QEMURunner,
803834
'arm64': ARM64QEMURunner,
804835
'arm64be': ARM64BEQEMURunner,
836+
'loongarch': LoongArchQEMURunner,
805837
'm68k': M68KQEMURunner,
806838
'mips': MIPSQEMURunner,
807839
'mipsel': MIPSELQEMURunner,

buildroot/buildroot-2022.02.tar.gz.sha256

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
44cced15a58505c93a87b215a632eba25f65560da64782fea50111f62b5ae86c buildroot-2023.02.2.tar.gz

buildroot/loongarch.config

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BR2_loongarch64=y
2+
BR2_BINUTILS_VERSION_2_39_X=y
3+
BR2_GCC_VERSION_12_X=y
4+
BR2_TARGET_GENERIC_ROOT_PASSWD="root"
5+
BR2_TARGET_GENERIC_GETTY_PORT="ttyS0"
6+
BR2_ROOTFS_OVERLAY="../overlay-reboot"
7+
BR2_TARGET_ROOTFS_CPIO=y
8+
# BR2_TARGET_ROOTFS_TAR is not set

buildroot/ppc64le.config

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
BR2_powerpc64le=y
2-
BR2_powerpc_power8=y
32
BR2_TOOLCHAIN_EXTERNAL=y
43
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_POWERPC64LE_POWER8_GLIBC_STABLE=y
54
BR2_TARGET_GENERIC_ROOT_PASSWD="root"

buildroot/rebuild.py

+23-30
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import shutil
88
import subprocess
99

10-
BUILDROOT_VERSION = '2022.02'
10+
BUILDROOT_VERSION = '2023.02.2'
1111
SUPPORTED_ARCHES = [
1212
'arm64',
1313
'arm64be',
1414
'arm',
15+
'loongarch',
1516
'm68k',
1617
'mips',
1718
'mipsel',
@@ -72,16 +73,17 @@ def build_image(architecture, edit_config):
7273

7374

7475
def download_and_extract_buildroot():
76+
if SRC_FOLDER.exists():
77+
shutil.rmtree(SRC_FOLDER)
7578
SRC_FOLDER.mkdir(parents=True)
7679

7780
tarball = Path(ROOT_FOLDER, f"buildroot-{BUILDROOT_VERSION}.tar.gz")
78-
tarball.unlink(missing_ok=True)
79-
80-
curl_cmd = [
81-
'curl', '-LSs', '-o', tarball,
82-
f"https://buildroot.org/downloads/{tarball.name}"
83-
]
84-
subprocess.run(curl_cmd, check=True)
81+
if not tarball.exists():
82+
curl_cmd = [
83+
'curl', '-LSs', '-o', tarball,
84+
f"https://buildroot.org/downloads/{tarball.name}"
85+
]
86+
subprocess.run(curl_cmd, check=True)
8587

8688
sha256_cmd = ['sha256sum', '--quiet', '-c', f"{tarball.name}.sha256"]
8789
subprocess.run(sha256_cmd, check=True, cwd=ROOT_FOLDER)
@@ -91,27 +93,18 @@ def download_and_extract_buildroot():
9193
]
9294
subprocess.run(tar_cmd, check=True)
9395

94-
tarball.unlink(missing_ok=True)
95-
96-
97-
def download_buildroot_if_necessary():
98-
if SRC_FOLDER.exists():
99-
# Make support/scripts/setlocalversion do nothing because we are in a
100-
# git repository so it will return information about this repo, not
101-
# Buildroot
102-
setlocalversion = Path(SRC_FOLDER, 'support/scripts/setlocalversion')
103-
setlocalversion.write_text('', encoding='utf-8')
104-
105-
installed_version = subprocess.run(['make', 'print-version'],
106-
capture_output=True,
107-
check=True,
108-
cwd=SRC_FOLDER,
109-
text=True).stdout.strip()
110-
if installed_version != BUILDROOT_VERSION:
111-
shutil.rmtree(SRC_FOLDER)
112-
download_and_extract_buildroot()
113-
else:
114-
download_and_extract_buildroot()
96+
if (patches := list(ROOT_FOLDER.glob('*.patch'))):
97+
for patch in patches:
98+
patch_cmd = [
99+
'patch', '--directory', SRC_FOLDER, '--input', patch,
100+
'--strip', '1'
101+
]
102+
try:
103+
subprocess.run(patch_cmd, check=True)
104+
except subprocess.CalledProcessError as err:
105+
raise RuntimeError(
106+
f"{patch} did not apply to Buildroot {BUILDROOT_VERSION}, does it need to be updated?"
107+
) from err
115108

116109

117110
def release_images():
@@ -161,7 +154,7 @@ def parse_arguments():
161154

162155
architectures = SUPPORTED_ARCHES if 'all' in args.architectures else args.architectures
163156

164-
download_buildroot_if_necessary()
157+
download_and_extract_buildroot()
165158
for arch in architectures:
166159
build_image(arch, args.edit_config)
167160

buildroot/riscv.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BR2_riscv=y
22
BR2_TOOLCHAIN_EXTERNAL=y
3-
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_RISCV64_LP64D_GLIBC_STABLE=y
3+
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_RISCV64_LP64D_UCLIBC_STABLE=y
44
BR2_TARGET_GENERIC_ROOT_PASSWD="root"
55
BR2_TARGET_GENERIC_GETTY_PORT="ttyS0"
66
BR2_ROOTFS_OVERLAY="../overlay-poweroff"

buildroot/s390.config

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
BR2_s390x=y
2-
BR2_KERNEL_HEADERS_4_4=y
3-
BR2_BINUTILS_VERSION_2_35_X=y
4-
BR2_GCC_VERSION_10_X=y
2+
BR2_TOOLCHAIN_EXTERNAL=y
3+
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_S390X_Z13_GLIBC_STABLE=y
54
BR2_TARGET_GENERIC_ROOT_PASSWD="root"
65
BR2_TARGET_GENERIC_GETTY_PORT="ttyS0"
76
BR2_ROOTFS_OVERLAY="../overlay-poweroff"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
From git@z Thu Jan 1 00:00:00 1970
2+
Subject: [PATCH v3] buildroot: Add basic support for LoongArch architecture
3+
(toolchain only)
4+
From: Zhiwei Duan <[email protected]>
5+
Date: Mon, 12 Dec 2022 16:34:57 +0800
6+
Message-Id: <[email protected]>
7+
MIME-Version: 1.0
8+
Content-Type: text/plain; charset="utf-8"
9+
Content-Transfer-Encoding: 7bit
10+
11+
With this patch, the buildroot can compile the rootfs of the loongarch architecture.
12+
Both external toolchain and buildroot toolchain can compile rootfs.
13+
buildroot toolchain: binutils>=2.38 GCC>=12 Linux>=5.19 glibc>=2.36.
14+
15+
Signed-off-by: Zhiwei Duan <[email protected]>
16+
Link: https://lore.kernel.org/r/[email protected]
17+
---
18+
arch/Config.in | 16 +++++++++++
19+
arch/Config.in.loongarch | 52 ++++++++++++++++++++++++++++++++++++
20+
package/glibc/Config.in | 1 +
21+
support/gnuconfig/config.sub | 11 ++++++--
22+
toolchain/Config.in | 2 +-
23+
5 files changed, 79 insertions(+), 3 deletions(-)
24+
create mode 100644 arch/Config.in.loongarch
25+
26+
diff --git a/arch/Config.in b/arch/Config.in
27+
index 1c0c400a98..88f805bb1e 100644
28+
--- a/arch/Config.in
29+
+++ b/arch/Config.in
30+
@@ -251,6 +251,17 @@ config BR2_xtensa
31+
http://en.wikipedia.org/wiki/Xtensa
32+
http://www.tensilica.com/
33+
34+
+config BR2_loongarch64
35+
+ bool "LOONGARCH64 (little endian)"
36+
+ select BR2_ARCH_IS_64
37+
+ select BR2_USE_MMU
38+
+ help
39+
+ LOONGARCH is a RISC microprocessor from LOONGARCH Technologies. Little
40+
+ endian.
41+
+ https://www.loongson.cn/
42+
+ #http://en.wikipedia.org/wiki/MIPS_Technologies
43+
+
44+
+
45+
endchoice
46+
47+
# For some architectures or specific cores, our internal toolchain
48+
@@ -414,6 +425,11 @@ if BR2_xtensa
49+
source "arch/Config.in.xtensa"
50+
endif
51+
52+
+if BR2_loongarch64
53+
+source "arch/Config.in.loongarch"
54+
+endif
55+
+
56+
+
57+
# Set up target binary format
58+
choice
59+
prompt "Target Binary Format"
60+
diff --git a/arch/Config.in.loongarch b/arch/Config.in.loongarch
61+
new file mode 100644
62+
index 0000000000..bf86490cff
63+
--- /dev/null
64+
+++ b/arch/Config.in.loongarch
65+
@@ -0,0 +1,52 @@
66+
+# loongarch config
67+
+config BR2_LOONGARCH_CPU_LOONGARCH64
68+
+ bool
69+
+ select BR2_LOONGARCH_NAN_LEGACY
70+
+
71+
+choice
72+
+ prompt "Target Architecture Variant"
73+
+ default BR2_loongarch_64 if BR2_loongarch64
74+
+ depends on BR2_loongarch64
75+
+ help
76+
+ Specific CPU variant to use
77+
+
78+
+config BR2_loongarch_64
79+
+ bool "Generic LOONGARCH64"
80+
+ depends on BR2_ARCH_IS_64
81+
+ select BR2_LOONGARCH_CPU_LOONGARCH64
82+
+endchoice
83+
+
84+
+config BR2_LOONGARCH_NAN_LEGACY
85+
+ bool
86+
+
87+
+#config BR2_GCC_TARGET_NAN
88+
+# default "legacy" if BR2_LOONGARCH_NAN_LEGACY
89+
+
90+
+config BR2_ARCH
91+
+ default "loongarch64" if BR2_loongarch64
92+
+
93+
+config BR2_NORMALIZED_ARCH
94+
+ default "loongarch"
95+
+
96+
+config BR2_ENDIAN
97+
+ default "LITTLE" if BR2_loongarch64
98+
+
99+
+config BR2_GCC_TARGET_ARCH
100+
+ default "loongarch64" if BR2_loongarch_64
101+
+
102+
+config BR2_READELF_ARCH_NAME
103+
+ default "LoongArch"
104+
+
105+
+config BR2_LOONGARCH_SOFT_FLOAT
106+
+ bool "Use soft-float"
107+
+ #default y
108+
+ select BR2_SOFT_FLOAT
109+
+ help
110+
+ If your target CPU does not have a Floating Point Unit (FPU)
111+
+ or a kernel FPU emulator, but you still wish to support
112+
+ floating point functions, then everything will need to be
113+
+ compiled with soft floating point support (-msoft-float).
114+
+
115+
+
116+
+# vim: ft=kconfig
117+
+# -*- mode:kconfig; -*-
118+
diff --git a/package/glibc/Config.in b/package/glibc/Config.in
119+
index 71c50504ac..d8325610f5 100644
120+
--- a/package/glibc/Config.in
121+
+++ b/package/glibc/Config.in
122+
@@ -21,6 +21,7 @@ config BR2_PACKAGE_GLIBC_ARCH_SUPPORTS
123+
default y if BR2_microblaze
124+
default y if BR2_nios2
125+
default y if BR2_arc && BR2_ARC_ATOMIC_EXT
126+
+ default y if BR2_loongarch64
127+
depends on !BR2_powerpc_SPE
128+
depends on BR2_RISCV_ISA_RVA || !BR2_riscv
129+
depends on BR2_USE_MMU
130+
diff --git a/support/gnuconfig/config.sub b/support/gnuconfig/config.sub
131+
index 9bc49a7e92..c751ddf15a 100755
132+
--- a/support/gnuconfig/config.sub
133+
+++ b/support/gnuconfig/config.sub
134+
@@ -164,7 +164,7 @@ case $1 in
135+
basic_os=$field2
136+
;;
137+
# Manufacturers
138+
- dec* | mips* | sequent* | encore* | pc533* | sgi* | sony* \
139+
+ dec* | mips* | loongarch* | sequent* | encore* | pc533* | sgi* | sony* \
140+
| att* | 7300* | 3300* | delta* | motorola* | sun[234]* \
141+
| unicom* | ibm* | next | hp | isi* | apollo | altos* \
142+
| convergent* | ncr* | news | 32* | 3600* | 3100* \
143+
@@ -632,6 +632,11 @@ case $1 in
144+
basic_machine=ymp-cray
145+
basic_os=unicos
146+
;;
147+
+ loongarch)
148+
+ basic_machine=loongarch-loongson
149+
+ basic_os=
150+
+ ;;
151+
+
152+
*)
153+
basic_machine=$1
154+
basic_os=
155+
@@ -1211,6 +1216,7 @@ case $cpu-$vendor in
156+
| mipsisa64sr71k | mipsisa64sr71kel \
157+
| mipsr5900 | mipsr5900el \
158+
| mipstx39 | mipstx39el \
159+
+ | loongarch | loongarch64 \
160+
| mmix \
161+
| mn10200 | mn10300 \
162+
| moxie \
163+
@@ -1253,7 +1259,8 @@ case $cpu-$vendor in
164+
| x86 | x86_64 | xc16x | xgate | xps100 \
165+
| xstormy16 | xtensa* \
166+
| ymp \
167+
- | z8k | z80)
168+
+ | z8k | z80 \
169+
+ | loongarch | loongarch64)
170+
;;
171+
172+
*)
173+
diff --git a/toolchain/Config.in b/toolchain/Config.in
174+
index 4947ab3aae..a4939af6fb 100644
175+
--- a/toolchain/Config.in
176+
+++ b/toolchain/Config.in
177+
@@ -24,7 +24,7 @@ config BR2_TOOLCHAIN_USES_UCLIBC
178+
# architectures
179+
select BR2_TOOLCHAIN_HAS_UCONTEXT if BR2_ARM_CPU_HAS_ARM || BR2_i386 \
180+
|| BR2_mips || BR2_mipsel || BR2_mips64 || BR2_mips64el \
181+
- || BR2_sparc || BR2_x86_64
182+
+ || BR2_sparc || BR2_x86_64 || BR2_loongarch64
183+
select BR2_TOOLCHAIN_SUPPORTS_PIE if !BR2_m68k && !BR2_microblaze && !BR2_STATIC_LIBS
184+
185+
config BR2_TOOLCHAIN_USES_MUSL
186+
--
187+
2.20.1
188+
189+
_______________________________________________
190+
buildroot mailing list
191+
192+
https://lists.buildroot.org/mailman/listinfo/buildroot
193+

images/loongarch/.release

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20230609-194440

images/loongarch/rootfs.cpio.zst

1.38 MB
Binary file not shown.

0 commit comments

Comments
 (0)