update-linux-headers.sh 9.5 KB

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