From 96536427bf1f5aeaef726a695a0f218ac2bb091c Mon Sep 17 00:00:00 2001 From: Stone749990226 <749990226@qq.com> Date: Wed, 15 Apr 2026 16:06:09 +0800 Subject: [PATCH] build: improve hvisor-tool cross-build compatibility and ivc kernel API support Keep tools compiler selection flexible: only fall back to CROSS_COMPILE gcc when CC is the default from make, so explicit CC overrides still work. Add a local virtio mmio UAPI compatibility header for environments where the cross sysroot lacks linux virtio_mmio definitions. Make demo binaries optional via BUILD_DEMOS (default off), so the main userspace hvisor binary is not blocked by optional rpmsg demo headers. Update ivc driver to include linux version checks and use the newer class_create API on Linux 6.4+, while keeping backward-compatible behavior for older kernels. --- driver/ivc_driver.c | 7 +++ tools/Makefile | 10 +++- tools/include/linux/virtio_mmio.h | 99 +++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 tools/include/linux/virtio_mmio.h diff --git a/driver/ivc_driver.c b/driver/ivc_driver.c index 189695f..7d1904b 100644 --- a/driver/ivc_driver.c +++ b/driver/ivc_driver.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "hvisor.h" @@ -216,7 +217,13 @@ static int __init ivc_init(void) { goto err1; pr_info("ivc get major id: %d\n", MAJOR(mdev_id)); +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0) + // Linux 6.4+ + ivc_class = class_create("hivc"); +#else + // old interface ivc_class = class_create(THIS_MODULE, "hivc"); +#endif if (IS_ERR(ivc_class)) { err = PTR_ERR(ivc_class); goto err1; diff --git a/tools/Makefile b/tools/Makefile index 720378e..d4d4657 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -10,6 +10,7 @@ objects ?= $(sources:.c=.o) ivc_demo_object ?= ivc_demo.o rpmsg_demo_object ?= rpmsg_demo.o hvisor_objects ?= $(filter-out $(ivc_demo_object) $(rpmsg_demo_object), $(objects)) +BUILD_DEMOS ?= n ROOT ?= # gnu or musl LIBC ?= gnu @@ -60,7 +61,9 @@ else CFLAGS += -O2 endif +ifeq ($(origin CC), default) CC := $(CROSS_COMPILE)gcc +endif ifeq ($(ARCH), arm64) ifeq ($(VIRTIO_GPU), y) @@ -76,9 +79,14 @@ else ifeq ($(ARCH), loongarch) endif endif + +ifeq ($(BUILD_DEMOS),y) +demo_targets := ivc_demo rpmsg_demo +endif + .PHONY: all clean -all: hvisor ivc_demo rpmsg_demo +all: hvisor $(demo_targets) %.d: %.c @set -e; rm -f $@; \ diff --git a/tools/include/linux/virtio_mmio.h b/tools/include/linux/virtio_mmio.h new file mode 100644 index 0000000..255c04b --- /dev/null +++ b/tools/include/linux/virtio_mmio.h @@ -0,0 +1,99 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +#ifndef _LINUX_VIRTIO_MMIO_H +#define _LINUX_VIRTIO_MMIO_H + +/* Magic value ("virt" string) - Read Only */ +#define VIRTIO_MMIO_MAGIC_VALUE 0x000 + +/* Virtio device version - Read Only */ +#define VIRTIO_MMIO_VERSION 0x004 + +/* Virtio device ID - Read Only */ +#define VIRTIO_MMIO_DEVICE_ID 0x008 + +/* Virtio vendor ID - Read Only */ +#define VIRTIO_MMIO_VENDOR_ID 0x00c + +/* Device features - Read Only */ +#define VIRTIO_MMIO_DEVICE_FEATURES 0x010 + +/* Device features selector - Write Only */ +#define VIRTIO_MMIO_DEVICE_FEATURES_SEL 0x014 + +/* Driver features - Write Only */ +#define VIRTIO_MMIO_DRIVER_FEATURES 0x020 + +/* Driver features selector - Write Only */ +#define VIRTIO_MMIO_DRIVER_FEATURES_SEL 0x024 + +#ifndef VIRTIO_MMIO_NO_LEGACY +/* Legacy guest page size - Write Only */ +#define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 +#endif + +/* Queue selector - Write Only */ +#define VIRTIO_MMIO_QUEUE_SEL 0x030 + +/* Queue size max - Read Only */ +#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 + +/* Queue size - Write Only */ +#define VIRTIO_MMIO_QUEUE_NUM 0x038 + +#ifndef VIRTIO_MMIO_NO_LEGACY +/* Legacy used ring alignment - Write Only */ +#define VIRTIO_MMIO_QUEUE_ALIGN 0x03c + +/* Legacy queue PFN - Read Write */ +#define VIRTIO_MMIO_QUEUE_PFN 0x040 +#endif + +/* Queue ready - Read Write */ +#define VIRTIO_MMIO_QUEUE_READY 0x044 + +/* Queue notifier - Write Only */ +#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 + +/* Interrupt status - Read Only */ +#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 + +/* Interrupt acknowledge - Write Only */ +#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 + +/* Device status - Read Write */ +#define VIRTIO_MMIO_STATUS 0x070 + +/* Queue descriptor table address (64-bit split) */ +#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 +#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 + +/* Queue available ring address (64-bit split) */ +#define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090 +#define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094 + +/* Queue used ring address (64-bit split) */ +#define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0 +#define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4 + +/* Shared memory selector */ +#define VIRTIO_MMIO_SHM_SEL 0x0ac + +/* Shared memory length (64-bit split) */ +#define VIRTIO_MMIO_SHM_LEN_LOW 0x0b0 +#define VIRTIO_MMIO_SHM_LEN_HIGH 0x0b4 + +/* Shared memory base address (64-bit split) */ +#define VIRTIO_MMIO_SHM_BASE_LOW 0x0b8 +#define VIRTIO_MMIO_SHM_BASE_HIGH 0x0bc + +/* Configuration atomicity value */ +#define VIRTIO_MMIO_CONFIG_GENERATION 0x0fc + +/* Per-device config space base */ +#define VIRTIO_MMIO_CONFIG 0x100 + +/* Interrupt flags */ +#define VIRTIO_MMIO_INT_VRING (1 << 0) +#define VIRTIO_MMIO_INT_CONFIG (1 << 1) + +#endif /* _LINUX_VIRTIO_MMIO_H */