Skip to contentSkip to conversation

arch-specific libraries

PATCH In review Jan Beulich 4 participants 8 messages Dec 8, 2025 – Dec 11, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover arch-specific libraries

1: x86: move arch_generic_hweightl() to arch-specific library 2: x86: move / split usercopy.c to / into arch-specific library 3: Arm: actually put library code in a library

Jan

#1 lore raw

PATCH v2 1/3 x86: move arch_generic_hweightl() to arch-specific library

Introduce arch/x86/lib/, and make it the home for the somewhat misplaced x86-specific file that lived in the arch-independent lib/.

Introduce ARCH_LIBS-y as a make variable, to arrange for arch-specific libraries to (generally) come ahead of generic one(s) when linking. Should any library be intended to come after the generic one(s), it can be appended to $(ALL_LIBS-y).

---
v2: Arrange to link arch-specific lib ahead of generic one.

---
 xen/Makefile                                                  | 4 +++-
 xen/arch/x86/Makefile                                         | 1 +
 xen/arch/x86/arch.mk                                          | 2 ++
 xen/arch/x86/lib/Makefile                                     | 1 +
 .../x86/lib/generic-hweightl.c}                               | 0
 xen/lib/Makefile                                              | 1 -
 6 files changed, 7 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/x86/lib/Makefile
 rename xen/{lib/x86-generic-hweightl.c => arch/x86/lib/generic-hweightl.c} (100%)
xen/Makefile +3 -1
index e6cf2874251c..13e336ba5484 100644
@@ -461,6 +461,7 @@ ALL_OBJS-y                += xsm/built_in.o
 ALL_OBJS-y                += arch/$(SRCARCH)/built_in.o
 ALL_OBJS-$(CONFIG_CRYPTO) += crypto/built_in.o
 
+ARCH_LIBS-y               :=
 ALL_LIBS-y                := lib/lib.a
 
 all-symbols-y :=
@@ -620,7 +621,8 @@ $(TARGET): outputmakefile asm-generic FORCE
 	$(Q)$(MAKE) $(build)=arch/$(SRCARCH) include
 	$(Q)$(MAKE) $(build)=. arch/$(SRCARCH)/include/asm/asm-offsets.h
 	$(Q)$(MAKE) $(build)=. MKRELOC=$(MKRELOC) 'ALL_OBJS=$(ALL_OBJS-y)' \
-	            'ALL_LIBS=$(ALL_LIBS-y)' 'all_symbols=$(all-symbols-y)' $@
+	            'ALL_LIBS=$(ARCH_LIBS-y) $(ALL_LIBS-y)' \
+	            'all_symbols=$(all-symbols-y)' $@
 
 SUBDIRS = xsm arch common crypto drivers lib test
 define all_sources
xen/arch/x86/Makefile +1 -0
index 300cc67407e9..61e2293a467e 100644
@@ -5,6 +5,7 @@ obj-y += efi/
 obj-y += genapic/
 obj-$(CONFIG_GUEST) += guest/
 obj-$(CONFIG_HVM) += hvm/
+obj-y += lib/
 obj-y += mm/
 obj-$(CONFIG_XENOPROF) += oprofile/
 obj-$(CONFIG_PV) += pv/
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..0203138a819a 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ARCH_LIBS-y += arch/x86/lib/lib.a
+
 CFLAGS += -DXEN_IMG_OFFSET=$(XEN_IMG_OFFSET)
 
 # Prevent floating-point variables from creeping into Xen.
xen/arch/x86/lib/Makefile new file +1 -0
new file mode 100644
index 000000000000..ddf7e19bdc1d
@@ -0,0 +1 @@
+lib-y += generic-hweightl.o
xen/arch/x86/lib/generic-hweightl.c renamed from xen/lib/x86-generic-hweightl.c +0 -0
similarity index 100%
rename from xen/lib/x86-generic-hweightl.c
rename to xen/arch/x86/lib/generic-hweightl.c
xen/lib/Makefile +0 -1
index 954d9216a39c..efca830d924c 100644
@@ -39,7 +39,6 @@ lib-y += strtol.o
 lib-y += strtoll.o
 lib-y += strtoul.o
 lib-y += strtoull.o
-lib-$(CONFIG_X86) += x86-generic-hweightl.o
 lib-$(CONFIG_X86) += xxhash32.o
 lib-$(CONFIG_X86) += xxhash64.o
 
Acked-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] x86: move arch_generic_hweightl() to arch-specific library

Quoted 9 lines · Jan Beulich

Introduce arch/x86/lib/, and make it the home for the somewhat misplaced x86-specific file that lived in the arch-independent lib/.

