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 1387fdd476SJan Kiszkatmpdir=`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"/' \ 541ff0b555SMichael 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 7887fdd476SJan Kiszka make -C "$linux" INSTALL_HDR_PATH="$tmpdir" SRCARCH=$arch headers_install 7987fdd476SJan Kiszka 8087fdd476SJan Kiszka rm -rf "$output/linux-headers/asm-$arch" 8187fdd476SJan Kiszka mkdir -p "$output/linux-headers/asm-$arch" 821842bdfdSMarc-André Lureau for header in kvm.h kvm_para.h unistd.h; do 8387fdd476SJan Kiszka cp "$tmpdir/include/asm/$header" "$output/linux-headers/asm-$arch" 8487fdd476SJan Kiszka done 85d56af005SBharat Bhushan if [ $arch = powerpc ]; then 86d56af005SBharat Bhushan cp "$tmpdir/include/asm/epapr_hcalls.h" "$output/linux-headers/asm-powerpc/" 87d56af005SBharat Bhushan fi 8844fb1dd4SMichael S. Tsirkin 89eddb4de3SPaolo Bonzini rm -rf "$output/include/standard-headers/asm-$arch" 90eddb4de3SPaolo Bonzini mkdir -p "$output/include/standard-headers/asm-$arch" 91eddb4de3SPaolo Bonzini if [ $arch = s390 ]; then 92eddb4de3SPaolo Bonzini cp_portable "$tmpdir/include/asm/kvm_virtio.h" "$output/include/standard-headers/asm-s390/" 93eddb4de3SPaolo Bonzini cp_portable "$tmpdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/" 94eddb4de3SPaolo Bonzini fi 9573aa529aSPaolo Bonzini if [ $arch = x86 ]; then 9673aa529aSPaolo Bonzini cp_portable "$tmpdir/include/asm/hyperv.h" "$output/include/standard-headers/asm-x86/" 971842bdfdSMarc-André Lureau cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/" 981842bdfdSMarc-André Lureau cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/" 991842bdfdSMarc-André Lureau cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/" 10073aa529aSPaolo Bonzini fi 10187fdd476SJan Kiszkadone 10287fdd476SJan Kiszka 10387fdd476SJan Kiszkarm -rf "$output/linux-headers/linux" 10487fdd476SJan Kiszkamkdir -p "$output/linux-headers/linux" 10505e492b0SMichael S. Tsirkinfor header in kvm.h kvm_para.h vfio.h vhost.h \ 1062ae823d4SAlexey Kardashevskiy psci.h userfaultfd.h; do 10787fdd476SJan Kiszka cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" 10887fdd476SJan Kiszkadone 109256d046cSPeter Maydellrm -rf "$output/linux-headers/asm-generic" 110256d046cSPeter Maydellmkdir -p "$output/linux-headers/asm-generic" 111256d046cSPeter Maydellfor header in kvm_para.h; do 112256d046cSPeter Maydell cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic" 113256d046cSPeter Maydelldone 11487fdd476SJan Kiszkaif [ -L "$linux/source" ]; then 11587fdd476SJan Kiszka cp "$linux/source/COPYING" "$output/linux-headers" 11687fdd476SJan Kiszkaelse 11787fdd476SJan Kiszka cp "$linux/COPYING" "$output/linux-headers" 11887fdd476SJan Kiszkafi 11987fdd476SJan Kiszka 12073aa529aSPaolo Bonzinicat <<EOF >$output/linux-headers/asm-x86/hyperv.h 12173aa529aSPaolo Bonzini#include "standard-headers/asm-x86/hyperv.h" 12273aa529aSPaolo BonziniEOF 12305e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_config.h 12405e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_config.h" 12505e492b0SMichael S. TsirkinEOF 12605e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_ring.h 12705e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_ring.h" 12805e492b0SMichael S. TsirkinEOF 1291ff0b555SMichael S. Tsirkin 130eddb4de3SPaolo Bonzinirm -rf "$output/include/standard-headers/linux" 131eddb4de3SPaolo Bonzinimkdir -p "$output/include/standard-headers/linux" 132eddb4de3SPaolo Bonzinifor i in "$tmpdir"/include/linux/*virtio*.h "$tmpdir/include/linux/input.h" \ 133fff02bc0SPaolo Bonzini "$tmpdir/include/linux/input-event-codes.h" \ 134eddb4de3SPaolo Bonzini "$tmpdir/include/linux/pci_regs.h"; do 135eddb4de3SPaolo Bonzini cp_portable "$i" "$output/include/standard-headers/linux" 136eddb4de3SPaolo Bonzinidone 1371ff0b555SMichael S. Tsirkin 1381ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/types.h 139*8bc92a76SPeter Maydell/* For QEMU all types are already defined via osdep.h, so this 140*8bc92a76SPeter Maydell * header does not need to do anything. 141*8bc92a76SPeter Maydell */ 1421ff0b555SMichael S. TsirkinEOF 1431ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/if_ether.h 1441ff0b555SMichael S. Tsirkin#define ETH_ALEN 6 1451ff0b555SMichael S. TsirkinEOF 1461ff0b555SMichael S. Tsirkin 14787fdd476SJan Kiszkarm -rf "$tmpdir" 148