Skip to contentSkip to conversation

x86: introduce lib/

PATCH Superseded Jan Beulich 2 participants 11 messages Nov 26, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86: introduce lib/

1: move arch_generic_hweightl() to arch-specific library 2: move / split usercopy.c to / into arch-specific library

Jan

#1 lore raw

PATCH 1/2 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/.

---
 xen/arch/x86/Makefile                                           | 1 +
 xen/arch/x86/arch.mk                                            | 2 ++
 xen/arch/x86/lib/Makefile                                       | 1 +
 .../x86-generic-hweightl.c => arch/x86/lib/generic-hweightl.c}  | 0
 xen/lib/Makefile                                                | 1 -
 5 files changed, 4 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/x86/lib/Makefile
 rename xen/{lib/x86-generic-hweightl.c => arch/x86/lib/generic-hweightl.c} (100%)
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..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_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 5ccb1e5241c5..38c1c7d6845c 100644
@@ -38,7 +38,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
 
#2 lore raw

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

xen/arch/x86/arch.mk line 7

Quoted 10 lines · Jan Beulich
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_LIBS-y += arch/x86/lib/lib.a
+

Oh, I'd realised it was this easy, I'd have done so straight away when adding x86's custom arch_generic_hweightl().

I assumed it was going to be more complicated getting the order of the arch specific lib correct with the generic lib.

More concretely.  Given an x86 lib, we should move things like arch/x86/memcpy.S to it.

Therefore, when we have common/lib.a and arch/lib.a, do we guarantee to have arch/lib.a with higher precedence so for matching functions the arch specific one guarantees to be taken?

Otherwise this feels like a trap waiting to happen.

~Andrew

#3 lore raw

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

Quoted 18 lines · Andrew Cooper

xen/arch/x86/arch.mk line 7

Quoted 10 lines · Jan Beulich
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_LIBS-y += arch/x86/lib/lib.a
+

Oh, I'd realised it was this easy, I'd have done so straight away when adding x86's custom arch_generic_hweightl().

I assumed it was going to be more complicated getting the order of the arch specific lib correct with the generic lib.

Quoted 6 lines

More concretely.  Given an x86 lib, we should move things like arch/x86/memcpy.S to it.

Therefore, when we have common/lib.a and arch/lib.a, do we guarantee to have arch/lib.a with higher precedence so for matching functions the arch specific one guarantees to be taken?

Not with the change above, it would need to become

ALL_LIBS-y := arch/x86/lib/lib.a $(ALL_LIBS-y)

to achieve that, requiring that ALL_LIBS-y won't change into a lazy-expansion variable. If that's okay (please confirm), I can adjust the patch.

Things would be yet easier if every arch had a lib/lib.a, as then in xen/Makefile we could simply have

ALL_LIBS-y := arch/$(SRCARCH)/lib/lib.a
ALL_LIBS-y += lib/lib.a

Alternatively we could move the setting of ALL_LIBS-y in xen/Makefile to after the arch/$(SRCARCH)/arch.mk inclusion. I'd be a little wary of that, though, as it would then be different from ALL_OBJS-y.

Jan

#4 lore raw

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

Quoted 34 lines · Jan Beulich
Quoted 17 lines · Andrew Cooper

xen/arch/x86/arch.mk line 7

Quoted 10 lines · Jan Beulich
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_LIBS-y += arch/x86/lib/lib.a
+

Oh, I'd realised it was this easy, I'd have done so straight away when adding x86's custom arch_generic_hweightl().

I assumed it was going to be more complicated getting the order of the arch specific lib correct with the generic lib.

Quoted 6 lines

More concretely.  Given an x86 lib, we should move things like arch/x86/memcpy.S to it.

Therefore, when we have common/lib.a and arch/lib.a, do we guarantee to have arch/lib.a with higher precedence so for matching functions the arch specific one guarantees to be taken?

Not with the change above, it would need to become

ALL_LIBS-y := arch/x86/lib/lib.a $(ALL_LIBS-y)

to achieve that, requiring that ALL_LIBS-y won't change into a lazy-expansion variable. If that's okay (please confirm), I can adjust the patch.

Things would be yet easier if every arch had a lib/lib.a, as then in xen/Makefile we could simply have

Quoted 6 lines

ALL_LIBS-y := arch/$(SRCARCH)/lib/lib.a
ALL_LIBS-y += lib/lib.a

Alternatively we could move the setting of ALL_LIBS-y in xen/Makefile to after the arch/$(SRCARCH)/arch.mk inclusion. I'd be a little wary of that, though, as it would then be different from ALL_OBJS-y.

I think this would be better handled by common code.

Arches are going to want a lib.a eventually.  ARM even has arch/arm/arm{32,64}/lib/ but like x86 they're just simple obj-y += at the moment.

However, arches shouldn't be forced to make an empty one simply to build.

Does this work:

ALL_LIBS-y                := $(wildcard arch/$(SRCARCH)/lib/lib.a) ALL_LIBS-y                += lib/lib.a

?  If so, I think it's the nicest option.

~Andrew

#5 lore raw

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

