You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

update-linux-headers.sh 9.1KB

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