Introduce ARCH_LIBS-y as a make variable, to arrange for arch-specific libraries to (generally) come ahead of generic one(s) when linking. Should any library be intended to come after the generic one(s), it can be appended to $(ALL_LIBS-y).

#3 lore raw

PATCH v2 2/3 x86: move / split usercopy.c to / into arch-specific library

In reply to Jan Beulich

The file wasn't correctly named for our purposes anyway. Split it into its "guest" and "unsafe" parts, thus allowing the latter to not be linked in at all (for presently having no caller). The building of the "guest" part can then (later) become conditional upon PV=y.

---
 xen/arch/x86/Makefile                         |  4 ----
 xen/arch/x86/lib/Makefile                     |  2 ++
 xen/arch/x86/{usercopy.c => lib/copy-guest.c} | 12 +---------
 xen/arch/x86/lib/copy-unsafe.c                | 22 +++++++++++++++++++
 4 files changed, 25 insertions(+), 15 deletions(-)
 rename xen/arch/x86/{usercopy.c => lib/copy-guest.c} (94%)
 create mode 100644 xen/arch/x86/lib/copy-unsafe.c
xen/arch/x86/Makefile +0 -4
index 61e2293a467e..dfb258d7ac1c 100644
@@ -72,7 +72,6 @@ obj-y += time.o
 obj-y += traps-setup.o
 obj-y += traps.o
 obj-$(CONFIG_INTEL) += tsx.o
-obj-y += usercopy.o
 obj-y += x86_emulate.o
 obj-$(CONFIG_TBOOT) += tboot.o
 obj-y += hpet.o
@@ -93,9 +92,6 @@ hostprogs-y += efi/mkreloc
 
 $(obj)/efi/mkreloc: HOSTCFLAGS += -I$(srctree)/include
 
-# Allows usercopy.c to include itself
-$(obj)/usercopy.o: CFLAGS-y += -iquote .
-
 ifneq ($(CONFIG_HVM),y)
 $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
 endif
xen/arch/x86/lib/Makefile +2 -0
index ddf7e19bdc1d..8fe2dfd88553 100644
@@ -1 +1,3 @@
+lib-y += copy-guest.o
+lib-y += copy-unsafe.o
 lib-y += generic-hweightl.o
xen/arch/x86/lib/copy-guest.c renamed from xen/arch/x86/usercopy.c +1 -11
similarity index 94%
rename from xen/arch/x86/usercopy.c
rename to xen/arch/x86/lib/copy-guest.c
index a24b52cc66c1..25eeb35427e2 100644
@@ -1,4 +1,4 @@
-/* 
+/*
  * User address space access functions.
  *
  * Copyright 1997 Andi Kleen <[email protected]>
@@ -6,8 +6,6 @@
  * Copyright 2002 Andi Kleen <[email protected]>
  */
 
-#include <xen/lib.h>
-#include <xen/sched.h>
 #include <asm/uaccess.h>
 
 #ifndef GUARD
@@ -139,14 +137,6 @@ unsigned int copy_from_guest_pv(void *to, const void __user *from,
     return n;
 }
 
-# undef GUARD
-# define GUARD UA_DROP
-# define copy_to_guest_ll copy_to_unsafe_ll
-# define copy_from_guest_ll copy_from_unsafe_ll
-# undef __user
-# define __user
-# include __FILE__
-
 #endif /* GUARD(1) */
 
 /*
xen/arch/x86/lib/copy-unsafe.c new file +22 -0
new file mode 100644
index 000000000000..a51500370fb1
@@ -0,0 +1,22 @@
+/*
+ * "Unsafe" access functions.
+ */
+
+#include <asm/uaccess.h>
+
+#define GUARD UA_DROP
+#define copy_to_guest_ll copy_to_unsafe_ll
+#define copy_from_guest_ll copy_from_unsafe_ll
+#undef __user
+#define __user
+#include "copy-guest.c"
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
#4 lore raw

Re: [PATCH v2 2/3] x86: move / split usercopy.c to / into arch-specific library

Quoted 59 lines · Jan Beulich

The file wasn't correctly named for our purposes anyway. Split it into its "guest" and "unsafe" parts, thus allowing the latter to not be linked in at all (for presently having no caller). The building of the "guest" part can then (later) become conditional upon PV=y.

---
  xen/arch/x86/Makefile                         |  4 ----
  xen/arch/x86/lib/Makefile                     |  2 ++
  xen/arch/x86/{usercopy.c => lib/copy-guest.c} | 12 +---------
  xen/arch/x86/lib/copy-unsafe.c                | 22 +++++++++++++++++++
  4 files changed, 25 insertions(+), 15 deletions(-)

