Skip to content

Commit

Permalink
Add getregs()/setregs()/getregset()/setregset() support for l…
Browse files Browse the repository at this point in the history
…oongarch64-linux-gnu/musl
  • Loading branch information
Fearyncess committed Sep 18, 2024
1 parent a0869f9 commit 80550b6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
64 changes: 54 additions & 10 deletions src/sys/ptrace/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub type AddressType = *mut ::libc::c_void;
target_os = "linux",
any(
all(
any(target_arch = "x86_64", target_arch = "aarch64"),
any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64",
),
any(target_env = "gnu", target_env = "musl")
),
all(target_arch = "x86", target_env = "gnu"),
Expand Down Expand Up @@ -179,6 +183,7 @@ libc_enum! {
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "loongarch64",
)
))]
libc_enum! {
Expand All @@ -202,6 +207,7 @@ libc_enum! {
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "loongarch64",
)
))]
/// Represents register set areas, such as general-purpose registers or
Expand All @@ -227,6 +233,7 @@ pub unsafe trait RegisterSet {
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "loongarch64",
)
))]
/// Register sets used in [`getregset`] and [`setregset`]
Expand Down Expand Up @@ -254,6 +261,8 @@ pub mod regset {
type Regs = libc::user_fpsimd_struct;
#[cfg(target_arch = "riscv64")]
type Regs = libc::__riscv_mc_d_ext_state;
#[cfg(target_arch = "loongarch64")]
type Regs = libc::user_fp_struct;
}
}

Expand Down Expand Up @@ -335,10 +344,20 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
target_os = "linux",
any(
all(
target_arch = "aarch64",
any(target_env = "gnu", target_env = "musl")
target_env = "musl",
any(
target_arch = "aarch64",
target_arch = "loongarch64",
)
),
all(target_arch = "riscv64", target_env = "gnu")
all(
target_env = "gnu",
any(
target_arch = "aarch64",
target_arch = "loongarch64",
target_arch = "riscv64",
)
)
)
))]
pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
Expand All @@ -355,10 +374,18 @@ pub fn getregs(pid: Pid) -> Result<user_regs_struct> {
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64"
target_arch = "riscv64",
target_arch = "loongarch64",
)
),
all(target_env = "musl", target_arch = "aarch64")
all(
target_env = "musl",
any(
target_arch = "aarch64",
target_arch = "loongarch64",
)
)
)
))]
pub fn getregset<S: RegisterSet>(pid: Pid) -> Result<S::Regs> {
Expand Down Expand Up @@ -420,9 +447,19 @@ pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
any(
all(
target_env = "gnu",
any(target_arch = "aarch64", target_arch = "riscv64")
any(
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "loongarch64",
)
),
all(target_env = "musl", target_arch = "aarch64")
all(
target_env = "musl",
any(
target_arch = "aarch64",
target_arch = "loongarch64",
)
)
)
))]
pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
Expand All @@ -439,10 +476,17 @@ pub fn setregs(pid: Pid, regs: user_regs_struct) -> Result<()> {
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64"
target_arch = "riscv64",
target_arch = "loongarch64"
)
),
all(target_env = "musl", target_arch = "aarch64")
all(
target_env = "musl",
any(
target_arch = "aarch64",
target_arch = "loongarch64"
)
)
)
))]
pub fn setregset<S: RegisterSet>(pid: Pid, mut regs: S::Regs) -> Result<()> {
Expand Down
18 changes: 17 additions & 1 deletion test/sys/test_ptrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ fn test_ptrace_interrupt() {
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "loongarch64",
)
))]
#[test]
Expand Down Expand Up @@ -239,6 +240,10 @@ fn test_ptrace_syscall() {
let get_syscall_id =
|| ptrace::getregs(child).unwrap().a7 as libc::c_long;

#[cfg(target_arch = "loongarch64")]
let get_syscall_id =
|| ptrace::getregs(child).unwrap().regs[11] as libc::c_long;

// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
#[cfg(target_arch = "x86_64")]
let rax_offset = offset_of!(libc::user_regs_struct, orig_rax);
Expand Down Expand Up @@ -299,10 +304,17 @@ fn test_ptrace_syscall() {
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "loongarch64",
target_arch = "riscv64"
)
),
all(target_env = "musl", target_arch = "aarch64")
all(
target_env = "musl",
any(
target_arch = "aarch64",
target_arch = "loongarch64"
)
)
)
))]
#[test]
Expand Down Expand Up @@ -348,6 +360,8 @@ fn test_ptrace_regsets() {
(&mut regstruct.regs[16], &mut fpregstruct.vregs[5]);
#[cfg(target_arch = "riscv64")]
let (reg, fpreg) = (&mut regstruct.t1, &mut fpregstruct.__f[5]);
#[cfg(target_arch = "loongarch64")]
let (reg, fpreg) = (&mut regstruct.regs[12], &mut fpregstruct.fpr[5]);

*reg = 0xdeadbeefu32 as _;
*fpreg = 0xfeedfaceu32 as _;
Expand All @@ -364,6 +378,8 @@ fn test_ptrace_regsets() {
let (reg, fpreg) = (regstruct.regs[16], fpregstruct.vregs[5]);
#[cfg(target_arch = "riscv64")]
let (reg, fpreg) = (regstruct.t1, fpregstruct.__f[5]);
#[cfg(target_arch = "loongarch64")]
let (reg, fpreg) = (regstruct.regs[12], fpregstruct.fpr[5]);
assert_eq!(reg, 0xdeadbeefu32 as _);
assert_eq!(fpreg, 0xfeedfaceu32 as _);

Expand Down

0 comments on commit 80550b6

Please sign in to comment.