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 basic loongarch64 support #126

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

Conversation

Godones
Copy link

@Godones Godones commented Sep 18, 2023

  • Refactor the repo code to merge
  • Use standard toolchain
  • Fix the compile command

image

I will submit other code about interrupt soon.

Cargo.lock Outdated Show resolved Hide resolved
1. Use r21 register to record percpu variable address
2. Add page table description, although la64 does not use page table for
   the time being.
3. Modify the kernel page table remap part in runtime. la64 does not
   require remapping.
This commit includes multiple changes aimed at ensuring all apps/task and fs can be supported properly. These edits affect various files within the 'axhal' module and its subdirectories, as well as specific system configuration aspects outlined in the 'loongarch64-qemu-virt.toml' file.

fix bugs about percpu, use r21 register;
fix bugs about timer interrupt;
Simplify saving and restoring trap context;
Since loongarch64 does not seem to have the musl tool chain, here we use
the gnu tool chain.

1. add pointer type definition
2. add float definition
3. add setjmp/longjmp
@Godones
Copy link
Author

Godones commented Sep 29, 2023

@hky1999 Support for la64 is now complete. Except for the two applications redis and sqlite, which were not tested, other apps have passed. If you have time, please check to see if there are any areas for improvement.

Copy link
Member

@equation314 equation314 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! Are you still available? Let’s work on merging this PR.

crates/kernel_guard/src/arch/loongarch64.rs Outdated Show resolved Hide resolved
crates/kernel_guard/src/arch/loongarch64.rs Outdated Show resolved Hide resolved
crates/kernel_guard/src/arch/mod.rs Outdated Show resolved Hide resolved
crates/percpu/src/imp.rs Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Could it run on the mainline qemu? So we can remove this script.

Copy link
Author

Choose a reason for hiding this comment

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

Maybe it can be run on the mainline qemu, but according to existing attempts qemu-system-loongarch64 v7.1 不能正确启动kernel, it is difficult to compile a complete tool chain, so most projects currently use a specific version.

Copy link
Member

Choose a reason for hiding this comment

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

What about qemu 8.1.3?

Copy link
Author

Choose a reason for hiding this comment

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

The main problem is that these versions of qemu require appropriate uefi support. I have not yet tried to compile the latest tool chain. Maybe we can remove the dependency on uefi later, so that there will be no restrictions on the qemu version.

crates/page_table_entry/src/arch/loongarch64.rs Outdated Show resolved Hide resolved
crates/page_table_entry/src/arch/loongarch64.rs Outdated Show resolved Hide resolved
fn is_present(&self) -> bool {
PTEFlags::from_bits_truncate(self.0 as usize).contains(PTEFlags::V)
}
fn is_huge(&self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

Any reasons for not supporting huge pages?

crates/percpu/src/imp.rs Outdated Show resolved Hide resolved
modules/axhal/Cargo.toml Outdated Show resolved Hide resolved
@Godones
Copy link
Author

Godones commented Dec 7, 2023

Thanks for your contribution! Are you still available? Let’s work on merging this PR.

I will fix these errors.

rename loongArch64 to loongarch64
Remove unnecessary firmware programs and add help documentation.
@Godones
Copy link
Author

Godones commented Dec 15, 2023

@equation314 Do you have time to check and see if there are any other problems?

@equation314
Copy link
Member

Thanks! Please fix conflicts and CI.

@@ -108,6 +110,8 @@ pub fn set_local_thread_pointer(cpu_id: usize) {
core::arch::asm!("mv gp, {}", in(reg) tp)
} else if #[cfg(target_arch = "aarch64")] {
core::arch::asm!("msr TPIDR_EL1, {}", in(reg) tp)
}else if #[cfg(target_arch = "loongarch64")] {
Copy link
Member

Choose a reason for hiding this comment

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

Add space

@Godones
Copy link
Author

Godones commented Dec 18, 2023

Thanks! Please fix conflicts and CI.

@equation314 This CI error may be due to a rust version issue. I'm not very familiar with CI, but it seems that the latest version of rust is used in CI.

@hky1999
Copy link
Contributor

hky1999 commented Dec 25, 2023

Thanks! Please fix conflicts and CI.

@equation314 This CI error may be due to a rust version issue. I'm not very familiar with CI, but it seems that the latest version of rust is used in CI.

Hello, CI should fit the latest version of Rust, currently I believe it should be rust version 1.77.0-nightly (bf8716f1c 2023-12-24).

Some warnings like this should be fixed.

warning: `axhal` (lib) generated 1 warning
warning: the feature `ip_in_core` has been stable since 1.77.0-nightly and no longer requires an attribute to enable
 --> api/arceos_api/src/lib.rs:6:12
  |
6 | #![feature(ip_in_core)]
  |            ^^^^^^^^^^
  |
  = note: `#[warn(stable_features)]` on by default

