187fdd476SJan Kiszka#!/bin/sh -e 287fdd476SJan Kiszka# 387fdd476SJan Kiszka# Update Linux kernel headers QEMU requires from a specified kernel tree. 487fdd476SJan Kiszka# 587fdd476SJan Kiszka# Copyright (C) 2011 Siemens AG 687fdd476SJan Kiszka# 787fdd476SJan Kiszka# Authors: 887fdd476SJan Kiszka# Jan Kiszka <jan.kiszka@siemens.com> 987fdd476SJan Kiszka# 1087fdd476SJan Kiszka# This work is licensed under the terms of the GNU GPL version 2. 1187fdd476SJan Kiszka# See the COPYING file in the top-level directory. 120166f5c4SPeter Maydell# 130166f5c4SPeter Maydell# The script will copy the headers into two target folders: 140166f5c4SPeter Maydell# 150166f5c4SPeter Maydell# - linux-headers/ for files that are required for compiling for a 160166f5c4SPeter Maydell# Linux host. Generally we have these so we can use kernel structs 170166f5c4SPeter Maydell# and defines that are more recent than the headers that might be 180166f5c4SPeter Maydell# installed on the host system. Usually this script can do simple 190166f5c4SPeter Maydell# file copies for these headers. 200166f5c4SPeter Maydell# 210166f5c4SPeter Maydell# - include/standard-headers/ for files that are used for guest 220166f5c4SPeter Maydell# device emulation and are required on all hosts. For instance, we 230166f5c4SPeter Maydell# get our definitions of the virtio structures from the Linux 240166f5c4SPeter Maydell# kernel headers, but we need those definitions regardless of which 250166f5c4SPeter Maydell# host OS we are building for. This script has to be careful to 260166f5c4SPeter Maydell# sanitize the headers to remove any use of Linux-specifics such as 270166f5c4SPeter Maydell# types like "__u64". This work is done in the cp_portable function. 2887fdd476SJan Kiszka 29bbd90802SStefan Weiltmpdir=$(mktemp -d) 3087fdd476SJan Kiszkalinux="$1" 3187fdd476SJan Kiszkaoutput="$2" 3287fdd476SJan Kiszka 3387fdd476SJan Kiszkaif [ -z "$linux" ] || ! [ -d "$linux" ]; then 3487fdd476SJan Kiszka cat << EOF 3587fdd476SJan Kiszkausage: update-kernel-headers.sh LINUX_PATH [OUTPUT_PATH] 3687fdd476SJan Kiszka 3787fdd476SJan KiszkaLINUX_PATH Linux kernel directory to obtain the headers from 3887fdd476SJan KiszkaOUTPUT_PATH output directory, usually the qemu source tree (default: $PWD) 3987fdd476SJan KiszkaEOF 4087fdd476SJan Kiszka exit 1 4187fdd476SJan Kiszkafi 4287fdd476SJan Kiszka 4387fdd476SJan Kiszkaif [ -z "$output" ]; then 4487fdd476SJan Kiszka output="$PWD" 4587fdd476SJan Kiszkafi 4687fdd476SJan Kiszka 47eddb4de3SPaolo Bonzinicp_portable() { 48eddb4de3SPaolo Bonzini f=$1 491ff0b555SMichael S. Tsirkin to=$2 501ff0b555SMichael S. Tsirkin if 511ff0b555SMichael S. Tsirkin grep '#include' "$f" | grep -v -e 'linux/virtio' \ 521ff0b555SMichael S. Tsirkin -e 'linux/types' \ 534d010861SVivek Kasireddy -e 'linux/ioctl' \ 54120758fbSPaolo Bonzini -e 'stdint' \ 551ff0b555SMichael S. Tsirkin -e 'linux/if_ether' \ 56fff02bc0SPaolo Bonzini -e 'input-event-codes' \ 572fe7c318SGerd Hoffmann -e 'sys/' \ 58e1c5f1f0SMarcel Apfelbaum -e 'pvrdma_verbs' \ 598e8ee850SGerd Hoffmann -e 'drm.h' \ 60d3b7b374SJason Baron -e 'limits' \ 61ab5ec23fSEric Farman -e 'linux/const' \ 62d3b7b374SJason Baron -e 'linux/kernel' \ 63d3b7b374SJason Baron -e 'linux/sysinfo' \ 64ec7b1080SMichael S. Tsirkin -e 'asm-generic/kvm_para' \ 651ff0b555SMichael S. Tsirkin > /dev/null 661ff0b555SMichael S. Tsirkin then 671ff0b555SMichael S. Tsirkin echo "Unexpected #include in input file $f". 681ff0b555SMichael S. Tsirkin exit 2 691ff0b555SMichael S. Tsirkin fi 701ff0b555SMichael S. Tsirkin 711ff0b555SMichael S. Tsirkin header=$(basename "$f"); 72c5022c31SPeter Maydell sed -e 's/__aligned_u64/__u64 __attribute__((aligned(8)))/g' \ 73c5022c31SPeter Maydell -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \ 74e1c5f1f0SMarcel Apfelbaum -e 's/u\([0-9][0-9]*\)/uint\1_t/g' \ 752fe7c318SGerd Hoffmann -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \ 761ff0b555SMichael S. Tsirkin -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \ 771ff0b555SMichael S. Tsirkin -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \ 78fff02bc0SPaolo Bonzini -e 's/"\(input-event-codes\.h\)"/"standard-headers\/linux\/\1"/' \ 791ff0b555SMichael S. Tsirkin -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \ 80ed219c40SMichael S. Tsirkin -e 's/__bitwise//' \ 811ff0b555SMichael S. Tsirkin -e 's/__attribute__((packed))/QEMU_PACKED/' \ 82c16758cbSMichael S. Tsirkin -e 's/__inline__/inline/' \ 839f2d175dSPaolo Bonzini -e 's/__BITS_PER_LONG/HOST_LONG_BITS/' \ 848e8ee850SGerd Hoffmann -e '/\"drm.h\"/d' \ 85e2f6bac3SGerd Hoffmann -e '/sys\/ioctl.h/d' \ 864d010861SVivek Kasireddy -e '/linux\/ioctl.h/d' \ 87ac98fa84SMarkus Armbruster -e 's/SW_MAX/SW_MAX_/' \ 88e1c5f1f0SMarcel Apfelbaum -e 's/atomic_t/int/' \ 89d3b7b374SJason Baron -e 's/__kernel_long_t/long/' \ 90d3b7b374SJason Baron -e 's/__kernel_ulong_t/unsigned long/' \ 91d3b7b374SJason Baron -e 's/struct ethhdr/struct eth_header/' \ 92d3b7b374SJason Baron -e '/\#define _LINUX_ETHTOOL_H/a \\n\#include "net/eth.h"' \ 931ff0b555SMichael S. Tsirkin "$f" > "$to/$header"; 941ff0b555SMichael S. Tsirkin} 951ff0b555SMichael S. Tsirkin 962879636dSPeter Maydell# This will pick up non-directories too (eg "Kconfig") but we will 972879636dSPeter Maydell# ignore them in the next loop. 982879636dSPeter MaydellARCHLIST=$(cd "$linux/arch" && echo *) 992879636dSPeter Maydell 1002879636dSPeter Maydellfor arch in $ARCHLIST; do 1012879636dSPeter Maydell # Discard anything which isn't a KVM-supporting architecture 102b55f546eSPeter Maydell if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] && 103b55f546eSPeter Maydell ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then 1042879636dSPeter Maydell continue 1052879636dSPeter Maydell fi 1062879636dSPeter Maydell 107f717e624SPaolo Bonzini if [ "$arch" = x86 ]; then 108f717e624SPaolo Bonzini arch_var=SRCARCH 109f717e624SPaolo Bonzini else 110f717e624SPaolo Bonzini arch_var=ARCH 111f717e624SPaolo Bonzini fi 112f717e624SPaolo Bonzini 113f717e624SPaolo Bonzini make -C "$linux" INSTALL_HDR_PATH="$tmpdir" $arch_var=$arch headers_install 11487fdd476SJan Kiszka 11587fdd476SJan Kiszka rm -rf "$output/linux-headers/asm-$arch" 11687fdd476SJan Kiszka mkdir -p "$output/linux-headers/asm-$arch" 1170289881fSZhang Yi for header in kvm.h unistd.h bitsperlong.h mman.h; do 11887fdd476SJan Kiszka cp "$tmpdir/include/asm/$header" "$output/linux-headers/asm-$arch" 11987fdd476SJan Kiszka done 1209882d3efSMichael S. Tsirkin 1219882d3efSMichael S. Tsirkin if [ $arch = mips ]; then 1229882d3efSMichael S. Tsirkin cp "$tmpdir/include/asm/sgidefs.h" "$output/linux-headers/asm-mips/" 123a0a6ef91SPaolo Bonzini cp "$tmpdir/include/asm/unistd_o32.h" "$output/linux-headers/asm-mips/" 124a0a6ef91SPaolo Bonzini cp "$tmpdir/include/asm/unistd_n32.h" "$output/linux-headers/asm-mips/" 125a0a6ef91SPaolo Bonzini cp "$tmpdir/include/asm/unistd_n64.h" "$output/linux-headers/asm-mips/" 126a0a6ef91SPaolo Bonzini fi 127a0a6ef91SPaolo Bonzini if [ $arch = powerpc ]; then 128a0a6ef91SPaolo Bonzini cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-powerpc/" 129a0a6ef91SPaolo Bonzini cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-powerpc/" 1309882d3efSMichael S. Tsirkin fi 1319882d3efSMichael S. Tsirkin 132eddb4de3SPaolo Bonzini rm -rf "$output/include/standard-headers/asm-$arch" 133eddb4de3SPaolo Bonzini mkdir -p "$output/include/standard-headers/asm-$arch" 134eddb4de3SPaolo Bonzini if [ $arch = s390 ]; then 135eddb4de3SPaolo Bonzini cp_portable "$tmpdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/" 1369f2d175dSPaolo Bonzini cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-s390/" 1379f2d175dSPaolo Bonzini cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/" 138eddb4de3SPaolo Bonzini fi 139f717e624SPaolo Bonzini if [ $arch = arm ]; then 140f717e624SPaolo Bonzini cp "$tmpdir/include/asm/unistd-eabi.h" "$output/linux-headers/asm-arm/" 141f717e624SPaolo Bonzini cp "$tmpdir/include/asm/unistd-oabi.h" "$output/linux-headers/asm-arm/" 142f717e624SPaolo Bonzini cp "$tmpdir/include/asm/unistd-common.h" "$output/linux-headers/asm-arm/" 143f717e624SPaolo Bonzini fi 144b1b9e0dcSCornelia Huck if [ $arch = arm64 ]; then 145b1b9e0dcSCornelia Huck cp "$tmpdir/include/asm/sve_context.h" "$output/linux-headers/asm-arm64/" 146b1b9e0dcSCornelia Huck fi 14773aa529aSPaolo Bonzini if [ $arch = x86 ]; then 1481842bdfdSMarc-André Lureau cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/" 1491842bdfdSMarc-André Lureau cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/" 1501842bdfdSMarc-André Lureau cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/" 151ec7b1080SMichael S. Tsirkin cp_portable "$tmpdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch" 15206e0259aSLi Zhijian # Remove everything except the macros from bootparam.h avoiding the 15306e0259aSLi Zhijian # unnecessary import of several video/ist/etc headers 15406e0259aSLi Zhijian sed -e '/__ASSEMBLY__/,/__ASSEMBLY__/d' \ 15506e0259aSLi Zhijian "$tmpdir/include/asm/bootparam.h" > "$tmpdir/bootparam.h" 15606e0259aSLi Zhijian cp_portable "$tmpdir/bootparam.h" \ 15706e0259aSLi Zhijian "$output/include/standard-headers/asm-$arch" 15873aa529aSPaolo Bonzini fi 159*1583ca8aSDaniel Henrique Barboza if [ $arch = riscv ]; then 160*1583ca8aSDaniel Henrique Barboza cp "$tmpdir/include/asm/ptrace.h" "$output/linux-headers/asm-riscv/" 161*1583ca8aSDaniel Henrique Barboza fi 16287fdd476SJan Kiszkadone 16387fdd476SJan Kiszka 16487fdd476SJan Kiszkarm -rf "$output/linux-headers/linux" 16587fdd476SJan Kiszkamkdir -p "$output/linux-headers/linux" 1669fc7dd23SDavid 'Digit' Turnerfor header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \ 1678cba58b5SEric Auger psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \ 1688cba58b5SEric Auger vduse.h iommufd.h; do 16987fdd476SJan Kiszka cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" 17087fdd476SJan Kiszkadone 171ec7b1080SMichael S. Tsirkin 1729882d3efSMichael S. Tsirkinrm -rf "$output/linux-headers/asm-generic" 1739882d3efSMichael S. Tsirkinmkdir -p "$output/linux-headers/asm-generic" 1740289881fSZhang Yifor header in unistd.h bitsperlong.h mman-common.h mman.h hugetlb_encode.h; do 1759882d3efSMichael S. Tsirkin cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic" 1769882d3efSMichael S. Tsirkindone 1779882d3efSMichael S. Tsirkin 17887fdd476SJan Kiszkaif [ -L "$linux/source" ]; then 17987fdd476SJan Kiszka cp "$linux/source/COPYING" "$output/linux-headers" 18087fdd476SJan Kiszkaelse 18187fdd476SJan Kiszka cp "$linux/COPYING" "$output/linux-headers" 18287fdd476SJan Kiszkafi 18387fdd476SJan Kiszka 184f5bba4caSPeter Maydell# Recent kernel sources split the copyright/license info into multiple 185f5bba4caSPeter Maydell# files, which we need to copy. This set of licenses is the set that 186f5bba4caSPeter Maydell# are referred to by SPDX lines in the headers we currently copy. 187f5bba4caSPeter Maydell# We don't copy the Documentation/process/license-rules.rst which 188f5bba4caSPeter Maydell# is also referred to by COPYING, since it's explanatory rather than license. 189f5bba4caSPeter Maydellif [ -d "$linux/LICENSES" ]; then 190f5bba4caSPeter Maydell mkdir -p "$output/linux-headers/LICENSES/preferred" \ 191f5bba4caSPeter Maydell "$output/linux-headers/LICENSES/exceptions" 192f5bba4caSPeter Maydell for l in preferred/GPL-2.0 preferred/BSD-2-Clause preferred/BSD-3-Clause \ 193f5bba4caSPeter Maydell exceptions/Linux-syscall-note; do 194f5bba4caSPeter Maydell cp "$linux/LICENSES/$l" "$output/linux-headers/LICENSES/$l" 195f5bba4caSPeter Maydell done 196f5bba4caSPeter Maydellfi 197f5bba4caSPeter Maydell 19805e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_config.h 19905e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_config.h" 20005e492b0SMichael S. TsirkinEOF 20105e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_ring.h 20205e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_ring.h" 20305e492b0SMichael S. TsirkinEOF 204a0a6ef91SPaolo Bonzinicat <<EOF >$output/linux-headers/linux/vhost_types.h 205a0a6ef91SPaolo Bonzini#include "standard-headers/linux/vhost_types.h" 206a0a6ef91SPaolo BonziniEOF 2071ff0b555SMichael S. Tsirkin 208eddb4de3SPaolo Bonzinirm -rf "$output/include/standard-headers/linux" 209eddb4de3SPaolo Bonzinimkdir -p "$output/include/standard-headers/linux" 210039d7c4dSMarc-André Lureaufor i in "$tmpdir"/include/linux/*virtio*.h \ 211039d7c4dSMarc-André Lureau "$tmpdir/include/linux/qemu_fw_cfg.h" \ 212a62a9e19SDr. David Alan Gilbert "$tmpdir/include/linux/fuse.h" \ 213039d7c4dSMarc-André Lureau "$tmpdir/include/linux/input.h" \ 214fff02bc0SPaolo Bonzini "$tmpdir/include/linux/input-event-codes.h" \ 2154d010861SVivek Kasireddy "$tmpdir/include/linux/udmabuf.h" \ 216d3b7b374SJason Baron "$tmpdir/include/linux/pci_regs.h" \ 217ab5ec23fSEric Farman "$tmpdir/include/linux/ethtool.h" \ 218ab5ec23fSEric Farman "$tmpdir/include/linux/const.h" \ 219ab5ec23fSEric Farman "$tmpdir/include/linux/kernel.h" \ 220a0a6ef91SPaolo Bonzini "$tmpdir/include/linux/vhost_types.h" \ 221fcbd14dbSzhenwei pi "$tmpdir/include/linux/sysinfo.h" \ 222fcbd14dbSzhenwei pi "$tmpdir/include/misc/pvpanic.h"; do 223eddb4de3SPaolo Bonzini cp_portable "$i" "$output/include/standard-headers/linux" 224eddb4de3SPaolo Bonzinidone 2258e8ee850SGerd Hoffmannmkdir -p "$output/include/standard-headers/drm" 2268e8ee850SGerd Hoffmanncp_portable "$tmpdir/include/drm/drm_fourcc.h" \ 2278e8ee850SGerd Hoffmann "$output/include/standard-headers/drm" 2281ff0b555SMichael S. Tsirkin 229e1c5f1f0SMarcel Apfelbaumrm -rf "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma" 230e1c5f1f0SMarcel Apfelbaummkdir -p "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma" 231e1c5f1f0SMarcel Apfelbaum 232e1c5f1f0SMarcel Apfelbaum# Remove the unused functions from pvrdma_verbs.h avoiding the unnecessary 233e1c5f1f0SMarcel Apfelbaum# import of several infiniband/networking/other headers 234e1c5f1f0SMarcel Apfelbaumtmp_pvrdma_verbs="$tmpdir/pvrdma_verbs.h" 235e1c5f1f0SMarcel Apfelbaum# Parse the entire file instead of single lines to match 236e1c5f1f0SMarcel Apfelbaum# function declarations expanding over multiple lines 237e1c5f1f0SMarcel Apfelbaum# and strip the declarations starting with pvrdma prefix. 238e1c5f1f0SMarcel Apfelbaumsed -e '1h;2,$H;$!d;g' -e 's/[^};]*pvrdma[^(| ]*([^)]*);//g' \ 239e1c5f1f0SMarcel Apfelbaum "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_verbs.h" > \ 240e1c5f1f0SMarcel Apfelbaum "$tmp_pvrdma_verbs"; 241e1c5f1f0SMarcel Apfelbaum 2423aa1b7afSCornelia Huckfor i in "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_dev_api.h" \ 243e1c5f1f0SMarcel Apfelbaum "$tmp_pvrdma_verbs"; do \ 244e1c5f1f0SMarcel Apfelbaum cp_portable "$i" \ 245e1c5f1f0SMarcel Apfelbaum "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/" 246e1c5f1f0SMarcel Apfelbaumdone 247e1c5f1f0SMarcel Apfelbaum 248e1c5f1f0SMarcel Apfelbaumrm -rf "$output/include/standard-headers/rdma/" 249e1c5f1f0SMarcel Apfelbaummkdir -p "$output/include/standard-headers/rdma/" 250e1c5f1f0SMarcel Apfelbaumfor i in "$tmpdir/include/rdma/vmw_pvrdma-abi.h"; do 251e1c5f1f0SMarcel Apfelbaum cp_portable "$i" \ 252e1c5f1f0SMarcel Apfelbaum "$output/include/standard-headers/rdma/" 253e1c5f1f0SMarcel Apfelbaumdone 254e1c5f1f0SMarcel Apfelbaum 2551ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/types.h 2568bc92a76SPeter Maydell/* For QEMU all types are already defined via osdep.h, so this 2578bc92a76SPeter Maydell * header does not need to do anything. 2588bc92a76SPeter Maydell */ 2591ff0b555SMichael S. TsirkinEOF 2601ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/if_ether.h 2611ff0b555SMichael S. Tsirkin#define ETH_ALEN 6 2621ff0b555SMichael S. TsirkinEOF 2631ff0b555SMichael S. Tsirkin 26487fdd476SJan Kiszkarm -rf "$tmpdir" 265