ia64/xen-unstable

view tools/libxc/xc_hvm_build.c @ 8964:8946b6dcd49e

Fix x86_64 Xen build.

event_callback_cs and failsafe_callback_cs are x86_32 only.

Signed-off-by: Ian Campbell <Ian.Campbell@XenSource.com>
author Ian.Campbell@xensource.com
date Wed Feb 22 17:26:39 2006 +0000 (2006-02-22)
parents f33e537c4ac5
children b5bb9920bf48
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 pae, 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->pae_enabled = pae;
163 va_hvm->nr_vcpus = vcpus;
165 set_hvm_info_checksum(va_hvm);
167 munmap(va_map, PAGE_SIZE);
169 return 0;
170 }
172 static int setup_guest(int xc_handle,
173 uint32_t dom, int memsize,
174 char *image, unsigned long image_size,
175 unsigned long nr_pages,
176 vcpu_guest_context_t *ctxt,
177 unsigned long shared_info_frame,
178 unsigned int control_evtchn,
179 unsigned int vcpus,
180 unsigned int pae,
181 unsigned int acpi,
182 unsigned int apic,
183 unsigned int store_evtchn,
184 unsigned long *store_mfn)
185 {
186 unsigned long *page_array = NULL;
188 unsigned long count, i;
189 shared_info_t *shared_info;
190 void *e820_page;
191 unsigned char e820_map_nr;
192 xc_mmu_t *mmu = NULL;
193 int rc;
195 struct domain_setup_info dsi;
196 unsigned long v_end;
198 unsigned long shared_page_frame = 0;
199 shared_iopage_t *sp;
201 memset(&dsi, 0, sizeof(struct domain_setup_info));
203 if ( (rc = parseelfimage(image, image_size, &dsi)) != 0 )
204 goto error_out;
206 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
207 {
208 PERROR("Guest OS must load to a page boundary.\n");
209 goto error_out;
210 }
212 /* memsize is in megabytes */
213 v_end = (unsigned long)memsize << 20;
215 printf("VIRTUAL MEMORY ARRANGEMENT:\n"
216 " Loaded HVM loader: %08lx->%08lx\n"
217 " TOTAL: %08lx->%08lx\n",
218 dsi.v_kernstart, dsi.v_kernend,
219 dsi.v_start, v_end);
220 printf(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
222 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
223 {
224 ERROR("Initial guest OS requires too much space\n"
225 "(%luMB is greater than %luMB limit)\n",
226 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
227 goto error_out;
228 }
230 if ( (page_array = malloc(nr_pages * sizeof(unsigned long))) == NULL )
231 {
232 PERROR("Could not allocate memory");
233 goto error_out;
234 }
236 if ( xc_get_pfn_list(xc_handle, dom, page_array, nr_pages) != nr_pages )
237 {
238 PERROR("Could not get the page frame list");
239 goto error_out;
240 }
242 loadelfimage(image, xc_handle, dom, page_array, &dsi);
244 if ( (mmu = xc_init_mmu_updates(xc_handle, dom)) == NULL )
245 goto error_out;
247 /* Write the machine->phys table entries. */
248 for ( count = 0; count < nr_pages; count++ )
249 {
250 if ( xc_add_mmu_update(xc_handle, mmu,
251 (page_array[count] << PAGE_SHIFT) |
252 MMU_MACHPHYS_UPDATE, count) )
253 goto error_out;
254 }
256 if ( set_hvm_info(xc_handle, dom, page_array, vcpus, pae, acpi, apic) ) {
257 fprintf(stderr, "Couldn't set hvm info for HVM guest.\n");
258 goto error_out;
259 }
261 if ( (e820_page = xc_map_foreign_range(
262 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
263 page_array[E820_MAP_PAGE >> PAGE_SHIFT])) == 0 )
264 goto error_out;
265 memset(e820_page, 0, PAGE_SIZE);
266 e820_map_nr = build_e820map(e820_page, v_end);
267 munmap(e820_page, PAGE_SIZE);
269 /* shared_info page starts its life empty. */
270 if ( (shared_info = xc_map_foreign_range(
271 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
272 shared_info_frame)) == 0 )
273 goto error_out;
274 memset(shared_info, 0, sizeof(shared_info_t));
275 /* Mask all upcalls... */
276 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
277 shared_info->vcpu_info[i].evtchn_upcall_mask = 1;
278 munmap(shared_info, PAGE_SIZE);
280 /* Populate the event channel port in the shared page */
281 shared_page_frame = page_array[(v_end >> PAGE_SHIFT) - 1];
282 if ( (sp = (shared_iopage_t *) xc_map_foreign_range(
283 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
284 shared_page_frame)) == 0 )
285 goto error_out;
286 memset(sp, 0, PAGE_SIZE);
287 sp->sp_global.eport = control_evtchn;
288 munmap(sp, PAGE_SIZE);
290 *store_mfn = page_array[(v_end >> PAGE_SHIFT) - 2];
291 if ( xc_clear_domain_page(xc_handle, dom, *store_mfn) )
292 goto error_out;
294 /* Send the page update requests down to the hypervisor. */
295 if ( xc_finish_mmu_updates(xc_handle, mmu) )
296 goto error_out;
298 free(mmu);
299 free(page_array);
301 /*
302 * Initial register values:
303 */
304 ctxt->user_regs.ds = 0;
305 ctxt->user_regs.es = 0;
306 ctxt->user_regs.fs = 0;
307 ctxt->user_regs.gs = 0;
308 ctxt->user_regs.ss = 0;
309 ctxt->user_regs.cs = 0;
310 ctxt->user_regs.eip = dsi.v_kernentry;
311 ctxt->user_regs.edx = 0;
312 ctxt->user_regs.eax = 0;
313 ctxt->user_regs.esp = 0;
314 ctxt->user_regs.ebx = 0; /* startup_32 expects this to be 0 to signal boot cpu */
315 ctxt->user_regs.ecx = 0;
316 ctxt->user_regs.esi = 0;
317 ctxt->user_regs.edi = 0;
318 ctxt->user_regs.ebp = 0;
320 ctxt->user_regs.eflags = 0;
322 return 0;
324 error_out:
325 free(mmu);
326 free(page_array);
327 return -1;
328 }
330 int xc_hvm_build(int xc_handle,
331 uint32_t domid,
332 int memsize,
333 const char *image_name,
334 unsigned int control_evtchn,
335 unsigned int vcpus,
336 unsigned int pae,
337 unsigned int acpi,
338 unsigned int apic,
339 unsigned int store_evtchn,
340 unsigned long *store_mfn)
341 {
342 dom0_op_t launch_op, op;
343 int rc, i;
344 vcpu_guest_context_t st_ctxt, *ctxt = &st_ctxt;
345 unsigned long nr_pages;
346 char *image = NULL;
347 unsigned long image_size;
348 xen_capabilities_info_t xen_caps;
350 if ( (rc = xc_version(xc_handle, XENVER_capabilities, &xen_caps)) != 0 )
351 {
352 PERROR("Failed to get xen version info");
353 goto error_out;
354 }
356 if ( !strstr(xen_caps, "hvm") )
357 {
358 PERROR("CPU doesn't support HVM extensions or "
359 "the extensions are not enabled");
360 goto error_out;
361 }
363 if ( (nr_pages = xc_get_tot_pages(xc_handle, domid)) < 0 )
364 {
365 PERROR("Could not find total pages for domain");
366 goto error_out;
367 }
369 if ( (image = xc_read_kernel_image(image_name, &image_size)) == NULL )
370 goto error_out;
372 if ( mlock(&st_ctxt, sizeof(st_ctxt) ) )
373 {
374 PERROR("%s: ctxt mlock failed", __func__);
375 return 1;
376 }
378 op.cmd = DOM0_GETDOMAININFO;
379 op.u.getdomaininfo.domain = (domid_t)domid;
380 if ( (xc_dom0_op(xc_handle, &op) < 0) ||
381 ((uint16_t)op.u.getdomaininfo.domain != domid) )
382 {
383 PERROR("Could not get info on domain");
384 goto error_out;
385 }
387 memset(ctxt, 0, sizeof(*ctxt));
389 ctxt->flags = VGCF_HVM_GUEST;
390 if ( setup_guest(xc_handle, domid, memsize, image, image_size, nr_pages,
391 ctxt, op.u.getdomaininfo.shared_info_frame, control_evtchn,
392 vcpus, pae, acpi, apic, store_evtchn, store_mfn) < 0)
393 {
394 ERROR("Error constructing guest OS");
395 goto error_out;
396 }
398 free(image);
400 /* FPU is set up to default initial state. */
401 memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
403 /* Virtual IDT is empty at start-of-day. */
404 for ( i = 0; i < 256; i++ )
405 {
406 ctxt->trap_ctxt[i].vector = i;
407 ctxt->trap_ctxt[i].cs = FLAT_KERNEL_CS;
408 }
410 /* No LDT. */
411 ctxt->ldt_ents = 0;
413 /* Use the default Xen-provided GDT. */
414 ctxt->gdt_ents = 0;
416 /* No debugging. */
417 memset(ctxt->debugreg, 0, sizeof(ctxt->debugreg));
419 /* No callback handlers. */
420 #if defined(__i386__)
421 ctxt->event_callback_cs = FLAT_KERNEL_CS;
422 ctxt->event_callback_eip = 0;
423 ctxt->failsafe_callback_cs = FLAT_KERNEL_CS;
424 ctxt->failsafe_callback_eip = 0;
425 #elif defined(__x86_64__)
426 ctxt->event_callback_eip = 0;
427 ctxt->failsafe_callback_eip = 0;
428 ctxt->syscall_callback_eip = 0;
429 #endif
431 memset( &launch_op, 0, sizeof(launch_op) );
433 launch_op.u.setvcpucontext.domain = (domid_t)domid;
434 launch_op.u.setvcpucontext.vcpu = 0;
435 launch_op.u.setvcpucontext.ctxt = ctxt;
437 launch_op.cmd = DOM0_SETVCPUCONTEXT;
438 rc = xc_dom0_op(xc_handle, &launch_op);
440 return rc;
442 error_out:
443 free(image);
444 return -1;
445 }
447 static inline int is_loadable_phdr(Elf32_Phdr *phdr)
448 {
449 return ((phdr->p_type == PT_LOAD) &&
450 ((phdr->p_flags & (PF_W|PF_X)) != 0));
451 }
453 static int parseelfimage(char *elfbase,
454 unsigned long elfsize,
455 struct domain_setup_info *dsi)
456 {
457 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
458 Elf32_Phdr *phdr;
459 Elf32_Shdr *shdr;
460 unsigned long kernstart = ~0UL, kernend=0UL;
461 char *shstrtab;
462 int h;
464 if ( !IS_ELF(*ehdr) )
465 {
466 ERROR("Kernel image does not have an ELF header.");
467 return -EINVAL;
468 }
470 if ( (ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize)) > elfsize )
471 {
472 ERROR("ELF program headers extend beyond end of image.");
473 return -EINVAL;
474 }
476 if ( (ehdr->e_shoff + (ehdr->e_shnum * ehdr->e_shentsize)) > elfsize )
477 {
478 ERROR("ELF section headers extend beyond end of image.");
479 return -EINVAL;
480 }
482 /* Find the section-header strings table. */
483 if ( ehdr->e_shstrndx == SHN_UNDEF )
484 {
485 ERROR("ELF image has no section-header strings table (shstrtab).");
486 return -EINVAL;
487 }
488 shdr = (Elf32_Shdr *)(elfbase + ehdr->e_shoff +
489 (ehdr->e_shstrndx*ehdr->e_shentsize));
490 shstrtab = elfbase + shdr->sh_offset;
492 for ( h = 0; h < ehdr->e_phnum; h++ )
493 {
494 phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
495 if ( !is_loadable_phdr(phdr) )
496 continue;
497 if ( phdr->p_paddr < kernstart )
498 kernstart = phdr->p_paddr;
499 if ( (phdr->p_paddr + phdr->p_memsz) > kernend )
500 kernend = phdr->p_paddr + phdr->p_memsz;
501 }
503 if ( (kernstart > kernend) ||
504 (ehdr->e_entry < kernstart) ||
505 (ehdr->e_entry > kernend) )
506 {
507 ERROR("Malformed ELF image.");
508 return -EINVAL;
509 }
511 dsi->v_start = 0x00000000;
513 dsi->v_kernstart = kernstart;
514 dsi->v_kernend = kernend;
515 dsi->v_kernentry = HVM_LOADER_ENTR_ADDR;
517 dsi->v_end = dsi->v_kernend;
519 return 0;
520 }
522 static int
523 loadelfimage(
524 char *elfbase, int xch, uint32_t dom, unsigned long *parray,
525 struct domain_setup_info *dsi)
526 {
527 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfbase;
528 Elf32_Phdr *phdr;
529 int h;
531 char *va;
532 unsigned long pa, done, chunksz;
534 for ( h = 0; h < ehdr->e_phnum; h++ )
535 {
536 phdr = (Elf32_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
537 if ( !is_loadable_phdr(phdr) )
538 continue;
540 for ( done = 0; done < phdr->p_filesz; done += chunksz )
541 {
542 pa = (phdr->p_paddr + done) - dsi->v_start;
543 if ((va = xc_map_foreign_range(
544 xch, dom, PAGE_SIZE, PROT_WRITE,
545 parray[pa >> PAGE_SHIFT])) == 0)
546 return -1;
547 chunksz = phdr->p_filesz - done;
548 if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
549 chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
550 memcpy(va + (pa & (PAGE_SIZE-1)),
551 elfbase + phdr->p_offset + done, chunksz);
552 munmap(va, PAGE_SIZE);
553 }
555 for ( ; done < phdr->p_memsz; done += chunksz )
556 {
557 pa = (phdr->p_paddr + done) - dsi->v_start;
558 if ((va = xc_map_foreign_range(
559 xch, dom, PAGE_SIZE, PROT_WRITE,
560 parray[pa >> PAGE_SHIFT])) == 0)
561 return -1;
562 chunksz = phdr->p_memsz - done;
563 if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
564 chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
565 memset(va + (pa & (PAGE_SIZE-1)), 0, chunksz);
566 munmap(va, PAGE_SIZE);
567 }
568 }
570 return 0;
571 }
573 /*
574 * Local variables:
575 * mode: C
576 * c-set-style: "BSD"
577 * c-basic-offset: 4
578 * tab-width: 4
579 * indent-tabs-mode: nil
580 * End:
581 */