I try to build this branch tonight.

➜  arceos git:(la64) ✗ qemu-system-loongarch64 --version                                                     
QEMU emulator version 8.1.94
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
  • The command is make A=apps/helloworld ARCH=loongarch64 run.
  • But I just get no output:
➜  arceos git:(la64) ✗ make A=apps/helloworld ARCH=loongarch64 run
    Building App: helloworld, Arch: loongarch64, Platform: loongarch64-qemu-virt, App type: rust
cargo build --target loongarch64-unknown-none --target-dir /home/hky/workspace/rcore-os/arceos/target --release  -Z build-std --manifest-path apps/helloworld/Cargo.toml --features "axstd/log-level-warn axstd/bus-pci"
warning: value assigned to `flags` is never read
  --> crates/kernel_guard/src/arch/loongarch64.rs:17:53
   |
17 |     unsafe { asm!("csrxchg {}, {}, 0x0", inout(reg) flags, in(reg) IE_BIT) };
   |                                                     ^^^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

warning: `kernel_guard` (lib) generated 1 warning
warning: the feature `const_maybe_uninit_zeroed` has been stable since 1.75.0 and no longer requires an attribute to enable
  --> modules/axhal/src/lib.rs:30:12
   |
30 | #![feature(const_maybe_uninit_zeroed)]
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(stable_features)]` on by default

warning: `axhal` (lib) generated 1 warning
warning: the feature `ip_in_core` has been stable since 1.77.0-nightly and no longer requires an attribute to enable
 --> api/arceos_api/src/lib.rs:6:12
  |
6 | #![feature(ip_in_core)]
  |            ^^^^^^^^^^
  |
  = note: `#[warn(stable_features)]` on by default

warning: `arceos_api` (lib) generated 1 warning
warning: the feature `ip_in_core` has been stable since 1.77.0-nightly and no longer requires an attribute to enable
  --> ulib/axstd/src/lib.rs:52:12
   |
52 | #![feature(ip_in_core)]
   |            ^^^^^^^^^^
   |
   = note: `#[warn(stable_features)]` on by default

warning: `axstd` (lib) generated 1 warning
    Finished release [optimized] target(s) in 0.11s
rust-objcopy --binary-architecture=loongarch64 apps/helloworld/helloworld_loongarch64-qemu-virt.elf --strip-all -O binary apps/helloworld/helloworld_loongarch64-qemu-virt.bin
    Running on qemu...
qemu-system-loongarch64 -m 1G -smp 1 -bios tools/la64/loongarch_bios_0310.bin -kernel apps/helloworld/helloworld_loongarch64-qemu-virt.elf -nographic -vga none

Could you offer some help or advice with it? Thanks a lot! @Godones

@Godones
Copy link
Author

Godones commented Dec 26, 2023

Thanks! Please fix conflicts and CI.

@equation314 This CI error may be due to a rust version issue. I'm not very familiar with CI, but it seems that the latest version of rust is used in CI.

Hello, CI should fit the latest version of Rust, currently I believe it should be rust version 1.77.0-nightly (bf8716f1c 2023-12-24).

Some warnings like this should be fixed.

warning: `axhal` (lib) generated 1 warning
warning: the feature `ip_in_core` has been stable since 1.77.0-nightly and no longer requires an attribute to enable
 --> api/arceos_api/src/lib.rs:6:12
  |
6 | #![feature(ip_in_core)]
  |            ^^^^^^^^^^
  |
  = note: `#[warn(stable_features)]` on by default

I try to build this branch tonight.

➜  arceos git:(la64) ✗ qemu-system-loongarch64 --version                                                     
QEMU emulator version 8.1.94
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
  • The command is make A=apps/helloworld ARCH=loongarch64 run.
  • But I just get no output:
➜  arceos git:(la64) ✗ make A=apps/helloworld ARCH=loongarch64 run
    Building App: helloworld, Arch: loongarch64, Platform: loongarch64-qemu-virt, App type: rust
cargo build --target loongarch64-unknown-none --target-dir /home/hky/workspace/rcore-os/arceos/target --release  -Z build-std --manifest-path apps/helloworld/Cargo.toml --features "axstd/log-level-warn axstd/bus-pci"
warning: value assigned to `flags` is never read
  --> crates/kernel_guard/src/arch/loongarch64.rs:17:53
   |
17 |     unsafe { asm!("csrxchg {}, {}, 0x0", inout(reg) flags, in(reg) IE_BIT) };
   |                                                     ^^^^^
   |
   = help: maybe it is overwritten before being read?
   = note: `#[warn(unused_assignments)]` on by default

warning: `kernel_guard` (lib) generated 1 warning
warning: the feature `const_maybe_uninit_zeroed` has been stable since 1.75.0 and no longer requires an attribute to enable
  --> modules/axhal/src/lib.rs:30:12
   |
