master
sh 280 lines 10.9 KB
Raw
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
29 tmpdir=$(mktemp -d)
30 hdrdir="$tmpdir/headers"
31 blddir="$tmpdir/build"
32 linux="$1"
33 output="$2"
34
35 if [ -z "$linux" ] || ! [ -d "$linux" ]; then
36 cat << EOF
37 usage: update-kernel-headers.sh LINUX_PATH [OUTPUT_PATH]
38
39 LINUX_PATH Linux kernel directory to obtain the headers from
40 OUTPUT_PATH output directory, usually the qemu source tree (default: $PWD)
41 EOF
42 exit 1
43 fi
44
45 if [ -z "$output" ]; then
46 output="$PWD"
47 fi
48
49 cp_portable() {
50 f=$1
51 to=$2
52 if
53 grep '#include' "$f" | grep -v -e 'linux/virtio' \
54 -e 'linux/types' \
55 -e 'linux/ioctl' \
56 -e 'stdint' \
57 -e 'linux/if_ether' \
58 -e 'input-event-codes' \
59 -e 'sys/' \
60 -e 'drm.h' \
61 -e 'limits' \
62 -e 'linux/const' \
63 -e 'linux/typelimits' \
64 -e 'linux/kernel' \
65 -e 'linux/sysinfo' \
66 -e 'asm/setup_data.h' \
67 -e 'asm/kvm_para.h' \
68 > /dev/null
69 then
70 echo "Unexpected #include in input file $f".
71 exit 2
72 fi
73
74 header=$(basename "$f");
75
76 if test -z "$arch"; then
77 # Let users of include/standard-headers/linux/ headers pick the
78 # asm-* header that they care about
79 arch_cmd='/<asm\/\([^>]*\)>/d'
80 else
81 arch_cmd='s/<asm\/\([^>]*\)>/"standard-headers\/asm-'$arch'\/\1"/'
82 fi
83
84 sed -e 's/__aligned_u64/__u64 __attribute__((aligned(8)))/g' \
85 -e 's/__u\([0-9][0-9]*\)/uint\1_t/g' \
86 -e 's/u\([0-9][0-9]*\)/uint\1_t/g' \
87 -e 's/__s\([0-9][0-9]*\)/int\1_t/g' \
88 -e 's/__le\([0-9][0-9]*\)/uint\1_t/g' \
89 -e 's/__be\([0-9][0-9]*\)/uint\1_t/g' \
90 -e 's/"\(input-event-codes\.h\)"/"standard-headers\/linux\/\1"/' \
91 -e 's/<linux\/\([^>]*\)>/"standard-headers\/linux\/\1"/' \
92 -e "$arch_cmd" \
93 -e 's/__bitwise//' \
94 -e 's/__counted_by(\w*)//' \
95 -e 's/__attribute__((packed))/QEMU_PACKED/' \
96 -e 's/__inline__/inline/' \
97 -e 's/__BITS_PER_LONG/HOST_LONG_BITS/' \
98 -e '/\"drm.h\"/d' \
99 -e '/sys\/ioctl.h/d' \
100 -e '/linux\/ioctl.h/d' \
101 -e 's/SW_MAX/SW_MAX_/' \
102 -e 's/atomic_t/int/' \
103 -e 's/__kernel_long_t/long/' \
104 -e 's/__kernel_ulong_t/unsigned long/' \
105 -e 's/struct ethhdr/struct eth_header/' \
106 -e '/\#define _LINUX_ETHTOOL_H/a \\n\#include "net/eth.h"' \
107 -e '/\#define _LINUX_VIRTIO_RING_H/a \\n\#define VIRTIO_RING_NO_LEGACY' \
108 "$f" > "$to/$header";
109 }
110
111 # This will pick up non-directories too (eg "Kconfig") but we will
112 # ignore them in the next loop.
113 ARCHLIST=$(cd "$linux/arch" && echo *)
114
115 for arch in $ARCHLIST; do
116 # Discard anything which isn't a KVM-supporting architecture
117 if ! [ -e "$linux/arch/$arch/include/asm/kvm.h" ] &&
118 ! [ -e "$linux/arch/$arch/include/uapi/asm/kvm.h" ] ; then
119 continue
120 fi
121
122 if [ "$arch" = x86 ]; then
123 arch_var=SRCARCH
124 else
125 arch_var=ARCH
126 fi
127
128 rm -rf "$hdrdir"
129 make -C "$linux" O="$blddir" INSTALL_HDR_PATH="$hdrdir" $arch_var=$arch headers_install
130
131 rm -rf "$output/linux-headers/asm-$arch"
132 mkdir -p "$output/linux-headers/asm-$arch"
133 for header in kvm.h unistd.h bitsperlong.h mman.h; do
134 if test -f "$hdrdir/include/asm/$header"; then
135 cp "$hdrdir/include/asm/$header" "$output/linux-headers/asm-$arch"
136 elif test -f "$hdrdir/include/asm-generic/$header"; then
137 # not installed as <asm/$header>, but used as such in kernel sources
138 cat <<EOF >$output/linux-headers/asm-$arch/$header
139 #include <asm-generic/$header>
140 EOF
141 fi
142 done
143
144 if [ $arch = mips ]; then
145 cp "$hdrdir/include/asm/sgidefs.h" "$output/linux-headers/asm-mips/"
146 cp "$hdrdir/include/asm/unistd_o32.h" "$output/linux-headers/asm-mips/"
147 cp "$hdrdir/include/asm/unistd_n32.h" "$output/linux-headers/asm-mips/"
148 cp "$hdrdir/include/asm/unistd_n64.h" "$output/linux-headers/asm-mips/"
149 fi
150 if [ $arch = powerpc ]; then
151 cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-powerpc/"
152 cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-powerpc/"
153 fi
154
155 rm -rf "$output/include/standard-headers/asm-$arch"
156 mkdir -p "$output/include/standard-headers/asm-$arch"
157 if [ $arch = s390 ]; then
158 cp_portable "$hdrdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/"
159 cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/"
160 fi
161 if [ $arch = arm64 ]; then
162 cp "$hdrdir/include/asm/sve_context.h" "$output/linux-headers/asm-arm64/"
163 cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-arm64/"
164 fi
165 if [ $arch = x86 ]; then
166 cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/"
167 cp "$hdrdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
168 cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
169
170 cp_portable "$hdrdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch"
171 cat <<EOF >$output/linux-headers/asm-$arch/kvm_para.h
172 #include "standard-headers/asm-$arch/kvm_para.h"
173 EOF
174
175 # Remove everything except the macros from bootparam.h avoiding the
176 # unnecessary import of several video/ist/etc headers
177 sed -e '/__ASSEMBLER__/,/__ASSEMBLER__/d' \
178 "$hdrdir/include/asm/bootparam.h" > "$hdrdir/bootparam.h"
179 cp_portable "$hdrdir/bootparam.h" \
180 "$output/include/standard-headers/asm-$arch"
181 cp_portable "$hdrdir/include/asm/setup_data.h" \
182 "$output/include/standard-headers/asm-x86"
183 fi
184 if [ $arch = riscv ]; then
185 cp "$hdrdir/include/asm/ptrace.h" "$output/linux-headers/asm-riscv/"
186 cp "$hdrdir/include/asm/unistd_32.h" "$output/linux-headers/asm-riscv/"
187 cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-riscv/"
188 fi
189 if [ $arch = loongarch ]; then
190 cp "$hdrdir/include/asm/kvm_para.h" "$output/linux-headers/asm-loongarch/"
191 cp "$hdrdir/include/asm/unistd_64.h" "$output/linux-headers/asm-loongarch/"
192 fi
193 done
194 arch=
195
196 rm -rf "$output/linux-headers/linux"
197 mkdir -p "$output/linux-headers/linux"
198 for header in const.h stddef.h kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \
199 psci.h psp-sev.h userfaultfd.h memfd.h mman.h nvme_ioctl.h \
200 vduse.h iommufd.h bits.h mshv.h; do
201 cp "$hdrdir/include/linux/$header" "$output/linux-headers/linux"
202 done
203
204 rm -rf "$output/linux-headers/asm-generic"
205 mkdir -p "$output/linux-headers/asm-generic"
206 for header in unistd.h bitsperlong.h mman-common.h mman.h hugetlb_encode.h; do
207 cp "$hdrdir/include/asm-generic/$header" "$output/linux-headers/asm-generic"
208 done
209
210 if [ -L "$linux/source" ]; then
211 cp "$linux/source/COPYING" "$output/linux-headers"
212 else
213 cp "$linux/COPYING" "$output/linux-headers"
214 fi
215
216 # Recent kernel sources split the copyright/license info into multiple
217 # files, which we need to copy. This set of licenses is the set that
218 # are referred to by SPDX lines in the headers we currently copy.
219 # We don't copy the Documentation/process/license-rules.rst which
220 # is also referred to by COPYING, since it's explanatory rather than license.
221 if [ -d "$linux/LICENSES" ]; then
222 mkdir -p "$output/linux-headers/LICENSES/preferred" \
223 "$output/linux-headers/LICENSES/exceptions"
224 for l in preferred/GPL-2.0 preferred/BSD-2-Clause preferred/BSD-3-Clause \
225 exceptions/Linux-syscall-note; do
226 cp "$linux/LICENSES/$l" "$output/linux-headers/LICENSES/$l"
227 done
228 fi
229
230 cat <<EOF >$output/linux-headers/linux/kvm_para.h
231 #include "standard-headers/linux/kvm_para.h"
232 #include <asm/kvm_para.h>
233 EOF
234 cat <<EOF >$output/linux-headers/linux/virtio_config.h
235 #include "standard-headers/linux/virtio_config.h"
236 EOF
237 cat <<EOF >$output/linux-headers/linux/virtio_ring.h
238 #include "standard-headers/linux/virtio_ring.h"
239 EOF
240 cat <<EOF >$output/linux-headers/linux/vhost_types.h
241 #include "standard-headers/linux/vhost_types.h"
242 EOF
243
244 rm -rf "$output/include/standard-headers/linux"
245 mkdir -p "$output/include/standard-headers/linux"
246 for i in "$hdrdir"/include/linux/*virtio*.h \
247 "$hdrdir/include/linux/qemu_fw_cfg.h" \
248 "$hdrdir/include/linux/fuse.h" \
249 "$hdrdir/include/linux/input.h" \
250 "$hdrdir/include/linux/input-event-codes.h" \
251 "$hdrdir/include/linux/udmabuf.h" \
252 "$hdrdir/include/linux/pci_regs.h" \
253 "$hdrdir/include/linux/ethtool.h" \
254 "$hdrdir/include/linux/const.h" \
255 "$hdrdir/include/linux/typelimits.h" \
256 "$hdrdir/include/linux/kernel.h" \
257 "$hdrdir/include/linux/kvm_para.h" \
258 "$hdrdir/include/linux/vhost_types.h" \
259 "$hdrdir/include/linux/vmclock-abi.h" \
260 "$hdrdir/include/linux/nitro_enclaves.h" \
261 "$hdrdir/include/linux/sysinfo.h"; do
262 cp_portable "$i" "$output/include/standard-headers/linux"
263 done
264 mkdir -p "$output/include/standard-headers/misc"
265 cp_portable "$hdrdir/include/misc/pvpanic.h" \
266 "$output/include/standard-headers/misc"
267 mkdir -p "$output/include/standard-headers/drm"
268 cp_portable "$hdrdir/include/drm/drm_fourcc.h" \
269 "$output/include/standard-headers/drm"
270
271 cat <<EOF >$output/include/standard-headers/linux/types.h
272 /* For QEMU all types are already defined via osdep.h, so this
273 * header does not need to do anything.
274 */
275 EOF
276 cat <<EOF >$output/include/standard-headers/linux/if_ether.h
277 #define ETH_ALEN 6
278 EOF
279
280 rm -rf "$tmpdir"