summaryrefslogtreecommitdiffstats
path: root/src/proof/cec/cecSimBack.c
blob: c3d09ff59b3d195f135636285378a03e743685f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**CFile****************************************************************

  FileName    [cecSimBack.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Combinational equivalence checking.]

  Synopsis    [Backward simulation.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: cecSimBack.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/

#include "cecInt.h"
#include "aig/gia/giaAig.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ObjSatVerify( Gia_Man_t * p, Gia_Obj_t * pRoot, int Value )
{
    Gia_Obj_t * pObj;
    int i, RetValue = 1;
    printf( "Obj = %4d  Value = %d  ", Gia_ObjId(p, pRoot), Value );
    Gia_ObjTerSimSet0( Gia_ManConst0(p) );
    Gia_ManForEachCi( p, pObj, i )
        if ( !Gia_ObjIsTravIdCurrent(p, pObj) )
            Gia_ObjTerSimSetX( pObj ), printf( "x" );
        else if ( pObj->fMark0 )
            Gia_ObjTerSimSet1( pObj ), printf( "1" );
        else
            Gia_ObjTerSimSet0( pObj ), printf( "0" );
    printf( " " );
    Gia_ManForEachAnd( p, pObj, i )
        Gia_ObjTerSimAnd( pObj );
    if ( Value ? Gia_ObjTerSimGet1(pRoot) : Gia_ObjTerSimGet0(pRoot) )
        printf( "Verification successful.\n" );
    else
        printf( "Verification failed.\n" ), RetValue = 0;
    return RetValue;
}

/**Function*************************************************************

  Synopsis    [Return 1 if pObj can have Value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word Cec_ManCheckSat2_rec( Gia_Man_t * p, Gia_Obj_t * pObj, word Value, Vec_Wrd_t * vValues )
{
    Gia_Obj_t * pFan0, * pFan1;
    if ( Gia_ObjIsTravIdCurrent(p, pObj) )
        return Value == (int)pObj->fMark0;
    Gia_ObjSetTravIdCurrent(p, pObj);
    pObj->fMark0 = Value;
    if ( !Gia_ObjIsAnd(pObj) )
        return 1;
    pFan0 = Gia_ObjFanin0(pObj);
    pFan1 = Gia_ObjFanin1(pObj);
    if ( Value )
    {
        return Cec_ManCheckSat2_rec( p, pFan0, !Gia_ObjFaninC0(pObj), vValues ) &&
               Cec_ManCheckSat2_rec( p, pFan1, !Gia_ObjFaninC1(pObj), vValues );
    }
    if ( Gia_ObjIsTravIdCurrent(p, pFan0) ) // already assigned
    {
        if ( Gia_ObjFaninC0(pObj) == (int)pFan0->fMark0 ) // justified
            return 1;
        return Cec_ManCheckSat2_rec( p, pFan1, Gia_ObjFaninC1(pObj), vValues );        
    }
    if ( Gia_ObjIsTravIdCurrent(p, pFan1) ) // already assigned
    {
        if ( Gia_ObjFaninC1(pObj) == (int)pFan1->fMark0 ) // justified
            return 1;
        return Cec_ManCheckSat2_rec( p, pFan0, Gia_ObjFaninC0(pObj), vValues );        
    }
    return Cec_ManCheckSat2_rec( p, pFan0, Gia_ObjFaninC0(pObj), vValues );
}
word Cec_ManCheckSat2( Gia_Man_t * p, Gia_Obj_t * pObj, int Value, Vec_Wrd_t * vValues )
{
    return 0;
}

/**Function*************************************************************

  Synopsis    [Return 1 if pObj can have Value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cec_ManCheckSat_rec( Gia_Man_t * p, Gia_Obj_t * pObj, int Value )
{
    Gia_Obj_t * pFan0, * pFan1;
    if ( Gia_ObjIsTravIdCurrent(p, pObj) )
        return Value == (int)pObj->fMark0;
    Gia_ObjSetTravIdCurrent(p, pObj);
    pObj->fMark0 = Value;
    if ( !Gia_ObjIsAnd(pObj) )
        return 1;
    pFan0 = Gia_ObjFanin0(pObj);
    pFan1 = Gia_ObjFanin1(pObj);
    if ( Value )
    {
        return Cec_ManCheckSat_rec( p, pFan0, !Gia_ObjFaninC0(pObj) ) &&
               Cec_ManCheckSat_rec( p, pFan1, !Gia_ObjFaninC1(pObj) );
    }
    if ( Gia_ObjIsTravIdCurrent(p, pFan0) ) // already assigned
    {
        if ( Gia_ObjFaninC0(pObj) == (int)pFan0->fMark0 ) // justified
            return 1;
        return Cec_ManCheckSat_rec( p, pFan1, Gia_ObjFaninC1(pObj) );        
    }
    if ( Gia_ObjIsTravIdCurrent(p, pFan1) ) // already assigned
    {
        if ( Gia_ObjFaninC1(pObj) == (int)pFan1->fMark0 ) // justified
            return 1;
        return Cec_ManCheckSat_rec( p, pFan0, Gia_ObjFaninC0(pObj) );        
    }
    return Cec_ManCheckSat_rec( p, pFan0, Gia_ObjFaninC0(pObj) );
}
void Cec_ManSimBack( Gia_Man_t * p )
{
    abctime clk = Abc_Clock();
    Vec_Wrd_t * vValues = Vec_WrdStart( Gia_ManObjNum(p) );
    Gia_Obj_t * pObj; 
    int i, Count = 0;
    word Res;
    Gia_ManSetPhase( p );
    //Gia_ManForEachAnd( p, pObj, i )
    //    printf( "%d", pObj->fPhase );
    //printf( "\n" );
    //return;

    Gia_ManForEachAnd( p, pObj, i )
    {
        Gia_ManIncrementTravId(p);
        //Gia_ManCleanMark0( p );
        Res = Cec_ManCheckSat_rec( p, pObj, !pObj->fPhase );
        //if ( Res )
        //    Cec_ObjSatVerify( p, pObj, !pObj->fPhase );

        //Res = Cec_ManCheckSat2_rec( p, pObj, !pObj->fPhase ? ~(word)0 : 0, vValues );
        //if ( Res )
        //    Cec_ObjSatVerify2( p, pObj, !pObj->fPhase, Res );

        Count += (int)(Res > 0);
    }
    Vec_WrdFree( vValues );
    printf( "Obj = %6d.  SAT = %6d.  ", Gia_ManAndNum(p), Count );
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END
********************/ /* hap code to call when log_dirty is enable. return 0 if no problem found. */ int hap_enable_log_dirty(struct domain *d) { /* turn on PG_log_dirty bit in paging mode */ hap_lock(d); d->arch.paging.mode |= PG_log_dirty; hap_unlock(d); /* set l1e entries of P2M table to NOT_WRITABLE. */ p2m_set_flags_global(d, (_PAGE_PRESENT|_PAGE_USER)); flush_tlb_mask(d->domain_dirty_cpumask); return 0; } int hap_disable_log_dirty(struct domain *d) { hap_lock(d); d->arch.paging.mode &= ~PG_log_dirty; hap_unlock(d); /* set l1e entries of P2M table with normal mode */ p2m_set_flags_global(d, __PAGE_HYPERVISOR|_PAGE_USER); return 0; } void hap_clean_dirty_bitmap(struct domain *d) { /* mark physical memory as NOT_WRITEABLE and flush the TLB */ p2m_set_flags_global(d, (_PAGE_PRESENT|_PAGE_USER)); flush_tlb_mask(d->domain_dirty_cpumask); } /************************************************/ /* HAP SUPPORT FUNCTIONS */ /************************************************/ static struct page_info *hap_alloc(struct domain *d) { struct page_info *pg = NULL; void *p; ASSERT(hap_locked_by_me(d)); if ( unlikely(list_empty(&d->arch.paging.hap.freelist)) ) return NULL; pg = list_entry(d->arch.paging.hap.freelist.next, struct page_info, list); list_del(&pg->list); d->arch.paging.hap.free_pages--; p = hap_map_domain_page(page_to_mfn(pg)); ASSERT(p != NULL); clear_page(p); hap_unmap_domain_page(p); return pg; } static void hap_free(struct domain *d, mfn_t mfn) { struct page_info *pg = mfn_to_page(mfn); ASSERT(hap_locked_by_me(d)); d->arch.paging.hap.free_pages++; list_add_tail(&pg->list, &d->arch.paging.hap.freelist); } static struct page_info *hap_alloc_p2m_page(struct domain *d) { struct page_info *pg; hap_lock(d); pg = hap_alloc(d); #if CONFIG_PAGING_LEVELS == 3 /* Under PAE mode, top-level P2M table should be allocated below 4GB space * because the size of h_cr3 is only 32-bit. We use alloc_domheap_pages to * force this requirement, and exchange the guaranteed 32-bit-clean * page for the one we just hap_alloc()ed. */ if ( d->arch.paging.hap.p2m_pages == 0 && mfn_x(page_to_mfn(pg)) >= (1UL << (32 - PAGE_SHIFT)) ) { free_domheap_page(pg); pg = alloc_domheap_pages(NULL, 0, MEMF_bits(32)); if ( likely(pg != NULL) ) { void *p = hap_map_domain_page(page_to_mfn(pg)); clear_page(p); hap_unmap_domain_page(p); } } #endif if ( likely(pg != NULL) ) { d->arch.paging.hap.total_pages--; d->arch.paging.hap.p2m_pages++; page_set_owner(pg, d); pg->count_info = 1; } hap_unlock(d); return pg; } void hap_free_p2m_page(struct domain *d, struct page_info *pg) { hap_lock(d); ASSERT(page_get_owner(pg) == d); /* Should have just the one ref we gave it in alloc_p2m_page() */ if ( (pg->count_info & PGC_count_mask) != 1 ) HAP_ERROR("Odd p2m page count c=%#x t=%"PRtype_info"\n", pg->count_info, pg->u.inuse.type_info); pg->count_info = 0; /* Free should not decrement domain's total allocation, since * these pages were allocated without an owner. */ page_set_owner(pg, NULL); free_domheap_page(pg); d->arch.paging.hap.p2m_pages--; ASSERT(d->arch.paging.hap.p2m_pages >= 0); hap_unlock(d); } /* Return the size of the pool, rounded up to the nearest MB */ static unsigned int hap_get_allocation(struct domain *d) { unsigned int pg = d->arch.paging.hap.total_pages; return ((pg >> (20 - PAGE_SHIFT)) + ((pg & ((1 << (20 - PAGE_SHIFT)) - 1)) ? 1 : 0)); } /* Set the pool of pages to the required number of pages. * Returns 0 for success, non-zero for failure. */ static unsigned int hap_set_allocation(struct domain *d, unsigned int pages, int *preempted) { struct page_info *pg; ASSERT(hap_locked_by_me(d)); while ( d->arch.paging.hap.total_pages != pages ) { if ( d->arch.paging.hap.total_pages < pages ) { /* Need to allocate more memory from domheap */ pg = alloc_domheap_page(NULL); if ( pg == NULL ) { HAP_PRINTK("failed to allocate hap pages.\n"); return -ENOMEM; } d->arch.paging.hap.free_pages++; d->arch.paging.hap.total_pages++; list_add_tail(&pg->list, &d->arch.paging.hap.freelist); } else if ( d->arch.paging.hap.total_pages > pages ) { /* Need to return memory to domheap */ ASSERT(!list_empty(&d->arch.paging.hap.freelist)); pg = list_entry(d->arch.paging.hap.freelist.next, struct page_info, list); list_del(&pg->list); d->arch.paging.hap.free_pages--; d->arch.paging.hap.total_pages--; pg->count_info = 0; free_domheap_page(pg); } /* Check to see if we need to yield and try again */ if ( preempted && hypercall_preempt_check() ) { *preempted = 1; return 0; } } return 0; } #if CONFIG_PAGING_LEVELS == 4 static void hap_install_xen_entries_in_l4(struct vcpu *v, mfn_t l4mfn) { struct domain *d = v->domain; l4_pgentry_t *l4e; l4e = hap_map_domain_page(l4mfn); ASSERT(l4e != NULL); /* Copy the common Xen mappings from the idle domain */ memcpy(&l4e[ROOT_PAGETABLE_FIRST_XEN_SLOT], &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT], ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t)); /* Install the per-domain mappings for this domain */ l4e[l4_table_offset(PERDOMAIN_VIRT_START)] = l4e_from_pfn(mfn_x(page_to_mfn(virt_to_page(d->arch.mm_perdomain_l3))), __PAGE_HYPERVISOR); /* Install a linear mapping */ l4e[l4_table_offset(LINEAR_PT_VIRT_START)] = l4e_from_pfn(mfn_x(l4mfn), __PAGE_HYPERVISOR); /* Install the domain-specific P2M table */ l4e[l4_table_offset(RO_MPT_VIRT_START)] = l4e_from_pfn(mfn_x(pagetable_get_mfn(d->arch.phys_table)), __PAGE_HYPERVISOR); hap_unmap_domain_page(l4e); } #endif /* CONFIG_PAGING_LEVELS == 4 */ #if CONFIG_PAGING_LEVELS == 3 static void hap_install_xen_entries_in_l2h(struct vcpu *v, mfn_t l2hmfn) { struct domain *d = v->domain; l2_pgentry_t *l2e; l3_pgentry_t *p2m; int i; l2e = hap_map_domain_page(l2hmfn); ASSERT(l2e != NULL); /* Copy the common Xen mappings from the idle domain */ memcpy(&l2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)], &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT], L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t)); /* Install the per-domain mappings for this domain */ for ( i = 0; i < PDPT_L2_ENTRIES; i++ ) l2e[l2_table_offset(PERDOMAIN_VIRT_START) + i] = l2e_from_pfn( mfn_x(page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i)), __PAGE_HYPERVISOR); /* No linear mapping; will be set up by monitor-table contructor. */ for ( i = 0; i < 4; i++ ) l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] = l2e_empty(); /* Install the domain-specific p2m table */ ASSERT(pagetable_get_pfn(d->arch.phys_table) != 0); p2m = hap_map_domain_page(pagetable_get_mfn(d->arch.phys_table)); for ( i = 0; i < MACHPHYS_MBYTES>>1; i++ ) { l2e[l2_table_offset(RO_MPT_VIRT_START) + i] = (l3e_get_flags(p2m[i]) & _PAGE_PRESENT) ? l2e_from_pfn(mfn_x(_mfn(l3e_get_pfn(p2m[i]))), __PAGE_HYPERVISOR) : l2e_empty(); } hap_unmap_domain_page(p2m); hap_unmap_domain_page(l2e); } #endif #if CONFIG_PAGING_LEVELS == 2 static void hap_install_xen_entries_in_l2(struct vcpu *v, mfn_t l2mfn) { struct domain *d = v->domain; l2_pgentry_t *l2e; int i; l2e = hap_map_domain_page(l2mfn); ASSERT(l2e != NULL); /* Copy the common Xen mappings from the idle domain */ memcpy(&l2e[L2_PAGETABLE_FIRST_XEN_SLOT], &idle_pg_table[L2_PAGETABLE_FIRST_XEN_SLOT], L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t)); /* Install the per-domain mappings for this domain */ for ( i = 0; i < PDPT_L2_ENTRIES; i++ ) l2e[l2_table_offset(PERDOMAIN_VIRT_START) + i] = l2e_from_pfn( mfn_x(page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i)), __PAGE_HYPERVISOR); /* Install the linear mapping */ l2e[l2_table_offset(LINEAR_PT_VIRT_START)] = l2e_from_pfn(mfn_x(l2mfn), __PAGE_HYPERVISOR); /* Install the domain-specific P2M table */ l2e[l2_table_offset(RO_MPT_VIRT_START)] = l2e_from_pfn(mfn_x(pagetable_get_mfn(d->arch.phys_table)), __PAGE_HYPERVISOR); hap_unmap_domain_page(l2e); } #endif static mfn_t hap_make_monitor_table(struct vcpu *v) { struct domain *d = v->domain; struct page_info *pg; ASSERT(pagetable_get_pfn(v->arch.monitor_table) == 0); #if CONFIG_PAGING_LEVELS == 4 { mfn_t m4mfn; if ( (pg = hap_alloc(d)) == NULL ) goto oom; m4mfn = page_to_mfn(pg); hap_install_xen_entries_in_l4(v, m4mfn); return m4mfn; } #elif CONFIG_PAGING_LEVELS == 3 { mfn_t m3mfn, m2mfn; l3_pgentry_t *l3e; l2_pgentry_t *l2e; int i; if ( (pg = hap_alloc(d)) == NULL ) goto oom; m3mfn = page_to_mfn(pg); /* Install a monitor l2 table in slot 3 of the l3 table. * This is used for all Xen entries, including linear maps */ if ( (pg = hap_alloc(d)) == NULL ) goto oom; m2mfn = page_to_mfn(pg); l3e = hap_map_domain_page(m3mfn); l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT); hap_install_xen_entries_in_l2h(v, m2mfn); /* Install the monitor's own linear map */ l2e = hap_map_domain_page(m2mfn); for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ ) l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] = (l3e_get_flags(l3e[i]) & _PAGE_PRESENT) ? l2e_from_pfn(l3e_get_pfn(l3e[i]), __PAGE_HYPERVISOR) : l2e_empty(); hap_unmap_domain_page(l2e); hap_unmap_domain_page(l3e); HAP_PRINTK("new monitor table: %#lx\n", mfn_x(m3mfn)); return m3mfn; } #else { mfn_t m2mfn; if ( (pg = hap_alloc(d)) == NULL ) goto oom; m2mfn = page_to_mfn(pg);; hap_install_xen_entries_in_l2(v, m2mfn); return m2mfn; } #endif oom: HAP_ERROR("out of memory building monitor pagetable\n"); domain_crash(d); return _mfn(INVALID_MFN); } static void hap_destroy_monitor_table(struct vcpu* v, mfn_t mmfn) { struct domain *d = v->domain; #if CONFIG_PAGING_LEVELS == 3 /* Need to destroy the l2 monitor page in slot 4 too */ { l3_pgentry_t *l3e = hap_map_domain_page(mmfn); ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT); hap_free(d, _mfn(l3e_get_pfn(l3e[3]))); hap_unmap_domain_page(l3e); } #endif /* Put the memory back in the pool */ hap_free(d, mmfn); } /************************************************/ /* HAP DOMAIN LEVEL FUNCTIONS */ /************************************************/ void hap_domain_init(struct domain *d) { hap_lock_init(d); INIT_LIST_HEAD(&d->arch.paging.hap.freelist); /* This domain will use HAP for log-dirty mode */ paging_log_dirty_init(d, hap_enable_log_dirty, hap_disable_log_dirty, hap_clean_dirty_bitmap); } /* return 0 for success, -errno for failure */ int hap_enable(struct domain *d, u32 mode) { unsigned int old_pages; int rv = 0; domain_pause(d); /* error check */ if ( (d == current->domain) ) { rv = -EINVAL; goto out; } old_pages = d->arch.paging.hap.total_pages; if ( old_pages == 0 ) { unsigned int r; hap_lock(d); r = hap_set_allocation(d, 256, NULL); hap_unlock(d); if ( r != 0 ) { hap_set_allocation(d, 0, NULL); rv = -ENOMEM; goto out; } } /* allocate P2m table */ if ( mode & PG_translate ) { rv = p2m_alloc_table(d, hap_alloc_p2m_page, hap_free_p2m_page); if ( rv != 0 ) goto out; } d->arch.paging.mode = mode | PG_HAP_enable; out: domain_unpause(d); return rv; } void hap_final_teardown(struct domain *d) { if ( d->arch.paging.hap.total_pages != 0 ) hap_teardown(d); p2m_teardown(d); ASSERT(d->arch.paging.hap.p2m_pages == 0); } void hap_teardown(struct domain *d) { struct vcpu *v; mfn_t mfn; ASSERT(d->is_dying); ASSERT(d != current->domain); if ( !hap_locked_by_me(d) ) hap_lock(d); /* Keep various asserts happy */ if ( paging_mode_enabled(d) ) { /* release the monitor table held by each vcpu */ for_each_vcpu ( d, v ) { if ( v->arch.paging.mode && paging_mode_external(d) ) { mfn = pagetable_get_mfn(v->arch.monitor_table); if ( mfn_valid(mfn) && (mfn_x(mfn) != 0) ) hap_destroy_monitor_table(v, mfn); v->arch.monitor_table = pagetable_null(); } } } if ( d->arch.paging.hap.total_pages != 0 ) { HAP_PRINTK("teardown of domain %u starts." " pages total = %u, free = %u, p2m=%u\n", d->domain_id, d->arch.paging.hap.total_pages, d->arch.paging.hap.free_pages, d->arch.paging.hap.p2m_pages); hap_set_allocation(d, 0, NULL); HAP_PRINTK("teardown done." " pages total = %u, free = %u, p2m=%u\n", d->arch.paging.hap.total_pages, d->arch.paging.hap.free_pages, d->arch.paging.hap.p2m_pages); ASSERT(d->arch.paging.hap.total_pages == 0); } d->arch.paging.mode &= ~PG_log_dirty; hap_unlock(d); } int hap_domctl(struct domain *d, xen_domctl_shadow_op_t *sc, XEN_GUEST_HANDLE(void) u_domctl) { int rc, preempted = 0; switch ( sc->op ) { case XEN_DOMCTL_SHADOW_OP_SET_ALLOCATION: hap_lock(d); rc = hap_set_allocation(d, sc->mb << (20 - PAGE_SHIFT), &preempted); hap_unlock(d); if ( preempted ) /* Not finished. Set up to re-run the call. */ rc = hypercall_create_continuation(__HYPERVISOR_domctl, "h", u_domctl); else /* Finished. Return the new allocation */ sc->mb = hap_get_allocation(d); return rc; case XEN_DOMCTL_SHADOW_OP_GET_ALLOCATION: sc->mb = hap_get_allocation(d); return 0; default: HAP_ERROR("Bad hap domctl op %u\n", sc->op); return -EINVAL; } } void hap_vcpu_init(struct vcpu *v) { v->arch.paging.mode = &hap_paging_real_mode; } /************************************************/ /* HAP PAGING MODE FUNCTIONS */ /************************************************/ /* * HAP guests can handle page faults (in the guest page tables) without * needing any action from Xen, so we should not be intercepting them. */ static int hap_page_fault(struct vcpu *v, unsigned long va, struct cpu_user_regs *regs) { HAP_ERROR("Intercepted a guest #PF (%u:%u) with HAP enabled.\n", v->domain->domain_id, v->vcpu_id); domain_crash(v->domain); return 0; } /* * HAP guests can handle invlpg without needing any action from Xen, so * should not be intercepting it. */ static int hap_invlpg(struct vcpu *v, unsigned long va) { HAP_ERROR("Intercepted a guest INVLPG (%u:%u) with HAP enabled.\n", v->domain->domain_id, v->vcpu_id); domain_crash(v->domain); return 0; } /* * HAP guests do not need to take any action on CR3 writes (they are still * intercepted, so that Xen's copy of the guest's CR3 can be kept in sync.) */ static void hap_update_cr3(struct vcpu *v, int do_locking) { } static void hap_update_paging_modes(struct vcpu *v) { struct domain *d; d = v->domain; hap_lock(d); /* update guest paging mode. Note that we rely on hvm functions to detect * guest's paging mode. So, make sure the shadow registers (CR0, CR4, EFER) * reflect guest's status correctly. */ if ( hvm_paging_enabled(v) ) { if ( hvm_long_mode_enabled(v) ) v->arch.paging.mode = &hap_paging_long_mode; else if ( hvm_pae_enabled(v) ) v->arch.paging.mode = &hap_paging_pae_mode; else v->arch.paging.mode = &hap_paging_protected_mode; } else { v->arch.paging.mode = &hap_paging_real_mode; } v->arch.paging.translate_enabled = !!hvm_paging_enabled(v); if ( pagetable_is_null(v->arch.monitor_table) ) { mfn_t mmfn = hap_make_monitor_table(v); v->arch.monitor_table = pagetable_from_mfn(mmfn); make_cr3(v, mfn_x(mmfn)); } hap_unlock(d); } #if CONFIG_PAGING_LEVELS == 3 static void p2m_install_entry_in_monitors(struct domain *d, l3_pgentry_t *l3e) /* Special case, only used for PAE hosts: update the mapping of the p2m * table. This is trivial in other paging modes (one top-level entry * points to the top-level p2m, no maintenance needed), but PAE makes * life difficult by needing a copy of the p2m table in eight l2h slots * in the monitor table. This function makes fresh copies when a p2m * l3e changes. */ { l2_pgentry_t *ml2e; struct vcpu *v; unsigned int index; index = ((unsigned long)l3e & ~PAGE_MASK) / sizeof(l3_pgentry_t); ASSERT(index < MACHPHYS_MBYTES>>1); for_each_vcpu ( d, v ) { if ( pagetable_get_pfn(v->arch.monitor_table) == 0 ) continue; ASSERT(paging_mode_external(v->domain)); if ( v == current ) /* OK to use linear map of monitor_table */ ml2e = __linear_l2_table + l2_linear_offset(RO_MPT_VIRT_START); else { l3_pgentry_t *ml3e; ml3e = hap_map_domain_page( pagetable_get_mfn(v->arch.monitor_table)); ASSERT(l3e_get_flags(ml3e[3]) & _PAGE_PRESENT); ml2e = hap_map_domain_page(_mfn(l3e_get_pfn(ml3e[3]))); ml2e += l2_table_offset(RO_MPT_VIRT_START); hap_unmap_domain_page(ml3e); } ml2e[index] = l2e_from_pfn(l3e_get_pfn(*l3e), __PAGE_HYPERVISOR); if ( v != current ) hap_unmap_domain_page(ml2e); } } #endif static void hap_write_p2m_entry(struct vcpu *v, unsigned long gfn, l1_pgentry_t *p, mfn_t table_mfn, l1_pgentry_t new, unsigned int level) { hap_lock(v->domain); safe_write_pte(p, new); #if CONFIG_PAGING_LEVELS == 3 /* install P2M in monitor table for PAE Xen */ if ( level == 3 ) /* We have written to the p2m l3: need to sync the per-vcpu * copies of it in the monitor tables */ p2m_install_entry_in_monitors(v->domain, (l3_pgentry_t *)p); #endif hap_unlock(v->domain); } static unsigned long hap_gva_to_gfn_real_mode( struct vcpu *v, unsigned long gva) { return ((paddr_t)gva >> PAGE_SHIFT); } /* Entry points into this mode of the hap code. */ struct paging_mode hap_paging_real_mode = { .page_fault = hap_page_fault, .invlpg = hap_invlpg, .gva_to_gfn = hap_gva_to_gfn_real_mode, .update_cr3 = hap_update_cr3, .update_paging_modes = hap_update_paging_modes, .write_p2m_entry = hap_write_p2m_entry, .guest_levels = 1 }; struct paging_mode hap_paging_protected_mode = { .page_fault = hap_page_fault, .invlpg = hap_invlpg, .gva_to_gfn = hap_gva_to_gfn_2level, .update_cr3 = hap_update_cr3, .update_paging_modes = hap_update_paging_modes, .write_p2m_entry = hap_write_p2m_entry, .guest_levels = 2 }; struct paging_mode hap_paging_pae_mode = { .page_fault = hap_page_fault, .invlpg = hap_invlpg, .gva_to_gfn = hap_gva_to_gfn_3level, .update_cr3 = hap_update_cr3, .update_paging_modes = hap_update_paging_modes, .write_p2m_entry = hap_write_p2m_entry, .guest_levels = 3 }; struct paging_mode hap_paging_long_mode = { .page_fault = hap_page_fault, .invlpg = hap_invlpg, .gva_to_gfn = hap_gva_to_gfn_4level, .update_cr3 = hap_update_cr3, .update_paging_modes = hap_update_paging_modes, .write_p2m_entry = hap_write_p2m_entry, .guest_levels = 4 }; /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * indent-tabs-mode: nil * End: */