xref: /qemu/scripts/update-linux-headers.sh (revision aa274c33c39e7de981dc195abe60e1a246c9d248)
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)
30b51ddd93SAlex Bennéehdrdir="$tmpdir/headers"
31b51ddd93SAlex Bennéeblddir="$tmpdir/build"
3287fdd476SJan Kiszkalinux="$1"
3387fdd476SJan Kiszkaoutput="$2"
3487fdd476SJan Kiszka
3587fdd476SJan Kiszkaif [ -z "$linux" ] || ! [ -d "$linux" ]; then
3687fdd476SJan Kiszka    cat << EOF
3787fdd476SJan Kiszkausage: update-kernel-headers.sh LINUX_PATH [OUTPUT_PATH]
3887fdd476SJan Kiszka
3987fdd476SJan KiszkaLINUX_PATH      Linux kernel directory to obtain the headers from
4087fdd476SJan KiszkaOUTPUT_PATH     output directory, usually the qemu source tree (default: $PWD)
4187fdd476SJan KiszkaEOF
4287fdd476SJan Kiszka    exit 1
4387fdd476SJan Kiszkafi
4487fdd476SJan Kiszka
4587fdd476SJan Kiszkaif [ -z "$output" ]; then
4687fdd476SJan Kiszka    output="$PWD"
4787fdd476SJan Kiszkafi
4887fdd476SJan Kiszka
49eddb4de3SPaolo Bonzinicp_portable() {
50eddb4de3SPaolo Bonzini    f=$1
511ff0b555SMichael S. Tsirkin    to=$2
521ff0b555SMichael S. Tsirkin    if
531ff0b555SMichael S. Tsirkin        grep '#include' "$f" | grep -v -e 'linux/virtio' \
541ff0b555SMichael S. Tsirkin                                     -e 'linux/types' \
554d010861SVivek Kasireddy                                     -e 'linux/ioctl' \
56120758fbSPaolo Bonzini                                     -e 'stdint' \
571ff0b555SMichael S. Tsirkin                                     -e 'linux/if_ether' \
58fff02bc0SPaolo Bonzini                                     -e 'input-event-codes' \
592fe7c318SGerd Hoffmann                                     -e 'sys/' \
608e8ee850SGerd Hoffmann                                     -e 'drm.h' \
61d3b7b374SJason Baron                                     -e 'limits' \
62ab5ec23fSEric Farman                                     -e 'linux/const' \
63d3b7b374SJason Baron                                     -e 'linux/kernel' \
64d3b7b374SJason Baron                                     -e 'linux/sysinfo' \
6566210a1aSMichael Roth                                     -e 'asm/setup_data.h' \
66*aa274c33SPaolo Bonzini                                     -e 'asm/kvm_para.h' \
671ff0b555SMichael S. Tsirkin                                     > /dev/null
681ff0b555SMichael S. Tsirkin    then
691ff0b555SMichael S. Tsirkin        echo "Unexpected #include in input file $f".
701ff0b555SMichael S. Tsirkin        exit 2
711ff0b555SMichael S. Tsirkin    fi
721ff0b555SMichael S. Tsirkin
731ff0b555SMichael S. Tsirkin    header=$(basename "$f");
74*aa274c33SPaolo Bonzini
75*aa274c33SPaolo Bonzini    if test -z "$arch"; then
76*aa274c33SPaolo Bonzini        # Let users of include/standard-headers/linux/ headers pick the
77*aa274c33SPaolo Bonzini        # asm-* header that they care about
78*aa274c33SPaolo Bonzini        arch_cmd='/<asm\/\([^>]*\)>/d'
79*aa274c33SPaolo Bonzini    else
80*aa274c33SPaolo Bonzini        arch_cmd='s/<asm\/\([^>]*\)>/"standard-headers\/asm-'$arch'\/\1"/'
81*aa274c33SPaolo Bonzini    fi
82*aa274c33SPaolo Bonzini
83c5022c31SPeter Maydell    sed -e 's/__aligned_u64/__u64 __attribute__((aligned(8)))/g' \
84c5022c31SPeter Maydell        -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \
85e1c5f1f0SMarcel Apfelbaum        -e 's/u\([0-9][0-9]*\)/uint\1_t/g' \
862fe7c318SGerd Hoffmann        -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \
871ff0b555SMichael S. Tsirkin        -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \
881ff0b555SMichael S. Tsirkin        -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \
89fff02bc0SPaolo Bonzini        -e 's/"\(input-event-codes\.h\)"/"standard-headers\/linux\/\1"/' \
901ff0b555SMichael S. Tsirkin        -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \
91*aa274c33SPaolo Bonzini        -e "$arch_cmd" \
92ed219c40SMichael S. Tsirkin        -e 's/__bitwise//' \
931ff0b555SMichael S. Tsirkin        -e 's/__attribute__((packed))/QEMU_PACKED/' \
94c16758cbSMichael S. Tsirkin        -e 's/__inline__/inline/' \
959f2d175dSPaolo Bonzini        -e 's/__BITS_PER_LONG/HOST_LONG_BITS/' \
968e8ee850SGerd Hoffmann        -e '/\"drm.h\"/d' \
97e2f6bac3SGerd Hoffmann        -e '/sys\/ioctl.h/d' \
984d010861SVivek Kasireddy        -e '/linux\/ioctl.h/d' \
99ac98fa84SMarkus Armbruster        -e 's/SW_MAX/SW_MAX_/' \
100e1c5f1f0SMarcel Apfelbaum        -e 's/atomic_t/int/' \
101d3b7b374SJason Baron        -e 's/__kernel_long_t/long/' \
102d3b7b374SJason Baron        -e 's/__kernel_ulong_t/unsigned long/' \
103d3b7b374SJason Baron        -e 's/struct ethhdr/struct eth_header/' \
104d3b7b374SJason Baron        -e '/\#define _LINUX_ETHTOOL_H/a \\n\#include "net/eth.h"' \
1051ff0b555SMichael S. Tsirkin        "$f" > "$to/$header";
1061ff0b555SMichael S. Tsirkin}
1071ff0b555SMichael S. Tsirkin
1082879636dSPeter Maydell# This will pick up non-directories too (eg "Kconfig") but we will
1092879636dSPeter Maydell# ignore them in the next loop.
1102879636dSPeter MaydellARCHLIST=$(cd "$linux/arch" && echo *)
1112879636dSPeter Maydell
1122879636dSPeter Maydellfor arch in $ARCHLIST; do
1132879636dSPeter Maydell    # Discard anything which isn't a KVM-supporting architecture
114b55f546eSPeter Maydell    if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] &&
115b55f546eSPeter Maydell        ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then
1162879636dSPeter Maydell        continue
1172879636dSPeter Maydell    fi
1182879636dSPeter Maydell
119f717e624SPaolo Bonzini    if [ "$arch" = x86 ]; then
120f717e624SPaolo Bonzini        arch_var=SRCARCH
121f717e624SPaolo Bonzini    else
122f717e624SPaolo Bonzini        arch_var=ARCH
123f717e624SPaolo Bonzini    fi
124f717e624SPaolo Bonzini
1253efc75adSThomas Huth    rm -rf "$hdrdir"
126b51ddd93SAlex Bennée    make -C "$linux" O="$blddir" INSTALL_HDR_PATH="$hdrdir" $arch_var=$arch headers_install
12787fdd476SJan Kiszka
12887fdd476SJan Kiszka    rm -rf "$output/linux-headers/asm-$arch"
12987fdd476SJan Kiszka    mkdir -p "$output/linux-headers/asm-$arch"
1300289881fSZhang Yi    for header in kvm.h unistd.h bitsperlong.h mman.h; do
131ef7c70f0SPaolo Bonzini        if test -f "$hdrdir/include/asm/$header"; then
132b51ddd93SAlex Bennée            cp "$hdrdir/include/asm/$header" "$output/linux-headers/asm-$arch"
133ef7c70f0SPaolo Bonzini        elif test -f "$hdrdir/include/asm-generic/$header"; then
134ef7c70f0SPaolo Bonzini            # not installed as <asm/$header>, but used as such in kernel sources
135ef7c70f0SPaolo Bonzini            cat <<EOF >$output/linux-headers/asm-$arch/$header
136ef7c70f0SPaolo Bonzini#include <asm-generic/$header>
137ef7c70f0SPaolo BonziniEOF
138ef7c70f0SPaolo Bonzini        fi
13987fdd476SJan Kiszka    done
1409882d3efSMichael S. Tsirkin
1419882d3efSMichael S. Tsirkin    if [ $arch = mips ]; then
142b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/sgidefs.h" "$output/linux-headers/asm-mips/"
143b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_o32.h" "$output/linux-headers/asm-mips/"
144b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_n32.h" "$output/linux-headers/asm-mips/"
145b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_n64.h" "$output/linux-headers/asm-mips/"
146a0a6ef91SPaolo Bonzini    fi
147a0a6ef91SPaolo Bonzini    if [ $arch = powerpc ]; then
148b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-powerpc/"
149b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-powerpc/"
1509882d3efSMichael S. Tsirkin    fi
1519882d3efSMichael S. Tsirkin
152eddb4de3SPaolo Bonzini    rm -rf "$output/include/standard-headers/asm-$arch"
153eddb4de3SPaolo Bonzini    mkdir -p "$output/include/standard-headers/asm-$arch"
154eddb4de3SPaolo Bonzini    if [ $arch = s390 ]; then
155b51ddd93SAlex Bennée        cp_portable "$hdrdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/"
156b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-s390/"
157b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/"
158eddb4de3SPaolo Bonzini    fi
159f717e624SPaolo Bonzini    if [ $arch = arm ]; then
160b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd-eabi.h" "$output/linux-headers/asm-arm/"
161b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd-oabi.h" "$output/linux-headers/asm-arm/"
162b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd-common.h" "$output/linux-headers/asm-arm/"
163f717e624SPaolo Bonzini    fi
164b1b9e0dcSCornelia Huck    if [ $arch = arm64 ]; then
165b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/sve_context.h" "$output/linux-headers/asm-arm64/"
166b1b9e0dcSCornelia Huck    fi
16773aa529aSPaolo Bonzini    if [ $arch = x86 ]; then
168b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/"
169b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
170b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
171*aa274c33SPaolo Bonzini
172b51ddd93SAlex Bennée        cp_portable "$hdrdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch"
173*aa274c33SPaolo Bonzini        cat <<EOF >$output/linux-headers/asm-$arch/kvm_para.h
174*aa274c33SPaolo Bonzini#include "standard-headers/asm-$arch/kvm_para.h"
175*aa274c33SPaolo BonziniEOF
176*aa274c33SPaolo Bonzini
17706e0259aSLi Zhijian        # Remove everything except the macros from bootparam.h avoiding the
17806e0259aSLi Zhijian        # unnecessary import of several video/ist/etc headers
17906e0259aSLi Zhijian        sed -e '/__ASSEMBLY__/,/__ASSEMBLY__/d' \
180b51ddd93SAlex Bennée               "$hdrdir/include/asm/bootparam.h" > "$hdrdir/bootparam.h"
181b51ddd93SAlex Bennée        cp_portable "$hdrdir/bootparam.h" \
18206e0259aSLi Zhijian                    "$output/include/standard-headers/asm-$arch"
183b51ddd93SAlex Bennée        cp_portable "$hdrdir/include/asm/setup_data.h" \
184bde26d90SThomas Huth                    "$output/include/standard-headers/asm-x86"
18573aa529aSPaolo Bonzini    fi
1861583ca8aSDaniel Henrique Barboza    if [ $arch = riscv ]; then
187b51ddd93SAlex Bennée        cp "$hdrdir/include/asm/ptrace.h" "$output/linux-headers/asm-riscv/"
1881583ca8aSDaniel Henrique Barboza    fi
18987fdd476SJan Kiszkadone
19066210a1aSMichael Rotharch=
19187fdd476SJan Kiszka
19287fdd476SJan Kiszkarm -rf "$output/linux-headers/linux"
19387fdd476SJan Kiszkamkdir -p "$output/linux-headers/linux"
1949fc7dd23SDavid 'Digit' Turnerfor header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \
1958cba58b5SEric Auger              psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \
196b40b8eb6SMichael Roth              vduse.h iommufd.h bits.h; do
197b51ddd93SAlex Bennée    cp "$hdrdir/include/linux/$header" "$output/linux-headers/linux"
19887fdd476SJan Kiszkadone
199ec7b1080SMichael S. Tsirkin
2009882d3efSMichael S. Tsirkinrm -rf "$output/linux-headers/asm-generic"
2019882d3efSMichael S. Tsirkinmkdir -p "$output/linux-headers/asm-generic"
2020289881fSZhang Yifor header in unistd.h bitsperlong.h mman-common.h mman.h hugetlb_encode.h; do
203b51ddd93SAlex Bennée    cp "$hdrdir/include/asm-generic/$header" "$output/linux-headers/asm-generic"
2049882d3efSMichael S. Tsirkindone
2059882d3efSMichael S. Tsirkin
20687fdd476SJan Kiszkaif [ -L "$linux/source" ]; then
20787fdd476SJan Kiszka    cp "$linux/source/COPYING" "$output/linux-headers"
20887fdd476SJan Kiszkaelse
20987fdd476SJan Kiszka    cp "$linux/COPYING" "$output/linux-headers"
21087fdd476SJan Kiszkafi
21187fdd476SJan Kiszka
212f5bba4caSPeter Maydell# Recent kernel sources split the copyright/license info into multiple
213f5bba4caSPeter Maydell# files, which we need to copy. This set of licenses is the set that
214f5bba4caSPeter Maydell# are referred to by SPDX lines in the headers we currently copy.
215f5bba4caSPeter Maydell# We don't copy the Documentation/process/license-rules.rst which
216f5bba4caSPeter Maydell# is also referred to by COPYING, since it's explanatory rather than license.
217f5bba4caSPeter Maydellif [ -d "$linux/LICENSES" ]; then
218f5bba4caSPeter Maydell    mkdir -p "$output/linux-headers/LICENSES/preferred" \
219f5bba4caSPeter Maydell             "$output/linux-headers/LICENSES/exceptions"
220f5bba4caSPeter Maydell    for l in preferred/GPL-2.0 preferred/BSD-2-Clause preferred/BSD-3-Clause \
221f5bba4caSPeter Maydell             exceptions/Linux-syscall-note; do
222f5bba4caSPeter Maydell        cp "$linux/LICENSES/$l" "$output/linux-headers/LICENSES/$l"
223f5bba4caSPeter Maydell    done
224f5bba4caSPeter Maydellfi
225f5bba4caSPeter Maydell
226*aa274c33SPaolo Bonzinicat <<EOF >$output/linux-headers/linux/kvm_para.h
227*aa274c33SPaolo Bonzini#include "standard-headers/linux/kvm_para.h"
228*aa274c33SPaolo Bonzini#include <asm/kvm_para.h>
229*aa274c33SPaolo BonziniEOF
23005e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_config.h
23105e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_config.h"
23205e492b0SMichael S. TsirkinEOF
23305e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_ring.h
23405e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_ring.h"
23505e492b0SMichael S. TsirkinEOF
236a0a6ef91SPaolo Bonzinicat <<EOF >$output/linux-headers/linux/vhost_types.h
237a0a6ef91SPaolo Bonzini#include "standard-headers/linux/vhost_types.h"
238a0a6ef91SPaolo BonziniEOF
2391ff0b555SMichael S. Tsirkin
240eddb4de3SPaolo Bonzinirm -rf "$output/include/standard-headers/linux"
241eddb4de3SPaolo Bonzinimkdir -p "$output/include/standard-headers/linux"
242b51ddd93SAlex Bennéefor i in "$hdrdir"/include/linux/*virtio*.h \
243b51ddd93SAlex Bennée         "$hdrdir/include/linux/qemu_fw_cfg.h" \
244b51ddd93SAlex Bennée         "$hdrdir/include/linux/fuse.h" \
245b51ddd93SAlex Bennée         "$hdrdir/include/linux/input.h" \
246b51ddd93SAlex Bennée         "$hdrdir/include/linux/input-event-codes.h" \
247b51ddd93SAlex Bennée         "$hdrdir/include/linux/udmabuf.h" \
248b51ddd93SAlex Bennée         "$hdrdir/include/linux/pci_regs.h" \
249b51ddd93SAlex Bennée         "$hdrdir/include/linux/ethtool.h" \
250b51ddd93SAlex Bennée         "$hdrdir/include/linux/const.h" \
251b51ddd93SAlex Bennée         "$hdrdir/include/linux/kernel.h" \
252*aa274c33SPaolo Bonzini         "$hdrdir/include/linux/kvm_para.h" \
253b51ddd93SAlex Bennée         "$hdrdir/include/linux/vhost_types.h" \
254b8116f4cSPaolo Bonzini         "$hdrdir/include/linux/sysinfo.h"; do
255eddb4de3SPaolo Bonzini    cp_portable "$i" "$output/include/standard-headers/linux"
256eddb4de3SPaolo Bonzinidone
257b8116f4cSPaolo Bonzinimkdir -p "$output/include/standard-headers/misc"
258b8116f4cSPaolo Bonzinicp_portable "$hdrdir/include/misc/pvpanic.h" \
259b8116f4cSPaolo Bonzini            "$output/include/standard-headers/misc"
2608e8ee850SGerd Hoffmannmkdir -p "$output/include/standard-headers/drm"
261b51ddd93SAlex Bennéecp_portable "$hdrdir/include/drm/drm_fourcc.h" \
2628e8ee850SGerd Hoffmann            "$output/include/standard-headers/drm"
2631ff0b555SMichael S. Tsirkin
2641ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/types.h
2658bc92a76SPeter Maydell/* For QEMU all types are already defined via osdep.h, so this
2668bc92a76SPeter Maydell * header does not need to do anything.
2678bc92a76SPeter Maydell */
2681ff0b555SMichael S. TsirkinEOF
2691ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/if_ether.h
2701ff0b555SMichael S. Tsirkin#define ETH_ALEN    6
2711ff0b555SMichael S. TsirkinEOF
2721ff0b555SMichael S. Tsirkin
27387fdd476SJan Kiszkarm -rf "$tmpdir"
274