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