[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 4/5] tests: use unit test fragment in vPCI test


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: dmukhin@xxxxxxxx
  • Date: Thu, 12 Feb 2026 18:49:51 -0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 205.220.161.53) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=ford.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ford.com; dkim=pass (signature was verified) header.d=saarlouis.ford.com; dkim=pass (signature was verified) header.d=ford.com; arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cr34swkzcPcEXFdzaCMCfFmOvkyK/MxDYdRkiiZHB64=; b=f2jaJfKT5WkIOCJqGY12u8c+7tOnSLnz9Xm6wBMwoHu+qhBdJDEAaZ5cMvi0a+uYVGr9qii7tQX5qKDrkqZmPpLDnE3MkYckQUBP3xy8phe24FY4xSXczP/DwqPZn1BlASMDN6F2AEhxtpB8lB9GVnGdyAnd9BbF6CKR+sYY2P+OZjKTe64Bb9FbbT8IofONgdSMSCnAL2R8T/VtyYLILC6ba8oYAr7d76lQtRU/G995+CXWrIV2mF7+GvCNDoTVSYBO2oH2rUupv4RzVGAqYyEIsZM4MJi+pbkUSVVOxWuOx1YY6DfUEy8/U27OMLprLyV0YrRqPaJf2F3djXrurg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WcJvPb7UUCQyQxCSk4Fshr7AZB1UJC6TIfZzFGuCOSTZinuthgVLtI5HRPU9ImG2uzQegKpQ8O9QbTHNLX5FWqXAmUNDiSSIanSPJBP0fgnhHEzMIF5QnQ8B1poDILsX7is2EEqH9Bk4FNWWXH7Mjc+QeayVUhOXLWwBRX0h26rGPx1cYxJD9PU1tgoMF0dJcMkSweISAO1gn38Ul/PiE590AsRdRTr4NAanYrQJYxa0S3mxHeJtmCGIhUwga2M6ro/xXUJpmJJJu4EXRz7APgPfTVTZWR59OSadI1LJzRV+iHcYDObmV8TEZPcal93UyH0ce7bMrigKsIQAHVWN7w==
  • Cc: andrew.cooper3@xxxxxxxxxx, anthony.perard@xxxxxxxxxx, jbeulich@xxxxxxxx, julien@xxxxxxx, michal.orzel@xxxxxxx, roger.pau@xxxxxxxxxx, sstabellini@xxxxxxxxxx, dmukhin@xxxxxxxx
  • Delivery-date: Fri, 13 Feb 2026 02:50:31 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Pser-m365-app: SER-APP

From: Denis Mukhin <dmukhin@xxxxxxxx> 

Use the new make fragment to generate test harness code for the vPCI unit
test.

Add new prepare-harness target to tests/Rules.mk as an optional step for
setting up mocked environment for building a test.

Add ability to override the test harness header file.

Fix hypervisor headers used to compile vpci.c against host environment
where necessary.

Fixup emul.h by adding missing mocks to account for new unit test infra.

Update .gitignore to exclude generated test build-time dependencies.

Signed-off-by: Denis Mukhin <dmukhin@xxxxxxxx>
---
Changes since v2:
- moved hypervisor headers changes to a separate patch
---
 tools/tests/Rules.mk        |  8 ++++--
 tools/tests/vpci/.gitignore |  2 ++
 tools/tests/vpci/Makefile   | 52 ++++++++++++-------------------------
 tools/tests/vpci/emul.h     | 50 +++++++++++++----------------------
 tools/tests/vpci/main.c     |  2 --
 5 files changed, 42 insertions(+), 72 deletions(-)
 create mode 100644 tools/tests/vpci/.gitignore

diff --git a/tools/tests/Rules.mk b/tools/tests/Rules.mk
index 8c4881e35da1..e8a9e82320cf 100644
--- a/tools/tests/Rules.mk
+++ b/tools/tests/Rules.mk
@@ -10,13 +10,16 @@ $(shell sed -n \
     's/^[ \t]*# *include[ \t]*[<"]\([^">]*\)[">].*/\1/p' $(1) 2>/dev/null)
 endef
 
+.PHONY: prepare-harness
+prepare-harness:
+
 # Generate mock environment by replicating header file hierarchy;
 # each mock header file will point to a harness header.
 #
 # $1 Hypervisor header.
 # $2 Test harness header.
 define emit-harness-nested-rule
-$(1): $(2)
+$(1): prepare-harness $(2)
        set -e; \
        mkdir -p $$(@D); \
        [ -e $$@ ] || ln -s $(2) $$@
@@ -44,10 +47,11 @@ endef
 #
 # $1 Hypervisor filename.
 # $2 Hypervisor source path.
+# $3 Harness header filename (optional).
 define vpath-with-harness-deps
 vpath $(1) $(2)
 $(call emit-harness-deps,$(addprefix $(2),$(1)),\
-                         $(CURDIR)/harness.h)
+                         $(strip $(or $(3),$(CURDIR)/harness.h)))
 endef
 
 .PHONY: all
diff --git a/tools/tests/vpci/.gitignore b/tools/tests/vpci/.gitignore
new file mode 100644
index 000000000000..d66c2cd56be6
--- /dev/null
+++ b/tools/tests/vpci/.gitignore
@@ -0,0 +1,2 @@
+/generated
+test-vpci
diff --git a/tools/tests/vpci/Makefile b/tools/tests/vpci/Makefile
index 97359ff67f86..a885ce41b73e 100644
--- a/tools/tests/vpci/Makefile
+++ b/tools/tests/vpci/Makefile
@@ -1,43 +1,23 @@
-XEN_ROOT=$(CURDIR)/../../..
-include $(XEN_ROOT)/tools/Rules.mk
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Unit tests for vPCI.
+#
 