rename xen/arch/x86/{usercopy.c => lib/copy-guest.c} (94%) create mode 100644 xen/arch/x86/lib/copy-unsafe.c

xen/arch/x86/Makefile +0 -4
index 61e2293a467e..dfb258d7ac1c 100644
@@ -72,7 +72,6 @@ obj-y += time.o
  obj-y += traps-setup.o
  obj-y += traps.o
  obj-$(CONFIG_INTEL) += tsx.o
-obj-y += usercopy.o
  obj-y += x86_emulate.o
  obj-$(CONFIG_TBOOT) += tboot.o
  obj-y += hpet.o
@@ -93,9 +92,6 @@ hostprogs-y += efi/mkreloc
 
  $(obj)/efi/mkreloc: HOSTCFLAGS += -I$(srctree)/include
 
-# Allows usercopy.c to include itself
-$(obj)/usercopy.o: CFLAGS-y += -iquote .
-
  ifneq ($(CONFIG_HVM),y)
  $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
  endif
xen/arch/x86/lib/Makefile +2 -0
index ddf7e19bdc1d..8fe2dfd88553 100644
@@ -1 +1,3 @@
+lib-y += copy-guest.o
+lib-y += copy-unsafe.o
  lib-y += generic-hweightl.o
xen/arch/x86/lib/copy-guest.c renamed from xen/arch/x86/usercopy.c +1 -1
similarity index 94%
rename from xen/arch/x86/usercopy.c
rename to xen/arch/x86/lib/copy-guest.c
index a24b52cc66c1..25eeb35427e2 100644
@@ -1,4 +1,4 @@
-/*
+/*
   * User address space access functions.
   *
   * Copyright 1997 Andi Kleen <[email protected]>

xen/arch/x86/lib/copy-guest.c line 10

Quoted 5 lines
xen/arch/x86/lib/copy-guest.c renamed from xen/arch/x86/usercopy.c +0 -2
@@ -6,8 +6,6 @@
   * Copyright 2002 Andi Kleen <[email protected]>
   */
 
-#include <xen/lib.h>
-#include <xen/sched.h>

This will not build :(

CC arch/x86/lib/copy-guest.o
In file included from ././include/xen/config.h:27,
from <command-line>:
arch/x86/lib/copy-guest.c: In function ‘copy_to_guest_pv’:
./arch/x86/include/asm/config.h:270:32: error: invalid use of undefined type ‘struct vcpu’
270 | (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT))

Quoted 47 lines
xen/arch/x86/lib/copy-guest.c renamed from xen/arch/x86/usercopy.c +0 -8
  #include <asm/uaccess.h>
 
  #ifndef GUARD
@@ -139,14 +137,6 @@ unsigned int copy_from_guest_pv(void *to, const void __user *from,
      return n;
  }
 
-# undef GUARD
-# define GUARD UA_DROP
-# define copy_to_guest_ll copy_to_unsafe_ll
-# define copy_from_guest_ll copy_from_unsafe_ll
-# undef __user
-# define __user
-# include __FILE__
-
  #endif /* GUARD(1) */
 
  /*
xen/arch/x86/lib/copy-unsafe.c new file +22 -0
new file mode 100644
index 000000000000..a51500370fb1
@@ -0,0 +1,22 @@
+/*
+ * "Unsafe" access functions.
+ */
+
+#include <asm/uaccess.h>
+
+#define GUARD UA_DROP
+#define copy_to_guest_ll copy_to_unsafe_ll
+#define copy_from_guest_ll copy_from_unsafe_ll
+#undef __user
+#define __user
+#include "copy-guest.c"
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 2 lines
Best regards,
-grygorii
#5 lore raw

Re: [PATCH v2 2/3] x86: move / split usercopy.c to / into arch-specific library

Quoted 70 lines · Grygorii Strashko
Quoted 59 lines · Jan Beulich

The file wasn't correctly named for our purposes anyway. Split it into its "guest" and "unsafe" parts, thus allowing the latter to not be linked in at all (for presently having no caller). The building of the "guest" part can then (later) become conditional upon PV=y.

---
  xen/arch/x86/Makefile                         |  4 ----
  xen/arch/x86/lib/Makefile                     |  2 ++
  xen/arch/x86/{usercopy.c => lib/copy-guest.c} | 12 +---------
  xen/arch/x86/lib/copy-unsafe.c                | 22 +++++++++++++++++++
  4 files changed, 25 insertions(+), 15 deletions(-)

rename xen/arch/x86/{usercopy.c => lib/copy-guest.c} (94%) create mode 100644 xen/arch/x86/lib/copy-unsafe.c

