Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add loongarch64 support #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions .github/workflows/actions/setup-musl/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,45 @@ runs:
path: ${{ inputs.arch }}-linux-musl-cross
key: ${{ inputs.arch }}-linux-musl-cross
- name: Download musl toolchain
if: steps.cache-musl.outputs.cache-hit != 'true'
if: ${{ steps.cache-musl.outputs.cache-hit != 'true' && inputs.arch != 'loongarch64' }}
shell: bash
run: |
MUSL_PATH=${{ inputs.arch }}-linux-musl-cross
wget https://musl.cc/${MUSL_PATH}.tgz
tar -xf ${MUSL_PATH}.tgz
- name: Download musl toolchain
if: ${{ steps.cache-musl.outputs.cache-hit != 'true' && inputs.arch == 'loongarch64' }}
shell: bash
run: |
TOOLCHAIN_REPO=https://github.com/LoongsonLab/oscomp-toolchains-for-oskernel
wget ${TOOLCHAIN_REPO}/releases/download/gcc-13.2.0-loongarch64/gcc-13.2.0-loongarch64-linux-gnu.tgz
wget ${TOOLCHAIN_REPO}/raw/refs/heads/main/musl-loongarch64-1.2.2.tgz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to download both gnu and musl toolchain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This musl is just a warpper

#!/bin/sh

MUSL_DIR=$(cd $(dirname $0)/..; pwd)

exec "${REALGCC:-loongarch64-linux-gnu-gcc}" "$@" -specs "${MUSL_DIR}/lib/musl-gcc.specs"

tar -xf gcc-13.2.0-loongarch64-linux-gnu.tgz
tar -xf musl-loongarch64-1.2.2.tgz
- uses: actions/cache/save@v4
if: steps.cache-musl.outputs.cache-hit != 'true'
if: ${{ steps.cache-musl.outputs.cache-hit != 'true' && inputs.arch != 'loongarch64' }}
with:
path: ${{ inputs.arch }}-linux-musl-cross
key: ${{ inputs.arch }}-linux-musl-cross
- uses: actions/cache/save@v4
if: ${{ steps.cache-musl.outputs.cache-hit != 'true' && inputs.arch == 'loongarch64' }}
with:
path: |
gcc-13.2.0-loongarch64-linux-gnu
musl-loongarch64-1.2.2
key: ${{ inputs.arch }}-linux-musl-cross

