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