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/' \ 588e8ee850SGerd Hoffmann -e 'drm.h' \ 59d3b7b374SJason Baron -e 'limits' \ 60ab5ec23fSEric Farman -e 'linux/const' \ 61d3b7b374SJason Baron -e 'linux/kernel' \ 62d3b7b374SJason Baron -e 'linux/sysinfo' \ 63ec7b1080SMichael S. Tsirkin -e 'asm/setup_data.h' \ 641ff0b555SMichael S. Tsirkin > /dev/null 651ff0b555SMichael S. Tsirkin then 661ff0b555SMichael S. Tsirkin echo "Unexpected #include in input file $f". 671ff0b555SMichael S. Tsirkin exit 2 681ff0b555SMichael S. Tsirkin fi 691ff0b555SMichael S. Tsirkin 701ff0b555SMichael S. Tsirkin header=$(basename "$f"); 71c5022c31SPeter Maydell sed -e 's/__aligned_u64/__u64 __attribute__((aligned(8)))/g' \ 72c5022c31SPeter Maydell -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \ 73e1c5f1f0SMarcel Apfelbaum -e 's/u\([0-9][0-9]*\)/uint\1_t/g' \ 742fe7c318SGerd Hoffmann -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \ 751ff0b555SMichael S. Tsirkin -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \ 761ff0b555SMichael S. Tsirkin -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \ 77fff02bc0SPaolo Bonzini -e 's/"\(input-event-codes\.h\)"/"standard-headers\/linux\/\1"/' \ 781ff0b555SMichael S. Tsirkin -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \ 79ed219c40SMichael S. Tsirkin -e 's/<asm\/\([^>]*\)>/"standard-headers\/asm-'$arch'\/\1"/' \ 801ff0b555SMichael S. Tsirkin -e 's/__bitwise//' \ 81c16758cbSMichael S. Tsirkin -e 's/__attribute__((packed))/QEMU_PACKED/' \ 829f2d175dSPaolo Bonzini -e 's/__inline__/inline/' \ 838e8ee850SGerd Hoffmann -e 's/__BITS_PER_LONG/HOST_LONG_BITS/' \ 84e2f6bac3SGerd Hoffmann -e '/\"drm.h\"/d' \ 854d010861SVivek Kasireddy -e '/sys\/ioctl.h/d' \ 86ac98fa84SMarkus Armbruster -e '/linux\/ioctl.h/d' \ 87e1c5f1f0SMarcel Apfelbaum -e 's/SW_MAX/SW_MAX_/' \ 88d3b7b374SJason Baron -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/' \ 921ff0b555SMichael S. Tsirkin -e '/\#define _LINUX_ETHTOOL_H/a \\n\#include "net/eth.h"' \ 931ff0b555SMichael S. Tsirkin "$f" > "$to/$header"; 941ff0b555SMichael S. Tsirkin} 952879636dSPeter Maydell 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 101b55f546eSPeter Maydell # Discard anything which isn't a KVM-supporting architecture 102b55f546eSPeter Maydell if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] && 1032879636dSPeter Maydell ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then 1042879636dSPeter Maydell continue 1052879636dSPeter Maydell fi 106f717e624SPaolo Bonzini 107f717e624SPaolo Bonzini if [ "$arch" = x86 ]; then 108f717e624SPaolo Bonzini arch_var=SRCARCH 109f717e624SPaolo Bonzini else 110f717e624SPaolo Bonzini arch_var=ARCH 111f717e624SPaolo Bonzini fi 112f717e624SPaolo Bonzini 11387fdd476SJan Kiszka make -C "$linux" INSTALL_HDR_PATH="$tmpdir" $arch_var=$arch headers_install 11487fdd476SJan Kiszka 11587fdd476SJan Kiszka rm -rf "$output/linux-headers/asm-$arch" 1160289881fSZhang Yi mkdir -p "$output/linux-headers/asm-$arch" 11787fdd476SJan Kiszka for header in kvm.h unistd.h bitsperlong.h mman.h; do 11887fdd476SJan Kiszka cp "$tmpdir/include/asm/$header" "$output/linux-headers/asm-$arch" 1199882d3efSMichael S. Tsirkin done 1209882d3efSMichael S. Tsirkin 1219882d3efSMichael S. Tsirkin if [ $arch = mips ]; then 122a0a6ef91SPaolo Bonzini 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/" 1299882d3efSMichael S. Tsirkin cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-powerpc/" 1309882d3efSMichael S. Tsirkin fi 131eddb4de3SPaolo Bonzini 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 1359f2d175dSPaolo 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/" 137eddb4de3SPaolo Bonzini cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/" 138f717e624SPaolo 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/" 143b1b9e0dcSCornelia Huck fi 144b1b9e0dcSCornelia Huck if [ $arch = arm64 ]; then 145b1b9e0dcSCornelia Huck cp "$tmpdir/include/asm/sve_context.h" "$output/linux-headers/asm-arm64/" 14673aa529aSPaolo Bonzini fi 1471842bdfdSMarc-André Lureau 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/" 150ec7b1080SMichael S. Tsirkin cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/" 15106e0259aSLi Zhijian 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" \ 15773aa529aSPaolo Bonzini "$output/include/standard-headers/asm-$arch" 158*1583ca8aSDaniel Henrique Barboza cp_portable "$tmpdir/include/asm/setup_data.h" \ 159*1583ca8aSDaniel Henrique Barboza "$output/standard-headers/asm-x86" 160*1583ca8aSDaniel Henrique Barboza fi 16187fdd476SJan Kiszka if [ $arch = riscv ]; then 16287fdd476SJan Kiszka cp "$tmpdir/include/asm/ptrace.h" "$output/linux-headers/asm-riscv/" 16387fdd476SJan Kiszka fi 16487fdd476SJan Kiszkadone 1659fc7dd23SDavid 'Digit' Turnerarch= 1668cba58b5SEric Auger 1678cba58b5SEric Augerrm -rf "$output/linux-headers/linux" 16887fdd476SJan Kiszkamkdir -p "$output/linux-headers/linux" 16987fdd476SJan Kiszkafor header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \ 170ec7b1080SMichael S. Tsirkin psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \ 1719882d3efSMichael S. Tsirkin vduse.h iommufd.h bits.h; do 1729882d3efSMichael S. Tsirkin cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" 1730289881fSZhang Yidone 1749882d3efSMichael S. Tsirkin 1759882d3efSMichael S. Tsirkinrm -rf "$output/linux-headers/asm-generic" 1769882d3efSMichael S. Tsirkinmkdir -p "$output/linux-headers/asm-generic" 17787fdd476SJan Kiszkafor header in unistd.h bitsperlong.h mman-common.h mman.h hugetlb_encode.h; do 17887fdd476SJan Kiszka cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic" 17987fdd476SJan Kiszkadone 18087fdd476SJan Kiszka 18187fdd476SJan Kiszkaif [ -L "$linux/source" ]; then 18287fdd476SJan Kiszka cp "$linux/source/COPYING" "$output/linux-headers" 183f5bba4caSPeter Maydellelse 184f5bba4caSPeter Maydell cp "$linux/COPYING" "$output/linux-headers" 185f5bba4caSPeter Maydellfi 186f5bba4caSPeter Maydell 187f5bba4caSPeter Maydell# Recent kernel sources split the copyright/license info into multiple 188f5bba4caSPeter Maydell# files, which we need to copy. This set of licenses is the set that 189f5bba4caSPeter Maydell# are referred to by SPDX lines in the headers we currently copy. 190f5bba4caSPeter Maydell# We don't copy the Documentation/process/license-rules.rst which 191f5bba4caSPeter Maydell# is also referred to by COPYING, since it's explanatory rather than license. 192f5bba4caSPeter Maydellif [ -d "$linux/LICENSES" ]; then 193f5bba4caSPeter Maydell mkdir -p "$output/linux-headers/LICENSES/preferred" \ 194f5bba4caSPeter Maydell "$output/linux-headers/LICENSES/exceptions" 195f5bba4caSPeter Maydell for l in preferred/GPL-2.0 preferred/BSD-2-Clause preferred/BSD-3-Clause \ 196f5bba4caSPeter Maydell exceptions/Linux-syscall-note; do 19705e492b0SMichael S. Tsirkin cp "$linux/LICENSES/$l" "$output/linux-headers/LICENSES/$l" 19805e492b0SMichael S. Tsirkin done 19905e492b0SMichael S. Tsirkinfi 20005e492b0SMichael S. Tsirkin 20105e492b0SMichael S. Tsirkincat <<EOF >$output/linux-headers/linux/virtio_config.h 20205e492b0SMichael S. Tsirkin#include "standard-headers/linux/virtio_config.h" 203a0a6ef91SPaolo BonziniEOF 204a0a6ef91SPaolo Bonzinicat <<EOF >$output/linux-headers/linux/virtio_ring.h 205a0a6ef91SPaolo Bonzini#include "standard-headers/linux/virtio_ring.h" 2061ff0b555SMichael S. TsirkinEOF 207eddb4de3SPaolo Bonzinicat <<EOF >$output/linux-headers/linux/vhost_types.h 208eddb4de3SPaolo Bonzini#include "standard-headers/linux/vhost_types.h" 209039d7c4dSMarc-André LureauEOF 210039d7c4dSMarc-André Lureau 211a62a9e19SDr. David Alan Gilbertrm -rf "$output/include/standard-headers/linux" 212039d7c4dSMarc-André Lureaumkdir -p "$output/include/standard-headers/linux" 213fff02bc0SPaolo Bonzinifor i in "$tmpdir"/include/linux/*virtio*.h \ 2144d010861SVivek Kasireddy "$tmpdir/include/linux/qemu_fw_cfg.h" \ 215d3b7b374SJason Baron "$tmpdir/include/linux/fuse.h" \ 216ab5ec23fSEric Farman "$tmpdir/include/linux/input.h" \ 217ab5ec23fSEric Farman "$tmpdir/include/linux/input-event-codes.h" \ 218ab5ec23fSEric Farman "$tmpdir/include/linux/udmabuf.h" \ 219a0a6ef91SPaolo Bonzini "$tmpdir/include/linux/pci_regs.h" \ 220fcbd14dbSzhenwei pi "$tmpdir/include/linux/ethtool.h" \ 221fcbd14dbSzhenwei pi "$tmpdir/include/linux/const.h" \ 222eddb4de3SPaolo Bonzini "$tmpdir/include/linux/kernel.h" \ 223eddb4de3SPaolo Bonzini "$tmpdir/include/linux/vhost_types.h" \ 2248e8ee850SGerd Hoffmann "$tmpdir/include/linux/sysinfo.h" \ 2258e8ee850SGerd Hoffmann "$tmpdir/include/misc/pvpanic.h"; do 2268e8ee850SGerd Hoffmann cp_portable "$i" "$output/include/standard-headers/linux" 2271ff0b555SMichael S. Tsirkindone 2281ff0b555SMichael S. Tsirkinmkdir -p "$output/include/standard-headers/drm" 2298bc92a76SPeter Maydellcp_portable "$tmpdir/include/drm/drm_fourcc.h" \ 2308bc92a76SPeter Maydell "$output/include/standard-headers/drm" 2318bc92a76SPeter Maydell 2321ff0b555SMichael S. Tsirkincat <<EOF >$output/include/standard-headers/linux/types.h 2331ff0b555SMichael S. Tsirkin/* For QEMU all types are already defined via osdep.h, so this 2341ff0b555SMichael S. Tsirkin * header does not need to do anything. 2351ff0b555SMichael S. Tsirkin */ 2361ff0b555SMichael S. TsirkinEOF 23787fdd476SJan Kiszkacat <<EOF >$output/include/standard-headers/linux/if_ether.h 238#define ETH_ALEN 6 239EOF 240 241rm -rf "$tmpdir" 242