xref: /openbmc/linux/Documentation/bpf/s390.rst (revision c95baf12f5077419db01313ab61c2aac007d40cd)
1*7e07e7aeSIlya Leoshkevich===================
2*7e07e7aeSIlya LeoshkevichTesting BPF on s390
3*7e07e7aeSIlya Leoshkevich===================
4*7e07e7aeSIlya Leoshkevich
5*7e07e7aeSIlya Leoshkevich1. Introduction
6*7e07e7aeSIlya Leoshkevich***************
7*7e07e7aeSIlya Leoshkevich
8*7e07e7aeSIlya LeoshkevichIBM Z are mainframe computers, which are descendants of IBM System/360 from
9*7e07e7aeSIlya Leoshkevichyear 1964. They are supported by the Linux kernel under the name "s390". This
10*7e07e7aeSIlya Leoshkevichdocument describes how to test BPF in an s390 QEMU guest.
11*7e07e7aeSIlya Leoshkevich
12*7e07e7aeSIlya Leoshkevich2. One-time setup
13*7e07e7aeSIlya Leoshkevich*****************
14*7e07e7aeSIlya Leoshkevich
15*7e07e7aeSIlya LeoshkevichThe following is required to build and run the test suite:
16*7e07e7aeSIlya Leoshkevich
17*7e07e7aeSIlya Leoshkevich  * s390 GCC
18*7e07e7aeSIlya Leoshkevich  * s390 development headers and libraries
19*7e07e7aeSIlya Leoshkevich  * Clang with BPF support
20*7e07e7aeSIlya Leoshkevich  * QEMU with s390 support
21*7e07e7aeSIlya Leoshkevich  * Disk image with s390 rootfs
22*7e07e7aeSIlya Leoshkevich
23*7e07e7aeSIlya LeoshkevichDebian supports installing compiler and libraries for s390 out of the box.
24*7e07e7aeSIlya LeoshkevichUsers of other distros may use debootstrap in order to set up a Debian chroot::
25*7e07e7aeSIlya Leoshkevich
26*7e07e7aeSIlya Leoshkevich  sudo debootstrap \
27*7e07e7aeSIlya Leoshkevich    --variant=minbase \
28*7e07e7aeSIlya Leoshkevich    --include=sudo \
29*7e07e7aeSIlya Leoshkevich    testing \
30*7e07e7aeSIlya Leoshkevich    ./s390-toolchain
31*7e07e7aeSIlya Leoshkevich  sudo mount --rbind /dev ./s390-toolchain/dev
32*7e07e7aeSIlya Leoshkevich  sudo mount --rbind /proc ./s390-toolchain/proc
33*7e07e7aeSIlya Leoshkevich  sudo mount --rbind /sys ./s390-toolchain/sys
34*7e07e7aeSIlya Leoshkevich  sudo chroot ./s390-toolchain
35*7e07e7aeSIlya Leoshkevich
36*7e07e7aeSIlya LeoshkevichOnce on Debian, the build prerequisites can be installed as follows::
37*7e07e7aeSIlya Leoshkevich
38*7e07e7aeSIlya Leoshkevich  sudo dpkg --add-architecture s390x
39*7e07e7aeSIlya Leoshkevich  sudo apt-get update
40*7e07e7aeSIlya Leoshkevich  sudo apt-get install \
41*7e07e7aeSIlya Leoshkevich    bc \
42*7e07e7aeSIlya Leoshkevich    bison \
43*7e07e7aeSIlya Leoshkevich    cmake \
44*7e07e7aeSIlya Leoshkevich    debootstrap \
45*7e07e7aeSIlya Leoshkevich    dwarves \
46*7e07e7aeSIlya Leoshkevich    flex \
47*7e07e7aeSIlya Leoshkevich    g++ \
48*7e07e7aeSIlya Leoshkevich    gcc \
49*7e07e7aeSIlya Leoshkevich    g++-s390x-linux-gnu \
50*7e07e7aeSIlya Leoshkevich    gcc-s390x-linux-gnu \
51*7e07e7aeSIlya Leoshkevich    gdb-multiarch \
52*7e07e7aeSIlya Leoshkevich    git \
53*7e07e7aeSIlya Leoshkevich    make \
54*7e07e7aeSIlya Leoshkevich    python3 \
55*7e07e7aeSIlya Leoshkevich    qemu-system-misc \
56*7e07e7aeSIlya Leoshkevich    qemu-utils \
57*7e07e7aeSIlya Leoshkevich    rsync \
58*7e07e7aeSIlya Leoshkevich    libcap-dev:s390x \
59*7e07e7aeSIlya Leoshkevich    libelf-dev:s390x \
60*7e07e7aeSIlya Leoshkevich    libncurses-dev
61*7e07e7aeSIlya Leoshkevich
62*7e07e7aeSIlya LeoshkevichLatest Clang targeting BPF can be installed as follows::
63*7e07e7aeSIlya Leoshkevich
64*7e07e7aeSIlya Leoshkevich  git clone https://github.com/llvm/llvm-project.git
65*7e07e7aeSIlya Leoshkevich  ln -s ../../clang llvm-project/llvm/tools/
66*7e07e7aeSIlya Leoshkevich  mkdir llvm-project-build
67*7e07e7aeSIlya Leoshkevich  cd llvm-project-build
68*7e07e7aeSIlya Leoshkevich  cmake \
69*7e07e7aeSIlya Leoshkevich    -DLLVM_TARGETS_TO_BUILD=BPF \
70*7e07e7aeSIlya Leoshkevich    -DCMAKE_BUILD_TYPE=Release \
71*7e07e7aeSIlya Leoshkevich    -DCMAKE_INSTALL_PREFIX=/opt/clang-bpf \
72*7e07e7aeSIlya Leoshkevich    ../llvm-project/llvm
73*7e07e7aeSIlya Leoshkevich  make
74*7e07e7aeSIlya Leoshkevich  sudo make install
75*7e07e7aeSIlya Leoshkevich  export PATH=/opt/clang-bpf/bin:$PATH
76*7e07e7aeSIlya Leoshkevich
77*7e07e7aeSIlya LeoshkevichThe disk image can be prepared using a loopback mount and debootstrap::
78*7e07e7aeSIlya Leoshkevich
79*7e07e7aeSIlya Leoshkevich  qemu-img create -f raw ./s390.img 1G
80*7e07e7aeSIlya Leoshkevich  sudo losetup -f ./s390.img
81*7e07e7aeSIlya Leoshkevich  sudo mkfs.ext4 /dev/loopX
82*7e07e7aeSIlya Leoshkevich  mkdir ./s390.rootfs
83*7e07e7aeSIlya Leoshkevich  sudo mount /dev/loopX ./s390.rootfs
84*7e07e7aeSIlya Leoshkevich  sudo debootstrap \
85*7e07e7aeSIlya Leoshkevich    --foreign \
86*7e07e7aeSIlya Leoshkevich    --arch=s390x \
87*7e07e7aeSIlya Leoshkevich    --variant=minbase \
88*7e07e7aeSIlya Leoshkevich    --include=" \
89*7e07e7aeSIlya Leoshkevich      iproute2, \
90*7e07e7aeSIlya Leoshkevich      iputils-ping, \
91*7e07e7aeSIlya Leoshkevich      isc-dhcp-client, \
92*7e07e7aeSIlya Leoshkevich      kmod, \
93*7e07e7aeSIlya Leoshkevich      libcap2, \
94*7e07e7aeSIlya Leoshkevich      libelf1, \
95*7e07e7aeSIlya Leoshkevich      netcat, \
96*7e07e7aeSIlya Leoshkevich      procps" \
97*7e07e7aeSIlya Leoshkevich    testing \
98*7e07e7aeSIlya Leoshkevich    ./s390.rootfs
99*7e07e7aeSIlya Leoshkevich  sudo umount ./s390.rootfs
100*7e07e7aeSIlya Leoshkevich  sudo losetup -d /dev/loopX
101*7e07e7aeSIlya Leoshkevich
102*7e07e7aeSIlya Leoshkevich3. Compilation
103*7e07e7aeSIlya Leoshkevich**************
104*7e07e7aeSIlya Leoshkevich
105*7e07e7aeSIlya LeoshkevichIn addition to the usual Kconfig options required to run the BPF test suite, it
106*7e07e7aeSIlya Leoshkevichis also helpful to select::
107*7e07e7aeSIlya Leoshkevich
108*7e07e7aeSIlya Leoshkevich  CONFIG_NET_9P=y
109*7e07e7aeSIlya Leoshkevich  CONFIG_9P_FS=y
110*7e07e7aeSIlya Leoshkevich  CONFIG_NET_9P_VIRTIO=y
111*7e07e7aeSIlya Leoshkevich  CONFIG_VIRTIO_PCI=y
112*7e07e7aeSIlya Leoshkevich
113*7e07e7aeSIlya Leoshkevichas that would enable a very easy way to share files with the s390 virtual
114*7e07e7aeSIlya Leoshkevichmachine.
115*7e07e7aeSIlya Leoshkevich
116*7e07e7aeSIlya LeoshkevichCompiling kernel, modules and testsuite, as well as preparing gdb scripts to
117*7e07e7aeSIlya Leoshkevichsimplify debugging, can be done using the following commands::
118*7e07e7aeSIlya Leoshkevich
119*7e07e7aeSIlya Leoshkevich  make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- menuconfig
120*7e07e7aeSIlya Leoshkevich  make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- bzImage modules scripts_gdb
121*7e07e7aeSIlya Leoshkevich  make ARCH=s390 CROSS_COMPILE=s390x-linux-gnu- \
122*7e07e7aeSIlya Leoshkevich    -C tools/testing/selftests \
123*7e07e7aeSIlya Leoshkevich    TARGETS=bpf \
124*7e07e7aeSIlya Leoshkevich    INSTALL_PATH=$PWD/tools/testing/selftests/kselftest_install \
125*7e07e7aeSIlya Leoshkevich    install
126*7e07e7aeSIlya Leoshkevich
127*7e07e7aeSIlya Leoshkevich4. Running the test suite
128*7e07e7aeSIlya Leoshkevich*************************
129*7e07e7aeSIlya Leoshkevich
130*7e07e7aeSIlya LeoshkevichThe virtual machine can be started as follows::
131*7e07e7aeSIlya Leoshkevich
132*7e07e7aeSIlya Leoshkevich  qemu-system-s390x \
133*7e07e7aeSIlya Leoshkevich    -cpu max,zpci=on \
134*7e07e7aeSIlya Leoshkevich    -smp 2 \
135*7e07e7aeSIlya Leoshkevich    -m 4G \
136*7e07e7aeSIlya Leoshkevich    -kernel linux/arch/s390/boot/compressed/vmlinux \
137*7e07e7aeSIlya Leoshkevich    -drive file=./s390.img,if=virtio,format=raw \
138*7e07e7aeSIlya Leoshkevich    -nographic \
139*7e07e7aeSIlya Leoshkevich    -append 'root=/dev/vda rw console=ttyS1' \
140*7e07e7aeSIlya Leoshkevich    -virtfs local,path=./linux,security_model=none,mount_tag=linux \
141*7e07e7aeSIlya Leoshkevich    -object rng-random,filename=/dev/urandom,id=rng0 \
142*7e07e7aeSIlya Leoshkevich    -device virtio-rng-ccw,rng=rng0 \
143*7e07e7aeSIlya Leoshkevich    -netdev user,id=net0 \
144*7e07e7aeSIlya Leoshkevich    -device virtio-net-ccw,netdev=net0
145*7e07e7aeSIlya Leoshkevich
146*7e07e7aeSIlya LeoshkevichWhen using this on a real IBM Z, ``-enable-kvm`` may be added for better
147*7e07e7aeSIlya Leoshkevichperformance. When starting the virtual machine for the first time, disk image
148*7e07e7aeSIlya Leoshkevichsetup must be finalized using the following command::
149*7e07e7aeSIlya Leoshkevich
150*7e07e7aeSIlya Leoshkevich  /debootstrap/debootstrap --second-stage
151*7e07e7aeSIlya Leoshkevich
152*7e07e7aeSIlya LeoshkevichDirectory with the code built on the host as well as ``/proc`` and ``/sys``
153*7e07e7aeSIlya Leoshkevichneed to be mounted as follows::
154*7e07e7aeSIlya Leoshkevich
155*7e07e7aeSIlya Leoshkevich  mkdir -p /linux
156*7e07e7aeSIlya Leoshkevich  mount -t 9p linux /linux
157*7e07e7aeSIlya Leoshkevich  mount -t proc proc /proc
158*7e07e7aeSIlya Leoshkevich  mount -t sysfs sys /sys
159*7e07e7aeSIlya Leoshkevich
160*7e07e7aeSIlya LeoshkevichAfter that, the test suite can be run using the following commands::
161*7e07e7aeSIlya Leoshkevich
162*7e07e7aeSIlya Leoshkevich  cd /linux/tools/testing/selftests/kselftest_install
163*7e07e7aeSIlya Leoshkevich  ./run_kselftest.sh
164*7e07e7aeSIlya Leoshkevich
165*7e07e7aeSIlya LeoshkevichAs usual, tests can be also run individually::
166*7e07e7aeSIlya Leoshkevich
167*7e07e7aeSIlya Leoshkevich  cd /linux/tools/testing/selftests/bpf
168*7e07e7aeSIlya Leoshkevich  ./test_verifier
169*7e07e7aeSIlya Leoshkevich
170*7e07e7aeSIlya Leoshkevich5. Debugging
171*7e07e7aeSIlya Leoshkevich************
172*7e07e7aeSIlya Leoshkevich
173*7e07e7aeSIlya LeoshkevichIt is possible to debug the s390 kernel using QEMU GDB stub, which is activated
174*7e07e7aeSIlya Leoshkevichby passing ``-s`` to QEMU.
175*7e07e7aeSIlya Leoshkevich
176*7e07e7aeSIlya LeoshkevichIt is preferable to turn KASLR off, so that gdb would know where to find the
177*7e07e7aeSIlya Leoshkevichkernel image in memory, by building the kernel with::
178*7e07e7aeSIlya Leoshkevich
179*7e07e7aeSIlya Leoshkevich  RANDOMIZE_BASE=n
180*7e07e7aeSIlya Leoshkevich
181*7e07e7aeSIlya LeoshkevichGDB can then be attached using the following command::
182*7e07e7aeSIlya Leoshkevich
183*7e07e7aeSIlya Leoshkevich  gdb-multiarch -ex 'target remote localhost:1234' ./vmlinux
184*7e07e7aeSIlya Leoshkevich
185*7e07e7aeSIlya Leoshkevich6. Network
186*7e07e7aeSIlya Leoshkevich**********
187*7e07e7aeSIlya Leoshkevich
188*7e07e7aeSIlya LeoshkevichIn case one needs to use the network in the virtual machine in order to e.g.
189*7e07e7aeSIlya Leoshkevichinstall additional packages, it can be configured using::
190*7e07e7aeSIlya Leoshkevich
191*7e07e7aeSIlya Leoshkevich  dhclient eth0
192*7e07e7aeSIlya Leoshkevich
193*7e07e7aeSIlya Leoshkevich7. Links
194*7e07e7aeSIlya Leoshkevich********
195*7e07e7aeSIlya Leoshkevich
196*7e07e7aeSIlya LeoshkevichThis document is a compilation of techniques, whose more comprehensive
197*7e07e7aeSIlya Leoshkevichdescriptions can be found by following these links:
198*7e07e7aeSIlya Leoshkevich
199*7e07e7aeSIlya Leoshkevich- `Debootstrap <https://wiki.debian.org/EmDebian/CrossDebootstrap>`_
200*7e07e7aeSIlya Leoshkevich- `Multiarch <https://wiki.debian.org/Multiarch/HOWTO>`_
201*7e07e7aeSIlya Leoshkevich- `Building LLVM <https://llvm.org/docs/CMake.html>`_
202*7e07e7aeSIlya Leoshkevich- `Cross-compiling the kernel <https://wiki.gentoo.org/wiki/Embedded_Handbook/General/Cross-compiling_the_kernel>`_
203*7e07e7aeSIlya Leoshkevich- `QEMU s390x Guest Support <https://wiki.qemu.org/Documentation/Platforms/S390X>`_
204*7e07e7aeSIlya Leoshkevich- `Plan 9 folder sharing over Virtio <https://wiki.qemu.org/Documentation/9psetup>`_
205*7e07e7aeSIlya Leoshkevich- `Using GDB with QEMU <https://wiki.osdev.org/Kernel_Debugging#Use_GDB_with_QEMU>`_
206