Browse Source

feat(ebpf): Implement FromPtRegs for mips

Signed-off-by: Dave Tucker <[email protected]>
Dave Tucker 1 month ago
parent
commit
1ccac3c135

+ 1 - 1
ebpf/aya-ebpf/build.rs

@@ -13,7 +13,7 @@ fn main() {
         }
         println!("cargo:rustc-cfg=bpf_target_arch=\"{arch}\"");
     }
-    println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\",\"powerpc64\",\"s390x\"))");
+    println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\",\"powerpc64\",\"s390x\",\"mips\"))");
     println!("cargo::rustc-check-cfg=cfg(unstable)");
 }
 

+ 49 - 1
ebpf/aya-ebpf/src/args.rs

@@ -1,7 +1,8 @@
 #[cfg(any(
     bpf_target_arch = "x86_64",
     bpf_target_arch = "arm",
-    bpf_target_arch = "powerpc64"
+    bpf_target_arch = "powerpc64",
+    bpf_target_arch = "mips",
 ))]
 use crate::bindings::pt_regs;
 // aarch64 uses user_pt_regs instead of pt_regs
@@ -203,6 +204,22 @@ impl<T> FromPtRegs for *const T {
     }
 }
 
+#[cfg(bpf_target_arch = "mips")]
+impl<T> FromPtRegs for *const T {
+    fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
+        // Assume N64 ABI like libbpf does.
+        if n <= 7 {
+            unsafe { bpf_probe_read(&ctx.regs[n + 4]).map(|v| v as *const _).ok() }
+        } else {
+            None
+        }
+    }
+
+    fn from_retval(ctx: &pt_regs) -> Option<Self> {
+        unsafe { bpf_probe_read(&ctx.regs[31]).map(|v| v as *const _).ok() }
+    }
+}
+
 #[cfg(bpf_target_arch = "x86_64")]
 impl<T> FromPtRegs for *mut T {
     fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
@@ -303,6 +320,22 @@ impl<T> FromPtRegs for *mut T {
     }
 }
 
+#[cfg(bpf_target_arch = "mips")]
+impl<T> FromPtRegs for *mut T {
+    fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
+        // Assume N64 ABI like libbpf does.
+        if n <= 7 {
+            unsafe { bpf_probe_read(&ctx.regs[n + 4]).map(|v| v as *mut _).ok() }
+        } else {
+            None
+        }
+    }
+
+    fn from_retval(ctx: &pt_regs) -> Option<Self> {
+        unsafe { bpf_probe_read(&ctx.regs[31]).map(|v| v as *mut _).ok() }
+    }
+}
+
 /// Helper macro to implement [`FromPtRegs`] for a primitive type.
 macro_rules! impl_from_pt_regs {
     ($type:ident) => {
@@ -405,6 +438,21 @@ macro_rules! impl_from_pt_regs {
                 Some(ctx.gprs[2] as *const $type as _)
             }
         }
+
+        #[cfg(bpf_target_arch = "mips")]
+        impl FromPtRegs for $type {
+            fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
+                if n <= 7 {
+                    Some(ctx.regs[n + 4] as *const $type as _)
+                } else {
+                    None
+                }
+            }
+
+            fn from_retval(ctx: &pt_regs) -> Option<Self> {
+                Some(ctx.regs[31] as *const $type as _)
+            }
+        }
     };
 }
 

+ 2 - 1
ebpf/aya-ebpf/src/programs/probe.rs

@@ -3,7 +3,8 @@ use core::ffi::c_void;
 #[cfg(any(
     bpf_target_arch = "x86_64",
     bpf_target_arch = "arm",
-    bpf_target_arch = "powerpc64"
+    bpf_target_arch = "powerpc64",
+    bpf_target_arch = "mips"
 ))]
 use crate::bindings::pt_regs;
 // aarch64 uses user_pt_regs instead of pt_regs

+ 2 - 1
ebpf/aya-ebpf/src/programs/retprobe.rs

@@ -3,7 +3,8 @@ use core::ffi::c_void;
 #[cfg(any(
     bpf_target_arch = "x86_64",
     bpf_target_arch = "arm",
-    bpf_target_arch = "powerpc64"
+    bpf_target_arch = "powerpc64",
+    bpf_target_arch = "mips"
 ))]
 use crate::bindings::pt_regs;
 // aarch64 uses user_pt_regs instead of pt_regs