-TARGET := test_vpci
+TESTS := test-vpci
 
-.PHONY: all
-all: $(TARGET)
+XEN_ROOT = $(CURDIR)/../../..
+CFLAGS += -DCONFIG_HAS_VPCI
 
-.PHONY: run
-run: $(TARGET)
-ifeq ($(CC),$(HOSTCC))
-       ./$(TARGET)
-else
-       $(warning HOSTCC != CC, will not run test)
-endif
+include $(XEN_ROOT)/tools/tests/Rules.mk
 
-$(TARGET): vpci.c vpci.h list.h main.c emul.h
-       $(CC) $(CFLAGS_xeninclude) -g -o $@ vpci.c main.c
+# Do not mock xen/vpci.h header for the test
+prepare-harness:
+       set -e; mkdir -p $(CURDIR)/generated/xen; \
+       ln -sf $(XEN_ROOT)/xen/include/xen/vpci.h $(CURDIR)/generated/xen
 
-.PHONY: clean
-clean:
-       rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h
+CFLAGS += -I $(XEN_ROOT)/xen/include/
 
-.PHONY: distclean
-distclean: clean
+$(eval $(call 
vpath-with-harness-deps,vpci.c,$(XEN_ROOT)/xen/drivers/vpci/,$(CURDIR)/emul.h))
 
-.PHONY: install
-install: all
-       $(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests
-       $(INSTALL_PROG) $(TARGET) $(DESTDIR)$(LIBEXEC)/tests
-
-.PHONY: uninstall
-uninstall:
-       $(RM) -- $(DESTDIR)$(LIBEXEC)/tests/$(TARGET)
-
-vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c
-       # Remove includes and add the test harness header
-       sed -e '/#include/d' -e '1s/^/#include "emul.h"/' <$< >$@
-
-list.h: $(XEN_ROOT)/xen/include/xen/list.h
-vpci.h: $(XEN_ROOT)/xen/include/xen/vpci.h
-list.h vpci.h:
-       sed -e '/#include/d' <$< >$@
+test-vpci: vpci.o main.o
+       $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
diff --git a/tools/tests/vpci/emul.h b/tools/tests/vpci/emul.h
index dd048cffbf9d..979e86e2692e 100644
--- a/tools/tests/vpci/emul.h
+++ b/tools/tests/vpci/emul.h
@@ -34,8 +34,16 @@
 #define ASSERT(x) assert(x)
 #define __must_check __attribute__((__warn_unused_result__))
 #define cf_check
+#define always_inline inline
 
-#include "list.h"
+typedef int64_t s_time_t;
+typedef uint8_t nodeid_t;
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+#include <xen/list.h>
 
 typedef bool rwlock_t;
 
@@ -43,10 +51,6 @@ struct domain {
     rwlock_t pci_lock;
 };
 
-struct pci_dev {
-    struct vpci *vpci;
-};
-
 struct vcpu
 {
     struct domain *domain;
@@ -57,35 +61,17 @@ extern const struct pci_dev test_pdev;
 
 typedef bool spinlock_t;
 #define spin_lock_init(l) (*(l) = false)
-#define spin_lock(l) (*(l) = true)
-#define spin_unlock(l) (*(l) = false)
-#define read_lock(l) (*(l) = true)
-#define read_unlock(l) (*(l) = false)
-#define write_lock(l) (*(l) = true)
-#define write_unlock(l) (*(l) = false)
+#define spin_lock(l) (assert(!*(l)), *(l) = true)
+#define spin_unlock(l) (assert(*(l)), *(l) = false)
+#define read_lock(l) (assert(!*(l)), *(l) = true)
+#define read_unlock(l) (assert(*(l)), *(l) = false)
+#define write_lock(l) (assert(!*(l)), *(l) = true)
+#define write_unlock(l) (assert(*(l)), *(l) = false)
 
-typedef union {
-    uint32_t sbdf;
-    struct {
-        union {
-            uint16_t bdf;
-            struct {
-                union {
-                    struct {
-                        uint8_t func : 3,
-                                dev  : 5;
-                    };
-                    uint8_t     extfunc;
-                };
-                uint8_t         bus;
-            };
-        };
-        uint16_t                seg;
-    };
-} pci_sbdf_t;
+#define lock_evaluate_nospec(l) (l)
+#define block_lock_speculation()
 
-#define CONFIG_HAS_VPCI
-#include "vpci.h"
+#include <xen/vpci.h>
 
 #define __hwdom_init
 
diff --git a/tools/tests/vpci/main.c b/tools/tests/vpci/main.c
index 2ef8d4e03f1d..3753417e866d 100644
--- a/tools/tests/vpci/main.c
+++ b/tools/tests/vpci/main.c
@@ -189,8 +189,6 @@ main(int argc, char **argv)
     uint32_t r24 = 0;
     uint8_t r28, r30;
     struct mask_data r32;
-    unsigned int i;
-    int rc;
 
     INIT_LIST_HEAD(&vpci.handlers);
     spin_lock_init(&vpci.lock);
-- 
2.52.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.