debuggers.hg

view tools/libxc/xc_hvm_build.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 52fd5baad593
children 3ffdb094c2c0 6868816898bd
line source
1 /******************************************************************************
2 * xc_hvm_build.c
3 */
5 #include <stddef.h>
6 #include <inttypes.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <zlib.h>
11 #include "xg_private.h"
12 #include "xc_private.h"
14 #include <xen/foreign/x86_32.h>
15 #include <xen/foreign/x86_64.h>
16 #include <xen/hvm/hvm_info_table.h>
17 #include <xen/hvm/params.h>
18 #include <xen/hvm/e820.h>
20 #include <xen/libelf/libelf.h>
22 #define SUPERPAGE_PFN_SHIFT 9
23 #define SUPERPAGE_NR_PFNS (1UL << SUPERPAGE_PFN_SHIFT)
25 #define SPECIALPAGE_BUFIOREQ 0
26 #define SPECIALPAGE_XENSTORE 1
27 #define SPECIALPAGE_IOREQ 2
28 #define SPECIALPAGE_IDENT_PT 3
29 #define SPECIALPAGE_SHINFO 4
30 #define NR_SPECIAL_PAGES 5
31 #define special_pfn(x) (0xff000u - NR_SPECIAL_PAGES + (x))
33 static void build_hvm_info(void *hvm_info_page, uint64_t mem_size)
34 {
35 struct hvm_info_table *hvm_info = (struct hvm_info_table *)
36 (((unsigned char *)hvm_info_page) + HVM_INFO_OFFSET);
37 uint64_t lowmem_end = mem_size, highmem_end = 0;
38 uint8_t sum;
39 int i;
41 if ( lowmem_end > HVM_BELOW_4G_RAM_END )
42 {
43 highmem_end = lowmem_end + (1ull<<32) - HVM_BELOW_4G_RAM_END;
44 lowmem_end = HVM_BELOW_4G_RAM_END;
45 }
47 memset(hvm_info_page, 0, PAGE_SIZE);
49 /* Fill in the header. */
50 strncpy(hvm_info->signature, "HVM INFO", 8);
51 hvm_info->length = sizeof(struct hvm_info_table);
53 /* Sensible defaults: these can be overridden by the caller. */
54 hvm_info->acpi_enabled = 1;
55 hvm_info->apic_mode = 1;
56 hvm_info->nr_vcpus = 1;
58 /* Memory parameters. */
59 hvm_info->low_mem_pgend = lowmem_end >> PAGE_SHIFT;
60 hvm_info->high_mem_pgend = highmem_end >> PAGE_SHIFT;
61 hvm_info->reserved_mem_pgstart = special_pfn(0);
63 /* Finish with the checksum. */
64 for ( i = 0, sum = 0; i < hvm_info->length; i++ )
65 sum += ((uint8_t *)hvm_info)[i];
66 hvm_info->checksum = -sum;
67 }
69 static int loadelfimage(
70 struct elf_binary *elf, int xch, uint32_t dom, unsigned long *parray)
71 {
72 privcmd_mmap_entry_t *entries = NULL;
73 size_t pages = (elf->pend - elf->pstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
74 int i, rc = -1;
76 /* Map address space for initial elf image. */
77 entries = calloc(pages, sizeof(privcmd_mmap_entry_t));
78 if ( entries == NULL )
79 goto err;
81 for ( i = 0; i < pages; i++ )
82 entries[i].mfn = parray[(elf->pstart >> PAGE_SHIFT) + i];
84 elf->dest = xc_map_foreign_ranges(
85 xch, dom, pages << PAGE_SHIFT, PROT_READ | PROT_WRITE, 1 << PAGE_SHIFT,
86 entries, pages);
87 if ( elf->dest == NULL )
88 goto err;
90 /* Load the initial elf image. */
91 elf_load_binary(elf);
92 rc = 0;
94 munmap(elf->dest, pages << PAGE_SHIFT);
95 elf->dest = NULL;
97 err:
98 free(entries);
100 return rc;
101 }
103 static int setup_guest(int xc_handle,
104 uint32_t dom, int memsize, int target,
105 char *image, unsigned long image_size)
106 {
107 xen_pfn_t *page_array = NULL;
108 unsigned long i, nr_pages = (unsigned long)memsize << (20 - PAGE_SHIFT);
109 unsigned long target_pages = (unsigned long)target << (20 - PAGE_SHIFT);
110 unsigned long pod_pages = 0;
111 unsigned long entry_eip, cur_pages;
112 struct xen_add_to_physmap xatp;
113 struct shared_info *shared_info;
114 void *hvm_info_page;
115 uint32_t *ident_pt;
116 struct elf_binary elf;
117 uint64_t v_start, v_end;
118 int rc;
119 xen_capabilities_info_t caps;
120 int pod_mode = 0;
123 /* An HVM guest must be initialised with at least 2MB memory. */
124 if ( memsize < 2 || target < 2 )
125 goto error_out;
127 if ( memsize > target )
128 pod_mode = 1;
130 if ( elf_init(&elf, image, image_size) != 0 )
131 goto error_out;
132 elf_parse_binary(&elf);
133 v_start = 0;
134 v_end = (unsigned long long)memsize << 20;
136 if ( xc_version(xc_handle, XENVER_capabilities, &caps) != 0 )
137 {
138 PERROR("Could not get Xen capabilities\n");
139 goto error_out;
140 }
142 if ( (elf.pstart & (PAGE_SIZE - 1)) != 0 )
143 {
144 PERROR("Guest OS must load to a page boundary.\n");
145 goto error_out;
146 }
148 IPRINTF("VIRTUAL MEMORY ARRANGEMENT:\n"
149 " Loader: %016"PRIx64"->%016"PRIx64"\n"
150 " TOTAL: %016"PRIx64"->%016"PRIx64"\n"
151 " ENTRY ADDRESS: %016"PRIx64"\n",
152 elf.pstart, elf.pend,
153 v_start, v_end,
154 elf_uval(&elf, elf.ehdr, e_entry));
156 if ( (page_array = malloc(nr_pages * sizeof(xen_pfn_t))) == NULL )
157 {
158 PERROR("Could not allocate memory.\n");
159 goto error_out;
160 }
162 for ( i = 0; i < nr_pages; i++ )
163 page_array[i] = i;
164 for ( i = HVM_BELOW_4G_RAM_END >> PAGE_SHIFT; i < nr_pages; i++ )
165 page_array[i] += HVM_BELOW_4G_MMIO_LENGTH >> PAGE_SHIFT;
167 /*
168 * Allocate memory for HVM guest, skipping VGA hole 0xA0000-0xC0000.
169 * We allocate pages in batches of no more than 8MB to ensure that
170 * we can be preempted and hence dom0 remains responsive.
171 */
172 rc = xc_domain_memory_populate_physmap(
173 xc_handle, dom, 0xa0, 0, 0, &page_array[0x00]);
174 cur_pages = 0xc0;
175 while ( (rc == 0) && (nr_pages > cur_pages) )
176 {
177 /* Clip count to maximum 8MB extent. */
178 unsigned long count = nr_pages - cur_pages;
179 if ( count > 2048 )
180 count = 2048;
182 /* Clip partial superpage extents to superpage boundaries. */
183 if ( ((cur_pages & (SUPERPAGE_NR_PFNS-1)) != 0) &&
184 (count > (-cur_pages & (SUPERPAGE_NR_PFNS-1))) )
185 count = -cur_pages & (SUPERPAGE_NR_PFNS-1); /* clip s.p. tail */
186 else if ( ((count & (SUPERPAGE_NR_PFNS-1)) != 0) &&
187 (count > SUPERPAGE_NR_PFNS) )
188 count &= ~(SUPERPAGE_NR_PFNS - 1); /* clip non-s.p. tail */
190 /* Attempt to allocate superpage extents. */
191 if ( ((count | cur_pages) & (SUPERPAGE_NR_PFNS - 1)) == 0 )
192 {
193 long done;
194 xen_pfn_t sp_extents[count >> SUPERPAGE_PFN_SHIFT];
195 struct xen_memory_reservation sp_req = {
196 .nr_extents = count >> SUPERPAGE_PFN_SHIFT,
197 .extent_order = SUPERPAGE_PFN_SHIFT,
198 .domid = dom
199 };
201 if ( pod_mode )
202 sp_req.mem_flags = XENMEMF_populate_on_demand;
204 set_xen_guest_handle(sp_req.extent_start, sp_extents);
205 for ( i = 0; i < sp_req.nr_extents; i++ )
206 sp_extents[i] = page_array[cur_pages+(i<<SUPERPAGE_PFN_SHIFT)];
207 done = xc_memory_op(xc_handle, XENMEM_populate_physmap, &sp_req);
208 if ( done > 0 )
209 {
210 done <<= SUPERPAGE_PFN_SHIFT;
211 if ( pod_mode && target_pages > cur_pages )
212 {
213 int d = target_pages - cur_pages;
214 pod_pages += ( done < d ) ? done : d;
215 }
216 cur_pages += done;
217 count -= done;
218 }
219 }
221 /* Fall back to 4kB extents. */
222 if ( count != 0 )
223 {
224 rc = xc_domain_memory_populate_physmap(
225 xc_handle, dom, count, 0, 0, &page_array[cur_pages]);
226 cur_pages += count;
227 if ( pod_mode )
228 pod_pages -= count;
229 }
230 }
232 if ( pod_mode )
233 rc = xc_domain_memory_set_pod_target(xc_handle,
234 dom,
235 pod_pages,
236 NULL, NULL, NULL);
238 if ( rc != 0 )
239 {
240 PERROR("Could not allocate memory for HVM guest.\n");
241 goto error_out;
242 }
244 if ( loadelfimage(&elf, xc_handle, dom, page_array) != 0 )
245 goto error_out;
247 if ( (hvm_info_page = xc_map_foreign_range(
248 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
249 HVM_INFO_PFN)) == NULL )
250 goto error_out;
251 build_hvm_info(hvm_info_page, v_end);
252 munmap(hvm_info_page, PAGE_SIZE);
254 /* Map and initialise shared_info page. */
255 xatp.domid = dom;
256 xatp.space = XENMAPSPACE_shared_info;
257 xatp.idx = 0;
258 xatp.gpfn = special_pfn(SPECIALPAGE_SHINFO);
259 if ( (xc_memory_op(xc_handle, XENMEM_add_to_physmap, &xatp) != 0) ||
260 ((shared_info = xc_map_foreign_range(
261 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
262 special_pfn(SPECIALPAGE_SHINFO))) == NULL) )
263 goto error_out;
264 memset(shared_info, 0, PAGE_SIZE);
265 /* NB. evtchn_upcall_mask is unused: leave as zero. */
266 memset(&shared_info->evtchn_mask[0], 0xff,
267 sizeof(shared_info->evtchn_mask));
268 munmap(shared_info, PAGE_SIZE);
270 /* Allocate and clear special pages. */
271 for ( i = 0; i < NR_SPECIAL_PAGES; i++ )
272 {
273 xen_pfn_t pfn = special_pfn(i);
274 if ( i == SPECIALPAGE_SHINFO )
275 continue;
276 rc = xc_domain_memory_populate_physmap(xc_handle, dom, 1, 0, 0, &pfn);
277 if ( rc != 0 )
278 {
279 PERROR("Could not allocate %d'th special page.\n", i);
280 goto error_out;
281 }
282 if ( xc_clear_domain_page(xc_handle, dom, special_pfn(i)) )
283 goto error_out;
284 }
286 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_STORE_PFN,
287 special_pfn(SPECIALPAGE_XENSTORE));
288 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_BUFIOREQ_PFN,
289 special_pfn(SPECIALPAGE_BUFIOREQ));
290 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_IOREQ_PFN,
291 special_pfn(SPECIALPAGE_IOREQ));
293 /*
294 * Identity-map page table is required for running with CR0.PG=0 when
295 * using Intel EPT. Create a 32-bit non-PAE page directory of superpages.
296 */
297 if ( (ident_pt = xc_map_foreign_range(
298 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
299 special_pfn(SPECIALPAGE_IDENT_PT))) == NULL )
300 goto error_out;
301 for ( i = 0; i < PAGE_SIZE / sizeof(*ident_pt); i++ )
302 ident_pt[i] = ((i << 22) | _PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
303 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
304 munmap(ident_pt, PAGE_SIZE);
305 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_IDENT_PT,
306 special_pfn(SPECIALPAGE_IDENT_PT) << PAGE_SHIFT);
308 /* Insert JMP <rel32> instruction at address 0x0 to reach entry point. */
309 entry_eip = elf_uval(&elf, elf.ehdr, e_entry);
310 if ( entry_eip != 0 )
311 {
312 char *page0 = xc_map_foreign_range(
313 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE, 0);
314 if ( page0 == NULL )
315 goto error_out;
316 page0[0] = 0xe9;
317 *(uint32_t *)&page0[1] = entry_eip - 5;
318 munmap(page0, PAGE_SIZE);
319 }
321 free(page_array);
322 return 0;
324 error_out:
325 free(page_array);
326 return -1;
327 }
329 static int xc_hvm_build_internal(int xc_handle,
330 uint32_t domid,
331 int memsize,
332 int target,
333 char *image,
334 unsigned long image_size)
335 {
336 if ( (image == NULL) || (image_size == 0) )
337 {
338 ERROR("Image required");
339 return -1;
340 }
342 return setup_guest(xc_handle, domid, memsize, target, image, image_size);
343 }
345 /* xc_hvm_build:
346 * Create a domain for a virtualized Linux, using files/filenames.
347 */
348 int xc_hvm_build(int xc_handle,
349 uint32_t domid,
350 int memsize,
351 const char *image_name)
352 {
353 char *image;
354 int sts;
355 unsigned long image_size;
357 if ( (image_name == NULL) ||
358 ((image = xc_read_image(image_name, &image_size)) == NULL) )
359 return -1;
361 sts = xc_hvm_build_internal(xc_handle, domid, memsize, memsize, image, image_size);
363 free(image);
365 return sts;
366 }
368 /* xc_hvm_build_target_mem:
369 * Create a domain for a pre-ballooned virtualized Linux, using
370 * files/filenames. If target < memsize, domain is created with
371 * memsize pages marked populate-on-demand, and with a PoD cache size
372 * of target. If target == memsize, pages are populated normally.
373 */
374 int xc_hvm_build_target_mem(int xc_handle,
375 uint32_t domid,
376 int memsize,
377 int target,
378 const char *image_name)
379 {
380 char *image;
381 int sts;
382 unsigned long image_size;
384 if ( (image_name == NULL) ||
385 ((image = xc_read_image(image_name, &image_size)) == NULL) )
386 return -1;
388 sts = xc_hvm_build_internal(xc_handle, domid, memsize, target, image, image_size);
390 free(image);
392 return sts;
393 }
395 /* xc_hvm_build_mem:
396 * Create a domain for a virtualized Linux, using memory buffers.
397 */
398 int xc_hvm_build_mem(int xc_handle,
399 uint32_t domid,
400 int memsize,
401 const char *image_buffer,
402 unsigned long image_size)
403 {
404 int sts;
405 unsigned long img_len;
406 char *img;
408 /* Validate that there is a kernel buffer */
410 if ( (image_buffer == NULL) || (image_size == 0) )
411 {
412 ERROR("kernel image buffer not present");
413 return -1;
414 }
416 img = xc_inflate_buffer(image_buffer, image_size, &img_len);
417 if ( img == NULL )
418 {
419 ERROR("unable to inflate ram disk buffer");
420 return -1;
421 }
423 sts = xc_hvm_build_internal(xc_handle, domid, memsize, memsize,
424 img, img_len);
426 /* xc_inflate_buffer may return the original buffer pointer (for
427 for already inflated buffers), so exercise some care in freeing */
429 if ( (img != NULL) && (img != image_buffer) )
430 free(img);
432 return sts;
433 }
435 /*
436 * Local variables:
437 * mode: C
438 * c-set-style: "BSD"
439 * c-basic-offset: 4
440 * tab-width: 4
441 * indent-tabs-mode: nil
442 * End:
443 */