This commit aims to support BPF arena kernel side [feature](https://lore.kernel.org/bpf/20240209040608.98927-1-alexei.starovoitov@gmail.com/): - arena is a memory region accessible from both BPF program and userspace; - base pointers for this memory region differ between kernel and user spaces; - `dst_reg = addr_space_cast(src_reg, dst_addr_space, src_addr_space)` translates src_reg, a pointer in src_addr_space to dst_reg, equivalent pointer in dst_addr_space, {src,dst}_addr_space are immediate constants; - number 0 is assigned to kernel address space; - number 1 is assigned to user address space. On the LLVM side, the goal is to make load and store operations on arena pointers "transparent" for BPF programs: - assume that pointers with non-zero address space are pointers to arena memory; - assume that arena is identified by address space number; - assume that address space zero corresponds to kernel address space; - assume that every BPF-side load or store from arena is done via pointer in user address space, thus convert base pointers using `addr_space_cast(src_reg, 0, 1)`; Only load, store, cmpxchg and atomicrmw IR instructions are handled by this transformation. For example, the following C code: ```c #define __as __attribute__((address_space(1))) void copy(int __as *from, int __as *to) { *to = *from; } ``` Compiled to the following IR: ```llvm define void @copy(ptr addrspace(1) %from, ptr addrspace(1) %to) { entry: %0 = load i32, ptr addrspace(1) %from, align 4 store i32 %0, ptr addrspace(1) %to, align 4 ret void } ``` Is transformed to: ```llvm %to2 = addrspacecast ptr addrspace(1) %to to ptr ;; ! %from1 = addrspacecast ptr addrspace(1) %from to ptr ;; ! %0 = load i32, ptr %from1, align 4, !tbaa !3 store i32 %0, ptr %to2, align 4, !tbaa !3 ret void ``` And compiled as: ```asm r2 = addr_space_cast(r2, 0, 1) r1 = addr_space_cast(r1, 0, 1) r1 = *(u32 *)(r1 + 0) *(u32 *)(r2 + 0) = r1 exit ``` Co-authored-by: Eduard Zingerman <eddyz87@gmail.com>
102 lines
3.1 KiB
C++
102 lines
3.1 KiB
C++
//===-- BPF.h - Top-level interface for BPF representation ------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_LIB_TARGET_BPF_BPF_H
|
|
#define LLVM_LIB_TARGET_BPF_BPF_H
|
|
|
|
#include "MCTargetDesc/BPFMCTargetDesc.h"
|
|
#include "llvm/IR/Instructions.h"
|
|
#include "llvm/IR/PassManager.h"
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
namespace llvm {
|
|
class BPFRegisterBankInfo;
|
|
class BPFSubtarget;
|
|
class BPFTargetMachine;
|
|
class InstructionSelector;
|
|
class PassRegistry;
|
|
|
|
ModulePass *createBPFCheckAndAdjustIR();
|
|
|
|
FunctionPass *createBPFISelDag(BPFTargetMachine &TM);
|
|
FunctionPass *createBPFMISimplifyPatchablePass();
|
|
FunctionPass *createBPFMIPeepholePass();
|
|
FunctionPass *createBPFMIPreEmitPeepholePass();
|
|
FunctionPass *createBPFMIPreEmitCheckingPass();
|
|
|
|
InstructionSelector *createBPFInstructionSelector(const BPFTargetMachine &,
|
|
const BPFSubtarget &,
|
|
const BPFRegisterBankInfo &);
|
|
|
|
void initializeBPFCheckAndAdjustIRPass(PassRegistry&);
|
|
void initializeBPFDAGToDAGISelPass(PassRegistry &);
|
|
void initializeBPFMIPeepholePass(PassRegistry &);
|
|
void initializeBPFMIPreEmitCheckingPass(PassRegistry&);
|
|
void initializeBPFMIPreEmitPeepholePass(PassRegistry &);
|
|
void initializeBPFMISimplifyPatchablePass(PassRegistry &);
|
|
|
|
class BPFAbstractMemberAccessPass
|
|
: public PassInfoMixin<BPFAbstractMemberAccessPass> {
|
|
BPFTargetMachine *TM;
|
|
|
|
public:
|
|
BPFAbstractMemberAccessPass(BPFTargetMachine *TM) : TM(TM) {}
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
|
|
|
static bool isRequired() { return true; }
|
|
};
|
|
|
|
class BPFPreserveDITypePass : public PassInfoMixin<BPFPreserveDITypePass> {
|
|
public:
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
|
|
|
static bool isRequired() { return true; }
|
|
};
|
|
|
|
class BPFIRPeepholePass : public PassInfoMixin<BPFIRPeepholePass> {
|
|
public:
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
|
|
|
static bool isRequired() { return true; }
|
|
};
|
|
|
|
class BPFASpaceCastSimplifyPass
|
|
: public PassInfoMixin<BPFASpaceCastSimplifyPass> {
|
|
public:
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
|
|
|
static bool isRequired() { return true; }
|
|
};
|
|
|
|
class BPFAdjustOptPass : public PassInfoMixin<BPFAdjustOptPass> {
|
|
public:
|
|
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
|
|
};
|
|
|
|
class BPFPreserveStaticOffsetPass
|
|
: public PassInfoMixin<BPFPreserveStaticOffsetPass> {
|
|
bool AllowPartial;
|
|
|
|
public:
|
|
BPFPreserveStaticOffsetPass(bool AllowPartial) : AllowPartial(AllowPartial) {}
|
|
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
|
|
|
|
static bool isRequired() { return true; }
|
|
|
|
static std::pair<GetElementPtrInst *, LoadInst *>
|
|
reconstructLoad(CallInst *Call);
|
|
|
|
static std::pair<GetElementPtrInst *, StoreInst *>
|
|
reconstructStore(CallInst *Call);
|
|
};
|
|
|
|
} // namespace llvm
|
|
|
|
#endif
|