Skip to content

Commit

Permalink
aya: add archs powerpc64 and s390x to aya
Browse files Browse the repository at this point in the history
bpfman, a project using aya, has a requirement to support powerpc64 and
s390x architectures. Adding these two architectures to aya.

Signed-off-by: Billy McFall <[email protected]>
  • Loading branch information
Billy99 committed Jun 26, 2024
1 parent bfafe9e commit f4feab5
Show file tree
Hide file tree
Showing 24 changed files with 189 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ linker = "arm-linux-gnueabihf-gcc"

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"

[target.powerpc64le-unknown-linux-gnu]
linker = "powerpc64le-linux-gnu-gcc"

[target.s390x-unknown-linux-gnu]
linker = "s390x-linux-gnu-gcc"
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ jobs:
- aarch64-unknown-linux-gnu
- armv7-unknown-linux-gnueabi
- riscv64gc-unknown-linux-gnu
- powerpc64le-unknown-linux-gnu
- s390x-unknown-linux-gnu
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -134,6 +136,8 @@ jobs:
- aarch64
- arm
- riscv64
- powerpc64
- s390x
target:
- bpfel-unknown-none
- bpfeb-unknown-none
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install headers
run: |
sudo apt -y update
sudo apt -y install libc6-dev libc6-dev-{arm64,armel,riscv64}-cross
sudo apt -y install libc6-dev libc6-dev-{arm64,armel,riscv64,ppc64el,s390x}-cross
- name: Run codegen
run: |
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ default-members = [

# ebpf crates are omitted; they must be built with:
# --target bpfe{b,l}-unknown-none
# CARGO_CFG_BPF_TARGET_ARCH={x86_64,aarch64,arm,riscv64}
# CARGO_CFG_BPF_TARGET_ARCH={x86_64,aarch64,arm,riscv64,powerpc64,s390x}
]

[workspace.package]
Expand Down
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions aya-obj/src/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ mod btf_internal_bindings;
mod linux_bindings_aarch64;
#[cfg(target_arch = "arm")]
mod linux_bindings_armv7;
#[cfg(target_arch = "powerpc64")]
mod linux_bindings_powerpc64;
#[cfg(target_arch = "riscv64")]
mod linux_bindings_riscv64;
#[cfg(target_arch = "s390x")]
mod linux_bindings_s390x;
#[cfg(target_arch = "x86_64")]
mod linux_bindings_x86_64;

Expand All @@ -25,7 +29,11 @@ pub use btf_internal_bindings::{bpf_core_relo, bpf_core_relo_kind, btf_ext_heade
pub use linux_bindings_aarch64::*;
#[cfg(target_arch = "arm")]
pub use linux_bindings_armv7::*;
#[cfg(target_arch = "powerpc64")]
pub use linux_bindings_powerpc64::*;
#[cfg(target_arch = "riscv64")]
pub use linux_bindings_riscv64::*;
#[cfg(target_arch = "s390x")]
pub use linux_bindings_s390x::*;
#[cfg(target_arch = "x86_64")]
pub use linux_bindings_x86_64::*;
2 changes: 1 addition & 1 deletion ebpf/aya-ebpf-bindings/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fn main() {
let arch = arch.split_once('-').map_or(&*arch, |x| x.0);
println!("cargo:rustc-cfg=bpf_target_arch=\"{arch}\"");
}
println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\"))");
println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\",\"powerpc64\",\"s390x\"))");
}
10 changes: 10 additions & 0 deletions ebpf/aya-ebpf-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ mod aarch64;
#[cfg(bpf_target_arch = "riscv64")]
mod riscv64;

#[cfg(bpf_target_arch = "powerpc64")]
mod powerpc64;

#[cfg(bpf_target_arch = "s390x")]
mod s390x;

