Skip to content

Commit 8f5a684

Browse files
authored
Merge pull request #4 from Godones/master
add loongarch64 arch support
2 parents f2563d4 + 68e1f8a commit 8f5a684

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ jobs:
5151
i686-unknown-linux-gnu,
5252
aarch64-unknown-linux-gnu,
5353
riscv64gc-unknown-linux-gnu,
54+
loongarch64-unknown-linux-gnu,
5455
]
5556
steps:
5657
- name: checkout
5758
uses: actions/checkout@v4
5859
- name: Set up cross
59-
run: cargo install cross
60+
run: cargo install cross --git https://github.com/cross-rs/cross
6061
- name: Build all features
6162
run: cross build --verbose --all-features --target ${{ matrix.target }}
6263
- name: Run tests

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/arch/loongarch64.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! loongarch64 arch-sepcific implementations
2+
3+
use crate::{JumpEntry, JumpLabelType};
4+
5+
/// Length of jump instruction to be replaced
6+
pub const ARCH_JUMP_INS_LENGTH: usize = 4;
7+
8+
const LOONGARCH64_INSN_NOP: u32 = 0x03400000;
9+
const LOONGARCH64_INSN_B: u32 = 0x50000000;
10+
/// New instruction generated according to jump label type and jump entry
11+
#[inline(always)]
12+
pub fn arch_jump_entry_instruction(
13+
jump_label_type: JumpLabelType,
14+
jump_entry: &JumpEntry,
15+
) -> [u8; ARCH_JUMP_INS_LENGTH] {
16+
match jump_label_type {
17+
// 010100 [IMM]
18+
// opcode I26[15:0] I26[25:16]
19+
JumpLabelType::Jmp => {
20+
// Note that loongarch64 only supports relative address within +/-128MB.
21+
// In current implementation, this assumption is always hold.
22+
let relative_addr = (jump_entry.target_addr() - jump_entry.code_addr()) as u32;
23+
// MASK 25:16 = 0b_0000_0011_1111_1111_0000_0000_0000_0000 = 0x03FF0000
24+
// MASK 15:0 = 0b_0000_0000_0000_0000_1111_1111_1111_1111 = 0x0000FFFF
25+
let mut b = LOONGARCH64_INSN_B;
26+
let relative_addr = relative_addr >> 2;
27+
b |= ((relative_addr & 0x03FF0000) >> 16) | ((relative_addr & 0x0000FFFF) << 10);
28+
b.to_ne_bytes()
29+
}
30+
JumpLabelType::Nop => LOONGARCH64_INSN_NOP.to_ne_bytes(),
31+
}
32+
}
33+
34+
#[doc(hidden)]
35+
#[macro_export]
36+
macro_rules! arch_static_key_init_nop_asm_template {
37+
() => {
38+
::core::concat!(
39+
r#"
40+
2:
41+
nop
42+
.pushsection "#,
43+
$crate::os_static_key_sec_name_attr!(),
44+
r#"
45+
.balign 8
46+
.quad 2b - .
47+
.quad {0} - .
48+
.quad {1} + {2} - .
49+
.popsection
50+
"#
51+
)
52+
};
53+
}
54+
55+
#[doc(hidden)]
56+
#[macro_export]
57+
macro_rules! arch_static_key_init_jmp_asm_template {
58+
() => {
59+
::core::concat!(
60+
r#"
61+
2:
62+
b {0}
63+
.pushsection "#,
64+
$crate::os_static_key_sec_name_attr!(),
65+
r#"
66+
.balign 8
67+
.quad 2b - .
68+
.quad {0} - .
69+
.quad {1} + {2} - .
70+
.popsection
71+
"#
72+
)
73+
};
74+
}

src/arch/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub use aarch64::*;
1717

1818
#[cfg(target_arch = "riscv64")]
1919
mod riscv64;
20-
2120
#[cfg(target_arch = "riscv64")]
2221
pub use riscv64::*;
22+
23+
#[cfg(target_arch = "loongarch64")]
24+
mod loongarch64;
25+
#[cfg(target_arch = "loongarch64")]
26+
pub use loongarch64::*;

0 commit comments

Comments
 (0)