[RFC PATCH] BPF backend

Alexei Starovoitov alexei.starovoitov at gmail.com
Wed Dec 3 12:41:52 PST 2014


cc-ing llvmdev...
Renato, that's the stuff we briefly discussed at LPC.
(not sure whom should I ping to get discussion going)

---------- Forwarded message ----------
From: Alexei Starovoitov <alexei.starovoitov at gmail.com>
Date: Tue, Dec 2, 2014 at 7:08 PM
Subject: [PATCH] [RFC PATCH] BPF backend
To: alexei.starovoitov at gmail.com
Cc: amara.emerson at arm.com, llvm-commits at cs.uiuc.edu


Hi All,

recently linux gained "universal in-kernel virtual machine" which is called
eBPF or extended BPF. The name comes from "Berkeley Packet Filter", since
new instruction set is based on it.
This patch adds a new backend that emits eBPF instruction set.

The concept and development are covered by the following articles:
http://lwn.net/Articles/599755/
http://lwn.net/Articles/575531/
http://lwn.net/Articles/603983/
http://lwn.net/Articles/606089/
http://lwn.net/Articles/612878/

One of use cases: dtrace/systemtap alternative.

bpf syscall manpage:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=b4fc1a460f3017e958e6a8ea560ea0afd91bf6fe

instruction set description and differences vs classic BPF:
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/filter.txt

Short summary of instruction set:
. 64-bit registers
  R0      - return value from in-kernel function, and exit value for
eBPF program
  R1 - R5 - arguments from eBPF program to in-kernel function
  R6 - R9 - callee saved registers that in-kernel function will preserve
  R10     - read-only frame pointer to access stack
. two-operand instructions like +, -, *, mov, load/store
. implicit prologue/epilogue (invisible stack pointer)
. no floating point, no simd

Short history of eBPF in kernel:
interpreter in 3.15, x64 JIT in 3.16, arm64 JIT, verifier, bpf syscall
in 3.18, more to come in the future.

Short history of compiler side:
gcc backend was done 2 years ago (never upstreamed and now out of date)
then llvm standalone backend (compileable without llvm src tree) was
done for llvm 3.2, 3.3 and 3.4
now it's been cleaned up and rebased to the latest llvm trunk
which is this patch or can be seen at: https://github.com/iovisor/llvm

It's a very small and simple backend.
There is no support for global variables, arbitrary function calls,
floating point, varargs,
exceptions, indirect jumps, arbitrary pointer arithmetic, alloca, etc.
>From C front-end point of view it's very restricted. It's done on
purpose, since kernel
rejects all programs that it cannot prove safe. It rejects programs with loops
and with memory accesses via arbitrary pointers. When kernel accepts
the program it is
guaranteed that program will terminate and will not crash the kernel.

This patch implements all 'must have' bits. There are several things
on TODO list,
so this is not the end of development.
Most of the code is a boiler plate code, copy-pasted from other backends.
Only odd things are lack or < and <= instructions, specialized
load_byte intrinsics
and 'compare and goto' as single instruction.

Current instruction set is fixed, but more instructions can be added
in the future.

Please let me know your feedback and steps to get it merged.

Signed-off-by: Alexei Starovoitov <ast at plumgrid.com>

http://reviews.llvm.org/D6494

Files:
  configure
  include/llvm/IR/Intrinsics.td
  include/llvm/IR/IntrinsicsBPF.td
  lib/Target/BPF/BPF.h
  lib/Target/BPF/BPF.td
  lib/Target/BPF/BPFAsmPrinter.cpp
  lib/Target/BPF/BPFCallingConv.td
  lib/Target/BPF/BPFFrameLowering.cpp
  lib/Target/BPF/BPFFrameLowering.h
  lib/Target/BPF/BPFISelDAGToDAG.cpp
  lib/Target/BPF/BPFISelLowering.cpp
  lib/Target/BPF/BPFISelLowering.h
  lib/Target/BPF/BPFInstrFormats.td
  lib/Target/BPF/BPFInstrInfo.cpp
  lib/Target/BPF/BPFInstrInfo.h
  lib/Target/BPF/BPFInstrInfo.td
  lib/Target/BPF/BPFMCInstLower.cpp
  lib/Target/BPF/BPFMCInstLower.h
  lib/Target/BPF/BPFRegisterInfo.cpp
  lib/Target/BPF/BPFRegisterInfo.h
  lib/Target/BPF/BPFRegisterInfo.td
  lib/Target/BPF/BPFSubtarget.cpp
  lib/Target/BPF/BPFSubtarget.h
  lib/Target/BPF/BPFTargetMachine.cpp
  lib/Target/BPF/BPFTargetMachine.h
  lib/Target/BPF/InstPrinter/BPFInstPrinter.cpp
  lib/Target/BPF/InstPrinter/BPFInstPrinter.h
  lib/Target/BPF/InstPrinter/LLVMBuild.txt
  lib/Target/BPF/InstPrinter/Makefile
  lib/Target/BPF/LLVMBuild.txt
  lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
  lib/Target/BPF/MCTargetDesc/BPFBaseInfo.h
  lib/Target/BPF/MCTargetDesc/BPFELFObjectWriter.cpp
  lib/Target/BPF/MCTargetDesc/BPFMCAsmInfo.h
  lib/Target/BPF/MCTargetDesc/BPFMCCodeEmitter.cpp
  lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
  lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.h
  lib/Target/BPF/MCTargetDesc/LLVMBuild.txt
  lib/Target/BPF/MCTargetDesc/Makefile
  lib/Target/BPF/Makefile
  lib/Target/BPF/TargetInfo/BPFTargetInfo.cpp
  lib/Target/BPF/TargetInfo/LLVMBuild.txt
  lib/Target/BPF/TargetInfo/Makefile
  lib/Target/LLVMBuild.txt
  test/CodeGen/BPF/alu8.ll
  test/CodeGen/BPF/atomics.ll
  test/CodeGen/BPF/basictest.ll
  test/CodeGen/BPF/cc_args.ll
  test/CodeGen/BPF/cc_ret.ll
  test/CodeGen/BPF/cmp.ll
  test/CodeGen/BPF/ex1.ll
  test/CodeGen/BPF/intrinsics.ll
  test/CodeGen/BPF/load.ll
  test/CodeGen/BPF/loops.ll
  test/CodeGen/BPF/sanity.ll
  test/CodeGen/BPF/setcc.ll
  test/CodeGen/BPF/shifts.ll
  test/CodeGen/BPF/sockex2.ll
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6494.16846.patch
Type: text/x-patch
Size: 156872 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141203/23e7d01a/attachment.bin>


More information about the llvm-commits mailing list