[RFC 0/8] Allow using KVM UAPI headers for mixed architectures

Bjoern Walk posted 8 patches 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261109171036.3806884-1-bwalk@linux.ibm.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, David Hildenbrand <david@kernel.org>, Igor Mammedov <imammedo@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, "Michael S. Tsirkin" <mst@redhat.com>, David Woodhouse <dwmw2@infradead.org>, Paul Durrant <paul@xen.org>, Song Gao <17746591750@163.com>, Bibo Mao <maobibo@loongson.cn>, Xianglai Li <lixianglai@loongson.cn>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Matthew Rosato <mjrosato@linux.ibm.com>, Farhan Ali <alifm@linux.ibm.com>, Eric Farman <farman@linux.ibm.com>, Cornelia Huck <cohuck@redhat.com>, Ilya Leoshkevich <iii@linux.ibm.com>, Halil Pasic <pasic@linux.ibm.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, Alex Williamson <alex@shazbot.org>, "Cédric Le Goater" <clg@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>, Peter Maydell <peter.maydell@linaro.org>, Marcelo Tosatti <mtosatti@redhat.com>, Zhao Liu <zhao1.liu@intel.com>, Nicholas Piggin <npiggin@gmail.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>, Amit Machhiwal <amachhiw@linux.ibm.com>, Chinmay Rath <rathc@linux.ibm.com>, Glenn Miles <milesg@linux.ibm.com>, Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>, Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, Laurent Vivier <lvivier@redhat.com>
accel/kvm/kvm-accel-ops.c              |   1 -
accel/kvm/kvm-all.c                    |   2 +-
backends/hostmem-memfd.c               |   1 -
hw/hyperv/hyperv.c                     |   1 -
hw/hyperv/meson.build                  |   2 +-
hw/i386/kvm/clock.c                    |   1 -
hw/i386/kvm/i8254.c                    |   1 -
hw/i386/kvm/xen_evtchn.c               |   1 -
hw/i386/kvm/xen_overlay.c              |   1 -
hw/intc/loongarch_dintc_kvm.c          |   2 +-
hw/intc/loongarch_extioi_kvm.c         |   1 -
hw/s390x/s390-pci-kvm.c                |   2 +-
hw/vfio/kvm-helpers.c                  |   1 -
hw/vfio/kvm-spapr.c                    |   2 +-
include/system/kvm.h                   |  14 +
linux-headers/asm-arm64/hwcap.h        | 159 ++++++++++++
linux-headers/asm-arm64/kvm.h          |   4 +-
linux-headers/asm-arm64/ptrace.h       | 337 +++++++++++++++++++++++++
linux-headers/asm-arm64/unistd.h       |   2 +-
linux-headers/asm-loongarch/unistd.h   |   6 +-
linux-headers/asm-mips/unistd.h        |   8 +-
linux-headers/asm-powerpc/unistd.h     |   4 +-
linux-headers/asm-riscv/kvm.h          |   4 +-
linux-headers/asm-riscv/unistd.h       |   6 +-
linux-headers/asm-s390/unistd.h        |   2 +-
linux-headers/linux/kvm.h              |   5 +-
meson.build                            |   2 +-
scripts/update-linux-headers.sh        |  63 +++--
target/arm/kvm-consts.h                |   3 +-
target/arm/kvm.c                       |   2 -
target/i386/kvm/kvm.c                  |   2 +-
target/i386/kvm/kvm_i386.h             |   2 -
target/i386/sev.c                      |   1 -
target/loongarch/cpu.c                 |   3 -
target/loongarch/kvm/kvm.c             |   1 -
target/ppc/kvm.c                       |   2 -
target/riscv/kvm/kvm-cpu.c             |   2 -
target/s390x/kvm/kvm.c                 |   2 +-
target/s390x/kvm/pv.c                  |   2 -
tests/qtest/migration/framework.c      |   2 +-
tests/qtest/migration/migration-util.c |   2 +-
41 files changed, 588 insertions(+), 73 deletions(-)
create mode 100644 linux-headers/asm-arm64/hwcap.h
create mode 100644 linux-headers/asm-arm64/ptrace.h
[RFC 0/8] Allow using KVM UAPI headers for mixed architectures
Posted by Bjoern Walk 1 week, 6 days ago
Recently, there has been made efforts in the Linux kernel [to bring
arm64 KVM to the s390 platform][1]. In part, this is done by sharing the
ARM kernel headers with s390 code. Similarly, it is expected that any
userspace application on S390 will pull in the ARM KVM UAPI headers to
get access to ARM-related API definitions.

[1]: https://lore.kernel.org/all/20261014092343.715112-C-seiden@linux.ibm.com

QEMU, as of now, has one implicit assumption when building KVM-enabled
code: the host architecture is equal to the guest (target) architecture.
This is enforced by symlinking the vendored kernel headers in
linux-headers to the corresponding architecture of the host CPU at
configure-time. The KVM API is included via <linux/kvm.h> which in turn
pulls in architecture-specific definitions from <asm/kvm.h> from the
forementioned linked directory.

This approach does not work in a mixed-architecture build, i.e. building
KVM-accelerated ARM CPU virtualisation on an s390 host.

This RFC suggests one possible solution by trying to split off the
architecture-specific definitions in <asm/kvm.h> from the generic
definitions in <linux/kvm.h> and allowing QEMU code directly to include
architecture-specific definitions based on the compiled target. For
common code, QEMU stubs the architecture-specific definitions in the KVM
API and poisons them for direct use.

