update-linux-headers.sh 11 KB

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