x86: Be able to retrieve system and domain policies
v3 cover x86: Be able to retrieve system and domain policies
Its long-since time to repost this work, seeing as it is blocking parts of the speculative improvement work already discussed. See indiviudal patches for details.
Andrew Cooper (2):
libx86: Introduce a helper to serialise cpuid_policy objects
x86: Introduce struct cpu_policy to refer to a group of individual policies
Roger Pau Monné (1):
libx86: Introduce a helper to serialise msr_policy objects
Sergey Dyasli (2):
x86/sysctl: Implement XEN_SYSCTL_get_cpu_policy
x86/domctl: Implement XEN_DOMCTL_get_cpu_policy
tools/flask/policy/modules/dom0.te | 2 +- tools/flask/policy/modules/xen.if | 2 +- tools/libxc/Makefile | 2 +- tools/libxc/include/xenctrl.h | 9 +++ tools/libxc/xc_cpuid_x86.c | 95 ++++++++++++++++++++++++++ tools/misc/xen-cpuid.c | 128 ++++++++++++++++++++++++++++++++++- xen/arch/x86/domctl.c | 32 +++++++++ xen/arch/x86/sysctl.c | 74 ++++++++++++++++++++ xen/include/asm-x86/cpuid.h | 3 + xen/include/public/arch-x86/xen.h | 18 +++++ xen/include/public/domctl.h | 18 +++++ xen/include/public/sysctl.h | 37 ++++++++++ xen/include/xen/lib/x86/cpu-policy.h | 24 +++++++ xen/include/xen/lib/x86/cpuid.h | 36 ++++++++++ xen/include/xen/lib/x86/msr.h | 26 +++++++ xen/lib/x86/Makefile | 1 + xen/lib/x86/cpuid.c | 91 +++++++++++++++++++++++++ xen/lib/x86/msr.c | 58 ++++++++++++++++ xen/lib/x86/private.h | 21 ++++++ xen/xsm/flask/hooks.c | 4 ++ xen/xsm/flask/policy/access_vectors | 4 +- 21 files changed, 678 insertions(+), 7 deletions(-) create mode 100644 xen/include/xen/lib/x86/cpu-policy.h create mode 100644 xen/lib/x86/msr.c
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 1/5 libx86: Introduce a helper to serialise cpuid_policy objects
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.
The serialisation of the Xen/Viridian leaves isn't fully implemented yet. It is just enough to be bug-compatible with the current DOMCTL_set_cpuid behaviour, but needs further hypervisor work before the toolstack can sensibly control these values.
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: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
v2:
* Move MIN() into xen-tools/libs.h
* Leave a TODO for the Xen/Viridian leaf infrastructure, and update the commit message
* Rewrite copy_to_buffer_offset() to avoid multiple evaluation of its parameters
* Change to an array typedef for constness reasons
v3:
* Rebase over libx86's movement
* Remove unnecessary brackets in copy_to_buffer_offset() and include extra type safety
--- xen/include/public/arch-x86/xen.h | 11 +++++ xen/include/xen/lib/x86/cpuid.h | 36 ++++++++++++++++ xen/lib/x86/cpuid.c | 91 +++++++++++++++++++++++++++++++++++++++ xen/lib/x86/private.h | 18 ++++++++ 4 files changed, 156 insertions(+)
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/lib/x86/cpuid.h +36 -0
@@ -45,6 +45,19 @@ static inline void cpuid_count_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)
@@ -248,6 +261,29 @@ void x86_cpuid_policy_fill_native(struct cpuid_policy *p);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 + +/** +* Serialise a cpuid_policy object into an array of cpuid leaves. +* +* @param policy The cpuid_policy to serialise. +* @param leaves The array of leaves to serialise into. +* @param nr_entries The number of entries in 'leaves'. +* @returns -errno +* +* Writes at most CPUID_MAX_SERIALISED_LEAVES. May fail with -ENOBUFS if the +* leaves array is too short. On success, nr_entries is updated with the +* actual number of leaves written. +*/ +int x86_cpuid_copy_to_buffer (const struct cpuid_policy *policy, +cpuid_leaf_buffer_t leaves,uint32_t *nr_entries); +#endif /* !XEN_LIB_X86_CPUID_H */ /*
xen/lib/x86/cpuid.c +91 -0
97 lines not on this page · raw message
xen/lib/x86/private.h +18 -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>
@@ -23,6 +28,19 @@ 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(*(src)) *src_ = (src); \ + typeof(*(dst)) *dst_ = (dst); \ + typeof(index) index_ = (index); \ + typeof(nr) nr_ = (nr), i_; \ + \ + for ( i_ = 0; i_ < nr_; i_++ ) \ + dst_[index_ + i_] = src_[i_]; \ + 0; \ +}) +#endif /* __XEN__ */ #endif /* XEN_LIB_X86_PRIVATE_H */
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 1/5] libx86: Introduce a helper to serialise cpuid_policy objects
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 05.11.18 at 12:16, <[email protected]> wrote:
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.
The serialisation of the Xen/Viridian leaves isn't fully implemented yet. It
is just enough to be bug-compatible with the currentDOMCTL_set_cpuidbehaviour, but needs further hypervisor work before the toolstack can sensibly
control these values.
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.
Quoted 4 lines
Signed-off-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Signed-off-by Sergey Dyasli <[email protected]>Reviewed-by Wei Liu <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 2/5 libx86: Introduce a helper to serialise msr_policy objects
In reply to Andrew CooperFrom: Roger Pau Monné <[email protected]>
As with CPUID, 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: Roger Pau Monné <[email protected]>
CC: Sergey Dyasli <[email protected]>
v2:
* Rebase over the msr_{domain,vcpu}_policy rename
* Only serialise msr_policy
* Change to an array typedef for constness reasons
v3:
* Rebase over libx86's movement
--- tools/libxc/Makefile | 2 +- xen/include/public/arch-x86/xen.h | 9 +++++- xen/include/xen/lib/x86/msr.h | 26 ++++++++++++++++++ xen/lib/x86/Makefile | 1 + xen/lib/x86/msr.c | 58 +++++++++++++++++++++++++++++++++++++++ xen/lib/x86/private.h | 3 ++ 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 xen/lib/x86/msr.c
tools/libxc/Makefile +1 -1
@@ -83,7 +83,7 @@ $(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign ifeq ($(CONFIG_X86),y) # Add libx86 to the build vpath %.c ../../xen/lib/x86 -GUEST_SRCS-y += cpuid.c +GUEST_SRCS-y += cpuid.c msr.c endif # new domain builder
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/lib/x86/msr.h +26 -0
@@ -2,6 +2,9 @@#ifndef XEN_LIB_X86_MSR_H #define XEN_LIB_X86_MSR_H +/* Maximum number of MSRs written when serialising msr_policy. */ +#define MSR_MAX_SERIALISED_ENTRIES 1 +/* MSR policy object for shared per-domain MSRs */ struct msr_policy {
@@ -22,6 +25,29 @@ struct msr_policy } plaform_info; }; +#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 + +/** +* Serialise an msr_policy object into an array. +* +* @param policy The msr_policy to serialise. +* @param msrs The array of msrs to serialise into. +* @param nr_entries The number of entries in 'msrs'. +* @returns -errno +* +* Writes at most MSR_MAX_SERIALISED_ENTRIES. May fail with -ENOBUFS if the +* buffer array is too short. On success, nr_entries is updated with the +* actual number of msrs written. +*/ +int x86_msr_copy_to_buffer (const struct msr_policy *policy, +msr_entry_buffer_t msrs,uint32_t *nr_entries); +#endif /* !XEN_LIB_X86_MSR_H */ /*
xen/lib/x86/Makefile +1 -0
@@ -1 +1,2 @@ obj-y += cpuid.o +obj-y += msr.o
xen/lib/x86/msr.c new file +58 -0
58 lines not on this page · raw message
xen/lib/x86/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> static inline bool test_bit (unsigned int bit,const void *vaddr)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 3/5 x86: Introduce struct cpu_policy to refer to a group of individual policies
In reply to Andrew CooperThis is prep work for the following patch - please refer to it as well.
When auditing and manipulating policies, it is necessary to do so with a complete set of policies, due to the interdependences of the contents. A containing structure like this will allow for clearer APIs and code.
As a first user, this structure is convenient for the mapping used by XEN_SYSCTL_get_cpu_policy (implemented in the next patch), and for auditing (later when XEN_DOMCTL_set_cpu_policy is implemented).
At this point, the distinction between *_max and *_default is introduced into the ABI. For now, *_default is mapped to *_max, but future development work will result in *_default being a logical subset of *_max.
---
CC: Jan Beulich <[email protected]>
CC: Sergey Dyasli <[email protected]>
v2:
* Drop __read_mostly from MSR declarations. Fixes clang build.
* s/policy_group/cpu_policy/g s/cpumsr_policy/cpu_policy/g
* Rebase over the msr_{domain,vcpu}_policy rename
* Don't include vcpu_msrs in the cpu_policy
--- xen/arch/x86/sysctl.c | 27 +++++++++++++++++++++++++++ xen/include/asm-x86/cpuid.h | 3 +++ xen/include/public/sysctl.h | 20 ++++++++++++++++++++ xen/include/xen/lib/x86/cpu-policy.h | 24 ++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 xen/include/xen/lib/x86/cpu-policy.h
xen/arch/x86/sysctl.c +27 -0
@@ -32,6 +32,33 @@#include <asm/psr.h> #include <asm/cpuid.h> +const struct cpu_policy system_policies[] = { + [ XEN_SYSCTL_cpu_policy_raw ] = { + &raw_cpuid_policy, + &raw_msr_policy, + }, + [ XEN_SYSCTL_cpu_policy_host ] = { + &host_cpuid_policy, + &host_msr_policy, + }, + [ XEN_SYSCTL_cpu_policy_pv_max ] = { + &pv_max_cpuid_policy, + &pv_max_msr_policy, + }, + [ XEN_SYSCTL_cpu_policy_hvm_max ] = { + &hvm_max_cpuid_policy, + &hvm_max_msr_policy, + }, + [ XEN_SYSCTL_cpu_policy_pv_default ] = { + &pv_max_cpuid_policy, + &pv_max_msr_policy, + }, + [ XEN_SYSCTL_cpu_policy_hvm_default ] = { + &hvm_max_cpuid_policy, + &hvm_max_msr_policy, + }, +}; +struct l3_cache_info {int ret;unsigned long size;
xen/include/asm-x86/cpuid.h +3 -0
@@ -8,6 +8,7 @@#include <xen/types.h> #include <xen/kernel.h> +#include <xen/lib/x86/cpu-policy.h> #include <xen/lib/x86/cpuid.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 cpu_policy system_policies[]; +/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu);
xen/include/public/sysctl.h +20 -0
@@ -1063,6 +1063,26 @@ struct xen_sysctl_set_parameter {uint16_t pad[3 ];/* IN: MUST be zero. */ }; +#if defined(__i386__) || defined(__x86_64__) +/* +* XEN_SYSCTL_get_cpu_policy (x86 specific) +* +* 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. +*/ +#define XEN_SYSCTL_cpu_policy_raw 0 +#define XEN_SYSCTL_cpu_policy_host 1 +#define XEN_SYSCTL_cpu_policy_pv_max 2 +#define XEN_SYSCTL_cpu_policy_hvm_max 3 +#define XEN_SYSCTL_cpu_policy_pv_default 4 +#define XEN_SYSCTL_cpu_policy_hvm_default 5 +#endif +struct xen_sysctl {uint32_t cmd;#define XEN_SYSCTL_readconsole 1
xen/include/xen/lib/x86/cpu-policy.h new file +24 -0
@@ -0,0 +1,24 @@ +/* Common data structures and functions consumed by hypervisor and toolstack */ +#ifndef XEN_LIB_X86_POLICIES_H +#define XEN_LIB_X86_POLICIES_H + +#include <xen/lib/x86/cpuid.h> +#include <xen/lib/x86/msr.h> + +struct cpu_policy +{ +struct cpuid_policy *cpuid; +struct msr_policy *msr; +}; + +#endif /* !XEN_LIB_X86_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 v3 3/5] x86: Introduce struct cpu_policy to refer to a group of individual policies
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.11.18 at 12:16, <[email protected]> wrote:
This is prep work for the following patch - please refer to it as well.
When auditing and manipulating policies, it is necessary to do so with a complete set of policies, due to the interdependences of the contents. A containing structure like this will allow for clearer APIs and code.
As a first user, this structure is convenient for the mapping used by
XEN_SYSCTL_get_cpu_policy(implemented in the next patch), and for auditing (later whenXEN_DOMCTL_set_cpu_policyis implemented).
Quoted 7 lines
At this point, the distinction between
*_maxand*_defaultis introduced into the ABI. For now,*_defaultis mapped to*_max, but future development work will result in*_defaultbeing a logical subset of*_max.Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Roger Pau Monné <[email protected]>Reviewed-by Wei Liu <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 4/5 x86/sysctl: Implement XEN_SYSCTL_get_cpu_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.
For the flask 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---
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]>
v2:
* Avoid introducing a Spectre V1 gadget
v3:
* Reorganise logic
--- tools/libxc/include/xenctrl.h | 6 +++ tools/libxc/xc_cpuid_x86.c | 57 ++++++++++++++++++++++++ tools/misc/xen-cpuid.c | 86 +++++++++++++++++++++++++++++++++++-- xen/arch/x86/sysctl.c | 47 ++++++++++++++++++++ xen/include/public/sysctl.h | 17 ++++++++ xen/xsm/flask/hooks.c | 1 + xen/xsm/flask/policy/access_vectors | 2 +- 7 files changed, 212 insertions(+), 4 deletions(-)
tools/libxc/include/xenctrl.h +6 -0
@@ -2533,6 +2533,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_cpu_policy_size (xc_interface *xch,uint32_t *nr_leaves, +uint32_t *nr_msrs); +int xc_get_system_cpu_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 +57 -0
63 lines not on this page · raw message
tools/misc/xen-cpuid.c +83 -3
115 lines not on this page · raw message
xen/arch/x86/sysctl.c +47 -0
59 lines not on this page · raw message
xen/include/public/sysctl.h +17 -0
@@ -1075,12 +1075,25 @@ struct xen_sysctl_set_parameter {* - Default_*: Default set of features a PV or HVM guest can use. This is * the security supported set. */ +struct xen_sysctl_cpu_policy {#define XEN_SYSCTL_cpu_policy_raw 0#define XEN_SYSCTL_cpu_policy_host 1#define XEN_SYSCTL_cpu_policy_pv_max 2#define XEN_SYSCTL_cpu_policy_hvm_max 3#define XEN_SYSCTL_cpu_policy_pv_default 4#define XEN_SYSCTL_cpu_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 the guest handle is NULL. */ +uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_policy', or the maximum number of MSRs if +* the guest handle is NULL. */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT */ +XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* OUT */ +}; +typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t ; +DEFINE_XEN_GUEST_HANDLE (xen_sysctl_cpu_policy_t);#endif struct xen_sysctl {
@@ -1112,6 +1125,7 @@ struct xen_sysctl {#define XEN_SYSCTL_get_cpu_featureset 26#define XEN_SYSCTL_livepatch_op 27#define XEN_SYSCTL_set_parameter 28 +#define XEN_SYSCTL_get_cpu_policy 29uint32_t interface_version;/* XEN_SYSCTL_INTERFACE_VERSION */ union {struct xen_sysctl_readconsole readconsole;
@@ -1141,6 +1155,9 @@ struct xen_sysctl {struct xen_sysctl_cpu_featureset cpu_featureset;struct xen_sysctl_livepatch_op livepatch;struct xen_sysctl_set_parameter set_parameter; +#if defined(__i386__) || defined(__x86_64__) +struct xen_sysctl_cpu_policy cpu_policy; +#endif uint8_t pad[128 ]; } u; };
xen/xsm/flask/hooks.c +1 -0
@@ -795,6 +795,7 @@ static int flask_sysctl(int cmd)case XEN_SYSCTL_cputopoinfo:case XEN_SYSCTL_numainfo:case XEN_SYSCTL_pcitopoinfo: +case XEN_SYSCTL_get_cpu_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_cpu_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 v3 4/5] x86/sysctl: Implement XEN_SYSCTL_get_cpu_policy
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 05.11.18 at 12:16, <[email protected]> wrote:
xen/include/public/sysctl.h +1
-0@@ -1075,12 +1075,25 @@ struct xen_sysctl_set_parameter {* - Default_*: Default set of features a PV or HVM guest can use. This is * the security supported set. */ +struct xen_sysctl_cpu_policy {#define XEN_SYSCTL_cpu_policy_raw 0#define XEN_SYSCTL_cpu_policy_host 1#define XEN_SYSCTL_cpu_policy_pv_max 2#define XEN_SYSCTL_cpu_policy_hvm_max 3
xen/include/public/sysctl.h struct xen_sysctl_set_parameter { · line 1092
Quoted 10 lines
xen/include/public/sysctl.h +8
-0#define XEN_SYSCTL_cpu_policy_pv_default 4#define XEN_SYSCTL_cpu_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 the guest handle is NULL. */ +uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to +* 'msr_policy', or the maximum number of MSRs if +* the guest handle is NULL. */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT */
I'm sorry for being picky, but I (continue to) think there should be explicit padding added above here, checked to be zero on input. With this hypervisor parts
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 4/5] x86/sysctl: Implement XEN_SYSCTL_get_cpu_policy
In reply to Andrew CooperQuoted 54 lines · Andrew Cooper
From: Sergey Dyasli <[email protected]>
Provide a SYSCTL for the toolstack to obtain complete system CPUID and MSR policy information.
For the flask 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 -> 0000000080000000Signed-off-by Andrew Cooper <[email protected]>Signed-off-by Sergey Dyasli <[email protected]>Signed-off-by Roger Pau Monné <[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 8 lines
v2:
* Avoid introducing a Spectre V1 gadget
v3:
* Reorganise logic--- tools/libxc/include/xenctrl.h | 6 +++ tools/libxc/xc_cpuid_x86.c | 57 ++++++++++++++++++++++++ tools/misc/xen-cpuid.c | 86 +++++++++++++++++++++++++++++++++++--
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH v3 5/5 x86/domctl: Implement XEN_DOMCTL_get_cpu_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.
Introduce a new flask access vector and update the default policies.
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]>
v3:
* Misc code hygene
* Fix comments to avoid having a SYSCTL/DOMCTL paste error
* Introduce a new flask vector rather than reusing set_cpuid
--- tools/flask/policy/modules/dom0.te | 2 +- tools/flask/policy/modules/xen.if | 2 +- tools/libxc/include/xenctrl.h | 3 ++ tools/libxc/xc_cpuid_x86.c | 38 ++++++++++++++++++++++++ tools/misc/xen-cpuid.c | 58 ++++++++++++++++++++++++++++++++----- xen/arch/x86/domctl.c | 32 ++++++++++++++++++++ xen/include/public/domctl.h | 18 ++++++++++++ xen/xsm/flask/hooks.c | 3 ++ xen/xsm/flask/policy/access_vectors | 2 ++ 9 files changed, 148 insertions(+), 10 deletions(-)
tools/flask/policy/modules/dom0.te +1 -1
@@ -39,7 +39,7 @@ allow dom0_t dom0_t:domain { }; allow dom0_t dom0_t:domain2 { set_cpuid gettsc settsc setscheduler set_vnumainfo - get_vnumainfo psr_cmt_op psr_alloc + get_vnumainfo psr_cmt_op psr_alloc get_cpu_policy }; allow dom0_t dom0_t:resource { add remove };
tools/flask/policy/modules/xen.if +1 -1
@@ -53,7 +53,7 @@ define(`create_domain_common', ` allow $1 $2:domain2 { set_cpuid settsc setscheduler setclaim set_vnumainfo get_vnumainfo cacheflush psr_cmt_op psr_alloc soft_reset - resource_map }; + resource_map get_cpu_policy }; allow $1 $2:security check_context; allow $1 $2:shadow enable; allow $1 $2:mmu { map_read map_write adjust memorymap physmap pinpage mmuext_op updatemp };
tools/libxc/include/xenctrl.h +3 -0
@@ -2538,6 +2538,9 @@ int xc_get_cpu_policy_size(xc_interface *xch, uint32_t *nr_leaves,int xc_get_system_cpu_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_cpu_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 +38 -0
@@ -189,6 +189,44 @@ int xc_get_system_cpu_policy(xc_interface *xch, uint32_t index,return ret; } +int xc_get_domain_cpu_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) || +xc_hypercall_bounce_pre (xch, msrs) ) +return -1 ; + + domctl.cmd = XEN_DOMCTL_get_cpu_policy; + domctl.domain = domid; + domctl.u.cpu_policy.nr_leaves = *nr_leaves; +set_xen_guest_handle (domctl.u.cpu_policy.cpuid_policy, leaves); + domctl.u.cpu_policy.nr_msrs = *nr_msrs; +set_xen_guest_handle (domctl.u.cpu_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.cpu_policy.nr_leaves; + *nr_msrs = domctl.u.cpu_policy.nr_msrs; + } + +return ret; +} +struct cpuid_domain_info { enum
tools/misc/xen-cpuid.c +50 -8
108 lines not on this page · raw message
xen/arch/x86/domctl.c +32 -0
@@ -1528,6 +1528,38 @@ long arch_do_domctl(recalculate_cpuid_policy (d);break ; +case XEN_DOMCTL_get_cpu_policy: +/* Process the CPUID leaves. */ +if (guest_handle_is_null (domctl->u.cpu_policy.cpuid_policy) ) + domctl->u.cpu_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ; +else if ( (ret =x86_cpuid_copy_to_buffer ( + d->arch.cpuid, + domctl->u.cpu_policy.cpuid_policy, + &domctl->u.cpu_policy.nr_leaves)) ) +break ; + +if (__copy_field_to_guest (u_domctl, domctl, + u.cpu_policy.nr_leaves) ) + { + ret = -EFAULT ; +break ; + } + +/* Process the MSR entries. */ +if (guest_handle_is_null (domctl->u.cpu_policy.msr_policy) ) + domctl->u.cpu_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ; +else if ( (ret =x86_msr_copy_to_buffer ( + d->arch.msr, + domctl->u.cpu_policy.msr_policy, + &domctl->u.cpu_policy.nr_msrs)) ) +break ; + +if (__copy_field_to_guest (u_domctl, domctl, + u.cpu_policy.nr_msrs) ) + ret = -EFAULT ; + +break ; +default : ret =iommu_do_domctl (domctl, d, u_domctl);break ;
xen/include/public/domctl.h +18 -0
@@ -652,6 +652,22 @@ struct xen_domctl_cpuid {uint32_t ecx; uint32_t edx; }; + +/* +* XEN_DOMCTL_get_cpu_policy (x86 specific) +* +* Query the CPUID and MSR policies for a specific domain. +*/ +struct xen_domctl_cpu_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;/* OUT */ +XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* OUT */ +}; +typedef struct xen_domctl_cpu_policy xen_domctl_cpu_policy_t ; +DEFINE_XEN_GUEST_HANDLE (xen_domctl_cpu_policy_t);#endif /*
@@ -1177,6 +1193,7 @@ struct xen_domctl {#define XEN_DOMCTL_soft_reset 79/* #define XEN_DOMCTL_set_gnttab_limits 80 - Moved into XEN_DOMCTL_createdomain */ #define XEN_DOMCTL_vuart_op 81 +#define XEN_DOMCTL_get_cpu_policy 82#define XEN_DOMCTL_gdbsx_guestmemio 1000#define XEN_DOMCTL_gdbsx_pausevcpu 1001#define XEN_DOMCTL_gdbsx_unpausevcpu 1002
@@ -1221,6 +1238,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_cpu_policy cpu_policy;struct xen_domctl_vcpuextstate vcpuextstate;struct xen_domctl_vcpu_msrs vcpu_msrs;#endif
xen/xsm/flask/hooks.c +3 -0
@@ -742,6 +742,9 @@ static int flask_domctl(struct domain *d, int cmd)case XEN_DOMCTL_soft_reset:return current_has_perm (d,SECCLASS_DOMAIN2 ,DOMAIN2__SOFT_RESET ); +case XEN_DOMCTL_get_cpu_policy: +return current_has_perm (d,SECCLASS_DOMAIN2 ,DOMAIN2__GET_CPU_POLICY ); +default :return avc_unknown_permission ("domctl" , cmd); }
xen/xsm/flask/policy/access_vectors +2 -0
@@ -248,6 +248,8 @@ class domain2 psr_alloc # XENMEM_resource_map resource_map +# XEN_DOMCTL_get_cpu_policy + get_cpu_policy } # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 5/5] x86/domctl: Implement XEN_DOMCTL_get_cpu_policy
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 05.11.18 at 12:16, <[email protected]> wrote:
xen/arch/x86/domctl.c +19
-0@@ -1528,6 +1528,38 @@ long arch_do_domctl(recalculate_cpuid_policy (d);break ; +case XEN_DOMCTL_get_cpu_policy: +/* Process the CPUID leaves. */ +if (guest_handle_is_null (domctl->u.cpu_policy.cpuid_policy) ) + domctl->u.cpu_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ; +else if ( (ret =x86_cpuid_copy_to_buffer ( + d->arch.cpuid, + domctl->u.cpu_policy.cpuid_policy, + &domctl->u.cpu_policy.nr_leaves)) ) +break ; + +if (__copy_field_to_guest (u_domctl, domctl, + u.cpu_policy.nr_leaves) ) + { + ret = -EFAULT ; +break ; + } + +/* Process the MSR entries. */ +if (guest_handle_is_null (domctl->u.cpu_policy.msr_policy) )
xen/arch/x86/domctl.c long arch_do_domctl( · line 1559
Quoted 10 lines
xen/arch/x86/domctl.c +10
-0+ domctl->u.cpu_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ; + else if ( (ret = x86_msr_copy_to_buffer( + d->arch.msr, + domctl->u.cpu_policy.msr_policy, + &domctl->u.cpu_policy.nr_msrs)) ) + break; + +if (__copy_field_to_guest (u_domctl, domctl, + u.cpu_policy.nr_msrs) ) + ret = -EFAULT ;
Is it really worthwhile having extra code to copy back two fields, rather than just setting copyback to true? Preferably with this changed, hypervisor side
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 5/5] x86/domctl: Implement XEN_DOMCTL_get_cpu_policy
In reply to Andrew CooperQuoted 25 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.
Introduce a new flask access vector and update the default policies.
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]>---
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]>v3:
* Misc code hygene
* Fix comments to avoid having a SYSCTL/DOMCTL paste error
* Introduce a new flask vector rather than reusingset_cpuid
Quoted 6 lines
--- tools/flask/policy/modules/dom0.te | 2 +- tools/flask/policy/modules/xen.if | 2 +- tools/libxc/include/xenctrl.h | 3 ++ tools/libxc/xc_cpuid_x86.c | 38 ++++++++++++++++++++++++ tools/misc/xen-cpuid.c | 58 ++++++++++++++++++++++++++++++++-----
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 5/5] x86/domctl: Implement XEN_DOMCTL_get_cpu_policy
In reply to Andrew CooperQuoted 6 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.
Quoted 7 lines
Introduce a new flask access vector and update the default policies.
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]>
Quoted 14 lines
tools/misc/xen-cpuid.c +2
-0@@ -3,6 +3,8 @@#include <err.h> #include <getopt.h> #include <string.h> +#include <errno.h> +#include <limits.h> #include <xenctrl.h>
tools/misc/xen-cpuid.c int main(int argc, char **argv) · line 314
Quoted 4 lines
tools/misc/xen-cpuid.c +1
-0@@ -309,11 +311,13 @@ int main(int argc, char **argv) {enum {MODE_UNKNOWN ,MODE_INFO ,MODE_DETAIL ,MODE_INTERPRET ,MODE_POLICY } mode =MODE_UNKNOWN ; +int domid =-1 ;
Would it be better to use DOMID_INVALID instead of -1?
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH v3 5/5] x86/domctl: Implement XEN_DOMCTL_get_cpu_policy
Quoted 12 lines · Roger Pau Monné
Quoted 11 lines · Andrew Cooper
tools/misc/xen-cpuid.c +2
-0@@ -3,6 +3,8 @@#include <err.h> #include <getopt.h> #include <string.h> +#include <errno.h> +#include <limits.h>
Quoted 8 lines
tools/misc/xen-cpuid.c int main(int argc, char **argv) · line 314
Quoted 7 lines
tools/misc/xen-cpuid.c +1
-0#include <xenctrl.h> @@ -309,11 +311,13 @@ int main(int argc, char **argv) {enum {MODE_UNKNOWN ,MODE_INFO ,MODE_DETAIL ,MODE_INTERPRET ,MODE_POLICY } mode =MODE_UNKNOWN ; +int domid =-1 ;Would it be better to use
DOMID_INVALIDinstead of -1?
That doesn't exist anywhere consistent or useful to use in this case. (Also, confusingly, DOMID_INVALID is 0x7ff4 in the public ABI, which is different to the INVALID_DOMID of ~0/-1 used by various bits of libxc and libxl)
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel