Skip to contentSkip to conversation

x86/cpu-policy: librarify copy-in/-out

PATCH Superseded Jan Beulich 2 participants 9 messages Dec 22, 2025 – Jan 6

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/cpu-policy: librarify copy-in/-out

The 1st patch is largely unrelated, but helps demonstrate that the component isn't being (silently) broken by the adjustments made.

1: enable build of fuzzing harness by default
2: move copy-in/-out functions to arch library

Jan

#1 lore raw

PATCH 1/2 x86/cpu-policy: enable build of fuzzing harness by default

... on x86, to make sure its bit-rotting can be limited at least a little.

tools/fuzz/Makefile +1 -0
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += libelf
 SUBDIRS-y += x86_instruction_emulator
+SUBDIRS-$(CONFIG_X86_64) += cpu-policy
 
 .PHONY: all clean distclean install uninstall
 all clean distclean install uninstall: %: subdirs-%
#2 lore raw

Re: [PATCH 1/2] x86/cpu-policy: enable build of fuzzing harness by default

tools/fuzz/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 6

Quoted 10 lines · Jan Beulich

... on x86, to make sure its bit-rotting can be limited at least a little.

tools/fuzz/Makefile +0 -0
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += libelf
 SUBDIRS-y += x86_instruction_emulator

Blank line here please.  Or it's going to get messy with extra additions.

Otherwise, Acked-by: Andrew Cooper <[email protected]>

Quoted 5 lines
tools/fuzz/Makefile +1 -0
+SUBDIRS-$(CONFIG_X86_64) += cpu-policy
 
 .PHONY: all clean distclean install uninstall
 all clean distclean install uninstall: %: subdirs-%
#3 lore raw

Re: [PATCH 1/2] x86/cpu-policy: enable build of fuzzing harness by default

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

... on x86, to make sure its bit-rotting can be limited at least a little.

tools/fuzz/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 10

Quoted 7 lines
tools/fuzz/Makefile +1 -0
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += libelf
 SUBDIRS-y += x86_instruction_emulator
+SUBDIRS-$(CONFIG_X86_64) += cpu-policy
 
 .PHONY: all clean distclean install uninstall
 all clean distclean install uninstall: %: subdirs-%

As it turns out this causes build failures on Ubuntu (and only there, and only with gcc, which I don't understand):

afl-policy-fuzzer.c: In function 'main':
afl-policy-fuzzer.c:153:9: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]

         fread(cp, sizeof(*cp), 1, fp);
         ^

cc1: all warnings being treated as errors

Given how the code uses calloc() up front I don't really see why evaluating the return value would actually be meaningful here, so I'm inclined to add a cast to void (provided that would make a difference, which I have yet to check). Opinions?

Jan

#4 lore raw

Re: [PATCH 1/2] x86/cpu-policy: enable build of fuzzing harness by default

Quoted 22 lines · Jan Beulich
Quoted 8 lines · Jan Beulich

... on x86, to make sure its bit-rotting can be limited at least a little.

tools/fuzz/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 10

Quoted 7 lines
tools/fuzz/Makefile +1 -0
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += libelf
 SUBDIRS-y += x86_instruction_emulator
+SUBDIRS-$(CONFIG_X86_64) += cpu-policy
 
 .PHONY: all clean distclean install uninstall
 all clean distclean install uninstall: %: subdirs-%

As it turns out this causes build failures on Ubuntu (and only there, and only with gcc, which I don't understand):

afl-policy-fuzzer.c: In function 'main':
afl-policy-fuzzer.c:153:9: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]

Quoted 8 lines
         fread(cp, sizeof(*cp), 1, fp);
         ^

cc1: all warnings being treated as errors

Given how the code uses calloc() up front I don't really see why evaluating the return value would actually be meaningful here, so I'm inclined to add a cast to void (provided that would make a difference, which I have yet to check). Opinions?

Simply casting doesn't work. Hence what I'm intending to do is