mod gen {
#[cfg(bpf_target_arch = "aarch64")]
pub use super::aarch64::*;
#[cfg(bpf_target_arch = "arm")]
pub use super::armv7::*;
#[cfg(bpf_target_arch = "powerpc64")]
pub use super::powerpc64::*;
#[cfg(bpf_target_arch = "riscv64")]
pub use super::riscv64::*;
#[cfg(bpf_target_arch = "s390x")]
pub use super::s390x::*;
#[cfg(bpf_target_arch = "x86_64")]
pub use super::x86_64::*;
}
Expand Down
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions ebpf/aya-ebpf-bindings/src/powerpc64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![allow(clippy::all, dead_code)]
pub mod bindings;
pub mod helpers;
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions ebpf/aya-ebpf-bindings/src/s390x/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![allow(clippy::all, dead_code)]
pub mod bindings;
pub mod helpers;
2 changes: 1 addition & 1 deletion ebpf/aya-ebpf-cty/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ fn main() {
let arch = arch.split_once('-').map_or(&*arch, |x| x.0);
println!("cargo:rustc-cfg=bpf_target_arch=\"{arch}\"");
}
println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\"))");
println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\",\"powerpc64\",\"s390x\"))");
println!("cargo::rustc-check-cfg=cfg(target_arch, values(\"asmjs\",\"nvptx\",\"xtensa\"))");
}
6 changes: 6 additions & 0 deletions ebpf/aya-ebpf-cty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ mod ad {
#[cfg(bpf_target_arch = "aarch64")]
pub type c_char = super::c_uchar;

#[cfg(bpf_target_arch = "powerpc64")]
pub type c_char = super::c_uchar;

#[cfg(bpf_target_arch = "riscv64")]
pub type c_char = super::c_uchar;

#[cfg(bpf_target_arch = "s390x")]
pub type c_char = super::c_uchar;

#[cfg(bpf_target_arch = "x86_64")]
pub type c_char = super::c_schar;
}
Expand Down
2 changes: 1 addition & 1 deletion ebpf/aya-ebpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
let arch = arch.split_once('-').map_or(&*arch, |x| x.0);
println!("cargo:rustc-cfg=bpf_target_arch=\"{arch}\"");
}
println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\"))");
println!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(\"x86_64\",\"arm\",\"aarch64\",\"riscv64\",\"powerpc64\",\"s390x\"))");
println!("cargo::rustc-check-cfg=cfg(unstable)");
}

Expand Down
101 changes: 98 additions & 3 deletions ebpf/aya-ebpf/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// aarch64 uses user_pt_regs instead of pt_regs
#[cfg(not(any(bpf_target_arch = "aarch64", bpf_target_arch = "riscv64")))]
#[cfg(any(
bpf_target_arch = "x86_64",
bpf_target_arch = "arm",
bpf_target_arch = "powerpc64"
))]
use crate::bindings::pt_regs;
#[cfg(bpf_target_arch = "aarch64")]
// aarch64 uses user_pt_regs instead of pt_regs
#[cfg(any(bpf_target_arch = "aarch64", bpf_target_arch = "s390x"))]
use crate::bindings::user_pt_regs as pt_regs;
// riscv64 uses user_regs_struct instead of pt_regs
#[cfg(bpf_target_arch = "riscv64")]
use crate::bindings::user_regs_struct as pt_regs;
use crate::{cty::c_void, helpers::bpf_probe_read};
Expand Down Expand Up @@ -168,6 +173,36 @@ impl<T> FromPtRegs for *const T {
}
}

#[cfg(bpf_target_arch = "powerpc64")]
impl<T> FromPtRegs for *const T {
fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
if n <= 7 {
unsafe { bpf_probe_read(&ctx.gpr[3 + n]).map(|v| v as *const _).ok() }
} else {
None
}
}

fn from_retval(ctx: &pt_regs) -> Option<Self> {
unsafe { bpf_probe_read(&ctx.gpr[3]).map(|v| v as *const _).ok() }
}
}

#[cfg(bpf_target_arch = "s390x")]
impl<T> FromPtRegs for *const T {
fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
if n <= 4 {
unsafe { bpf_probe_read(&ctx.gprs[2 + n]).map(|v| v as *const _).ok() }
} else {
None
}
}

fn from_retval(ctx: &pt_regs) -> Option<Self> {
unsafe { bpf_probe_read(&ctx.gprs[2]).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> {
Expand Down Expand Up @@ -238,6 +273,36 @@ impl<T> FromPtRegs for *mut T {
}
}

#[cfg(bpf_target_arch = "powerpc64")]
impl<T> FromPtRegs for *mut T {
fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
if n <= 7 {
unsafe { bpf_probe_read(&ctx.gpr[3 + n]).map(|v| v as *mut _).ok() }
} else {
None
}
}

fn from_retval(ctx: &pt_regs) -> Option<Self> {
unsafe { bpf_probe_read(&ctx.gpr[3]).map(|v| v as *mut _).ok() }
}
}

#[cfg(bpf_target_arch = "s390x")]
impl<T> FromPtRegs for *mut T {
fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
if n <= 4 {
unsafe { bpf_probe_read(&ctx.gprs[2 + n]).map(|v| v as *mut _).ok() }
} else {
None
}
}

fn from_retval(ctx: &pt_regs) -> Option<Self> {
unsafe { bpf_probe_read(&ctx.gprs[2]).map(|v| v as *mut _).ok() }
}
}

/// Helper macro to implement [`FromPtRegs`] for a primitive type.
macro_rules! impl_from_pt_regs {
($type:ident) => {
Expand Down Expand Up @@ -310,6 +375,36 @@ macro_rules! impl_from_pt_regs {
Some(ctx.ra as *const $type as _)
}
}

#[cfg(bpf_target_arch = "powerpc64")]
impl FromPtRegs for $type {
fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
if n <= 7 {
Some(ctx.gpr[3 + n] as *const $type as _)
} else {
None
}
}

fn from_retval(ctx: &pt_regs) -> Option<Self> {
Some(ctx.gpr[3] as *const $type as _)
}
}

