xref: /qemu/scripts/update-linux-headers.sh (revision 40bf8e9aede0f9105a9e1e4aaf17b20aaa55f9a0)
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.
1287fdd476SJan Kiszka
13bbd90802SStefan Weiltmpdir=$(mktemp -d)
1487fdd476SJan Kiszkalinux="$1"
1587fdd476SJan Kiszkaoutput="$2"
1687fdd476SJan Kiszka
1787fdd476SJan Kiszkaif [ -z "$linux" ] || ! [ -d "$linux" ]; then
1887fdd476SJan Kiszka    cat << EOF
1987fdd476SJan Kiszkausage: update-kernel-headers.sh LINUX_PATH [OUTPUT_PATH]
2087fdd476SJan Kiszka
2187fdd476SJan KiszkaLINUX_PATH      Linux kernel directory to obtain the headers from
2287fdd476SJan KiszkaOUTPUT_PATH     output directory, usually the qemu source tree (default: $PWD)
2387fdd476SJan KiszkaEOF
2487fdd476SJan Kiszka    exit 1
2587fdd476SJan Kiszkafi
2687fdd476SJan Kiszka
2787fdd476SJan Kiszkaif [ -z "$output" ]; then
2887fdd476SJan Kiszka    output="$PWD"
2987fdd476SJan Kiszkafi
3087fdd476SJan Kiszka
31eddb4de3SPaolo Bonzinicp_portable() {
32eddb4de3SPaolo Bonzini    f=$1
331ff0b555SMichael S. Tsirkin    to=$2
341ff0b555SMichael S. Tsirkin    if
351ff0b555SMichael S. Tsirkin        grep '#include' "$f" | grep -v -e 'linux/virtio' \
361ff0b555SMichael S. Tsirkin                                     -e 'linux/types' \
37120758fbSPaolo Bonzini                                     -e 'stdint' \
381ff0b555SMichael S. Tsirkin                                     -e 'linux/if_ether' \
39fff02bc0SPaolo Bonzini                                     -e 'input-event-codes' \
402fe7c318SGerd Hoffmann                                     -e 'sys/' \
411ff0b555SMichael S. Tsirkin                                     > /dev/null
421ff0b555SMichael S. Tsirkin    then
431ff0b555SMichael S. Tsirkin        echo "Unexpected #include in input file $f".
441ff0b555SMichael S. Tsirkin        exit 2
451ff0b555SMichael S. Tsirkin    fi
461ff0b555SMichael S. Tsirkin
471ff0b555SMichael S. Tsirkin    header=$(basename "$f");
481ff0b555SMichael S. Tsirkin    sed -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \
492fe7c318SGerd Hoffmann        -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \
501ff0b555SMichael S. Tsirkin        -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \
511ff0b555SMichael S. Tsirkin        -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \
52fff02bc0SPaolo Bonzini        -e 's/"\(input-event-codes\.h\)"/"standard-headers\/linux\/\1"/' \
531ff0b555SMichael S. Tsirkin        -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \
54ed219c40SMichael S. Tsirkin        -e 's/__bitwise//' \
551ff0b555SMichael S. Tsirkin        -e 's/__attribute__((packed))/QEMU_PACKED/' \
56c16758cbSMichael S. Tsirkin        -e 's/__inline__/inline/' \
57e2f6bac3SGerd Hoffmann        -e '/sys\/ioctl.h/d' \
58ac98fa84SMarkus Armbruster        -e 's/SW_MAX/SW_MAX_/' \
591ff0b555SMichael S. Tsirkin        "$f" > "$to/$header";
601ff0b555SMichael S. Tsirkin}
611ff0b555SMichael S. Tsirkin
622879636dSPeter Maydell# This will pick up non-directories too (eg "Kconfig") but we will
632879636dSPeter Maydell# ignore them in the next loop.
642879636dSPeter MaydellARCHLIST=$(cd "$linux/arch" && echo *)
652879636dSPeter Maydell
662879636dSPeter Maydellfor arch in $ARCHLIST; do
672879636dSPeter Maydell    # Discard anything which isn't a KVM-supporting architecture
68b55f546eSPeter Maydell    if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] &&
69b55f546eSPeter Maydell        ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then
702879636dSPeter Maydell        continue
712879636dSPeter Maydell    fi
722879636dSPeter Maydell
732879636dSPeter Maydell    # Blacklist architectures which have KVM headers but are actually dead
741842bdfdSMarc-André Lureau    if [ "$arch" = "ia64" -o "$arch" = "mips" ]; then
752879636dSPeter Maydell        continue
762879636dSPeter Maydell    fi
772879636dSPeter Maydell
78f717e624SPaolo Bonzini    if [ "$arch" = x86 ]; then
79f717e624SPaolo Bonzini        arch_var=SRCARCH
80f717e624SPaolo Bonzini    else
81f717e624SPaolo Bonzini        arch_var=ARCH
82f717e624SPaolo Bonzini    fi
83f717e624SPaolo Bonzini
84f717e624SPaolo Bonzini    make -C "$linux" INSTALL_HDR_PATH="$tmpdir" $arch_var=$arch headers_install
8587fdd476SJan Kiszka
8687fdd476SJan Kiszka    rm -rf "$output/linux-headers/asm-$arch"
8787fdd476SJan Kiszka    mkdir -p "$output/linux-headers/asm-$arch"
881842bdfdSMarc-André Lureau    for header in kvm.h kvm_para.h unistd.h; do
8987fdd476SJan Kiszka        cp "$tmpdir/include/asm/$header" "$output/linux-headers/asm-$arch"
9087fdd476SJan Kiszka    done
91d56af005SBharat Bhushan    if [ $arch = powerpc ]; then
92d56af005SBharat Bhushan        cp "$tmpdir/include/asm/epapr_hcalls.h" "$output/linux-headers/asm-powerpc/"
93d56af005SBharat Bhushan    fi
9444fb1dd4SMichael S. Tsirkin
95eddb4de3SPaolo Bonzini    rm -rf "$output/include/standard-headers/asm-$arch"
96eddb4de3SPaolo Bonzini    mkdir -p "$output/include/standard-headers/asm-$arch"
97eddb4de3SPaolo Bonzini    if [ $arch = s390 ]; then
98eddb4de3SPaolo Bonzini        cp_portable "$tmpdir/include/asm/kvm_virtio.h" "$output/include/standard-headers/asm-s390/"
99eddb4de3SPaolo Bonzini        cp_portable "$tmpdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/"
100eddb4de3SPaolo Bonzini    fi
101f717e624SPaolo Bonzini    if [ $arch = arm ]; then
102f717e624SPaolo Bonzini        cp "$tmpdir/include/asm/unistd-eabi.h" "$output/linux-headers/asm-arm/"
103f717e624SPaolo Bonzini        cp "$tmpdir/include/asm/unistd-oabi.h" "$output/linux-headers/asm-arm/"
104f717e624SPaolo Bonzini        cp "$tmpdir/include/asm/unistd-common.h" "$output/linux-headers/asm-arm/"
105f717e624SPaolo Bonzini    fi
10673aa529aSPaolo Bonzini    if [ $arch = x86 ]; then
107*40bf8e9aSRoman Kagan        cat <<-EOF >"$output/include/standard-headers/asm-x86/hyperv.h"
108*40bf8e9aSRoman Kagan        /* this is a temporary placeholder until kvm_para.h stops including it */
109*40bf8e9aSRoman Kagan        EOF
1101842bdfdSMarc-André Lureau        cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/"
1111842bdfdSMarc-André Lureau        cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
1121842bdfdSMarc-André Lureau        cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
11373aa529aSPaolo Bonzini    fi
11487fdd476SJan Kiszkadone
11587fdd476SJan Kiszka
11687fdd476SJan Kiszkarm -rf "$output/linux-headers/linux"
11787fdd476SJan Kiszkamkdir -p "$output/linux-headers/linux"
11874c98e20SCornelia Huckfor header in kvm.h kvm_para.h vfio.h vfio_ccw.h vhost.h \
1192ae823d4SAlexey Kardashevskiy              psci.h userfaultfd.h; do
12087fdd476SJan Kiszka    cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
12187fdd476SJan Kiszkadone
122256d046cSPeter Maydellrm -rf "$output/linux-headers/asm-generic"
123256d046cSPeter Maydellmkdir -p "$output/linux-headers/asm-generic"
124256d046cSPeter Maydellfor header in kvm_para.h; do
125256d046cSPeter Maydell    cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic"
126256d046cSPeter Maydelldone
12787fdd476SJan Kiszkaif [ -L "$linux/source" ]; then
12887fdd476SJan Kiszka    cp "$linux/source/COPYING" "$output/linux-headers"
12987fdd476SJan Kiszkaelse
13087fdd476SJan Kiszka    cp "$linux/COPYING" "$output/linux-headers"
13187fdd476SJan Kiszkafi
13287fdd476SJan Kiszka
13373aa529aSPaolo Bonzinicat <<EOF >$output/linux-headers/asm-x86/hyperv.h
13473aa529aSPaolo Bonzini#include "standard-headers/asm-x86/hyperv.h"
13573aa529aSPaolo BonziniEOF
13605e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_config.h
13705e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_config.h"
13805e492b0SMichael S. TsirkinEOF
13905e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_ring.h
14005e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_ring.h"
14105e492b0SMichael S. TsirkinEOF
1421ff0b555SMichael S. Tsirkin
143eddb4de3SPaolo Bonzinirm -rf "$output/include/standard-headers/linux"
144eddb4de3SPaolo Bonzinimkdir -p "$output/include/standard-headers/linux"
145eddb4de3SPaolo Bonzinifor i in "$tmpdir"/include/linux/*virtio*.h "$tmpdir/include/linux/input.h" \
146fff02bc0SPaolo Bonzini         "$tmpdir/include/linux/input-event-codes.h" \
147eddb4de3SPaolo Bonzini         "$tmpdir/include/linux/pci_regs.h"; do
148eddb4de3SPaolo Bonzini    cp_portable "$i" "$output/include/standard-headers/linux"
149eddb4de3SPaolo Bonzinidone
1501ff0b555SMichael S. Tsirkin
1511ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/types.h
1528bc92a76SPeter Maydell/* For QEMU all types are already defined via osdep.h, so this
1538bc92a76SPeter Maydell * header does not need to do anything.
1548bc92a76SPeter Maydell */
1551ff0b555SMichael S. TsirkinEOF
1561ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/if_ether.h
1571ff0b555SMichael S. Tsirkin#define ETH_ALEN    6
1581ff0b555SMichael S. TsirkinEOF
1591ff0b555SMichael S. Tsirkin
16087fdd476SJan Kiszkarm -rf "$tmpdir"
161