Quoted 48 lines · Andrew Cooper
Quoted 34 lines · Jan Beulich
Quoted 17 lines · Andrew Cooper

xen/arch/x86/arch.mk line 7

Quoted 10 lines · Jan Beulich
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_LIBS-y += arch/x86/lib/lib.a
+

Oh, I'd realised it was this easy, I'd have done so straight away when adding x86's custom arch_generic_hweightl().

I assumed it was going to be more complicated getting the order of the arch specific lib correct with the generic lib.

Quoted 6 lines

More concretely.  Given an x86 lib, we should move things like arch/x86/memcpy.S to it.

Therefore, when we have common/lib.a and arch/lib.a, do we guarantee to have arch/lib.a with higher precedence so for matching functions the arch specific one guarantees to be taken?

Not with the change above, it would need to become

ALL_LIBS-y := arch/x86/lib/lib.a $(ALL_LIBS-y)

to achieve that, requiring that ALL_LIBS-y won't change into a lazy-expansion variable. If that's okay (please confirm), I can adjust the patch.

Things would be yet easier if every arch had a lib/lib.a, as then in xen/Makefile we could simply have

Quoted 6 lines

ALL_LIBS-y := arch/$(SRCARCH)/lib/lib.a
ALL_LIBS-y += lib/lib.a

Alternatively we could move the setting of ALL_LIBS-y in xen/Makefile to after the arch/$(SRCARCH)/arch.mk inclusion. I'd be a little wary of that, though, as it would then be different from ALL_OBJS-y.

I think this would be better handled by common code.

Arches are going to want a lib.a eventually.  ARM even has arch/arm/arm{32,64}/lib/ but like x86 they're just simple obj-y += at the moment.

Quoted 8 lines

However, arches shouldn't be forced to make an empty one simply to build.

Does this work:

ALL_LIBS-y                := $(wildcard arch/$(SRCARCH)/lib/lib.a) ALL_LIBS-y                += lib/lib.a

?  If so, I think it's the nicest option.

I had thought of doing it this way initially, but on a fresh build arch/$(SRCARCH)/lib/lib.a wouldn't be there when the Makefile is read in. Whether switching ALL_LIBS-y to be a lazy-expansion variable would work I didn't try; I'd prefer not to change the kind of variable that it is.

Jan

#6 lore raw

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

Quoted 52 lines · Jan Beulich
Quoted 51 lines · Andrew Cooper
Quoted 34 lines · Jan Beulich
Quoted 17 lines · Andrew Cooper

xen/arch/x86/arch.mk line 7

Quoted 10 lines · Jan Beulich
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_LIBS-y += arch/x86/lib/lib.a
+

Oh, I'd realised it was this easy, I'd have done so straight away when adding x86's custom arch_generic_hweightl().

I assumed it was going to be more complicated getting the order of the arch specific lib correct with the generic lib.

Quoted 6 lines

More concretely.  Given an x86 lib, we should move things like arch/x86/memcpy.S to it.

Therefore, when we have common/lib.a and arch/lib.a, do we guarantee to have arch/lib.a with higher precedence so for matching functions the arch specific one guarantees to be taken?

Not with the change above, it would need to become

ALL_LIBS-y := arch/x86/lib/lib.a $(ALL_LIBS-y)

to achieve that, requiring that ALL_LIBS-y won't change into a lazy-expansion variable. If that's okay (please confirm), I can adjust the patch.

Things would be yet easier if every arch had a lib/lib.a, as then in xen/Makefile we could simply have

Quoted 6 lines

ALL_LIBS-y := arch/$(SRCARCH)/lib/lib.a
ALL_LIBS-y += lib/lib.a

Alternatively we could move the setting of ALL_LIBS-y in xen/Makefile to after the arch/$(SRCARCH)/arch.mk inclusion. I'd be a little wary of that, though, as it would then be different from ALL_OBJS-y.

I think this would be better handled by common code.

Arches are going to want a lib.a eventually.  ARM even has arch/arm/arm{32,64}/lib/ but like x86 they're just simple obj-y += at the moment.

However, arches shouldn't be forced to make an empty one simply to build.

Does this work:

Quoted 8 lines
Quoted 4 lines

ALL_LIBS-y                := $(wildcard arch/$(SRCARCH)/lib/lib.a) ALL_LIBS-y                += lib/lib.a

?  If so, I think it's the nicest option.

I had thought of doing it this way initially, but on a fresh build arch/$(SRCARCH)/lib/lib.a wouldn't be there when the Makefile is read in. Whether switching ALL_LIBS-y to be a lazy-expansion variable would work I didn't try; I'd prefer not to change the kind of variable that it is.

Hmm.  What about:

    $(filter arch%,$(ALL_LIBS-y)) $(filter-out arch%,$(ALL_LIBS-y))

in the link, at which point it doesn't matter about the exact order in ALL_LIBS-y?

We do a similar trick with UBSAN_FLAGS to force the ordering of -fxxx vs -fno-xxx.

~Andrew

#7 lore raw

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

Quoted 62 lines · Andrew Cooper
Quoted 52 lines · Jan Beulich
Quoted 51 lines · Andrew Cooper
Quoted 34 lines · Jan Beulich
Quoted 17 lines · Andrew Cooper

xen/arch/x86/arch.mk line 7

Quoted 10 lines · Jan Beulich
xen/arch/x86/arch.mk +2 -0
index 16368a498bb7..a0ee050c931b 100644
@@ -3,6 +3,8 @@
 
 export XEN_IMG_OFFSET := 0x200000
 
+ALL_LIBS-y += arch/x86/lib/lib.a
+

Oh, I'd realised it was this easy, I'd have done so straight away when adding x86's custom arch_generic_hweightl().

I assumed it was going to be more complicated getting the order of the arch specific lib correct with the generic lib.

Quoted 6 lines

More concretely.  Given an x86 lib, we should move things like arch/x86/memcpy.S to it.

Therefore, when we have common/lib.a and arch/lib.a, do we guarantee to have arch/lib.a with higher precedence so for matching functions the arch specific one guarantees to be taken?

Not with the change above, it would need to become

ALL_LIBS-y := arch/x86/lib/lib.a $(ALL_LIBS-y)

to achieve that, requiring that ALL_LIBS-y won't change into a lazy-expansion variable. If that's okay (please confirm), I can adjust the patch.

Things would be yet easier if every arch had a lib/lib.a, as then in xen/Makefile we could simply have

Quoted 6 lines

ALL_LIBS-y := arch/$(SRCARCH)/lib/lib.a
ALL_LIBS-y += lib/lib.a

Alternatively we could move the setting of ALL_LIBS-y in xen/Makefile to after the arch/$(SRCARCH)/arch.mk inclusion. I'd be a little wary of that, though, as it would then be different from ALL_OBJS-y.

I think this would be better handled by common code.

Arches are going to want a lib.a eventually.  ARM even has arch/arm/arm{32,64}/lib/ but like x86 they're just simple obj-y += at the moment.

However, arches shouldn't be forced to make an empty one simply to build.

Does this work:

Quoted 8 lines
Quoted 4 lines

ALL_LIBS-y                := $(wildcard arch/$(SRCARCH)/lib/lib.a) ALL_LIBS-y                += lib/lib.a

?  If so, I think it's the nicest option.

I had thought of doing it this way initially, but on a fresh build arch/$(SRCARCH)/lib/lib.a wouldn't be there when the Makefile is read in. Whether switching ALL_LIBS-y to be a lazy-expansion variable would work I didn't try; I'd prefer not to change the kind of variable that it is.

Quoted 6 lines

Hmm.  What about:

    $(filter arch%,$(ALL_LIBS-y)) $(filter-out arch%,$(ALL_LIBS-y))

in the link, at which point it doesn't matter about the exact order in ALL_LIBS-y?

Hmm, we could apparently do something like that, but looking at how ALL_LIBS (note: not ALL_LIBS-y) is used, I wonder if I didn't make us depend on unspecified behavior in f301f9a9e84f ("lib: collect library files in an archive"): I don't think it is well-defined whether undefined symbols are resolved by pulling in archive members when doing a relocatable link. (Of course this is only a tangential aspect here.)

Another question is whether it is really always going to be the case that arch-specific libraries want to take precedence over common ones.

To summarize, I think by this point I'd still prefer the

ALL_LIBS-y := arch/x86/lib/lib.a $(ALL_LIBS-y)

approach mentioned earlier.

Jan

#8 lore raw

PATCH 2/2 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                         |  1 -
 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(+), 12 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 -1
index 61e2293a467e..76540d77e55f 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
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:
+ */
Signature · 1 line
2.48.2
#9 lore raw

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

xen/arch/x86/Makefile obj-y += time.o · line 77

Quoted 12 lines · Jan Beulich
xen/arch/x86/Makefile +0 -1
index 61e2293a467e..76540d77e55f 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

There's

# Allows usercopy.c to include itself
$(obj)/usercopy.o: CFLAGS-y += -iquote .

which looks like it wants moving too.

But, given that this presumably compiles, doesn't it mean we've got a search path problem anyway?

~Andrew

#10 lore raw

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

Quoted 16 lines · Andrew Cooper

xen/arch/x86/Makefile obj-y += time.o · line 77

Quoted 12 lines · Jan Beulich
xen/arch/x86/Makefile +0 -1
index 61e2293a467e..76540d77e55f 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

There's

Quoted 4 lines

# Allows usercopy.c to include itself
$(obj)/usercopy.o: CFLAGS-y += -iquote .

which looks like it wants moving too.

It doesn't need moving, and I apparently sent a stale patch. This hunk is simply missing:

@@ -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 .
-
$(obj)/x86_emulate.o: CFLAGS-y += -Os
ifneq ($(CONFIG_HVM),y) $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label

Quoted 2 lines

But, given that this presumably compiles, doesn't it mean we've got a search path problem anyway?

Iirc the need for the piece above was because of using __FILE__ with #include, which now we don't anymore. Or maybe it was a leftover altogether. Anyway, I don't see there being a search path problem, as #include "..." (as used now) would better always work.

Jan