arm/altp2m: Add support for altp2m_vcpu_{initialise,destroy}
RFC PATCH v6 29/43 arm/altp2m: Add support for altp2m_vcpu_{initialise,destroy}
From: Rose Spangler <[email protected]>
This commit adds support for the altp2m_vcpu_initialise and altp2m_vcpu_destroy functions on ARM. The x86-specific function calls, altp2m_vcpu_disable_ve and altp2m_vcpu_update_p2m, are gated with #ifdef CONFIG_X86.
This is commit 2/2 of the altp2m_vcpu_{initialise,destroy} phase.
---
v6: Introduced this patch.
The equivalent to this patch in the v4/v5 was originally part of the patch named "arm/altp2m: Add HVMOP_altp2m_set_domain_state". In that patch, the check that the VCPU passed to altp2m_vcpu_initialise isn't the current CPU was removed. This was because ARM only implements the external altp2m mode, meaning the current VCPU could never be a VCPU of the guest for which altp2m is being initialized for. Therefore, skipping this check was a slight optimization. I've removed that here because altp2m_vcpu_initialise is only called during an HVMOP, and skipping this check on ARM (especially when x86 also implements the external altp2m mode) causes more confusion than the slight optimization is worth in my view.
--- xen/common/altp2m.c | 14 ++++++++++---- xen/include/xen/altp2m.h | 2 -- 2 files changed, 10 insertions(+), 6 deletions(-)
xen/common/altp2m.c +10 -4
@@ -11,6 +11,7 @@#include <xsm/xsm.h> #if defined(CONFIG_X86 ) && defined(CONFIG_HVM ) +#include <asm/hvm/hvm.h> #include <asm/hvm/nestedhvm.h> #endif
@@ -59,7 +60,6 @@ void altp2m_teardown(struct domain *d)XVFREE (d->altp2m_p2m); } -#ifdef CONFIG_X86 void altp2m_vcpu_initialise (struct vcpu *v) {if ( !v->domain->nr_altp2m )
@@ -68,10 +68,12 @@ void altp2m_vcpu_initialise(struct vcpu *v)if ( v != current )vcpu_pause (v); -vcpu_altp2m (v).p2midx =0 ; +altp2m_set_vcpu_idx (v,0 );atomic_inc (&altp2m_get_altp2m (v)->active_vcpus); +#ifdef CONFIG_X86 altp2m_vcpu_update_p2m (v); +#endif if ( v != current )vcpu_unpause (v);
@@ -90,15 +92,19 @@ void altp2m_vcpu_destroy(struct vcpu *v)if ( (p2m =altp2m_get_altp2m (v)) )atomic_dec (&p2m->active_vcpus); +#ifdef CONFIG_X86 altp2m_vcpu_disable_ve (v); +#endif -vcpu_altp2m (v).p2midx =INVALID_ALTP2M ; +altp2m_set_vcpu_idx (v,INVALID_ALTP2M ); + +#ifdef CONFIG_X86 altp2m_vcpu_update_p2m (v); +#endif if ( v != current )vcpu_unpause (v); } -#endif /* * altp2m operations are envisioned as being used in several different
xen/include/xen/altp2m.h +0 -2
@@ -24,11 +24,9 @@ int altp2m_init(struct domain *d);/* Free altp2m views */ void altp2m_teardown (struct domain *d); -#ifdef CONFIG_X86 /* Alternate p2m VCPU */ void altp2m_vcpu_initialise (struct vcpu *v);void altp2m_vcpu_destroy (struct vcpu *v); -#endif #ifdef CONFIG_ALTP2M
Signature · 1 line
2.34.1
RFC PATCH v6 30/43 arm/altp2m: Add altp2m view validity/visibility indicator
From: Rose Spangler <[email protected]>
This commit adds the altp2m_view_state enum, which indicates whether an altp2m view is invalid, visible, or invisible. The altp2m_state array is comprised of altp2m_view_state values, where each entry corresponds to the altp2m view with the same entry. This is analogous to the altp2m_eptp and altp2m_visible_eptp arrays on x86.
This addition is necessary to tell whether an altp2m is valid, in addition to whether it is visible. Since all altp2m views are allocated up-front during p2m initialization, there must be an additional mechanism to determine whether an altp2m view is valid. Since the implementation of HVMOP_altp2m_set_visibility also needs an additional mechanism to determine whether a view is visible, it makes sense to combine altp2m view validity and visibility into a single mechanism. Therefore, the altp2m_state array is used to track both view validitity and visibility.
This is commit 1/5 of the altp2m view validity/visibility phase.
---
v6: Introduced this patch.
In the v4/v5 patch series, view validity was determined by simply checking if the altp2m p2m_domain pointer is NULL. This isn't possible in the v6 patch series, since altp2m views are allocated up-front during altp2m_init.
Since the altp2m_view_state enum definition has to be available in asm/domain.h, this commit removes the sched.h dependency from altp2m.h. Unfortunately, this means that it's not possible to have static inline functions in altp2m.h which need a complete definition of arch_domain and arch_vcpu. If there would be a better place to put the altp2m_view_state definition, feedback would be appreciated.
--- xen/arch/arm/altp2m.c | 13 +++++++++++++ xen/arch/arm/include/asm/altp2m.h | 23 +++++++++++++---------- xen/arch/arm/include/asm/domain.h | 4 ++++ xen/arch/arm/mmu/p2m.c | 14 +++++++++++++- 4 files changed, 43 insertions(+), 11 deletions(-)
xen/arch/arm/altp2m.c +13 -0
@@ -5,6 +5,8 @@* Copyright (c) 2016 Sergej Proskurin <[email protected]> */ +#include <xen/sched.h> +#include <asm/p2m.h> /* Check to see if vcpu should be switched to a different p2m. */
@@ -14,6 +16,17 @@ void altp2m_check(struct vcpu *v, uint16_t idx)BUG (); } +/* Alternate p2m VCPU */ +uint16_t altp2m_vcpu_idx (const struct vcpu *v) +{ +return v->arch.ap2m_idx; +} + +void altp2m_set_vcpu_idx (struct vcpu *v,unsigned int idx) +{ + v->arch.ap2m_idx = idx; +} +/* * Local variables: * mode: C
xen/arch/arm/include/asm/altp2m.h +13 -10
@@ -9,7 +9,10 @@#ifndef __ASM_ARM_ALTP2M_H#define __ASM_ARM_ALTP2M_H -#include <xen/sched.h> +#include <xen/mem_access.h> + +struct domain ; +struct vcpu ;#ifdef CONFIG_ALTP2M
@@ -24,15 +27,15 @@ static inline bool altp2m_supported(void)#define altp2m_unlock (d) spin_unlock(&(d)->arch.altp2m_lock)/* Alternate p2m VCPU */ -static inline uint16_t altp2m_vcpu_idx (const struct vcpu *v) -{ -return v->arch.ap2m_idx; -} - -static inline void altp2m_set_vcpu_idx (struct vcpu *v,unsigned int idx) -{ - v->arch.ap2m_idx = idx; -} +uint16_t altp2m_vcpu_idx (const struct vcpu *v); +void altp2m_set_vcpu_idx (struct vcpu *v,unsigned int idx); + +/* The current state of an altp2m view */ +enum altp2m_view_state { +ALTP2M_INVALID , +ALTP2M_VISIBLE , +ALTP2M_INVISIBLE , +};#else /* CONFIG_ALTP2M */
xen/arch/arm/include/asm/domain.h +4 -0
@@ -3,6 +3,7 @@#include <xen/cache.h> #include <xen/timer.h> +#include <asm/altp2m.h> #include <asm/page.h> #include <asm/p2m.h> #include <asm/suspend.h>
@@ -135,6 +136,9 @@ struct arch_domain* concurrently. */ spinlock_t altp2m_lock; + +/* Validity/visibility of altp2m views */ +enum altp2m_view_state *altp2m_state;#endif } __cacheline_aligned;
xen/arch/arm/mmu/p2m.c +13 -1
@@ -1494,6 +1494,8 @@ int p2m_teardown(struct domain *d) d->altp2m_active = false; +FREE_XENHEAP_PAGE (d->arch.altp2m_state); +for ( i =0 ; i < d->nr_altp2m; i++ ) { rc =p2m_teardown_one (d->altp2m_p2m[i]);
@@ -1617,7 +1619,7 @@ struct p2m_domain *p2m_init_one(struct domain *d)static int p2m_init_altp2m (struct domain *d) {#ifdef CONFIG_ALTP2M -int rc; +int rc, i; rc =altp2m_init (d);if ( rc )
@@ -1626,6 +1628,16 @@ static int p2m_init_altp2m(struct domain *d) return rc; } +if ( (d->arch.altp2m_state =alloc_xenheap_page ()) ==NULL ) + { +return -ENOMEM ; + } + +for ( i =0 ; i < d->nr_altp2m; i++ ) + { + d->arch.altp2m_state[i] =ALTP2M_INVALID ; + } + d->altp2m_active = false;#endif
Signature · 1 line
2.34.1
RFC PATCH v6 31/43 altp2m: Add altp2m_view_is_{valid,visible}
In reply to [email protected]From: Rose Spangler <[email protected]>
This commit adds arch independent helper functions to determine whether a view is valid or visible. This makes it possible to determine whether a view is valid and/or visible in common altp2m routines (namely, altp2m_init_by_id).
This is commit 2/5 of the altp2m view validity/visibility phase.
---
v6: Introduced this patch.
These functions could be used to replace the equivalent calls in x86 code if that would be preferred. For now, they are only used in common code.
The altp2m_view_is_valid function also has overlap with the altp2m_is_ept_valid function, but that function has an additional ASSERT and check that idx < d->nr_altp2m, so I wasn't sure if that function should be repurposed for this. If renaming that function instead of introducing a new function on x86 would be preferred, then I can do that.
Additionally, these functions don't have bounds checks since the functions are intended to replace code which don't have bounds checks currently. Bounds checks could be added here if that would be preferred.
--- xen/arch/arm/altp2m.c | 10 ++++++++++ xen/arch/arm/include/asm/altp2m.h | 3 +++ xen/arch/x86/include/asm/altp2m.h | 10 ++++++++++ 3 files changed, 23 insertions(+)
xen/arch/arm/altp2m.c +10 -0
@@ -27,6 +27,16 @@ void altp2m_set_vcpu_idx(struct vcpu *v, unsigned int idx) v->arch.ap2m_idx = idx; } +bool altp2m_view_is_valid (struct domain *d,unsigned int idx) +{ +return d->arch.altp2m_state[array_index_nospec (idx, d->nr_altp2m)] !=ALTP2M_INVALID ; +} + +bool altp2m_view_is_visible (struct domain *d,unsigned int idx) +{ +return d->arch.altp2m_state[array_index_nospec (idx, d->nr_altp2m)] ==ALTP2M_VISIBLE ; +} +/* * Local variables: * mode: C
xen/arch/arm/include/asm/altp2m.h +3 -0
@@ -30,6 +30,9 @@ static inline bool altp2m_supported(void)uint16_t altp2m_vcpu_idx (const struct vcpu *v);void altp2m_set_vcpu_idx (struct vcpu *v,unsigned int idx); +bool altp2m_view_is_valid (struct domain *d,unsigned int idx); +bool altp2m_view_is_visible (struct domain *d,unsigned int idx); +/* The current state of an altp2m view */ enum altp2m_view_state {ALTP2M_INVALID ,
xen/arch/x86/include/asm/altp2m.h +10 -0
@@ -65,6 +65,16 @@ static inline void altp2m_set_vcpu_idx(struct vcpu *v, unsigned int idx)vcpu_altp2m (v).p2midx = idx; } +static inline bool altp2m_view_is_valid (struct domain *d,unsigned int idx) +{ +return d->arch.altp2m_eptp[array_index_nospec (idx,MAX_EPTP )] !=mfn_x (INVALID_MFN ); +} + +static inline bool altp2m_view_is_visible (struct domain *d,unsigned int idx) +{ +return d->arch.altp2m_visible_eptp[array_index_nospec (idx,MAX_EPTP )] !=mfn_x (INVALID_MFN ); +} +/* * Alternate p2m: shadow p2m tables used for alternate memory views */
Signature · 1 line
2.34.1