update-linux-headers.sh 9.1 KB

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