- name: Add to PATH environment variable
if: inputs.arch != 'loongarch64'
shell: bash
run: |
echo "$PWD/${{ inputs.arch }}-linux-musl-cross/bin" >> $GITHUB_PATH
- name: Add to PATH environment variable
if: inputs.arch == 'loongarch64'
shell: bash
run: |
echo "$PWD/gcc-13.2.0-loongarch64-linux-gnu/bin" >> $GITHUB_PATH
echo "$PWD/musl-loongarch64-1.2.2/bin" >> $GITHUB_PATH
- name: Verify installation
shell: bash
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/actions/setup-qemu/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ runs:
sudo apt-get update && sudo apt-get install -y ninja-build libslirp-dev libglib2.0-dev
wget https://download.qemu.org/$QEMU_PATH.tar.xz && tar -xJf $QEMU_PATH.tar.xz
cd $QEMU_PATH \
&& ./configure --prefix=$PREFIX --target-list=x86_64-softmmu,riscv64-softmmu,aarch64-softmmu --enable-slirp \
&& ./configure --prefix=$PREFIX --target-list=x86_64-softmmu,riscv64-softmmu,aarch64-softmmu,loongarch64-softmmu --enable-slirp \
&& make -j > /dev/null 2>&1 \
&& make install
- uses: actions/cache/save@v4
Expand All @@ -44,3 +44,4 @@ runs:
qemu-system-x86_64 --version
qemu-system-aarch64 --version
qemu-system-riscv64 --version
qemu-system-loongarch64 --version
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
arch: [x86_64, riscv64, aarch64]
arch: [x86_64, riscv64, aarch64, loongarch64]
rust-toolchain: [nightly, nightly-2024-12-25]
env:
RUSTUP_TOOLCHAIN: ${{ matrix.rust-toolchain }}
Expand All @@ -18,7 +18,7 @@ jobs:
with:
toolchain: ${{ matrix.rust-toolchain }}
components: rust-src, clippy, rustfmt
targets: x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none, aarch64-unknown-none-softfloat
targets: x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none, aarch64-unknown-none-softfloat, loongarch64-unknown-none
- uses: Swatinem/rust-cache@v2
- name: Check rust version
run: rustc --version --verbose
Expand All @@ -40,7 +40,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
arch: [x86_64, riscv64, aarch64]
arch: [x86_64, riscv64, aarch64, loongarch64]
rust-toolchain: [nightly, nightly-2024-12-25]
env:
RUSTUP_TOOLCHAIN: ${{ matrix.rust-toolchain }}
Expand All @@ -50,7 +50,7 @@ jobs:
with:
toolchain: ${{ matrix.rust-toolchain }}
components: rust-src, llvm-tools
targets: x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none, aarch64-unknown-none-softfloat
targets: x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none, aarch64-unknown-none-softfloat, loongarch64-unknown-none
- uses: Swatinem/rust-cache@v2
- run: cargo install cargo-binutils
- name: Build helloworld
Expand Down Expand Up @@ -92,7 +92,7 @@ jobs:
with:
toolchain: ${{ matrix.rust-toolchain }}
components: rust-src, llvm-tools
targets: x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none, aarch64-unknown-none-softfloat
targets: x86_64-unknown-none, riscv64gc-unknown-none-elf, aarch64-unknown-none, aarch64-unknown-none-softfloat, loongarch64-unknown-none
- uses: Swatinem/rust-cache@v2
- run: cargo install cargo-binutils
- name: Build helloworld for x86_64-pc-oslab
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Test CI
on: [push, pull_request]

env:
qemu-version: 8.2.0
qemu-version: 9.2.1
rust-toolchain: nightly-2024-12-25
arceos-apps: '855106c'

Expand All @@ -26,7 +26,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
arch: [x86_64, riscv64, aarch64]
arch: [x86_64, riscv64, aarch64, loongarch64]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
Expand Down
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
FROM rust:slim

RUN echo /etc/apt/sources.list << deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm main
RUN wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc

