x86: CPUID and MSR policy marshalling support
cover x86: CPUID and MSR policy marshalling support
This series introduces libx86, a small shared library between the hypervisor and libxc, and hypercalls to get/set full CPUID/MSR policies. Future work will arrange for XEN_DOMCTL_set_cpumsr_policy to function properly, after the auditing and comparison logic is sorted.
In the meantime, the data marshalling logic is in a suitable state for review.
Some open questions:
* The position of libx86 in the source tree. It probably doesn't want to live in its current location.
* Naming of various files, particularly policy.c (which I don't like, but can't think of anything better).
* A TOCTOU race in the final patch, caused by the copy_from_guest() ABI. More details in that patch.
This series is based on x86-next, and can be found in git form here:
http://xenbits.xen.org/gitweb/?p=people/andrewcoop/xen.git;a=shortlog;h=refs/heads/cpuid-marshal-v1
Andrew Cooper (5):
libx86: Introduce libx86/cpuid.h
libx86: Introduce libx86/msr.h and share msr_{domain,vcpu}_policy with userspace
libx86: Introduce a helper to serialise a cpuid_policy object
x86: Collect policies together into groups
libx86: Introduce a helper to deserialise a cpuid_policy object
Roger Pau Monné (5):
libx86: generate cpuid-autogen.h in the libx86 include dir
libx86: Share struct cpuid_policy with userspace
libx86: introduce a libx86 shared library
libx86: Introduce a helper to serialise msr_{domain,vcpu}_policy objects
libx86: introduce a helper to deserialize MSR policies
Sergey Dyasli (3):
x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy x86/domctl: Implement XEN_DOMCTL_get_cpumsr_policy x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
.gitignore | 2 +-
tools/include/Makefile | 15 ++
tools/libxc/Makefile | 14 +-
tools/libxc/include/xenctrl.h | 15 +-
tools/libxc/xc_cpuid_x86.c | 163 +++++++++++++++++---
tools/misc/xen-cpuid.c | 135 +++++++++++++++-
tools/tests/x86_emulator/x86-emulate.h | 2 +
xen/arch/x86/cpu/common.c | 2 +-
xen/arch/x86/cpuid.c | 32 +---
xen/arch/x86/domctl.c | 133 ++++++++++++++++
xen/arch/x86/msr.c | 4 +-
xen/arch/x86/sysctl.c | 101 ++++++++++++
xen/arch/x86/x86_emulate/x86_emulate.h | 7 +-
xen/common/Makefile | 1 +
xen/common/libx86/Makefile | 3 +
xen/common/libx86/cpuid.c | 226 +++++++++++++++++++++++++++
xen/common/libx86/libx86-private.h | 74 +++++++++
xen/common/libx86/msr.c | 134 ++++++++++++++++
xen/common/libx86/policies.c | 19 +++
xen/include/Makefile | 6 +-
xen/include/asm-x86/cpufeatures.h | 2 +-
xen/include/asm-x86/cpuid.h | 228 +---------------------------
xen/include/asm-x86/msr.h | 65 ++------
xen/include/public/arch-x86/xen.h | 18 +++
xen/include/public/domctl.h | 25 +++
xen/include/public/sysctl.h | 41 +++++
xen/include/{asm-x86 => xen/libx86}/cpuid.h | 103 +++++--------
xen/include/xen/libx86/msr.h | 78 ++++++++++
xen/include/xen/libx86/policies.h | 35 +++++
xen/include/xen/xmalloc.h | 7 +
xen/xsm/flask/hooks.c | 3 +
xen/xsm/flask/policy/access_vectors | 4 +-
32 files changed, 1281 insertions(+), 416 deletions(-)
create mode 100644 xen/common/libx86/Makefile
create mode 100644 xen/common/libx86/cpuid.c
create mode 100644 xen/common/libx86/libx86-private.h
create mode 100644 xen/common/libx86/msr.c
create mode 100644 xen/common/libx86/policies.c
copy xen/include/{asm-x86 => xen/libx86}/cpuid.h (77%)
create mode 100644 xen/include/xen/libx86/msr.h
create mode 100644 xen/include/xen/libx86/policies.hSignature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 1/13 libx86: Introduce libx86/cpuid.h
Begin to untangle the header dependency tangle by moving definition of struct cpuid_leaf out of x86_emulate.h into the new cpuid.h.
Additionally, plumb the header through to libxc. This is technically a redundant include at this point, but it helps build-test the later changes, and will be used eventually.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- tools/include/Makefile | 3 +++ tools/libxc/xc_cpuid_x86.c | 2 ++ xen/arch/x86/x86_emulate/x86_emulate.h | 7 ++----- xen/include/asm-x86/cpuid.h | 4 +++- xen/include/xen/libx86/cpuid.h | 20 ++++++++++++++++++++ 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 xen/include/xen/libx86/cpuid.h
tools/include/Makefile +3 -0
@@ -21,6 +21,9 @@ xen/.dir: ln -sf $(addprefix $(XEN_ROOT)/xen/include/xen/,libelf.h elfstructs.h) xen/libelf/ ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi +ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 +endif touch $@ # Not xen/xsm as that clashes with link to
tools/libxc/xc_cpuid_x86.c +2 -0
@@ -27,6 +27,8 @@#include <xen/hvm/params.h> #include <xen-tools/libs.h> +#include <xen/libx86/cpuid.h> + enum { #define XEN_CPUFEATURE(name, value) X86_FEATURE_##name = value,#include <xen/arch-x86/cpufeatureset.h>
xen/arch/x86/x86_emulate/x86_emulate.h +2 -5
@@ -23,6 +23,8 @@#ifndef __X86_EMULATE_H__#define __X86_EMULATE_H__ +#include <xen/libx86/cpuid.h> +#define MAX_INST_LEN 15#if defined(__i386__)
@@ -172,11 +174,6 @@ enum x86_emulate_fpu_type { X86EMUL_FPU_none }; -struct cpuid_leaf -{ -uint32_t a, b, c, d; -}; -struct x86_emulate_state ;/*
xen/include/asm-x86/cpuid.h +3 -1
@@ -18,7 +18,9 @@#ifndef __ASSEMBLY__#include <xen/types.h> #include <xen/kernel.h> -#include <asm/x86_emulate.h> + +#include <xen/libx86/cpuid.h> +#include <public/sysctl.h> extern const uint32_t known_features[FSCAPINTS ];
xen/include/xen/libx86/cpuid.h new file +20 -0
@@ -0,0 +1,20 @@ +/* Common data structures and functions consumed by hypervisor and toolstack */ +#ifndef XEN_LIBX86_CPUID_H +#define XEN_LIBX86_CPUID_H + +struct cpuid_leaf +{ +uint32_t a, b, c, d; +}; + +#endif /* !XEN_LIBX86_CPUID_H */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 01/13] libx86: Introduce libx86/cpuid.h
Quoted 9 lines · Andrew Cooper
Begin to untangle the header dependency tangle by moving definition of struct
cpuid_leafout ofx86_emulate.hinto the newcpuid.h.Additionally, plumb the header through to libxc. This is technically a redundant include at this point, but it helps build-test the later changes, and will be used eventually.
Signed-off-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 01/13] libx86: Introduce libx86/cpuid.h
In reply to Andrew Coopertools/include/Makefile xen/.dir: · line 26
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
tools/include/Makefile +3
-0@@ -21,6 +21,9 @@ xen/.dir: ln -sf $(addprefix $(XEN_ROOT)/xen/include/xen/,libelf.h elfstructs.h) xen/libelf/ ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi +ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 +endif
Why not set the include path suitably? And otherwise, what harm would there be installing the link also when targeting ARM?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 01/13] libx86: Introduce libx86/cpuid.h
Quoted 11 lines · Jan Beulich
tools/include/Makefile xen/.dir: · line 26
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
tools/include/Makefile +3
-0@@ -21,6 +21,9 @@ xen/.dir: ln -sf $(addprefix $(XEN_ROOT)/xen/include/xen/,libelf.h elfstructs.h) xen/libelf/ ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi +ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 +endifWhy not set the include path suitably?
Because this is how everything else is currently done. If we want to change how tools get their hypervisor header files, that should be independent work.
Quoted 1 line
And otherwise, what harm would there be installing the link also when targeting ARM?
I'm fairly sure the ARM folk would object to having libx86 linked into their tools.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 01/13] libx86: Introduce libx86/cpuid.h
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 14:03, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
tools/include/Makefile xen/.dir: · line 26
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
tools/include/Makefile +3
-0@@ -21,6 +21,9 @@ xen/.dir: ln -sf $(addprefix $(XEN_ROOT)/xen/include/xen/,libelf.h elfstructs.h) xen/libelf/ ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi +ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 +endifWhy not set the include path suitably?
Quoted 3 lines
Because this is how everything else is currently done. If we want to change how tools get their hypervisor header files, that should be independent work.
How this gets done here has no direct implications on pre-existing mechanisms.
Quoted 5 lines
Quoted 1 line
And otherwise, what harm would there be installing the link also when
targeting ARM?
I'm fairly sure the ARM folk would object to having libx86 linked into their tools.
"Linked into" is ambiguous in this context - the presence of the headers does - afaict - not imply any code to appear in an ARM build of the tools.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 01/13] libx86: Introduce libx86/cpuid.h
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 14:03, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
tools/include/Makefile xen/.dir: · line 26
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
tools/include/Makefile +3
-0@@ -21,6 +21,9 @@ xen/.dir: ln -sf $(addprefix $(XEN_ROOT)/xen/include/xen/,libelf.h elfstructs.h) xen/libelf/ ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi +ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 +endifWhy not set the include path suitably?
Quoted 6 lines
Quoted 3 lines
Because this is how everything else is currently done. If we want to change how tools get their hypervisor header files, that should be independent work.
How this gets done here has no direct implications on pre-existing mechanisms.
While not wrong refactoring the build system appears to be orthogonal to the entire series. This had been proposed before when I tried to separate the xen and tools build systems apart before and I do not recall why it wasn't done.
In short don't block this series on this item.
--
Doug
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 01/13] libx86: Introduce libx86/cpuid.h
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 06.07.18 at 03:35, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 14:03, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
tools/include/Makefile xen/.dir: · line 21
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
tools/include/Makefile +0
-0@@ -21,6 +21,9 @@ xen/.dir: ln -sf $(addprefix $(XEN_ROOT)/xen/include/xen/,libelf.h elfstructs.h)xen/libelf/
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
tools/include/Makefile xen/.dir: · line 26
Quoted 5 lines
tools/include/Makefile +3
-0ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi +ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 +endifWhy not set the include path suitably?
Because this is how everything else is currently done. If we want to change how tools get their hypervisor header files, that should be independent work.
Quoted 7 lines
Quoted 2 lines
How this gets done here has no direct implications on pre-existing mechanisms.
While not wrong refactoring the build system appears to be orthogonal to the entire series. This had been proposed before when I tried to separate the xen and tools build systems apart before and I do not recall why it wasn't done.
I don't understand: I've not said the current model needs refactoring _in this series_ (it's likely worthwhile as an independent task). Leave as is what is there, but don't tie yourself to doing new things the same way.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 2/13 libx86: generate cpuid-autogen.h in the libx86 include dir
In reply to Andrew CooperFrom: Roger Pau Monné <[email protected]>
This prevents having to generate it inside the libxc folder.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
RFC - I'm not sure a parallel build of Xen and the tools works.
--- .gitignore | 2 +- tools/include/Makefile | 11 +++++++++++ tools/libxc/Makefile | 9 --------- tools/libxc/xc_cpuid_x86.c | 1 - xen/include/Makefile | 6 +++--- xen/include/asm-x86/cpufeatures.h | 2 +- xen/include/xen/libx86/cpuid.h | 2 ++ 7 files changed, 18 insertions(+), 15 deletions(-)
.gitignore +1 -1
@@ -310,7 +310,6 @@ xen/arch/*/efi/runtime.c xen/include/headers*.chk xen/include/asm xen/include/asm-*/asm-offsets.h -xen/include/asm-x86/cpuid-autogen.h xen/include/compat/* xen/include/config/ xen/include/generated/
@@ -318,6 +317,7 @@ xen/include/public/public xen/include/xen/*.new xen/include/xen/acm_policy.h xen/include/xen/compile.h +xen/include/xen/libx86/cpuid-autogen.h xen/test/livepatch/config.h xen/test/livepatch/xen_bye_world.livepatch xen/test/livepatch/xen_hello_world.livepatch
tools/include/Makefile +11 -0
@@ -26,6 +26,17 @@ ifeq ($(CONFIG_X86),y) endif touch $@ +ifeq ($(CONFIG_X86),y) +$(XEN_ROOT)/xen/include/xen/libx86/cpuid-autogen.h: $(XEN_ROOT)/xen/include/public/arch-x86/cpufeatureset.h $(XEN_ROOT)/xen/tools/gen-cpuid.py FORCE + $(PYTHON) $(XEN_ROOT)/xen/tools/gen-cpuid.py -i $< -o [email protected] + $(call move-if-changed,[email protected],$@) + +all: $(XEN_ROOT)/xen/include/xen/libx86/cpuid-autogen.h +# NB: Use a FORCE target to always rebuild the autogenerated cpuid header +FORCE: +.PHONY: FORCE +endif + # Not xen/xsm as that clashes with link to # $(XEN_ROOT)/xen/include/public/xsm above. xen-xsm/.dir: $(XEN_ROOT)/xen/xsm/flask/policy/mkflask.sh \
tools/libxc/Makefile +0 -9
@@ -147,15 +147,6 @@ $(eval $(genpath-target)) xc_private.h: _paths.h -ifeq ($(CONFIG_X86),y) - -_xc_cpuid_autogen.h: $(XEN_ROOT)/xen/include/public/arch-x86/cpufeatureset.h $(XEN_ROOT)/xen/tools/gen-cpuid.py - $(PYTHON) $(XEN_ROOT)/xen/tools/gen-cpuid.py -i $^ -o [email protected] - $(call move-if-changed,[email protected],$@) - -build: _xc_cpuid_autogen.h -endif - $(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) \ $(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): xc_private.h
tools/libxc/xc_cpuid_x86.c +0 -1
@@ -33,7 +33,6 @@ enum {#define XEN_CPUFEATURE (name, value) X86_FEATURE_##name = value,#include <xen/arch-x86/cpufeatureset.h> }; -#include "_xc_cpuid_autogen.h" #define bitmaskof (idx) (1u << ((idx) & 31))#define featureword_of (idx) ((idx) >> 5)
xen/include/Makefile +3 -3
@@ -142,13 +142,13 @@ endif ifeq ($(XEN_TARGET_ARCH),x86_64) -$(BASEDIR)/include/asm-x86/cpuid-autogen.h: $(BASEDIR)/include/public/arch-x86/cpufeatureset.h $(BASEDIR)/tools/gen-cpuid.py FORCE +$(BASEDIR)/include/xen/libx86/cpuid-autogen.h: $(BASEDIR)/include/public/arch-x86/cpufeatureset.h $(BASEDIR)/tools/gen-cpuid.py FORCE $(PYTHON) $(BASEDIR)/tools/gen-cpuid.py -i $< -o [email protected] $(call move-if-changed,[email protected],$@) -all: $(BASEDIR)/include/asm-x86/cpuid-autogen.h +all: $(BASEDIR)/include/xen/libx86/cpuid-autogen.h endif clean:: rm -rf compat config generated headers*.chk - rm -f $(BASEDIR)/include/asm-x86/cpuid-autogen.h + rm -f $(BASEDIR)/include/xen/libx86/cpuid-autogen.h
xen/include/asm-x86/cpufeatures.h +1 -1
@@ -2,7 +2,7 @@* Explicitly intended for multiple inclusion. */ -#include <asm/cpuid-autogen.h> +#include <xen/libx86/cpuid-autogen.h> #define FSCAPINTS FEATURESET_NR_ENTRIES
xen/include/xen/libx86/cpuid.h +2 -0
@@ -2,6 +2,8 @@ #ifndef XEN_LIBX86_CPUID_H #define XEN_LIBX86_CPUID_H +#include <xen/libx86/cpuid-autogen.h> + struct cpuid_leaf { uint32_t a, b, c, d;
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 02/13] libx86: generate cpuid-autogen.h in the libx86 include dir
Quoted 6 lines · Andrew Cooper
From: Roger Pau Monné <[email protected]>
This prevents having to generate it inside the libxc folder.
Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>
Quoted 8 lines
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>RFC - I'm not sure a parallel build of Xen and the tools works.
There is a potential race that two make processes write to .new file at the same time, isn't there?
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 02/13] libx86: generate cpuid-autogen.h in the libx86 include dir
In reply to Andrew CooperQuoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Roger Pau Monné <[email protected]>
This prevents having to generate it inside the libxc folder.
And this is useful / desirable / necessary because of what?
Quoted 1 line
RFC - I'm not sure a parallel build of Xen and the tools works.
I'm pretty sure it won't with this approach. Considering my comment just given on patch 1, this suggests yet another model as alternative:
Instead of making the directory a symlink, create a real directory in the tools tree, symlink all files, and generate the file there. If the "master" tree sat at the root of the tree, the same would then be doable for the hypervisor build, thus completely decoupling the two instances of building the header.
This would then also deal with the risk of the two rules (intentionally or not) to go out of sync, producing (slightly) different headers.
tools/include/Makefile ifeq ($(CONFIG_X86),y) · line 30
Quoted 8 lines
tools/include/Makefile +2
-0@@ -26,6 +26,17 @@ ifeq ($(CONFIG_X86),y) endif touch $@ +ifeq ($(CONFIG_X86),y) +$(XEN_ROOT)/xen/include/xen/libx86/cpuid-autogen.h: $(XEN_ROOT)/xen/include/public/arch-x86/cpufeatureset.h $(XEN_ROOT)/xen/tools/gen-cpuid.py FORCE
This being an independent rule, I think it needs to depend on xen/.dir.
tools/include/Makefile ifeq ($(CONFIG_X86),y) · line 38
Quoted 8 lines
tools/include/Makefile +8
-0+ $(PYTHON) $(XEN_ROOT)/xen/tools/gen-cpuid.py -i $< -o [email protected] + $(call move-if-changed,[email protected],$@) + +all: $(XEN_ROOT)/xen/include/xen/libx86/cpuid-autogen.h +# NB: Use a FORCE target to always rebuild the autogenerated cpuid header +FORCE: +.PHONY: FORCE +endif
The comment is slightly misleading, I'm afraid: The move-if-changed means the header itself isn't always rebuilt, it's only the intermediate file which is. Perhaps "... to always invoke the autogenerated cpuid header rule"?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 3/13 libx86: Share struct cpuid_policy with userspace
In reply to Andrew CooperFrom: Roger Pau Monné <[email protected]>
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- tools/libxc/xc_cpuid_x86.c | 2 + tools/tests/x86_emulator/x86-emulate.h | 2 + xen/include/asm-x86/cpuid.h | 219 --------------------------------- xen/include/xen/libx86/cpuid.h | 219 +++++++++++++++++++++++++++++++++ 4 files changed, 223 insertions(+), 219 deletions(-)
tools/libxc/xc_cpuid_x86.c +2 -0
@@ -27,6 +27,8 @@#include <xen/hvm/params.h> #include <xen-tools/libs.h> +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +#include <xen/libx86/cpuid.h> enum {
tools/tests/x86_emulator/x86-emulate.h +2 -0
@@ -21,6 +21,8 @@#define ARRAY_SIZE (a) (sizeof(a) / sizeof(*(a))) +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +#if __GNUC__ >4 || (__GNUC__ ==4 && __GNUC_MINOR__ >=6 )/* Force a compilation error if condition is true */ #define BUILD_BUG_ON (cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })
xen/include/asm-x86/cpuid.h +0 -219
231 lines not on this page · raw message
xen/include/xen/libx86/cpuid.h +219 -0
230 lines not on this page · raw message
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 03/13] libx86: Share struct cpuid_policy with userspace
Quoted 24 lines · Andrew Cooper
From: Roger Pau Monné <[email protected]>
No functional change.
Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>--- tools/libxc/xc_cpuid_x86.c | 2 + tools/tests/x86_emulator/x86-emulate.h | 2 + xen/include/asm-x86/cpuid.h | 219 --------------------------------- xen/include/xen/libx86/cpuid.h | 219 +++++++++++++++++++++++++++++++++ 4 files changed, 223 insertions(+), 219 deletions(-)tools/libxc/xc_cpuid_x86.c +0
-0
tools/libxc/xc_cpuid_x86.c line 31
Quoted 5 lines
tools/libxc/xc_cpuid_x86.c +2
-0@@ -27,6 +27,8 @@#include <xen/hvm/params.h> #include <xen-tools/libs.h> +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +
It appears that this is more suitable to be put into include/xen-tools/libs.h
tools/tests/x86_emulator/x86-emulate.h line 25
Quoted 13 lines
tools/libxc/xc_cpuid_x86.c +0
-0#include <xen/libx86/cpuid.h> enum {tools/tests/x86_emulator/x86-emulate.h +2
-0@@ -21,6 +21,8 @@#define ARRAY_SIZE (a) (sizeof(a) / sizeof(*(a))) +#define MAX (x, y) ((x) > (y) ? (x) : (y)) +
And with that libs.h included here you can also delete the BUILD_BUG_ON in this file.
This can be done either in the next version or as a follow-up patch if that's easier.
In any case:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 03/13] libx86: Share struct cpuid_policy with userspace
In reply to Andrew CooperQuoted 7 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Roger Pau Monné <[email protected]>
No functional change.
Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>
This is all fine with me (subject to path name adjustments), but the description is too short. I'd expect this to at least make clear that this is for convenience, not because the structure layouts etc have to match.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 4/13 libx86: introduce a libx86 shared library
In reply to Andrew CooperFrom: Roger Pau Monné <[email protected]>
Move x86_cpuid_lookup_deep_deps() into the shared library, removing the individual copies from the hypervisor and libxc respectively.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- tools/libxc/Makefile | 5 +++++ tools/libxc/include/xenctrl.h | 1 - tools/libxc/xc_cpuid_x86.c | 29 +------------------------ xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/cpuid.c | 32 +-------------------------- xen/common/Makefile | 1 + xen/common/libx86/Makefile | 1 + xen/common/libx86/cpuid.c | 44 ++++++++++++++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 42 ++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/cpuid.h | 2 -- xen/include/xen/libx86/cpuid.h | 2 ++ 11 files changed, 98 insertions(+), 63 deletions(-) create mode 100644 xen/common/libx86/Makefile create mode 100644 xen/common/libx86/cpuid.c create mode 100644 xen/common/libx86/libx86-private.h
tools/libxc/Makefile +5 -0
@@ -80,6 +80,11 @@ GUEST_SRCS-y += $(ELF_SRCS-y) $(patsubst %.c,%.o,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign $(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign +# Add libx86 to the build +vpath %.c ../../xen/common/libx86 + +GUEST_SRCS-$(CONFIG_X86) += cpuid.c + # new domain builder GUEST_SRCS-y += xc_dom_core.c xc_dom_boot.c GUEST_SRCS-y += xc_dom_elfloader.c
tools/libxc/include/xenctrl.h +0 -1
@@ -2547,7 +2547,6 @@ enum xc_static_cpu_featuremask { XC_FEATUREMASK_DEEP_FEATURES, };const uint32_t *xc_get_static_cpu_featuremask (enum xc_static_cpu_featuremask ); -const uint32_t *xc_get_feature_deep_deps (uint32_t feature);#endif
tools/libxc/xc_cpuid_x86.c +1 -28
@@ -133,33 +133,6 @@ const uint32_t *xc_get_static_cpu_featuremask( } } -const uint32_t *xc_get_feature_deep_deps (uint32_t feature) -{ -static const struct { -uint32_t feature; -uint32_t fs[FEATURESET_NR_ENTRIES ]; - } deep_deps[] =INIT_DEEP_DEPS ; - -unsigned int start =0 , end =ARRAY_SIZE (deep_deps); - -BUILD_BUG_ON (ARRAY_SIZE (deep_deps) !=NR_DEEP_DEPS ); - -/* deep_deps[] is sorted. Perform a binary search. */ -while ( start < end ) - { -unsigned int mid = start + ((end - start) /2 ); - -if ( deep_deps[mid].feature > feature ) - end = mid; -else if ( deep_deps[mid].feature < feature ) - start = mid +1 ; -else -return deep_deps[mid].fs; - } - -return NULL ; -} -struct cpuid_domain_info { enum
@@ -679,7 +652,7 @@ static void sanitise_featureset(struct cpuid_domain_info *info)const uint32_t *dfs;if ( !test_bit (b, disabled_features) || - !(dfs =xc_get_feature_deep_deps (b)) ) + !(dfs =x86_cpuid_lookup_deep_deps (b)) )continue ;for ( i =0 ; i <ARRAY_SIZE (disabled_features); ++i )
xen/arch/x86/cpu/common.c +1 -1
@@ -62,7 +62,7 @@ void __init setup_clear_cpu_cap(unsigned int cap) __builtin_return_address(0), cap);__clear_bit (cap, boot_cpu_data.x86_capability); - dfs =lookup_deep_deps (cap); + dfs =x86_cpuid_lookup_deep_deps (cap);if (!dfs)return ;
xen/arch/x86/cpuid.c +1 -31
@@ -97,7 +97,7 @@ static void sanitise_featureset(uint32_t *fs)for_each_set_bit (i, (void *)disabled_features,sizeof (disabled_features) *8 ) { -const uint32_t *dfs =lookup_deep_deps (i); +const uint32_t *dfs =x86_cpuid_lookup_deep_deps (i);unsigned int j;ASSERT (dfs);/* deep_features[] should guarentee this. */
@@ -544,36 +544,6 @@ bool recheck_cpu_features(unsigned int cpu)return okay; } -const uint32_t *lookup_deep_deps (uint32_t feature) -{ -static const struct { -uint32_t feature; -uint32_t fs[FSCAPINTS ]; - } deep_deps[] =INIT_DEEP_DEPS ; -unsigned int start =0 , end =ARRAY_SIZE (deep_deps); - -BUILD_BUG_ON (ARRAY_SIZE (deep_deps) !=NR_DEEP_DEPS ); - -/* Fast early exit. */ -if ( !test_bit (feature, deep_features) ) -return NULL ; - -/* deep_deps[] is sorted. Perform a binary search. */ -while ( start < end ) - { -unsigned int mid = start + ((end - start) /2 ); - -if ( deep_deps[mid].feature > feature ) - end = mid; -else if ( deep_deps[mid].feature < feature ) - start = mid +1 ; -else -return deep_deps[mid].fs; - } - -return NULL ; -} -void recalculate_cpuid_policy (struct domain *d) {struct cpuid_policy *p = d->arch.cpuid;
xen/common/Makefile +1 -0
@@ -80,3 +80,4 @@ subdir-$(CONFIG_UBSAN) += ubsan subdir-y += libelf subdir-$(CONFIG_HAS_DEVICE_TREE) += libfdt +subdir-$(CONFIG_X86) += libx86
xen/common/libx86/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-y += cpuid.o
xen/common/libx86/cpuid.c new file +44 -0
44 lines not on this page · raw message
xen/common/libx86/libx86-private.h new file +42 -0
42 lines not on this page · raw message
xen/include/asm-x86/cpuid.h +0 -2
@@ -17,8 +17,6 @@ extern const uint32_t special_features[FSCAPINTS];void init_guest_cpuid (void ); -const uint32_t *lookup_deep_deps (uint32_t feature); -/* * Expected levelling capabilities (given cpuid vendor/family information), * and levelling capabilities actually available (given MSR probing).
xen/include/xen/libx86/cpuid.h +2 -0
@@ -228,6 +228,8 @@ static inline void cpuid_featureset_to_policy( p->feat._7d0 = fs[FEATURESET_7d0]; } +const uint32_t *x86_cpuid_lookup_deep_deps (uint32_t feature); +#endif /* !XEN_LIBX86_CPUID_H */ /*
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 04/13] libx86: introduce a libx86 shared library
Quoted 35 lines · Andrew Cooper
From: Roger Pau Monné <[email protected]>
Move
x86_cpuid_lookup_deep_deps()into the shared library, removing the individual copies from the hypervisor and libxc respectively.Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>--- tools/libxc/Makefile | 5 +++++ tools/libxc/include/xenctrl.h | 1 - tools/libxc/xc_cpuid_x86.c | 29 +------------------------ xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/cpuid.c | 32 +-------------------------- xen/common/Makefile | 1 + xen/common/libx86/Makefile | 1 + xen/common/libx86/cpuid.c | 44 ++++++++++++++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 42 ++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/cpuid.h | 2 -- xen/include/xen/libx86/cpuid.h | 2 ++ 11 files changed, 98 insertions(+), 63 deletions(-) create mode 100644 xen/common/libx86/Makefile create mode 100644 xen/common/libx86/cpuid.c create mode 100644 xen/common/libx86/libx86-private.htools/libxc/Makefile +0
-0
tools/libxc/Makefile GUEST_SRCS-y += $(ELF_SRCS-y) · line 86
Quoted 7 lines
tools/libxc/Makefile +4
-0@@ -80,6 +80,11 @@ GUEST_SRCS-y += $(ELF_SRCS-y) $(patsubst %.c,%.o,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign $(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign +# Add libx86 to the build +vpath %.c ../../xen/common/libx86 + +GUEST_SRCS-$(CONFIG_X86) += cpuid.c
I think it is better to not expose libx86 to Arm build, so the vpath should be guarded by CONFIG_X86 too.
ifeq ($(CONFIG_X86),y)
vpath ...
GUEST_SRCS-y += ...
endif
The rest looks fine.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 04/13] libx86: introduce a libx86 shared library
In reply to Andrew CooperQuoted 14 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Roger Pau Monné <[email protected]>
Move
x86_cpuid_lookup_deep_deps()into the shared library, removing the individual copies from the hypervisor and libxc respectively.Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
Quoted 10 lines
--- tools/libxc/Makefile | 5 +++++ tools/libxc/include/xenctrl.h | 1 - tools/libxc/xc_cpuid_x86.c | 29 +------------------------ xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/cpuid.c | 32 +-------------------------- xen/common/Makefile | 1 + xen/common/libx86/Makefile | 1 + xen/common/libx86/cpuid.c | 44 ++++++++++++++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 42 ++++++++++++++++++++++++++++++++++++
I'm pretty opposed to redundancies in names like this - why can't this simply be private.h - its placement in the tree makes clear whose private header this is.
Quoted 25 lines
xen/common/libx86/libx86-private.h new file +22
-0@@ -0,0 +1,42 @@ +#ifndef XEN_LIBX86_PRIVATE_H +#define XEN_LIBX86_PRIVATE_H + +#ifdef __XEN__ + +#include <xen/bitops.h> +#include <xen/kernel.h> +#include <xen/lib.h> +#include <xen/types.h> + +#else + +#include <inttypes.h> +#include <stdbool.h> +#include <stddef.h> + +#include <xen-tools/libs.h> + +#define MAX (x, y) ((x) > (y) ? (x) : (y)) + +#define ARRAY_SIZE (x) (sizeof x / sizeof *x) +
xen/common/libx86/libx86-private.h line 32
Quoted 10 lines
xen/common/libx86/libx86-private.h new file +10
-0+static inline bool test_bit (unsigned int bit,const void *vaddr) +{ +const char *addr = vaddr; + +return addr[bit /8 ] & (1u << (bit %8 )); +} + +#endif /* __XEN__ */ + +#endif /* XEN_LIBX86_PRIVATE_H */
Did you consider making this x86-independent but still shared? None of the above is x86-specific. libelf could then possibly use this too, for exampled.
Also I think we pretty consistently use sizeof(x), not sizeof x, so I'd like to ask that ARRAY_SIZE() be adjusted accordingly.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 04/13] libx86: introduce a libx86 shared library
Quoted 40 lines · Jan Beulich
Quoted 26 lines
xen/common/libx86/libx86-private.h new file +22
-0@@ -0,0 +1,42 @@ +#ifndef XEN_LIBX86_PRIVATE_H +#define XEN_LIBX86_PRIVATE_H + +#ifdef __XEN__ + +#include <xen/bitops.h> +#include <xen/kernel.h> +#include <xen/lib.h> +#include <xen/types.h> + +#else + +#include <inttypes.h> +#include <stdbool.h> +#include <stddef.h> + +#include <xen-tools/libs.h> + +#define MAX (x, y) ((x) > (y) ? (x) : (y)) + +#define ARRAY_SIZE (x) (sizeof x / sizeof *x) +xen/common/libx86/libx86-private.h line 32
Quoted 10 lines
xen/common/libx86/libx86-private.h new file +10
-0+static inline bool test_bit (unsigned int bit,const void *vaddr) +{ +const char *addr = vaddr; + +return addr[bit /8 ] & (1u << (bit %8 )); +} + +#endif /* __XEN__ */ + +#endif /* XEN_LIBX86_PRIVATE_H */ Did you consider making this x86-independent but still shared? None of the above is x86-specific. libelf could then possibly use this too, for exampled.
Quoted 2 lines
Also I think we pretty consistently use sizeof(x), not sizeof x, so I'd like to ask that
ARRAY_SIZE()be adjusted accordingly.
In response to several questions across the series, all of the common macros now live in <xen-tools/libs.h> alongside the already-common BUILD_BUG_ON() implementation.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 5/13 libx86: Introduce libx86/msr.h and share msr_{domain, vcpu}_policy with userspace
In reply to Andrew CooperNo functional change.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- tools/libxc/xc_cpuid_x86.c | 1 + xen/include/asm-x86/msr.h | 51 +++----------------------------------- xen/include/xen/libx86/msr.h | 58 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 48 deletions(-) create mode 100644 xen/include/xen/libx86/msr.h
tools/libxc/xc_cpuid_x86.c +1 -0
@@ -30,6 +30,7 @@#define MAX (x, y) ((x) > (y) ? (x) : (y))#include <xen/libx86/cpuid.h> +#include <xen/libx86/msr.h> enum {#define XEN_CPUFEATURE(name, value) X86_FEATURE_##name = value,
xen/include/asm-x86/msr.h +3 -48
69 lines not on this page · raw message
xen/include/xen/libx86/msr.h new file +58 -0
58 lines not on this page · raw message
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 05/13] libx86: Introduce libx86/msr.h and share msr_{domain, vcpu}_policy with userspace
Quoted 3 lines · Andrew Cooper
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 05/13] libx86: Introduce libx86/msr.h and share msr_{domain, vcpu}_policy with userspace
In reply to Andrew CooperQuoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
No functional change.
Signed-off-by Andrew Cooper <[email protected]>
Same as for the earlier patch: Please make clear here that this is for convenience only.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 6/13 libx86: Introduce a helper to serialise a cpuid_policy object
In reply to Andrew CooperThe serialised form is made up of the leaf, subleaf and data tuple. As this is the architectural form, it is expected not to change going forwards.
x86_cpuid_copy_to_buffer() is implemented using Xen's regular copy_to_guest primitives, with an API-compatible memcpy() is used for the libxc half of the build.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- xen/common/libx86/cpuid.c | 94 ++++++++++++++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 17 +++++++ xen/include/public/arch-x86/xen.h | 11 +++++ xen/include/xen/libx86/cpuid.h | 25 ++++++++++ 4 files changed, 147 insertions(+)
xen/common/libx86/cpuid.c +94 -0
@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = { + leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ; + +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry; + +return 0 ; +} + +/* +* Serialise a cpuid_policy object into an array. Writes at most +* CPUID_MAX_SERIALISED_LEAVES. On success, nr_entries_p is updated with the +* actual number of leaves written. +*/ +int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +cpuid_leaf_buffer_t leaves, +uint32_t *nr_entries_p) +{ +const uint32_t nr_entries = *nr_entries_p; +uint32_t curr_entry =0 , leaf, subleaf; + +#define COPY_LEAF (l, s, data) \ + ({ int ret; \ + if ( (ret = copy_leaf_to_buffer( \ + l, s, data, leaves, &curr_entry, nr_entries)) ) \ + return ret; \ + }) + +/* Basic leaves. */ +for ( leaf =0 ; leaf <=MIN (p->basic.max_leaf, +ARRAY_SIZE (p->basic.raw) -1 ); ++leaf ) + { +switch ( leaf ) + { +case 0x4 : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->cache.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->cache.raw[subleaf]); +break ; + +case 0x7 : +for ( subleaf =0 ; + subleaf <=MIN (p->feat.max_subleaf, +ARRAY_SIZE (p->feat.raw) -1 ); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->feat.raw[subleaf]); +break ; + +case 0xb : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->topo.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->topo.raw[subleaf]); +break ; + +case 0xd : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->xstate.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->xstate.raw[subleaf]); +break ; + +default : +COPY_LEAF (leaf,XEN_CPUID_NO_SUBLEAF , &p->basic.raw[leaf]); +break ; + } + } + +COPY_LEAF (0x40000000 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv_limit }); +COPY_LEAF (0x40000100 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv2_limit }); + +/* Extended leaves. */ +for ( leaf =0 ; leaf <=MIN (p->extd.max_leaf &0xfffful , +ARRAY_SIZE (p->extd.raw) -1 ); ++leaf ) +COPY_LEAF (0x80000000 | leaf,XEN_CPUID_NO_SUBLEAF , &p->extd.raw[leaf]); + +#undef COPY_LEAF + + *nr_entries_p = curr_entry; + +return 0 ; +} + +/* * Local variables: * mode: C * c-file-style: "BSD"
xen/common/libx86/libx86-private.h +17 -0
@@ -8,8 +8,13 @@#include <xen/lib.h> #include <xen/types.h> +#include <asm/guest_access.h> + +#define copy_to_buffer_offset copy_to_guest_offset +#else +#include <errno.h> #include <inttypes.h> #include <stdbool.h> #include <stddef.h>
@@ -17,6 +22,7 @@#include <xen-tools/libs.h> #define MAX (x, y) ((x) > (y) ? (x) : (y)) +#define MIN (x, y) ((x) < (y) ? (x) : (y))#define ARRAY_SIZE (x) (sizeof x / sizeof *x)
@@ -27,6 +33,17 @@ static inline bool test_bit(unsigned int bit, const void *vaddr)return addr[bit /8 ] & (1u << (bit %8 )); } +/* memcpy(), but with copy_to_guest_offset()'s API */ +#define copy_to_buffer_offset (dst, index, src, nr) ({ \ + const typeof(*(dst)) *s = (src); \ + unsigned int i; \ + \ + for ( i = 0; i < (nr); i++ ) \ + (dst)[(index) + i] = s[i]; \ + \ + 0; \ +}) +#endif /* __XEN__ */ #endif /* XEN_LIBX86_PRIVATE_H */
xen/include/public/arch-x86/xen.h +11 -0
@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* +* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +#endif /* !__ASSEMBLY__ */ /*
xen/include/xen/libx86/cpuid.h +25 -0
@@ -30,6 +30,19 @@ struct cpuid_leaf#define CPUID_GUEST_NR_EXTD MAX(CPUID_GUEST_NR_EXTD_INTEL, \ CPUID_GUEST_NR_EXTD_AMD) +/* +* Maximum number of leaves a struct cpuid_policy turns into when serialised +* for interaction with the toolstack. (Sum of all leaves in each union, less +* the entries in basic which sub-unions hang off of.) +*/ +#define CPUID_MAX_SERIALISED_LEAVES \ + (CPUID_GUEST_NR_BASIC + \ + CPUID_GUEST_NR_FEAT - !!CPUID_GUEST_NR_FEAT + \ + CPUID_GUEST_NR_CACHE - !!CPUID_GUEST_NR_CACHE + \ + CPUID_GUEST_NR_TOPO - !!CPUID_GUEST_NR_TOPO + \ + CPUID_GUEST_NR_XSTATE - !!CPUID_GUEST_NR_XSTATE + \ + CPUID_GUEST_NR_EXTD + 2 /* hv_limit and hv2_limit */ ) + struct cpuid_policy {#define DECL_BITFIELD (word) _DECL_BITFIELD(FEATURESET_ ## word)
@@ -230,6 +243,18 @@ static inline void cpuid_featureset_to_policy(const uint32_t *x86_cpuid_lookup_deep_deps (uint32_t feature); +#ifdef __XEN__ +#include <public/arch-x86/xen.h> +typedef XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t)cpuid_leaf_buffer_t ; +#else +#include <xen/arch-x86/xen.h> +typedef xen_cpuid_leaf_t *cpuid_leaf_buffer_t ; +#endif + +int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +cpuid_leaf_buffer_t leaves, +uint32_t *nr_entries_p); +#endif /* !XEN_LIBX86_CPUID_H */ /*
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 9 lines · Andrew Cooper
The serialised form is made up of the leaf, subleaf and data tuple. As this is the architectural form, it is expected not to change going forwards.
x86_cpuid_copy_to_buffer()is implemented using Xen's regularcopy_to_guestprimitives, with an API-compatiblememcpy()is used for the libxc half of the build.Signed-off-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>
[...]
Quoted 9 lines
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/*
xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +
Is putting this in public header while leaving CPUID_MAX_SERIALISED_LEAVES in a private header useful?
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 10:42, <[email protected]> wrote:
Quoted 7 lines · Andrew Cooper
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +Is putting this in public header while leaving
CPUID_MAX_SERIALISED_LEAVESin a private header useful?
Quoted 2 lines
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Yeah, this is indeed hard to tell from this patch alone. And going over the titles of the remainder of the series it's also not clear where a use of this structure in a domctl/sysctl would appear.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 10:42, <[email protected]> wrote:
Quoted 7 lines · Andrew Cooper
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +Is putting this in public header while leaving
CPUID_MAX_SERIALISED_LEAVESin a private header useful?
Quoted 5 lines
Quoted 2 lines
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Yeah, this is indeed hard to tell from this patch alone. And going over the titles of the remainder of the series it's also not clear where a use of this structure in a domctl/sysctl would appear.
An array of (some number of) xen_cpuid_leaf_t objects is the externally visible representation of the policy, which is why this definition is externally visible.
Content of this nature will (eventually) exist in the migration stream, and it is also the interface by which toolstacks (libxl and Xapi) can make under-specified modifications to the policies.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 29 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 10:42, <[email protected]> wrote:
Quoted 7 lines · Andrew Cooper
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +Is putting this in public header while leaving
CPUID_MAX_SERIALISED_LEAVESin a private header useful?Quoted 5 lines
Quoted 2 lines
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Yeah, this is indeed hard to tell from this patch alone. And going over the titles of the remainder of the series it's also not clear where a use of this structure in a domctl/sysctl would appear.
Quoted 7 lines
An array of (some number of)
xen_cpuid_leaf_tobjects is the externally visible representation of the policy, which is why this definition is externally visible.Content of this nature will (eventually) exist in the migration stream, and it is also the interface by which toolstacks (libxl and Xapi) can make under-specified modifications to the policies.
Right. An array is needed. How can the caller of x86_cpuid_copy_to_buffer know how big this array is? It needs to pass in an array for the data, right?
Wei.
Quoted 2 lines
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
In reply to Andrew CooperQuoted 30 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 18:23, <[email protected]> wrote:
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 10:42, <[email protected]> wrote:
Quoted 7 lines · Andrew Cooper
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +Is putting this in public header while leaving
CPUID_MAX_SERIALISED_LEAVESin a private header useful?Quoted 5 lines
Quoted 2 lines
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Yeah, this is indeed hard to tell from this patch alone. And going over the titles of the remainder of the series it's also not clear where a use of this structure in a domctl/sysctl would appear.
Quoted 7 lines
An array of (some number of)
xen_cpuid_leaf_tobjects is the externally visible representation of the policy, which is why this definition is externally visible.Content of this nature will (eventually) exist in the migration stream, and it is also the interface by which toolstacks (libxl and Xapi) can make under-specified modifications to the policies.
But then how can a merge be as simple as the one done here? Shouldn't you e.g. match CPUID entries by leaf/subleaf, rather than relying on a given ordering within the arrays?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 18:23, <[email protected]> wrote:
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 10:42, <[email protected]> wrote:
Quoted 7 lines · Andrew Cooper
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +Is putting this in public header while leaving
CPUID_MAX_SERIALISED_LEAVESin a private header useful?Quoted 5 lines
Quoted 2 lines
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Yeah, this is indeed hard to tell from this patch alone. And going over the titles of the remainder of the series it's also not clear where a use of this structure in a domctl/sysctl would appear.
An array of (some number of)
xen_cpuid_leaf_tobjects is the externally visible representation of the policy, which is why this definition is externally visible.
Quoted 6 lines
Quoted 3 lines
Content of this nature will (eventually) exist in the migration stream, and it is also the interface by which toolstacks (libxl and Xapi) can make under-specified modifications to the policies.
But then how can a merge be as simple as the one done here? Shouldn't you e.g. match CPUID entries by leaf/subleaf, rather than relying on a given ordering within the arrays?
I'm confused as to where you think there is any implied ordering within the array.
When serialising the object (this patch), we write the values out in a fixed order because that is the natural way to do it.
When deserialising the object (patch 13), we walk the array linearly filling in the cpuid_policy object based on the leaf/subleaf. The array can have the leaves in any order and this won't affect the resulting deserialisation. (The array can also contain duplicate leaves, which we can't reasonably be detect, and why there is a sanity upper bounds check).
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 05.07.18 at 15:39, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 18:23, <[email protected]> wrote:
Quoted 22 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 10:42, <[email protected]> wrote:
Quoted 7 lines · Andrew Cooper
xen/include/public/arch-x86/xen.h +1
-0@@ -314,6 +314,17 @@ struct xen_arch_domainconfig {#define XEN_ACPI_GPE0_CPUHP_BIT 2#endif +/* xen/include/public/arch-x86/xen.h struct xen_arch_domainconfig { · line 327
Quoted 10 lines
xen/include/public/arch-x86/xen.h +10
-0+* Representations of architectural CPUID information. Used as the +* serialised version of Xen's internal representation. +*/ +typedef struct xen_cpuid_leaf { +#define XEN_CPUID_NO_SUBLEAF 0xffffffffu +uint32_t leaf, subleaf; +uint32_t a, b, c, d; +}xen_cpuid_leaf_t ; +DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +Is putting this in public header while leaving
CPUID_MAX_SERIALISED_LEAVESin a private header useful?Quoted 5 lines
Quoted 2 lines
How does an external user know how big a buffer it needs to provide to accommodate the return structure?
Yeah, this is indeed hard to tell from this patch alone. And going over the titles of the remainder of the series it's also not clear where a use of this structure in a domctl/sysctl would appear.
An array of (some number of)
xen_cpuid_leaf_tobjects is the externally visible representation of the policy, which is why this definition is externally visible.Quoted 6 lines
Quoted 3 lines
Content of this nature will (eventually) exist in the migration stream, and it is also the interface by which toolstacks (libxl and Xapi) can make under-specified modifications to the policies.
But then how can a merge be as simple as the one done here? Shouldn't you e.g. match CPUID entries by leaf/subleaf, rather than relying on a given ordering within the arrays?
I'm confused as to where you think there is any implied ordering within the array.
Quoted 8 lines
When serialising the object (this patch), we write the values out in a fixed order because that is the natural way to do it.
When deserialising the object (patch 13), we walk the array linearly filling in the
cpuid_policyobject based on the leaf/subleaf. The array can have the leaves in any order and this won't affect the resulting deserialisation. (The array can also contain duplicate leaves, which we can't reasonably be detect, and why there is a sanity upper bounds check).
Oh, yes, I see now - I must have been pretty blind. I'm sorry for the noise.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
In reply to Andrew CooperQuoted 16 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/cpuid.c +9
-0@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = {
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 55
Quoted 10 lines
xen/common/libx86/cpuid.c +10
-0+ leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ; + +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry;
Following on from what Wei has said - you don't mean to have a way here then to indicate to a higher up caller how many slots would have been needed?
Quoted 42 lines
xen/common/libx86/cpuid.c +42
-0+int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +cpuid_leaf_buffer_t leaves, +uint32_t *nr_entries_p) +{ +const uint32_t nr_entries = *nr_entries_p; +uint32_t curr_entry =0 , leaf, subleaf; + +#define COPY_LEAF (l, s, data) \ + ({ int ret; \ + if ( (ret = copy_leaf_to_buffer( \ + l, s, data, leaves, &curr_entry, nr_entries)) ) \ + return ret; \ + }) + +/* Basic leaves. */ +for ( leaf =0 ; leaf <=MIN (p->basic.max_leaf, +ARRAY_SIZE (p->basic.raw) -1 ); ++leaf ) + { +switch ( leaf ) + { +case 0x4 : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->cache.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->cache.raw[subleaf]); +break ; + +case 0x7 : +for ( subleaf =0 ; + subleaf <=MIN (p->feat.max_subleaf, +ARRAY_SIZE (p->feat.raw) -1 ); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->feat.raw[subleaf]); +break ; + +case 0xb : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->topo.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->topo.raw[subleaf]); +break ; + +case 0xd : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->xstate.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->xstate.raw[subleaf]); +break ; +
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 116
Quoted 10 lines
xen/common/libx86/cpuid.c +10
-0+default : +COPY_LEAF (leaf,XEN_CPUID_NO_SUBLEAF , &p->basic.raw[leaf]); +break ; + } + } + +COPY_LEAF (0x40000000 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv_limit }); +COPY_LEAF (0x40000100 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv2_limit });
Is it a good idea to produce wrong (zero) EBX, ECX, and EDX values here?
Quoted 4 lines
xen/common/libx86/cpuid.c +0
-0@@ -27,6 +33,17 @@ static inline bool test_bit(unsigned int bit, const void *vaddr)return addr[bit /8 ] & (1u << (bit %8 )); }
xen/common/libx86/cpuid.c static inline bool test_bit(unsigned int bit, const void *vaddr) · line 45
Quoted 10 lines
xen/common/libx86/cpuid.c +10
-0+/* memcpy(), but with copy_to_guest_offset()'s API */ +#define copy_to_buffer_offset (dst, index, src, nr) ({ \ + const typeof(*(dst)) *s = (src); \ + unsigned int i; \ + \ + for ( i = 0; i < (nr); i++ ) \ + (dst)[(index) + i] = s[i]; \ + \ + 0; \ +})
Should you try to avoid multiple evaluation of macro arguments here?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/cpuid.c +14
-0@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = { + leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ;
Quoted 8 lines
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 55
Quoted 5 lines
xen/common/libx86/cpuid.c +5
-0+ +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry;Following on from what Wei has said - you don't mean to have a way here then to indicate to a higher up caller how many slots would have been needed?
I don't understand your query. An individual build has a compile-time static maximum number of leaves, and this number can be obtained in the usual way by making a hypercall with a NULL guest handle.
The external representation must not encode this number, as it will change build to build, hardware to hardware, and in such times as we gain a load of new features in microcode.
Quoted 46 lines
Quoted 46 lines
xen/common/libx86/cpuid.c +45
-0+int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +cpuid_leaf_buffer_t leaves, +uint32_t *nr_entries_p) +{ +const uint32_t nr_entries = *nr_entries_p; +uint32_t curr_entry =0 , leaf, subleaf; + +#define COPY_LEAF (l, s, data) \ + ({ int ret; \ + if ( (ret = copy_leaf_to_buffer( \ + l, s, data, leaves, &curr_entry, nr_entries)) ) \ + return ret; \ + }) + +/* Basic leaves. */ +for ( leaf =0 ; leaf <=MIN (p->basic.max_leaf, +ARRAY_SIZE (p->basic.raw) -1 ); ++leaf ) + { +switch ( leaf ) + { +case 0x4 : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->cache.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->cache.raw[subleaf]); +break ; + +case 0x7 : +for ( subleaf =0 ; + subleaf <=MIN (p->feat.max_subleaf, +ARRAY_SIZE (p->feat.raw) -1 ); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->feat.raw[subleaf]); +break ; + +case 0xb : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->topo.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->topo.raw[subleaf]); +break ; + +case 0xd : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->xstate.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->xstate.raw[subleaf]); +break ; + +default : +COPY_LEAF (leaf,XEN_CPUID_NO_SUBLEAF , &p->basic.raw[leaf]); +break ;
Quoted 8 lines
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 116
Quoted 7 lines
xen/common/libx86/cpuid.c +7
-0+ } + } + +COPY_LEAF (0x40000000 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv_limit }); +COPY_LEAF (0x40000100 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv2_limit });Is it a good idea to produce wrong (zero) EBX, ECX, and EDX values here?
The handling of these leaves currently problematic, and this patch is bug-compatible with how DOMCTL_set_cpuid currently behaves (See update_domain_cpuid_info()).
Annoyingly, I need this marshalling series implemented before I can fix the hypervisor leaves to use the "new" CPUID infrastructure; the main complication being because of the dynamic location of the Xen leaves.
Eventually, the interface will be that Xen leaves live at 0x40000000 and the toolstack can manipulate a subset of the information by providing leaves in the usual manor. To enable viridian, the toolstack writes HyperV's signature at 0x40000000, and Xen's at 0x40000100. This also allows for a mechanism to hide the Xen CPUID leaves by writing a 0 max leaf.
Amongst other things, this will allow sensible control of the Viridian features without having to squeeze more bits into the HVMPARAM.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 31 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/cpuid.c +14
-0@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = { + leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ;Quoted 8 lines
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 55
Quoted 5 lines
xen/common/libx86/cpuid.c +5
-0+ +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry;Following on from what Wei has said - you don't mean to have a way here then to indicate to a higher up caller how many slots would have been needed?
Quoted 3 lines
I don't understand your query. An individual build has a compile-time static maximum number of leaves, and this number can be obtained in the usual way by making a hypercall with a NULL guest handle.
Ah, this is what I was looking for. I think this should be wrapped into a function btw.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 31 lines · Wei Liu
Quoted 30 lines · Andrew Cooper
Quoted 29 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/cpuid.c +9
-0@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = {xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 55
Quoted 10 lines
xen/common/libx86/cpuid.c +10
-0+ leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ; + +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry;Following on from what Wei has said - you don't mean to have a way here then to indicate to a higher up caller how many slots would have been needed?
Quoted 5 lines
Quoted 3 lines
I don't understand your query. An individual build has a compile-time static maximum number of leaves, and this number can be obtained in the usual way by making a hypercall with a NULL guest handle.
Ah, this is what I was looking for. I think this should be wrapped into a function btw.
It is. xc_get_cpumsr_policy_size(), in patch 9 where it is first used.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
In reply to Andrew CooperQuoted 32 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 18:46, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/cpuid.c +14
-0@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = { + leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ;Quoted 8 lines
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 55
Quoted 5 lines
xen/common/libx86/cpuid.c +5
-0+ +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry;Following on from what Wei has said - you don't mean to have a way here then to indicate to a higher up caller how many slots would have been needed?
Quoted 3 lines
I don't understand your query. An individual build has a compile-time static maximum number of leaves, and this number can be obtained in the usual way by making a hypercall with a NULL guest handle.
My point is that this generally is a sub-optimal interface. Seeing how closely tied libxc is to a specific hypervisor build (or at least version), I don't see why the caller couldn't set up a suitably sized array without first querying with a null handle, and only re-issue the call in the unlikely event that actually a larger buffer is necessary.
Quoted 3 lines
The external representation must not encode this number, as it will change build to build, hardware to hardware, and in such times as we gain a load of new features in microcode.
Of course.
Quoted 54 lines
Quoted 45 lines
Quoted 45 lines
xen/common/libx86/cpuid.c +45
-0+int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +cpuid_leaf_buffer_t leaves, +uint32_t *nr_entries_p) +{ +const uint32_t nr_entries = *nr_entries_p; +uint32_t curr_entry =0 , leaf, subleaf; + +#define COPY_LEAF (l, s, data) \ + ({ int ret; \ + if ( (ret = copy_leaf_to_buffer( \ + l, s, data, leaves, &curr_entry, nr_entries)) ) \ + return ret; \ + }) + +/* Basic leaves. */ +for ( leaf =0 ; leaf <=MIN (p->basic.max_leaf, +ARRAY_SIZE (p->basic.raw) -1 ); ++leaf ) + { +switch ( leaf ) + { +case 0x4 : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->cache.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->cache.raw[subleaf]); +break ; + +case 0x7 : +for ( subleaf =0 ; + subleaf <=MIN (p->feat.max_subleaf, +ARRAY_SIZE (p->feat.raw) -1 ); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->feat.raw[subleaf]); +break ; + +case 0xb : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->topo.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->topo.raw[subleaf]); +break ; + +case 0xd : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->xstate.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->xstate.raw[subleaf]); +break ; + +default : +COPY_LEAF (leaf,XEN_CPUID_NO_SUBLEAF , &p->basic.raw[leaf]); +break ;Quoted 8 lines
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 116
Quoted 7 lines
xen/common/libx86/cpuid.c +7
-0+ } + } + +COPY_LEAF (0x40000000 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv_limit }); +COPY_LEAF (0x40000100 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv2_limit });Is it a good idea to produce wrong (zero) EBX, ECX, and EDX values here?
Quoted 7 lines
The handling of these leaves currently problematic, and this patch is bug-compatible with how
DOMCTL_set_cpuidcurrently behaves (Seeupdate_domain_cpuid_info()).Annoyingly, I need this marshalling series implemented before I can fix the hypervisor leaves to use the "new" CPUID infrastructure; the main complication being because of the dynamic location of the Xen leaves.
Well, okay, but I'd prefer if such restrictions and bug-compatibilities were spelled out in the commit message.
Quoted 8 lines
Eventually, the interface will be that Xen leaves live at
0x40000000and the toolstack can manipulate a subset of the information by providing leaves in the usual manor. To enable viridian, the toolstack writes HyperV's signature at0x40000000, and Xen's at0x40000100. This also allows for a mechanism to hide the Xen CPUID leaves by writing a 0 max leaf.Amongst other things, this will allow sensible control of the Viridian features without having to squeeze more bits into the HVMPARAM.
Ah, interesting - you basically mean to deprecate the current way of configuring Viridian features then, if I get this right?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 06/13] libx86: Introduce a helper to serialise a cpuid_policy object
Quoted 31 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 18:46, <[email protected]> wrote:
Quoted 29 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/cpuid.c +9
-0@@ -34,6 +34,100 @@ const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) }/* +* Copy a single cpuid_leaf into a provided xen_cpuid_leaf_t buffer, +* performing boundary checking against the buffer size. +*/ +static int copy_leaf_to_buffer (uint32_t leaf,uint32_t subleaf, +const struct cpuid_leaf *data, +cpuid_leaf_buffer_t leaves, +uint32_t *curr_entry,const uint32_t nr_entries) +{ +const xen_cpuid_leaf_t val = {xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 55
Quoted 10 lines
xen/common/libx86/cpuid.c +10
-0+ leaf, subleaf, data->a, data->b, data->c, data->d, + }; + +if ( *curr_entry == nr_entries ) +return -ENOBUFS ; + +if (copy_to_buffer_offset (leaves, *curr_entry, &val,1 ) ) +return -EFAULT ; + + ++*curr_entry;Following on from what Wei has said - you don't mean to have a way here then to indicate to a higher up caller how many slots would have been needed?
Quoted 8 lines
Quoted 3 lines
I don't understand your query. An individual build has a compile-time static maximum number of leaves, and this number can be obtained in the usual way by making a hypercall with a NULL guest handle.
My point is that this generally is a sub-optimal interface. Seeing how closely tied libxc is to a specific hypervisor build (or at least version), I don't see why the caller couldn't set up a suitably sized array without first querying with a null handle, and only re-issue the call in the unlikely event that actually a larger buffer is necessary.
I'm all for good interface design, but what you describe isn't plausibly going to happen.
Code using the raw hypercall accessors in this series have no idea what size the buffers need to be, and always need to explicitly ask Xen.
Code in one of the followup series which allows for manipulation of the policy objects entirely in the toolstack will use its own static idea of the size of the policies, and never needs to ask Xen. (At this point, if you've got a mismatched Xen and Libxc, then tough - you've got no option but to recompile.)
Anything else is unnecessary extra complexity.
Quoted 58 lines
Quoted 58 lines
Quoted 46 lines
Quoted 46 lines
xen/common/libx86/cpuid.c +45
-0+int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +cpuid_leaf_buffer_t leaves, +uint32_t *nr_entries_p) +{ +const uint32_t nr_entries = *nr_entries_p; +uint32_t curr_entry =0 , leaf, subleaf; + +#define COPY_LEAF (l, s, data) \ + ({ int ret; \ + if ( (ret = copy_leaf_to_buffer( \ + l, s, data, leaves, &curr_entry, nr_entries)) ) \ + return ret; \ + }) + +/* Basic leaves. */ +for ( leaf =0 ; leaf <=MIN (p->basic.max_leaf, +ARRAY_SIZE (p->basic.raw) -1 ); ++leaf ) + { +switch ( leaf ) + { +case 0x4 : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->cache.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->cache.raw[subleaf]); +break ; + +case 0x7 : +for ( subleaf =0 ; + subleaf <=MIN (p->feat.max_subleaf, +ARRAY_SIZE (p->feat.raw) -1 ); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->feat.raw[subleaf]); +break ; + +case 0xb : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->topo.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->topo.raw[subleaf]); +break ; + +case 0xd : +for ( subleaf =0 ; subleaf <ARRAY_SIZE (p->xstate.raw); ++subleaf ) +COPY_LEAF (leaf, subleaf, &p->xstate.raw[subleaf]); +break ; + +default : +COPY_LEAF (leaf,XEN_CPUID_NO_SUBLEAF , &p->basic.raw[leaf]); +break ;Quoted 8 lines
xen/common/libx86/cpuid.c const uint32_t *x86_cpuid_lookup_deep_deps(uint32_t feature) · line 116
Quoted 7 lines
xen/common/libx86/cpuid.c +7
-0+ } + } + +COPY_LEAF (0x40000000 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv_limit }); +COPY_LEAF (0x40000100 ,XEN_CPUID_NO_SUBLEAF , + &(struct cpuid_leaf ){ p->hv2_limit });Is it a good idea to produce wrong (zero) EBX, ECX, and EDX values here?
The handling of these leaves currently problematic, and this patch is bug-compatible with how
DOMCTL_set_cpuidcurrently behaves (Seeupdate_domain_cpuid_info()).
Quoted 5 lines
Quoted 3 lines
Annoyingly, I need this marshalling series implemented before I can fix the hypervisor leaves to use the "new" CPUID infrastructure; the main complication being because of the dynamic location of the Xen leaves.
Well, okay, but I'd prefer if such restrictions and bug-compatibilities were spelled out in the commit message.
I'll do that, and leave a /* TODO */ here.
Quoted 10 lines
Quoted 8 lines
Eventually, the interface will be that Xen leaves live at
0x40000000and the toolstack can manipulate a subset of the information by providing leaves in the usual manor. To enable viridian, the toolstack writes HyperV's signature at0x40000000, and Xen's at0x40000100. This also allows for a mechanism to hide the Xen CPUID leaves by writing a 0 max leaf.Amongst other things, this will allow sensible control of the Viridian features without having to squeeze more bits into the HVMPARAM.
Ah, interesting - you basically mean to deprecate the current way of configuring Viridian features then, if I get this right?
Correct. The xl.cfg interface can remain the same, but this new libxc/Xen interface will be far more flexible than the current "all or nothing" approach.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 7/13 libx86: Introduce a helper to serialise msr_{domain, vcpu}_policy objects
In reply to Andrew CooperFrom: Roger Pau Monné <[email protected]>
As with CPUID, the an architectural form is used for representing the MSR data. It is expected not to change moving forwards, but does have a 32 bit field (currently reserved) which can be used compatibly if needs be.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- tools/include/Makefile | 1 + tools/libxc/Makefile | 2 +- xen/common/libx86/Makefile | 1 + xen/common/libx86/libx86-private.h | 3 ++ xen/common/libx86/msr.c | 71 ++++++++++++++++++++++++++++++++++++++ xen/include/public/arch-x86/xen.h | 9 ++++- xen/include/xen/libx86/msr.h | 16 +++++++++ 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 xen/common/libx86/msr.c
tools/include/Makefile +1 -0
@@ -22,6 +22,7 @@ xen/.dir: ln -s ../xen-foreign xen/foreign ln -sf $(XEN_ROOT)/xen/include/acpi acpi ifeq ($(CONFIG_X86),y) + ln -sf $(XEN_ROOT)/xen/include/asm-x86 xen/asm ln -sf $(XEN_ROOT)/xen/include/xen/libx86 xen/libx86 endif touch $@
tools/libxc/Makefile +1 -1
@@ -83,7 +83,7 @@ $(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign # Add libx86 to the build vpath %.c ../../xen/common/libx86 -GUEST_SRCS-$(CONFIG_X86) += cpuid.c +GUEST_SRCS-$(CONFIG_X86) += cpuid.c msr.c # new domain builder GUEST_SRCS-y += xc_dom_core.c xc_dom_boot.c
xen/common/libx86/Makefile +1 -0
@@ -1 +1,2 @@ obj-y += cpuid.o +obj-y += msr.o
xen/common/libx86/libx86-private.h +3 -0
@@ -9,6 +9,7 @@#include <xen/types.h> #include <asm/guest_access.h> +#include <asm/msr-index.h> #define copy_to_buffer_offset copy_to_guest_offset
@@ -19,6 +20,8 @@#include <stdbool.h> #include <stddef.h> +#include <xen/asm/msr-index.h> +#include <xen-tools/libs.h> #define MAX (x, y) ((x) > (y) ? (x) : (y))
xen/common/libx86/msr.c new file +71 -0
71 lines not on this page · raw message
xen/include/public/arch-x86/xen.h +8 -1
@@ -315,7 +315,7 @@ struct xen_arch_domainconfig {#endif /* -* Representations of architectural CPUID information. Used as the +* Representations of architectural CPUID and MSR information. Used as the * serialised version of Xen's internal representation. */ typedef struct xen_cpuid_leaf {
@@ -325,6 +325,13 @@ typedef struct xen_cpuid_leaf { } xen_cpuid_leaf_t;DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +typedef struct xen_msr_entry { +uint32_t idx; +uint32_t flags;/* Reserved MBZ. */ +uint64_t val; +}xen_msr_entry_t ; +DEFINE_XEN_GUEST_HANDLE (xen_msr_entry_t); +#endif /* !__ASSEMBLY__ */ /*
xen/include/xen/libx86/msr.h +16 -0
@@ -2,6 +2,10 @@#ifndef XEN_LIBX86_MSR_H #define XEN_LIBX86_MSR_H +#define MSR_MAX_SERIALISED_ENTRIES \ + (1 + /* msr_domain_policy */ \ + 2 /* msr_vcpu_policy*/ ) +/* MSR policy object for shared per-domain MSRs */ struct msr_domain_policy {
@@ -45,6 +49,18 @@ struct msr_vcpu_policy } misc_features_enables; }; +#ifdef __XEN__ +#include <public/arch-x86/xen.h> +typedef XEN_GUEST_HANDLE_64 (xen_msr_entry_t)msr_entry_buffer_t ; +#else +#include <xen/arch-x86/xen.h> +typedef xen_msr_entry_t *msr_entry_buffer_t ; +#endif + +int x86_msr_copy_to_buffer (const struct msr_domain_policy *dp, +const struct msr_vcpu_policy *vp, +msr_entry_buffer_t msrs,uint32_t *nr_entries_p); +#endif /* !XEN_LIBX86_MSR_H */ /*
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 07/13] libx86: Introduce a helper to serialise msr_{domain, vcpu}_policy objects
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Roger Pau Monné <[email protected]>
As with CPUID, the an architectural form is used for representing the MSR
Stray "the".
Apart from that same remark(s) as for the respective CPUID change, plus ...
xen/include/public/arch-x86/xen.h typedef struct xen_cpuid_leaf { · line 330
Quoted 7 lines
xen/include/public/arch-x86/xen.h +3
-0@@ -325,6 +325,13 @@ typedef struct xen_cpuid_leaf { } xen_cpuid_leaf_t;DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +typedef struct xen_msr_entry { + uint32_t idx; + uint32_t flags; /* Reserved MBZ. */
... it remains unclear in this patch whose responsibility it is to clear this field. I wonder whether copy_msr_to_buffer() should make sure of this.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 07/13] libx86: Introduce a helper to serialise msr_{domain, vcpu}_policy objects
Quoted 10 lines · Jan Beulich
xen/include/public/arch-x86/xen.h typedef struct xen_cpuid_leaf { · line 330
Quoted 8 lines
xen/include/public/arch-x86/xen.h +3
-0@@ -325,6 +325,13 @@ typedef struct xen_cpuid_leaf { } xen_cpuid_leaf_t;DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +typedef struct xen_msr_entry { + uint32_t idx; + uint32_t flags; /* Reserved MBZ. */... it remains unclear in this patch whose responsibility it is to clear this field. I wonder whether
copy_msr_to_buffer()should make sure of this.
copy_msr_to_buffer() does explicitly zero this field.
Quoted 1 line
+ const
xen_msr_entry_tent = { .idx = idx, .val = val };
guarantees that the flags field is 0.
FWIW, an earlier version of this series has an AVAILABLE bit specified in the flags field, but attempting to the marshalling code work sensibly is why I eventually decided to write c/s 2df1d2ba132ff "x86/msr: Drop {MISC_ENABLES,PLATFORM_INFO}.available" as an alternative fix for the problem.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 07/13] libx86: Introduce a helper to serialise msr_{domain, vcpu}_policy objects
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 18:56, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/include/public/arch-x86/xen.h typedef struct xen_cpuid_leaf { · line 330
Quoted 8 lines
xen/include/public/arch-x86/xen.h +3
-0@@ -325,6 +325,13 @@ typedef struct xen_cpuid_leaf { } xen_cpuid_leaf_t;DEFINE_XEN_GUEST_HANDLE (xen_cpuid_leaf_t); +typedef struct xen_msr_entry { + uint32_t idx; + uint32_t flags; /* Reserved MBZ. */... it remains unclear in this patch whose responsibility it is to clear this field. I wonder whether
copy_msr_to_buffer()should make sure of this.
Quoted 5 lines
copy_msr_to_buffer()does explicitly zero this field.Quoted 1 line
+ const
xen_msr_entry_tent = { .idx = idx, .val = val };guarantees that the flags field is 0.
Oh, I'm sorry - so it does. I had searched the patch for instances of "flags" only ...
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 8/13 x86: Collect policies together into groups
In reply to Andrew CooperThis is mainly prep work for the following patch, but this specific abstraction is also specifically useful for the future auditing logic.
Not all of msr_vcpu_policy will be interesting from a domain building perspective, but some soon-to-appear fields will be (SGX Launch Hash specifically). The exact split of MSRs between domain and vcpu policies is internal to Xen and liable to change moving forwards, so treat the two structures consistently from the start to avoid problems in the future.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Ian Jackson <[email protected]>
--- xen/arch/x86/msr.c | 4 +++- xen/arch/x86/sysctl.c | 23 +++++++++++++++++++++++ xen/include/asm-x86/cpuid.h | 3 +++ xen/include/asm-x86/msr.h | 16 +++++++++------- xen/include/xen/libx86/policies.h | 25 +++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 xen/include/xen/libx86/policies.h
xen/arch/x86/msr.c +3 -1
@@ -31,7 +31,9 @@ struct msr_domain_policy __read_mostly raw_msr_domain_policy,__read_mostly hvm_max_msr_domain_policy,__read_mostly pv_max_msr_domain_policy; -struct msr_vcpu_policy __read_mostly hvm_max_msr_vcpu_policy, +struct msr_vcpu_policy __read_mostly raw_msr_vcpu_policy, +__read_mostly host_msr_vcpu_policy, +__read_mostly hvm_max_msr_vcpu_policy,__read_mostly pv_max_msr_vcpu_policy;static void __init calculate_raw_policy (void )
xen/arch/x86/sysctl.c +23 -0
@@ -31,6 +31,29 @@#include <asm/psr.h> #include <asm/cpuid.h> +const struct policy_group system_policies[] = { + { + &raw_cpuid_policy, + &raw_msr_domain_policy, + &raw_msr_vcpu_policy, + }, + { + &host_cpuid_policy, + &host_msr_domain_policy, + &host_msr_vcpu_policy, + }, + { + &pv_max_cpuid_policy, + &pv_max_msr_domain_policy, + &pv_max_msr_vcpu_policy, + }, + { + &hvm_max_cpuid_policy, + &hvm_max_msr_domain_policy, + &hvm_max_msr_vcpu_policy, + }, +}; +struct l3_cache_info {int ret;unsigned long size;
xen/include/asm-x86/cpuid.h +3 -0
@@ -9,6 +9,7 @@#include <xen/kernel.h> #include <xen/libx86/cpuid.h> +#include <xen/libx86/policies.h> #include <public/sysctl.h>
@@ -50,6 +51,8 @@ extern struct cpuidmasks cpuidmask_defaults;extern struct cpuid_policy raw_cpuid_policy, host_cpuid_policy, pv_max_cpuid_policy, hvm_max_cpuid_policy; +extern const struct policy_group system_policies[]; +/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu);
xen/include/asm-x86/msr.h +9 -7
@@ -260,13 +260,15 @@ static inline void wrmsr_tsc_aux(uint32_t val) } } -/* RAW msr domain policy: contains the actual values from H/W MSRs */ -extern struct msr_domain_policy raw_msr_domain_policy; -/* -* HOST msr domain policy: features that Xen actually decided to use, -* a subset of RAW policy. -*/ -extern struct msr_domain_policy host_msr_domain_policy; +extern struct msr_domain_policy __read_mostly raw_msr_domain_policy, +__read_mostly host_msr_domain_policy, +__read_mostly hvm_max_msr_domain_policy, +__read_mostly pv_max_msr_domain_policy; + +extern struct msr_vcpu_policy __read_mostly raw_msr_vcpu_policy, +__read_mostly host_msr_vcpu_policy, +__read_mostly hvm_max_msr_vcpu_policy, +__read_mostly pv_max_msr_vcpu_policy;void init_guest_msr_policy (void );int init_domain_msr_policy (struct domain *d);
xen/include/xen/libx86/policies.h new file +25 -0
@@ -0,0 +1,25 @@ +/* Common data structures and functions consumed by hypervisor and toolstack */ +#ifndef XEN_LIBX86_POLICIES_H +#define XEN_LIBX86_POLICIES_H + +#include <xen/libx86/cpuid.h> +#include <xen/libx86/msr.h> + +struct policy_group +{ +struct cpuid_policy *cp; +struct msr_domain_policy *dp; +struct msr_vcpu_policy *vp; +}; + +#endif /* !XEN_LIBX86_POLICIES_H */ + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 08/13] x86: Collect policies together into groups
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
This is mainly prep work for the following patch, but this specific abstraction is also specifically useful for the future auditing logic.
Not all of
msr_vcpu_policywill be interesting from a domain building perspective, but some soon-to-appear fields will be (SGX Launch Hash specifically). The exact split of MSRs between domain and vcpu policies is internal to Xen and liable to change moving forwards, so treat the two structures consistently from the start to avoid problems in the future.
I hope the utility of this grouping will become evident in later patches.
Quoted 18 lines
xen/arch/x86/sysctl.c +12
-0@@ -31,6 +31,29 @@#include <asm/psr.h> #include <asm/cpuid.h> +const struct policy_group system_policies[] = { + { + &raw_cpuid_policy, + &raw_msr_domain_policy, + &raw_msr_vcpu_policy, + }, + { + &host_cpuid_policy, + &host_msr_domain_policy, + &host_msr_vcpu_policy, + }, + {
xen/arch/x86/sysctl.c line 55
Quoted 10 lines
xen/arch/x86/sysctl.c +10
-0+ &pv_max_cpuid_policy, + &pv_max_msr_domain_policy, + &pv_max_msr_vcpu_policy, + }, + { + &hvm_max_cpuid_policy, + &hvm_max_msr_domain_policy, + &hvm_max_msr_vcpu_policy, + }, +};
This being non-static I immediately wonder how correct indexing into this array is supposed to work. No enum or alike is being added here.
Quoted 6 lines
xen/include/xen/libx86/policies.h new file +3
-0@@ -0,0 +1,25 @@ +/* Common data structures and functions consumed by hypervisor and toolstack */ +#ifndef XEN_LIBX86_POLICIES_H +#define XEN_LIBX86_POLICIES_H
xen/include/xen/libx86/policies.h line 13
Quoted 10 lines
xen/include/xen/libx86/policies.h new file +10
-0+ +#include <xen/libx86/cpuid.h> +#include <xen/libx86/msr.h> + +struct policy_group +{ +struct cpuid_policy *cp; +struct msr_domain_policy *dp; +struct msr_vcpu_policy *vp; +};
This is sort of surprising without any libxc change here, and hence without any use of the type in libxc, but perhaps that'll surface later on.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 08/13] x86: Collect policies together into groups
Quoted 11 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
This is mainly prep work for the following patch, but this specific abstraction is also specifically useful for the future auditing logic.
Not all of
msr_vcpu_policywill be interesting from a domain building perspective, but some soon-to-appear fields will be (SGX Launch Hash specifically). The exact split of MSRs between domain and vcpu policies is internal to Xen and liable to change moving forwards, so treat the two structures consistently from the start to avoid problems in the future.I hope the utility of this grouping will become evident in later patches.
Probably moreso in the followup series with the auditing/subset logic, but patch 13 does provide the first aspect.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 08/13] x86: Collect policies together into groups
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 19:15, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
This is mainly prep work for the following patch, but this specific abstraction is also specifically useful for the future auditing logic.
Not all of
msr_vcpu_policywill be interesting from a domain building perspective, but some soon-to-appear fields will be (SGX Launch Hash specifically). The exact split of MSRs between domain and vcpu policies is internal to Xen and liable to change moving forwards, so treat the two structures consistently from the start to avoid problems in the future.
Quoted 5 lines
Quoted 2 lines
I hope the utility of this grouping will become evident in later patches.
Probably moreso in the followup series with the auditing/subset logic, but patch 13 does provide the first aspect.
Does it? It's a faint hint at best, as long as x86_policies_are_compatible() is all empty.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 9/13 x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy
In reply to Andrew CooperFrom: Sergey Dyasli <[email protected]>
Provide a SYSCTL for the toolstack to obtain complete system CPUID and MSR policy information. The split of default vs max policies is introduced into the API, including a description of the intended behaviour. For now, max is the default, but this is intended to change moving forwards.
For the XSM side of things, this subop is closely related to {phys,cputopo,numa}info, so shares the physinfo access vector.
Extend the xen-cpuid utility to be able to dump the system policies. An example output is:
Xen reports there are maximum 113 leaves and 3 MSRs
Raw policy: 93 leaves, 3 MSRs
CPUID:
leaf subleaf -> eax ebx ecx edx
00000000:ffffffff -> 0000000d:756e6547:6c65746e:49656e69
00000001:ffffffff -> 000306c3:00100800:7ffafbff:bfebfbff
00000002:ffffffff -> 76036301:00f0b5ff:00000000:00c10000
00000004:00000000 -> 1c004121:01c0003f:0000003f:00000000
00000004:00000001 -> 1c004122:01c0003f:0000003f:00000000
00000004:00000002 -> 1c004143:01c0003f:000001ff:00000000
00000004:00000003 -> 1c03c163:03c0003f:00001fff:00000006
00000005:ffffffff -> 00000040:00000040:00000003:00042120
00000006:ffffffff -> 00000077:00000002:00000009:00000000
00000007:00000000 -> 00000000:000027ab:00000000:9c000000
0000000a:ffffffff -> 07300403:00000000:00000000:00000603
0000000b:00000000 -> 00000001:00000002:00000100:00000000
0000000b:00000001 -> 00000004:00000008:00000201:00000000
0000000d:00000000 -> 00000007:00000340:00000340:00000000
0000000d:00000001 -> 00000001:00000000:00000000:00000000
0000000d:00000002 -> 00000100:00000240:00000000:00000000
80000000:ffffffff -> 80000008:00000000:00000000:00000000
80000001:ffffffff -> 00000000:00000000:00000021:2c100800
80000002:ffffffff -> 65746e49:2952286c:6f655820:2952286e
80000003:ffffffff -> 55504320:2d334520:30343231:20337620
80000004:ffffffff -> 2e332040:48473034:0000007a:00000000
80000006:ffffffff -> 00000000:00000000:01006040:00000000
80000007:ffffffff -> 00000000:00000000:00000000:00000100
80000008:ffffffff -> 00003027:00000000:00000000:00000000
MSRs:
index -> value
000000ce -> 0000000080000000
00000048 -> 0000000000000000
00000140 -> 0000000000000000---
CC: Jan Beulich <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Daniel De Graaf <[email protected]>
--- tools/libxc/include/xenctrl.h | 6 +++ tools/libxc/xc_cpuid_x86.c | 59 ++++++++++++++++++++++++ tools/misc/xen-cpuid.c | 89 +++++++++++++++++++++++++++++++++++-- xen/arch/x86/sysctl.c | 86 +++++++++++++++++++++++++++++++++-- xen/include/public/sysctl.h | 41 +++++++++++++++++ xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 2 +- 7 files changed, 275 insertions(+), 9 deletions(-)
tools/libxc/include/xenctrl.h +6 -0
@@ -2536,6 +2536,12 @@ int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);int xc_get_cpu_featureset (xc_interface *xch,uint32_t index,uint32_t *nr_features,uint32_t *featureset); +int xc_get_cpumsr_policy_size (xc_interface *xch,uint32_t *nr_leaves, +uint32_t *nr_msrs); +int xc_get_system_cpumsr_policy (xc_interface *xch,uint32_t index, +uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves, +uint32_t *nr_msrs,xen_msr_entry_t *msrs); +uint32_t xc_get_cpu_featureset_size (void );enum xc_static_cpu_featuremask {
tools/libxc/xc_cpuid_x86.c +59 -0
65 lines not on this page · raw message
tools/misc/xen-cpuid.c +85 -4
124 lines not on this page · raw message
xen/arch/x86/sysctl.c +82 -4
110 lines not on this page · raw message
xen/include/public/sysctl.h +41 -0
59 lines not on this page · raw message
xen/xsm/flask/hooks.c +1 -0
@@ -801,6 +801,7 @@ static int flask_sysctl(int cmd)case XEN_SYSCTL_cputopoinfo:case XEN_SYSCTL_numainfo:case XEN_SYSCTL_pcitopoinfo: +case XEN_SYSCTL_get_cpumsr_policy:return domain_has_xen (current->domain,XEN__PHYSINFO );case XEN_SYSCTL_psr_cmt_op:
xen/xsm/flask/policy/access_vectors +1 -1
@@ -28,7 +28,7 @@ class xen # XENPF_microcode_update microcode # XEN_SYSCTL_physinfo, XEN_SYSCTL_cputopoinfo, XEN_SYSCTL_numainfo -# XEN_SYSCTL_pcitopoinfo +# XEN_SYSCTL_pcitopoinfo, XEN_SYSCTL_get_cpumsr_policy physinfo # XENPF_platform_quirk quirk
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 09/13] x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy
Oh, here we go - the title doesn't suggest this is about CPUID as well.
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
Extend the xen-cpuid utility to be able to dump the system policies. An example output is:
Xen reports there are maximum 113 leaves and 3 MSRs Raw policy: 93 leaves, 3 MSRs
CPUID:
leaf subleaf -> eax ebx ecx edx
00000000:ffffffff -> 0000000d:756e6547:6c65746e:49656e69
I'd like to suggest to suppress the :fffffff when there are no sub-leaves.
tools/misc/xen-cpuid.c int main(int argc, char **argv) · line 461
Quoted 6 lines
tools/misc/xen-cpuid.c +1
-1@@ -377,7 +458,7 @@ int main(int argc, char **argv)if ( i == nr_features )break ; -if ( *ptr ==':' ) +if ( *ptr ==':' || *ptr =='-' )
None of the other changes to this file give any hint why a dash needs recognizing here all of the sudden. Is this a stray / leftover change?
xen/arch/x86/sysctl.c line 35
Quoted 8 lines
xen/arch/x86/sysctl.c +1
-1@@ -32,22 +32,32 @@#include <asm/cpuid.h> const struct policy_group system_policies[] = { - { + [ XEN_SYSCTL_cpumsr_policy_raw ] = {
Aha - this clarifies a question I had on the earlier patch. But it would be nice if that other patch was self contained also in the way of allowing readers to understand the intentions. And with this I now wonder whether the pointers in struct policy_group shouldn't all be const qualified.
Quoted 5 lines
xen/arch/x86/sysctl.c +1
-0@@ -318,6 +328,74 @@ long arch_do_sysctl(break ; } +case XEN_SYSCTL_get_cpumsr_policy:
xen/arch/x86/sysctl.c long arch_do_sysctl( · line 341
Quoted 10 lines
xen/arch/x86/sysctl.c +10
-0+ { +const struct policy_group *group; + +/* Bad policy index? */ +if ( sysctl->u.cpumsr_policy.index >=ARRAY_SIZE (system_policies) ) + { + ret = -EINVAL ; +break ; + } + group = &system_policies[sysctl->u.cpumsr_policy.index];
Isn't this introducing at least half of a Spectre v1 gadget?
Quoted 20 lines
xen/arch/x86/sysctl.c +20
-0+/* Request for maximum number of leaves/MSRs? */ +if (guest_handle_is_null (sysctl->u.cpumsr_policy.cpuid_policy) ) + { + sysctl->u.cpumsr_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ; +if (__copy_field_to_guest (u_sysctl, sysctl, + u.cpumsr_policy.nr_leaves) ) + { + ret = -EFAULT ; +break ; + } + } +if (guest_handle_is_null (sysctl->u.cpumsr_policy.msr_policy) ) + { + sysctl->u.cpumsr_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ; +if (__copy_field_to_guest (u_sysctl, sysctl, + u.cpumsr_policy.nr_msrs) ) + { + ret = -EFAULT ; +break ; + }
xen/arch/x86/sysctl.c long arch_do_sysctl( · line 372
Quoted 10 lines
xen/arch/x86/sysctl.c +10
-0+ } + +/* Serialise the information the caller wants. */ +if ( !guest_handle_is_null (sysctl->u.cpumsr_policy.cpuid_policy) ) + { +if ( (ret =x86_cpuid_copy_to_buffer ( + group->cp, + sysctl->u.cpumsr_policy.cpuid_policy, + &sysctl->u.cpumsr_policy.nr_leaves)) ) +break ;
Coming back to an earlier question, I realize the null handle logic above is supposed to allow sizing the buffers, but I think it would be better to allow single invocations to generally work, making a second invocation necessary just as a fallback. IOW I think the code here wants to return to the caller the required number of slots in case of -ENOBUFS. And it should the also ...
Quoted 4 lines
xen/arch/x86/sysctl.c +4
-0+if (__copy_field_to_guest (u_sysctl, sysctl, + u.cpumsr_policy.nr_leaves) ) + { + ret = -EFAULT ;
xen/arch/x86/sysctl.c long arch_do_sysctl( · line 387
Quoted 10 lines
xen/arch/x86/sysctl.c +10
-0+break ; + } + } +if ( !guest_handle_is_null (sysctl->u.cpumsr_policy.msr_policy) ) + { +if ( (ret =x86_msr_copy_to_buffer ( + group->dp, group->vp, + sysctl->u.cpumsr_policy.msr_policy, + &sysctl->u.cpumsr_policy.nr_msrs)) ) +break ;
... be allowed to reach here despite the earlier error.
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1068
Quoted 9 lines
xen/include/public/sysctl.h +3
-0@@ -1063,6 +1063,43 @@ struct xen_sysctl_set_parameter {uint16_t pad[3 ];/* IN: MUST be zero. */ }; +#if defined(__i386__) || defined(__x86_64__) +/* +* XEN_SYSCTL_get_cpumsr_policy (x86 specific)
Perhaps express the "x86 specific" also in the opcode name? And make more obvious that this is about CPUID and MSRs at the same time? E.g. XEN_SYSCTL_x86_get_cpuid_msr_policy?
I'm sure you have reasons to munge it all into a single operation.
Quoted 17 lines
xen/include/public/sysctl.h +17
-0+* Return information about CPUID and MSR policies available on this host. +* - Raw: The real H/W values. +* - Host: The values Xen is using, (after command line overrides, etc). +* - Max_*: Maximum set of features a PV or HVM guest can use. Includes +* experimental features outside of security support. +* - Default_*: Default set of features a PV or HVM guest can use. This is +* the security supported set. +*/ +struct xen_sysctl_cpumsr_policy { +#define XEN_SYSCTL_cpumsr_policy_raw 0 +#define XEN_SYSCTL_cpumsr_policy_host 1 +#define XEN_SYSCTL_cpumsr_policy_pv_max 2 +#define XEN_SYSCTL_cpumsr_policy_hvm_max 3 +#define XEN_SYSCTL_cpumsr_policy_pv_default 4 +#define XEN_SYSCTL_cpumsr_policy_hvm_default 5 +uint32_t index;/* IN: Which policy to query? */ +uint32_t nr_leaves;/* IN/OUT: Number of leaves in/written to
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1096
Quoted 10 lines
xen/include/public/sysctl.h +10
-0+* 'cpuid_policy', or the maximum number of leaves if +* any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ +uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_domain_policy', or the maximum number of MSRs +* if any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT: */
Explicit padding (checked to be zero in the handler) above here please.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 09/13] x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy
Quoted 6 lines · Jan Beulich
Oh, here we go - the title doesn't suggest this is about CPUID as well.
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
Extend the xen-cpuid utility to be able to dump the system policies. An example output is:
Quoted 6 lines
Quoted 5 lines
Xen reports there are maximum 113 leaves and 3 MSRs Raw policy: 93 leaves, 3 MSRs
CPUID:
leaf subleaf -> eax ebx ecx edx
00000000:ffffffff -> 0000000d:756e6547:6c65746e:49656e69I'd like to suggest to suppress the :fffffff when there are no sub-leaves.
This is a developer tool, rather than a user tool, and it is dumping raw data.
If there were an easy formatter way of expressing "uint32_t or blank" then yes, but I'm not aware of one.
Quoted 9 lines
tools/misc/xen-cpuid.c int main(int argc, char **argv) · line 461
Quoted 7 lines
tools/misc/xen-cpuid.c +1
-1@@ -377,7 +458,7 @@ int main(int argc, char **argv)if ( i == nr_features )break ; -if ( *ptr ==':' ) +if ( *ptr ==':' || *ptr =='-' )None of the other changes to this file give any hint why a dash needs recognizing here all of the sudden. Is this a stray / leftover change?
Hmm - at a guess that is a XenServer compatibility improvement, but probably can be pulled out into a separate change.
Xapi represents feature bitmaps with dash delimiters rather than colon delimiters, and this alters the parsing to accept both forms.
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/sysctl.c +0
-0@@ -32,22 +32,32 @@#include <asm/cpuid.h>
Quoted 6 lines
xen/arch/x86/sysctl.c line 35
Quoted 3 lines
xen/arch/x86/sysctl.c +1
-1const struct policy_group system_policies[] = { - { + [ XEN_SYSCTL_cpumsr_policy_raw ] = {Aha - this clarifies a question I had on the earlier patch. But it would be nice if that other patch was self contained also in the way of allowing readers to understand the intentions.
One thing I could do is introduce the XEN_SYSCTL_cpumsr_policy_* defines in the previous patch? I don't want to merge the two patches as that is too many moving parts to review in a single patch.
Quoted 3 lines
And with this I now
wonder whether the pointers in structpolicy_groupshouldn't all be const qualified.
Unfortunately that doesn't work with the logic to create a policy_group for an individual domain during audit.
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/sysctl.c +0
-0@@ -318,6 +328,74 @@ long arch_do_sysctl(break ; }
Quoted 12 lines
xen/arch/x86/sysctl.c long arch_do_sysctl( · line 341
Quoted 11 lines
xen/arch/x86/sysctl.c +11
-0+case XEN_SYSCTL_get_cpumsr_policy: + { +const struct policy_group *group; + +/* Bad policy index? */ +if ( sysctl->u.cpumsr_policy.index >=ARRAY_SIZE (system_policies) ) + { + ret = -EINVAL ; +break ; + } + group = &system_policies[sysctl->u.cpumsr_policy.index];Isn't this introducing at least half of a Spectre v1 gadget?
Nope :(
It's both halves of the Spectre gadget, when you account for the dereference when calling x86_*_copy_to_buffer() slightly lower.
I suppose we want to port the Linux array nospec lookup logic so we can protect the clearly-visible gadgets.
Quoted 31 lines
Quoted 21 lines
xen/arch/x86/sysctl.c +20
-0+/* Request for maximum number of leaves/MSRs? */ +if (guest_handle_is_null (sysctl->u.cpumsr_policy.cpuid_policy) ) + { + sysctl->u.cpumsr_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ; +if (__copy_field_to_guest (u_sysctl, sysctl, + u.cpumsr_policy.nr_leaves) ) + { + ret = -EFAULT ; +break ; + } + } +if (guest_handle_is_null (sysctl->u.cpumsr_policy.msr_policy) ) + { + sysctl->u.cpumsr_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ; +if (__copy_field_to_guest (u_sysctl, sysctl, + u.cpumsr_policy.nr_msrs) ) + { + ret = -EFAULT ; +break ; + }xen/arch/x86/sysctl.c long arch_do_sysctl( · line 372
Quoted 10 lines
xen/arch/x86/sysctl.c +10
-0+ } + +/* Serialise the information the caller wants. */ +if ( !guest_handle_is_null (sysctl->u.cpumsr_policy.cpuid_policy) ) + { +if ( (ret =x86_cpuid_copy_to_buffer ( + group->cp, + sysctl->u.cpumsr_policy.cpuid_policy, + &sysctl->u.cpumsr_policy.nr_leaves)) ) +break ;
Quoted 6 lines
Coming back to an earlier question, I realize the null handle logic above is supposed to allow sizing the buffers, but I think it would be better to allow single invocations to generally work, making a second invocation necessary just as a fallback. IOW I think the code here wants to return to the caller the required number of slots in case of
-ENOBUFS. And it should the also ...
I don't agree. Whatever happens, the toolstack has to (once) make a hypercall requesting the size of the buffers, and there is no plausible manipulation where the toolstack would start with one sized buffer, and dynamically size it based on -ENOBUFS.
The independent handling (e.g. only getting CPUID or MSR) is of more use to the toolstack than having Xen try to stumble on in the face of a hard error.
Quoted 7 lines
Quoted 7 lines
xen/include/public/sysctl.h +0
-0@@ -1063,6 +1063,43 @@ struct xen_sysctl_set_parameter {uint16_t pad[3 ];/* IN: MUST be zero. */ };
Quoted 8 lines
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1068
Quoted 3 lines
xen/include/public/sysctl.h +3
-0+#if defined(__i386__) || defined(__x86_64__) +/* +* XEN_SYSCTL_get_cpumsr_policy (x86 specific) Perhaps express the "x86 specific" also in the opcode name? And make more obvious that this is about CPUID and MSRs at the same time? E.g.
XEN_SYSCTL_x86_get_cpuid_msr_policy?I'm sure you have reasons to munge it all into a single operation.
(Answering in reverse order)
The get operations don't strictly need to be a single operation. The set operation specifically must be a single operation, and the getters have an interface to match.
As for naming, cpumsr_policy wasn't chosen by me, but I can't think of anything better. The code is currently consistent and, while I'm open to a rename, it will impact large quantities of the series.
One concern I have if we end up with a new block of information. I was hoping for a generic name, but simply "policy" on its own is too generic. cpumsr is, I believe, a contraction of cpuid_msr to avoid excessive code volume.
Suggestions welcome.
Quoted 22 lines
Quoted 22 lines
xen/include/public/sysctl.h +21
-0+* Return information about CPUID and MSR policies available on this host. +* - Raw: The real H/W values. +* - Host: The values Xen is using, (after command line overrides, etc). +* - Max_*: Maximum set of features a PV or HVM guest can use. Includes +* experimental features outside of security support. +* - Default_*: Default set of features a PV or HVM guest can use. This is +* the security supported set. +*/ +struct xen_sysctl_cpumsr_policy { +#define XEN_SYSCTL_cpumsr_policy_raw 0 +#define XEN_SYSCTL_cpumsr_policy_host 1 +#define XEN_SYSCTL_cpumsr_policy_pv_max 2 +#define XEN_SYSCTL_cpumsr_policy_hvm_max 3 +#define XEN_SYSCTL_cpumsr_policy_pv_default 4 +#define XEN_SYSCTL_cpumsr_policy_hvm_default 5 +uint32_t index;/* IN: Which policy to query? */ +uint32_t nr_leaves;/* IN/OUT: Number of leaves in/written to +* 'cpuid_policy', or the maximum number of leaves if +* any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */
Quoted 8 lines
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1096
Quoted 6 lines
xen/include/public/sysctl.h +6
-0+uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_domain_policy', or the maximum number of MSRs +* if any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT: */ Explicit padding (checked to be zero in the handler) above here please.
Why? SYSCTLs are unstable and we don't perform similar checks for other subops.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 09/13] x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 19:57, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
xen/arch/x86/sysctl.c line 35
Quoted 8 lines
xen/arch/x86/sysctl.c +1
-1@@ -32,22 +32,32 @@#include <asm/cpuid.h> const struct policy_group system_policies[] = { - { + [ XEN_SYSCTL_cpumsr_policy_raw ] = {Aha - this clarifies a question I had on the earlier patch. But it would be nice if that other patch was self contained also in the way of allowing readers to understand the intentions.
Quoted 3 lines
One thing I could do is introduce the XEN_SYSCTL_cpumsr_policy_* defines in the previous patch? I don't want to merge the two patches as that is too many moving parts to review in a single patch.
I think this would help, yes.
Quoted 6 lines
Quoted 3 lines
And with this I now
wonder whether the pointers in structpolicy_groupshouldn't all be const qualified.Unfortunately that doesn't work with the logic to create a
policy_groupfor an individual domain during audit.
I don't understand: Is x86_policies_are_compatible() supposed to alter the policies? Otherwise, if you maintained local separate pointers in update_domain_cpumsr_policy(), using "new" just for the purpose of passing to x86_policies_are_compatible(), all should be fine and it would be crystal clear that anyone handed a group won't alter anything the group refers to.
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/sysctl.c +0
-0@@ -318,6 +328,74 @@ long arch_do_sysctl(break ; }Quoted 12 lines
xen/arch/x86/sysctl.c long arch_do_sysctl( · line 341
Quoted 11 lines
xen/arch/x86/sysctl.c +11
-0+case XEN_SYSCTL_get_cpumsr_policy: + { +const struct policy_group *group; + +/* Bad policy index? */ +if ( sysctl->u.cpumsr_policy.index >=ARRAY_SIZE (system_policies) ) + { + ret = -EINVAL ; +break ; + } + group = &system_policies[sysctl->u.cpumsr_policy.index];Isn't this introducing at least half of a Spectre v1 gadget?
Quoted 7 lines
Nope :(
It's both halves of the Spectre gadget, when you account for the dereference when calling x86_*_copy_to_buffer() slightly lower.
I suppose we want to port the Linux array nospec lookup logic so we can protect the clearly-visible gadgets.
I'm confused: You first say "nope", but the rest of your response reads as if you meant "yes.".
Quoted 25 lines
Quoted 6 lines
Quoted 6 lines
xen/include/public/sysctl.h +0
-0@@ -1063,6 +1063,43 @@ struct xen_sysctl_set_parameter {uint16_t pad[3 ];/* IN: MUST be zero. */ };Quoted 8 lines
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1068
Quoted 3 lines
xen/include/public/sysctl.h +3
-0+#if defined(__i386__) || defined(__x86_64__) +/* +* XEN_SYSCTL_get_cpumsr_policy (x86 specific) Perhaps express the "x86 specific" also in the opcode name? And make more obvious that this is about CPUID and MSRs at the same time? E.g.
XEN_SYSCTL_x86_get_cpuid_msr_policy?I'm sure you have reasons to munge it all into a single operation.
(Answering in reverse order)
The get operations don't strictly need to be a single operation. The set operation specifically must be a single operation, and the getters have an interface to match.
As for naming,
cpumsr_policywasn't chosen by me, but I can't think of anything better. The code is currently consistent and, while I'm open to a rename, it will impact large quantities of the series.
Quoted 6 lines
One concern I have if we end up with a new block of information. I was hoping for a generic name, but simply "policy" on its own is too generic. cpumsr is, I believe, a contraction of
cpuid_msrto avoid excessive code volume.Suggestions welcome.
To cover potential future additions, why not XEN_SYSCTL_get_cpu_policy? That's neither misleading by abbreviating too much, nor more specific than we need it to be. However, in this case it might be worthwhile to consider adding in "x86", as ARM might plausibly want something similar at some point. Otoh the same name (but different structure contents) could be used for both.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
xen/include/public/sysctl.h +13
-0+struct xen_sysctl_cpumsr_policy { +#define XEN_SYSCTL_cpumsr_policy_raw 0 +#define XEN_SYSCTL_cpumsr_policy_host 1 +#define XEN_SYSCTL_cpumsr_policy_pv_max 2 +#define XEN_SYSCTL_cpumsr_policy_hvm_max 3 +#define XEN_SYSCTL_cpumsr_policy_pv_default 4 +#define XEN_SYSCTL_cpumsr_policy_hvm_default 5 +uint32_t index;/* IN: Which policy to query? */ +uint32_t nr_leaves;/* IN/OUT: Number of leaves in/written to +* 'cpuid_policy', or the maximum number of leaves if +* any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ Quoted 8 lines
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1096
Quoted 6 lines
xen/include/public/sysctl.h +6
-0+uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_domain_policy', or the maximum number of MSRs +* if any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT: */ Explicit padding (checked to be zero in the handler) above here please.
Quoted 2 lines
Why? SYSCTLs are unstable and we don't perform similar checks for other subops.
You don't like the interface version bumps anyway, as being only partly useful. If you added and checked explicit padding, no bump would be needed once the field gains meaning.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 09/13] x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 19:57, <[email protected]> wrote:
Quoted 11 lines · Jan Beulich
xen/arch/x86/sysctl.c line 35
Quoted 8 lines
xen/arch/x86/sysctl.c +1
-1@@ -32,22 +32,32 @@#include <asm/cpuid.h> const struct policy_group system_policies[] = { - { + [ XEN_SYSCTL_cpumsr_policy_raw ] = {Aha - this clarifies a question I had on the earlier patch. But it would be nice if that other patch was self contained also in the way of allowing readers to understand the intentions.
Quoted 4 lines
Quoted 3 lines
One thing I could do is introduce the XEN_SYSCTL_cpumsr_policy_* defines in the previous patch? I don't want to merge the two patches as that is too many moving parts to review in a single patch.
I think this would help, yes.
Ok, in which case I'll also move the max vs default discussion into the previous patch.
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines
And with this I now
wonder whether the pointers in structpolicy_groupshouldn't all be const qualified.Unfortunately that doesn't work with the logic to create a
policy_groupfor an individual domain during audit.I don't understand: Is
x86_policies_are_compatible()supposed to alter the policies? Otherwise, if you maintained local separate pointers inupdate_domain_cpumsr_policy(), using "new" just for the purpose of passing tox86_policies_are_compatible(), all should be fine and it would be crystal clear that anyone handed a group won't alter anything the group refers to.
With this diff:
(quoted diff) +0 -0
b/xen/include/xen/libx86/policies.h
index 21e0a40..03fe6dd 100644
xen/include/xen/libx86/policies.h +0 -0
@@ -7,9 +7,9 @@
struct policy_group
{
- struct cpuid_policy *cp;
- struct msr_domain_policy *dp;
- struct msr_vcpu_policy *vp;
+ const struct cpuid_policy *cp;
+ const struct msr_domain_policy *dp;
+ const struct msr_vcpu_policy *vp;
};
/*
The resulting compiler complains are
domctl.c: In function ‘update_domain_cpumsr_policy’:
domctl.c:355:15: error: passing argument 1 of ‘x86_cpuid_copy_from_buffer’ discards ‘const’ qualifier from pointer target type [-Werror]
new.cp, xdpc->cpuid_policy, xdpc->nr_leaves,
^
In file included from /local/xen.git/xen/include/asm/cpuid.h:11:0,
from /local/xen.git/xen/include/asm/cpufeature.h:10,
from /local/xen.git/xen/include/asm/processor.h:14,
from /local/xen.git/xen/include/asm/system.h:6,
from /local/xen.git/xen/include/xen/list.h:11,
from /local/xen.git/xen/include/xen/mm.h:50,
from domctl.c:9:
/local/xen.git/xen/include/xen/libx86/cpuid.h:257:5: note: expected ‘struct cpuid_policy *’ but argument is of type ‘const struct cpuid_policy *’
int x86_cpuid_copy_from_buffer(struct cpuid_policy *p,
^
domctl.c:360:15: error: passing argument 1 of ‘x86_msr_copy_from_buffer’ discards ‘const’ qualifier from pointer target type [-Werror]
new.dp, new.vp,
^
In file included from /local/xen.git/xen/include/xen/libx86/policies.h:6:0,
from /local/xen.git/xen/include/asm/cpuid.h:12,
from /local/xen.git/xen/include/asm/cpufeature.h:10,
from /local/xen.git/xen/include/asm/processor.h:14,
from /local/xen.git/xen/include/asm/system.h:6,
from /local/xen.git/xen/include/xen/list.h:11,
from /local/xen.git/xen/include/xen/mm.h:50,
from domctl.c:9:
/local/xen.git/xen/include/xen/libx86/msr.h:63:5: note: expected ‘struct msr_domain_policy *’ but argument is of type ‘const struct msr_domain_policy *’
int x86_msr_copy_from_buffer(struct msr_domain_policy *dp,
^
domctl.c:360:23: error: passing argument 2 of ‘x86_msr_copy_from_buffer’ discards ‘const’ qualifier from pointer target type [-Werror]
new.dp, new.vp,
^
In file included from /local/xen.git/xen/include/xen/libx86/policies.h:6:0,
from /local/xen.git/xen/include/asm/cpuid.h:12,
from /local/xen.git/xen/include/asm/cpufeature.h:10,
from /local/xen.git/xen/include/asm/processor.h:14,
from /local/xen.git/xen/include/asm/system.h:6,
from /local/xen.git/xen/include/xen/list.h:11,
from /local/xen.git/xen/include/xen/mm.h:50,
from domctl.c:9:
/local/xen.git/xen/include/xen/libx86/msr.h:63:5: note: expected ‘struct msr_vcpu_policy *’ but argument is of type ‘const struct msr_vcpu_policy *’
int x86_msr_copy_from_buffer(struct msr_domain_policy *dp,
^
domctl.c:373:84: error: assignment discards ‘const’ qualifier from pointer target type [-Werror]
SWAP(new.cp, d->arch.cpuid);
^
domctl.c:374:80: error: assignment discards ‘const’ qualifier from pointer target type [-Werror]
SWAP(new.dp, d->arch.msr);
^
domctl.c:375:80: error: assignment discards ‘const’ qualifier from pointer target type [-Werror]
SWAP(new.vp, v->arch.msr);
^
domctl.c:391:11: error: passing argument 1 of ‘xfree’ discards ‘const’ qualifier from pointer target type [-Werror]
xfree(new.cp);
^
In file included from /local/xen.git/xen/include/xen/lib.h:7:0,
from domctl.c:8:
/local/xen.git/xen/include/xen/xmalloc.h:34:13: note: expected ‘void *’ but argument is of type ‘const struct cpuid_policy *’
extern void xfree(void *);
^
domctl.c:392:11: error: passing argument 1 of ‘xfree’ discards ‘const’ qualifier from pointer target type [-Werror]
xfree(new.dp);
^
In file included from /local/xen.git/xen/include/xen/lib.h:7:0,
from domctl.c:8:
/local/xen.git/xen/include/xen/xmalloc.h:34:13: note: expected ‘void *’ but argument is of type ‘const struct msr_domain_policy *’
extern void xfree(void *);
^
domctl.c:393:11: error: passing argument 1 of ‘xfree’ discards ‘const’ qualifier from pointer target type [-Werror]
xfree(new.vp);
^
In file included from /local/xen.git/xen/include/xen/lib.h:7:0,
from domctl.c:8:
/local/xen.git/xen/include/xen/xmalloc.h:34:13: note: expected ‘void *’ but argument is of type ‘const struct msr_vcpu_policy *’
extern void xfree(void *);
^cc1: all warnings being treated as errors
Furthermore, while x86_policies_are_compatible() isn't intended to modify the policies, the other important function for levelling (x86_calculate_compatible_policy(a, b, out)) will write to its parameter.
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/sysctl.c +0
-0@@ -318,6 +328,74 @@ long arch_do_sysctl(break ; }Quoted 12 lines
xen/arch/x86/sysctl.c long arch_do_sysctl( · line 341
Quoted 11 lines
xen/arch/x86/sysctl.c +11
-0+case XEN_SYSCTL_get_cpumsr_policy: + { +const struct policy_group *group; + +/* Bad policy index? */ +if ( sysctl->u.cpumsr_policy.index >=ARRAY_SIZE (system_policies) ) + { + ret = -EINVAL ; +break ; + } + group = &system_policies[sysctl->u.cpumsr_policy.index];Isn't this introducing at least half of a Spectre v1 gadget?
Nope :(
Quoted 7 lines
Quoted 5 lines
It's both halves of the Spectre gadget, when you account for the dereference when calling x86_*_copy_to_buffer() slightly lower.
I suppose we want to port the Linux array nospec lookup logic so we can protect the clearly-visible gadgets.
I'm confused: You first say "nope", but the rest of your response reads as if you meant "yes.".
"No its not half a gadget. Its a full gadget".
Quoted 30 lines
Quoted 30 lines
Quoted 7 lines
Quoted 7 lines
xen/include/public/sysctl.h +0
-0@@ -1063,6 +1063,43 @@ struct xen_sysctl_set_parameter {uint16_t pad[3 ];/* IN: MUST be zero. */ };Quoted 8 lines
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1068
Quoted 3 lines
xen/include/public/sysctl.h +3
-0+#if defined(__i386__) || defined(__x86_64__) +/* +* XEN_SYSCTL_get_cpumsr_policy (x86 specific) Perhaps express the "x86 specific" also in the opcode name? And make more obvious that this is about CPUID and MSRs at the same time? E.g.
XEN_SYSCTL_x86_get_cpuid_msr_policy?I'm sure you have reasons to munge it all into a single operation.
(Answering in reverse order)
The get operations don't strictly need to be a single operation. The set operation specifically must be a single operation, and the getters have an interface to match.
As for naming,
cpumsr_policywasn't chosen by me, but I can't think of anything better. The code is currently consistent and, while I'm open to a rename, it will impact large quantities of the series.One concern I have if we end up with a new block of information. I was hoping for a generic name, but simply "policy" on its own is too generic. cpumsr is, I believe, a contraction of
cpuid_msrto avoid excessive code volume.
Quoted 7 lines
Quoted 1 line
Suggestions welcome.
To cover potential future additions, why not
XEN_SYSCTL_get_cpu_policy? That's neither misleading by abbreviating too much, nor more specific than we need it to be. However, in this case it might be worthwhile to consider adding in "x86", as ARM might plausibly want something similar at some point. Otoh the same name (but different structure contents) could be used for both.
Hmm - I suppose "cpu policy" does logically cover any and all of the main core, without including any of the uncore or chipset, which matches the intended purpose.
I'll see about applying this naming scheme consistently across the series.
Julien/Stefano: Are you liable to want something like this on ARM?
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
xen/include/public/sysctl.h +9
-0+struct xen_sysctl_cpumsr_policy { +#define XEN_SYSCTL_cpumsr_policy_raw 0 +#define XEN_SYSCTL_cpumsr_policy_host 1 +#define XEN_SYSCTL_cpumsr_policy_pv_max 2 +#define XEN_SYSCTL_cpumsr_policy_hvm_max 3 +#define XEN_SYSCTL_cpumsr_policy_pv_default 4 +#define XEN_SYSCTL_cpumsr_policy_hvm_default 5 +uint32_t index;/* IN: Which policy to query? */ +uint32_t nr_leaves;/* IN/OUT: Number of leaves in/written to xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1087
Quoted 10 lines
xen/include/public/sysctl.h +10
-0+* 'cpuid_policy', or the maximum number of leaves if +* any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ +uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_domain_policy', or the maximum number of MSRs +* if any of the guest handles is NULL. +* NB. All policies come from the same space, +* so have the same maximum length. */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT: */ Explicit padding (checked to be zero in the handler) above here please.
Quoted 5 lines
Quoted 2 lines
Why? SYSCTLs are unstable and we don't perform similar checks for other subops.
You don't like the interface version bumps anyway, as being only partly useful. If you added and checked explicit padding, no bump would be needed once the field gains meaning.
Right, but possibly the only thing worse than an interface version of questionable utility is inconsistent ABI expectations across different subops of an otherwise consistent hypercall.
In principle I'd like to improve the ABI expectations of these ops, but
a) we need something much better than this suggestion, and b) I'm not going to get diverted into fixing that rats nest as part of this series.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 09/13] x86/sysctl: Implement XEN_SYSCTL_get_cpumsr_policy
Quoted 51 lines
Quoted 1 line
Quoted 1 line · On 05.07.18 at 16:08, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 19:57, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
And with this I now
wonder whether the pointers in structpolicy_groupshouldn't all be const qualified.Quoted 8 lines
Quoted 2 lines
Unfortunately that doesn't work with the logic to create a
policy_groupfor an individual domain during audit.I don't understand: Is
x86_policies_are_compatible()supposed to alter the policies? Otherwise, if you maintained local separate pointers inupdate_domain_cpumsr_policy(), using "new" just for the purpose of passing tox86_policies_are_compatible(), all should be fine and it would be crystal clear that anyone handed a group won't alter anything the group refers to.With this diff:
xen/include/xen/libx86/policies.h +3
-3@@ -7,9 +7,9 @@struct policy_group { -struct cpuid_policy *cp; -struct msr_domain_policy *dp; -struct msr_vcpu_policy *vp; +const struct cpuid_policy *cp; +const struct msr_domain_policy *dp; +const struct msr_vcpu_policy *vp; };/* The resulting compiler complains are
domctl.c: In function ‘update_domain_cpumsr_policy’:
domctl.c:355:15: error: passing argument 1 of ‘x86_cpuid_copy_from_buffer’ discards ‘const’ qualifier from pointer target type [-Werror]new.cp, xdpc->cpuid_policy, xdpc->nr_leaves, ^ In file included from /local/xen.git/xen/include/asm/cpuid.h:11:0, from /local/xen.git/xen/include/asm/cpufeature.h:10, from /local/xen.git/xen/include/asm/processor.h:14, from /local/xen.git/xen/include/asm/system.h:6, from /local/xen.git/xen/include/xen/list.h:11, from /local/xen.git/xen/include/xen/mm.h:50, from domctl.c:9:
Quoted 8 lines
/local/xen.git/xen/include/xen/libx86/cpuid.h:257:5: note: expected ‘struct
cpuid_policy*’ but argument is of type ‘const structcpuid_policy*’
int x86_cpuid_copy_from_buffer(structcpuid_policy*p,^ domctl.c:360:15: error: passing argument 1 of ‘x86_msr_copy_from_buffer’ discards ‘const’ qualifier from pointer target type [-Werror] new.dp, new.vp, ^
As said - you'd need local variables being pointers to non-const, and "new" should be used solely for handing the entire pack to the auditing function.
Quoted 3 lines
Furthermore, while
x86_policies_are_compatible()isn't intended to modify the policies, the other important function for levelling (x86_calculate_compatible_policy(a, b, out)) will write to its parameter.
Hmm, that's unfortunate. I'd really like to have a way to make sure (not just by commentary) that checking functions can't alter their inputs. I agree though that having a second container structure doesn't look very appealing.
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -318,6 +328,74 @@ long arch_do_sysctl( break; }Quoted 12 lines
Review long arch_do_sysctl( · line 341
Quoted 11 lines
(quoted diff) +11
-0+ case XEN_SYSCTL_get_cpumsr_policy: + { + const struct policy_group *group; + + /* Bad policy index? */ + if ( sysctl->u.cpumsr_policy.index >= ARRAY_SIZE(system_policies) ) + { + ret = -EINVAL; + break; + } + group = &system_policies[sysctl->u.cpumsr_policy.index];Isn't this introducing at least half of a Spectre v1 gadget?
Nope :(
It's both halves of the Spectre gadget, when you account for the dereference when calling x86_*_copy_to_buffer() slightly lower.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I suppose we want to port the Linux array nospec lookup logic so we can protect the clearly-visible gadgets.
I'm confused: You first say "nope", but the rest of your response reads as if you meant "yes.".
"No its not half a gadget. Its a full gadget".
Right - as I hadn't checked further, I did write "at least".
Quoted 27 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
(quoted diff) +9
-0+struct xen_sysctl_cpumsr_policy { +#define XEN_SYSCTL_cpumsr_policy_raw 0 +#define XEN_SYSCTL_cpumsr_policy_host 1 +#define XEN_SYSCTL_cpumsr_policy_pv_max 2 +#define XEN_SYSCTL_cpumsr_policy_hvm_max 3 +#define XEN_SYSCTL_cpumsr_policy_pv_default 4 +#define XEN_SYSCTL_cpumsr_policy_hvm_default 5 + uint32_t index; /* IN: Which policy to query? */ + uint32_t nr_leaves; /* IN/OUT: Number of leaves in/written toReview long arch_do_sysctl( · line 360
Quoted 10 lines
(quoted diff) +10
-0+ * 'cpuid_policy', or the maximum number of leaves if + * any of the guest handles is NULL. + * NB. All policies come from the same space, + * so have the same maximum length. */ + uint32_t nr_msrs; /* IN/OUT: Number of MSRs in/written to + * 'msr_domain_policy', or the maximum number of MSRs + * if any of the guest handles is NULL. + * NB. All policies come from the same space, + * so have the same maximum length. */ + XEN_GUEST_HANDLE_64(xen_cpuid_leaf_t) cpuid_policy; /* OUT: */Explicit padding (checked to be zero in the handler) above here please.
Quoted 5 lines
Quoted 2 lines
Why? SYSCTLs are unstable and we don't perform similar checks for other subops.
You don't like the interface version bumps anyway, as being only partly useful. If you added and checked explicit padding, no bump would be needed once the field gains meaning.
Quoted 7 lines
Right, but possibly the only thing worse than an interface version of questionable utility is inconsistent ABI expectations across different subops of an otherwise consistent hypercall.
In principle I'd like to improve the ABI expectations of these ops, but
a) we need something much better than this suggestion, and b) I'm not going to get diverted into fixing that rats nest as part of this series.
I can't help a), but as to b) you certainly shouldn't, yet my frequently made remark applies here perfectly well: Let's not use bad examples as excuse to widen the badness. As to consistency, it only took me looking at the first few lines of sysctl.h to find padding fields in xen_sysctl_readconsole (I didn't go look whether there's also checking for them to be zero on input, but the latest for the live patching sysctl you'll find that padding is validated to be zero).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 10/13 x86/domctl: Implement XEN_DOMCTL_get_cpumsr_policy
In reply to Andrew CooperFrom: Sergey Dyasli <[email protected]>
This finally (after literally years of work!) marks the point where the toolstack can ask the hypervisor for the current CPUID configuration of a specific domain.
Also extend xen-cpuid's --policy mode to be able to take a domid and dump a specific domains CPUID and MSR policy.
---
CC: Jan Beulich <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Daniel De Graaf <[email protected]>
--- tools/libxc/include/xenctrl.h | 3 ++ tools/libxc/xc_cpuid_x86.c | 40 +++++++++++++++++++++++ tools/misc/xen-cpuid.c | 64 +++++++++++++++++++++++++++++++------ xen/arch/x86/domctl.c | 46 ++++++++++++++++++++++++++ xen/include/public/domctl.h | 18 +++++++++++ xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 1 + 7 files changed, 164 insertions(+), 9 deletions(-)
tools/libxc/include/xenctrl.h +3 -0
@@ -2541,6 +2541,9 @@ int xc_get_cpumsr_policy_size(xc_interface *xch, uint32_t *nr_leaves,int xc_get_system_cpumsr_policy (xc_interface *xch,uint32_t index,uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves,uint32_t *nr_msrs,xen_msr_entry_t *msrs); +int xc_get_domain_cpumsr_policy (xc_interface *xch,uint32_t domid, +uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves, +uint32_t *nr_msrs,xen_msr_entry_t *msrs);uint32_t xc_get_cpu_featureset_size (void );
tools/libxc/xc_cpuid_x86.c +40 -0
@@ -193,6 +193,46 @@ int xc_get_system_cpumsr_policy(xc_interface *xch, uint32_t index,return ret; } +int xc_get_domain_cpumsr_policy (xc_interface *xch,uint32_t domid, +uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves, +uint32_t *nr_msrs,xen_msr_entry_t *msrs) +{ +DECLARE_DOMCTL ; +DECLARE_HYPERCALL_BOUNCE (leaves, + *nr_leaves *sizeof (*leaves), +XC_HYPERCALL_BUFFER_BOUNCE_OUT ); +DECLARE_HYPERCALL_BOUNCE (msrs, + *nr_msrs *sizeof (*msrs), +XC_HYPERCALL_BUFFER_BOUNCE_OUT ); +int ret; + +if (xc_hypercall_bounce_pre (xch, leaves) ) +return -1 ; + +if (xc_hypercall_bounce_pre (xch, msrs) ) +return -1 ; + + domctl.cmd = XEN_DOMCTL_get_cpumsr_policy; + domctl.domain = domid; + domctl.u.cpumsr_policy.nr_leaves = *nr_leaves; +set_xen_guest_handle (domctl.u.cpumsr_policy.cpuid_policy, leaves); + domctl.u.cpumsr_policy.nr_msrs = *nr_msrs; +set_xen_guest_handle (domctl.u.cpumsr_policy.msr_policy, msrs); + + ret =do_domctl (xch, &domctl); + +xc_hypercall_bounce_post (xch, leaves); +xc_hypercall_bounce_post (xch, msrs); + +if ( !ret ) + { + *nr_leaves = domctl.u.cpumsr_policy.nr_leaves; + *nr_msrs = domctl.u.cpumsr_policy.nr_msrs; + } + +return ret; +} +struct cpuid_domain_info { enum
tools/misc/xen-cpuid.c +55 -9
114 lines not on this page · raw message
xen/arch/x86/domctl.c +46 -0
52 lines not on this page · raw message
xen/include/public/domctl.h +18 -0
@@ -635,6 +635,22 @@ struct xen_domctl_cpuid {uint32_t ecx; uint32_t edx; }; + +/* +* XEN_SYSCTL_{get,set}_cpumsr_policy (x86 specific) +* +* Query or set the CPUID and MSR policies for a specific domain. +*/ +struct xen_domctl_cpumsr_policy { +uint32_t nr_leaves;/* IN/OUT: Number of leaves in/written to +* 'cpuid_policy'. */ +uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_domain_policy' */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* IN/OUT: */ +XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* IN/OUT: */ +}; +typedef struct xen_domctl_cpumsr_policy xen_domctl_cpumsr_policy_t ; +DEFINE_XEN_GUEST_HANDLE (xen_domctl_cpumsr_policy_t);#endif /*
@@ -1172,6 +1188,7 @@ struct xen_domctl {#define XEN_DOMCTL_soft_reset 79#define XEN_DOMCTL_set_gnttab_limits 80#define XEN_DOMCTL_vuart_op 81 +#define XEN_DOMCTL_get_cpumsr_policy 82#define XEN_DOMCTL_gdbsx_guestmemio 1000#define XEN_DOMCTL_gdbsx_pausevcpu 1001#define XEN_DOMCTL_gdbsx_unpausevcpu 1002
@@ -1216,6 +1233,7 @@ struct xen_domctl {struct xen_domctl_mem_sharing_op mem_sharing_op;#if defined(__i386__) || defined(__x86_64__)struct xen_domctl_cpuid cpuid; +struct xen_domctl_cpumsr_policy cpumsr_policy;struct xen_domctl_vcpuextstate vcpuextstate;struct xen_domctl_vcpu_msrs vcpu_msrs;#endif
xen/xsm/flask/hooks.c +1 -0
@@ -717,6 +717,7 @@ static int flask_domctl(struct domain *d, int cmd)return current_has_perm (d,SECCLASS_DOMAIN ,DOMAIN__SET_VIRQ_HANDLER );case XEN_DOMCTL_set_cpuid: +case XEN_DOMCTL_get_cpumsr_policy:return current_has_perm (d,SECCLASS_DOMAIN2 ,DOMAIN2__SET_CPUID );case XEN_DOMCTL_gettscinfo:
xen/xsm/flask/policy/access_vectors +1 -0
@@ -213,6 +213,7 @@ class domain2 # target = the new target domain set_as_target # XEN_DOMCTL_set_cpuid +# XEN_DOMCTL_get_cpumsr_policy set_cpuid # XEN_DOMCTL_gettscinfo gettsc
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 10/13] x86/domctl: Implement XEN_DOMCTL_get_cpumsr_policy
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Sergey Dyasli <[email protected]>
This finally (after literally years of work!) marks the point where the toolstack can ask the hypervisor for the current CPUID configuration of a specific domain.
Quoted 5 lines
Also extend xen-cpuid's --policy mode to be able to take a domid and dump a specific domains CPUID and MSR policy.
Signed-off-by Andrew Cooper <[email protected]>Signed-off-by Sergey Dyasli <[email protected]>
Remarks given for the sysctl patch apply here respectively.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 10/13] x86/domctl: Implement XEN_DOMCTL_get_cpumsr_policy
In reply to Andrew CooperQuoted 17 lines · Andrew Cooper
From: Sergey Dyasli <[email protected]>
This finally (after literally years of work!) marks the point where the toolstack can ask the hypervisor for the current CPUID configuration of a specific domain.
Also extend xen-cpuid's --policy mode to be able to take a domid and dump a specific domains CPUID and MSR policy.
Signed-off-by Andrew Cooper <[email protected]>Signed-off-by Sergey Dyasli <[email protected]>---
tools/misc/xen-cpuid.c +0
-0
...
Quoted 7 lines
tools/misc/xen-cpuid.c +1
-1@@ -407,17 +434,36 @@ int main(int argc, char **argv)if ( !msrs )err (1 ,"calloc(max_msrs)" ); -for ( pol =0 ; pol <ARRAY_SIZE (sys_policies); ++pol ) +if ( domid !=-1 ) {
tools/misc/xen-cpuid.c int main(int argc, char **argv) · line 445
Quoted 10 lines
tools/misc/xen-cpuid.c +3
-3+char name[20 ];uint32_t nr_leaves = max_leaves;uint32_t nr_msrs = max_msrs; -if (xc_get_system_cpumsr_policy (xch, pol, &nr_leaves, leaves, +if (xc_get_domain_cpumsr_policy (xch, domid, &nr_leaves, leaves, &nr_msrs, msrs) ) -err (1 ,"xc_get_system_cpumsr_policy(, %s,,)" , - sys_policies[pol]); + err(1, "xc_get_domain_cpuid_policy(, %d, %d,, %d,)",
^^^^^
This wants to be "cpumsr" for consistency.
Quoted 26 lines
tools/misc/xen-cpuid.c +20
-1+ domid, nr_leaves, nr_msrs); -print_policy (sys_policies[pol], leaves, nr_leaves, msrs, nr_msrs); +snprintf (name,sizeof (name),"Domain %d" , domid); +print_policy (name, leaves, nr_leaves, msrs, nr_msrs); + } +else + { +/* Get system policies */ +for ( pol =0 ; pol <ARRAY_SIZE (sys_policies); ++pol ) + { +uint32_t nr_leaves = max_leaves; +uint32_t nr_msrs = max_msrs; + +if (xc_get_system_cpumsr_policy (xch, pol, &nr_leaves, leaves, + &nr_msrs, msrs) ) +err (1 ,"xc_get_system_cpumsr_policy(, %s,,)" , + sys_policies[pol]); + +print_policy (sys_policies[pol], leaves, nr_leaves, + msrs, nr_msrs); + } }free (leaves);
Signature · 6 lines
Thanks, Sergey _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 11/13 libx86: Introduce a helper to deserialise a cpuid_policy object
In reply to Andrew CooperAs 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]>
CC: Ian Jackson <[email protected]>
--- xen/common/libx86/cpuid.c | 88 ++++++++++++++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 12 ++++++ xen/include/xen/libx86/cpuid.h | 4 ++ 3 files changed, 104 insertions(+)
xen/common/libx86/cpuid.c +88 -0
@@ -128,6 +128,94 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p, }/* +* Copy CPUID data from a buffer, filling in a cpuid_policy object. Performs +* boudary checking of the incomming leaves before filling the appropriate +* policy unions, but no content validation is performed. +*/ +int x86_cpuid_copy_from_buffer (struct cpuid_policy *p, +const cpuid_leaf_buffer_t leaves, +uint32_t nr_leaves,uint32_t *err_leaf, +uint32_t *err_subleaf) +{ +unsigned int i; +xen_cpuid_leaf_t data; +struct cpuid_leaf *l = (void *)&data.a; + +if ( nr_leaves >CPUID_MAX_SERIALISED_LEAVES ) +return -E2BIG ; + +for ( i =0 ; i < nr_leaves; ++i ) + { +if (copy_from_buffer_offset (&data, leaves, i,1 ) ) +return -EFAULT ; + +switch ( data.leaf ) + { + case 0 ... ARRAY_SIZE(p->basic.raw) - 1: +switch ( data.leaf ) + { +case 0x4 : +if ( data.subleaf >=ARRAY_SIZE (p->cache.raw) ) + goto out_of_range; + + p->cache.raw[data.subleaf] = *l; +break ; + +case 0x7 : +if ( data.subleaf >=ARRAY_SIZE (p->feat.raw) ) + goto out_of_range; + + p->feat.raw[data.subleaf] = *l; +break ; + +case 0xb : +if ( data.subleaf >=ARRAY_SIZE (p->topo.raw) ) + goto out_of_range; + + p->topo.raw[data.subleaf] = *l; +break ; + +case 0xd : +if ( data.subleaf >=ARRAY_SIZE (p->xstate.raw) ) + goto out_of_range; + + p->xstate.raw[data.leaf] = *l; +break ; + +default : + p->basic.raw[data.leaf] = *l; +break ; + } +break ; + +case 0x40000000 : + p->hv_limit = l->a; +break ; + +case 0x40000100 : + p->hv2_limit = l->a; +break ; + + case 0x80000000 ... 0x80000000 + ARRAY_SIZE(p->extd.raw) - 1: + p->extd.raw[data.leaf &0xffff ] = *l; +break ; + +default : + goto out_of_range; + } + } + +return 0 ; + + out_of_range: +if ( err_leaf ) + *err_leaf = data.leaf; +if ( err_subleaf ) + *err_subleaf = data.subleaf; +return -ERANGE ; +} + +/* * Local variables: * mode: C * c-file-style: "BSD"
xen/common/libx86/libx86-private.h +12 -0
@@ -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
@@ -47,6 +48,17 @@ 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(*(dst)) *s = (src); \ + unsigned int i; \ + \ + for ( i = 0; i < (nr); i++ ) \ + (dst)[i] = s[(index) + i]; \ + \ + 0; \ +}) +#endif /* __XEN__ */ #endif /* XEN_LIBX86_PRIVATE_H */
xen/include/xen/libx86/cpuid.h +4 -0
@@ -254,6 +254,10 @@ typedef xen_cpuid_leaf_t *cpuid_leaf_buffer_t;int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p,cpuid_leaf_buffer_t leaves,uint32_t *nr_entries_p); +int x86_cpuid_copy_from_buffer (struct cpuid_policy *p, +const cpuid_leaf_buffer_t leaves, +uint32_t nr_leaves,uint32_t *err_leaf, +uint32_t *err_subleaf);#endif /* !XEN_LIBX86_CPUID_H */
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 11/13] libx86: Introduce a helper to deserialise a cpuid_policy object
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
xen/common/libx86/libx86-private.h +0
-0@@ -47,6 +48,17 @@ static inline bool test_bit(unsigned int bit, const void *vaddr)0 ; \ })
xen/common/libx86/libx86-private.h static inline bool test_bit(unsigned int bit, const void *vaddr) · line 60
Quoted 10 lines
xen/common/libx86/libx86-private.h +10
-0+/* memcpy(), but with copy_from_guest_offset()'s API */ +#define copy_from_buffer_offset (dst, src, index, nr) ({ \ + const typeof(*(dst)) *s = (src); \ + unsigned int i; \ + \ + for ( i = 0; i < (nr); i++ ) \ + (dst)[i] = s[(index) + i]; \ + \ + 0; \ +})
Again the question on whether multiple evaluation of macro arguments would better be avoided here.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 12/13 libx86: introduce a helper to deserialize MSR policies
In reply to Andrew CooperFrom: Roger Pau Monné <[email protected]>
--- xen/common/libx86/msr.c | 63 ++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/libx86/msr.h | 4 +++ 2 files changed, 67 insertions(+)
xen/common/libx86/msr.c +63 -0
@@ -61,6 +61,69 @@ int x86_msr_copy_to_buffer(const struct msr_domain_policy *dp, }/* +* Copy MSR data from a buffer, filling optionally msr_domain_policy and +* msr_vcpu_policy objects. MSR indicies are checked for being in range, but +* no content validation is performed for in-range MSRs. +*/ +int x86_msr_copy_from_buffer (struct msr_domain_policy *dp, +struct msr_vcpu_policy *vp, +const msr_entry_buffer_t msrs,uint32_t nr_msrs, +uint32_t *err_msr) +{ +unsigned int i; +xen_msr_entry_t data; + +if ( nr_msrs >MSR_MAX_SERIALISED_ENTRIES ) +return -E2BIG ; + +for ( i =0 ; i < nr_msrs; i++ ) + { +if (copy_from_buffer_offset (&data, msrs, i,1 ) ) +return -EFAULT ; + +if ( data.flags )/* .flags MBZ */ + goto err; + +switch ( data.idx ) + { +case MSR_SPEC_CTRL : +if ( data.val > ~0u ) + goto err; + +if ( vp ) + vp->spec_ctrl.raw = data.val; +break ; + +case MSR_INTEL_PLATFORM_INFO : +if ( data.val > ~0u ) + goto err; + +if ( dp ) + dp->plaform_info.raw = data.val; +break ; + +case MSR_INTEL_MISC_FEATURES_ENABLES : +if ( data.val > ~0u ) + goto err; + +if ( vp ) + vp->misc_features_enables.raw = data.val; +break ; + +default : + goto err; + } + } + +return 0 ; + + err: +if ( err_msr ) + *err_msr = data.idx; +return -EINVAL ; +} + +/* * Local variables: * mode: C * c-file-style: "BSD"
xen/include/xen/libx86/msr.h +4 -0
@@ -60,6 +60,10 @@ typedef xen_msr_entry_t *msr_entry_buffer_t;int x86_msr_copy_to_buffer (const struct msr_domain_policy *dp,const struct msr_vcpu_policy *vp,msr_entry_buffer_t msrs,uint32_t *nr_entries_p); +int x86_msr_copy_from_buffer (struct msr_domain_policy *dp, +struct msr_vcpu_policy *vp, +const msr_entry_buffer_t msrs,uint32_t nr_msrs, +uint32_t *err_msr);#endif /* !XEN_LIBX86_MSR_H */
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 13/13 x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
In reply to Andrew CooperFrom: Sergey Dyasli <[email protected]>
This hypercall allows the toolstack to present one combined CPUID and MSR policy for a domain, which can be audited in one go by Xen, which is necessary for correctness of the auditing.
A stub x86_policies_are_compatible() function is introduced, although at present it will always fail the hypercall.
The hypercall ABI allows for update of individual CPUID or MSR entries, so begins by duplicating the existing policy (for which a helper is introduced), merging the toolstack data, then checking compatibility of the result.
The system PV/HVM max policy is used for the compatiblity check.
---
CC: Jan Beulich <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Daniel De Graaf <[email protected]>
One awkard corner case is re-deserialising of the vcpu msrs. The correct fix would be to allocate a buffer, copy the MSRs list, then deserialise from that, but trips the bounds checks in the copy_from_guest() helpers. The compat XLAT are would work, but would require that we allocate it even for 64bit PV guests.
--- tools/libxc/Makefile | 2 +- tools/libxc/include/xenctrl.h | 5 +++ tools/libxc/xc_cpuid_x86.c | 49 +++++++++++++++++++++ xen/arch/x86/domctl.c | 87 +++++++++++++++++++++++++++++++++++++ xen/common/libx86/Makefile | 1 + xen/common/libx86/policies.c | 19 ++++++++ xen/include/public/domctl.h | 7 +++ xen/include/xen/libx86/policies.h | 10 +++++ xen/include/xen/xmalloc.h | 7 +++ xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 1 + 11 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 xen/common/libx86/policies.c
tools/libxc/Makefile +1 -1
@@ -83,7 +83,7 @@ $(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign # Add libx86 to the build vpath %.c ../../xen/common/libx86 -GUEST_SRCS-$(CONFIG_X86) += cpuid.c msr.c +GUEST_SRCS-$(CONFIG_X86) += cpuid.c msr.c policies.c # new domain builder GUEST_SRCS-y += xc_dom_core.c xc_dom_boot.c
tools/libxc/include/xenctrl.h +5 -0
@@ -2544,6 +2544,11 @@ int xc_get_system_cpumsr_policy(xc_interface *xch, uint32_t index,int xc_get_domain_cpumsr_policy (xc_interface *xch,uint32_t domid,uint32_t *nr_leaves,xen_cpuid_leaf_t *leaves,uint32_t *nr_msrs,xen_msr_entry_t *msrs); +int xc_set_domain_cpumsr_policy (xc_interface *xch,uint32_t domid, +uint32_t nr_leaves,xen_cpuid_leaf_t *leaves, +uint32_t nr_msrs,xen_msr_entry_t *msrs, +uint32_t *err_leaf_p,uint32_t *err_subleaf_p, +uint32_t *err_msr_idx_p);uint32_t xc_get_cpu_featureset_size (void );
tools/libxc/xc_cpuid_x86.c +49 -0
55 lines not on this page · raw message
xen/arch/x86/domctl.c +87 -0
99 lines not on this page · raw message
xen/common/libx86/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-y += cpuid.o obj-y += msr.o +obj-y += policies.o
xen/common/libx86/policies.c new file +19 -0
@@ -0,0 +1,19 @@ +#include "libx86-private.h" + +#include <xen/libx86/policies.h> + +int x86_policies_are_compatible (const struct policy_group *lhs, +const struct policy_group *rhs) +{ +return -EOPNOTSUPP ; +} + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/include/public/domctl.h +7 -0
@@ -648,6 +648,12 @@ struct xen_domctl_cpumsr_policy {* 'msr_domain_policy' */ XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* IN/OUT: */ XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* IN/OUT: */ +uint32_t err_leaf, err_subleaf;/* OUT, set_policy only. If not ~0, +* indicates the leaf/subleaf which +* auditing objected to. */ +uint32_t err_msr_idx;/* OUT, set_policy only. If not ~0, +* indicates the MSR idx which +* auditing objected to. */ };typedef struct xen_domctl_cpumsr_policy xen_domctl_cpumsr_policy_t ;DEFINE_XEN_GUEST_HANDLE (xen_domctl_cpumsr_policy_t);
@@ -1189,6 +1195,7 @@ struct xen_domctl {#define XEN_DOMCTL_set_gnttab_limits 80#define XEN_DOMCTL_vuart_op 81#define XEN_DOMCTL_get_cpumsr_policy 82 +#define XEN_DOMCTL_set_cpumsr_policy 83#define XEN_DOMCTL_gdbsx_guestmemio 1000#define XEN_DOMCTL_gdbsx_pausevcpu 1001#define XEN_DOMCTL_gdbsx_unpausevcpu 1002
xen/include/xen/libx86/policies.h +10 -0
@@ -12,6 +12,16 @@ struct policy_group struct msr_vcpu_policy *vp; }; +/* +* Calculate whether two policies are compatible. +* +* For typical usage, lhs should be a system policy, and rhs should be a +* proposed guest policy. This largely amounts to "is rhs a subset of lhs", +* but some of the checks are rather more complicated in practice. +*/ +int x86_policies_are_compatible (const struct policy_group *lhs, +const struct policy_group *rhs); +#endif /* !XEN_LIBX86_POLICIES_H */ /*
xen/include/xen/xmalloc.h +7 -0
@@ -13,6 +13,13 @@#define xmalloc (_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))#define xzalloc (_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type))) +/* Allocate space for a typed object and copy an existing instance. */ +#define xmemdup (ptr) \ + ({ typeof(*ptr) *n_ = xmalloc(typeof(*ptr)); \ + if ( n_ ) \ + memcpy(n_, ptr, sizeof(*ptr)); \ + n_; }) +/* Allocate space for array of typed objects. */ #define xmalloc_array (_type, _num) \ ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
xen/xsm/flask/hooks.c +1 -0
@@ -718,6 +718,7 @@ static int flask_domctl(struct domain *d, int cmd)case XEN_DOMCTL_set_cpuid:case XEN_DOMCTL_get_cpumsr_policy: +case XEN_DOMCTL_set_cpumsr_policy:return current_has_perm (d,SECCLASS_DOMAIN2 ,DOMAIN2__SET_CPUID );case XEN_DOMCTL_gettscinfo:
xen/xsm/flask/policy/access_vectors +1 -0
@@ -214,6 +214,7 @@ class domain2 set_as_target # XEN_DOMCTL_set_cpuid # XEN_DOMCTL_get_cpumsr_policy +# XEN_DOMCTL_set_cpumsr_policy set_cpuid # XEN_DOMCTL_gettscinfo gettsc
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Sergey Dyasli <[email protected]>
This hypercall allows the toolstack to present one combined CPUID and MSR policy for a domain, which can be audited in one go by Xen, which is necessary for correctness of the auditing.
Quoted 6 lines
A stub
x86_policies_are_compatible()function is introduced, although at present it will always fail the hypercall.The hypercall ABI allows for update of individual CPUID or MSR entries, so begins by duplicating the existing policy (for which a helper is introduced), merging the toolstack data, then checking compatibility of the result.
This reads to me as if it was fine for the tool stack to supply only partial data (or else there would be no need to merge anything). What's the thinking behind this, rather than requiring complete sets of data to be supplied?
Quoted 17 lines
One awkard corner case is re-deserialising of the vcpu msrs. The correct fix would be to allocate a buffer, copy the MSRs list, then deserialise from that, but trips the bounds checks in the
copy_from_guest()helpers. The compat XLAT are would work, but would require that we allocate it even for 64bit PV guests.xen/arch/x86/domctl.c +3
-0@@ -330,6 +330,71 @@ static int update_domain_cpuid_info(struct domain *d,return 0 ; } +static int update_domain_cpumsr_policy (struct domain *d, +xen_domctl_cpumsr_policy_t *xdpc) +{
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 345
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+struct policy_group new = {}; +const struct policy_group *sys =is_pv_domain (d) + ? &system_policies[XEN_SYSCTL_cpumsr_policy_pv_max] + : &system_policies[XEN_SYSCTL_cpumsr_policy_hvm_max]; +struct vcpu *v = d->vcpu[0 ]; +int ret = -ENOMEM ; + +/* Initialise some help identifying auditing errors. */ + xdpc->err_leaf = xdpc->err_subleaf =XEN_CPUID_NO_SUBLEAF ; + xdpc->err_msr_idx = ~0 ;
I'm having trouble extracting information from the comment.
Quoted 11 lines
xen/arch/x86/domctl.c +11
-0+/* Start with existing domain's policies */ +if ( !(new.cp =xmemdup (d->arch.cpuid)) || + !(new.dp =xmemdup (d->arch.msr)) || + !(new.vp =xmemdup (v->arch.msr)) ) + goto out; + +/* Merge the toolstack provided data. */ +if ( (ret =x86_cpuid_copy_from_buffer ( + new.cp, xdpc->cpuid_policy, xdpc->nr_leaves, + &xdpc->err_leaf, &xdpc->err_subleaf)) ) + goto out;
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 367
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+ +if ( (ret =x86_msr_copy_from_buffer ( + new.dp, new.vp, + xdpc->msr_policy, xdpc->nr_msrs, &xdpc->err_msr_idx)) ) + goto out; + +/* Audit the combined dataset. */ + ret =x86_policies_are_compatible (sys, &new); +if ( ret ) + goto out;
I'm afraid I don't follow - where's the merging? All you do is copy the first so many entries coming from libxc, and using the later so many entries from the previous policies. How's that going to provide a complete set, rather than e.g. some duplicate entries and some missing ones?
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 378
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+/* +* Audit was successful. Replace existing policies, leaving the old +* policies to be freed. +*/ +SWAP (new.cp, d->arch.cpuid); +SWAP (new.dp, d->arch.msr); +SWAP (new.vp, v->arch.msr); + +/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list )
This open-coded almost-for_each_domain() doesn't look very nice.
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 382
Quoted 4 lines
xen/arch/x86/domctl.c +4
-0+ { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ +if ( (ret =x86_msr_copy_from_buffer ( +NULL , v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs,NULL )) )
Why can't you go from vCPU 0's v->arch.msr here, which is the copied-in (and sanitized) representation already? Also, is it really a good idea to assume all vCPU-s have the same policies?
Quoted 11 lines
xen/arch/x86/domctl.c +7
-0@@ -1570,6 +1635,28 @@ long arch_do_domctl(domain_unpause (d);break ; +case XEN_DOMCTL_set_cpumsr_policy: +if ( d == currd ||/* no domain_pause() */ + d->max_vcpus ==0 )/* No vcpus yet. */ + { + ret = -EINVAL ; +break ; + }
xen/arch/x86/domctl.c long arch_do_domctl( · line 1654
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+ +domain_pause (d); + +if ( d->creation_finished ) + ret = -EEXIST ;/* No changing once the domain is running. */ +else + { + ret =update_domain_cpumsr_policy (d, &domctl->u.cpumsr_policy); +if ( !ret )/* Copy domctl->u.cpumsr_policy.err_* to guest. */ + copyback = true;
x86_cpuid_copy_from_buffer(), for example, sets the err_ fields only when returning -ERANGE. Is the if() condition inverted?
xen/include/public/domctl.h struct xen_domctl_cpumsr_policy { · line 656
Quoted 12 lines
xen/include/public/domctl.h +6
-0@@ -648,6 +648,12 @@ struct xen_domctl_cpumsr_policy {* 'msr_domain_policy' */ XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* IN/OUT: */ XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* IN/OUT: */ +uint32_t err_leaf, err_subleaf;/* OUT, set_policy only. If not ~0, +* indicates the leaf/subleaf which +* auditing objected to. */ +uint32_t err_msr_idx;/* OUT, set_policy only. If not ~0, +* indicates the MSR idx which +* auditing objected to. */
Explicit padding again please, with the handler checking it to be zero.
xen/include/xen/xmalloc.h line 21
Quoted 12 lines
xen/include/xen/xmalloc.h +6
-0@@ -13,6 +13,13 @@#define xmalloc (_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))#define xzalloc (_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type))) +/* Allocate space for a typed object and copy an existing instance. */ +#define xmemdup (ptr) \ + ({ typeof(*ptr) *n_ = xmalloc(typeof(*ptr)); \ + if ( n_ ) \ + memcpy(n_, ptr, sizeof(*ptr)); \ + n_; })
Would be nice if this could handle input pointers to const-qualified types. I vaguely recall having seen a solution to this recently, but I don't recall where that was or how it looked like. Until then, may I suggest to use void * instead, despite this opening the risk of type incompatibilities?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Sergey Dyasli <[email protected]>
This hypercall allows the toolstack to present one combined CPUID and MSR policy for a domain, which can be audited in one go by Xen, which is necessary for correctness of the auditing.
A stub
x86_policies_are_compatible()function is introduced, although at present it will always fail the hypercall.
Quoted 7 lines
Quoted 3 lines
The hypercall ABI allows for update of individual CPUID or MSR entries, so begins by duplicating the existing policy (for which a helper is introduced), merging the toolstack data, then checking compatibility of the result.
This reads to me as if it was fine for the tool stack to supply only partial data (or else there would be no need to merge anything). What's the thinking behind this, rather than requiring complete sets of data to be supplied?
Unless you have an identical build of Xen and libxc, the toolstack won't always provide the same leaves as Xen expects. Such a restriction would massively hamper development.
More importantly, with the max_extd_leaf being vendor dependent (which is data earlier in the structure), the toolstack can't reasonably create a dataset which matches those expectations. Also you'll get into problems when migrating from older hosts, and when levelling a policy down with a lower max_leaf.
The reason for having an architectural representation was deliberately to prohibit having a hypercall which was a memcpy of an unstable structure in the background, but with this design comes the fact that Xen must not expect to find an exact match of data.
Quoted 14 lines
Quoted 14 lines
One awkard corner case is re-deserialising of the vcpu msrs. The correct fix would be to allocate a buffer, copy the MSRs list, then deserialise from that, but trips the bounds checks in the
copy_from_guest()helpers. The compat XLAT are would work, but would require that we allocate it even for 64bit PV guests.xen/arch/x86/domctl.c +0
-0@@ -330,6 +330,71 @@ static int update_domain_cpuid_info(struct domain *d,return 0 ; }
Quoted 14 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 345
Quoted 13 lines
xen/arch/x86/domctl.c +13
-0+static int update_domain_cpumsr_policy (struct domain *d, +xen_domctl_cpumsr_policy_t *xdpc) +{ +struct policy_group new = {}; +const struct policy_group *sys =is_pv_domain (d) + ? &system_policies[XEN_SYSCTL_cpumsr_policy_pv_max] + : &system_policies[XEN_SYSCTL_cpumsr_policy_hvm_max]; +struct vcpu *v = d->vcpu[0 ]; +int ret = -ENOMEM ; + +/* Initialise some help identifying auditing errors. */ + xdpc->err_leaf = xdpc->err_subleaf =XEN_CPUID_NO_SUBLEAF ; + xdpc->err_msr_idx = ~0 ;I'm having trouble extracting information from the comment.
So am I...
These are the fields inside the domctl which try to provide some hint as to which piece of data is problematic, rather than leaving the user with simply "somthing is wrong".
Quoted 19 lines
Quoted 19 lines
xen/arch/x86/domctl.c +18
-0+/* Start with existing domain's policies */ +if ( !(new.cp =xmemdup (d->arch.cpuid)) || + !(new.dp =xmemdup (d->arch.msr)) || + !(new.vp =xmemdup (v->arch.msr)) ) + goto out; + +/* Merge the toolstack provided data. */ +if ( (ret =x86_cpuid_copy_from_buffer ( + new.cp, xdpc->cpuid_policy, xdpc->nr_leaves, + &xdpc->err_leaf, &xdpc->err_subleaf)) ) + goto out; + +if ( (ret =x86_msr_copy_from_buffer ( + new.dp, new.vp, + xdpc->msr_policy, xdpc->nr_msrs, &xdpc->err_msr_idx)) ) + goto out; + +/* Audit the combined dataset. */
Quoted 8 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 367
Quoted 3 lines
xen/arch/x86/domctl.c +3
-0+ ret =x86_policies_are_compatible (sys, &new); +if ( ret ) + goto out;I'm afraid I don't follow - where's the merging? All you do is copy the first so many entries coming from libxc, and using the later so many entries from the previous policies. How's that going to provide a complete set, rather than e.g. some duplicate entries and some missing ones?
Consider the case where max_leaf is lower than Xen's maximum. The data provided by the toolstack can be self consistent and complete without matching Xen's exact size of structure.
Quoted 11 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 378
Quoted 11 lines
xen/arch/x86/domctl.c +10
-0+/* +* Audit was successful. Replace existing policies, leaving the old +* policies to be freed. +*/ +SWAP (new.cp, d->arch.cpuid); +SWAP (new.dp, d->arch.msr); +SWAP (new.vp, v->arch.msr); + +/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list )
Quoted 1 line
This open-coded almost-for_each_domain() doesn't look very nice.
ITYM for_each_vcpu()
And yes, but for_each_vcpu() is wrong to use here, and we don't have a for_each_vcpu_other_than_0() helper.
Quoted 8 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 382
Quoted 5 lines
xen/arch/x86/domctl.c +4
-0+ { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ +if ( (ret =x86_msr_copy_from_buffer ( +NULL , v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs,NULL )) )Why can't you go from vCPU 0's
v->arch.msrhere, which is the copied-in (and sanitized) representation already? Also, is it really a good idea to assume all vCPU-s have the same policies?
There are multiple colliding issues which lead to this code, but as several people have pointed out, its probably over the top.
First, as to the same policy. This hypercall can currently only be used before the vcpu has started executing.
As such, it is setting the init state of the MSRs from the guests point of view, and there is exactly one MSR I'm aware of which has an init value which depends on the core (that being APIC_BASE.BSP which can trivially be handled in Xen). All other MSRs have identical init state AFAICT, and I don't want to create an interface which makes it easy to accidentally end up with wrong values.
The re-de-serialising actually came from your suggested usecase to be able to increase the policy at runtime, .e.g. after a microcode update and hypervisor livepatch. In that case, a partial set doesn't want to copy vCPU0's generally R/W MSRs over other vcpus, because that would be a bad thing.
However, these two points aren't compatible, so if we accept the "only before the domain has started running" aspect, then it should be safe to copy v0's policy over all other vcpus.
Really, the problem here comes from (necessarily) how we will have to implement SGX_LC and the fact that the LC Hash MSRs may be read-only domain-wide state, or read-write per-vcpu state depending on a different MSR setting (MSR_FEAT_CTRL.SGX_LC).
I was initially hoping to have only msr_domain_policy poked via this interface, and msr_vcpu_policy poked exclusively via per-vcpu hypercalls. Then again, at least the complexity is visible at this point, rather than at some point in the future when we need to try and retrofit it.
Quoted 16 lines
Quoted 16 lines
xen/arch/x86/domctl.c +11
-0@@ -1570,6 +1635,28 @@ long arch_do_domctl(domain_unpause (d);break ; +case XEN_DOMCTL_set_cpumsr_policy: +if ( d == currd ||/* no domain_pause() */ + d->max_vcpus ==0 )/* No vcpus yet. */ + { + ret = -EINVAL ; +break ; + } + +domain_pause (d); + +if ( d->creation_finished )
Quoted 8 lines
xen/arch/x86/domctl.c long arch_do_domctl( · line 1654
Quoted 6 lines
xen/arch/x86/domctl.c +6
-0+ ret = -EEXIST ;/* No changing once the domain is running. */ + else + { + ret =update_domain_cpumsr_policy (d, &domctl->u.cpumsr_policy); +if ( !ret )/* Copy domctl->u.cpumsr_policy.err_* to guest. */ + copyback = true;
x86_cpuid_copy_from_buffer(), for example, sets theerr_fields only when returning-ERANGE. Is theif()condition inverted?
Oops yes. This was part of a blanket "ret > 0" correction I needed to do because the privcmd() ioctl interface isn't sufficiently expressive.
Quoted 22 lines
xen/include/public/domctl.h struct xen_domctl_cpumsr_policy { · line 656
Quoted 13 lines
xen/include/public/domctl.h +6
-0@@ -648,6 +648,12 @@ struct xen_domctl_cpumsr_policy {* 'msr_domain_policy' */ XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* IN/OUT: */ XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* IN/OUT: */ +uint32_t err_leaf, err_subleaf;/* OUT, set_policy only. If not ~0, +* indicates the leaf/subleaf which +* auditing objected to. */ +uint32_t err_msr_idx;/* OUT, set_policy only. If not ~0, +* indicates the MSR idx which +* auditing objected to. */ Explicit padding again please, with the handler checking it to be zero.
Quoted 6 lines
xen/include/xen/xmalloc.h +0
-0@@ -13,6 +13,13 @@#define xmalloc (_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))#define xzalloc (_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type)))
Quoted 10 lines
xen/include/xen/xmalloc.h line 21
Quoted 6 lines
xen/include/xen/xmalloc.h +6
-0+/* Allocate space for a typed object and copy an existing instance. */ +#define xmemdup (ptr) \ + ({ typeof(*ptr) *n_ = xmalloc(typeof(*ptr)); \ + if ( n_ ) \ + memcpy(n_, ptr, sizeof(*ptr)); \ + n_; })Would be nice if this could handle input pointers to const-qualified types. I vaguely recall having seen a solution to this recently, but I don't recall where that was or how it looked like. Until then, may I suggest to use void * instead, despite this opening the risk of type incompatibilities?
The only way I'm aware of which might fix the const qualified aspect is _Generic(), but ISTR you can't use typeof() inside.
As for xmemdup() specific, no - this will never want const qualified types. The caller is always going to mutate the structure, or they wouldn't have dup'd the object to begin with.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 30 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 20:47, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
From: Sergey Dyasli <[email protected]>
This hypercall allows the toolstack to present one combined CPUID and MSR policy for a domain, which can be audited in one go by Xen, which is necessary for correctness of the auditing.
A stub
x86_policies_are_compatible()function is introduced, although at present it will always fail the hypercall.Quoted 7 lines
Quoted 3 lines
The hypercall ABI allows for update of individual CPUID or MSR entries, so begins by duplicating the existing policy (for which a helper is introduced), merging the toolstack data, then checking compatibility of the result.
This reads to me as if it was fine for the tool stack to supply only partial data (or else there would be no need to merge anything). What's the thinking behind this, rather than requiring complete sets of data to be supplied?
Unless you have an identical build of Xen and libxc, the toolstack won't always provide the same leaves as Xen expects. Such a restriction would massively hamper development.
More importantly, with the
max_extd_leafbeing vendor dependent (which is data earlier in the structure), the toolstack can't reasonably create a dataset which matches those expectations. Also you'll get into problems when migrating from older hosts, and when levelling a policy down with a lowermax_leaf.
Quoted 4 lines
The reason for having an architectural representation was deliberately to prohibit having a hypercall which was a memcpy of an unstable structure in the background, but with this design comes the fact that Xen must not expect to find an exact match of data.
Okay, I can see the point together with the migration aspect you've mentioned in another reply on this series.
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/domctl.c +0
-0@@ -330,6 +330,71 @@ static int update_domain_cpuid_info(struct domain *d,return 0 ; }Quoted 14 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 345
Quoted 13 lines
xen/arch/x86/domctl.c +13
-0+static int update_domain_cpumsr_policy (struct domain *d, +xen_domctl_cpumsr_policy_t *xdpc) +{ +struct policy_group new = {}; +const struct policy_group *sys =is_pv_domain (d) + ? &system_policies[XEN_SYSCTL_cpumsr_policy_pv_max] + : &system_policies[XEN_SYSCTL_cpumsr_policy_hvm_max]; +struct vcpu *v = d->vcpu[0 ]; +int ret = -ENOMEM ; + +/* Initialise some help identifying auditing errors. */ + xdpc->err_leaf = xdpc->err_subleaf =XEN_CPUID_NO_SUBLEAF ; + xdpc->err_msr_idx = ~0 ;I'm having trouble extracting information from the comment.
Quoted 5 lines
So am I...
These are the fields inside the domctl which try to provide some hint as to which piece of data is problematic, rather than leaving the user with simply "somthing is wrong".
Yeah, I did understand the purpose of the fields. I take it the comment will be dropped or improved then.
Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
xen/arch/x86/domctl.c +18
-0+/* Start with existing domain's policies */ +if ( !(new.cp =xmemdup (d->arch.cpuid)) || + !(new.dp =xmemdup (d->arch.msr)) || + !(new.vp =xmemdup (v->arch.msr)) ) + goto out; + +/* Merge the toolstack provided data. */ +if ( (ret =x86_cpuid_copy_from_buffer ( + new.cp, xdpc->cpuid_policy, xdpc->nr_leaves, + &xdpc->err_leaf, &xdpc->err_subleaf)) ) + goto out; + +if ( (ret =x86_msr_copy_from_buffer ( + new.dp, new.vp, + xdpc->msr_policy, xdpc->nr_msrs, &xdpc->err_msr_idx)) ) + goto out; + +/* Audit the combined dataset. */ Quoted 8 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 367
Quoted 3 lines
xen/arch/x86/domctl.c +3
-0+ ret =x86_policies_are_compatible (sys, &new); +if ( ret ) + goto out;I'm afraid I don't follow - where's the merging? All you do is copy the first so many entries coming from libxc, and using the later so many entries from the previous policies. How's that going to provide a complete set, rather than e.g. some duplicate entries and some missing ones?
Quoted 3 lines
Consider the case where
max_leafis lower than Xen's maximum. The data provided by the toolstack can be self consistent and complete without matching Xen's exact size of structure.
Right. The question on proper merging remains though: Shouldn't CPUID entries be matched by leaf/subleaf, and MSR ones by MSR index? Currently there's an implied ordering agreement of the arrays between caller and callee.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+/* +* Audit was successful. Replace existing policies, leaving the old +* policies to be freed. +*/ +SWAP (new.cp, d->arch.cpuid); +SWAP (new.dp, d->arch.msr); +SWAP (new.vp, v->arch.msr); + +/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list )
Quoted 3 lines
Quoted 1 line
This open-coded almost-for_each_domain() doesn't look very nice.
ITYM
for_each_vcpu()
Oops, of course.
Quoted 2 lines
And yes, but
for_each_vcpu()is wrong to use here, and we don't have afor_each_vcpu_other_than_0()helper.
Perhaps still better to do
for_each_vcpu(d, v)
{
if ( v->vcpu_id == 0 )
continue;
...
}?
Quoted 14 lines
Quoted 7 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 382
Quoted 4 lines
xen/arch/x86/domctl.c +4
-0+ { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ +if ( (ret =x86_msr_copy_from_buffer ( +NULL , v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs,NULL )) )Why can't you go from vCPU 0's
v->arch.msrhere, which is the copied-in (and sanitized) representation already? Also, is it really a good idea to assume all vCPU-s have the same policies?There are multiple colliding issues which lead to this code, but as several people have pointed out, its probably over the top.
First, as to the same policy. This hypercall can currently only be used before the vcpu has started executing.
Quoted 6 lines
As such, it is setting the init state of the MSRs from the guests point of view, and there is exactly one MSR I'm aware of which has an init value which depends on the core (that being APIC_BASE.BSP which can trivially be handled in Xen). All other MSRs have identical init state AFAICT, and I don't want to create an interface which makes it easy to accidentally end up with wrong values.
So what about migration? There are certainly differing incoming values there. Of course there's the MSRs restore record, but no atomic sanity check between those and the policy here is possible.
Quoted 9 lines
The re-de-serialising actually came from your suggested usecase to be able to increase the policy at runtime, .e.g. after a microcode update and hypervisor livepatch. In that case, a partial set doesn't want to copy vCPU0's generally R/W MSRs over other vcpus, because that would be a bad thing.
However, these two points aren't compatible, so if we accept the "only before the domain has started running" aspect, then it should be safe to copy v0's policy over all other vcpus.
I think the description of the change wants to be extended to cover this.
Quoted 10 lines
Really, the problem here comes from (necessarily) how we will have to implement
SGX_LCand the fact that the LC Hash MSRs may be read-only domain-wide state, or read-write per-vcpu state depending on a different MSR setting (MSR_FEAT_CTRL.SGX_LC).I was initially hoping to have only
msr_domain_policypoked via this interface, andmsr_vcpu_policypoked exclusively via per-vcpu hypercalls. Then again, at least the complexity is visible at this point, rather than at some point in the future when we need to try and retrofit it.
Oh, that's certainly very worthwhile; I wasn't even aware of this SGX induced challenge.
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
xen/include/xen/xmalloc.h +0
-0@@ -13,6 +13,13 @@#define xmalloc (_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))#define xzalloc (_type) ((_type *)_xzalloc(sizeof(_type), __alignof__(_type)))Quoted 10 lines
xen/include/xen/xmalloc.h line 21
Quoted 6 lines
xen/include/xen/xmalloc.h +6
-0+/* Allocate space for a typed object and copy an existing instance. */ +#define xmemdup (ptr) \ + ({ typeof(*ptr) *n_ = xmalloc(typeof(*ptr)); \ + if ( n_ ) \ + memcpy(n_, ptr, sizeof(*ptr)); \ + n_; })Would be nice if this could handle input pointers to const-qualified types. I vaguely recall having seen a solution to this recently, but I don't recall where that was or how it looked like. Until then, may I suggest to use void * instead, despite this opening the risk of type incompatibilities?
Quoted 6 lines
The only way I'm aware of which might fix the const qualified aspect is
_Generic(), but ISTR you can't usetypeof()inside.As for
xmemdup()specific, no - this will never want const qualified types. The caller is always going to mutate the structure, or they wouldn't have dup'd the object to begin with.
I don't follow: Why wouldn't a caller possibly want to mutate a clone of a const input structure? What is pointless to be const id the output structure, yes, but typeof(*(ptr)) [note the missing parentheses in your code, btw] includes the possible const from what the input points to.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 19 lines · Jan Beulich
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/domctl.c +10
-0+/* +* Audit was successful. Replace existing policies, leaving the old +* policies to be freed. +*/ +SWAP (new.cp, d->arch.cpuid); +SWAP (new.dp, d->arch.msr); +SWAP (new.vp, v->arch.msr); + +/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list )Quoted 2 lines
Quoted 1 line
This open-coded almost-for_each_domain() doesn't look very nice.
ITYM
for_each_vcpu()Oops, of course.
Quoted 2 lines
And yes, but
for_each_vcpu()is wrong to use here, and we don't have afor_each_vcpu_other_than_0()helper.Perhaps still better to do
Quoted 8 lines
for_each_vcpu(d, v) { if ( v->vcpu_id == 0 ) continue; ... }?
Do you think that looks cleaner? I doubt the compiler can optimise that into my version.
Quoted 20 lines
Quoted 14 lines
Quoted 8 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 382
Quoted 5 lines
xen/arch/x86/domctl.c +4
-0+ { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ +if ( (ret =x86_msr_copy_from_buffer ( +NULL , v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs,NULL )) )Why can't you go from vCPU 0's
v->arch.msrhere, which is the copied-in (and sanitized) representation already? Also, is it really a good idea to assume all vCPU-s have the same policies?There are multiple colliding issues which lead to this code, but as several people have pointed out, its probably over the top.
First, as to the same policy. This hypercall can currently only be used before the vcpu has started executing.
Quoted 6 lines
As such, it is setting the init state of the MSRs from the guests point of view, and there is exactly one MSR I'm aware of which has an init value which depends on the core (that being APIC_BASE.BSP which can trivially be handled in Xen). All other MSRs have identical init state AFAICT, and I don't want to create an interface which makes it easy to accidentally end up with wrong values.
Quoted 3 lines
So what about migration? There are certainly differing incoming values there. Of course there's the MSRs restore record, but no atomic sanity check between those and the policy here is possible.
Migration is still a problem. This CPUID/MSR work is the next step on the path to fixing the "state before policy" problem we've got when restoring a guest.
Once we have a working CPUID and R/O MSR configuration "blob" which the toolstack can manipulate, we can (in Xen) require that the toolstack provide the blob before memory and R/W register state.
When we get to that point, the toolstack shall call DOMCTL_get_cpumsr_policy (modulo whatever plan I device to fix our R/W MSR from the VCPU state problem) and place this ahead of the main memory/register state in the migration stream.
The receiving side will either feed the blob back to Xen, or fabricate the policy out of thin air at this point. The current behaviour is to fabricate a policy out of thin air after all migration stream is complete, but before unpausing the domain.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 05.07.18 at 19:55, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/domctl.c +10
-0+/* +* Audit was successful. Replace existing policies, leaving the old +* policies to be freed. +*/ +SWAP (new.cp, d->arch.cpuid); +SWAP (new.dp, d->arch.msr); +SWAP (new.vp, v->arch.msr); + +/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list )Quoted 2 lines
Quoted 1 line
This open-coded almost-for_each_domain() doesn't look very nice.
ITYM
for_each_vcpu()Oops, of course.
Quoted 2 lines
And yes, but
for_each_vcpu()is wrong to use here, and we don't have afor_each_vcpu_other_than_0()helper.Perhaps still better to do
for_each_vcpu(d, v) { if ( v->vcpu_id == 0 ) continue; ... }
Quoted 3 lines
Quoted 1 line
?
Do you think that looks cleaner?
Yes. There is exactly one other open coded use of next_in_list in x86 code (with the asm-offsets cleanup in place that I've just sent), and I'd prefer to see that go away as well (but it's less obvious what a reasonable replacement would look like there).
Quoted 1 line
I doubt the compiler can optimise that into my version.
I'm pretty sure it can't, but I'm also pretty sure it doesn't matter here.
Quoted 23 lines
Quoted 19 lines
Quoted 13 lines
Quoted 7 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 382
Quoted 4 lines
xen/arch/x86/domctl.c +4
-0+ { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ +if ( (ret =x86_msr_copy_from_buffer ( +NULL , v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs,NULL )) )Why can't you go from vCPU 0's
v->arch.msrhere, which is the copied-in (and sanitized) representation already? Also, is it really a good idea to assume all vCPU-s have the same policies?There are multiple colliding issues which lead to this code, but as several people have pointed out, its probably over the top.
First, as to the same policy. This hypercall can currently only be used before the vcpu has started executing.
Quoted 6 lines
As such, it is setting the init state of the MSRs from the guests point of view, and there is exactly one MSR I'm aware of which has an init value which depends on the core (that being APIC_BASE.BSP which can trivially be handled in Xen). All other MSRs have identical init state AFAICT, and I don't want to create an interface which makes it easy to accidentally end up with wrong values.
Quoted 3 lines
So what about migration? There are certainly differing incoming values there. Of course there's the MSRs restore record, but no atomic sanity check between those and the policy here is possible.
Quoted 7 lines
Migration is still a problem. This CPUID/MSR work is the next step on the path to fixing the "state before policy" problem we've got when restoring a guest.
Once we have a working CPUID and R/O MSR configuration "blob" which the toolstack can manipulate, we can (in Xen) require that the toolstack provide the blob before memory and R/W register state.
Hmm, you talk about r/o MSRs here only, but this then covers the domain policy object only when looking at what we currently have. Both MSRs in the vCPU policy object are r/w ones, and hence I'd like it to be at least clear what the interaction between the policy and other MSR restore is supposed to be in the end.
This is in particular relevant wrt the derivation of data from vCPU 0 here. With what you do currently, you already make the code dependent upon the MSRs record coming after the configuration done here, or else the cloning of vCPU 0 register values would clobber the intended (restored) ones.
Quoted 4 lines
When we get to that point, the toolstack shall call
DOMCTL_get_cpumsr_policy(modulo whatever plan I device to fix our R/W MSR from the VCPU state problem) and place this ahead of the main memory/register state in the migration stream.
It is perhaps the case that r/w MSRs weren't actually meant to go into the policy structures?
Quoted 4 lines
The receiving side will either feed the blob back to Xen, or fabricate the policy out of thin air at this point. The current behaviour is to fabricate a policy out of thin air after all migration stream is complete, but before unpausing the domain.
I.e., as mentioned above, you clobber the values coming from the CPU_MSR record. I don't think this can be the way to go, not even temporarily.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 49 lines · Jan Beulich
Quoted 23 lines
Quoted 20 lines
Quoted 14 lines
Quoted 8 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 382
Quoted 5 lines
xen/arch/x86/domctl.c +4
-0+ { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ +if ( (ret =x86_msr_copy_from_buffer ( +NULL , v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs,NULL )) )Why can't you go from vCPU 0's
v->arch.msrhere, which is the copied-in (and sanitized) representation already? Also, is it really a good idea to assume all vCPU-s have the same policies?There are multiple colliding issues which lead to this code, but as several people have pointed out, its probably over the top.
First, as to the same policy. This hypercall can currently only be used before the vcpu has started executing.
Quoted 6 lines
As such, it is setting the init state of the MSRs from the guests point of view, and there is exactly one MSR I'm aware of which has an init value which depends on the core (that being APIC_BASE.BSP which can trivially be handled in Xen). All other MSRs have identical init state AFAICT, and I don't want to create an interface which makes it easy to accidentally end up with wrong values.
Quoted 3 lines
So what about migration? There are certainly differing incoming values there. Of course there's the MSRs restore record, but no atomic sanity check between those and the policy here is possible.
Quoted 7 lines
Migration is still a problem. This CPUID/MSR work is the next step on the path to fixing the "state before policy" problem we've got when restoring a guest.
Once we have a working CPUID and R/O MSR configuration "blob" which the toolstack can manipulate, we can (in Xen) require that the toolstack provide the blob before memory and R/W register state.
Hmm, you talk about r/o MSRs here only, but this then covers the domain policy object only when looking at what we currently have. Both MSRs in the vCPU policy object are r/w ones, and hence I'd like it to be at least clear what the interaction between the policy and other MSR restore is supposed to be in the end.
This is in particular relevant wrt the derivation of data from vCPU 0 here. With what you do currently, you already make the code dependent upon the MSRs record coming after the configuration done here, or else the cloning of vCPU 0 register values would clobber the intended (restored) ones.
Quoted 4 lines
When we get to that point, the toolstack shall call
DOMCTL_get_cpumsr_policy(modulo whatever plan I device to fix our R/W MSR from the VCPU state problem) and place this ahead of the main memory/register state in the migration stream.It is perhaps the case that r/w MSRs weren't actually meant to go into the policy structures?
Quoted 7 lines
Quoted 4 lines
The receiving side will either feed the blob back to Xen, or fabricate the policy out of thin air at this point. The current behaviour is to fabricate a policy out of thin air after all migration stream is complete, but before unpausing the domain.
I.e., as mentioned above, you clobber the values coming from the
CPU_MSRrecord. I don't think this can be the way to go, not even temporarily.
Jan and I have discussed this on IRC and have a plan.
The only reason msr_vcpu_policy was included in XEN_DOMCTL_set_cpumsr_policy was for SGX_LC, and because SGX_LC introduces some MSRs which may want to be RO domain-wide settings or RW per-vcpu settings. We can fix this by having an SGX_LC set in both domain and vcpu blocks, as which block to use is determined by a different RO setting.
As a result, we can retain the current separation of RO vs RW MSRs and limit XEN_DOMCTL_set_cpumsr_policy to just the domain MSRs. This simplifies things massively, and still allows SGX_LC to coexist in a fairly clean manner.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
In reply to Andrew CooperQuoted 26 lines · Andrew Cooper
From: Sergey Dyasli <[email protected]>
This hypercall allows the toolstack to present one combined CPUID and MSR policy for a domain, which can be audited in one go by Xen, which is necessary for correctness of the auditing.
A stub
x86_policies_are_compatible()function is introduced, although at present it will always fail the hypercall.The hypercall ABI allows for update of individual CPUID or MSR entries, so begins by duplicating the existing policy (for which a helper is introduced), merging the toolstack data, then checking compatibility of the result.
The system PV/HVM max policy is used for the compatiblity check.
Signed-off-by Sergey Dyasli <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Daniel De Graaf <[email protected]>
Quoted 5 lines
One awkard corner case is re-deserialising of the vcpu msrs. The correct fix would be to allocate a buffer, copy the MSRs list, then deserialise from that, but trips the bounds checks in the
copy_from_guest()helpers. The compat XLAT are would work, but would require that we allocate it even for 64bit PV guests.
I'm not sure I follow this. The issue isn't obvious from looking at the code.
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 380
Quoted 5 lines
---
xen/arch/x86/domctl.c +4
-0+/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list ) + { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */
What is the TOCTOU race here?
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 13/13] x86/domctl: Implement XEN_DOMCTL_set_cpumsr_policy
Quoted 35 lines · Wei Liu
Quoted 26 lines · Andrew Cooper
From: Sergey Dyasli <[email protected]>
This hypercall allows the toolstack to present one combined CPUID and MSR policy for a domain, which can be audited in one go by Xen, which is necessary for correctness of the auditing.
A stub
x86_policies_are_compatible()function is introduced, although at present it will always fail the hypercall.The hypercall ABI allows for update of individual CPUID or MSR entries, so begins by duplicating the existing policy (for which a helper is introduced), merging the toolstack data, then checking compatibility of the result.
The system PV/HVM max policy is used for the compatiblity check.
Signed-off-by Sergey Dyasli <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
CC: Daniel De Graaf <[email protected]>Quoted 5 lines
One awkard corner case is re-deserialising of the vcpu msrs. The correct fix would be to allocate a buffer, copy the MSRs list, then deserialise from that, but trips the bounds checks in the
copy_from_guest()helpers. The compat XLAT are would work, but would require that we allocate it even for 64bit PV guests.I'm not sure I follow this. The issue isn't obvious from looking at the code.
Quoted 6 lines
xen/arch/x86/domctl.c static int update_domain_cpuid_info(struct domain *d, · line 380
Quoted 5 lines
---
xen/arch/x86/domctl.c +4
-0+/* Merge the (now audited) vCPU MSRs into every other msr_vcpu_policy. */ +for ( ; v; v = v->next_in_list ) + { +/* XXX - Figure out how to avoid a TOCTOU race here. XLAT area? */ What is the TOCTOU race here?
In the lines you snipped...
Quoted 4 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ if ( (ret = x86_msr_copy_from_buffer( + NULL, v->arch.msr, xdpc->msr_policy, xdpc->nr_msrs, NULL)) )
xdpc->msr_policy is a pointer into toolstack userspace, which we re-read.
The problem is that we can't copy this up front to a hypervisor buffer (which would avoid the TOCTOU race), without tripping the access_ok() check in copy_from_guest().
However, Jan and Sergey (IRL) have suggested an alternative approach which will work for now.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
Some open questions:
* The position of libx86 in the source tree. It probably doesn't want to live in its current location.
So did you intentionally decide against ...
Quoted 11 lines
.gitignore | 2 +- tools/include/Makefile | 15 ++ tools/libxc/Makefile | 14 +- tools/libxc/include/xenctrl.h | 15 +- tools/libxc/xc_cpuid_x86.c | 163 +++++++++++++++++--- tools/misc/xen-cpuid.c | 135 +++++++++++++++- tools/tests/x86_emulator/x86-emulate.h | 2 + xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/cpuid.c | 32 +--- xen/arch/x86/domctl.c | 133 ++++++++++++++++ xen/arch/x86/msr.c | 4 +-
Quoted 8 lines
xen/arch/x86/sysctl.c | 101 ++++++++++++ xen/arch/x86/x86_emulate/x86_emulate.h | 7 +- xen/common/Makefile | 1 + xen/common/libx86/Makefile | 3 + xen/common/libx86/cpuid.c | 226 +++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 74 +++++++++ xen/common/libx86/msr.c | 134 ++++++++++++++++ xen/common/libx86/policies.c | 19 +++
xen/lib/x86/... or lib/x86/... here and ...
Quoted 10 lines
xen/include/Makefile | 6 +-
xen/include/asm-x86/cpufeatures.h | 2 +-
xen/include/asm-x86/cpuid.h | 228 +---------------------------
xen/include/asm-x86/msr.h | 65 ++------
xen/include/public/arch-x86/xen.h | 18 +++
xen/include/public/domctl.h | 25 +++
xen/include/public/sysctl.h | 41 +++++
xen/include/{asm-x86 => xen/libx86}/cpuid.h | 103 +++++--------
xen/include/xen/libx86/msr.h | 78 ++++++++++
xen/include/xen/libx86/policies.h | 35 +++++xen/include/lib/x86/... or include/lib/x86/... here, as suggested on irc? Personally I'd favor the top level variants, as that makes sufficiently clear that the code is neither specific to the hypervisor nor specific to the tools.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 38 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 03.07.18 at 22:55, <[email protected]> wrote:
Some open questions:
* The position of libx86 in the source tree. It probably doesn't want to live in its current location.
So did you intentionally decide against ...
Quoted 11 lines
.gitignore | 2 +- tools/include/Makefile | 15 ++ tools/libxc/Makefile | 14 +- tools/libxc/include/xenctrl.h | 15 +- tools/libxc/xc_cpuid_x86.c | 163 +++++++++++++++++--- tools/misc/xen-cpuid.c | 135 +++++++++++++++- tools/tests/x86_emulator/x86-emulate.h | 2 + xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/cpuid.c | 32 +--- xen/arch/x86/domctl.c | 133 ++++++++++++++++ xen/arch/x86/msr.c | 4 +-Quoted 8 lines
xen/arch/x86/sysctl.c | 101 ++++++++++++ xen/arch/x86/x86_emulate/x86_emulate.h | 7 +- xen/common/Makefile | 1 + xen/common/libx86/Makefile | 3 + xen/common/libx86/cpuid.c | 226 +++++++++++++++++++++++++++ xen/common/libx86/libx86-private.h | 74 +++++++++ xen/common/libx86/msr.c | 134 ++++++++++++++++ xen/common/libx86/policies.c | 19 +++xen/lib/x86/... or
lib/x86/... here and ...Quoted 10 lines
xen/include/Makefile | 6 +- xen/include/asm-x86/cpufeatures.h | 2 +- xen/include/asm-x86/cpuid.h | 228 +--------------------------- xen/include/asm-x86/msr.h | 65 ++------ xen/include/public/arch-x86/xen.h | 18 +++ xen/include/public/domctl.h | 25 +++ xen/include/public/sysctl.h | 41 +++++ xen/include/{asm-x86 => xen/libx86}/cpuid.h | 103 +++++-------- xen/include/xen/libx86/msr.h | 78 ++++++++++ xen/include/xen/libx86/policies.h | 35 +++++
Quoted 2 lines
xen/include/lib/x86/... or
include/lib/x86/... here, as suggested on irc?
I tried, got stuck, then decided it would be better use of time to post a v1 for review.
Once a general decision has been made, I'll try to remember/work out how to use git filter-branch without it trying to delete my entire history.
Quoted 3 lines
Personally I'd favor the top level variants, as that makes sufficiently clear that the code is neither specific to the hypervisor nor specific to the tools.
That's the concern I've got with the top level variant. I view this as hypervisor code which we also want to use in libxc, rather than specifically separate code.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 04.07.18 at 12:40, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Personally I'd favor the top level variants, as that makes sufficiently clear that the code is neither specific to the hypervisor nor specific to the tools.
That's the concern I've got with the top level variant. I view this as hypervisor code which we also want to use in libxc, rather than specifically separate code.
But isn't the nature of library code to be used in different environments? Hence why would you want this specifically qualified as hypervisor code?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel