rustc_target/spec/base/bpf.rs
1use std::collections::BTreeMap;
2
3use rustc_abi::Endian;
4
5use crate::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, TargetOptions};
6
7pub(crate) fn opts(endian: Endian) -> TargetOptions {
8 TargetOptions {
9 allow_asm: true,
10 endian,
11 linker_flavor: LinkerFlavor::Bpf,
12 atomic_cas: false,
13 dynamic_linking: true,
14 no_builtins: true,
15 panic_strategy: PanicStrategy::Abort,
16 position_independent_executables: true,
17 // Disable MergeFunctions since:
18 // - older kernels don't support bpf-to-bpf calls
19 // - on newer kernels, userspace still needs to relocate before calling
20 // BPF_PROG_LOAD and not all BPF libraries do that yet
21 merge_functions: MergeFunctions::Disabled,
22 obj_is_bitcode: true,
23 singlethread: true,
24 // Atomic loads and stores up to 64 bits are supported from CPU v4.
25 min_atomic_width: Some(8),
26 max_atomic_width: Some(64),
27 per_cpu_atomic_widths: BTreeMap::from([
28 // CPU versions from v1 to v3 support only read-modify-write atomic
29 // operations (atomicrmw, cmpxchg), they do not support atomic
30 // loads and stores. Proof:
31 // - src/llvm-project/llvm/test/CodeGen/BPF/atomic-load-store.ll
32 // works only on v4.
33 // - All other atomic*.ll tests enabled on lower CPU versions don't
34 // perform loads and stores.
35 // `Atomic*` types from `core::sync::atomic` require availability
36 // of both kinds of operations, so they need to be disabled for
37 // CPU versions lower than v4. BPF developers wanting to use r-m-w
38 // operations on older kernels/CPUs can call intrinsics directly
39 // instead of using `core::sync::atomic`.
40 ("v1".into(), (8, 0)),
41 ("v2".into(), (8, 0)),
42 ("v3".into(), (8, 0)),
43 ("generic".into(), (8, 0)), // generic == v3
44 ]),
45 ..Default::default()
46 }
47}