ia64/xen-unstable

changeset 16383:ef4b60c99735

x86, hvm: Small code cleanups.
Based on patch from Xin Li.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Nov 16 16:22:00 2007 +0000 (2007-11-16)
parents 79d050b2b35e
children e82fb0729b51
files xen/arch/x86/hvm/hvm.c xen/arch/x86/hvm/vmx/vmx.c
line diff
     1.1 --- a/xen/arch/x86/hvm/hvm.c	Fri Nov 16 14:45:35 2007 +0000
     1.2 +++ b/xen/arch/x86/hvm/hvm.c	Fri Nov 16 16:22:00 2007 +0000
     1.3 @@ -667,7 +667,7 @@ int hvm_set_cr0(unsigned long value)
     1.4      struct vcpu *v = current;
     1.5      p2m_type_t p2mt;
     1.6      unsigned long gfn, mfn, old_value = v->arch.hvm_vcpu.guest_cr[0];
     1.7 -  
     1.8 +
     1.9      HVM_DBG_LOG(DBG_LEVEL_VMMU, "Update CR0 value = %lx", value);
    1.10  
    1.11      if ( (u32)value != value )
    1.12 @@ -684,7 +684,7 @@ int hvm_set_cr0(unsigned long value)
    1.13      /* ET is reserved and should be always be 1. */
    1.14      value |= X86_CR0_ET;
    1.15  
    1.16 -    if ( (value & (X86_CR0_PE|X86_CR0_PG)) == X86_CR0_PG )
    1.17 +    if ( (value & (X86_CR0_PE | X86_CR0_PG)) == X86_CR0_PG )
    1.18      {
    1.19          hvm_inject_exception(TRAP_gp_fault, 0, 0);
    1.20          return 0;
    1.21 @@ -710,10 +710,10 @@ int hvm_set_cr0(unsigned long value)
    1.22              /* The guest CR3 must be pointing to the guest physical. */
    1.23              gfn = v->arch.hvm_vcpu.guest_cr[3]>>PAGE_SHIFT;
    1.24              mfn = mfn_x(gfn_to_mfn_current(gfn, &p2mt));
    1.25 -            if ( !p2m_is_ram(p2mt) || !mfn_valid(mfn) || 
    1.26 +            if ( !p2m_is_ram(p2mt) || !mfn_valid(mfn) ||
    1.27                   !get_page(mfn_to_page(mfn), v->domain))
    1.28              {
    1.29 -                gdprintk(XENLOG_ERR, "Invalid CR3 value = %lx (mfn=%lx)\n", 
    1.30 +                gdprintk(XENLOG_ERR, "Invalid CR3 value = %lx (mfn=%lx)\n",
    1.31                           v->arch.hvm_vcpu.guest_cr[3], mfn);
    1.32                  domain_crash(v->domain);
    1.33                  return 0;
    1.34 @@ -742,7 +742,7 @@ int hvm_set_cr0(unsigned long value)
    1.35          }
    1.36      }
    1.37  
    1.38 -    if ( !list_empty(&(domain_hvm_iommu(v->domain)->pdev_list)) )
    1.39 +    if ( !list_empty(&domain_hvm_iommu(v->domain)->pdev_list) )
    1.40      {
    1.41          if ( (value & X86_CR0_CD) && !(value & X86_CR0_NW) )
    1.42          {
     2.1 --- a/xen/arch/x86/hvm/vmx/vmx.c	Fri Nov 16 14:45:35 2007 +0000
     2.2 +++ b/xen/arch/x86/hvm/vmx/vmx.c	Fri Nov 16 16:22:00 2007 +0000
     2.3 @@ -518,11 +518,37 @@ void vmx_vmcs_save(struct vcpu *v, struc
     2.4      vmx_vmcs_exit(v);
     2.5  }
     2.6  
     2.7 -int vmx_vmcs_restore(struct vcpu *v, struct hvm_hw_cpu *c)
     2.8 +static int vmx_restore_cr0_cr3(
     2.9 +    struct vcpu *v, unsigned long cr0, unsigned long cr3)
    2.10  {
    2.11      unsigned long mfn = 0;
    2.12      p2m_type_t p2mt;
    2.13  
    2.14 +    if ( cr0 & X86_CR0_PG )
    2.15 +    {
    2.16 +        mfn = mfn_x(gfn_to_mfn(v->domain, cr3 >> PAGE_SHIFT, &p2mt));
    2.17 +        if ( !p2m_is_ram(p2mt) || !get_page(mfn_to_page(mfn), v->domain) )
    2.18 +        {
    2.19 +            gdprintk(XENLOG_ERR, "Invalid CR3 value=0x%lx\n", cr3);
    2.20 +            return -EINVAL;
    2.21 +        }
    2.22 +    }
    2.23 +
    2.24 +    if ( v->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PG )
    2.25 +        put_page(pagetable_get_page(v->arch.guest_table));
    2.26 +
    2.27 +    v->arch.guest_table = pagetable_from_pfn(mfn);
    2.28 +
    2.29 +    v->arch.hvm_vcpu.guest_cr[0] = cr0 | X86_CR0_ET;
    2.30 +    v->arch.hvm_vcpu.guest_cr[3] = cr3;
    2.31 +
    2.32 +    return 0;
    2.33 +}
    2.34 +
    2.35 +int vmx_vmcs_restore(struct vcpu *v, struct hvm_hw_cpu *c)
    2.36 +{
    2.37 +    int rc;
    2.38 +
    2.39      if ( c->pending_valid &&
    2.40           ((c->pending_type == 1) || (c->pending_type > 6) ||
    2.41            (c->pending_reserved != 0)) )
    2.42 @@ -532,26 +558,13 @@ int vmx_vmcs_restore(struct vcpu *v, str
    2.43          return -EINVAL;
    2.44      }
    2.45  
    2.46 -    if ( c->cr0 & X86_CR0_PG )
    2.47 -    {
    2.48 -        mfn = mfn_x(gfn_to_mfn(v->domain, c->cr3 >> PAGE_SHIFT, &p2mt));
    2.49 -        if ( !p2m_is_ram(p2mt) || !get_page(mfn_to_page(mfn), v->domain) )
    2.50 -        {
    2.51 -            gdprintk(XENLOG_ERR, "Invalid CR3 value=0x%"PRIx64"\n", c->cr3);
    2.52 -            return -EINVAL;
    2.53 -        }
    2.54 -    }
    2.55 -
    2.56 -    if ( v->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PG )
    2.57 -        put_page(pagetable_get_page(v->arch.guest_table));
    2.58 -
    2.59 -    v->arch.guest_table = pagetable_from_pfn(mfn);
    2.60 +    rc = vmx_restore_cr0_cr3(v, c->cr0, c->cr3);
    2.61 +    if ( rc )
    2.62 +        return rc;
    2.63  
    2.64      vmx_vmcs_enter(v);
    2.65  
    2.66 -    v->arch.hvm_vcpu.guest_cr[0] = c->cr0 | X86_CR0_ET;
    2.67      v->arch.hvm_vcpu.guest_cr[2] = c->cr2;
    2.68 -    v->arch.hvm_vcpu.guest_cr[3] = c->cr3;
    2.69      v->arch.hvm_vcpu.guest_cr[4] = c->cr4;
    2.70      vmx_update_guest_cr(v, 0);
    2.71      vmx_update_guest_cr(v, 2);
    2.72 @@ -1846,30 +1859,16 @@ static void vmx_world_save(struct vcpu *
    2.73  static int vmx_world_restore(struct vcpu *v, struct vmx_assist_context *c)
    2.74  {
    2.75      struct cpu_user_regs *regs = guest_cpu_user_regs();
    2.76 -    unsigned long mfn = 0;
    2.77 -    p2m_type_t p2mt;
    2.78 -
    2.79 -    if ( c->cr0 & X86_CR0_PG )
    2.80 -    {
    2.81 -        mfn = mfn_x(gfn_to_mfn(v->domain, c->cr3 >> PAGE_SHIFT, &p2mt));
    2.82 -        if ( !p2m_is_ram(p2mt) || !get_page(mfn_to_page(mfn), v->domain) )
    2.83 -        {
    2.84 -            gdprintk(XENLOG_ERR, "Invalid CR3 value=%x", c->cr3);
    2.85 -            return -EINVAL;
    2.86 -        }
    2.87 -    }
    2.88 -
    2.89 -    if ( v->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PG )
    2.90 -        put_page(pagetable_get_page(v->arch.guest_table));
    2.91 -
    2.92 -    v->arch.guest_table = pagetable_from_pfn(mfn);
    2.93 +    int rc;
    2.94 +
    2.95 +    rc = vmx_restore_cr0_cr3(v, c->cr0, c->cr3);
    2.96 +    if ( rc )
    2.97 +        return rc;
    2.98  
    2.99      regs->eip = c->eip;
   2.100      regs->esp = c->esp;
   2.101      regs->eflags = c->eflags | 2;
   2.102  
   2.103 -    v->arch.hvm_vcpu.guest_cr[0] = c->cr0;
   2.104 -    v->arch.hvm_vcpu.guest_cr[3] = c->cr3;
   2.105      v->arch.hvm_vcpu.guest_cr[4] = c->cr4;
   2.106      vmx_update_guest_cr(v, 0);
   2.107      vmx_update_guest_cr(v, 4);
   2.108 @@ -2016,9 +2015,8 @@ static int vmx_assist(struct vcpu *v, in
   2.109  static int vmx_set_cr0(unsigned long value)
   2.110  {
   2.111      struct vcpu *v = current;
   2.112 -    int rc = hvm_set_cr0(value);
   2.113 -
   2.114 -    if ( rc == 0 )
   2.115 +
   2.116 +    if ( hvm_set_cr0(value) == 0 )
   2.117          return 0;
   2.118  
   2.119      /*