#[cfg(bpf_target_arch = "s390x")]
impl FromPtRegs for $type {
fn from_argument(ctx: &pt_regs, n: usize) -> Option<Self> {
if n <= 4 {
Some(ctx.gprs[2 + n] as *const $type as _)
} else {
None
}
}

fn from_retval(ctx: &pt_regs) -> Option<Self> {
Some(ctx.gprs[2] as *const $type as _)
}
}
};
}

Expand Down
10 changes: 8 additions & 2 deletions ebpf/aya-ebpf/src/programs/probe.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use core::ffi::c_void;

#[cfg(not(any(bpf_target_arch = "aarch64", bpf_target_arch = "riscv64")))]
#[cfg(any(
bpf_target_arch = "x86_64",
bpf_target_arch = "arm",
bpf_target_arch = "powerpc64"
))]
use crate::bindings::pt_regs;
#[cfg(bpf_target_arch = "aarch64")]
// aarch64 uses user_pt_regs instead of pt_regs
#[cfg(any(bpf_target_arch = "aarch64", bpf_target_arch = "s390x"))]
use crate::bindings::user_pt_regs as pt_regs;
// riscv64 uses user_regs_struct instead of pt_regs
#[cfg(bpf_target_arch = "riscv64")]
use crate::bindings::user_regs_struct as pt_regs;
use crate::{args::FromPtRegs, EbpfContext};
Expand Down
10 changes: 8 additions & 2 deletions ebpf/aya-ebpf/src/programs/retprobe.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use core::ffi::c_void;

#[cfg(not(any(bpf_target_arch = "aarch64", bpf_target_arch = "riscv64")))]
#[cfg(any(
bpf_target_arch = "x86_64",
bpf_target_arch = "arm",
bpf_target_arch = "powerpc64"
))]
use crate::bindings::pt_regs;
#[cfg(bpf_target_arch = "aarch64")]
// aarch64 uses user_pt_regs instead of pt_regs
#[cfg(any(bpf_target_arch = "aarch64", bpf_target_arch = "s390x"))]
use crate::bindings::user_pt_regs as pt_regs;
// riscv64 uses user_regs_struct instead of pt_regs
#[cfg(bpf_target_arch = "riscv64")]
use crate::bindings::user_regs_struct as pt_regs;
use crate::{args::FromPtRegs, EbpfContext};
Expand Down
6 changes: 6 additions & 0 deletions xtask/src/codegen/aya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
aarch64_sysroot,
armv7_sysroot,
riscv64_sysroot,
powerpc64_sysroot,
s390x_sysroot,
} = opts;
let types = [
// BPF
Expand Down Expand Up @@ -179,6 +181,8 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
Architecture::ARMv7 => "armv7-unknown-linux-gnu",
Architecture::AArch64 => "aarch64-unknown-linux-gnu",
Architecture::RISCV64 => "riscv64-unknown-linux-gnu",
Architecture::PowerPC64 => "powerpc64le-unknown-linux-gnu",
Architecture::S390X => "s390x-unknown-linux-gnu",
};
bindgen = bindgen.clang_args(&["-target", target]);

Expand All @@ -189,6 +193,8 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
Architecture::ARMv7 => armv7_sysroot,
Architecture::AArch64 => aarch64_sysroot,
Architecture::RISCV64 => riscv64_sysroot,
Architecture::PowerPC64 => powerpc64_sysroot,
Architecture::S390X => s390x_sysroot,
};
bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);

Expand Down
6 changes: 6 additions & 0 deletions xtask/src/codegen/aya_ebpf_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
aarch64_sysroot,
armv7_sysroot,
riscv64_sysroot,
powerpc64_sysroot,
s390x_sysroot,
} = opts;

let dir = PathBuf::from("ebpf/aya-ebpf-bindings");
Expand Down Expand Up @@ -80,6 +82,8 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
Architecture::ARMv7 => "armv7-unknown-linux-gnu",
Architecture::AArch64 => "aarch64-unknown-linux-gnu",
Architecture::RISCV64 => "riscv64-unknown-linux-gnu",
Architecture::PowerPC64 => "powerpc64le-unknown-linux-gnu",
Architecture::S390X => "s390x-unknown-linux-gnu",
};
bindgen = bindgen.clang_args(&["-target", target]);

Expand All @@ -90,6 +94,8 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
Architecture::ARMv7 => armv7_sysroot,
Architecture::AArch64 => aarch64_sysroot,
Architecture::RISCV64 => riscv64_sysroot,
Architecture::PowerPC64 => powerpc64_sysroot,
Architecture::S390X => s390x_sysroot,
};
bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);

Expand Down
Loading

0 comments on commit f4feab5

Please sign in to comment.