update-linux-headers.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 mman.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. cp "$tmpdir/include/asm/unistd_o32.h" "$output/linux-headers/asm-mips/"
  93. cp "$tmpdir/include/asm/unistd_n32.h" "$output/linux-headers/asm-mips/"
  94. cp "$tmpdir/include/asm/unistd_n64.h" "$output/linux-headers/asm-mips/"
  95. fi
  96. if [ $arch = powerpc ]; then
  97. cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-powerpc/"
  98. cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-powerpc/"
  99. fi
  100. rm -rf "$output/include/standard-headers/asm-$arch"
  101. mkdir -p "$output/include/standard-headers/asm-$arch"
  102. if [ $arch = s390 ]; then
  103. cp_portable "$tmpdir/include/asm/virtio-ccw.h" "$output/include/standard-headers/asm-s390/"
  104. cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-s390/"
  105. cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-s390/"
  106. fi
  107. if [ $arch = arm ]; then
  108. cp "$tmpdir/include/asm/unistd-eabi.h" "$output/linux-headers/asm-arm/"
  109. cp "$tmpdir/include/asm/unistd-oabi.h" "$output/linux-headers/asm-arm/"
  110. cp "$tmpdir/include/asm/unistd-common.h" "$output/linux-headers/asm-arm/"
  111. fi
  112. if [ $arch = x86 ]; then
  113. cp "$tmpdir/include/asm/unistd_32.h" "$output/linux-headers/asm-x86/"
  114. cp "$tmpdir/include/asm/unistd_x32.h" "$output/linux-headers/asm-x86/"
  115. cp "$tmpdir/include/asm/unistd_64.h" "$output/linux-headers/asm-x86/"
  116. cp_portable "$tmpdir/include/asm/kvm_para.h" "$output/include/standard-headers/asm-$arch"
  117. # Remove everything except the macros from bootparam.h avoiding the
  118. # unnecessary import of several video/ist/etc headers
  119. sed -e '/__ASSEMBLY__/,/__ASSEMBLY__/d' \
  120. "$tmpdir/include/asm/bootparam.h" > "$tmpdir/bootparam.h"
  121. cp_portable "$tmpdir/bootparam.h" \
  122. "$output/include/standard-headers/asm-$arch"
  123. fi
  124. done
  125. rm -rf "$output/linux-headers/linux"
  126. mkdir -p "$output/linux-headers/linux"
  127. for header in kvm.h vfio.h vfio_ccw.h vhost.h \
  128. psci.h psp-sev.h userfaultfd.h mman.h; do
  129. cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux"
  130. done
  131. rm -rf "$output/linux-headers/asm-generic"
  132. mkdir -p "$output/linux-headers/asm-generic"
  133. for header in unistd.h bitsperlong.h mman-common.h mman.h hugetlb_encode.h; do
  134. cp "$tmpdir/include/asm-generic/$header" "$output/linux-headers/asm-generic"
  135. done
  136. if [ -L "$linux/source" ]; then
  137. cp "$linux/source/COPYING" "$output/linux-headers"
  138. else
  139. cp "$linux/COPYING" "$output/linux-headers"
  140. fi
  141. # Recent kernel sources split the copyright/license info into multiple
  142. # files, which we need to copy. This set of licenses is the set that
  143. # are referred to by SPDX lines in the headers we currently copy.
  144. # We don't copy the Documentation/process/license-rules.rst which
  145. # is also referred to by COPYING, since it's explanatory rather than license.
  146. if [ -d "$linux/LICENSES" ]; then
  147. mkdir -p "$output/linux-headers/LICENSES/preferred" \
  148. "$output/linux-headers/LICENSES/exceptions"
  149. for l in preferred/GPL-2.0 preferred/BSD-2-Clause preferred/BSD-3-Clause \
  150. exceptions/Linux-syscall-note; do
  151. cp "$linux/LICENSES/$l" "$output/linux-headers/LICENSES/$l"
  152. done
  153. fi
  154. cat <<EOF >$output/linux-headers/linux/virtio_config.h
  155. #include "standard-headers/linux/virtio_config.h"
  156. EOF
  157. cat <<EOF >$output/linux-headers/linux/virtio_ring.h
  158. #include "standard-headers/linux/virtio_ring.h"
  159. EOF
  160. cat <<EOF >$output/linux-headers/linux/vhost_types.h
  161. #include "standard-headers/linux/vhost_types.h"
  162. EOF
  163. rm -rf "$output/include/standard-headers/linux"
  164. mkdir -p "$output/include/standard-headers/linux"
  165. for i in "$tmpdir"/include/linux/*virtio*.h \
  166. "$tmpdir/include/linux/qemu_fw_cfg.h" \
  167. "$tmpdir/include/linux/input.h" \
  168. "$tmpdir/include/linux/input-event-codes.h" \
  169. "$tmpdir/include/linux/pci_regs.h" \
  170. "$tmpdir/include/linux/ethtool.h" "$tmpdir/include/linux/kernel.h" \
  171. "$tmpdir/include/linux/vhost_types.h" \
  172. "$tmpdir/include/linux/sysinfo.h"; do
  173. cp_portable "$i" "$output/include/standard-headers/linux"
  174. done
  175. mkdir -p "$output/include/standard-headers/drm"
  176. cp_portable "$tmpdir/include/drm/drm_fourcc.h" \
  177. "$output/include/standard-headers/drm"
  178. rm -rf "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma"
  179. mkdir -p "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma"
  180. # Remove the unused functions from pvrdma_verbs.h avoiding the unnecessary
  181. # import of several infiniband/networking/other headers
  182. tmp_pvrdma_verbs="$tmpdir/pvrdma_verbs.h"
  183. # Parse the entire file instead of single lines to match
  184. # function declarations expanding over multiple lines
  185. # and strip the declarations starting with pvrdma prefix.
  186. sed -e '1h;2,$H;$!d;g' -e 's/[^};]*pvrdma[^(| ]*([^)]*);//g' \
  187. "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_verbs.h" > \
  188. "$tmp_pvrdma_verbs";
  189. for i in "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h" \
  190. "$linux/drivers/infiniband/hw/vmw_pvrdma/pvrdma_dev_api.h" \
  191. "$tmp_pvrdma_verbs"; do \
  192. cp_portable "$i" \
  193. "$output/include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/"
  194. done
  195. rm -rf "$output/include/standard-headers/rdma/"
  196. mkdir -p "$output/include/standard-headers/rdma/"
  197. for i in "$tmpdir/include/rdma/vmw_pvrdma-abi.h"; do
  198. cp_portable "$i" \
  199. "$output/include/standard-headers/rdma/"
  200. done
  201. cat <<EOF >$output/include/standard-headers/linux/types.h
  202. /* For QEMU all types are already defined via osdep.h, so this
  203. * header does not need to do anything.
  204. */
  205. EOF
  206. cat <<EOF >$output/include/standard-headers/linux/if_ether.h
  207. #define ETH_ALEN 6
  208. EOF
  209. rm -rf "$tmpdir"