Skip to contentSkip to conversation

libx86: Remaining serialisation logic

PATCH In review Andrew Cooper 3 participants 11 messages Feb 25, 2019 – Mar 11, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover libx86: Remaining serialisation logic

Andrew Cooper (2):
libx86: Introduce a helper to deserialise cpuid_policy objects tools/cpu-policy: Add unit tests

Roger Pau Monné (1):
libx86: introduce a helper to deserialise msr_policy objects

 tools/tests/Makefile                     |   1 +
 tools/tests/cpu-policy/.gitignore        |   1 +
 tools/tests/cpu-policy/Makefile          |  28 ++++
 tools/tests/cpu-policy/test-cpu-policy.c | 258 +++++++++++++++++++++++++++++++
 xen/include/xen/lib/x86/cpuid.h          |  23 ++-
 xen/include/xen/lib/x86/msr.h            |  21 +++
 xen/lib/x86/cpuid.c                      | 109 +++++++++++++
 xen/lib/x86/msr.c                        |  67 ++++++++
 xen/lib/x86/private.h                    |  17 ++
 9 files changed, 524 insertions(+), 1 deletion(-)
 create mode 100644 tools/tests/cpu-policy/.gitignore
 create mode 100644 tools/tests/cpu-policy/Makefile
 create mode 100644 tools/tests/cpu-policy/test-cpu-policy.c
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 1/3 libx86: introduce a helper to deserialise msr_policy objects

From: Roger Pau Monné <[email protected]>

As with the serialise side, Xen's copy_from_guest API is used, with a compatibility wrapper for the userspace build.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

v2:
* Fix comment

---
 xen/include/xen/lib/x86/msr.h | 21 ++++++++++++++
 xen/lib/x86/msr.c             | 67 +++++++++++++++++++++++++++++++++++++++++++
 xen/lib/x86/private.h         | 14 +++++++++
 3 files changed, 102 insertions(+)
xen/include/xen/lib/x86/msr.h +21 -0
index e2cfbb1..6236622 100644
@@ -48,6 +48,27 @@ typedef xen_msr_entry_t msr_entry_buffer_t[];
 int x86_msr_copy_to_buffer(const struct msr_policy *policy,
                            msr_entry_buffer_t msrs, uint32_t *nr_entries);
 
+/**
+ * Unserialise an msr_policy object from an array of msrs.
+ *
+ * @param policy     The msr_policy object to unserialise into.
+ * @param msrs       The array of msrs to unserialise from.
+ * @param nr_entries The number of entries in 'msrs'.
+ * @param err_msr    Optional hint filled on error.
+ * @returns -errno
+ *
+ * Reads at most MSR_MAX_SERIALISED_ENTRIES.  May fail for a number of reasons
+ * based on the content in an individual 'msrs' entry, including the MSR index
+ * not being valid in the policy, the flags field being nonzero, or if the
+ * value provided would truncate when stored in the policy.  In such cases,
+ * the optional err_* pointer is filled in to aid diagnostics.
+ *
+ * No content validation is performed on the data stored in the policy object.
+ */
+int x86_msr_copy_from_buffer(struct msr_policy *policy,
+                             const msr_entry_buffer_t msrs, uint32_t nr_entries,
+                             uint32_t *err_msr);
+
 #endif /* !XEN_LIB_X86_MSR_H */
 
 /*
xen/lib/x86/msr.c +67 -0

73 lines not on this page · raw message

xen/lib/x86/private.h +14 -0
index 3ee99aa..e0ff2da 100644
@@ -12,6 +12,7 @@
 #include <asm/msr-index.h>
 
 #define copy_to_buffer_offset copy_to_guest_offset
+#define copy_from_buffer_offset copy_from_guest_offset
 
 #else
 
@@ -44,6 +45,19 @@ static inline bool test_bit(unsigned int bit, const void *vaddr)
     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 */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH v2 1/3] libx86: introduce a helper to deserialise msr_policy objects

