x86/hvm: HVMOP_get/set_param improvements
cover x86/hvm: HVMOP_get/set_param improvements
The following 3 patches re-structure the code implementing HVMOP_set_param and HVMOP_get_param.
Patch #1 gives each operation its own function
Patch #2 splits out checks for getting/setting non-reflexive params and setting params with change-once semantics, as well as the XSM check into separate functions
Patch #3 adds all remaining ioreq server params to non-reflexive lists
PATCH 1/3 x86/hvm: give HVMOP_set_param and HVMOP_get_param their own functions
The level of switch nesting in those ops is getting unreadable. Giving them their own functions does introduce some code duplication in the the pre-op checks but the overall result is easier to follow.
This patch is code movement. There is no functional change.
--- xen/arch/x86/hvm/hvm.c | 562 ++++++++++++++++++++++++++---------------------- 1 file changed, 300 insertions(+), 262 deletions(-)
xen/arch/x86/hvm/hvm.c +300 -262
@@ -5640,6 +5640,300 @@ static int hvmop_set_evtchn_upcall_vector(return 0 ; } +static int hvmop_set_param ( +XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) +{ +struct xen_hvm_param a; +struct domain *d; +struct vcpu *v; +int rc =0 ; + +if (copy_from_guest (&a, arg,1 ) ) +return -EFAULT ; + +if ( a.index >=HVM_NR_PARAMS ) +return -EINVAL ; + + d =rcu_lock_domain_by_any_id (a.domid); +if ( d ==NULL ) +return -ESRCH ; + + rc = -EINVAL ; +if ( !has_hvm_container_domain (d) ) + goto out; + +if (is_pvh_domain (d) + && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) + goto out; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); +if ( rc ) + goto out; + +switch ( a.index ) + { +case HVM_PARAM_CALLBACK_IRQ : +hvm_set_callback_via (d, a.value); +hvm_latch_shinfo_size (d); +break ; +case HVM_PARAM_TIMER_MODE : +if ( a.value > HVMPTM_one_missed_tick_pending ) + rc = -EINVAL ; +break ; +case HVM_PARAM_VIRIDIAN : +/* This should only ever be set once by the tools and read by the guest. */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + +if ( a.value != d->arch.hvm_domain.params[a.index] ) + { + rc = -EEXIST ; +if ( d->arch.hvm_domain.params[a.index] !=0 ) +break ; + + rc = -EINVAL ; +if ( (a.value & ~HVMPV_feature_mask) || + !(a.value & HVMPV_base_freq) ) +break ; + } + + rc =0 ; +break ; +case HVM_PARAM_IDENT_PT : +/* Not reflexive, as we must domain_pause(). */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + + rc = -EINVAL ; +if ( d->arch.hvm_domain.params[a.index] !=0 ) +break ; + + rc =0 ; +if ( !paging_mode_hap (d) ) +break ; + +/* +* Update GUEST_CR3 in each VMCS to point at identity map. +* All foreign updates to guest state must synchronise on +* the domctl_lock. +*/ + rc = -ERESTART ; +if ( !domctl_lock_acquire () ) +break ; + + rc =0 ; +domain_pause (d); + d->arch.hvm_domain.params[a.index] = a.value; + for_each_vcpu ( d, v ) +paging_update_cr3 (v); +domain_unpause (d); + +domctl_lock_release (); +break ; +case HVM_PARAM_DM_DOMAIN : +/* Not reflexive, as we may need to domain_pause(). */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + +if ( a.value ==DOMID_SELF ) + a.value = current->domain->domain_id; + + rc =hvm_set_dm_domain (d, a.value); +break ; +case HVM_PARAM_ACPI_S_STATE : +/* Not reflexive, as we must domain_pause(). */ + rc = -EPERM ; +if ( current->domain == d ) +break ; + + rc =0 ; +if ( a.value ==3 ) +hvm_s3_suspend (d); +else if ( a.value ==0 ) +hvm_s3_resume (d); +else + rc = -EINVAL ; + +break ; +case HVM_PARAM_ACPI_IOPORTS_LOCATION : + rc =pmtimer_change_ioport (d, a.value); +break ; +case HVM_PARAM_MEMORY_EVENT_CR0 : +case HVM_PARAM_MEMORY_EVENT_CR3 : +case HVM_PARAM_MEMORY_EVENT_CR4 : +if ( d == current->domain ) + rc = -EPERM ; +break ; +case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : +case HVM_PARAM_MEMORY_EVENT_MSR : +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } +if ( a.value & HVMPME_onchangeonly ) + rc = -EINVAL ; +break ; +case HVM_PARAM_NESTEDHVM : + rc =xsm_hvm_param_nested (XSM_PRIV , d); +if ( rc ) +break ; +if ( a.value >1 ) + rc = -EINVAL ; +/* Remove the check below once we have +* shadow-on-shadow. +*/ +if ( cpu_has_svm && !paging_mode_hap (d) && a.value ) + rc = -EINVAL ; +/* Set up NHVM state for any vcpus that are already up */ +if ( a.value && + !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM ] ) +for_each_vcpu (d, v) +if ( rc ==0 ) + rc =nestedhvm_vcpu_initialise (v); +if ( !a.value || rc ) +for_each_vcpu (d, v) +nestedhvm_vcpu_destroy (v); +break ; +case HVM_PARAM_BUFIOREQ_EVTCHN : + rc = -EINVAL ; +break ; +case HVM_PARAM_TRIPLE_FAULT_REASON : +if ( a.value >SHUTDOWN_MAX ) + rc = -EINVAL ; +break ; +case HVM_PARAM_IOREQ_SERVER_PFN : +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } + d->arch.hvm_domain.ioreq_gmfn.base = a.value; +break ; +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : + { +unsigned int i; + +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } +if ( a.value ==0 || + a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) + { + rc = -EINVAL ; +break ; + } +for ( i =0 ; i < a.value; i++ ) +set_bit (i, &d->arch.hvm_domain.ioreq_gmfn.mask); + +break ; + } + } + +if ( rc !=0 ) + goto out; + + d->arch.hvm_domain.params[a.index] = a.value; + +switch ( a.index ) + { +case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : + { +domain_pause (d); +domain_unpause (d);/* Causes guest to latch new status */ +break ; + } +case HVM_PARAM_MEMORY_EVENT_CR3 : + { +for_each_vcpu ( d, v ) + hvm_funcs.update_guest_cr(v, 0); /* Latches new CR3 mask through CR0 code */ +break ; + } + } + +HVM_DBG_LOG (DBG_LEVEL_HCALL ,"set param %u = %" PRIx64, + a.index, a.value); + + out: +rcu_unlock_domain (d); +return rc; +} + +static int hvmop_get_param ( +XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) +{ +struct xen_hvm_param a; +struct domain *d; +int rc =0 ; + +if (copy_from_guest (&a, arg,1 ) ) +return -EFAULT ; + +if ( a.index >=HVM_NR_PARAMS ) +return -EINVAL ; + + d =rcu_lock_domain_by_any_id (a.domid); +if ( d ==NULL ) +return -ESRCH ; + + rc = -EINVAL ; +if ( !has_hvm_container_domain (d) ) + goto out; + +if (is_pvh_domain (d) + && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) + goto out; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); +if ( rc ) + goto out; + +switch ( a.index ) + { +case HVM_PARAM_ACPI_S_STATE : + a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ; +break ; +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + { + rc = -EPERM ; + goto out; + } +case HVM_PARAM_IOREQ_PFN : +case HVM_PARAM_BUFIOREQ_PFN : +case HVM_PARAM_BUFIOREQ_EVTCHN : { +domid_t domid; + +/* May need to create server */ + domid = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN ]; + rc =hvm_create_ioreq_server (d, domid,1 ,1 ,NULL ); +if ( rc !=0 && rc != -EEXIST ) + goto out; +/*FALLTHRU*/ + } +default : + a.value = d->arch.hvm_domain.params[a.index]; +break ; + } + + rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ; + +HVM_DBG_LOG (DBG_LEVEL_HCALL ,"get param %u = %" PRIx64, + a.index, a.value); + + out: +rcu_unlock_domain (d); +return rc; +} +/* * Note that this value is effectively part of the ABI, even if we don't need * to make it a formal part of it: A guest suspended for migration in the
@@ -5651,7 +5945,6 @@ static int hvmop_set_evtchn_upcall_vector(long do_hvm_op (unsigned long op,XEN_GUEST_HANDLE_PARAM (void ) arg) { -struct domain *curr_d = current->domain;unsigned long start_iter, mask;long rc =0 ;
@@ -5705,269 +5998,14 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)break ;case HVMOP_set_param: -case HVMOP_get_param: - { -struct xen_hvm_param a; -struct domain *d; -struct vcpu *v; - -if (copy_from_guest (&a, arg,1 ) ) -return -EFAULT ; - -if ( a.index >=HVM_NR_PARAMS ) -return -EINVAL ; - - d =rcu_lock_domain_by_any_id (a.domid); -if ( d ==NULL ) -return -ESRCH ; - - rc = -EINVAL ; -if ( !has_hvm_container_domain (d) ) - goto param_fail; - -if (is_pvh_domain (d) - && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) - goto param_fail; - - rc =xsm_hvm_param (XSM_TARGET , d, op); -if ( rc ) - goto param_fail; - -if ( op == HVMOP_set_param ) - { - rc =0 ; - -switch ( a.index ) - { -case HVM_PARAM_CALLBACK_IRQ : -hvm_set_callback_via (d, a.value); -hvm_latch_shinfo_size (d); -break ; -case HVM_PARAM_TIMER_MODE : -if ( a.value > HVMPTM_one_missed_tick_pending ) - rc = -EINVAL ; -break ; -case HVM_PARAM_VIRIDIAN : -/* This should only ever be set once by the tools and read by the guest. */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - -if ( a.value != d->arch.hvm_domain.params[a.index] ) - { - rc = -EEXIST ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - - rc = -EINVAL ; -if ( (a.value & ~HVMPV_feature_mask) || - !(a.value & HVMPV_base_freq) ) -break ; - } - - rc =0 ; -break ; -case HVM_PARAM_IDENT_PT : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - - rc = -EINVAL ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - - rc =0 ; -if ( !paging_mode_hap (d) ) -break ; - -/* -* Update GUEST_CR3 in each VMCS to point at identity map. -* All foreign updates to guest state must synchronise on -* the domctl_lock. -*/ - rc = -ERESTART ; -if ( !domctl_lock_acquire () ) -break ; - - rc =0 ; -domain_pause (d); - d->arch.hvm_domain.params[a.index] = a.value; - for_each_vcpu ( d, v ) -paging_update_cr3 (v); -domain_unpause (d); - -domctl_lock_release (); -break ; -case HVM_PARAM_DM_DOMAIN : -/* Not reflexive, as we may need to domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - -if ( a.value ==DOMID_SELF ) - a.value = curr_d->domain_id; - - rc =hvm_set_dm_domain (d, a.value); -break ; -case HVM_PARAM_ACPI_S_STATE : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - - rc =0 ; -if ( a.value ==3 ) -hvm_s3_suspend (d); -else if ( a.value ==0 ) -hvm_s3_resume (d); -else - rc = -EINVAL ; - -break ; -case HVM_PARAM_ACPI_IOPORTS_LOCATION : - rc =pmtimer_change_ioport (d, a.value); -break ; -case HVM_PARAM_MEMORY_EVENT_CR0 : -case HVM_PARAM_MEMORY_EVENT_CR3 : -case HVM_PARAM_MEMORY_EVENT_CR4 : -if ( d == current->domain ) - rc = -EPERM ; -break ; -case HVM_PARAM_MEMORY_EVENT_INT3 : -case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : -case HVM_PARAM_MEMORY_EVENT_MSR : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -if ( a.value & HVMPME_onchangeonly ) - rc = -EINVAL ; -break ; -case HVM_PARAM_NESTEDHVM : - rc =xsm_hvm_param_nested (XSM_PRIV , d); -if ( rc ) -break ; -if ( a.value >1 ) - rc = -EINVAL ; -/* Remove the check below once we have -* shadow-on-shadow. -*/ -if ( cpu_has_svm && !paging_mode_hap (d) && a.value ) - rc = -EINVAL ; -/* Set up NHVM state for any vcpus that are already up */ -if ( a.value && - !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM ] ) -for_each_vcpu (d, v) -if ( rc ==0 ) - rc =nestedhvm_vcpu_initialise (v); -if ( !a.value || rc ) -for_each_vcpu (d, v) -nestedhvm_vcpu_destroy (v); -break ; -case HVM_PARAM_BUFIOREQ_EVTCHN : - rc = -EINVAL ; -break ; -case HVM_PARAM_TRIPLE_FAULT_REASON : -if ( a.value >SHUTDOWN_MAX ) - rc = -EINVAL ; -break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } - d->arch.hvm_domain.ioreq_gmfn.base = a.value; -break ; -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : - { -unsigned int i; - -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -if ( a.value ==0 || - a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) - { - rc = -EINVAL ; -break ; - } -for ( i =0 ; i < a.value; i++ ) -set_bit (i, &d->arch.hvm_domain.ioreq_gmfn.mask); - -break ; - } - } - -if ( rc ==0 ) - { - d->arch.hvm_domain.params[a.index] = a.value; - -switch ( a.index ) - { -case HVM_PARAM_MEMORY_EVENT_INT3 : -case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : - { -domain_pause (d); -domain_unpause (d);/* Causes guest to latch new status */ -break ; - } -case HVM_PARAM_MEMORY_EVENT_CR3 : - { -for_each_vcpu ( d, v ) - hvm_funcs.update_guest_cr(v, 0); /* Latches new CR3 mask through CR0 code */ -break ; - } - } - - } - - } -else - { -switch ( a.index ) - { -case HVM_PARAM_ACPI_S_STATE : - a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ; -break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -case HVM_PARAM_IOREQ_PFN : -case HVM_PARAM_BUFIOREQ_PFN : -case HVM_PARAM_BUFIOREQ_EVTCHN : { -domid_t domid; - -/* May need to create server */ - domid = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN ]; - rc =hvm_create_ioreq_server (d, domid,1 ,1 ,NULL ); -if ( rc !=0 && rc != -EEXIST ) - goto param_fail; -/*FALLTHRU*/ - } -default : - a.value = d->arch.hvm_domain.params[a.index]; -break ; - } - rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ; - } - -HVM_DBG_LOG (DBG_LEVEL_HCALL ,"%s param %u = %" PRIx64, - op == HVMOP_set_param ?"set" :"get" , - a.index, a.value); + rc =hvmop_set_param ( +guest_handle_cast (arg, xen_hvm_param_t)); +break ; - param_fail: -rcu_unlock_domain (d); +case HVMOP_get_param: + rc =hvmop_get_param ( +guest_handle_cast (arg, xen_hvm_param_t));break ; - }case HVMOP_set_pci_intx_level: rc = hvmop_set_pci_intx_level(
Signature · 1 line
1.7.10.4
Re: [PATCH 1/3] x86/hvm: give HVMOP_set_param and HVMOP_get_param their own functions
Quoted 10 lines · Paul Durrant
The level of switch nesting in those ops is getting unreadable. Giving them their own functions does introduce some code duplication in the the pre-op checks but the overall result is easier to follow.
This patch is code movement. There is no functional change.
Signed-off-by Paul Durrant <[email protected]>Cc Keir Fraser <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>
Please latch current at the top of the function, and fix Xen style during motion. (newlines between break and case lines, drop superfluous braces, brace layout around get bufiorq_evtchn).
With that fixed, Reviewed-by: Andrew Cooper <[email protected]>
Quoted 593 lines
--- xen/arch/x86/hvm/hvm.c | 562 ++++++++++++++++++++++++++---------------------- 1 file changed, 300 insertions(+), 262 deletions(-)xen/arch/x86/hvm/hvm.c +300
-262@@ -5640,6 +5640,300 @@ static int hvmop_set_evtchn_upcall_vector(return 0 ; } +static int hvmop_set_param ( +XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) +{ +struct xen_hvm_param a; +struct domain *d; +struct vcpu *v; +int rc =0 ; + +if (copy_from_guest (&a, arg,1 ) ) +return -EFAULT ; + +if ( a.index >=HVM_NR_PARAMS ) +return -EINVAL ; + + d =rcu_lock_domain_by_any_id (a.domid); +if ( d ==NULL ) +return -ESRCH ; + + rc = -EINVAL ; +if ( !has_hvm_container_domain (d) ) + goto out; + +if (is_pvh_domain (d) + && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) + goto out; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); +if ( rc ) + goto out; + +switch ( a.index ) + { +case HVM_PARAM_CALLBACK_IRQ : +hvm_set_callback_via (d, a.value); +hvm_latch_shinfo_size (d); +break ; +case HVM_PARAM_TIMER_MODE : +if ( a.value > HVMPTM_one_missed_tick_pending ) + rc = -EINVAL ; +break ; +case HVM_PARAM_VIRIDIAN : +/* This should only ever be set once by the tools and read by the guest. */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + +if ( a.value != d->arch.hvm_domain.params[a.index] ) + { + rc = -EEXIST ; +if ( d->arch.hvm_domain.params[a.index] !=0 ) +break ; + + rc = -EINVAL ; +if ( (a.value & ~HVMPV_feature_mask) || + !(a.value & HVMPV_base_freq) ) +break ; + } + + rc =0 ; +break ; +case HVM_PARAM_IDENT_PT : +/* Not reflexive, as we must domain_pause(). */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + + rc = -EINVAL ; +if ( d->arch.hvm_domain.params[a.index] !=0 ) +break ; + + rc =0 ; +if ( !paging_mode_hap (d) ) +break ; + +/* +* Update GUEST_CR3 in each VMCS to point at identity map. +* All foreign updates to guest state must synchronise on +* the domctl_lock. +*/ + rc = -ERESTART ; +if ( !domctl_lock_acquire () ) +break ; + + rc =0 ; +domain_pause (d); + d->arch.hvm_domain.params[a.index] = a.value; + for_each_vcpu ( d, v ) +paging_update_cr3 (v); +domain_unpause (d); + +domctl_lock_release (); +break ; +case HVM_PARAM_DM_DOMAIN : +/* Not reflexive, as we may need to domain_pause(). */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + +if ( a.value ==DOMID_SELF ) + a.value = current->domain->domain_id; + + rc =hvm_set_dm_domain (d, a.value); +break ; +case HVM_PARAM_ACPI_S_STATE : +/* Not reflexive, as we must domain_pause(). */ + rc = -EPERM ; +if ( current->domain == d ) +break ; + + rc =0 ; +if ( a.value ==3 ) +hvm_s3_suspend (d); +else if ( a.value ==0 ) +hvm_s3_resume (d); +else + rc = -EINVAL ; + +break ; +case HVM_PARAM_ACPI_IOPORTS_LOCATION : + rc =pmtimer_change_ioport (d, a.value); +break ; +case HVM_PARAM_MEMORY_EVENT_CR0 : +case HVM_PARAM_MEMORY_EVENT_CR3 : +case HVM_PARAM_MEMORY_EVENT_CR4 : +if ( d == current->domain ) + rc = -EPERM ; +break ; +case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : +case HVM_PARAM_MEMORY_EVENT_MSR : +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } +if ( a.value & HVMPME_onchangeonly ) + rc = -EINVAL ; +break ; +case HVM_PARAM_NESTEDHVM : + rc =xsm_hvm_param_nested (XSM_PRIV , d); +if ( rc ) +break ; +if ( a.value >1 ) + rc = -EINVAL ; +/* Remove the check below once we have +* shadow-on-shadow. +*/ +if ( cpu_has_svm && !paging_mode_hap (d) && a.value ) + rc = -EINVAL ; +/* Set up NHVM state for any vcpus that are already up */ +if ( a.value && + !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM ] ) +for_each_vcpu (d, v) +if ( rc ==0 ) + rc =nestedhvm_vcpu_initialise (v); +if ( !a.value || rc ) +for_each_vcpu (d, v) +nestedhvm_vcpu_destroy (v); +break ; +case HVM_PARAM_BUFIOREQ_EVTCHN : + rc = -EINVAL ; +break ; +case HVM_PARAM_TRIPLE_FAULT_REASON : +if ( a.value >SHUTDOWN_MAX ) + rc = -EINVAL ; +break ; +case HVM_PARAM_IOREQ_SERVER_PFN : +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } + d->arch.hvm_domain.ioreq_gmfn.base = a.value; +break ; +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : + { +unsigned int i; + +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } +if ( a.value ==0 || + a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) + { + rc = -EINVAL ; +break ; + } +for ( i =0 ; i < a.value; i++ ) +set_bit (i, &d->arch.hvm_domain.ioreq_gmfn.mask); + +break ; + } + } + +if ( rc !=0 ) + goto out; + + d->arch.hvm_domain.params[a.index] = a.value; + +switch ( a.index ) + { +case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : + { +domain_pause (d); +domain_unpause (d);/* Causes guest to latch new status */ +break ; + } +case HVM_PARAM_MEMORY_EVENT_CR3 : + { +for_each_vcpu ( d, v ) + hvm_funcs.update_guest_cr(v, 0); /* Latches new CR3 mask through CR0 code */ +break ; + } + } + +HVM_DBG_LOG (DBG_LEVEL_HCALL ,"set param %u = %" PRIx64, + a.index, a.value); + + out: +rcu_unlock_domain (d); +return rc; +} + +static int hvmop_get_param ( +XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) +{ +struct xen_hvm_param a; +struct domain *d; +int rc =0 ; + +if (copy_from_guest (&a, arg,1 ) ) +return -EFAULT ; + +if ( a.index >=HVM_NR_PARAMS ) +return -EINVAL ; + + d =rcu_lock_domain_by_any_id (a.domid); +if ( d ==NULL ) +return -ESRCH ; + + rc = -EINVAL ; +if ( !has_hvm_container_domain (d) ) + goto out; + +if (is_pvh_domain (d) + && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) + goto out; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); +if ( rc ) + goto out; + +switch ( a.index ) + { +case HVM_PARAM_ACPI_S_STATE : + a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ; +break ; +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + { + rc = -EPERM ; + goto out; + } +case HVM_PARAM_IOREQ_PFN : +case HVM_PARAM_BUFIOREQ_PFN : +case HVM_PARAM_BUFIOREQ_EVTCHN : { +domid_t domid; + +/* May need to create server */ + domid = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN ]; + rc =hvm_create_ioreq_server (d, domid,1 ,1 ,NULL ); +if ( rc !=0 && rc != -EEXIST ) + goto out; +/*FALLTHRU*/ + } +default : + a.value = d->arch.hvm_domain.params[a.index]; +break ; + } + + rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ; + +HVM_DBG_LOG (DBG_LEVEL_HCALL ,"get param %u = %" PRIx64, + a.index, a.value); + + out: +rcu_unlock_domain (d); +return rc; +} +/* * Note that this value is effectively part of the ABI, even if we don't need * to make it a formal part of it: A guest suspended for migration in the @@ -5651,7 +5945,6 @@ static int hvmop_set_evtchn_upcall_vector(long do_hvm_op (unsigned long op,XEN_GUEST_HANDLE_PARAM (void ) arg) { -struct domain *curr_d = current->domain;unsigned long start_iter, mask;long rc =0 ;@@ -5705,269 +5998,14 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)break ;case HVMOP_set_param: -case HVMOP_get_param: - { -struct xen_hvm_param a; -struct domain *d; -struct vcpu *v; - -if (copy_from_guest (&a, arg,1 ) ) -return -EFAULT ; - -if ( a.index >=HVM_NR_PARAMS ) -return -EINVAL ; - - d =rcu_lock_domain_by_any_id (a.domid); -if ( d ==NULL ) -return -ESRCH ; - - rc = -EINVAL ; -if ( !has_hvm_container_domain (d) ) - goto param_fail; - -if (is_pvh_domain (d) - && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) - goto param_fail; - - rc =xsm_hvm_param (XSM_TARGET , d, op); -if ( rc ) - goto param_fail; - -if ( op == HVMOP_set_param ) - { - rc =0 ; - -switch ( a.index ) - { -case HVM_PARAM_CALLBACK_IRQ : -hvm_set_callback_via (d, a.value); -hvm_latch_shinfo_size (d); -break ; -case HVM_PARAM_TIMER_MODE : -if ( a.value > HVMPTM_one_missed_tick_pending ) - rc = -EINVAL ; -break ; -case HVM_PARAM_VIRIDIAN : -/* This should only ever be set once by the tools and read by the guest. */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - -if ( a.value != d->arch.hvm_domain.params[a.index] ) - { - rc = -EEXIST ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - - rc = -EINVAL ; -if ( (a.value & ~HVMPV_feature_mask) || - !(a.value & HVMPV_base_freq) ) -break ; - } - - rc =0 ; -break ; -case HVM_PARAM_IDENT_PT : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - - rc = -EINVAL ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - - rc =0 ; -if ( !paging_mode_hap (d) ) -break ; - -/* -* Update GUEST_CR3 in each VMCS to point at identity map. -* All foreign updates to guest state must synchronise on -* the domctl_lock. -*/ - rc = -ERESTART ; -if ( !domctl_lock_acquire () ) -break ; - - rc =0 ; -domain_pause (d); - d->arch.hvm_domain.params[a.index] = a.value; - for_each_vcpu ( d, v ) -paging_update_cr3 (v); -domain_unpause (d); - -domctl_lock_release (); -break ; -case HVM_PARAM_DM_DOMAIN : -/* Not reflexive, as we may need to domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - -if ( a.value ==DOMID_SELF ) - a.value = curr_d->domain_id; - - rc =hvm_set_dm_domain (d, a.value); -break ; -case HVM_PARAM_ACPI_S_STATE : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - - rc =0 ; -if ( a.value ==3 ) -hvm_s3_suspend (d); -else if ( a.value ==0 ) -hvm_s3_resume (d); -else - rc = -EINVAL ; - -break ; -case HVM_PARAM_ACPI_IOPORTS_LOCATION : - rc =pmtimer_change_ioport (d, a.value); -break ; -case HVM_PARAM_MEMORY_EVENT_CR0 : -case HVM_PARAM_MEMORY_EVENT_CR3 : -case HVM_PARAM_MEMORY_EVENT_CR4 : -if ( d == current->domain ) - rc = -EPERM ; -break ; -case HVM_PARAM_MEMORY_EVENT_INT3 : -case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : -case HVM_PARAM_MEMORY_EVENT_MSR : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -if ( a.value & HVMPME_onchangeonly ) - rc = -EINVAL ; -break ; -case HVM_PARAM_NESTEDHVM : - rc =xsm_hvm_param_nested (XSM_PRIV , d); -if ( rc ) -break ; -if ( a.value >1 ) - rc = -EINVAL ; -/* Remove the check below once we have -* shadow-on-shadow. -*/ -if ( cpu_has_svm && !paging_mode_hap (d) && a.value ) - rc = -EINVAL ; -/* Set up NHVM state for any vcpus that are already up */ -if ( a.value && - !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM ] ) -for_each_vcpu (d, v) -if ( rc ==0 ) - rc =nestedhvm_vcpu_initialise (v); -if ( !a.value || rc ) -for_each_vcpu (d, v) -nestedhvm_vcpu_destroy (v); -break ; -case HVM_PARAM_BUFIOREQ_EVTCHN : - rc = -EINVAL ; -break ; -case HVM_PARAM_TRIPLE_FAULT_REASON : -if ( a.value >SHUTDOWN_MAX ) - rc = -EINVAL ; -break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } - d->arch.hvm_domain.ioreq_gmfn.base = a.value; -break ; -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : - { -unsigned int i; - -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -if ( a.value ==0 || - a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) - { - rc = -EINVAL ; -break ; - } -for ( i =0 ; i < a.value; i++ ) -set_bit (i, &d->arch.hvm_domain.ioreq_gmfn.mask); - -break ; - } - } - -if ( rc ==0 ) - { - d->arch.hvm_domain.params[a.index] = a.value; - -switch ( a.index ) - { -case HVM_PARAM_MEMORY_EVENT_INT3 : -case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : - { -domain_pause (d); -domain_unpause (d);/* Causes guest to latch new status */ -break ; - } -case HVM_PARAM_MEMORY_EVENT_CR3 : - { -for_each_vcpu ( d, v ) - hvm_funcs.update_guest_cr(v, 0); /* Latches new CR3 mask through CR0 code */ -break ; - } - } - - } - - } -else - { -switch ( a.index ) - { -case HVM_PARAM_ACPI_S_STATE : - a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ; -break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -case HVM_PARAM_IOREQ_PFN : -case HVM_PARAM_BUFIOREQ_PFN : -case HVM_PARAM_BUFIOREQ_EVTCHN : { -domid_t domid; - -/* May need to create server */ - domid = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN ]; - rc =hvm_create_ioreq_server (d, domid,1 ,1 ,NULL ); -if ( rc !=0 && rc != -EEXIST ) - goto param_fail; -/*FALLTHRU*/ - } -default : - a.value = d->arch.hvm_domain.params[a.index]; -break ; - } - rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ; - } - -HVM_DBG_LOG (DBG_LEVEL_HCALL ,"%s param %u = %" PRIx64, - op == HVMOP_set_param ?"set" :"get" , - a.index, a.value); + rc =hvmop_set_param ( +guest_handle_cast (arg, xen_hvm_param_t)); +break ; - param_fail: -rcu_unlock_domain (d); +case HVMOP_get_param: + rc =hvmop_get_param ( +guest_handle_cast (arg, xen_hvm_param_t));break ; - }case HVMOP_set_pci_intx_level: rc = hvmop_set_pci_intx_level(
Re: [PATCH 1/3] x86/hvm: give HVMOP_set_param and HVMOP_get_param their own functions
Quoted 15 lines
-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 25 April 2015 01:12
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH 1/3] x86/hvm: giveHVMOP_set_paramandHVMOP_get_paramtheir own functionsQuoted 6 lines · Paul Durrant
The level of switch nesting in those ops is getting unreadable. Giving them their own functions does introduce some code duplication in the the pre-op checks but the overall result is easier to follow.
This patch is code movement. There is no functional change.
Quoted 9 lines
Quoted 4 lines
Signed-off-by Paul Durrant <[email protected]>Cc Keir Fraser <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>Please latch current at the top of the function, and fix Xen style during motion. (newlines between break and case lines, drop superfluous braces, brace layout around get
bufiorq_evtchn).
Sure, will do.
Quoted 3 lines
With that fixed, Reviewed-by: Andrew Cooper
<[email protected]>
Cool.
Paul
Quoted 601 lines
Quoted 2 lines
--- xen/arch/x86/hvm/hvm.c | 562 ++++++++++++++++++++++++++----------------------
Quoted 42 lines
1 file changed, 300 insertions(+), 262 deletions(-)xen/arch/x86/hvm/hvm.c +32
-0@@ -5640,6 +5640,300 @@ static int hvmop_set_evtchn_upcall_vector(return 0 ; } +static int hvmop_set_param ( +XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) +{ +struct xen_hvm_param a; +struct domain *d; +struct vcpu *v; +int rc =0 ; + +if (copy_from_guest (&a, arg,1 ) ) +return -EFAULT ; + +if ( a.index >=HVM_NR_PARAMS ) +return -EINVAL ; + + d =rcu_lock_domain_by_any_id (a.domid); +if ( d ==NULL ) +return -ESRCH ; + + rc = -EINVAL ; +if ( !has_hvm_container_domain (d) ) + goto out; + +if (is_pvh_domain (d) + && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) + goto out; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); +if ( rc ) + goto out; + +switch ( a.index ) + {xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5684
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+case HVM_PARAM_CALLBACK_IRQ : +hvm_set_callback_via (d, a.value); +hvm_latch_shinfo_size (d); +break ; +case HVM_PARAM_TIMER_MODE : +if ( a.value > HVMPTM_one_missed_tick_pending ) + rc = -EINVAL ; +break ; +case HVM_PARAM_VIRIDIAN : +/* This should only ever be set once by the tools and read by the guest. */
Quoted 162 lines
xen/arch/x86/hvm/hvm.c +162
-0+ rc = -EPERM ; +if ( d == current->domain ) +break ; + +if ( a.value != d->arch.hvm_domain.params[a.index] ) + { + rc = -EEXIST ; +if ( d->arch.hvm_domain.params[a.index] !=0 ) +break ; + + rc = -EINVAL ; +if ( (a.value & ~HVMPV_feature_mask) || + !(a.value & HVMPV_base_freq) ) +break ; + } + + rc =0 ; +break ; +case HVM_PARAM_IDENT_PT : +/* Not reflexive, as we must domain_pause(). */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + + rc = -EINVAL ; +if ( d->arch.hvm_domain.params[a.index] !=0 ) +break ; + + rc =0 ; +if ( !paging_mode_hap (d) ) +break ; + +/* +* Update GUEST_CR3 in each VMCS to point at identity map. +* All foreign updates to guest state must synchronise on +* the domctl_lock. +*/ + rc = -ERESTART ; +if ( !domctl_lock_acquire () ) +break ; + + rc =0 ; +domain_pause (d); + d->arch.hvm_domain.params[a.index] = a.value; + for_each_vcpu ( d, v ) +paging_update_cr3 (v); +domain_unpause (d); + +domctl_lock_release (); +break ; +case HVM_PARAM_DM_DOMAIN : +/* Not reflexive, as we may need to domain_pause(). */ + rc = -EPERM ; +if ( d == current->domain ) +break ; + +if ( a.value ==DOMID_SELF ) + a.value = current->domain->domain_id; + + rc =hvm_set_dm_domain (d, a.value); +break ; +case HVM_PARAM_ACPI_S_STATE : +/* Not reflexive, as we must domain_pause(). */ + rc = -EPERM ; +if ( current->domain == d ) +break ; + + rc =0 ; +if ( a.value ==3 ) +hvm_s3_suspend (d); +else if ( a.value ==0 ) +hvm_s3_resume (d); +else + rc = -EINVAL ; + +break ; +case HVM_PARAM_ACPI_IOPORTS_LOCATION : + rc =pmtimer_change_ioport (d, a.value); +break ; +case HVM_PARAM_MEMORY_EVENT_CR0 : +case HVM_PARAM_MEMORY_EVENT_CR3 : +case HVM_PARAM_MEMORY_EVENT_CR4 : +if ( d == current->domain ) + rc = -EPERM ; +break ; +case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : +case HVM_PARAM_MEMORY_EVENT_MSR : +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } +if ( a.value & HVMPME_onchangeonly ) + rc = -EINVAL ; +break ; +case HVM_PARAM_NESTEDHVM : + rc =xsm_hvm_param_nested (XSM_PRIV , d); +if ( rc ) +break ; +if ( a.value >1 ) + rc = -EINVAL ; +/* Remove the check below once we have +* shadow-on-shadow. +*/ +if ( cpu_has_svm && !paging_mode_hap (d) && a.value ) + rc = -EINVAL ; +/* Set up NHVM state for any vcpus that are already up */ +if ( a.value && + !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM ] ) +for_each_vcpu (d, v) +if ( rc ==0 ) + rc =nestedhvm_vcpu_initialise (v); +if ( !a.value || rc ) +for_each_vcpu (d, v) +nestedhvm_vcpu_destroy (v); +break ; +case HVM_PARAM_BUFIOREQ_EVTCHN : + rc = -EINVAL ; +break ; +case HVM_PARAM_TRIPLE_FAULT_REASON : +if ( a.value >SHUTDOWN_MAX ) + rc = -EINVAL ; +break ; +case HVM_PARAM_IOREQ_SERVER_PFN : +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } + d->arch.hvm_domain.ioreq_gmfn.base = a.value; +break ; +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : + { +unsigned int i; + +if ( d == current->domain ) + { + rc = -EPERM ; +break ; + } +if ( a.value ==0 || + a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) + { + rc = -EINVAL ; +break ; + } +for ( i =0 ; i < a.value; i++ ) +set_bit (i, &d->arch.hvm_domain.ioreq_gmfn.mask); + +break ; + } + } + +if ( rc !=0 ) + goto out; + + d->arch.hvm_domain.params[a.index] = a.value; + +switch ( a.index ) + { +case HVM_PARAM_MEMORY_EVENT_INT3 :xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector(
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : + { +domain_pause (d); +domain_unpause (d);/* Causes guest to latch new status */ +break ; + } +case HVM_PARAM_MEMORY_EVENT_CR3 : + { +for_each_vcpu ( d, v ) + hvm_funcs.update_guest_cr(v, 0); /* Latches new CR3 maskthrough CR0 code */
Quoted 84 lines
xen/arch/x86/hvm/hvm.c +80
-0+break ; + } + } + +HVM_DBG_LOG (DBG_LEVEL_HCALL ,"set param %u = %" PRIx64, + a.index, a.value); + + out: +rcu_unlock_domain (d); +return rc; +} + +static int hvmop_get_param ( +XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) +{ +struct xen_hvm_param a; +struct domain *d; +int rc =0 ; + +if (copy_from_guest (&a, arg,1 ) ) +return -EFAULT ; + +if ( a.index >=HVM_NR_PARAMS ) +return -EINVAL ; + + d =rcu_lock_domain_by_any_id (a.domid); +if ( d ==NULL ) +return -ESRCH ; + + rc = -EINVAL ; +if ( !has_hvm_container_domain (d) ) + goto out; + +if (is_pvh_domain (d) + && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) + goto out; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); +if ( rc ) + goto out; + +switch ( a.index ) + { +case HVM_PARAM_ACPI_S_STATE : + a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ; +break ; +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + { + rc = -EPERM ; + goto out; + } +case HVM_PARAM_IOREQ_PFN : +case HVM_PARAM_BUFIOREQ_PFN : +case HVM_PARAM_BUFIOREQ_EVTCHN : { +domid_t domid; + +/* May need to create server */ + domid = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN ]; + rc =hvm_create_ioreq_server (d, domid,1 ,1 ,NULL ); +if ( rc !=0 && rc != -EEXIST ) + goto out; +/*FALLTHRU*/ + } +default : + a.value = d->arch.hvm_domain.params[a.index]; +break ; + } + + rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ; + +HVM_DBG_LOG (DBG_LEVEL_HCALL ,"get param %u = %" PRIx64, + a.index, a.value); + + out: +rcu_unlock_domain (d); +return rc; +} +/* * Note that this value is effectively part of the ABI, even if we don't need * to make it a formal part of it: A guest suspended for migration in the xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5945
Quoted 1 line
xen/arch/x86/hvm/hvm.c +0
-0@@ -5651,7 +5945,6 @@ static int hvmop_set_evtchn_upcall_vector( long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void)arg)
xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op,
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +0
-1{ -struct domain *curr_d = current->domain;unsigned long start_iter, mask;long rc =0 ;@@ -5705,269 +5998,14 @@ long do_hvm_op(unsigned long op,
XEN_GUEST_HANDLE_PARAM(void)arg)Quoted 37 lines
xen/arch/x86/hvm/hvm.c +0
-34break ;case HVMOP_set_param: -case HVMOP_get_param: - { -struct xen_hvm_param a; -struct domain *d; -struct vcpu *v; - -if (copy_from_guest (&a, arg,1 ) ) -return -EFAULT ; - -if ( a.index >=HVM_NR_PARAMS ) -return -EINVAL ; - - d =rcu_lock_domain_by_any_id (a.domid); -if ( d ==NULL ) -return -ESRCH ; - - rc = -EINVAL ; -if ( !has_hvm_container_domain (d) ) - goto param_fail; - -if (is_pvh_domain (d) - && (a.index !=HVM_PARAM_CALLBACK_IRQ ) ) - goto param_fail; - - rc =xsm_hvm_param (XSM_TARGET , d, op); -if ( rc ) - goto param_fail; - -if ( op == HVMOP_set_param ) - { - rc =0 ; - -switch ( a.index ) - {xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +0
-10-case HVM_PARAM_CALLBACK_IRQ : -hvm_set_callback_via (d, a.value); -hvm_latch_shinfo_size (d); -break ; -case HVM_PARAM_TIMER_MODE : -if ( a.value > HVMPTM_one_missed_tick_pending ) - rc = -EINVAL ; -break ; -case HVM_PARAM_VIRIDIAN : -/* This should only ever be set once by the tools and read by the guest. */
Quoted 161 lines
xen/arch/x86/hvm/hvm.c +0
-161- rc = -EPERM ; -if ( curr_d == d ) -break ; - -if ( a.value != d->arch.hvm_domain.params[a.index] ) - { - rc = -EEXIST ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - - rc = -EINVAL ; -if ( (a.value & ~HVMPV_feature_mask) || - !(a.value & HVMPV_base_freq) ) -break ; - } - - rc =0 ; -break ; -case HVM_PARAM_IDENT_PT : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - - rc = -EINVAL ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - - rc =0 ; -if ( !paging_mode_hap (d) ) -break ; - -/* -* Update GUEST_CR3 in each VMCS to point at identity map. -* All foreign updates to guest state must synchronise on -* the domctl_lock. -*/ - rc = -ERESTART ; -if ( !domctl_lock_acquire () ) -break ; - - rc =0 ; -domain_pause (d); - d->arch.hvm_domain.params[a.index] = a.value; - for_each_vcpu ( d, v ) -paging_update_cr3 (v); -domain_unpause (d); - -domctl_lock_release (); -break ; -case HVM_PARAM_DM_DOMAIN : -/* Not reflexive, as we may need to domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - -if ( a.value ==DOMID_SELF ) - a.value = curr_d->domain_id; - - rc =hvm_set_dm_domain (d, a.value); -break ; -case HVM_PARAM_ACPI_S_STATE : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( curr_d == d ) -break ; - - rc =0 ; -if ( a.value ==3 ) -hvm_s3_suspend (d); -else if ( a.value ==0 ) -hvm_s3_resume (d); -else - rc = -EINVAL ; - -break ; -case HVM_PARAM_ACPI_IOPORTS_LOCATION : - rc =pmtimer_change_ioport (d, a.value); -break ; -case HVM_PARAM_MEMORY_EVENT_CR0 : -case HVM_PARAM_MEMORY_EVENT_CR3 : -case HVM_PARAM_MEMORY_EVENT_CR4 : -if ( d == current->domain ) - rc = -EPERM ; -break ; -case HVM_PARAM_MEMORY_EVENT_INT3 : -case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : -case HVM_PARAM_MEMORY_EVENT_MSR : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -if ( a.value & HVMPME_onchangeonly ) - rc = -EINVAL ; -break ; -case HVM_PARAM_NESTEDHVM : - rc =xsm_hvm_param_nested (XSM_PRIV , d); -if ( rc ) -break ; -if ( a.value >1 ) - rc = -EINVAL ; -/* Remove the check below once we have -* shadow-on-shadow. -*/ -if ( cpu_has_svm && !paging_mode_hap (d) && a.value ) - rc = -EINVAL ; -/* Set up NHVM state for any vcpus that are already up */ -if ( a.value && - !d->arch.hvm_domain.params[HVM_PARAM_NESTEDHVM ] ) -for_each_vcpu (d, v) -if ( rc ==0 ) - rc =nestedhvm_vcpu_initialise (v); -if ( !a.value || rc ) -for_each_vcpu (d, v) -nestedhvm_vcpu_destroy (v); -break ; -case HVM_PARAM_BUFIOREQ_EVTCHN : - rc = -EINVAL ; -break ; -case HVM_PARAM_TRIPLE_FAULT_REASON : -if ( a.value >SHUTDOWN_MAX ) - rc = -EINVAL ; -break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } - d->arch.hvm_domain.ioreq_gmfn.base = a.value; -break ; -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : - { -unsigned int i; - -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -if ( a.value ==0 || - a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) - { - rc = -EINVAL ; -break ; - } -for ( i =0 ; i < a.value; i++ ) -set_bit (i, &d->arch.hvm_domain.ioreq_gmfn.mask); - -break ; - } - } - -if ( rc ==0 ) - { - d->arch.hvm_domain.params[a.index] = a.value; - -switch ( a.index ) - { -case HVM_PARAM_MEMORY_EVENT_INT3 :xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg)
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +0
-10-case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : - { -domain_pause (d); -domain_unpause (d);/* Causes guest to latch new status */ -break ; - } -case HVM_PARAM_MEMORY_EVENT_CR3 : - { -for_each_vcpu ( d, v ) - hvm_funcs.update_guest_cr(v, 0); /* Latches new CR3 maskthrough CR0 code */
Quoted 58 lines
xen/arch/x86/hvm/hvm.c +6
-46-break ; - } - } - - } - - } -else - { -switch ( a.index ) - { -case HVM_PARAM_ACPI_S_STATE : - a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ; -break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } -case HVM_PARAM_IOREQ_PFN : -case HVM_PARAM_BUFIOREQ_PFN : -case HVM_PARAM_BUFIOREQ_EVTCHN : { -domid_t domid; - -/* May need to create server */ - domid = d- arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN ]; - rc =hvm_create_ioreq_server (d, domid,1 ,1 ,NULL ); -if ( rc !=0 && rc != -EEXIST ) - goto param_fail; -/*FALLTHRU*/ - } -default : - a.value = d->arch.hvm_domain.params[a.index]; -break ; - } - rc =__copy_to_guest (arg, &a,1 ) ? -EFAULT :0 ; - } - -HVM_DBG_LOG (DBG_LEVEL_HCALL ,"%s param %u = %" PRIx64, - op == HVMOP_set_param ?"set" :"get" , - a.index, a.value); + rc =hvmop_set_param ( +guest_handle_cast (arg, xen_hvm_param_t)); +break ; - param_fail: -rcu_unlock_domain (d); +case HVMOP_get_param: + rc =hvmop_get_param ( +guest_handle_cast (arg, xen_hvm_param_t));break ; - }case HVMOP_set_pci_intx_level:rc = hvmop_set_pci_intx_level(
The subject line should obviously be prefixed [PATCH 0/3], but the content is at least correct.
Apologies for that,
Paul
Quoted 18 lines
-----Original Message-----
From: Paul Durrant [mailto:[email protected]]
Sent: 24 April 2015 17:21
To: [email protected]
Cc: Paul Durrant
Subject: [PATCH 0/2] x86/hvm: HVMOP_get/set_param improvementsThe following 3 patches re-structure the code implementing
HVMOP_set_param
andHVMOP_get_param.Patch #1 gives each operation its own function
Patch #2 splits out checks for getting/setting non-reflexive params and setting params with change-once semantics, as well as the XSM check into separate functions
Patch #3 adds all remaining ioreq server params to non-reflexive lists
PATCH 2/3 x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
In reply to Paul DurrantSome parameters can only (validly) be set once. Some cannot be set by a guest for its own domain. Consolidate these checks, along with the XSM check, in a new hvm_allow_set_param() function for clarity.
Also, introduce hvm_allow_get_param() for similar reasons.
--- xen/arch/x86/hvm/hvm.c | 141 +++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 62 deletions(-)
xen/arch/x86/hvm/hvm.c +79 -62
@@ -5640,6 +5640,57 @@ static int hvmop_set_evtchn_upcall_vector(return 0 ; } +static int hvm_allow_set_param (struct domain *d, +struct xen_hvm_param *a) +{ +uint64_t value = d->arch.hvm_domain.params[a->index]; +int rc =0 ; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); +if ( rc ) +return rc; + +/* The following parameters cannot be set by the guest */ +switch (a->index) + { +case HVM_PARAM_VIRIDIAN : +case HVM_PARAM_IDENT_PT : +case HVM_PARAM_DM_DOMAIN : +case HVM_PARAM_ACPI_S_STATE : +case HVM_PARAM_MEMORY_EVENT_CR0 : +case HVM_PARAM_MEMORY_EVENT_CR3 : +case HVM_PARAM_MEMORY_EVENT_CR4 : +case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : +case HVM_PARAM_MEMORY_EVENT_MSR : +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + rc = -EPERM ; +break ; +default : +break ; + } + +if ( rc ) +return rc; + +/* The following parameters can only be changed once */ +switch (a->index) + { +case HVM_PARAM_VIRIDIAN : +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if (value !=0 && a->value != value) + rc = -EEXIST ; +break ; +default : +break ; + } + +return rc; +} +static int hvmop_set_param (XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) {
@@ -5666,7 +5717,7 @@ static int hvmop_set_param( && (a.index != HVM_PARAM_CALLBACK_IRQ) ) goto out; - rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); + rc =hvm_allow_set_param (d, &a);if ( rc ) goto out;
@@ -5681,31 +5732,11 @@ static int hvmop_set_param( rc = -EINVAL ;break ;case HVM_PARAM_VIRIDIAN : -/* This should only ever be set once by the tools and read by the guest. */ - rc = -EPERM ; -if ( d == current->domain ) -break ; - -if ( a.value != d->arch.hvm_domain.params[a.index] ) - { - rc = -EEXIST ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - +if ( (a.value & ~HVMPV_feature_mask) || + !(a.value & HVMPV_base_freq) ) rc = -EINVAL ; -if ( (a.value & ~HVMPV_feature_mask) || - !(a.value & HVMPV_base_freq) ) -break ; - } - - rc =0 ;break ;case HVM_PARAM_IDENT_PT : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( d == current->domain ) -break ; - rc = -EINVAL ;if ( d->arch.hvm_domain.params[a.index] !=0 )break ;
@@ -5733,22 +5764,12 @@ static int hvmop_set_param(domctl_lock_release ();break ;case HVM_PARAM_DM_DOMAIN : -/* Not reflexive, as we may need to domain_pause(). */ - rc = -EPERM ; -if ( d == current->domain ) -break ; -if ( a.value ==DOMID_SELF ) a.value = current->domain->domain_id; rc =hvm_set_dm_domain (d, a.value);break ;case HVM_PARAM_ACPI_S_STATE : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( current->domain == d ) -break ; - rc =0 ;if ( a.value ==3 )hvm_s3_suspend (d);
@@ -5761,20 +5782,9 @@ static int hvmop_set_param(case HVM_PARAM_ACPI_IOPORTS_LOCATION : rc =pmtimer_change_ioport (d, a.value);break ; -case HVM_PARAM_MEMORY_EVENT_CR0 : -case HVM_PARAM_MEMORY_EVENT_CR3 : -case HVM_PARAM_MEMORY_EVENT_CR4 : -if ( d == current->domain ) - rc = -EPERM ; -break ;case HVM_PARAM_MEMORY_EVENT_INT3 :case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP :case HVM_PARAM_MEMORY_EVENT_MSR : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - }if ( a.value & HVMPME_onchangeonly ) rc = -EINVAL ;break ;
@@ -5807,22 +5817,12 @@ static int hvmop_set_param( rc = -EINVAL ;break ;case HVM_PARAM_IOREQ_SERVER_PFN : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } d->arch.hvm_domain.ioreq_gmfn.base = a.value;break ;case HVM_PARAM_NR_IOREQ_SERVER_PAGES : {unsigned int i; -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - }if ( a.value ==0 || a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) {
@@ -5866,6 +5866,30 @@ static int hvmop_set_param(return rc; } +static int hvm_allow_get_param (struct domain *d, +struct xen_hvm_param *a) +{ +int rc =0 ; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); +if ( rc ) +return rc; + +/* The following parameters cannot be read by the guest */ +switch (a->index) + { +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + rc = -EPERM ; +break ; +default : +break ; + } + +return rc; +} +static int hvmop_get_param (XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) {
@@ -5891,7 +5915,7 @@ static int hvmop_get_param( && (a.index != HVM_PARAM_CALLBACK_IRQ) ) goto out; - rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); + rc =hvm_allow_get_param (d, &a);if ( rc ) goto out;
@@ -5900,13 +5924,6 @@ static int hvmop_get_param(case HVM_PARAM_ACPI_S_STATE : a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ;break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : -if ( d == current->domain ) - { - rc = -EPERM ; - goto out; - }case HVM_PARAM_IOREQ_PFN :case HVM_PARAM_BUFIOREQ_PFN :case HVM_PARAM_BUFIOREQ_EVTCHN : {
Signature · 1 line
1.7.10.4
Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
Quoted 19 lines · Paul Durrant
Some parameters can only (validly) be set once. Some cannot be set by a guest for its own domain. Consolidate these checks, along with the XSM check, in a new
hvm_allow_set_param()function for clarity.Also, introduce
hvm_allow_get_param()for similar reasons.Signed-off-by Paul Durrant <[email protected]>Cc Keir Fraser <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>--- xen/arch/x86/hvm/hvm.c | 141 +++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 62 deletions(-)xen/arch/x86/hvm/hvm.c +0
-0
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5644
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +2
-0@@ -5640,6 +5640,57 @@ static int hvmop_set_evtchn_upcall_vector(return 0 ; } +static int hvm_allow_set_param (struct domain *d, +struct xen_hvm_param *a)
const
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5647
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +3
-0+{ +uint64_t value = d->arch.hvm_domain.params[a->index]; +int rc =0 ;
No need for the 0 initialiser (and I know Coverity will complain about it).
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5654
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +7
-0+ + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); +if ( rc ) +return rc; + +/* The following parameters cannot be set by the guest */ + switch (a->index)
style
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +8
-0+ { +case HVM_PARAM_VIRIDIAN : +case HVM_PARAM_IDENT_PT : +case HVM_PARAM_DM_DOMAIN : +case HVM_PARAM_ACPI_S_STATE : +case HVM_PARAM_MEMORY_EVENT_CR0 : +case HVM_PARAM_MEMORY_EVENT_CR3 : +case HVM_PARAM_MEMORY_EVENT_CR4 :
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5672
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : +case HVM_PARAM_MEMORY_EVENT_MSR : +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + rc = -EPERM ; +break ; +default : +break ;
Please can we turn the guest-settable list into a whitelist rather than a blacklist. We have had several XSAs in the past where a guest has been able to modify a value it shouldn't have because new params were default writable by everyone.
Quoted 147 lines
xen/arch/x86/hvm/hvm.c +33
-54+ } + +if ( rc ) +return rc; + +/* The following parameters can only be changed once */ +switch (a->index) + { +case HVM_PARAM_VIRIDIAN : +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if (value !=0 && a->value != value) + rc = -EEXIST ; +break ; +default : +break ; + } + +return rc; +} +static int hvmop_set_param (XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) {@@ -5666,7 +5717,7 @@ static int hvmop_set_param( && (a.index != HVM_PARAM_CALLBACK_IRQ) ) goto out; - rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); + rc =hvm_allow_set_param (d, &a);if ( rc ) goto out;@@ -5681,31 +5732,11 @@ static int hvmop_set_param( rc = -EINVAL ;break ;case HVM_PARAM_VIRIDIAN : -/* This should only ever be set once by the tools and read by the guest. */ - rc = -EPERM ; -if ( d == current->domain ) -break ; - -if ( a.value != d->arch.hvm_domain.params[a.index] ) - { - rc = -EEXIST ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - +if ( (a.value & ~HVMPV_feature_mask) || + !(a.value & HVMPV_base_freq) ) rc = -EINVAL ; -if ( (a.value & ~HVMPV_feature_mask) || - !(a.value & HVMPV_base_freq) ) -break ; - } - - rc =0 ;break ;case HVM_PARAM_IDENT_PT : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( d == current->domain ) -break ; - rc = -EINVAL ;if ( d->arch.hvm_domain.params[a.index] !=0 )break ;@@ -5733,22 +5764,12 @@ static int hvmop_set_param(domctl_lock_release ();break ;case HVM_PARAM_DM_DOMAIN : -/* Not reflexive, as we may need to domain_pause(). */ - rc = -EPERM ; -if ( d == current->domain ) -break ; -if ( a.value ==DOMID_SELF ) a.value = current->domain->domain_id; rc =hvm_set_dm_domain (d, a.value);break ;case HVM_PARAM_ACPI_S_STATE : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( current->domain == d ) -break ; - rc =0 ;if ( a.value ==3 )hvm_s3_suspend (d);@@ -5761,20 +5782,9 @@ static int hvmop_set_param(case HVM_PARAM_ACPI_IOPORTS_LOCATION : rc =pmtimer_change_ioport (d, a.value);break ; -case HVM_PARAM_MEMORY_EVENT_CR0 : -case HVM_PARAM_MEMORY_EVENT_CR3 : -case HVM_PARAM_MEMORY_EVENT_CR4 : -if ( d == current->domain ) - rc = -EPERM ; -break ;case HVM_PARAM_MEMORY_EVENT_INT3 :case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP :case HVM_PARAM_MEMORY_EVENT_MSR : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - }if ( a.value & HVMPME_onchangeonly ) rc = -EINVAL ;break ;@@ -5807,22 +5817,12 @@ static int hvmop_set_param( rc = -EINVAL ;break ;case HVM_PARAM_IOREQ_SERVER_PFN : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } d->arch.hvm_domain.ioreq_gmfn.base = a.value;break ;case HVM_PARAM_NR_IOREQ_SERVER_PAGES : {unsigned int i; -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - }if ( a.value ==0 || a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) {@@ -5866,6 +5866,30 @@ static int hvmop_set_param(return rc; } +static int hvm_allow_get_param (struct domain *d, +struct xen_hvm_param *a) +{ +int rc =0 ; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); +if ( rc ) +return rc; +
xen/arch/x86/hvm/hvm.c static int hvmop_set_param( · line 5887
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+/* The following parameters cannot be read by the guest */ +switch (a->index) + { +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + rc = -EPERM ; +break ; +default : +break ;
Again, this would be safer as a whitelist.
~Andrew
Quoted 31 lines
xen/arch/x86/hvm/hvm.c +6
-8+ } + +return rc; +} +static int hvmop_get_param (XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) {@@ -5891,7 +5915,7 @@ static int hvmop_get_param( && (a.index != HVM_PARAM_CALLBACK_IRQ) ) goto out; - rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); + rc =hvm_allow_get_param (d, &a);if ( rc ) goto out;@@ -5900,13 +5924,6 @@ static int hvmop_get_param(case HVM_PARAM_ACPI_S_STATE : a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ;break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : -if ( d == current->domain ) - { - rc = -EPERM ; - goto out; - }case HVM_PARAM_IOREQ_PFN :case HVM_PARAM_BUFIOREQ_PFN :case HVM_PARAM_BUFIOREQ_EVTCHN : {
Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
Quoted 32 lines
-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 25 April 2015 01:19
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checksQuoted 4 lines · Paul Durrant
Some parameters can only (validly) be set once. Some cannot be set by a guest for its own domain. Consolidate these checks, along with the XSM check, in a new
hvm_allow_set_param()function for clarity.Quoted 8 lines
Also, introduce
hvm_allow_get_param()for similar reasons.Signed-off-by Paul Durrant <[email protected]>Cc Keir Fraser <[email protected]>Cc Jan Beulich <[email protected]>Cc Andrew Cooper <[email protected]>--- xen/arch/x86/hvm/hvm.c | 141 +++++++++++++++++++++++++++---------------------
Quoted 10 lines
1 file changed, 79 insertions(+), 62 deletions(-)xen/arch/x86/hvm/hvm.c +0
-0@@ -5640,6 +5640,57 @@ static int hvmop_set_evtchn_upcall_vector(return 0 ; }
Quoted 5 lines
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5644
Quoted 2 lines
xen/arch/x86/hvm/hvm.c +2
-0+static int hvm_allow_set_param (struct domain *d, +struct xen_hvm_param *a)const
Good point.
Quoted 6 lines
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5647
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +3
-0+{ +uint64_t value = d->arch.hvm_domain.params[a->index]; +int rc =0 ;No need for the 0 initialiser (and I know Coverity will complain about it).
Quoted 10 lines
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5654
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +7
-0+ + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); +if ( rc ) +return rc; + +/* The following parameters cannot be set by the guest */ + switch (a->index)style
Oops.
Quoted 19 lines
Quoted 8 lines
xen/arch/x86/hvm/hvm.c +8
-0+ { +case HVM_PARAM_VIRIDIAN : +case HVM_PARAM_IDENT_PT : +case HVM_PARAM_DM_DOMAIN : +case HVM_PARAM_ACPI_S_STATE : +case HVM_PARAM_MEMORY_EVENT_CR0 : +case HVM_PARAM_MEMORY_EVENT_CR3 : +case HVM_PARAM_MEMORY_EVENT_CR4 :xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5672
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+case HVM_PARAM_MEMORY_EVENT_INT3 : +case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP : +case HVM_PARAM_MEMORY_EVENT_MSR : +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + rc = -EPERM ; +break ; +default : +break ;
Quoted 5 lines
Please can we turn the guest-settable list into a whitelist rather than a blacklist. We have had several XSAs in the past where a guest has been able to modify a value it shouldn't have because new params were default writable by everyone.
Yes, I think that's probably a good idea.
Quoted 152 lines
Quoted 34 lines
xen/arch/x86/hvm/hvm.c +22
-1+ } + +if ( rc ) +return rc; + +/* The following parameters can only be changed once */ +switch (a->index) + { +case HVM_PARAM_VIRIDIAN : +case HVM_PARAM_IOREQ_SERVER_PFN : +case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if (value !=0 && a->value != value) + rc = -EEXIST ; +break ; +default : +break ; + } + +return rc; +} +static int hvmop_set_param (XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) {@@ -5666,7 +5717,7 @@ static int hvmop_set_param( && (a.index != HVM_PARAM_CALLBACK_IRQ) ) goto out; - rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_set_param); + rc =hvm_allow_set_param (d, &a);if ( rc ) goto out;xen/arch/x86/hvm/hvm.c static int hvmop_set_param( · line 5684
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +0
-1@@ -5681,31 +5732,11 @@ static int hvmop_set_param( rc = -EINVAL ;break ;case HVM_PARAM_VIRIDIAN : -/* This should only ever be set once by the tools and read by the guest. */
Quoted 113 lines
xen/arch/x86/hvm/hvm.c +15
-52- rc = -EPERM ; -if ( d == current->domain ) -break ; - -if ( a.value != d->arch.hvm_domain.params[a.index] ) - { - rc = -EEXIST ; -if ( d->arch.hvm_domain.params[a.index] !=0 ) -break ; - +if ( (a.value & ~HVMPV_feature_mask) || + !(a.value & HVMPV_base_freq) ) rc = -EINVAL ; -if ( (a.value & ~HVMPV_feature_mask) || - !(a.value & HVMPV_base_freq) ) -break ; - } - - rc =0 ;break ;case HVM_PARAM_IDENT_PT : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( d == current->domain ) -break ; - rc = -EINVAL ;if ( d->arch.hvm_domain.params[a.index] !=0 )break ;@@ -5733,22 +5764,12 @@ static int hvmop_set_param(domctl_lock_release ();break ;case HVM_PARAM_DM_DOMAIN : -/* Not reflexive, as we may need to domain_pause(). */ - rc = -EPERM ; -if ( d == current->domain ) -break ; -if ( a.value ==DOMID_SELF ) a.value = current->domain->domain_id; rc =hvm_set_dm_domain (d, a.value);break ;case HVM_PARAM_ACPI_S_STATE : -/* Not reflexive, as we must domain_pause(). */ - rc = -EPERM ; -if ( current->domain == d ) -break ; - rc =0 ;if ( a.value ==3 )hvm_s3_suspend (d);@@ -5761,20 +5782,9 @@ static int hvmop_set_param(case HVM_PARAM_ACPI_IOPORTS_LOCATION : rc =pmtimer_change_ioport (d, a.value);break ; -case HVM_PARAM_MEMORY_EVENT_CR0 : -case HVM_PARAM_MEMORY_EVENT_CR3 : -case HVM_PARAM_MEMORY_EVENT_CR4 : -if ( d == current->domain ) - rc = -EPERM ; -break ;case HVM_PARAM_MEMORY_EVENT_INT3 :case HVM_PARAM_MEMORY_EVENT_SINGLE_STEP :case HVM_PARAM_MEMORY_EVENT_MSR : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - }if ( a.value & HVMPME_onchangeonly ) rc = -EINVAL ;break ;@@ -5807,22 +5817,12 @@ static int hvmop_set_param( rc = -EINVAL ;break ;case HVM_PARAM_IOREQ_SERVER_PFN : -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - } d->arch.hvm_domain.ioreq_gmfn.base = a.value;break ;case HVM_PARAM_NR_IOREQ_SERVER_PAGES : {unsigned int i; -if ( d == current->domain ) - { - rc = -EPERM ; -break ; - }if ( a.value ==0 || a.value >sizeof (d->arch.hvm_domain.ioreq_gmfn.mask) *8 ) {@@ -5866,6 +5866,30 @@ static int hvmop_set_param(return rc; } +static int hvm_allow_get_param (struct domain *d, +struct xen_hvm_param *a) +{ +int rc =0 ; + + rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); +if ( rc ) +return rc; + +/* The following parameters cannot be read by the guest */ +switch (a->index) + { +case HVM_PARAM_IOREQ_SERVER_PFN :
Quoted 9 lines
xen/arch/x86/hvm/hvm.c static int hvmop_set_evtchn_upcall_vector( · line 5672
Quoted 6 lines
xen/arch/x86/hvm/hvm.c +6
-0+case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +if ( d == current->domain ) + rc = -EPERM ; +break ; +default : +break ;Again, this would be safer as a whitelist.
Hmm, maybe. I'll code it that way in v2 but it does seem to go against the general idea of HVM params (which AFAICT were originally analogous to start_info for PV guests i.e. for passing info from Xen to guests).
Paul
Quoted 33 lines
~Andrew
Quoted 31 lines
xen/arch/x86/hvm/hvm.c +6
-8+ } + +return rc; +} +static int hvmop_get_param (XEN_GUEST_HANDLE_PARAM (xen_hvm_param_t) arg) {@@ -5891,7 +5915,7 @@ static int hvmop_get_param( && (a.index != HVM_PARAM_CALLBACK_IRQ) ) goto out; - rc =xsm_hvm_param (XSM_TARGET , d, HVMOP_get_param); + rc =hvm_allow_get_param (d, &a);if ( rc ) goto out;@@ -5900,13 +5924,6 @@ static int hvmop_get_param(case HVM_PARAM_ACPI_S_STATE : a.value = d->arch.hvm_domain.is_s3_suspended ?3 :0 ;break ; -case HVM_PARAM_IOREQ_SERVER_PFN : -case HVM_PARAM_NR_IOREQ_SERVER_PAGES : -if ( d == current->domain ) - { - rc = -EPERM ; - goto out; - }case HVM_PARAM_IOREQ_PFN :case HVM_PARAM_BUFIOREQ_PFN :case HVM_PARAM_BUFIOREQ_EVTCHN : {
Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
In reply to Paul DurrantQuoted 2 lines
Quoted 1 line
Quoted 1 line
Paul Durrant <[email protected]> 04/24/15 6:35 PM >>>
+ /* The following parameters cannot be set by the guest */
Please add a stop ad the end of at least sentence-like comments.
Quoted 2 lines
case
HVM_PARAM_DM_DOMAIN:
- /* Not reflexive, as we may need todomain_pause(). */
Especially when the reason for not allowing guest access is other than a simple permission thing, I think for documentation purposes it would be useful to retain these comments.
a.value = current->domain->domain_id;
>+ /* The following parameters cannot be read by the guest */
Perhaps "cannot" isn't the right term here - "shouldn't"?
Jan
Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
Quoted 8 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 29 April 2015 13:29
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Paul Durrant <[email protected]> 04/24/15 6:35 PM >>>
+ /* The following parameters cannot be set by the guest */
Please add a stop ad the end of at least sentence-like comments.
Ok. I tend to skip full stops for single lines and only use them for multi-line. Just my habit though...
Quoted 7 lines
Quoted 2 lines
case
HVM_PARAM_DM_DOMAIN:
- /* Not reflexive, as we may need todomain_pause(). */Especially when the reason for not allowing guest access is other than a simple permission thing, I think for documentation purposes it would be useful to retain these comments.
I have acted upon Andrew's request for whitelists in v3 of the series, so I think this point is moot. I'll wait for your comments on v3 when I've posted it (which should be today with any luck).
Quoted 6 lines
a.value =
current->domain->domain_id;>+ /* The following parameters cannot be read by the guest */
Perhaps "cannot" isn't the right term here - "shouldn't"?
Yes, you are probably correct.
Paul
Quoted 1 line
Jan
Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checks
In reply to Jan BeulichQuoted 28 lines
-----Original Message-----
From: Paul Durrant
Sent: 01 May 2015 14:39
To: 'Jan Beulich'
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checksQuoted 8 lines
-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 29 April 2015 13:29
To: Paul Durrant
Cc: Andrew Cooper; [email protected]; Keir (Xen.org)
Subject: Re: [PATCH 2/3] x86/hvm: introduce functions for HVMOP_get/set_param allowance checksQuoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Paul Durrant <[email protected]> 04/24/15 6:35 PM >>>
+ /* The following parameters cannot be set by the guest */
Please add a stop ad the end of at least sentence-like comments.
Ok. I tend to skip full stops for single lines and only use them for multi-line. Just my habit though...
Quoted 3 lines
Quoted 2 lines
case
HVM_PARAM_DM_DOMAIN:
- /* Not reflexive, as we may need todomain_pause(). */
Quoted 8 lines
Quoted 4 lines
Especially when the reason for not allowing guest access is other than a simple permission thing, I think for documentation purposes it would be useful to retain these comments.
I have acted upon Andrew's request for whitelists in v3 of the series, so I think this point is moot. I'll wait for your comments on v3 when I've posted it (which should be today with any luck).
s/v3/v2
Quoted 13 lines
Quoted 7 lines
a.value =
current->domain->domain_id;>+ /* The following parameters cannot be read by the guest */
Perhaps "cannot" isn't the right term here - "shouldn't"?
Yes, you are probably correct.
Paul
Quoted 1 line
Jan
PATCH 3/3 x86/hvm: disallow guest get and set of all ioreq server HVM params
In reply to Paul DurrantA guest has no need to touch these parameters and reading HVM_PARAM_IOREQ_PFN, HVM_PARAM_BUFIOREQ_PFN, or HVM_PARAM_BUFIOREQ_EVTCHN may cause Xen to create a default ioreq server where one did not already exist.
--- xen/arch/x86/hvm/hvm.c | 7 +++++++ 1 file changed, 7 insertions(+)
xen/arch/x86/hvm/hvm.c +7 -0
@@ -5665,6 +5665,9 @@ static int hvm_allow_set_param(struct domain *d,case HVM_PARAM_MEMORY_EVENT_MSR :case HVM_PARAM_IOREQ_SERVER_PFN :case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +case HVM_PARAM_IOREQ_PFN : +case HVM_PARAM_BUFIOREQ_PFN : +case HVM_PARAM_BUFIOREQ_EVTCHN :if ( d == current->domain ) rc = -EPERM ;break ;
@@ -5880,6 +5883,10 @@ static int hvm_allow_get_param(struct domain *d, {case HVM_PARAM_IOREQ_SERVER_PFN :case HVM_PARAM_NR_IOREQ_SERVER_PAGES : +case HVM_PARAM_IOREQ_PFN : +case HVM_PARAM_BUFIOREQ_PFN : +case HVM_PARAM_BUFIOREQ_EVTCHN : +case HVM_PARAM_DM_DOMAIN :if ( d == current->domain ) rc = -EPERM ;break ;
Signature · 1 line
1.7.10.4