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 31*eddb4de3SPaolo Bonzinicp_portable() { 32*eddb4de3SPaolo 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' \ 392fe7c318SGerd Hoffmann -e 'sys/' \ 401ff0b555SMichael S. Tsirkin > /dev/null 411ff0b555SMichael S. Tsirkin then 421ff0b555SMichael S. Tsirkin echo "Unexpected #include in input file $f". 431ff0b555SMichael S. Tsirkin exit 2 441ff0b555SMichael S. Tsirkin fi 451ff0b555SMichael S. Tsirkin 461ff0b555SMichael S. Tsirkin header=$(basename "$f"); 471ff0b555SMichael S. Tsirkin sed -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \ 482fe7c318SGerd Hoffmann -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \ 491ff0b555SMichael S. Tsirkin -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \ 501ff0b555SMichael S. Tsirkin -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \ 511ff0b555SMichael S. Tsirkin -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \ 521ff0b555SMichael S. Tsirkin -e 's/__bitwise__//' \ 531ff0b555SMichael S. Tsirkin -e 's/__attribute__((packed))/QEMU_PACKED/' \ 54c16758cbSMichael S. Tsirkin -e 's/__inline__/inline/' \ 55e2f6bac3SGerd Hoffmann -e '/sys\/ioctl.h/d' \ 561ff0b555SMichael S. Tsirkin "$f" > "$to/$header"; 571ff0b555SMichael S. Tsirkin} 581ff0b555SMichael S. Tsirkin 592879636dSPeter Maydell# This will pick up non-directories too (eg "Kconfig") but we will 602879636dSPeter Maydell# ignore them in the next loop. 612879636dSPeter MaydellARCHLIST=$(cd "$linux/arch" && echo *) 622879636dSPeter Maydell 632879636dSPeter Maydellfor arch in $ARCHLIST; do 642879636dSPeter Maydell # Discard anything which isn't a KVM-supporting architecture 65b55f546eSPeter Maydell if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] && 66b55f546eSPeter Maydell ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then 672879636dSPeter Maydell continue 682879636dSPeter Maydell fi 692879636dSPeter Maydell 702879636dSPeter Maydell # Blacklist architectures which have KVM headers but are actually dead 712879636dSPeter Maydell if [ "$arch" = "ia64" ]; then 722879636dSPeter Maydell continue 732879636dSPeter Maydell fi 742879636dSPeter Maydell 7587fdd476SJan Kiszka make -C "$linux" INSTALL_HDR_PATH="$tmpdir" SRCARCH=$arch headers_install 7687fdd476SJan Kiszka 7787fdd476SJan Kiszka rm -rf "$output/linux-headers/asm-$arch" 7887fdd476SJan Kiszka mkdir -p "$output/linux-headers/asm-$arch" 7987fdd476SJan Kiszka for header in kvm.h kvm_para.h; do 8087fdd476SJan Kiszka cp "$tmpdir/include/asm/$header" "$output/linux-headers/asm-$arch" 8187fdd476SJan Kiszka done 8287fdd476SJan Kiszka if [ $arch = x86 ]; then 8387fdd476SJan Kiszka cp "$tmpdir/include/asm/hyperv.h" "$output/linux-headers/asm-x86" 8487fdd476SJan Kiszka fi 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 89*eddb4de3SPaolo Bonzini rm -rf "$output/include/standard-headers/asm-$arch" 90*eddb4de3SPaolo Bonzini mkdir -p "$output/include/standard-headers/asm-$arch" 91*eddb4de3SPaolo Bonzini if [ $arch = s390 ]; then 92*eddb4de3SPaolo Bonzini cp_portable "$tmpdir/include/asm/kvm_virtio.h" "$output/include/standard-headers/asm-s390/" 93*eddb4de3SPaolo Bonzini cp_portable "$tmpdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/" 94*eddb4de3SPaolo Bonzini fi 9587fdd476SJan Kiszkadone 9687fdd476SJan Kiszka 9787fdd476SJan Kiszkarm -rf "$output/linux-headers/linux" 9887fdd476SJan Kiszkamkdir -p "$output/linux-headers/linux" 9905e492b0SMichael S. Tsirkinfor header in kvm.h kvm_para.h vfio.h vhost.h \ 1002872e192SAlexander Graf psci.h; do 10187fdd476SJan Kiszka cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" 10287fdd476SJan Kiszkadone 103256d046cSPeter Maydellrm -rf "$output/linux-headers/asm-generic" 104256d046cSPeter Maydellmkdir -p "$output/linux-headers/asm-generic" 105256d046cSPeter Maydellfor header in kvm_para.h; do 106256d046cSPeter Maydell cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic" 107256d046cSPeter Maydelldone 10887fdd476SJan Kiszkaif [ -L "$linux/source" ]; then 10987fdd476SJan Kiszka cp "$linux/source/COPYING" "$output/linux-headers" 11087fdd476SJan Kiszkaelse 11187fdd476SJan Kiszka cp "$linux/COPYING" "$output/linux-headers" 11287fdd476SJan Kiszkafi 11387fdd476SJan Kiszka 11405e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_config.h 11505e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_config.h" 11605e492b0SMichael S. TsirkinEOF 11705e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_ring.h 11805e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_ring.h" 11905e492b0SMichael S. TsirkinEOF 1201ff0b555SMichael S. Tsirkin 121*eddb4de3SPaolo Bonzinirm -rf "$output/include/standard-headers/linux" 122*eddb4de3SPaolo Bonzinimkdir -p "$output/include/standard-headers/linux" 123*eddb4de3SPaolo Bonzinifor i in "$tmpdir"/include/linux/*virtio*.h "$tmpdir/include/linux/input.h" \ 124*eddb4de3SPaolo Bonzini "$tmpdir/include/linux/pci_regs.h"; do 125*eddb4de3SPaolo Bonzini cp_portable "$i" "$output/include/standard-headers/linux" 126*eddb4de3SPaolo Bonzinidone 1271ff0b555SMichael S. Tsirkin 1281ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/types.h 1291ff0b555SMichael S. Tsirkin#include <stdint.h> 1301ff0b555SMichael S. Tsirkin#include "qemu/compiler.h" 1311ff0b555SMichael S. TsirkinEOF 1321ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/if_ether.h 1331ff0b555SMichael S. Tsirkin#define ETH_ALEN 6 1341ff0b555SMichael S. TsirkinEOF 1351ff0b555SMichael S. Tsirkin 13687fdd476SJan Kiszkarm -rf "$tmpdir" 137