ia64/xen-unstable

view tools/libxc/xc_hvm_build.c @ 8740:3d7ea7972b39

Update patches for linux 2.6.15.

Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Thu Feb 02 17:16:00 2006 +0000 (2006-02-02)
parents f1b361b05bf3
children 0e7bdd973e17
line source
1 /******************************************************************************
2 * xc_hvm_build.c
3 */
5 #include <stddef.h>
6 #include "xg_private.h"
7 #define ELFSIZE 32
8 #include "xc_elf.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <zlib.h>
12 #include <xen/hvm/hvm_info_table.h>
13 #include <xen/hvm/ioreq.h>
15 #define HVM_LOADER_ENTR_ADDR 0x00100000
17 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
18 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
19 #ifdef __x86_64__
20 #define L3_PROT (_PAGE_PRESENT)
21 #endif
23 #define E820MAX 128
25 #define E820_RAM 1
26 #define E820_RESERVED 2
27 #define E820_ACPI 3
28 #define E820_NVS 4
29 #define E820_IO 16
30 #define E820_SHARED_PAGE 17
31 #define E820_XENSTORE 18
33 #define E820_MAP_PAGE 0x00090000
34 #define E820_MAP_NR_OFFSET 0x000001E8
35 #define E820_MAP_OFFSET 0x000002D0
37 struct e820entry {
38 uint64_t addr;
39 uint64_t size;
40 uint32_t type;
41 } __attribute__((packed));
43 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
44 #define round_pgdown(_p) ((_p)&PAGE_MASK)
46 static int
47 parseelfimage(
48 char *elfbase, unsigned long elfsize, struct domain_setup_info *dsi);
49 static int
50 loadelfimage(
51 char *elfbase, int xch, uint32_t dom, unsigned long *parray,
52 struct domain_setup_info *dsi);
54 static unsigned char build_e820map(void *e820_page, unsigned long mem_size)
55 {
56 struct e820entry *e820entry =
57 (struct e820entry *)(((unsigned char *)e820_page) + E820_MAP_OFFSET);
58 unsigned char nr_map = 0;
60 /* XXX: Doesn't work for > 4GB yet */
61 e820entry[nr_map].addr = 0x0;
62 e820entry[nr_map].size = 0x9F800;
63 e820entry[nr_map].type = E820_RAM;
64 nr_map++;
66 e820entry[nr_map].addr = 0x9F800;
67 e820entry[nr_map].size = 0x800;
68 e820entry[nr_map].type = E820_RESERVED;
69 nr_map++;
71 e820entry[nr_map].addr = 0xA0000;
72 e820entry[nr_map].size = 0x20000;
73 e820entry[nr_map].type = E820_IO;
74 nr_map++;
76 e820entry[nr_map].addr = 0xF0000;
77 e820entry[nr_map].size = 0x10000;
78 e820entry[nr_map].type = E820_RESERVED;
79 nr_map++;
81 #define STATIC_PAGES 2 /* for ioreq_t and store_mfn */
82 /* Most of the ram goes here */
83 e820entry[nr_map].addr = 0x100000;
84 e820entry[nr_map].size = mem_size - 0x100000 - STATIC_PAGES*PAGE_SIZE;
85 e820entry[nr_map].type = E820_RAM;
86 nr_map++;
88 /* Statically allocated special pages */
90 /* Shared ioreq_t page */
91 e820entry[nr_map].addr = mem_size - PAGE_SIZE;
92 e820entry[nr_map].size = PAGE_SIZE;
93 e820entry[nr_map].type = E820_SHARED_PAGE;
94 nr_map++;
96 /* For xenstore */
97 e820entry[nr_map].addr = mem_size - 2*PAGE_SIZE;
98 e820entry[nr_map].size = PAGE_SIZE;
99 e820entry[nr_map].type = E820_XENSTORE;
100 nr_map++;
102 e820entry[nr_map].addr = mem_size;
103 e820entry[nr_map].size = 0x3 * PAGE_SIZE;
104 e820entry[nr_map].type = E820_NVS;
105 nr_map++;
107 e820entry[nr_map].addr = mem_size + 0x3 * PAGE_SIZE;
108 e820entry[nr_map].size = 0xA * PAGE_SIZE;
109 e820entry[nr_map].type = E820_ACPI;
110 nr_map++;
112 e820entry[nr_map].addr = 0xFEC00000;
113 e820entry[nr_map].size = 0x1400000;
114 e820entry[nr_map].type = E820_IO;
115 nr_map++;
117 return (*(((unsigned char *)e820_page) + E820_MAP_NR_OFFSET) = nr_map);
118 }
120 static void
121 set_hvm_info_checksum(struct hvm_info_table *t)
122 {
123 uint8_t *ptr = (uint8_t *)t, sum = 0;
124 unsigned int i;
126 t->checksum = 0;
128 for (i = 0; i < t->length; i++)
129 sum += *ptr++;
131 t->checksum = -sum;
132 }
134 /*
135 * Use E820 reserved memory 0x9F800 to pass HVM info to vmxloader
136 * hvmloader will use this info to set BIOS accordingly
137 */
138 static int set_hvm_info(int xc_handle, uint32_t dom,
139 unsigned long *pfn_list, unsigned int vcpus,
140 unsigned int acpi, unsigned int apic)
141 {
142 char *va_map;
143 struct hvm_info_table *va_hvm;
146 va_map = xc_map_foreign_range(
147 xc_handle,
148 dom,
149 PAGE_SIZE,
150 PROT_READ|PROT_WRITE,
151 pfn_list[HVM_INFO_PFN]);
153 if ( va_map == NULL )
154 return -1;
156 va_hvm = (struct hvm_info_table *)(va_map + HVM_INFO_OFFSET);
157 memset(va_hvm, 0, sizeof(*va_hvm));
158 strncpy(va_hvm->signature, "HVM INFO", 8);
159 va_hvm->length = sizeof(struct hvm_info_table);
160 va_hvm->acpi_enabled = acpi;
161 va_hvm->apic_enabled = apic;
162 va_hvm->nr_vcpus = vcpus;
164 set_hvm_info_checksum(va_hvm);
166 munmap(va_map, PAGE_SIZE);
168 return 0;
169 }
171 #ifdef __i386__
172 static int zap_mmio_range(int xc_handle, uint32_t dom,
173 l2_pgentry_32_t *vl2tab,
174 unsigned long mmio_range_start,
175 unsigned long mmio_range_size)
176 {
177 unsigned long mmio_addr;
178 unsigned long mmio_range_end = mmio_range_start + mmio_range_size;
179 unsigned long vl2e;
180 l1_pgentry_32_t *vl1tab;
182 mmio_addr = mmio_range_start & PAGE_MASK;
183 for (; mmio_addr < mmio_range_end; mmio_addr += PAGE_SIZE) {
184 vl2e = vl2tab[l2_table_offset(mmio_addr)];
185 if (vl2e == 0)
186 continue;
187 vl1tab = xc_map_foreign_range(
188 xc_handle, dom, PAGE_SIZE,
189 PROT_READ|PROT_WRITE, vl2e >> PAGE_SHIFT);
190 if ( vl1tab == 0 )
191 {
192 PERROR("Failed zap MMIO range");
193 return -1;
194 }
195 vl1tab[l1_table_offset(mmio_addr)] = 0;
196 munmap(vl1tab, PAGE_SIZE);
197 }
198 return 0;
199 }
201 static int zap_mmio_ranges(int xc_handle, uint32_t dom, unsigned long l2tab,
202 unsigned char e820_map_nr, unsigned char *e820map)
203 {
204 unsigned int i;
205 struct e820entry *e820entry = (struct e820entry *)e820map;
207 l2_pgentry_32_t *vl2tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
208 PROT_READ|PROT_WRITE,
209 l2tab >> PAGE_SHIFT);
210 if ( vl2tab == 0 )
211 return -1;
213 for ( i = 0; i < e820_map_nr; i++ )
214 {
215 if ( (e820entry[i].type == E820_IO) &&
216 (zap_mmio_range(xc_handle, dom, vl2tab,
217 e820entry[i].addr, e820entry[i].size) == -1))
218 return -1;
219 }
221 munmap(vl2tab, PAGE_SIZE);
222 return 0;
223 }
224 #else
225 static int zap_mmio_range(int xc_handle, uint32_t dom,
226 l3_pgentry_t *vl3tab,
227 unsigned long mmio_range_start,
228 unsigned long mmio_range_size)
229 {
230 unsigned long mmio_addr;
231 unsigned long mmio_range_end = mmio_range_start + mmio_range_size;
232 unsigned long vl2e = 0;
233 unsigned long vl3e;
234 l1_pgentry_t *vl1tab;
235 l2_pgentry_t *vl2tab;
237 mmio_addr = mmio_range_start & PAGE_MASK;
238 for ( ; mmio_addr < mmio_range_end; mmio_addr += PAGE_SIZE )
239 {
240 vl3e = vl3tab[l3_table_offset(mmio_addr)];
241 if ( vl3e == 0 )
242 continue;
244 vl2tab = xc_map_foreign_range(
245 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, vl3e>>PAGE_SHIFT);
246 if ( vl2tab == NULL )
247 {
248 PERROR("Failed zap MMIO range");
249 return -1;
250 }
252 vl2e = vl2tab[l2_table_offset(mmio_addr)];
253 if ( vl2e == 0 )
254 {
255 munmap(vl2tab, PAGE_SIZE);
256 continue;
257 }
259 vl1tab = xc_map_foreign_range(
260 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, vl2e>>PAGE_SHIFT);
261 if ( vl1tab == NULL )
262 {
263 PERROR("Failed zap MMIO range");
264 munmap(vl2tab, PAGE_SIZE);
265 return -1;
266 }
268 vl1tab[l1_table_offset(mmio_addr)] = 0;
269 munmap(vl2tab, PAGE_SIZE);
270 munmap(vl1tab, PAGE_SIZE);
271 }
272 return 0;
273 }
275 static int zap_mmio_ranges(int xc_handle, uint32_t dom, unsigned long l3tab,
276 unsigned char e820_map_nr, unsigned char *e820map)
277 {
278 unsigned int i;
279 struct e820entry *e820entry = (struct e820entry *)e820map;
281 l3_pgentry_t *vl3tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
282 PROT_READ|PROT_WRITE,
283 l3tab >> PAGE_SHIFT);
284 if (vl3tab == 0)
285 return -1;
286 for ( i = 0; i < e820_map_nr; i++ ) {
287 if ( (e820entry[i].type == E820_IO) &&
288 (zap_mmio_range(xc_handle, dom, vl3tab,
289 e820entry[i].addr, e820entry[i].size) == -1) )
290 return -1;
291 }
292 munmap(vl3tab, PAGE_SIZE);
293 return 0;
294 }
296 #endif
298 static int setup_guest(int xc_handle,
299 uint32_t dom, int memsize,
300 char *image, unsigned long image_size,
301 unsigned long nr_pages,
302 vcpu_guest_context_t *ctxt,
303 unsigned long shared_info_frame,
304 unsigned int control_evtchn,
305 unsigned int vcpus,
306 unsigned int acpi,
307 unsigned int apic,
308 unsigned int store_evtchn,
309 unsigned long *store_mfn)
310 {
311 l1_pgentry_t *vl1tab=NULL, *vl1e=NULL;
312 l2_pgentry_t *vl2tab=NULL, *vl2e=NULL;
313 unsigned long *page_array = NULL;
314 #ifdef __x86_64__
315 l3_pgentry_t *vl3tab=NULL;
316 unsigned long l3tab;
317 #endif
318 unsigned long l2tab = 0;
319 unsigned long l1tab = 0;
320 unsigned long count, i;
321 shared_info_t *shared_info;
322 void *e820_page;
323 unsigned char e820_map_nr;
324 xc_mmu_t *mmu = NULL;
325 int rc;
327 unsigned long nr_pt_pages;
328 unsigned long ppt_alloc;
330 struct domain_setup_info dsi;
331 unsigned long vpt_start;
332 unsigned long vpt_end;
333 unsigned long v_end;
335 unsigned long shared_page_frame = 0;
336 shared_iopage_t *sp;
338 memset(&dsi, 0, sizeof(struct domain_setup_info));
340 if ( (rc = parseelfimage(image, image_size, &dsi)) != 0 )
341 goto error_out;
343 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
344 {
345 PERROR("Guest OS must load to a page boundary.\n");
346 goto error_out;
347 }
349 /* memsize is in megabytes */
350 v_end = (unsigned long)memsize << 20;
352 #ifdef __i386__
353 nr_pt_pages = 1 + ((memsize + 3) >> 2);
354 #else
355 nr_pt_pages = 5 + ((memsize + 1) >> 1);
356 #endif
357 vpt_start = v_end;
358 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
360 printf("VIRTUAL MEMORY ARRANGEMENT:\n"
361 " Loaded HVM loader: %08lx->%08lx\n"
362 " Page tables: %08lx->%08lx\n"
363 " TOTAL: %08lx->%08lx\n",
364 dsi.v_kernstart, dsi.v_kernend,
365 vpt_start, vpt_end,
366 dsi.v_start, v_end);
367 printf(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
369 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
370 {
371 ERROR("Initial guest OS requires too much space\n"
372 "(%luMB is greater than %luMB limit)\n",
373 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
374 goto error_out;
375 }
377 if ( (page_array = malloc(nr_pages * sizeof(unsigned long))) == NULL )
378 {
379 PERROR("Could not allocate memory");
380 goto error_out;
381 }
383 if ( xc_get_pfn_list(xc_handle, dom, page_array, nr_pages) != nr_pages )
384 {
385 PERROR("Could not get the page frame list");
386 goto error_out;
387 }
389 loadelfimage(image, xc_handle, dom, page_array, &dsi);
391 if ( (mmu = xc_init_mmu_updates(xc_handle, dom)) == NULL )
392 goto error_out;
394 /* First allocate page for page dir or pdpt */
395 ppt_alloc = vpt_start >> PAGE_SHIFT;
396 if ( page_array[ppt_alloc] > 0xfffff )
397 {
398 unsigned long nmfn;
399 nmfn = xc_make_page_below_4G( xc_handle, dom, page_array[ppt_alloc] );
400 if ( nmfn == 0 )
401 {
402 fprintf(stderr, "Couldn't get a page below 4GB :-(\n");
403 goto error_out;
404 }
405 page_array[ppt_alloc] = nmfn;
406 }
408 #ifdef __i386__
409 l2tab = page_array[ppt_alloc++] << PAGE_SHIFT;
410 ctxt->ctrlreg[3] = l2tab;
412 if ( (vl2tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
413 PROT_READ|PROT_WRITE,
414 l2tab >> PAGE_SHIFT)) == NULL )
415 goto error_out;
416 memset(vl2tab, 0, PAGE_SIZE);
417 vl2e = &vl2tab[l2_table_offset(0)];
418 for ( count = 0; count < (v_end >> PAGE_SHIFT); count++ )
419 {
420 if ( ((unsigned long)vl1e & (PAGE_SIZE-1)) == 0 )
421 {
422 l1tab = page_array[ppt_alloc++] << PAGE_SHIFT;
423 if ( vl1tab != NULL )
424 munmap(vl1tab, PAGE_SIZE);
425 if ( (vl1tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
426 PROT_READ|PROT_WRITE,
427 l1tab >> PAGE_SHIFT)) == NULL )
428 {
429 munmap(vl2tab, PAGE_SIZE);
430 goto error_out;
431 }
432 memset(vl1tab, 0, PAGE_SIZE);
433 vl1e = &vl1tab[l1_table_offset(count << PAGE_SHIFT)];
434 *vl2e++ = l1tab | L2_PROT;
435 }
437 *vl1e = (page_array[count] << PAGE_SHIFT) | L1_PROT;
438 vl1e++;
439 }
440 munmap(vl1tab, PAGE_SIZE);
441 munmap(vl2tab, PAGE_SIZE);
442 #else
443 l3tab = page_array[ppt_alloc++] << PAGE_SHIFT;
444 ctxt->ctrlreg[3] = l3tab;
446 if ( (vl3tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
447 PROT_READ|PROT_WRITE,
448 l3tab >> PAGE_SHIFT)) == NULL )
449 goto error_out;
450 memset(vl3tab, 0, PAGE_SIZE);
452 /* Fill in every PDPT entry. */
453 for ( i = 0; i < L3_PAGETABLE_ENTRIES_PAE; i++ )
454 {
455 l2tab = page_array[ppt_alloc++] << PAGE_SHIFT;
456 if ( (vl2tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
457 PROT_READ|PROT_WRITE,
458 l2tab >> PAGE_SHIFT)) == NULL )
459 goto error_out;
460 memset(vl2tab, 0, PAGE_SIZE);
461 munmap(vl2tab, PAGE_SIZE);
462 vl2tab = NULL;
463 vl3tab[i] = l2tab | L3_PROT;
464 }
466 for ( count = 0; count < (v_end >> PAGE_SHIFT); count++ )
467 {
468 if ( !(count & ((1 << (L3_PAGETABLE_SHIFT - L1_PAGETABLE_SHIFT)) - 1)) )
469 {
470 l2tab = vl3tab[count >> (L3_PAGETABLE_SHIFT - L1_PAGETABLE_SHIFT)]
471 & PAGE_MASK;
473 if (vl2tab != NULL)
474 munmap(vl2tab, PAGE_SIZE);
476 if ( (vl2tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
477 PROT_READ|PROT_WRITE,
478 l2tab >> PAGE_SHIFT)) == NULL )
479 goto error_out;
481 vl2e = &vl2tab[l2_table_offset(count << PAGE_SHIFT)];
482 }
483 if ( ((unsigned long)vl1e & (PAGE_SIZE-1)) == 0 )
484 {
485 l1tab = page_array[ppt_alloc++] << PAGE_SHIFT;
486 if ( vl1tab != NULL )
487 munmap(vl1tab, PAGE_SIZE);
488 if ( (vl1tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
489 PROT_READ|PROT_WRITE,
490 l1tab >> PAGE_SHIFT)) == NULL )
491 {
492 munmap(vl2tab, PAGE_SIZE);
493 goto error_out;
494 }
495 memset(vl1tab, 0, PAGE_SIZE);
496 vl1e = &vl1tab[l1_table_offset(count << PAGE_SHIFT)];
497 *vl2e++ = l1tab | L2_PROT;
498 }
500 *vl1e = (page_array[count] << PAGE_SHIFT) | L1_PROT;
501 vl1e++;
502 }
504 munmap(vl1tab, PAGE_SIZE);
505 munmap(vl2tab, PAGE_SIZE);
506 munmap(vl3tab, PAGE_SIZE);
507 #endif
508 /* Write the machine->phys table entries. */
509 for ( count = 0; count < nr_pages; count++ )
510 {
511 if ( xc_add_mmu_update(xc_handle, mmu,
512 (page_array[count] << PAGE_SHIFT) |
513 MMU_MACHPHYS_UPDATE, count) )
514 goto error_out;
515 }
517 if ( set_hvm_info(xc_handle, dom, page_array, vcpus, acpi, apic) ) {
518 fprintf(stderr, "Couldn't set hvm info for HVM guest.\n");
519 goto error_out;
520 }
522 if ( (e820_page = xc_map_foreign_range(
523 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
524 page_array[E820_MAP_PAGE >> PAGE_SHIFT])) == 0 )
525 goto error_out;
526 memset(e820_page, 0, PAGE_SIZE);
527 e820_map_nr = build_e820map(e820_page, v_end);
528 #if defined (__i386__)
529 if (zap_mmio_ranges(xc_handle, dom, l2tab, e820_map_nr,
530 ((unsigned char *)e820_page) + E820_MAP_OFFSET) == -1)
531 #else
532 if (zap_mmio_ranges(xc_handle, dom, l3tab, e820_map_nr,
533 ((unsigned char *)e820_page) + E820_MAP_OFFSET) == -1)
534 #endif
535 goto error_out;
536 munmap(e820_page, PAGE_SIZE);
538 /* shared_info page starts its life empty. */
539 if ( (shared_info = xc_map_foreign_range(
540 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
541 shared_info_frame)) == 0 )
542 goto error_out;
543 memset(shared_info, 0, sizeof(shared_info_t));
544 /* Mask all upcalls... */
545 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
546 shared_info->vcpu_info[i].evtchn_upcall_mask = 1;
547 munmap(shared_info, PAGE_SIZE);
549 /* Populate the event channel port in the shared page */
550 shared_page_frame = page_array[(v_end >> PAGE_SHIFT) - 1];
551 if ( (sp = (shared_iopage_t *) xc_map_foreign_range(
552 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
553 shared_page_frame)) == 0 )
554 goto error_out;
555 memset(sp, 0, PAGE_SIZE);
556 sp->sp_global.eport = control_evtchn;
557 munmap(sp, PAGE_SIZE);
559 *store_mfn = page_array[(v_end >> PAGE_SHIFT) - 2];
560 if ( xc_clear_domain_page(xc_handle, dom, *store_mfn) )
561 goto error_out;
563 /* Send the page update requests down to the hypervisor. */
564 if ( xc_finish_mmu_updates(xc_handle, mmu) )
565 goto error_out;
567 free(mmu);
568 free(page_array);
570 /*
571 * Initial register values:
572 */
573 ctxt->user_regs.ds = 0;
574 ctxt->user_regs.es = 0;
575 ctxt->user_regs.fs = 0;
576 ctxt->user_regs.gs = 0;
577 ctxt->user_regs.ss = 0;
578 ctxt->user_regs.cs = 0;
579 ctxt->user_regs.eip = dsi.v_kernentry;
580 ctxt->user_regs.edx = 0;
581 ctxt->user_regs.eax = 0;
582 ctxt->user_regs.esp = 0;
583 ctxt->user_regs.ebx = 0; /* startup_32 expects this to be 0 to signal boot cpu */
584 ctxt->user_regs.ecx = 0;
585 ctxt->user_regs.esi = 0;
586 ctxt->user_regs.edi = 0;
587 ctxt->user_regs.ebp = 0;
589 ctxt->user_regs.eflags = 0;
591 return 0;
593 error_out:
594 free(mmu);
595 free(page_array);
596 return -1;
597 }
599 int xc_hvm_build(int xc_handle,
600 uint32_t domid,
601 int memsize,
602 const char *image_name,
603 unsigned int control_evtchn,
604 unsigned int vcpus,
605 unsigned int acpi,
606 unsigned int apic,
607 unsigned int store_evtchn,
608 unsigned long *store_mfn)
609 {
610 dom0_op_t launch_op, op;
611 int rc, i;
612 vcpu_guest_context_t st_ctxt, *ctxt = &st_ctxt;
613 unsigned long nr_pages;
614 char *image = NULL;
615 unsigned long image_size;
616 xen_capabilities_info_t xen_caps;
618 if ( (rc = xc_version(xc_handle, XENVER_capabilities, &xen_caps)) != 0 )
619 {
620 PERROR("Failed to get xen version info");
621 goto error_out;
622 }
624 if ( !strstr(xen_caps, "hvm") )
625 {
626 PERROR("CPU doesn't support HVM extensions or "
627 "the extensions are not enabled");
628 goto error_out;
629 }
631 if ( (nr_pages = xc_get_tot_pages(xc_handle, domid)) < 0 )
632 {
633 PERROR("Could not find total pages for domain");
634 goto error_out;
635 }
637 if ( (image = xc_read_kernel_image(image_name, &image_size)) == NULL )
638 goto error_out;
640 if ( mlock(&st_ctxt, sizeof(st_ctxt) ) )
641 {
642 PERROR("%s: ctxt mlock failed", __func__);
643 return 1;
644 }
646 op.cmd = DOM0_GETDOMAININFO;
647 op.u.getdomaininfo.domain = (domid_t)domid;
648 if ( (xc_dom0_op(xc_handle, &op) < 0) ||
649 ((uint16_t)op.u.getdomaininfo.domain != domid) )
650 {
651 PERROR("Could not get info on domain");
652 goto error_out;
653 }
655 memset(ctxt, 0, sizeof(*ctxt));
657 ctxt->flags = VGCF_HVM_GUEST;
658 if ( setup_guest(xc_handle, domid, memsize, image, image_size, nr_pages,
659 ctxt, op.u.getdomaininfo.shared_info_frame, control_evtchn,
660 vcpus, acpi, apic, store_evtchn, store_mfn) < 0)
661 {
662 ERROR("Error constructing guest OS");
663 goto error_out;
664 }
666 free(image);
668 /* FPU is set up to default initial state. */
669 memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
671 /* Virtual IDT is empty at start-of-day. */
672 for ( i = 0; i < 256; i++ )
673 {
674 ctxt->trap_ctxt[i].vector = i;
675 ctxt->trap_ctxt[i].cs = FLAT_KERNEL_CS;
676 }
678 /* No LDT. */
679 ctxt->ldt_ents = 0;
681 /* Use the default Xen-provided GDT. */
682 ctxt->gdt_ents = 0;
684 /* No debugging. */
685 memset(ctxt->debugreg, 0, sizeof(ctxt->debugreg));
687 /* No callback handlers. */
688 #if defined(__i386__)
689 ctxt->event_callback_cs = FLAT_KERNEL_CS;
690 ctxt->event_callback_eip = 0;
691 ctxt->failsafe_callback_cs = FLAT_KERNEL_CS;
692 ctxt->failsafe_callback_eip = 0;
693 #elif defined(__x86_64__)
694 ctxt->event_callback_eip = 0;
695 ctxt->failsafe_callback_eip = 0;
696 ctxt->syscall_callback_eip = 0;
697 #endif
699 memset( &launch_op, 0, sizeof(launch_op) );
701 launch_op.u.setvcpucontext.domain = (domid_t)domid;
702 launch_op.u.setvcpucontext.vcpu = 0;
703 launch_op.u.setvcpucontext.ctxt = ctxt;
705 launch_op.cmd = DOM0_SETVCPUCONTEXT;
706 rc = xc_dom0_op(xc_handle, &launch_op);
708 return rc;
710 error_out:
711 free(image);
712 return -1;
713 }
715 static inline int is_loadable_phdr(Elf32_Phdr *phdr)
716 {
717 return ((phdr->p_type == PT_LOAD) &&
718 ((phdr->p_flags & (PF_W|PF_X)) != 0));
719 }
721 static int parseelfimage(char *elfbase,
722 unsigned long elfsize,
723 struct domain_setup_info *dsi)
724 {
725 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
726 Elf32_Phdr *phdr;
727 Elf32_Shdr *shdr;
728 unsigned long kernstart = ~0UL, kernend=0UL;
729 char *shstrtab;
730 int h;
732 if ( !IS_ELF(*ehdr) )
733 {
734 ERROR("Kernel image does not have an ELF header.");
735 return -EINVAL;
736 }
738 if ( (ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize)) > elfsize )
739 {
740 ERROR("ELF program headers extend beyond end of image.");
741 return -EINVAL;
742 }
744 if ( (ehdr->e_shoff + (ehdr->e_shnum * ehdr->e_shentsize)) > elfsize )
745 {
746 ERROR("ELF section headers extend beyond end of image.");
747 return -EINVAL;
748 }
750 /* Find the section-header strings table. */
751 if ( ehdr->e_shstrndx == SHN_UNDEF )
752 {
753 ERROR("ELF image has no section-header strings table (shstrtab).");
754 return -EINVAL;
755 }
756 shdr = (Elf32_Shdr *)(elfbase + ehdr->e_shoff +
757 (ehdr->e_shstrndx*ehdr->e_shentsize));
758 shstrtab = elfbase + shdr->sh_offset;
760 for ( h = 0; h < ehdr->e_phnum; h++ )
761 {
762 phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
763 if ( !is_loadable_phdr(phdr) )
764 continue;
765 if ( phdr->p_paddr < kernstart )
766 kernstart = phdr->p_paddr;
767 if ( (phdr->p_paddr + phdr->p_memsz) > kernend )
768 kernend = phdr->p_paddr + phdr->p_memsz;
769 }
771 if ( (kernstart > kernend) ||
772 (ehdr->e_entry < kernstart) ||
773 (ehdr->e_entry > kernend) )
774 {
775 ERROR("Malformed ELF image.");
776 return -EINVAL;
777 }
779 dsi->v_start = 0x00000000;
781 dsi->v_kernstart = kernstart;
782 dsi->v_kernend = kernend;
783 dsi->v_kernentry = HVM_LOADER_ENTR_ADDR;
785 dsi->v_end = dsi->v_kernend;
787 return 0;
788 }
790 static int
791 loadelfimage(
792 char *elfbase, int xch, uint32_t dom, unsigned long *parray,
793 struct domain_setup_info *dsi)
794 {
795 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
796 Elf32_Phdr *phdr;
797 int h;
799 char *va;
800 unsigned long pa, done, chunksz;
802 for ( h = 0; h < ehdr->e_phnum; h++ )
803 {
804 phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
805 if ( !is_loadable_phdr(phdr) )
806 continue;
808 for ( done = 0; done < phdr->p_filesz; done += chunksz )
809 {
810 pa = (phdr->p_paddr + done) - dsi->v_start;
811 if ((va = xc_map_foreign_range(
812 xch, dom, PAGE_SIZE, PROT_WRITE,
813 parray[pa >> PAGE_SHIFT])) == 0)
814 return -1;
815 chunksz = phdr->p_filesz - done;
816 if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
817 chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
818 memcpy(va + (pa & (PAGE_SIZE-1)),
819 elfbase + phdr->p_offset + done, chunksz);
820 munmap(va, PAGE_SIZE);
821 }
823 for ( ; done < phdr->p_memsz; done += chunksz )
824 {
825 pa = (phdr->p_paddr + done) - dsi->v_start;
826 if ((va = xc_map_foreign_range(
827 xch, dom, PAGE_SIZE, PROT_WRITE,
828 parray[pa >> PAGE_SHIFT])) == 0)
829 return -1;
830 chunksz = phdr->p_memsz - done;
831 if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
832 chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
833 memset(va + (pa & (PAGE_SIZE-1)), 0, chunksz);
834 munmap(va, PAGE_SIZE);
835 }
836 }
838 return 0;
839 }
841 /*
842 * Local variables:
843 * mode: C
844 * c-set-style: "BSD"
845 * c-basic-offset: 4
846 * tab-width: 4
847 * indent-tabs-mode: nil
848 * End:
849 */