RUN apt-get update \
&& apt-get install -y --no-install-recommends libclang-dev qemu-system wget make \
&& apt-get install -y --no-install-recommends libclang-19-dev wget make python3 \
xz-utils python3-venv ninja-build bzip2 meson \
pkg-config libglib2.0-dev git libslirp-dev \
&& rm -rf /var/lib/apt/lists/*

RUN cargo install cargo-binutils axconfig-gen
Expand All @@ -13,9 +18,24 @@ RUN rustc --version
RUN wget https://musl.cc/aarch64-linux-musl-cross.tgz \
&& wget https://musl.cc/riscv64-linux-musl-cross.tgz \
&& wget https://musl.cc/x86_64-linux-musl-cross.tgz \
&& wget https://github.com/LoongsonLab/oscomp-toolchains-for-oskernel/releases/download/gcc-13.2.0-loongarch64/gcc-13.2.0-loongarch64-linux-gnu.tgz \
&& wget https://github.com/LoongsonLab/oscomp-toolchains-for-oskernel/raw/refs/heads/main/musl-loongarch64-1.2.2.tgz \
&& tar zxf aarch64-linux-musl-cross.tgz \
&& tar zxf riscv64-linux-musl-cross.tgz \
&& tar zxf x86_64-linux-musl-cross.tgz \
&& tar zxf gcc-13.2.0-loongarch64-linux-gnu.tgz \
&& tar zxf musl-loongarch64-1.2.2.tgz && cd musl-loongarch64-1.2.2 && ./setup && cd .. \
&& rm -f *.tgz

RUN wget https://download.qemu.org/qemu-9.2.1.tar.xz \
&& tar xf qemu-9.2.1.tar.xz \
&& cd qemu-9.2.1 \
&& ./configure --prefix=/qemu-bin-9.2.1 \
--target-list=loongarch64-softmmu,riscv64-softmmu,aarch64-softmmu,x86_64-softmmu \
--enable-gcov --enable-debug --enable-slirp \
&& make -j$(nproc) \
&& make install
RUN rm -rf qemu-9.2.1 qemu-9.2.1.tar.xz

ENV PATH="/x86_64-linux-musl-cross/bin:/aarch64-linux-musl-cross/bin:/riscv64-linux-musl-cross/bin:$PATH"
ENV PATH="/gcc-13.2.0-loongarch64-linux-gnu/bin:/musl-loongarch64-1.2.2/bin:/qemu-bin-9.2.1/bin:$PATH"
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ else ifeq ($(ARCH), aarch64)
endif
else ifeq ($(ARCH), riscv64)
TARGET := riscv64gc-unknown-none-elf
else ifeq ($(ARCH), loongarch64)
TARGET := loongarch64-unknown-none
else
$(error "ARCH" must be one of "x86_64", "riscv64", or "aarch64")
$(error "ARCH" must be one of "x86_64", "riscv64", "aarch64" or "loongarch64")
endif

export AX_ARCH=$(ARCH)
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ ArceOS was inspired a lot by [Unikraft](https://github.com/unikraft/unikraft).

## Features & TODOs

* [x] Architecture: x86_64, riscv64, aarch64
* [x] Platform: QEMU pc-q35 (x86_64), virt (riscv64/aarch64)
* [x] Architecture: x86_64, riscv64, aarch64, loongarch64
* [x] Platform: QEMU pc-q35 (x86_64), virt (riscv64/aarch64/loongarch64)
* [x] Multi-thread
* [x] FIFO/RR/CFS scheduler
* [x] VirtIO net/blk/gpu drivers
Expand Down Expand Up @@ -78,12 +78,16 @@ Download & install [musl](https://musl.cc) toolchains:
wget https://musl.cc/aarch64-linux-musl-cross.tgz
wget https://musl.cc/riscv64-linux-musl-cross.tgz
wget https://musl.cc/x86_64-linux-musl-cross.tgz
wget https://github.com/LoongsonLab/oscomp-toolchains-for-oskernel/releases/download/gcc-13.2.0-loongarch64/gcc-13.2.0-loongarch64-linux-gnu.tgz
wget https://github.com/LoongsonLab/oscomp-toolchains-for-oskernel/raw/refs/heads/main/musl-loongarch64-1.2.2.tgz
# install
tar zxf aarch64-linux-musl-cross.tgz
tar zxf riscv64-linux-musl-cross.tgz
tar zxf x86_64-linux-musl-cross.tgz
tar zxf gcc-13.2.0-loongarch64-linux-gnu.tgz
tar zxf musl-loongarch64-1.2.2.tgz && cd musl-loongarch64-1.2.2 && ./setup && cd ..
# exec below command in bash OR add below info in ~/.bashrc
export PATH=`pwd`/x86_64-linux-musl-cross/bin:`pwd`/aarch64-linux-musl-cross/bin:`pwd`/riscv64-linux-musl-cross/bin:$PATH
export PATH=`pwd`/x86_64-linux-musl-cross/bin:`pwd`/aarch64-linux-musl-cross/bin:`pwd`/riscv64-linux-musl-cross/bin:`pwd`/gcc-13.2.0-loongarch64-linux-gnu/bin:`pwd`/musl-loongarch64-1.2.2/bin:$PATH
```

Other systems and arch please refer to [Qemu Download](https://www.qemu.org/download/#linux)
Expand All @@ -97,7 +101,7 @@ make A=path/to/app ARCH=<arch> LOG=<log>

Where `path/to/app` is the relative path to the application. Examples applications can be found in the [examples](examples/) directory or the [arceos-apps](https://github.com/arceos-org/arceos-apps) repository.

`<arch>` should be one of `riscv64`, `aarch64`, `x86_64`.
`<arch>` should be one of `riscv64`, `aarch64`, `x86_64`, `loongarch64`.

`<log>` should be one of `off`, `error`, `warn`, `info`, `debug`, `trace`.

Expand Down
78 changes: 78 additions & 0 deletions configs/platforms/loongarch64-qemu-virt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

# Architecture identifier.
arch = "loongarch64"
# Platform identifier.
platform = "loongarch64-qemu-virt"

#
# Platform configs
#
[plat]
# Platform family.
family = "loongarch64-qemu-virt"

# Base address of the whole physical memory.
phys-memory-base = 0x8000_0000 # uint
# Size of the whole physical memory. (128M)
phys-memory-size = 0x800_0000 # uint
# Base physical address of the kernel image.
kernel-base-paddr = 0x8000_0000 # uint

# Base virtual address of the kernel image.
kernel-base-vaddr = "0xffff_0000_8000_0000" # uint
# Linear mapping offset, for quick conversions between physical and virtual
# addresses.
phys-virt-offset = "0xffff_0000_0000_0000" # uint
# Offset of bus address and phys address. some boards, the bus address is
# different from the physical address.
phys-bus-offset = 0 # uint
# Kernel address space base.
kernel-aspace-base = "0xffff_0000_0000_0000" # uint
# Kernel address space size.
kernel-aspace-size = "0x0000_ffff_ffff_f000" # uint

#
# Device specifications
#
[devices]
# MMIO regions with format (`base_paddr`, `size`).
mmio-regions = [
[0x100E_0000, 0x0000_1000], # GED
[0x1FE0_0000, 0x0000_1000], # UART
[0x2000_0000, 0x1000_0000], # PCI
[0x4000_0000, 0x0002_0000], # PCI RANGES
] # [(uint, uint)]
# VirtIO MMIO regions with format (`base_paddr`, `size`).
virtio-mmio-regions = [] # [(uint, uint)]
# Base physical address of the PCIe ECAM space.
pci-ecam-base = 0x2000_0000 # uint
# End PCI bus number.
pci-bus-end = 0 # uint
# PCI device memory ranges.
pci-ranges = [
[0, 0],
[0x4000_0000, 0x0002_0000]
] # [(uint, uint)]
# poweroff {
# value = <0x00000034>;
# offset = <0x00000000>;
# compatible = "syscon-poweroff";
# };
# ged@100e001c {
# reg-io-width = <0x00000001>;
# reg-shift = <0x00000000>;
# reg = <0x00000000 0x100e001c 0x00000000 0x00000003>;
# compatible = "syscon";
# };
ged-paddr = 0x100E001C # uint
# serial@1fe001e0 {
# interrupt-parent = <0x00008003>;
# interrupts = <0x00000002 0x00000004>;
# clock-frequency = <0x05f5e100>;
# reg = <0x00000000 0x1fe001e0 0x00000000 0x00000100>;
# compatible = "ns16550a";
# };
uart-paddr = 0x1FE001E0 # uint

# Timer interrupt frequency in Hz.
timer-frequency = 100_000_000 # uint
4 changes: 4 additions & 0 deletions modules/axhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ arm_pl011 = "0.1"
arm_pl031 = { version = "0.2", optional = true }
dw_apb_uart = "0.1"

[target.'cfg(target_arch = "loongarch64")'.dependencies]
loongArch64 = "0.2.4"
ns16550a = "0.4.0"

[build-dependencies]
axconfig = { workspace = true }
2 changes: 2 additions & 0 deletions modules/axhal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const BUILTIN_PLATFORMS: &[&str] = &[
"aarch64-bsta1000b",
"aarch64-qemu-virt",
"aarch64-raspi4",
"loongarch64-qemu-virt",
"riscv64-qemu-virt",
"x86_64-pc-oslab",
"x86_64-qemu-q35",
Expand All @@ -15,6 +16,7 @@ const BUILTIN_PLATFORM_FAMILIES: &[&str] = &[
"aarch64-phytium-pi",
"aarch64-qemu-virt",
"aarch64-raspi",
"loongarch64-qemu-virt",
"riscv64-qemu-virt",
"x86-pc",
];
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/linker.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ SECTIONS
. = ALIGN(4K);
_edata = .;

.bss : ALIGN(4K) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is the lma in the .bss section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Sections:
Idx Name              Size     VMA              LMA              Type
  0                   00000000 0000000000000000 0000000000000000 
  1 .text             0000f000 ffff000080000000 ffff000080000000 TEXT
  2 .rodata           00003c80 ffff00008000f000 ffff00008000f000 DATA
  3 .init_array       00000000 ffff000080012c80 ffff000080012c80 
  4 .data             00002018 ffff000080013000 ffff000080013000 DATA
  5 .tdata            00000000 ffff000080015020 ffff000080015020 DATA
  6 .tbss             00000000 ffff000080015020 ffff000080015020 DATA
  7 linkme_IRQ        00000000 ffff000080015020 ffff000080015020 DATA
  8 linkm2_IRQ        00000008 ffff000080015020 ffff000080015020 DATA
  9 linkme_PAGE_FAULT 00000000 ffff000080015028 ffff000080015028 DATA
 10 linkm2_PAGE_FAULT 00000008 ffff000080015028 ffff000080015028 DATA
 11 .percpu           00000040 0000000000000000 ffff000080016000 DATA
 12 .bss              00044000 ffff000080017000 ffff000080017000 BSS
 13 .symtab           000011d0 0000000000000000 0000000000000000 
 14 .shstrtab         0000008f 0000000000000000 0000000000000000 
 15 .strtab           0000273a 0000000000000000 0000000000000000 

Not Added

Sections:
Idx Name              Size     VMA              LMA              Type
  0                   00000000 0000000000000000 0000000000000000 
  1 .text             0000f000 ffff000080000000 ffff000080000000 TEXT
  2 .rodata           00003c78 ffff00008000f000 ffff00008000f000 DATA
  3 .init_array       00000000 ffff000080012c80 ffff000080012c80 
  4 .data             00002018 ffff000080013000 ffff000080013000 DATA
  5 .tdata            00000000 ffff000080015020 ffff000080015020 DATA
  6 .tbss             00000000 ffff000080015020 ffff000080015020 DATA
  7 linkme_IRQ        00000000 ffff000080015020 ffff000080015020 DATA
  8 linkm2_IRQ        00000008 ffff000080015020 ffff000080015020 DATA
  9 linkme_PAGE_FAULT 00000000 ffff000080015028 ffff000080015028 DATA
 10 linkm2_PAGE_FAULT 00000008 ffff000080015028 ffff000080015028 DATA
 11 .percpu           00000040 0000000000000000 ffff000080016000 DATA
 12 .bss              00044000 ffff000080017000 fffe00010002d000 BSS
 13 .symtab           000011d0 0000000000000000 0000000000000000 
 14 .shstrtab         0000008f 0000000000000000 0000000000000000 
 15 .strtab           0000273a 0000000000000000 0000000000000000  

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not added will display:

qemu-system-loongarch64: could not load kernel 'examples/helloworld/helloworld_loongarch64-qemu-virt.elf': The image segments are too big to load

.bss : AT(.) ALIGN(4K) {
boot_stack = .;
*(.bss.stack)
. = ALIGN(4K);
Expand Down
Loading
Loading