30 | #![feature(const_maybe_uninit_zeroed)]
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(stable_features)]` on by default

warning: `axhal` (lib) generated 1 warning
warning: the feature `ip_in_core` has been stable since 1.77.0-nightly and no longer requires an attribute to enable
 --> api/arceos_api/src/lib.rs:6:12
  |
6 | #![feature(ip_in_core)]
  |            ^^^^^^^^^^
  |
  = note: `#[warn(stable_features)]` on by default

warning: `arceos_api` (lib) generated 1 warning
warning: the feature `ip_in_core` has been stable since 1.77.0-nightly and no longer requires an attribute to enable
  --> ulib/axstd/src/lib.rs:52:12
   |
52 | #![feature(ip_in_core)]
   |            ^^^^^^^^^^
   |
   = note: `#[warn(stable_features)]` on by default

warning: `axstd` (lib) generated 1 warning
    Finished release [optimized] target(s) in 0.11s
rust-objcopy --binary-architecture=loongarch64 apps/helloworld/helloworld_loongarch64-qemu-virt.elf --strip-all -O binary apps/helloworld/helloworld_loongarch64-qemu-virt.bin
    Running on qemu...
qemu-system-loongarch64 -m 1G -smp 1 -bios tools/la64/loongarch_bios_0310.bin -kernel apps/helloworld/helloworld_loongarch64-qemu-virt.elf -nographic -vga none

Could you offer some help or advice with it? Thanks a lot! @Godones

The Qemu version should be:

❯ qemu-system-loongarch64 --version
QEMU emulator version 6.2.50 (v6.0.0-7567-gac069a8ffb)
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

You can complie it according to tools/la64/la-qemu.sh or https://github.com/foxsen/qemu-loongarch-runenv/

@@ -24,7 +24,17 @@ qemu_args-aarch64 := \
-machine virt \
-kernel $(OUT_BIN)

LOONGARCH_BIOS = tools/la64/loongarch_bios_0310.bin
qemu_args-loongarch64 := \
-bios $(LOONGARCH_BIOS) \
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that it can be boot upon default bios provided by QEMU 8.1.94,
I'll try to verify other applications later, why not just delete this line?
屏幕截图 2023-12-26 141649

Copy link
Author

Choose a reason for hiding this comment

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

There should be no bios here.

image

The first instruction is not located in the BIOS.

Copy link
Author

Choose a reason for hiding this comment

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

The latest version of la qemu seems to have changed the memory layout, which is causing some loading errors. Also, We need to use the environment prepared by the BIOS for us, such as multi-core startup.
image

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I tried both SMP=2 and SMP=4 and they got stucked at different place.
Sorry I haven't try your provided QEMU emulator, I appreciate and admire your excellent work.
But @equation314 suggested that code should run on mainstream QEMU version (by the way I personally do not recommend that the system itself should rely a special BIOS version as loongaarch currently provides), maybe we should have a little discussion about it?

Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to use mainline QEMU with the specific BIOS?

Copy link
Author

Choose a reason for hiding this comment

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

I will try it.

Copy link
Author

Choose a reason for hiding this comment

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

Now, we can use the mainline QEMU for la64.
I discussed the issue of mainline qemu with some teachers at loongarch. They said that mainline qemu cannot currently boot the ELF format kernel. because our kernel is not as complex as Linux, we do not need the information passed by the bios, so here I chose to remove the BIOS dependency and start the kernel directly.

Copy link
Author

Choose a reason for hiding this comment

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

I tested it on qemu-8.2.0. The latest rust tool chain seems to cause several programs under apps/c/pthread to fail.

Copy link
Member

@equation314 equation314 Dec 27, 2023

Choose a reason for hiding this comment

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

Could you provide a list of the current support status of apps?

Copy link
Author

Choose a reason for hiding this comment

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

These apps have not been tested: apps/c/iperf|redis|sqlite3. Other apps have been tested.

Rewrite boot code.
Add pci address space definition
Fix word errors for platforms/*.toml
@@ -0,0 +1,9 @@
/// Shutdown the whole system, including all CPUs.
pub fn terminate() -> ! {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any instruction to exit QEMU? This is necessary for CI.

Copy link
Author

Choose a reason for hiding this comment

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

I didn't find relevant information on the Internet. Maybe I can ask the engineers at la.

Copy link
Author

Choose a reason for hiding this comment

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

Mainline qemu does not have an exit function. Is it possible to use other means to exit qemu from the outside?

Copy link
Member

@equation314 equation314 Dec 27, 2023

Choose a reason for hiding this comment

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

This doc needs to be updated.

Copy link
Member

Choose a reason for hiding this comment

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

This script is no longer in use.

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.

3 participants