lib{xc,xl}: support for guest MSR features
v2 cover lib{xc,xl}: support for guest MSR features
Hello,
The following series adds support for handling guest MSR features as defined in arch-x86/cpufeatureset.h.
The end result is the user being able to use such features with the xl.cfg(5) cpuid option. This also involves adding support to all the underlying layers, so both libxl and libxc also get new functionality in order to properly parse those.
Thanks, Roger.
Roger Pau Monne (6):
libs/guest: introduce support for setting guest MSRs
libxl: change the type of libxl_cpuid_policy_list
libxl: introduce MSR data in libxl_cpuid_policy
libxl: split logic to parse user provided CPUID features
libxl: use the cpuid feature names from cpufeatureset.h
libxl: add support for parsing MSR features
docs/man/xl.cfg.5.pod.in | 24 +- tools/include/libxl.h | 8 +- tools/include/xenctrl.h | 21 +- tools/libs/guest/xg_cpuid_x86.c | 168 +++++++++- tools/libs/light/libxl_cpuid.c | 527 ++++++++++++++++-------------- tools/libs/light/libxl_internal.h | 5 + tools/xl/xl_parse.c | 3 + 7 files changed, 479 insertions(+), 277 deletions(-)
Signature · 1 line
2.41.0
PATCH v2 1/6 libs/guest: introduce support for setting guest MSRs
Like it's done with CPUID, introduce support for passing MSR values to xc_cpuid_apply_policy(). The chosen format for expressing MSR policy data matches the current one used for CPUID. Note that existing callers of xc_cpuid_apply_policy() can pass NULL as the value for the newly introduced 'msr' parameter in order to preserve the same functionality, and in fact that's done in libxl on this patch.
--- tools/include/xenctrl.h | 21 +++- tools/libs/guest/xg_cpuid_x86.c | 168 +++++++++++++++++++++++++++++++- tools/libs/light/libxl_cpuid.c | 2 +- 3 files changed, 187 insertions(+), 4 deletions(-)
tools/include/xenctrl.h +19 -2
@@ -1822,6 +1822,21 @@ struct xc_xend_cpuid {char *policy[4 ]; }; +/* +* MSR policy data. +* +* The format of the policy string is the following: +* '1' -> force to 1 +* '0' -> force to 0 +* 'x' -> we don't care (use default) +* 'k' -> pass through host value +*/ +struct xc_msr { +uint32_t index; +char policy[65 ]; +}; +#define XC_MSR_INPUT_UNUSED 0xffffffffu +/* * Make adjustments to the CPUID settings for a domain. *
@@ -1833,13 +1848,15 @@ struct xc_xend_cpuid {* Either pass a full new @featureset (and @nr_features), or adjust individual * features (@pae, @itsc, @nested_virt). * -* Then (optionally) apply legacy XEND overrides (@xend) to the result. +* Then (optionally) apply legacy XEND CPUID overrides (@xend) or MSR (@msr) +* to the result. */ int xc_cpuid_apply_policy (xc_interface *xch,uint32_t domid,bool restore,const uint32_t *featureset,unsigned int nr_features,bool pae,bool itsc, -bool nested_virt,const struct xc_xend_cpuid *xend); +bool nested_virt,const struct xc_xend_cpuid *xend, +const struct xc_msr *msr);int xc_mca_op (xc_interface *xch,struct xen_mc *mc);int xc_mca_op_inject_v2 (xc_interface *xch,unsigned int flags,xc_cpumap_t cpumap,unsigned int nr_cpus);
tools/libs/guest/xg_cpuid_x86.c +167 -1
183 lines not on this page · raw message
tools/libs/light/libxl_cpuid.c +1 -1
@@ -502,7 +502,7 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, info->tsc_mode == LIBXL_TSC_MODE_ALWAYS_EMULATE); r =xc_cpuid_apply_policy (ctx->xch, domid, restore,NULL ,0 , - pae, itsc, nested_virt, info->cpuid); + pae, itsc, nested_virt, info->cpuid,NULL );if (r)LOGEVD (ERROR , -r, domid,"Failed to apply CPUID policy" );
Signature · 1 line
2.41.0
Re: [PATCH v2 1/6] libs/guest: introduce support for setting guest MSRs
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3
-0+static int xc_msr_policy (xc_interface *xch,domid_t domid, +const struct xc_msr *msr) +{
[...]
tools/libs/guest/xg_cpuid_x86.c
Quoted 2 lines
tools/libs/guest/xg_cpuid_x86.c +2
-0+ + rc = -ENOMEM ;
This `rc' value looks unused, should it be moved to the if true block?
Quoted 12 lines
tools/libs/guest/xg_cpuid_x86.c +12
-0+if ( (host =calloc (nr_msrs,sizeof (*host))) ==NULL || + (def =calloc (nr_msrs,sizeof (*def))) ==NULL || + (cur =calloc (nr_msrs,sizeof (*cur))) ==NULL ) + { +ERROR ("Unable to allocate memory for %u CPUID leaves" , nr_leaves); + goto fail; + } + +/* Get the domain's current policy. */ + nr_leaves =0 ; + nr_cur = nr_msrs; + rc =get_domain_cpu_policy (xch, domid, &nr_leaves,NULL , &nr_cur, cur);
[...]
Quoted 20 lines
tools/libs/guest/xg_cpuid_x86.c +20
-0+for ( ; msr->index !=XC_MSR_INPUT_UNUSED ; ++msr ) + { +xen_msr_entry_t *cur_msr =find_msr (cur, nr_cur, msr->index); +const xen_msr_entry_t *def_msr =find_msr (def, nr_def, msr->index); +const xen_msr_entry_t *host_msr =find_msr (host, nr_host, msr->index); +unsigned int i; + +if ( cur_msr ==NULL || def_msr ==NULL || host_msr ==NULL ) + { +ERROR ("Missing MSR %#x" , msr->index); + rc = -ENOENT ; + goto fail; + } + +for ( i =0 ; i <ARRAY_SIZE (msr->policy) -1 ; i++ ) + { +bool val; + +if ( msr->policy[i] =='1' ) + val = true;
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 552
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10
-0+else if ( msr->policy[i] =='0' ) + val = false; +else if ( msr->policy[i] =='x' ) + val =test_bit (63 - i, &def_msr->val); +else if ( msr->policy[i] =='k' ) + val =test_bit (63 - i, &host_msr->val); +else + { +ERROR ("Bad character '%c' in policy string '%s'" , + msr->policy[i], msr->policy);
Would it be useful to also display msr->index?
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 559
Quoted 7 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0+ rc = -EINVAL ; + goto fail; + } + +clear_bit (63 - i, &cur_msr->val); +if ( val ) +set_bit (63 - i, &cur_msr->val);
Does this need to be first clear then set? A opposed to just do one or the other.
Quoted 7 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0+ } + } + +/* Feed the transformed policy back up to Xen. */ + rc =xc_set_domain_cpu_policy (xch, domid,0 ,NULL , nr_cur, cur, + &err_leaf, &err_subleaf, &err_msr); +if ( rc )
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 576
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10
-0+ { +PERROR ("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr %#x)" , + domid, err_leaf, err_subleaf, err_msr); + rc = -errno; + goto fail; + } + +/* Success! */ + + fail:
Even if this label is only used on "fail", the code path is also use on success. So a label named "out" might be more appropriate.
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 583
Quoted 7 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0+free (cur); +free (def); +free (host); + +return rc; +} +
In any case, patch looks fine to me:
Thanks,
Signature · 1 line
Anthony PERARD
Re: [PATCH v2 1/6] libs/guest: introduce support for setting guest MSRs
Quoted 9 lines · Anthony PERARD
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3
-0+static int xc_msr_policy (xc_interface *xch,domid_t domid, +const struct xc_msr *msr) +{[...]
Quoted 4 lines
tools/libs/guest/xg_cpuid_x86.c
Quoted 2 lines
tools/libs/guest/xg_cpuid_x86.c +2
-0+ + rc = -ENOMEM ;This `rc' value looks unused, should it be moved to the if true block?
This is mostly copy&paste from the cpuid counterpart, some parts of the code tend to use this kind of error assignation, but it only makes sense when the if is the code block is a single line in order to avoid the braces. Since here the code block already requires braces, doing the assign outside is pointless.
Quoted 37 lines
Quoted 12 lines
tools/libs/guest/xg_cpuid_x86.c +12
-0+if ( (host =calloc (nr_msrs,sizeof (*host))) ==NULL || + (def =calloc (nr_msrs,sizeof (*def))) ==NULL || + (cur =calloc (nr_msrs,sizeof (*cur))) ==NULL ) + { +ERROR ("Unable to allocate memory for %u CPUID leaves" , nr_leaves); + goto fail; + } + +/* Get the domain's current policy. */ + nr_leaves =0 ; + nr_cur = nr_msrs; + rc =get_domain_cpu_policy (xch, domid, &nr_leaves,NULL , &nr_cur, cur);[...]
Quoted 24 lines
tools/libs/guest/xg_cpuid_x86.c +24
-0+for ( ; msr->index !=XC_MSR_INPUT_UNUSED ; ++msr ) + { +xen_msr_entry_t *cur_msr =find_msr (cur, nr_cur, msr->index); +const xen_msr_entry_t *def_msr =find_msr (def, nr_def, msr->index); +const xen_msr_entry_t *host_msr =find_msr (host, nr_host, msr->index); +unsigned int i; + +if ( cur_msr ==NULL || def_msr ==NULL || host_msr ==NULL ) + { +ERROR ("Missing MSR %#x" , msr->index); + rc = -ENOENT ; + goto fail; + } + +for ( i =0 ; i <ARRAY_SIZE (msr->policy) -1 ; i++ ) + { +bool val; + +if ( msr->policy[i] =='1' ) + val = true; +else if ( msr->policy[i] =='0' ) + val = false; +else if ( msr->policy[i] =='x' ) + val =test_bit (63 - i, &def_msr->val);
Quoted 8 lines
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 552
Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +6
-0+else if ( msr->policy[i] =='k' ) + val =test_bit (63 - i, &host_msr->val); +else + { +ERROR ("Bad character '%c' in policy string '%s'" , + msr->policy[i], msr->policy);Would it be useful to also display
msr->index?
Why not, will add it.
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 559
Quoted 7 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0+ rc = -EINVAL ; + goto fail; + } + +clear_bit (63 - i, &cur_msr->val); +if ( val ) +set_bit (63 - i, &cur_msr->val);Does this need to be first clear then set? A opposed to just do one or the other.
Will adjust, I assume some people prefer 3 lines instead of 4 if you use and if ... else ...?
Quoted 30 lines
Quoted 7 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0+ } + } + +/* Feed the transformed policy back up to Xen. */ + rc =xc_set_domain_cpu_policy (xch, domid,0 ,NULL , nr_cur, cur, + &err_leaf, &err_subleaf, &err_msr); +if ( rc )tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 576
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10
-0+ { +PERROR ("Failed to set d%d's policy (err leaf %#x, subleaf %#x, msr %#x)" , + domid, err_leaf, err_subleaf, err_msr); + rc = -errno; + goto fail; + } + +/* Success! */ + + fail:Even if this label is only used on "fail", the code path is also use on success. So a label named "out" might be more appropriate.
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 583
Quoted 7 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0+free (cur); +free (def); +free (host); + +return rc; +} +In any case, patch looks fine to me:
Quoted 1 line
Acked-by Anthony PERARD <[email protected]>
Will make the adjustments requested.
Thanks, Roger.
Re: [PATCH v2 1/6] libs/guest: introduce support for setting guest MSRs
In reply to Roger Pau MonneQuoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +0
-0@@ -423,10 +423,169 @@ static int xc_cpuid_xend_policy(return rc; }
tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 434
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +9
-0+static int compare_msr (const void *l,const void *r) +{ +const xen_msr_entry_t *lhs = l; +const xen_msr_entry_t *rhs = r; + +if ( lhs->idx == rhs->idx ) +return 0 ; + +return lhs->idx < rhs->idx ?-1 :1 ;
The sum total of logic here is just
return lhs->idx - rhs->idx;
(I think. Double check which way around the subtraction works.)
You don't need to return -1, 0 or 1. You only need negative, 0 or positive.
~Andrew
Re: [PATCH v2 1/6] libs/guest: introduce support for setting guest MSRs
Quoted 21 lines · Andrew Cooper
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +0
-0@@ -423,10 +423,169 @@ static int xc_cpuid_xend_policy(return rc; }tools/libs/guest/xg_cpuid_x86.c static int xc_cpuid_xend_policy( · line 434
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +9
-0+static int compare_msr (const void *l,const void *r) +{ +const xen_msr_entry_t *lhs = l; +const xen_msr_entry_t *rhs = r; + +if ( lhs->idx == rhs->idx ) +return 0 ; + +return lhs->idx < rhs->idx ?-1 :1 ;The sum total of logic here is just
Quoted 3 lines
return
lhs->idx-rhs->idx;(I think. Double check which way around the subtraction works.)
Since MSR index is a 32bit value, what about one index being ~0u and the other 0u: the result would then wrongly be -1 ((int)(~0u - 0u)), when it should instead be a positive value to denote the left hand side is greater than the right hand side.
Thanks, Roger.
PATCH v2 2/6 libxl: change the type of libxl_cpuid_policy_list
In reply to Roger Pau MonneCurrently libxl_cpuid_policy_list is an opaque type to the users of libxl, and internally it's an array of xc_xend_cpuid objects.
Change the type to instead be a structure that contains one array for CPUID policies, in preparation for it also holding another array for MSR policies.
--- tools/include/libxl.h | 8 +-- tools/libs/light/libxl_cpuid.c | 89 ++++++++++++++++++++----------- tools/libs/light/libxl_internal.h | 4 ++ 3 files changed, 65 insertions(+), 36 deletions(-)
tools/include/libxl.h +2 -6
@@ -1455,12 +1455,8 @@ typedef struct {void libxl_bitmap_init (libxl_bitmap *map);void libxl_bitmap_dispose (libxl_bitmap *map); -/* -* libxl_cpuid_policy is opaque in the libxl ABI. Users of both libxl and -* libxc may not make assumptions about xc_xend_cpuid. -*/ -typedef struct xc_xend_cpuid libxl_cpuid_policy ; -typedef libxl_cpuid_policy *libxl_cpuid_policy_list ; +struct libxl__cpu_policy ; +typedef struct libxl__cpu_policy *libxl_cpuid_policy_list ;void libxl_cpuid_dispose (libxl_cpuid_policy_list *cpuid_list);int libxl_cpuid_policy_list_length (const libxl_cpuid_policy_list *l); void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
tools/libs/light/libxl_cpuid.c +59 -30
185 lines not on this page · raw message
tools/libs/light/libxl_internal.h +4 -0
@@ -4869,6 +4869,10 @@ int libxl__setresuid(uid_t ruid, uid_t euid, uid_t suid); _hidden int libxl__domain_set_paging_mempool_size(libxl__gc *gc,libxl_domain_config *d_config,uint32_t domid); +struct libxl__cpu_policy { +struct xc_xend_cpuid *cpuid; +}; +#endif /*
Signature · 1 line
2.41.0
Re: [PATCH v2 2/6] libxl: change the type of libxl_cpuid_policy_list
Quoted 17 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +4
-10-void libxl_cpuid_dispose (libxl_cpuid_policy_list *p_cpuid_list) +void libxl_cpuid_dispose (libxl_cpuid_policy_list *pl) { -int i, j; -libxl_cpuid_policy_list cpuid_list = *p_cpuid_list; +libxl_cpuid_policy_list policy = *pl; -if (cpuid_list ==NULL ) +if (policy ==NULL )return ; -for (i =0 ; cpuid_list[i].input[0 ] !=XEN_CPUID_INPUT_UNUSED ; i++) { -for (j =0 ; j <4 ; j++) -if (cpuid_list[i].policy[j] !=NULL ) { -free (cpuid_list[i].policy[j]); - cpuid_list[i].policy[j] =NULL ; - } +
tools/libs/light/libxl_cpuid.c int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl) · line 38
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+if (policy->cpuid) { +unsigned int i, j; +struct xc_xend_cpuid *cpuid_list = policy->cpuid; + +for (i =0 ; cpuid_list[i].input[0 ] !=XEN_CPUID_INPUT_UNUSED ; i++) { +for (j =0 ; j <4 ; j++) +if (cpuid_list[i].policy[j] !=NULL ) { +free (cpuid_list[i].policy[j]); + cpuid_list[i].policy[j] =NULL ; + }
This looks like a lot of work. We could just call free(cpuid_list[i].policy[j]) and that's all, as cpuid_list will be gone right after the loop.
Also, please add {} for the second "for ()" loop.
tools/libs/light/libxl_cpuid.c int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl) · line 41
Quoted 3 lines
tools/libs/light/libxl_cpuid.c +2
-0+ } +free (policy->cpuid); }
Beside some the coding style pointing out, the patch looks fine:
Thanks,
Signature · 1 line
Anthony PERARD
Re: [PATCH v2 2/6] libxl: change the type of libxl_cpuid_policy_list
Quoted 24 lines · Anthony PERARD
Quoted 23 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +10
-10-void libxl_cpuid_dispose (libxl_cpuid_policy_list *p_cpuid_list) +void libxl_cpuid_dispose (libxl_cpuid_policy_list *pl) { -int i, j; -libxl_cpuid_policy_list cpuid_list = *p_cpuid_list; +libxl_cpuid_policy_list policy = *pl; -if (cpuid_list ==NULL ) +if (policy ==NULL )return ; -for (i =0 ; cpuid_list[i].input[0 ] !=XEN_CPUID_INPUT_UNUSED ; i++) { -for (j =0 ; j <4 ; j++) -if (cpuid_list[i].policy[j] !=NULL ) { -free (cpuid_list[i].policy[j]); - cpuid_list[i].policy[j] =NULL ; - } + +if (policy->cpuid) { +unsigned int i, j; +struct xc_xend_cpuid *cpuid_list = policy->cpuid; + +for (i =0 ; cpuid_list[i].input[0 ] !=XEN_CPUID_INPUT_UNUSED ; i++) { +for (j =0 ; j <4 ; j++)
Quoted 8 lines
tools/libs/light/libxl_cpuid.c int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl) · line 38
Quoted 4 lines
tools/libs/light/libxl_cpuid.c +4
-0+if (cpuid_list[i].policy[j] !=NULL ) { +free (cpuid_list[i].policy[j]); + cpuid_list[i].policy[j] =NULL ; + }This looks like a lot of work. We could just call
free(cpuid_list[i].policy[j])and that's all, ascpuid_listwill be gone right after the loop.
I wasn't planning on changing the code, as this is just indentation movement, but will do.
Quoted 8 lines
Also, please add {} for the second "for ()" loop.
tools/libs/light/libxl_cpuid.c int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl) · line 41
Quoted 3 lines
tools/libs/light/libxl_cpuid.c +2
-0+ } +free (policy->cpuid); }Beside some the coding style pointing out, the patch looks fine:
Reviewed-by Anthony PERARD <[email protected]>
Thanks, Roger.
PATCH v2 3/6 libxl: introduce MSR data in libxl_cpuid_policy
In reply to Roger Pau MonneAdd a new array field to libxl_cpuid_policy in order to store the MSR policies.
Note that libxl_cpuid_policy_list_{copy,length,parse_json,gen_json} are not adjusted to deal with the new MSR array now part of libxl_cpuid_policy_list.
Adding the MSR data in the libxl_cpuid_policy_list type is done so that existing users can seamlessly pass MSR features as part of the CPUID data, without requiring the introduction of a separate domain_build_info field, and a new set of handlers functions.
--- tools/libs/light/libxl_cpuid.c | 6 +++++- tools/libs/light/libxl_internal.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-)
tools/libs/light/libxl_cpuid.c +5 -1
@@ -40,6 +40,9 @@ void libxl_cpuid_dispose(libxl_cpuid_policy_list *pl)free (policy->cpuid); } +if (policy->msr) +free (policy->msr); +free (policy); *pl =NULL ;return ;
@@ -516,7 +519,8 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, r =xc_cpuid_apply_policy (ctx->xch, domid, restore,NULL ,0 , pae, itsc, nested_virt, - info->cpuid ? info->cpuid->cpuid :NULL ,NULL ); + info->cpuid ? info->cpuid->cpuid :NULL , + info->cpuid ? info->cpuid->msr :NULL );if (r)LOGEVD (ERROR , -r, domid,"Failed to apply CPUID policy" );
tools/libs/light/libxl_internal.h +1 -0
@@ -4871,6 +4871,7 @@ _hidden int libxl__domain_set_paging_mempool_size(struct libxl__cpu_policy {struct xc_xend_cpuid *cpuid; +struct xc_msr *msr; };#endif
Signature · 1 line
2.41.0
Re: [PATCH v2 3/6] libxl: introduce MSR data in libxl_cpuid_policy
Quoted 6 lines · Roger Pau Monne
Add a new array field to
libxl_cpuid_policyin order to store the MSR policies.Note that libxl_cpuid_policy_list_{copy,length,parse_json,gen_json} are not adjusted to deal with the new MSR array now part of
libxl_cpuid_policy_list.
Why? Isn't this going to be an issue? Or maybe that going to be dealt with in a future patch?
Quoted 17 lines
Adding the MSR data in the
libxl_cpuid_policy_listtype is done so that existing users can seamlessly pass MSR features as part of the CPUID data, without requiring the introduction of a separatedomain_build_infofield, and a new set of handlers functions.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_cpuid.c | 6 +++++- tools/libs/light/libxl_internal.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-)tools/libs/light/libxl_cpuid.c +0
-0
tools/libs/light/libxl_cpuid.c void libxl_cpuid_dispose(libxl_cpuid_policy_list *pl) · line 43
Quoted 4 lines
tools/libs/light/libxl_cpuid.c +1
-0@@ -40,6 +40,9 @@ void libxl_cpuid_dispose(libxl_cpuid_policy_list *pl)free (policy->cpuid); } +if (policy->msr)
You don't need to test for NULL, you can call free() in this case as well.
Quoted 5 lines
tools/libs/light/libxl_cpuid.c +2
-0+free (policy->msr); +free (policy); *pl =NULL ;return ;
Signature · 1 line
Anthony PERARD
Re: [PATCH v2 3/6] libxl: introduce MSR data in libxl_cpuid_policy
Quoted 10 lines · Anthony PERARD
Quoted 6 lines · Roger Pau Monne
Add a new array field to
libxl_cpuid_policyin order to store the MSR policies.Note that libxl_cpuid_policy_list_{copy,length,parse_json,gen_json} are not adjusted to deal with the new MSR array now part of
libxl_cpuid_policy_list.Why? Isn't this going to be an issue? Or maybe that going to be dealt with in a future patch?
I'm unsure what's the point of those? The CPUID/MSR data is passed as a migration stream record from the hypervisor, so I don't see much point into converting it into json. It also seems quite complex, and can't likely we done without breaking (or adjusting) the current format.
My plan was to leave this unimplemented and if someone is in interested in having the data in json they can as well implement it.
Would you be OK if I add a note to the commit message that implementing json marshalling is left to implement for interested parties?
Quoted 20 lines
Quoted 20 lines
Adding the MSR data in the
libxl_cpuid_policy_listtype is done so that existing users can seamlessly pass MSR features as part of the CPUID data, without requiring the introduction of a separatedomain_build_infofield, and a new set of handlers functions.Signed-off-by Roger Pau Monné <[email protected]>--- tools/libs/light/libxl_cpuid.c | 6 +++++- tools/libs/light/libxl_internal.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-)tools/libs/light/libxl_cpuid.c +0
-0@@ -40,6 +40,9 @@ void libxl_cpuid_dispose(libxl_cpuid_policy_list *pl)free (policy->cpuid); }
Quoted 4 lines
tools/libs/light/libxl_cpuid.c void libxl_cpuid_dispose(libxl_cpuid_policy_list *pl) · line 43
Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+if (policy->msr)You don't need to test for NULL, you can call
free()in this case as well.
Right, thanks, will adjust.
Roger.
Re: [PATCH v2 3/6] libxl: introduce MSR data in libxl_cpuid_policy
Quoted 9 lines · Roger Pau Monné
Quoted 8 lines · Anthony PERARD
Quoted 6 lines · Roger Pau Monne
Add a new array field to
libxl_cpuid_policyin order to store the MSR policies.Note that libxl_cpuid_policy_list_{copy,length,parse_json,gen_json} are not adjusted to deal with the new MSR array now part of
libxl_cpuid_policy_list.
Quoted 8 lines
Quoted 2 lines
Why? Isn't this going to be an issue? Or maybe that going to be dealt with in a future patch?
I'm unsure what's the point of those? The CPUID/MSR data is passed as a migration stream record from the hypervisor, so I don't see much point into converting it into json. It also seems quite complex, and can't likely we done without breaking (or adjusting) the current format.
I think this data is used when the machine reboots. I've only try to edit the file "/var/lib/xen/userdata-d.*" and it change which cpu flags where available in the machine after a reboot. You could maybe confirm that when you change an msr value, it is actually conserved across reboot.
Quoted 6 lines
My plan was to leave this unimplemented and if someone is in interested in having the data in json they can as well implement it.
Would you be OK if I add a note to the commit message that implementing json marshalling is left to implement for interested parties?
My guess is, we will have to think of something and add the msr into json, even if it's done in a separate patch.
Cheers,
Signature · 1 line
Anthony PERARD
PATCH v2 4/6 libxl: split logic to parse user provided CPUID features
In reply to Roger Pau MonneMove the CPUID value parsers out of libxl_cpuid_parse_config() into a newly created cpuid_add() local helper. This is in preparation for also adding MSR feature parsing support.
No functional change intended.
--- tools/libs/light/libxl_cpuid.c | 120 +++++++++++++++++---------------- 1 file changed, 63 insertions(+), 57 deletions(-)
tools/libs/light/libxl_cpuid.c +63 -57
@@ -97,6 +97,66 @@ static struct xc_xend_cpuid *cpuid_find_match(libxl_cpuid_policy_list *pl,return *list + i; } +static int cpuid_add (libxl_cpuid_policy_list *policy, +const struct cpuid_flags *flag,const char *val) +{ +struct xc_xend_cpuid *entry =cpuid_find_match (policy, flag->leaf, + flag->subleaf); +unsigned long num; +char flags[33 ], *resstr, *endptr; +unsigned int i; + + resstr = entry->policy[flag->reg -1 ]; + num =strtoull (val, &endptr,0 ); + flags[flag->length] =0 ; +if (endptr != val) { +/* if this was a valid number, write the binary form into the string */ +for (i =0 ; i < flag->length; i++) { + flags[flag->length -1 - i] ="01" [!!(num & (1 << i))]; + } + }else { +switch (val[0 ]) { +case 'x' :case 'k' :case 's' : +memset (flags, val[0 ], flag->length); +break ; +default : +return 3 ; + } + } + +if (resstr ==NULL ) { + resstr =strdup ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ); + } + +/* the family and model entry is potentially split up across +* two fields in Fn0000_0001_EAX, so handle them here separately. +*/ +if (!strcmp (flag->name,"family" )) { +if (num <16 ) { +memcpy (resstr + (32 -4 ) - flag->bit, flags +4 ,4 ); +memcpy (resstr + (32 -8 ) -20 ,"00000000" ,8 ); + }else { + num -=15 ; +memcpy (resstr + (32 -4 ) - flag->bit,"1111" ,4 ); +for (i =0 ; i <7 ; i++) { + flags[7 - i] ="01" [num &1 ]; + num >>=1 ; + } +memcpy (resstr + (32 -8 ) -20 , flags,8 ); + } + }else if (!strcmp (flag->name,"model" )) { +memcpy (resstr + (32 -4 ) -16 , flags,4 ); +memcpy (resstr + (32 -4 ) - flag->bit, flags +4 ,4 ); + }else { +memcpy (resstr + (32 - flag->length) - flag->bit, flags, + flag->length); + } + entry->policy[flag->reg -1 ] = resstr; + +return 0 ; + +} +/* parse a single key=value pair and translate it into the libxc * used interface using 32-characters strings for each register. * Will overwrite earlier entries and thus can be called multiple
@@ -341,12 +401,8 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str) {NULL, 0, NA, CPUID_REG_INV, 0, 0} };#undef NA -char *sep, *val, *endptr; -int i; +const char *sep, *val;const struct cpuid_flags *flag; -struct xc_xend_cpuid *entry; -unsigned long num; -char flags[33 ], *resstr; sep =strchr (str,'=' );if (sep ==NULL ) {
@@ -356,60 +412,10 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str) }for (flag = cpuid_flags; flag->name !=NULL ; flag++) {if (!strncmp (str, flag->name, sep - str) && flag->name[sep - str] ==0 ) -break ; - } -if (flag->name ==NULL ) { -return 2 ; - } - entry =cpuid_find_match (policy, flag->leaf, flag->subleaf); - resstr = entry->policy[flag->reg -1 ]; - num =strtoull (val, &endptr,0 ); - flags[flag->length] =0 ; -if (endptr != val) { -/* if this was a valid number, write the binary form into the string */ -for (i =0 ; i < flag->length; i++) { - flags[flag->length -1 - i] ="01" [!!(num & (1 << i))]; - } - }else { -switch (val[0 ]) { -case 'x' :case 'k' :case 's' : -memset (flags, val[0 ], flag->length); -break ; -default : -return 3 ; - } - } - -if (resstr ==NULL ) { - resstr =strdup ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ); +return cpuid_add (policy, flag, val); } -/* the family and model entry is potentially split up across -* two fields in Fn0000_0001_EAX, so handle them here separately. -*/ -if (!strncmp (str,"family" , sep - str)) { -if (num <16 ) { -memcpy (resstr + (32 -4 ) - flag->bit, flags +4 ,4 ); -memcpy (resstr + (32 -8 ) -20 ,"00000000" ,8 ); - }else { - num -=15 ; -memcpy (resstr + (32 -4 ) - flag->bit,"1111" ,4 ); -for (i =0 ; i <7 ; i++) { - flags[7 - i] ="01" [num &1 ]; - num >>=1 ; - } -memcpy (resstr + (32 -8 ) -20 , flags,8 ); - } - }else if (!strncmp (str,"model" , sep - str)) { -memcpy (resstr + (32 -4 ) -16 , flags,4 ); -memcpy (resstr + (32 -4 ) - flag->bit, flags +4 ,4 ); - }else { -memcpy (resstr + (32 - flag->length) - flag->bit, flags, - flag->length); - } - entry->policy[flag->reg -1 ] = resstr; - -return 0 ; +return 2 ; }/* parse a single list item from the legacy Python xend syntax, where
Signature · 1 line
2.41.0
Re: [PATCH v2 4/6] libxl: split logic to parse user provided CPUID features
Quoted 7 lines · Roger Pau Monne
Move the CPUID value parsers out of
libxl_cpuid_parse_config()into a newly createdcpuid_add()local helper. This is in preparation for also adding MSR feature parsing support.No functional change intended.
Signed-off-by Roger Pau Monné <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD
PATCH v2 5/6 libxl: use the cpuid feature names from cpufeatureset.h
In reply to Roger Pau MonneThe current implementation in libxl_cpuid_parse_config() requires keeping a list of cpuid feature bits that should be mostly in sync with the contents of cpufeatureset.h.
Avoid such duplication by using the automatically generated list of cpuid features in INIT_FEATURE_NAMES in order to map feature names to featureset bits, and then translate from featureset bits into cpuid leaf, subleaf, register tuple.
Note that the full contents of the previous cpuid translation table can't be removed. That's because some feature names allowed by libxl are not described in the featuresets, or because naming has diverged and the previous nomenclature is preserved for compatibility reasons.
Should result in no functional change observed by callers, albeit some new cpuid features will be available as a result of the change.
While there constify cpuid_flags name field.
---
Changes since v1:
- const unnamed structure cast.
- Declare struct feature_name outside the function.
- Use strcmp.
- Fix indentation.
- Add back missing feature name options.
- Return ERROR_NOMEM if allocation fails.
- Improve xl.cfg documentation about how to reference the features described in the public header.
--- docs/man/xl.cfg.5.pod.in | 24 +-- tools/libs/light/libxl_cpuid.c | 267 ++++++++++++--------------------- tools/xl/xl_parse.c | 3 + 3 files changed, 107 insertions(+), 187 deletions(-)
docs/man/xl.cfg.5.pod.in +8 -16
@@ -2010,24 +2010,16 @@ proccount procpkg stepping =back -List of keys taking a character: +List of keys taking a character can be found in the public header file +L<arch-x86/cpufeatureset.h|https://xenbits.xen.org/docs/unstable/hypercall/x86_64/include,public,arch-x86,cpufeatureset.h.html> -=over 4 - -3dnow 3dnowext 3dnowprefetch abm acpi adx aes altmovcr8 apic arat avx avx2 -avx512-4fmaps avx512-4vnniw avx512bw avx512cd avx512dq avx512er avx512f -avx512ifma avx512pf avx512vbmi avx512vl bmi1 bmi2 clflushopt clfsh clwb cmov -cmplegacy cmpxchg16 cmpxchg8 cmt cntxid dca de ds dscpl dtes64 erms est extapic -f16c ffxsr fma fma4 fpu fsgsbase fxsr hle htt hypervisor ia64 ibs invpcid -invtsc lahfsahf lm lwp mca mce misalignsse mmx mmxext monitor movbe mpx msr -mtrr nodeid nx ospke osvw osxsave pae page1gb pat pbe pcid pclmulqdq pdcm -perfctr_core perfctr_nb pge pku popcnt pse pse36 psn rdrand rdseed rdtscp rtm -sha skinit smap smep smx ss sse sse2 sse3 sse4.1 sse4.2 sse4_1 sse4_2 sse4a -ssse3 svm svm_decode svm_lbrv svm_npt svm_nrips svm_pausefilt svm_tscrate -svm_vmcbclean syscall sysenter tbm tm tm2 topoext tsc tsc-deadline tsc_adjust -umip vme vmx wdt x2apic xop xsave xtpr +The feature names described in C<cpufeatureset.h> should be specified in all +lowercase letters, and with underscores converted to hyphens. For example in +order to reference feature C<LAHF_LM> the string C<lahf-lm> should be used. -=back +Note that C<clflush> is described as an option that takes a value, and that +takes precedence over the C<clflush> flag in C<cpufeatureset.h>. The feature +flag must be referenced as C<clfsh>. =back
tools/libs/light/libxl_cpuid.c +96 -171
347 lines not on this page · raw message
tools/xl/xl_parse.c +3 -0
@@ -2620,6 +2620,9 @@ skip_usbdev:case 3 : errstr ="illegal CPUID value (must be: [0|1|x|k|s])" ;break ; +case ERROR_NOMEM : + errstr ="out of memory" ; +break ;default : errstr ="unknown error" ;break ;
Signature · 1 line
2.41.0
Re: [PATCH v2 5/6] libxl: use the cpuid feature names from cpufeatureset.h
Quoted 14 lines · Roger Pau Monne
The current implementation in
libxl_cpuid_parse_config()requires keeping a list of cpuid feature bits that should be mostly in sync with the contents ofcpufeatureset.h.Avoid such duplication by using the automatically generated list of cpuid features in
INIT_FEATURE_NAMESin order to map feature names to featureset bits, and then translate from featureset bits into cpuid leaf, subleaf, register tuple.Note that the full contents of the previous cpuid translation table can't be removed. That's because some feature names allowed by libxl are not described in the featuresets, or because naming has diverged and the previous nomenclature is preserved for compatibility reasons.
Quoted 6 lines
Should result in no functional change observed by callers, albeit some new cpuid features will be available as a result of the change.
While there constify
cpuid_flagsname field.Signed-off-by Roger Pau Monné <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD
PATCH v2 6/6 libxl: add support for parsing MSR features
In reply to Roger Pau MonneIntroduce support for handling MSR features in libxl_cpuid_parse_config(). The MSR policies are added to the libxl_cpuid_policy like the CPUID one, which gets passed to xc_cpuid_apply_policy().
This allows existing users of libxl to provide MSR related features as key=value pairs to libxl_cpuid_parse_config() without requiring the usage of a different API.
--- tools/libs/light/libxl_cpuid.c | 61 +++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-)
tools/libs/light/libxl_cpuid.c +60 -1
@@ -158,6 +158,57 @@ static int cpuid_add(libxl_cpuid_policy_list *policy,return 0 ; } +static struct xc_msr *msr_find_match (libxl_cpuid_policy_list *pl,uint32_t index) +{ +unsigned int i =0 ; +libxl_cpuid_policy_list policy = *pl; + +if (policy ==NULL ) + policy = *pl =calloc (1 ,sizeof (*policy)); + +if (policy->msr !=NULL ) +for (i =0 ; policy->msr[i].index !=XC_MSR_INPUT_UNUSED ; i++) +if (policy->msr[i].index == index) +return &policy->msr[i]; + + policy->msr =realloc (policy->msr,sizeof (struct xc_msr ) * (i +2 )); + policy->msr[i].index = index; +memset (policy->msr[i].policy,'x' ,ARRAY_SIZE (policy->msr[0 ].policy) -1 ); + policy->msr[i].policy[ARRAY_SIZE (policy->msr[0 ].policy) -1 ] ='\0' ; + policy->msr[i +1 ].index =XC_MSR_INPUT_UNUSED ; + +return &policy->msr[i]; +} + +static int msr_add (libxl_cpuid_policy_list *policy,uint32_t index,unsigned int bit, +const char *val) +{ +struct xc_msr *entry =msr_find_match (policy, index); + +/* Only allow options taking a character for MSRs, no values allowed. */ +if (strlen (val) !=1 ) +return 3 ; + +switch (val[0 ]) { +case '0' : +case '1' : +case 'x' : +case 'k' : + entry->policy[63 - bit] = val[0 ]; +break ; + +case 's' : +/* Translate s -> k as xc_msr doesn't support the deprecated 's'. */ + entry->policy[63 - bit] ='k' ; +break ; + +default : +return 3 ; + } + +return 0 ; +} +struct feature_name {const char *name;unsigned int bit;
@@ -337,7 +388,15 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str) }case FEAT_MSR : -return 2 ; + { +unsigned int bit = feat->bit %32 ; + +if (feature_to_policy[feat->bit /32 ].msr.reg ==CPUID_REG_EDX ) + bit +=32 ; + +return msr_add (policy, feature_to_policy[feat->bit /32 ].msr.index, + bit, val); + } }return 2 ;
Signature · 1 line
2.41.0
Re: [PATCH v2 6/6] libxl: add support for parsing MSR features
Quoted 8 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +0
-0@@ -158,6 +158,57 @@ static int cpuid_add(libxl_cpuid_policy_list *policy,return 0 ; }
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 170
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+static struct xc_msr *msr_find_match (libxl_cpuid_policy_list *pl,uint32_t index) +{ +unsigned int i =0 ; +libxl_cpuid_policy_list policy = *pl; + +if (policy ==NULL ) + policy = *pl =calloc (1 ,sizeof (*policy)); + +if (policy->msr !=NULL ) +for (i =0 ; policy->msr[i].index !=XC_MSR_INPUT_UNUSED ; i++)
Could you add { } for this two blocks? One line after a if() without { } is ok, but not more.
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 176
Quoted 6 lines
tools/libs/light/libxl_cpuid.c +6
-0+if (policy->msr[i].index == index) +return &policy->msr[i]; + + policy->msr =realloc (policy->msr,sizeof (struct xc_msr ) * (i +2 )); + policy->msr[i].index = index; +memset (policy->msr[i].policy,'x' ,ARRAY_SIZE (policy->msr[0 ].policy) -1 );
Is this "array_size() - 1" correct? The -1 need to go, right?
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 177
Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+ policy->msr[i].policy[ARRAY_SIZE (policy->msr[0 ].policy) -1 ] ='\0' ;
Is it for convenience? Maybe for easier debugging (printf)? Also, I guess having a NUL at the end mean the -1 on the previous statement kind of useful.
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 181
Quoted 4 lines
tools/libs/light/libxl_cpuid.c +4
-0+ policy->msr[i +1 ].index =XC_MSR_INPUT_UNUSED ; + +return &policy->msr[i]; +}
Thanks,
Signature · 1 line
Anthony PERARD
Re: [PATCH v2 6/6] libxl: add support for parsing MSR features
Quoted 9 lines · Anthony PERARD
Quoted 8 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +0
-0@@ -158,6 +158,57 @@ static int cpuid_add(libxl_cpuid_policy_list *policy,return 0 ; }
Quoted 13 lines
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 170
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+static struct xc_msr *msr_find_match (libxl_cpuid_policy_list *pl,uint32_t index) +{ +unsigned int i =0 ; +libxl_cpuid_policy_list policy = *pl; + +if (policy ==NULL ) + policy = *pl =calloc (1 ,sizeof (*policy)); + +if (policy->msr !=NULL ) +for (i =0 ; policy->msr[i].index !=XC_MSR_INPUT_UNUSED ; i++)Could you add { } for this two blocks? One line after a
if()without { } is ok, but not more.
Sure.
Quoted 7 lines
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 176
Quoted 6 lines
tools/libs/light/libxl_cpuid.c +6
-0+if (policy->msr[i].index == index) +return &policy->msr[i]; + + policy->msr =realloc (policy->msr,sizeof (struct xc_msr ) * (i +2 )); + policy->msr[i].index = index; +memset (policy->msr[i].policy,'x' ,ARRAY_SIZE (policy->msr[0 ].policy) -1 );
Quoted 7 lines
Is this "
array_size()- 1" correct? The -1 need to go, right?tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 177
Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+ policy->msr[i].policy[ARRAY_SIZE (policy->msr[0 ].policy) -1 ] ='\0' ;Is it for convenience? Maybe for easier debugging (printf)? Also, I guess having a NUL at the end mean the -1 on the previous statement kind of useful.
Yes, it's also to match the format of the policy string used by xc_xend_cpuid, which also has a terminating zero.
Are you OK with this?
Thanks, Roger.
Anthony PERARD
Acked-byRe: [PATCH v2 6/6] libxl: add support for parsing MSR features
Quoted 37 lines · Roger Pau Monné
Quoted 9 lines · Anthony PERARD
Quoted 8 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +0
-0@@ -158,6 +158,57 @@ static int cpuid_add(libxl_cpuid_policy_list *policy,return 0 ; }Quoted 13 lines
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 170
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+static struct xc_msr *msr_find_match (libxl_cpuid_policy_list *pl,uint32_t index) +{ +unsigned int i =0 ; +libxl_cpuid_policy_list policy = *pl; + +if (policy ==NULL ) + policy = *pl =calloc (1 ,sizeof (*policy)); + +if (policy->msr !=NULL ) +for (i =0 ; policy->msr[i].index !=XC_MSR_INPUT_UNUSED ; i++)Could you add { } for this two blocks? One line after a
if()without { } is ok, but not more.Sure.
Quoted 11 lines
tools/libs/light/libxl_cpuid.c static int cpuid_add(libxl_cpuid_policy_list *policy, · line 176
Quoted 6 lines
tools/libs/light/libxl_cpuid.c +6
-0+if (policy->msr[i].index == index) +return &policy->msr[i]; + + policy->msr =realloc (policy->msr,sizeof (struct xc_msr ) * (i +2 )); + policy->msr[i].index = index; +memset (policy->msr[i].policy,'x' ,ARRAY_SIZE (policy->msr[0 ].policy) -1 );Is this "
array_size()- 1" correct? The -1 need to go, right?Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+ policy->msr[i].policy[ARRAY_SIZE (policy->msr[0 ].policy) -1 ] ='\0' ;
Quoted 8 lines
Quoted 3 lines
Is it for convenience? Maybe for easier debugging (printf)? Also, I guess having a NUL at the end mean the -1 on the previous statement kind of useful.
Yes, it's also to match the format of the policy string used by
xc_xend_cpuid, which also has a terminating zero.Are you OK with this?
Yes.
With the other style change done:
Thanks,
Signature · 1 line
Anthony PERARD