tools/fuzz/cpu-policy/afl-policy-fuzzer.c +3 -2
@@ -133,6 +133,7 @@ int main(int argc, char **argv)
 #endif
     {
         struct cpu_policy *cp = NULL;
+        size_t size;
 
         if ( fp != stdin )
         {
@@ -150,9 +151,9 @@ int main(int argc, char **argv)
         if ( !cp )
             goto skip;
 
-        fread(cp, sizeof(*cp), 1, fp);
+        size = fread(cp, sizeof(*cp), 1, fp);
 
-        if ( !feof(fp) )
+        if ( !size || !feof(fp) )
             goto skip;
 
         check_policy(cp);

along with amending the description:

"Since on Ubuntu fread()'s return value needs evaluating, adjust the code
there to also skip the test when there's no data at all."

May I keep your ack with that adjustment?

Jan

#5 lore raw

Re: [PATCH 1/2] x86/cpu-policy: enable build of fuzzing harness by default

Quoted 55 lines · Jan Beulich
Quoted 21 lines · Jan Beulich
Quoted 8 lines · Jan Beulich

... on x86, to make sure its bit-rotting can be limited at least a little.

tools/fuzz/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 10

Quoted 7 lines
tools/fuzz/Makefile +1 -0
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += libelf
 SUBDIRS-y += x86_instruction_emulator
+SUBDIRS-$(CONFIG_X86_64) += cpu-policy
 
 .PHONY: all clean distclean install uninstall
 all clean distclean install uninstall: %: subdirs-%

As it turns out this causes build failures on Ubuntu (and only there, and only with gcc, which I don't understand):

afl-policy-fuzzer.c: In function 'main':
afl-policy-fuzzer.c:153:9: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]

Quoted 8 lines
         fread(cp, sizeof(*cp), 1, fp);
         ^

cc1: all warnings being treated as errors

Given how the code uses calloc() up front I don't really see why evaluating the return value would actually be meaningful here, so I'm inclined to add a cast to void (provided that would make a difference, which I have yet to check). Opinions?

Simply casting doesn't work. Hence what I'm intending to do is

tools/fuzz/cpu-policy/afl-policy-fuzzer.c +3 -2
@@ -133,6 +133,7 @@ int main(int argc, char **argv)
 #endif
     {
         struct cpu_policy *cp = NULL;
+        size_t size;
 
         if ( fp != stdin )
         {
@@ -150,9 +151,9 @@ int main(int argc, char **argv)
         if ( !cp )
             goto skip;
 
-        fread(cp, sizeof(*cp), 1, fp);
+        size = fread(cp, sizeof(*cp), 1, fp);
 
-        if ( !feof(fp) )
+        if ( !size || !feof(fp) )
             goto skip;
 
         check_policy(cp);
Quoted 6 lines

along with amending the description:

"Since on Ubuntu fread()'s return value needs evaluating, adjust the code
there to also skip the test when there's no data at all."

May I keep your ack with that adjustment?

Yes.  This is just harness code, so whatever compiles.

~Andrew

#6 lore raw

PATCH 2/2 x86/cpu-policy: move copy-in/-out functions to arch library

In reply to Jan Beulich

This is to aid with MGMT_HYPERCALL work, leaving the functions potentially unreferenced (which Misra dislikes). By moving them to separate archive members, the linker simply will not pick them up when not needed.

As the CPUID and MSR ones are always used together, put the "from" and "to" variants of each together in one file respectively.

tools/fuzz/cpu-policy/Makefile +6 -2
@@ -20,9 +20,13 @@ install: all
 CFLAGS += $(CFLAGS_xeninclude) -D__XEN_TOOLS__
 CFLAGS += $(APPEND_CFLAGS) -Og
 
-vpath %.c ../../../xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/arch/x86/lib
 
-afl-policy-fuzzer: afl-policy-fuzzer.o msr.o cpuid.o
+lib-y :=
+include $(XEN_ROOT)/xen/arch/x86/lib/Makefile.cpu-policy
+
+afl-policy-fuzzer: afl-policy-fuzzer.o cpuid.o $(lib-y)
 	$(CC) $(CFLAGS) $^ -o $@
 
 -include $(DEPS_INCLUDE)
tools/libs/guest/Makefile.common +6 -2
@@ -33,9 +33,13 @@ LIBELF_OBJS += libelf-dominfo.o
 OBJS-y += $(LIBELF_OBJS)
 
 ifeq ($(CONFIG_X86),y) # Add libx86 to the build
-vpath %.c ../../../xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/arch/x86/lib
 
-OBJS-y                 += cpuid.o msr.o policy.o
+lib-y :=
+include $(XEN_ROOT)/xen/arch/x86/lib/Makefile.cpu-policy
+
+OBJS-y                 += cpuid.o policy.o $(lib-y)
 endif
 
 # new domain builder
tools/tests/cpu-policy/Makefile +6 -2
@@ -42,11 +42,15 @@ CFLAGS += $(APPEND_CFLAGS)
 
 LDFLAGS += $(APPEND_LDFLAGS)
 
-vpath %.c ../../../xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/arch/x86/lib
+
+lib-y :=
+include $(XEN_ROOT)/xen/arch/x86/lib/Makefile.cpu-policy
 
 %.o: Makefile
 
-test-cpu-policy: test-cpu-policy.o msr.o cpuid.o policy.o
+test-cpu-policy: test-cpu-policy.o cpuid.o policy.o $(lib-y)
 	$(CC) $^ -o $@ $(LDFLAGS)
 
 -include $(DEPS_INCLUDE)
xen/arch/x86/lib/Makefile +2 -0
@@ -6,3 +6,5 @@ lib-y += generic-hweightl.o
 lib-y += memcpy.o
 lib-y += memset.o
 lib-y += scrub-page.o
+
+include $(srcdir)/Makefile.cpu-policy
xen/arch/x86/lib/Makefile.cpu-policy new file +2 -0
@@ -0,0 +1,2 @@
+lib-y += cp-copy-from-buffer.o
+lib-y += cp-copy-to-buffer.o
xen/arch/x86/lib/cp-copy-from-buffer.c new file +238 -0

238 lines not on this page · raw message

xen/arch/x86/lib/cp-copy-to-buffer.c new file +204 -0

204 lines not on this page · raw message

xen/lib/x86/Makefile +0 -1
@@ -1,3 +1,2 @@
 obj-y += cpuid.o
-obj-y += msr.o
 obj-y += policy.o
xen/lib/x86/cpuid.c +0 -226

232 lines not on this page · raw message

xen/lib/x86/msr.c deleted +0 -130

130 lines not on this page · raw message

xen/lib/x86/private.h +0 -31
@@ -12,9 +12,6 @@
 
 #include <asm/msr.h>
 
-#define copy_to_buffer_offset copy_to_guest_offset
-#define copy_from_buffer_offset copy_from_guest_offset
-
 #else
 
 #include <errno.h>
@@ -35,34 +32,6 @@ static inline bool test_bit(unsigned int
     return addr[bit / 8] & (1u << (bit % 8));
 }
 
-#define array_access_nospec(a, i) (a)[(i)]
-
-/* memcpy(), but with copy_to_guest_offset()'s API. */
-#define copy_to_buffer_offset(dst, index, src, nr)      \
-({                                                      \
-    const typeof(*(src)) *src_ = (src);                 \
-    typeof(*(dst)) *dst_ = (dst);                       \
-    typeof(index) index_ = (index);                     \
-    typeof(nr) nr_ = (nr), i_;                          \
-                                                        \
-    for ( i_ = 0; i_ < nr_; i_++ )                      \
-        dst_[index_ + i_] = src_[i_];                   \
-    0;                                                  \
-})
-
-/* memcpy(), but with copy_from_guest_offset()'s API. */
-#define copy_from_buffer_offset(dst, src, index, nr)    \
-({                                                      \
-    const typeof(*(src)) *src_ = (src);                 \
-    typeof(*(dst)) *dst_ = (dst);                       \
-    typeof(index) index_ = (index);                     \
-    typeof(nr) nr_ = (nr), i_;                          \
-                                                        \
-    for ( i_ = 0; i_ < nr_; i_++ )                      \
-        dst_[i_] = src_[index_ + i_];                   \
-    0;                                                  \
-})
-
 #endif /* __XEN__ */
 
 #endif /* XEN_LIB_X86_PRIVATE_H */
#7 lore raw

Re: [PATCH 2/2] x86/cpu-policy: move copy-in/-out functions to arch library

Quoted 31 lines · Jan Beulich
tools/tests/cpu-policy/Makefile +6 -2
@@ -42,11 +42,15 @@ CFLAGS += $(APPEND_CFLAGS)
 
 LDFLAGS += $(APPEND_LDFLAGS)
 
-vpath %.c ../../../xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/arch/x86/lib
+
+lib-y :=
+include $(XEN_ROOT)/xen/arch/x86/lib/Makefile.cpu-policy
 
 %.o: Makefile
 
-test-cpu-policy: test-cpu-policy.o msr.o cpuid.o policy.o
+test-cpu-policy: test-cpu-policy.o cpuid.o policy.o $(lib-y)
 	$(CC) $^ -o $@ $(LDFLAGS)
 
 -include $(DEPS_INCLUDE)
xen/arch/x86/lib/Makefile +2 -0
@@ -6,3 +6,5 @@ lib-y += generic-hweightl.o
 lib-y += memcpy.o
 lib-y += memset.o
 lib-y += scrub-page.o
+
+include $(srcdir)/Makefile.cpu-policy

xen/arch/x86/lib/Makefile.cpu-policy line 2

Quoted 2 lines
xen/arch/x86/lib/Makefile.cpu-policy new file +2 -0
@@ -0,0 +1,2 @@
+lib-y += cp-copy-from-buffer.o
+lib-y += cp-copy-to-buffer.o

cp works as a name in source code because it's used by a type called cpu_policy.

In this case, cp with it's UNIX association makes the file name very weird.

If we're going to be properly lib-ing the whole thing, then we're going to have enough TUs for a directory anyway.

So, I think we want xen/arch/x86/lib/cpu-policy/copy-{to,from}-buffer.o right from the outset.

Also, I'd really prefer not to have things split between the old and new locations.  Everything wants to move in one go.

I'd suggest having one patch doing a wholesale move of xen/lib/x86 into xen/arch/x86/lib/cpu-policy and fixing up the vpath's, then a subsequent patch splitting copy-{to,from}-buffer.o out.

xen/arch/x86/lib/cp-copy-from-buffer.c line 1

Quoted 4 lines
xen/arch/x86/lib/cp-copy-from-buffer.c new file +1 -0
@@ -0,0 +1,238 @@
+#ifdef __XEN__

Hmm.  I should sort out SPDX tags.  Defaults would say GPL-2-only, but libxenguest is LGPL-2.1 so it probably ought to be LGPL.

~Andrew

#8 lore raw

Re: [PATCH 2/2] x86/cpu-policy: move copy-in/-out functions to arch library

Quoted 38 lines · Andrew Cooper
Quoted 31 lines · Jan Beulich
tools/tests/cpu-policy/Makefile +6 -2
@@ -42,11 +42,15 @@ CFLAGS += $(APPEND_CFLAGS)
 
 LDFLAGS += $(APPEND_LDFLAGS)
 
-vpath %.c ../../../xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/lib/x86
+vpath %.c $(XEN_ROOT)/xen/arch/x86/lib
+
+lib-y :=
+include $(XEN_ROOT)/xen/arch/x86/lib/Makefile.cpu-policy
 
 %.o: Makefile
 
-test-cpu-policy: test-cpu-policy.o msr.o cpuid.o policy.o
+test-cpu-policy: test-cpu-policy.o cpuid.o policy.o $(lib-y)
 	$(CC) $^ -o $@ $(LDFLAGS)
 
 -include $(DEPS_INCLUDE)
xen/arch/x86/lib/Makefile +2 -0
@@ -6,3 +6,5 @@ lib-y += generic-hweightl.o
 lib-y += memcpy.o
 lib-y += memset.o
 lib-y += scrub-page.o
+
+include $(srcdir)/Makefile.cpu-policy

xen/arch/x86/lib/Makefile.cpu-policy line 2

Quoted 2 lines
xen/arch/x86/lib/Makefile.cpu-policy new file +2 -0
@@ -0,0 +1,2 @@
+lib-y += cp-copy-from-buffer.o
+lib-y += cp-copy-to-buffer.o

cp works as a name in source code because it's used by a type called cpu_policy.

Quoted 8 lines

In this case, cp with it's UNIX association makes the file name very weird.

If we're going to be properly lib-ing the whole thing, then we're going to have enough TUs for a directory anyway.

So, I think we want xen/arch/x86/lib/cpu-policy/copy-{to,from}-buffer.o right from the outset.

I did consider this, but didn't like it very much because of this implication:
Either I need to touch arch.mk again to specify yet another archive to the linker (not very scalable). Or I need to teach the build system to know to combine archives from sub-directories into the parent directory's (i.e. follow how built_in.o are being treated).

As you ask for it, I'll use the former of the two approaches (for likely being simpler). Let me know if you disagree, or if you know of another good approach.

Quoted 6 lines

Also, I'd really prefer not to have things split between the old and new locations.  Everything wants to move in one go.

I'd suggest having one patch doing a wholesale move of xen/lib/x86 into xen/arch/x86/lib/cpu-policy and fixing up the vpath's, then a subsequent patch splitting copy-{to,from}-buffer.o out.

Can do; I would have thought that doing the move piecemeal ought to be okay.

Jan