ia64/xen-unstable

view tools/libxc/xc_private.c @ 4895:24dfd18ea63e

bitkeeper revision 1.1159.258.120 (42848bfe8kMyWWcBA64rq7h7l7AyoA)

Shadow code bug fix (found by Ian) that was breaking refcounts, and subsequently
causing migration problems.
author mafetter@fleming.research
date Fri May 13 11:14:06 2005 +0000 (2005-05-13)
parents 1e99cd1cb3a3
children 2419f5c72fe5 6096356005ba 657d51f27c5b
line source
1 /******************************************************************************
2 * xc_private.c
3 *
4 * Helper functions for the rest of the library.
5 */
7 #include "xc_private.h"
9 void *xc_map_foreign_batch(int xc_handle, u32 dom, int prot,
10 unsigned long *arr, int num )
11 {
12 privcmd_mmapbatch_t ioctlx;
13 void *addr;
14 addr = mmap(NULL, num*PAGE_SIZE, prot, MAP_SHARED, xc_handle, 0);
15 if ( addr != NULL )
16 {
17 ioctlx.num=num;
18 ioctlx.dom=dom;
19 ioctlx.addr=(unsigned long)addr;
20 ioctlx.arr=arr;
21 if ( ioctl( xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx ) < 0 )
22 {
23 perror("XXXXXXXX");
24 munmap(addr, num*PAGE_SIZE);
25 return 0;
26 }
27 }
28 return addr;
30 }
32 /*******************/
34 void *xc_map_foreign_range(int xc_handle, u32 dom,
35 int size, int prot,
36 unsigned long mfn )
37 {
38 privcmd_mmap_t ioctlx;
39 privcmd_mmap_entry_t entry;
40 void *addr;
41 addr = mmap(NULL, size, prot, MAP_SHARED, xc_handle, 0);
42 if ( addr != NULL )
43 {
44 ioctlx.num=1;
45 ioctlx.dom=dom;
46 ioctlx.entry=&entry;
47 entry.va=(unsigned long) addr;
48 entry.mfn=mfn;
49 entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
50 if ( ioctl( xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx ) < 0 )
51 {
52 munmap(addr, size);
53 return 0;
54 }
55 }
56 return addr;
57 }
59 /*******************/
61 /* NB: arr must be mlock'ed */
62 int get_pfn_type_batch(int xc_handle,
63 u32 dom, int num, unsigned long *arr)
64 {
65 dom0_op_t op;
66 op.cmd = DOM0_GETPAGEFRAMEINFO2;
67 op.u.getpageframeinfo2.domain = (domid_t)dom;
68 op.u.getpageframeinfo2.num = num;
69 op.u.getpageframeinfo2.array = arr;
70 return do_dom0_op(xc_handle, &op);
71 }
73 #define GETPFN_ERR (~0U)
74 unsigned int get_pfn_type(int xc_handle,
75 unsigned long mfn,
76 u32 dom)
77 {
78 dom0_op_t op;
79 op.cmd = DOM0_GETPAGEFRAMEINFO;
80 op.u.getpageframeinfo.pfn = mfn;
81 op.u.getpageframeinfo.domain = (domid_t)dom;
82 if ( do_dom0_op(xc_handle, &op) < 0 )
83 {
84 PERROR("Unexpected failure when getting page frame info!");
85 return GETPFN_ERR;
86 }
87 return op.u.getpageframeinfo.type;
88 }
92 /*******************/
94 #define FIRST_MMU_UPDATE 1
96 static int flush_mmu_updates(int xc_handle, mmu_t *mmu)
97 {
98 int err = 0;
99 privcmd_hypercall_t hypercall;
101 if ( mmu->idx == FIRST_MMU_UPDATE )
102 return 0;
104 mmu->updates[0].ptr = MMU_EXTENDED_COMMAND;
105 mmu->updates[0].val = MMUEXT_SET_FOREIGNDOM;
106 mmu->updates[0].val |= (unsigned long)mmu->subject << 16;
108 hypercall.op = __HYPERVISOR_mmu_update;
109 hypercall.arg[0] = (unsigned long)mmu->updates;
110 hypercall.arg[1] = (unsigned long)mmu->idx;
111 hypercall.arg[2] = 0;
113 if ( mlock(mmu->updates, sizeof(mmu->updates)) != 0 )
114 {
115 PERROR("Could not lock pagetable update array");
116 err = 1;
117 goto out;
118 }
120 if ( do_xen_hypercall(xc_handle, &hypercall) < 0 )
121 {
122 ERROR("Failure when submitting mmu updates");
123 err = 1;
124 }
126 mmu->idx = FIRST_MMU_UPDATE;
128 (void)munlock(mmu->updates, sizeof(mmu->updates));
130 out:
131 return err;
132 }
134 mmu_t *init_mmu_updates(int xc_handle, domid_t dom)
135 {
136 mmu_t *mmu = malloc(sizeof(mmu_t));
137 if ( mmu == NULL )
138 return mmu;
139 mmu->idx = FIRST_MMU_UPDATE;
140 mmu->subject = dom;
141 return mmu;
142 }
144 int add_mmu_update(int xc_handle, mmu_t *mmu,
145 unsigned long ptr, unsigned long val)
146 {
147 mmu->updates[mmu->idx].ptr = ptr;
148 mmu->updates[mmu->idx].val = val;
150 if ( ++mmu->idx == MAX_MMU_UPDATES )
151 return flush_mmu_updates(xc_handle, mmu);
153 return 0;
154 }
156 int finish_mmu_updates(int xc_handle, mmu_t *mmu)
157 {
158 return flush_mmu_updates(xc_handle, mmu);
159 }
162 long long xc_domain_get_cpu_usage( int xc_handle, domid_t domid )
163 {
164 dom0_op_t op;
166 op.cmd = DOM0_GETDOMAININFO;
167 op.u.getdomaininfo.domain = (domid_t)domid;
168 op.u.getdomaininfo.ctxt = NULL;
169 if ( (do_dom0_op(xc_handle, &op) < 0) ||
170 ((u16)op.u.getdomaininfo.domain != domid) )
171 {
172 PERROR("Could not get info on domain");
173 return -1;
174 }
175 return op.u.getdomaininfo.cpu_time;
176 }
179 /* This is shared between save and restore, and may generally be useful. */
180 unsigned long csum_page (void * page)
181 {
182 int i;
183 unsigned long *p = page;
184 unsigned long long sum=0;
186 for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
187 sum += p[i];
189 return sum ^ (sum>>32);
190 }
192 unsigned long xc_get_m2p_start_mfn ( int xc_handle )
193 {
194 unsigned long mfn;
196 if ( ioctl( xc_handle, IOCTL_PRIVCMD_GET_MACH2PHYS_START_MFN, &mfn ) < 0 )
197 {
198 perror("xc_get_m2p_start_mfn:");
199 return 0;
200 }
201 return mfn;
202 }