update-linux-headers.sh 10 KB

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