This requires some rearranging of header files. This is peformed by a
series of steps:

0. The first two patches do some housekeeping that arose as a fallout
   from the rest of the patches.

1. We declare the already existing include file "system/kvm.h" for the
   preferred entry point to the KVM API, allowing us to consolidate the
   architecture-specific handling at a single origin.

2. We need to adjust the vendored kernel headers for KVM. One part is
   that we need to split off the architecture-specific defintions from
   generic definitions. We can do this by simply removing the include of
   <asm/kvm.h> in <linux/kvm.h> and require QEMU source code to include
   the relevant <asm/kvm.h> manually.
   The other part is to adjust include paths of transient dependencies
   to refer explicitly to the architecture they do belong to. For
   example, on ARM, <asm/linux.h> includes <asm/ptrace.h>. We need to
   change those includes to refer to "asm-arm64/ptrace.h". This is
   unproblematic, since we should never expect that individual kernel
   files are mixed within architectures.

3. Now we can extent "system/kvm.h" with the logic required to pull in
   the parts of the KVM API that is needed in the source code. For
   target-specific code which requires definitions from the
   architecture-specific KVM API, we make this selection based on the
   TARGET_* macros. For common code which should only ever required
   generic definitions from the KVM API, we stub the
   architecture-specific definitions and just include the stripped
   <linux/kvm.h>.

This allows for example to build a native qemu-system-arm64 binary on an
s390 host with ARM KVM acceleration as demonstrated by the last patch
(which is not considered part of this RFC).

Tree: https://github.ibm.com/linuxonz/qemu/tree/canton/kvm-uapi-header-changes/rfc

Bjoern Walk (8):
  hw/hyperv: move hyperv.c to specific_ss
  include: add header dependencies explicitly
  Prefer including "system/kvm.h" over <linux/kvm.h>
  scripts/update-linux-headers: Post-process KVM UAPI kernel headers
  scripts/update-linux-headers: Vendor hwcap.h and ptrace.h for arm64
  linux-headers: Update to post-processed KVM headers
  kvm: Adjust system/kvm.h for updated KVM kernel headers
  include: Explicitly include ARM-specific KVM UAPI headers

Nina Schoetterl-Glausch (1):
  meson: enable aarch64 KVM target on s390x host

 accel/kvm/kvm-accel-ops.c              |   1 -
 accel/kvm/kvm-all.c                    |   2 +-
 backends/hostmem-memfd.c               |   1 -
 hw/hyperv/hyperv.c                     |   1 -
 hw/hyperv/meson.build                  |   2 +-
 hw/i386/kvm/clock.c                    |   1 -
 hw/i386/kvm/i8254.c                    |   1 -
 hw/i386/kvm/xen_evtchn.c               |   1 -
 hw/i386/kvm/xen_overlay.c              |   1 -
 hw/intc/loongarch_dintc_kvm.c          |   2 +-
 hw/intc/loongarch_extioi_kvm.c         |   1 -
 hw/s390x/s390-pci-kvm.c                |   2 +-
 hw/vfio/kvm-helpers.c                  |   1 -
 hw/vfio/kvm-spapr.c                    |   2 +-
 include/system/kvm.h                   |  14 +
 linux-headers/asm-arm64/hwcap.h        | 159 ++++++++++++
 linux-headers/asm-arm64/kvm.h          |   4 +-
 linux-headers/asm-arm64/ptrace.h       | 337 +++++++++++++++++++++++++
 linux-headers/asm-arm64/unistd.h       |   2 +-
 linux-headers/asm-loongarch/unistd.h   |   6 +-
 linux-headers/asm-mips/unistd.h        |   8 +-
 linux-headers/asm-powerpc/unistd.h     |   4 +-
 linux-headers/asm-riscv/kvm.h          |   4 +-
 linux-headers/asm-riscv/unistd.h       |   6 +-
 linux-headers/asm-s390/unistd.h        |   2 +-
 linux-headers/linux/kvm.h              |   5 +-
 meson.build                            |   2 +-
 scripts/update-linux-headers.sh        |  63 +++--
 target/arm/kvm-consts.h                |   3 +-
 target/arm/kvm.c                       |   2 -
 target/i386/kvm/kvm.c                  |   2 +-
 target/i386/kvm/kvm_i386.h             |   2 -
 target/i386/sev.c                      |   1 -
 target/loongarch/cpu.c                 |   3 -
 target/loongarch/kvm/kvm.c             |   1 -
 target/ppc/kvm.c                       |   2 -
 target/riscv/kvm/kvm-cpu.c             |   2 -
 target/s390x/kvm/kvm.c                 |   2 +-
 target/s390x/kvm/pv.c                  |   2 -
 tests/qtest/migration/framework.c      |   2 +-
 tests/qtest/migration/migration-util.c |   2 +-
 41 files changed, 588 insertions(+), 73 deletions(-)
 create mode 100644 linux-headers/asm-arm64/hwcap.h
 create mode 100644 linux-headers/asm-arm64/ptrace.h

-- 
2.56.0
Re: [RFC 0/8] Allow using KVM UAPI headers for mixed architectures
Posted by Bjoern Walk 1 week, 6 days ago
Bjoern Walk <bwalk@linux.ibm.com> [2026-11-09, 06:10PM +0100]:
> Tree: https://github.ibm.com/linuxonz/qemu/tree/canton/kvm-uapi-header-changes/rfc

Sorry, wrong link. Here's the correct one:
https://gitlab.com/bwalk/qemu/-/tree/canton/kvm-uapi-header-changes/rfc