Skip to content

Commit 531f0f9

Browse files
committed
Add getregs()/setregs()/getregset()/setregset() support for loongarch64-linux-gnu/musl
1 parent a1384d0 commit 531f0f9

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

changelog/2507.added.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `getregs()`/`setregs()`/`getregset()`/`setregset()` support for loongarch64-linux-gnu/musl

src/sys/ptrace/linux.rs

+54-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ pub type AddressType = *mut ::libc::c_void;
1414
target_os = "linux",
1515
any(
1616
all(
17-
any(target_arch = "x86_64", target_arch = "aarch64"),
17+
any(
18+
target_arch = "x86_64",
19+
target_arch = "aarch64",
20+
target_arch = "loongarch64",
21+
),
1822
any(target_env = "gnu", target_env = "musl")
1923
),
2024
all(target_arch = "x86", target_env = "gnu"),
@@ -179,6 +183,7 @@ libc_enum! {
179183
target_arch = "x86",
180184
target_arch = "aarch64",
181185
target_arch = "riscv64",
186+
target_arch = "loongarch64",
182187
)
183188
))]
184189
libc_enum! {
@@ -202,6 +207,7 @@ libc_enum! {
202207
target_arch = "x86",
203208
target_arch = "aarch64",
204209
target_arch = "riscv64",
210+
target_arch = "loongarch64",
205211
)
206212
))]
207213
/// Represents register set areas, such as general-purpose registers or
@@ -227,6 +233,7 @@ pub unsafe trait RegisterSet {
227233
target_arch = "x86",
228234
target_arch = "aarch64",
229235
target_arch = "riscv64",
236+
target_arch = "loongarch64",
230237
)
231238
))]
232239
/// Register sets used in [`getregset`] and [`setregset`]
@@ -254,6 +261,8 @@ pub mod regset {
254261
type Regs = libc::user_fpsimd_struct;
255262
#[cfg(target_arch = "riscv64")]
256263
type Regs = libc::__riscv_mc_d_ext_state;
264+
#[cfg(target_arch = "loongarch64")]
265+
type Regs = libc::user_fp_struct;
257266
}
258267
}
259268

@@ -335,10 +344,20 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
335344
target_os = "linux",
336345
any(
337346
all(
338-
target_arch = "aarch64",
339-
any(target_env = "gnu", target_env = "musl")
347+
target_env = "musl",
348+
any(
349+
target_arch = "aarch64",
350+
target_arch = "loongarch64",
351+
)
340352
),
341-
all(target_arch = "riscv64", target_env = "gnu")
353+
all(
354+
target_env = "gnu",
355+
any(
356+
target_arch = "aarch64",
357+
target_arch = "loongarch64",
358+
target_arch = "riscv64",
359+
)
360+
)
342361
)
343362
))]
344363
pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
@@ -355,10 +374,18 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
355374
target_arch = "x86_64",
356375
target_arch = "x86",
357376
target_arch = "aarch64",
358-
target_arch = "riscv64"
377+
target_arch = "riscv64",
378+
target_arch = "loongarch64",
379+
359380
)
360381
),
361-
all(target_env = "musl", target_arch = "aarch64")
382+
all(
383+
target_env = "musl",
384+
any(
385+
target_arch = "aarch64",
386+
target_arch = "loongarch64",
387+
)
388+
)
362389
)
363390
))]
364391
pub fn getregset<S: RegisterSet>(pid: Pid) -> Result<S::Regs> {
@@ -420,9 +447,19 @@ pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
420447
any(
421448
all(
422449
target_env = "gnu",
423-
any(target_arch = "aarch64", target_arch = "riscv64")
450+
any(
451+
target_arch = "aarch64",
452+
target_arch = "riscv64",
453+
target_arch = "loongarch64",
454+
)
424455
),
425-
all(target_env = "musl", target_arch = "aarch64")
456+
all(
457+
target_env = "musl",
458+
any(
459+
target_arch = "aarch64",
460+
target_arch = "loongarch64",
461+
)
462+
)
426463
)
427464
))]
428465
pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
@@ -439,10 +476,17 @@ pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
439476
target_arch = "x86_64",
440477
target_arch = "x86",
441478
target_arch = "aarch64",
442-
target_arch = "riscv64"
479+
target_arch = "riscv64",
480+
target_arch = "loongarch64"
443481
)
444482
),
445-
all(target_env = "musl", target_arch = "aarch64")
483+
all(
484+
target_env = "musl",
485+
any(
486+
target_arch = "aarch64",
487+
target_arch = "loongarch64"
488+
)
489+
)
446490
)
447491
))]
448492
pub fn setregset<S: RegisterSet>(pid: Pid, mut regs: S::Regs) -> Result<()> {

test/sys/test_ptrace.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ fn test_ptrace_interrupt() {
185185
target_arch = "x86",
186186
target_arch = "aarch64",
187187
target_arch = "riscv64",
188+
target_arch = "loongarch64",
188189
)
189190
))]
190191
#[test]
@@ -239,6 +240,10 @@ fn test_ptrace_syscall() {
239240
let get_syscall_id =
240241
|| ptrace::getregs(child).unwrap().a7 as libc::c_long;
241242

243+
#[cfg(target_arch = "loongarch64")]
244+
let get_syscall_id =
245+
|| ptrace::getregs(child).unwrap().regs[11] as libc::c_long;
246+
242247
// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
243248
#[cfg(target_arch = "x86_64")]
244249
let rax_offset = offset_of!(libc::user_regs_struct, orig_rax);
@@ -299,10 +304,14 @@ fn test_ptrace_syscall() {
299304
target_arch = "x86_64",
300305
target_arch = "x86",
301306
target_arch = "aarch64",
307+
target_arch = "loongarch64",
302308
target_arch = "riscv64"
303309
)
304310
),
305-
all(target_env = "musl", target_arch = "aarch64")
311+
all(
312+
target_env = "musl",
313+
any(target_arch = "aarch64", target_arch = "loongarch64")
314+
)
306315
)
307316
))]
308317
#[test]
@@ -348,6 +357,9 @@ fn test_ptrace_regsets() {
348357
(&mut regstruct.regs[16], &mut fpregstruct.vregs[5]);
349358
#[cfg(target_arch = "riscv64")]
350359
let (reg, fpreg) = (&mut regstruct.t1, &mut fpregstruct.__f[5]);
360+
#[cfg(target_arch = "loongarch64")]
361+
let (reg, fpreg) =
362+
(&mut regstruct.regs[12], &mut fpregstruct.fpr[5]);
351363

352364
*reg = 0xdeadbeefu32 as _;
353365
*fpreg = 0xfeedfaceu32 as _;
@@ -364,6 +376,8 @@ fn test_ptrace_regsets() {
364376
let (reg, fpreg) = (regstruct.regs[16], fpregstruct.vregs[5]);
365377
#[cfg(target_arch = "riscv64")]
366378
let (reg, fpreg) = (regstruct.t1, fpregstruct.__f[5]);
379+
#[cfg(target_arch = "loongarch64")]
380+
let (reg, fpreg) = (regstruct.regs[12], fpregstruct.fpr[5]);
367381
assert_eq!(reg, 0xdeadbeefu32 as _);
368382
assert_eq!(fpreg, 0xfeedfaceu32 as _);
369383

0 commit comments

Comments
 (0)