1#!/bin/sh -e 2# 3# Update Linux kernel headers QEMU requires from a specified kernel tree. 4# 5# Copyright (C) 2011 Siemens AG 6# 7# Authors: 8# Jan Kiszka <jan.kiszka@siemens.com> 9# 10# This work is licensed under the terms of the GNU GPL version 2. 11# See the COPYING file in the top-level directory. 12# 13# The script will copy the headers into two target folders: 14# 15# - linux-headers/ for files that are required for compiling for a 16# Linux host. Generally we have these so we can use kernel structs 17# and defines that are more recent than the headers that might be 18# installed on the host system. Usually this script can do simple 19# file copies for these headers. 20# 21# - include/standard-headers/ for files that are used for guest 22# device emulation and are required on all hosts. For instance, we 23# get our definitions of the virtio structures from the Linux 24# kernel headers, but we need those definitions regardless of which 25# host OS we are building for. This script has to be careful to 26# sanitize the headers to remove any use of Linux-specifics such as 27# types like "__u64". This work is done in the cp_portable function. 28 29tmpdir=$(mktemp -d) 30linux="$1" 31output="$2" 32 33if [ -z "$linux" ] || ! [ -d "$linux" ]; then 34 cat << EOF 35usage: update-kernel-headers.sh LINUX_PATH [OUTPUT_PATH] 36 37LINUX_PATH Linux kernel directory to obtain the headers from 38OUTPUT_PATH output directory, usually the qemu source tree (default: $PWD) 39EOF 40 exit 1 41fi 42 43if [ -z "$output" ]; then 44 output="$PWD" 45fi 46 47cp_portable() { 48 f=$1 49 to=$2 50 if 51 grep '#include' "$f" | grep -v -e 'linux/virtio' \ 52 -e 'linux/types' \ 53 -e 'linux/ioctl' \ 54 -e 'stdint' \ 55 -e 'linux/if_ether' \ 56 -e 'input-event-codes' \ 57 -e 'sys/' \ 58 -e 'pvrdma_verbs' \ 59 -e 'drm.h' \ 60 -e 'limits' \ 61 -e 'linux/const' \ 62 -e 'linux/kernel' \ 63 -e 'linux/sysinfo' \ 64 -e 'asm/setup_data.h' \ 65 > /dev/null 66 then 67 echo "Unexpected #include in input file $f". 68 exit 2 69 fi 70 71 header=$(basename "$f"); 72 sed -e 's/__aligned_u64/__u64 __attribute__((aligned(8)))/g' \ 73 -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \ 74 -e 's/u\([0-9][0-9]*\)/uint\1_t/g' \ 75 -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \ 76 -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \ 77 -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \ 78 -e 's/"\(input-event-codes\.h\)"/"standard-headers\/linux\/\1"/' \ 79 -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \ 80 -e 's/<asm\/\([^>]*\)>/"standard-headers\/asm-'$arch'\/\1"/' \ 81 -e 's/__bitwise//' \ 82 -e 's/__attribute__((packed))/QEMU_PACKED/' \ 83 -e 's/__inline__/inline/' \ 84 -e 's/__BITS_PER_LONG/HOST_LONG_BITS/' \ 85 -e '/\"drm.h\"/d' \ 86 -e '/sys\/ioctl.h/d' \ 87 -e '/linux\/ioctl.h/d' \ 88 -e 's/SW_MAX/SW_MAX_/' \ 89 -e 's/atomic_t/int/' \ 90 -e 's/__kernel_long_t/long/' \ 91 -e 's/__kernel_ulong_t/unsigned long/' \ 92 -e 's/struct ethhdr/struct eth_header/' \ 93 -e '/\#define _LINUX_ETHTOOL_H/a \\n\#include "net/eth.h"' \ 94 "$f" > "$to/$header"; 95} 96 97# This will pick up non-directories too (eg "Kconfig") but we will 98# ignore them in the next loop. 99ARCHLIST=$(cd "$linux/arch" && echo *) 100 101for arch in $ARCHLIST; do 102 # Discard anything which isn't a KVM-supporting architecture 103 if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] && 104 ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then 105 continue 106 fi 107 108 if [ "$arch" = x86 ]; then 109 arch_var=SRCARCH 110 else 111 arch_var=ARCH 112 fi 113 114 make -C "$linux" INSTALL_HDR_PATH="$tmpdir" $arch_var=$arch headers_install 115 116 rm -rf "$output/linux-headers/asm-$arch" 117 mkdir -p "$output/linux-headers/asm-$arch" 118 for header in kvm.h unistd.h bitsperlong.h mman.h; do 119 cp "$tmpdir/include/asm/$header" "$output/linux-headers/asm-$arch" 120 done 121 122 if [ $arch = mips ]; then 123 cp "$tmpdir/include/asm/sgidefs.h" "$output/linux-headers/asm-mips/" 124 cp "$tmpdir/include/asm/unistd_o32.h" "$output/linux-headers/asm-mips/" 125 cp "$tmpdir/include/asm/unistd_n32.h" "$output/linux-headers/asm-mips/" 126 cp "$tmpdir/include/asm/unistd_n64.h" "$output/linux-headers/asm-mips/" 127 fi 128 if [ $arch = powerpc ]; then 129 cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-powerpc/" 130 cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-powerpc/" 131 fi 132 133 rm -rf "$output/include/standard-headers/asm-$arch" 134 mkdir -p "$output/include/standard-headers/asm-$arch" 135 if [ $arch = s390 ]; then 136 cp_portable "$tmpdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/" 137 cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-s390/" 138 cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/" 139 fi 140 if [ $arch = arm ]; then 141 cp "$tmpdir/include/asm/unistd-eabi.h" "$output/linux-headers/asm-arm/" 142 cp "$tmpdir/include/asm/unistd-oabi.h" "$output/linux-headers/asm-arm/" 143 cp "$tmpdir/include/asm/unistd-common.h" "$output/linux-headers/asm-arm/" 144 fi 145 if [ $arch = arm64 ]; then 146 cp "$tmpdir/include/asm/sve_context.h" "$output/linux-headers/asm-arm64/" 147 fi 148 if [ $arch = x86 ]; then 149 cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/" 150 cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/" 151 cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/" 152 cp_portable "$tmpdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch" 153 # Remove everything except the macros from bootparam.h avoiding the 154 # unnecessary import of several video/ist/etc headers 155 sed -e '/__ASSEMBLY__/,/__ASSEMBLY__/d' \ 156 "$tmpdir/include/asm/bootparam.h" > "$tmpdir/bootparam.h" 157 cp_portable "$tmpdir/bootparam.h" \ 158 "$output/include/standard-headers/asm-$arch" 159 cp_portable "$tmpdir/include/asm/setup_data.h" \ 160 "$output/standard-headers/asm-x86" 161 fi 162 if [ $arch = riscv ]; then 163 cp "$tmpdir/include/asm/ptrace.h" "$output/linux-headers/asm-riscv/" 164 fi 165done 166arch= 167 168rm -rf "$output/linux-headers/linux" 169mkdir -p "$output/linux-headers/linux" 170for header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \ 171 psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \ 172 vduse.h iommufd.h bits.h; do 173 cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" 174done 175 176rm -rf "$output/linux-headers/asm-generic" 177mkdir -p "$output/linux-headers/asm-generic" 178for header in unistd.h bitsperlong.h mman-common.h mman.h hugetlb_encode.h; do 179 cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic" 180done 181 182if [ -L "$linux/source" ]; then 183 cp "$linux/source/COPYING" "$output/linux-headers" 184else 185 cp "$linux/COPYING" "$output/linux-headers" 186fi 187 188# Recent kernel sources split the copyright/license info into multiple 189# files, which we need to copy. This set of licenses is the set that 190# are referred to by SPDX lines in the headers we currently copy. 191# We don't copy the Documentation/process/license-rules.rst which 192# is also referred to by COPYING, since it's explanatory rather than license. 193if [ -d "$linux/LICENSES" ]; then 194 mkdir -p "$output/linux-headers/LICENSES/preferred" \ 195 "$output/linux-headers/LICENSES/exceptions" 196 for l in preferred/GPL-2.0 preferred/BSD-2-Clause preferred/BSD-3-Clause \ 197 exceptions/Linux-syscall-note; do 198 cp "$linux/LICENSES/$l" "$output/linux-headers/LICENSES/$l" 199 done 200fi 201 202cat <<EOF >$output/linux-headers/linux/virtio_config.h 203#include "standard-headers/linux/virtio_config.h" 204EOF 205cat <<EOF >$output/linux-headers/linux/virtio_ring.h 206#include "standard-headers/linux/virtio_ring.h" 207EOF 208cat <<EOF >$output/linux-headers/linux/vhost_types.h 209#include "standard-headers/linux/vhost_types.h" 210EOF 211 212rm -rf "$output/include/standard-headers/linux" 213mkdir -p "$output/include/standard-headers/linux" 214for i in "$tmpdir"/include/linux/*virtio*.h \ 215 "$tmpdir/include/linux/qemu_fw_cfg.h" \ 216 "$tmpdir/include/linux/fuse.h" \ 217 "$tmpdir/include/linux/input.h" \ 218 "$tmpdir/include/linux/input-event-codes.h" \ 219 "$tmpdir/include/linux/udmabuf.h" \ 220 "$tmpdir/include/linux/pci_regs.h" \ 221 "$tmpdir/include/linux/ethtool.h" \ 222 "$tmpdir/include/linux/const.h" \ 223 "$tmpdir/include/linux/kernel.h" \ 224 "$tmpdir/include/linux/vhost_types.h" \ 225 "$tmpdir/include/linux/sysinfo.h" \ 226 "$tmpdir/include/misc/pvpanic.h"; do 227 cp_portable "$i" "$output/include/standard-headers/linux" 228done 229mkdir -p "$output/include/standard-headers/drm" 230cp_portable "$tmpdir/include/drm/drm_fourcc.h" \ 231 "$output/include/standard-headers/drm" 232 233rm -rf "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma" 234mkdir -p "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma" 235 236# Remove the unused functions from pvrdma_verbs.h avoiding the unnecessary 237# import of several infiniband/networking/other headers 238tmp_pvrdma_verbs="$tmpdir/pvrdma_verbs.h" 239# Parse the entire file instead of single lines to match 240# function declarations expanding over multiple lines 241# and strip the declarations starting with pvrdma prefix. 242sed -e '1h;2,$H;$!d;g' -e 's/[^};]*pvrdma[^(| ]*([^)]*);//g' \ 243 "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_verbs.h" > \ 244 "$tmp_pvrdma_verbs"; 245 246for i in "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_dev_api.h" \ 247 "$tmp_pvrdma_verbs"; do \ 248 cp_portable "$i" \ 249 "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/" 250done 251 252rm -rf "$output/include/standard-headers/rdma/" 253mkdir -p "$output/include/standard-headers/rdma/" 254for i in "$tmpdir/include/rdma/vmw_pvrdma-abi.h"; do 255 cp_portable "$i" \ 256 "$output/include/standard-headers/rdma/" 257done 258 259cat <<EOF >$output/include/standard-headers/linux/types.h 260/* For QEMU all types are already defined via osdep.h, so this 261 * header does not need to do anything. 262 */ 263EOF 264cat <<EOF >$output/include/standard-headers/linux/if_ether.h 265#define ETH_ALEN 6 266EOF 267 268rm -rf "$tmpdir" 269