update-linux-headers.sh 7.9 KB

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