if ( (rc = init_vcpu_msr_policy(v)) )
goto fail;
+
+ if ( cache_flush_permitted(d) &&
+ !cond_zalloc_cpumask_var(&v->arch.dirty_cache) )
+ {
+ rc = -ENOMEM;
+ goto fail;
+ }
}
else if ( (rc = xstate_alloc_save_area(v)) != 0 )
return rc;
vcpu_destroy_fpu(v);
xfree(v->arch.msrs);
v->arch.msrs = NULL;
+ FREE_CPUMASK_VAR(v->arch.dirty_cache);
return rc;
}
xfree(v->arch.msrs);
v->arch.msrs = NULL;
+ FREE_CPUMASK_VAR(v->arch.dirty_cache);
+
if ( is_hvm_vcpu(v) )
hvm_vcpu_destroy(v);
else
cpumask_set_cpu(cpu, nd->dirty_cpumask);
write_atomic(&n->dirty_cpu, cpu);
+ if ( cache_flush_permitted(nd) )
+ __cpumask_set_cpu(cpu, n->arch.dirty_cache);
+
if ( !is_idle_domain(nd) )
{
memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES);
return bits;
}
+void vcpu_flush_cache(struct vcpu *curr)
+{
+ ASSERT(curr == current);
+ ASSERT(cache_flush_permitted(curr->domain));
+
+ flush_mask(curr->arch.dirty_cache, FLUSH_CACHE);
+ cpumask_clear(curr->arch.dirty_cache);
+ __cpumask_set_cpu(smp_processor_id(), curr->arch.dirty_cache);
+}
+
+void domain_flush_cache(const struct domain *d)
+{
+ const struct vcpu *v;
+ cpumask_t *mask = this_cpu(scratch_cpumask);
+
+ ASSERT(cache_flush_permitted(d));
+
+ cpumask_clear(mask);
+ for_each_vcpu( d, v )
+ cpumask_or(mask, mask, v->arch.dirty_cache);
+
+ flush_mask(mask, FLUSH_CACHE);
+ /*
+ * Clearing the mask of vCPUs in the domain would be racy unless all vCPUs
+ * are paused, so just leave them as-is, at the cost of possibly doing
+ * redundant flushes in later calls. It's still better than doing a
+ * host-wide cache flush.
+ */
+}
+
/*
* Local variables:
* mode: C
domain_pause_nosync(v->domain);
/* Flush physical caches. */
- flush_all(FLUSH_CACHE);
+ domain_flush_cache(v->domain);
hvm_set_uc_mode(v, 1);
domain_unpause(v->domain);
if ( cache_flush_permitted(d) &&
d->vcpu && d->vcpu[0] && p2m_memory_type_changed(d) )
{
- flush_all(FLUSH_CACHE);
+ domain_flush_cache(d);
}
}
static void cf_check svm_wbinvd_intercept(void)
{
- if ( cache_flush_permitted(current->domain) )
- flush_all(FLUSH_CACHE);
+ struct vcpu *curr = current;
+
+ if ( cache_flush_permitted(curr->domain) )
+ vcpu_flush_cache(curr);
}
static void svm_vmexit_do_invalidate_cache(struct cpu_user_regs *regs,
static void cf_check vmx_wbinvd_intercept(void)
{
- if ( !cache_flush_permitted(current->domain) )
+ struct vcpu *curr = current;
+
+ if ( !cache_flush_permitted(curr->domain) )
return;
if ( cpu_has_wbinvd_exiting )
- flush_all(FLUSH_CACHE);
+ vcpu_flush_cache(curr);
else
wbinvd();
}
struct vcpu_msrs *msrs;
+ /*
+ * When vCPU is allowed cache control track the pCPUs the vCPU has run on
+ * since the last flush.
+ */
+ cpumask_var_t dirty_cache;
+
struct {
bool next_interrupt_enabled;
} monitor;
#define arch_init_idle_domain arch_init_idle_domain
void arch_init_idle_domain(struct domain *d);
+void vcpu_flush_cache(struct vcpu *curr);
+void domain_flush_cache(const struct domain *d);
+
#endif /* __ASM_DOMAIN_H__ */
/*
break;
case MMUEXT_FLUSH_CACHE:
- /*
- * Dirty pCPU caches where the current vCPU has been scheduled are
- * not tracked, and hence we need to resort to a global cache
- * flush for correctness.
- */
+ if ( unlikely(currd != pg_owner) )
+ rc = -EPERM;
+ else if ( likely(cache_flush_permitted(currd)) )
+ vcpu_flush_cache(curr);
+ else
+ rc = -EINVAL;
+ break;
+
case MMUEXT_FLUSH_CACHE_GLOBAL:
if ( unlikely(currd != pg_owner) )
rc = -EPERM;
else if ( likely(cache_flush_permitted(currd)) )
- {
- unsigned int cpu;
- cpumask_t *mask = this_cpu(scratch_cpumask);
-
- cpumask_clear(mask);
- for_each_online_cpu(cpu)
- if ( !cpumask_intersects(mask,
- per_cpu(cpu_sibling_mask, cpu)) )
- __cpumask_set_cpu(cpu, mask);
- flush_mask(mask, FLUSH_CACHE);
- }
+ domain_flush_cache(currd);
else
rc = -EINVAL;
break;
* newer linux uses this in some start-of-day timing loops.
*/
if ( cache_flush_permitted(current->domain) )
- /*
- * Handle wbnoinvd as wbinvd, at the expense of higher cost. Broadcast
- * the flush to all pCPUs, Xen doesn't track where the vCPU has ran
- * previously.
- */
- flush_all(FLUSH_CACHE);
+ /* Handle wbnoinvd as wbinvd, at the expense of higher cost. */
+ vcpu_flush_cache(current);
return X86EMUL_OKAY;
}