Quoted 5 lines · Andrew Cooper
xen/lib/x86/msr.c +5 -0
+
+        switch ( data.idx )
+        {
+            /*
+             * Assign data.val to 'field', checking for truncation if the

xen/lib/x86/msr.c int x86_msr_copy_to_buffer(const struct msr_policy *p, · line 95

Quoted 10 lines
xen/lib/x86/msr.c +10 -0
+             * backing storage for 'field' is smaller than uint64_t
+             */
+#define ASSIGN(field)                           \
+({                                              \
+    if ( (typeof(field))data.val != data.val )  \
+    {                                           \
+        rc = -EOVERFLOW;                        \
+        goto err;                               \
+    }                                           \
+    field = data.val;                           \

Missing parentheses around "field" in the macro. Although I don't think it will break in practice, it is better to follow general macro writing rules.

Other than this, this patch looks good to me.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH v2 1/3] libx86: introduce a helper to deserialise msr_policy objects

Quoted 11 lines · Wei Liu
Quoted 10 lines · Andrew Cooper
xen/lib/x86/msr.c +10 -0
+
+        switch ( data.idx )
+        {
+            /*
+             * Assign data.val to 'field', checking for truncation if the
+             * backing storage for 'field' is smaller than uint64_t
+             */
+#define ASSIGN(field)                           \
+({                                              \
+    if ( (typeof(field))data.val != data.val )  \
Quoted 8 lines

xen/lib/x86/msr.c int x86_msr_copy_to_buffer(const struct msr_policy *p, · line 95

Quoted 5 lines
xen/lib/x86/msr.c +5 -0
+    {                                           \
+        rc = -EOVERFLOW;                        \
+        goto err;                               \
+    }                                           \
+    field = data.val;                           \

Missing parentheses around "field" in the macro. Although I don't think it will break in practice, it is better to follow general macro writing rules.

Fixed.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#4 lore raw

Re: [PATCH v2 1/3] libx86: introduce a helper to deserialise msr_policy objects

In reply to Wei Liu
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 13:31, <[email protected]> wrote:
Quoted 11 lines · Andrew Cooper
xen/lib/x86/msr.c +11 -0
+
+        switch ( data.idx )
+        {
+            /*
+             * Assign data.val to 'field', checking for truncation if the
+             * backing storage for 'field' is smaller than uint64_t
+             */
+#define ASSIGN(field)                           \
+({                                              \
+    if ( (typeof(field))data.val != data.val )  \
+    {                                           \
Quoted 8 lines

xen/lib/x86/msr.c int x86_msr_copy_to_buffer(const struct msr_policy *p, · line 95

Quoted 4 lines
xen/lib/x86/msr.c +4 -0
+        rc = -EOVERFLOW;                        \
+        goto err;                               \
+    }                                           \
+    field = data.val;                           \

Missing parentheses around "field" in the macro. Although I don't think it will break in practice, it is better to follow general macro writing rules.

With this, or even better with p-> pulled up here from the invocation,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

PATCH v2 2/3 libx86: Introduce a helper to deserialise cpuid_policy objects

In reply to Andrew Cooper

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

v2:
* Avoid explicit type overlap.
* Fix comment.
* Use array_access_nospec(), with a dummy implementation for userspace builds.

---
 xen/include/xen/lib/x86/cpuid.h |  21 ++++++++
 xen/lib/x86/cpuid.c             | 109 ++++++++++++++++++++++++++++++++++++++++
 xen/lib/x86/private.h           |   3 ++
 3 files changed, 133 insertions(+)
xen/include/xen/lib/x86/cpuid.h +21 -0
index 22d43ef..767a33b 100644
@@ -319,6 +319,27 @@ typedef xen_cpuid_leaf_t cpuid_leaf_buffer_t[];
 int x86_cpuid_copy_to_buffer(const struct cpuid_policy *policy,
                              cpuid_leaf_buffer_t leaves, uint32_t *nr_entries);
 
+/**
+ * Unserialise a cpuid_policy object from an array of cpuid leaves.
+ *
+ * @param policy      The cpuid_policy to unserialise into.
+ * @param leaves      The array of leaves to unserialise from.
+ * @param nr_entries  The number of entries in 'leaves'.
+ * @param err_leaf    Optional hint filled on error.
+ * @param err_subleaf Optional hint filled on error.
+ * @returns -errno
+ *
+ * Reads at most CPUID_MAX_SERIALISED_LEAVES.  May return -ERANGE if an
+ * incoming leaf is out of range of cpuid_policy, in which case the optional
+ * err_* pointers are filled to aid diagnostics.
+ *
+ * No content validation of in-range leaves is performed.
+ */
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_entries, uint32_t *err_leaf,
+                               uint32_t *err_subleaf);
+
 #endif /* !XEN_LIB_X86_CPUID_H */
 
 /*
xen/lib/x86/cpuid.c +109 -0

115 lines not on this page · raw message

xen/lib/x86/private.h +3 -0
index e0ff2da..6fb5022 100644
@@ -6,6 +6,7 @@
 #include <xen/bitops.h>
 #include <xen/kernel.h>
 #include <xen/lib.h>
+#include <xen/nospec.h>
 #include <xen/types.h>
 
 #include <asm/guest_access.h>
@@ -32,6 +33,8 @@ static inline bool test_bit(unsigned int bit, const void *vaddr)
     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)      \
 ({                                                      \
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] libx86: Introduce a helper to deserialise cpuid_policy objects

Quoted 3 lines · Andrew Cooper

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#7 lore raw

Re: [PATCH v2 2/3] libx86: Introduce a helper to deserialise cpuid_policy objects

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 27.02.19 at 13:31, <[email protected]> wrote:
Quoted 3 lines · Andrew Cooper

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

PATCH v2 3/3 tools/cpu-policy: Add unit tests

In reply to Andrew Cooper

These will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>

v2:
* Pospone the AFL fuzzer for now. It needs a bit more work.
* Exit early for malloc() failures, which won't occur in practice.

---
 tools/tests/Makefile                     |   1 +
 tools/tests/cpu-policy/.gitignore        |   1 +
 tools/tests/cpu-policy/Makefile          |  28 ++++
 tools/tests/cpu-policy/test-cpu-policy.c | 258 +++++++++++++++++++++++++++++++
 xen/include/xen/lib/x86/cpuid.h          |   2 +-
 5 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 tools/tests/cpu-policy/.gitignore
 create mode 100644 tools/tests/cpu-policy/Makefile
 create mode 100644 tools/tests/cpu-policy/test-cpu-policy.c
tools/tests/Makefile +1 -0
index a9fc50d..067a380 100644
@@ -5,6 +5,7 @@ CFLAGS  += $(CFLAGS_libxenctrl)
 LDLIBS += $(LDLIBS_libxenctrl)
 
 SUBDIRS-y :=
+SUBDIRS-$(CONFIG_X86) += cpu-policy
 SUBDIRS-$(CONFIG_X86) += mce-test
 SUBDIRS-y += mem-sharing
 ifeq ($(XEN_TARGET_ARCH),__fixme__)
tools/tests/cpu-policy/.gitignore new file +1 -0
new file mode 100644
index 0000000..83bdb6b
@@ -0,0 +1 @@
+test-cpu-policy
tools/tests/cpu-policy/Makefile new file +28 -0
new file mode 100644
index 0000000..eeed7f3
@@ -0,0 +1,28 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+.PHONY: all
+all: test-cpu-policy
+
+.PHONY: clean
+clean:
+	$(RM) -f -- *.o .*.d .*.d2 test-cpu-policy
+
+.PHONY: distclean
+distclean: clean
+	$(RM) -f -- *~
+
+.PHONY: install
+install: all
+
+.PHONY: uninstall
+
+CFLAGS += -Werror $(CFLAGS_xeninclude) -D__XEN_TOOLS__ -O3
+CFLAGS += $(APPEND_CFLAGS)
+
+vpath %.c ../../../xen/lib/x86
+
+test-cpu-policy: test-cpu-policy.o msr.o cpuid.o
+	$(CC) $(CFLAGS) $^ -o $@
+
+-include $(DEPS_INCLUDE)
tools/tests/cpu-policy/test-cpu-policy.c new file +258 -0

258 lines not on this page · raw message

xen/include/xen/lib/x86/cpuid.h +1 -1
index 767a33b..95b37b6 100644
@@ -66,8 +66,8 @@ static inline void cpuid_count_leaf(
 #undef XCHG
 
 #define CPUID_GUEST_NR_BASIC      (0xdu + 1)
-#define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_CACHE      (5u + 1)
+#define CPUID_GUEST_NR_FEAT       (0u + 1)
 #define CPUID_GUEST_NR_TOPO       (1u + 1)
 #define CPUID_GUEST_NR_XSTATE     (62u + 1)
 #define CPUID_GUEST_NR_EXTD_INTEL (0x8u + 1)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw

Re: [PATCH v2 3/3] tools/cpu-policy: Add unit tests

Quoted 6 lines · Andrew Cooper

These will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

Looks good to me. But I will let you and Jan figure out whether we should use centralised gitignore or not.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v2 3/3
#10 lore raw

Re: [PATCH v2 3/3] tools/cpu-policy: Add unit tests

In reply to Andrew Cooper
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 25.02.19 at 16:47, <[email protected]> wrote:

These will be extended with further libx86 work.

Fix the sorting of the CPUID_GUEST_NR_* constants, noticed while writing the unit tests.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel