... and use it in place of the opencoded instances.
For consistency, restructure init_domain_cpuid_policy() to be like
init_{domain,vcpu}_msr_policy() by operating on the local pointer where
possible.
No change in behaviour.
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
int init_domain_cpuid_policy(struct domain *d)
{
- d->arch.cpuid = xmalloc(struct cpuid_policy);
+ struct cpuid_policy *p =
+ xmemdup(is_pv_domain(d) ? &pv_max_cpuid_policy
+ : &hvm_max_cpuid_policy);
- if ( !d->arch.cpuid )
+ if ( !p )
return -ENOMEM;
- *d->arch.cpuid = is_pv_domain(d)
- ? pv_max_cpuid_policy : hvm_max_cpuid_policy;
-
if ( d->disable_migrate )
- d->arch.cpuid->extd.itsc = cpu_has_itsc;
+ p->extd.itsc = cpu_has_itsc;
+
+ d->arch.cpuid = p;
recalculate_cpuid_policy(d);
int init_domain_msr_policy(struct domain *d)
{
- struct msr_domain_policy *dp;
-
- dp = xmalloc(struct msr_domain_policy);
+ struct msr_domain_policy *dp =
+ xmemdup(is_pv_domain(d) ? &pv_max_msr_domain_policy
+ : &hvm_max_msr_domain_policy);
if ( !dp )
return -ENOMEM;
- *dp = is_pv_domain(d) ? pv_max_msr_domain_policy :
- hvm_max_msr_domain_policy;
-
/* See comment in intel_ctxt_switch_levelling() */
if ( is_control_domain(d) )
dp->plaform_info.cpuid_faulting = false;
int init_vcpu_msr_policy(struct vcpu *v)
{
struct domain *d = v->domain;
- struct msr_vcpu_policy *vp;
-
- vp = xmalloc(struct msr_vcpu_policy);
+ struct msr_vcpu_policy *vp =
+ xmemdup(is_pv_domain(d) ? &pv_max_msr_vcpu_policy
+ : &hvm_max_msr_vcpu_policy);
if ( !vp )
return -ENOMEM;
- *vp = is_pv_domain(d) ? pv_max_msr_vcpu_policy :
- hvm_max_msr_vcpu_policy;
-
v->arch.msr = vp;
return 0;
#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.
+ *
+ * Note: Due to const propagating in the typeof(), ptr needs to be mutable.
+ * This can be fixed by changing n_ to being void *, but then we lose type
+ * safety on the return value.
+ */
+#define xmemdup(ptr) \
+({ \
+ typeof(*(ptr)) *p_ = (ptr), *n_ = xmalloc(typeof(*p_)); \
+ \
+ if ( n_ ) \
+ memcpy(n_, p_, sizeof(*n_)); \
+ n_; \
+})
+
/* Allocate space for array of typed objects. */
#define xmalloc_array(_type, _num) \
((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))