xen/arch/x86/Makefile +0 -4
index 61e2293a467e..dfb258d7ac1c 100644
@@ -72,7 +72,6 @@ obj-y += time.o
  obj-y += traps-setup.o
  obj-y += traps.o
  obj-$(CONFIG_INTEL) += tsx.o
-obj-y += usercopy.o
  obj-y += x86_emulate.o
  obj-$(CONFIG_TBOOT) += tboot.o
  obj-y += hpet.o
@@ -93,9 +92,6 @@ hostprogs-y += efi/mkreloc
 
  $(obj)/efi/mkreloc: HOSTCFLAGS += -I$(srctree)/include
 
-# Allows usercopy.c to include itself
-$(obj)/usercopy.o: CFLAGS-y += -iquote .
-
  ifneq ($(CONFIG_HVM),y)
  $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
  endif
xen/arch/x86/lib/Makefile +2 -0
index ddf7e19bdc1d..8fe2dfd88553 100644
@@ -1 +1,3 @@
+lib-y += copy-guest.o
+lib-y += copy-unsafe.o
  lib-y += generic-hweightl.o
xen/arch/x86/lib/copy-guest.c renamed from xen/arch/x86/usercopy.c +1 -1
similarity index 94%
rename from xen/arch/x86/usercopy.c
rename to xen/arch/x86/lib/copy-guest.c
index a24b52cc66c1..25eeb35427e2 100644
@@ -1,4 +1,4 @@
-/*
+/*
   * User address space access functions.
   *
   * Copyright 1997 Andi Kleen <[email protected]>

xen/arch/x86/lib/copy-guest.c line 10

Quoted 5 lines
xen/arch/x86/lib/copy-guest.c renamed from xen/arch/x86/usercopy.c +0 -2
@@ -6,8 +6,6 @@
   * Copyright 2002 Andi Kleen <[email protected]>
   */
 
-#include <xen/lib.h>
-#include <xen/sched.h>

This will not build :(

Quoted 7 lines

CC arch/x86/lib/copy-guest.o
In file included from ././include/xen/config.h:27,
from <command-line>:
arch/x86/lib/copy-guest.c: In function ‘copy_to_guest_pv’:
./arch/x86/include/asm/config.h:270:32: error: invalid use of undefined type ‘struct vcpu’
270 | (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT))

Hmm, yes. It does build here, but of course I have many other patches in place. The issue looks to be that for me access_ok() isn't a macro anymore, but a function.

Thanks for pointing out,
Jan

#6 lore raw

PATCH v2 3/3 Arm: actually put library code in a library

In reply to Jan Beulich

Now that the build system supports it, build the $(ARCH)/lib/ directories actually into libraries, thus permitting unreferenced objects to be omitted.

---
v2: New.

---
 xen/arch/arm/arch.mk            |  2 ++
 xen/arch/arm/arm32/lib/Makefile | 10 +++++-----
 xen/arch/arm/arm64/lib/Makefile |  8 ++++----
 3 files changed, 11 insertions(+), 9 deletions(-)
xen/arch/arm/arch.mk +2 -0
index 9c4bedfb3bd4..dea8dbd18a44 100644
@@ -1,6 +1,8 @@
 ########################################
 # arm-specific definitions
 
+ARCH_LIBS-y += arch/arm/$(ARCH)/lib/lib.a
+
 $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS))
 $(call cc-option-add,CFLAGS,CC,-Wnested-externs)
 
xen/arch/arm/arm32/lib/Makefile +5 -5
index 18326b284e3b..f5ff7f92d195 100644
@@ -1,5 +1,5 @@
-obj-y += memcpy.o memmove.o memset.o memchr.o
-obj-y += findbit.o
-obj-y += bitops.o
-obj-y += strchr.o strrchr.o
-obj-y += lib1funcs.o lshrdi3.o div64.o
+lib-y += memcpy.o memmove.o memset.o memchr.o
+lib-y += findbit.o
+lib-y += bitops.o
+lib-y += strchr.o strrchr.o
+lib-y += lib1funcs.o lshrdi3.o div64.o
xen/arch/arm/arm64/lib/Makefile +4 -4
index 66cfac435ab4..d6b231dc6193 100644
@@ -1,4 +1,4 @@
-obj-y += memcpy.o memcmp.o memmove.o memset.o memchr.o
-obj-y += clear_page.o
-obj-y += bitops.o
-obj-y += strchr.o strcmp.o strlen.o strncmp.o strnlen.o strrchr.o
+lib-y += memcpy.o memcmp.o memmove.o memset.o memchr.o
+lib-y += clear_page.o
+lib-y += bitops.o
+lib-y += strchr.o strcmp.o strlen.o strncmp.o strnlen.o strrchr.o