Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 74751d6

Browse files
committedJul 8, 2024·
Add loongarch64 support
1 parent 5662b1f commit 74751d6

File tree

15 files changed

+329
-18
lines changed

15 files changed

+329
-18
lines changed
 

‎Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ once_cell = { version = "1.5.2", optional = true }
3535
# addition to the libc backend. The linux_raw backend is used by default. The
3636
# libc backend can be selected via adding `--cfg=rustix_use_libc` to
3737
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
38-
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
38+
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), target_arch="loongarch64")))'.dependencies]
3939
linux-raw-sys = { version = "0.4.14", default-features = false, features = ["general", "errno", "ioctl", "no_std", "elf"] }
4040
libc_errno = { package = "errno", version = "0.3.8", default-features = false, optional = true }
4141
libc = { version = "0.2.153", default-features = false, optional = true }
@@ -44,7 +44,7 @@ libc = { version = "0.2.153", default-features = false, optional = true }
4444
#
4545
# On all other Unix-family platforms, and under Miri, we always use the libc
4646
# backend, so enable its dependencies unconditionally.
47-
[target.'cfg(all(not(windows), any(rustix_use_libc, miri, not(all(target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies]
47+
[target.'cfg(all(not(windows), any(rustix_use_libc, miri, not(all(target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), target_arch = "loongarch64"))))))'.dependencies]
4848
libc_errno = { package = "errno", version = "0.3.8", default-features = false }
4949
libc = { version = "0.2.153", default-features = false }
5050

+265
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
//! loongarch64 Linux system calls.
2+
3+
use crate::backend::reg::{
4+
ArgReg, FromAsm, RetReg, SyscallNumber, ToAsm, A0, A1, A2, A3, A4, A5, R0,
5+
};
6+
use core::arch::asm;
7+
8+
#[inline]
9+
pub(in crate::backend) unsafe fn syscall0_readonly(nr: SyscallNumber<'_>) -> RetReg<R0> {
10+
let r0;
11+
asm!(
12+
"syscall 0",
13+
in("$a7") nr.to_asm(),
14+
lateout("$a0") r0,
15+
options(nostack, preserves_flags, readonly)
16+
);
17+
FromAsm::from_asm(r0)
18+
}
19+
20+
#[inline]
21+
pub(in crate::backend) unsafe fn syscall1(nr: SyscallNumber<'_>, a0: ArgReg<'_, A0>) -> RetReg<R0> {
22+
let r0;
23+
asm!(
24+
"syscall 0",
25+
in("$a7") nr.to_asm(),
26+
inlateout("$a0") a0.to_asm() => r0,
27+
options(nostack, preserves_flags)
28+
);
29+
FromAsm::from_asm(r0)
30+
}
31+
32+
#[inline]
33+
pub(in crate::backend) unsafe fn syscall1_readonly(
34+
nr: SyscallNumber<'_>,
35+
a0: ArgReg<'_, A0>,
36+
) -> RetReg<R0> {
37+
let r0;
38+
asm!(
39+
"syscall 0",
40+
in("$a7") nr.to_asm(),
41+
inlateout("$a0") a0.to_asm() => r0,
42+
options(nostack, preserves_flags, readonly)
43+
);
44+
FromAsm::from_asm(r0)
45+
}
46+
47+
#[inline]
48+
pub(in crate::backend) unsafe fn syscall1_noreturn(nr: SyscallNumber<'_>, a0: ArgReg<'_, A0>) -> ! {
49+
asm!(
50+
"syscall 0",
51+
in("$a7") nr.to_asm(),
52+
in("$a0") a0.to_asm(),
53+
options(noreturn)
54+
);
55+
}
56+
57+
#[inline]
58+
pub(in crate::backend) unsafe fn syscall2(
59+
nr: SyscallNumber<'_>,
60+
a0: ArgReg<'_, A0>,
61+
a1: ArgReg<'_, A1>,
62+
) -> RetReg<R0> {
63+
let r0;
64+
asm!(
65+
"syscall 0",
66+
in("$a7") nr.to_asm(),
67+
inlateout("$a0") a0.to_asm() => r0,
68+
in("$a1") a1.to_asm(),
69+
options(nostack, preserves_flags)
70+
);
71+
FromAsm::from_asm(r0)
72+
}
73+
74+
#[inline]
75+
pub(in crate::backend) unsafe fn syscall2_readonly(
76+
nr: SyscallNumber<'_>,
77+
a0: ArgReg<'_, A0>,
78+
a1: ArgReg<'_, A1>,
79+
) -> RetReg<R0> {
80+
let r0;
81+
asm!(
82+
"syscall 0",
83+
in("$a7") nr.to_asm(),
84+
inlateout("$a0") a0.to_asm() => r0,
85+
in("$a1") a1.to_asm(),
86+
options(nostack, preserves_flags, readonly)
87+
);
88+
FromAsm::from_asm(r0)
89+
}
90+
91+
#[inline]
92+
pub(in crate::backend) unsafe fn syscall3(
93+
nr: SyscallNumber<'_>,
94+
a0: ArgReg<'_, A0>,
95+
a1: ArgReg<'_, A1>,
96+
a2: ArgReg<'_, A2>,
97+
) -> RetReg<R0> {
98+
let r0;
99+
asm!(
100+
"syscall 0",
101+
in("$a7") nr.to_asm(),
102+
inlateout("$a0") a0.to_asm() => r0,
103+
in("$a1") a1.to_asm(),
104+
in("$a2") a2.to_asm(),
105+
options(nostack, preserves_flags)
106+
);
107+
FromAsm::from_asm(r0)
108+
}
109+
110+
#[inline]
111+
pub(in crate::backend) unsafe fn syscall3_readonly(
112+
nr: SyscallNumber<'_>,
113+
a0: ArgReg<'_, A0>,
114+
a1: ArgReg<'_, A1>,
115+
a2: ArgReg<'_, A2>,
116+
) -> RetReg<R0> {
117+
let r0;
118+
asm!(
119+
"syscall 0",
120+
in("$a7") nr.to_asm(),
121+
inlateout("$a0") a0.to_asm() => r0,
122+
in("$a1") a1.to_asm(),
123+
in("$a2") a2.to_asm(),
124+
options(nostack, preserves_flags, readonly)
125+
);
126+
FromAsm::from_asm(r0)
127+
}
128+
129+
#[inline]
130+
pub(in crate::backend) unsafe fn syscall4(
131+
nr: SyscallNumber<'_>,
132+
a0: ArgReg<'_, A0>,
133+
a1: ArgReg<'_, A1>,
134+
a2: ArgReg<'_, A2>,
135+
a3: ArgReg<'_, A3>,
136+
) -> RetReg<R0> {
137+
let r0;
138+
asm!(
139+
"syscall 0",
140+
in("$a7") nr.to_asm(),
141+
inlateout("$a0") a0.to_asm() => r0,
142+
in("$a1") a1.to_asm(),
143+
in("$a2") a2.to_asm(),
144+
in("$a3") a3.to_asm(),
145+
options(nostack, preserves_flags)
146+
);
147+
FromAsm::from_asm(r0)
148+
}
149+
150+
#[inline]
151+
pub(in crate::backend) unsafe fn syscall4_readonly(
152+
nr: SyscallNumber<'_>,
153+
a0: ArgReg<'_, A0>,
154+
a1: ArgReg<'_, A1>,
155+
a2: ArgReg<'_, A2>,
156+
a3: ArgReg<'_, A3>,
157+
) -> RetReg<R0> {
158+
let r0;
159+
asm!(
160+
"syscall 0",
161+
in("$a7") nr.to_asm(),
162+
inlateout("$a0") a0.to_asm() => r0,
163+
in("$a1") a1.to_asm(),
164+
in("$a2") a2.to_asm(),
165+
in("$a3") a3.to_asm(),
166+
options(nostack, preserves_flags, readonly)
167+
);
168+
FromAsm::from_asm(r0)
169+
}
170+
171+
#[inline]
172+
pub(in crate::backend) unsafe fn syscall5(
173+
nr: SyscallNumber<'_>,
174+
a0: ArgReg<'_, A0>,
175+
a1: ArgReg<'_, A1>,
176+
a2: ArgReg<'_, A2>,
177+
a3: ArgReg<'_, A3>,
178+
a4: ArgReg<'_, A4>,
179+
) -> RetReg<R0> {
180+
let r0;
181+
asm!(
182+
"syscall 0",
183+
in("$a7") nr.to_asm(),
184+
inlateout("$a0") a0.to_asm() => r0,
185+
in("$a1") a1.to_asm(),
186+
in("$a2") a2.to_asm(),
187+
in("$a3") a3.to_asm(),
188+
in("$a4") a4.to_asm(),
189+
options(nostack, preserves_flags)
190+
);
191+
FromAsm::from_asm(r0)
192+
}
193+
194+
#[inline]
195+
pub(in crate::backend) unsafe fn syscall5_readonly(
196+
nr: SyscallNumber<'_>,
197+
a0: ArgReg<'_, A0>,
198+
a1: ArgReg<'_, A1>,
199+
a2: ArgReg<'_, A2>,
200+
a3: ArgReg<'_, A3>,
201+
a4: ArgReg<'_, A4>,
202+
) -> RetReg<R0> {
203+
let r0;
204+
asm!(
205+
"syscall 0",
206+
in("$a7") nr.to_asm(),
207+
inlateout("$a0") a0.to_asm() => r0,
208+
in("$a1") a1.to_asm(),
209+
in("$a2") a2.to_asm(),
210+
in("$a3") a3.to_asm(),
211+
in("$a4") a4.to_asm(),
212+
options(nostack, preserves_flags, readonly)
213+
);
214+
FromAsm::from_asm(r0)
215+
}
216+
217+
#[inline]
218+
pub(in crate::backend) unsafe fn syscall6(
219+
nr: SyscallNumber<'_>,
220+
a0: ArgReg<'_, A0>,
221+
a1: ArgReg<'_, A1>,
222+
a2: ArgReg<'_, A2>,
223+
a3: ArgReg<'_, A3>,
224+
a4: ArgReg<'_, A4>,
225+
a5: ArgReg<'_, A5>,
226+
) -> RetReg<R0> {
227+
let r0;
228+
asm!(
229+
"syscall 0",
230+
in("$a7") nr.to_asm(),
231+
inlateout("$a0") a0.to_asm() => r0,
232+
in("$a1") a1.to_asm(),
233+
in("$a2") a2.to_asm(),
234+
in("$a3") a3.to_asm(),
235+
in("$a4") a4.to_asm(),
236+
in("$a5") a5.to_asm(),
237+
options(nostack, preserves_flags)
238+
);
239+
FromAsm::from_asm(r0)
240+
}
241+
242+
#[inline]
243+
pub(in crate::backend) unsafe fn syscall6_readonly(
244+
nr: SyscallNumber<'_>,
245+
a0: ArgReg<'_, A0>,
246+
a1: ArgReg<'_, A1>,
247+
a2: ArgReg<'_, A2>,
248+
a3: ArgReg<'_, A3>,
249+
a4: ArgReg<'_, A4>,
250+
a5: ArgReg<'_, A5>,
251+
) -> RetReg<R0> {
252+
let r0;
253+
asm!(
254+
"syscall 0",
255+
in("$a7") nr.to_asm(),
256+
inlateout("$a0") a0.to_asm() => r0,
257+
in("$a1") a1.to_asm(),
258+
in("$a2") a2.to_asm(),
259+
in("$a3") a3.to_asm(),
260+
in("$a4") a4.to_asm(),
261+
in("$a5") a5.to_asm(),
262+
options(nostack, preserves_flags, readonly)
263+
);
264+
FromAsm::from_asm(r0)
265+
}

‎src/backend/linux_raw/arch/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub(in crate::backend) mod asm;
4141
#[cfg(any(
4242
target_arch = "arm",
4343
target_arch = "aarch64",
44+
target_arch = "loongarch64",
4445
target_arch = "mips",
4546
target_arch = "mips32r6",
4647
target_arch = "mips64",

‎src/backend/linux_raw/conv.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ pub(super) fn opt_mut<T: Sized, Num: ArgNumber>(t: Option<&mut T>) -> ArgReg<'_,
235235

236236
/// Convert an optional immutable reference into a `usize` for passing to a
237237
/// syscall.
238-
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
238+
#[cfg(any(
239+
target_arch = "aarch64",
240+
target_arch = "loongarch64",
241+
target_arch = "riscv64"
242+
))]
239243
#[inline]
240244
pub(super) fn opt_ref<T: Sized, Num: ArgNumber>(t: Option<&T>) -> ArgReg<'_, Num> {
241245
// This optimizes into the equivalent of `transmute(t)`, and has the

‎src/backend/linux_raw/fs/dir.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::fd::{AsFd, BorrowedFd, OwnedFd};
22
use crate::ffi::{CStr, CString};
3-
use crate::fs::{
4-
fcntl_getfl, fstat, fstatfs, fstatvfs, openat, FileType, Mode, OFlags, Stat, StatFs, StatVfs,
5-
};
3+
use crate::fs::{fcntl_getfl, fstatfs, fstatvfs, openat, FileType, Mode, OFlags, StatFs, StatVfs};
4+
#[cfg(not(target_arch = "loongarch64"))]
5+
use crate::fs::{fstat, Stat};
66
use crate::io;
77
#[cfg(feature = "process")]
88
use crate::process::fchdir;
@@ -215,6 +215,7 @@ impl Dir {
215215
}
216216

217217
/// `fstat(self)`
218+
#[cfg(not(target_arch = "loongarch64"))]
218219
#[inline]
219220
pub fn stat(&self) -> io::Result<Stat> {
220221
fstat(&self.fd)

0 commit comments

Comments
 (0)
Please sign in to comment.