direct-io.hg

view tools/libxc/xc_linux_build.c @ 3435:0fd048d86eed

bitkeeper revision 1.1159.220.3 (41e670c37jmaTxUns3KlvsbVRCg-UA)

The getdomaininfo hypercall now listens to the exec_domain parameter
that was already passed to it, and performs some basic sanity checking.

Added exec_domain (aka vcpu) parameters to xc_domain_getfullinfo()
and xc_domain_get_cpu_usage().
author mafetter@fleming.research
date Thu Jan 13 12:59:47 2005 +0000 (2005-01-13)
parents 2419f5c72fe5
children 6096356005ba
line source
1 /******************************************************************************
2 * xc_linux_build.c
3 */
5 #include "xc_private.h"
6 #define ELFSIZE 32
7 #include "xc_elf.h"
8 #include <stdlib.h>
9 #include <zlib.h>
11 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
12 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
14 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
15 #define round_pgdown(_p) ((_p)&PAGE_MASK)
17 struct domain_setup_info
18 {
19 unsigned long v_start;
20 unsigned long v_end;
21 unsigned long v_kernstart;
22 unsigned long v_kernend;
23 unsigned long v_kernentry;
25 unsigned int use_writable_pagetables;
26 unsigned int load_bsd_symtab;
28 unsigned long symtab_addr;
29 unsigned long symtab_len;
30 };
32 static int
33 parseelfimage(
34 char *elfbase, unsigned long elfsize, struct domain_setup_info *dsi);
35 static int
36 loadelfimage(
37 char *elfbase, int xch, u32 dom, unsigned long *parray,
38 unsigned long vstart);
39 static int
40 loadelfsymtab(
41 char *elfbase, int xch, u32 dom, unsigned long *parray,
42 struct domain_setup_info *dsi);
44 static int setup_guestos(int xc_handle,
45 u32 dom,
46 char *image, unsigned long image_size,
47 gzFile initrd_gfd, unsigned long initrd_len,
48 unsigned long nr_pages,
49 unsigned long *pvsi, unsigned long *pvke,
50 full_execution_context_t *ctxt,
51 const char *cmdline,
52 unsigned long shared_info_frame,
53 unsigned int control_evtchn,
54 unsigned long flags)
55 {
56 l1_pgentry_t *vl1tab=NULL, *vl1e=NULL;
57 l2_pgentry_t *vl2tab=NULL, *vl2e=NULL;
58 unsigned long *page_array = NULL;
59 unsigned long l2tab;
60 unsigned long l1tab;
61 unsigned long count, i;
62 start_info_t *start_info;
63 shared_info_t *shared_info;
64 mmu_t *mmu = NULL;
65 int rc;
67 unsigned long nr_pt_pages;
68 unsigned long ppt_alloc;
69 unsigned long *physmap, *physmap_e, physmap_pfn;
71 struct domain_setup_info dsi;
72 unsigned long vinitrd_start;
73 unsigned long vinitrd_end;
74 unsigned long vphysmap_start;
75 unsigned long vphysmap_end;
76 unsigned long vstartinfo_start;
77 unsigned long vstartinfo_end;
78 unsigned long vstack_start;
79 unsigned long vstack_end;
80 unsigned long vpt_start;
81 unsigned long vpt_end;
82 unsigned long v_end;
84 char *n_vcpus;
86 memset(&dsi, 0, sizeof(struct domain_setup_info));
88 rc = parseelfimage(image, image_size, &dsi);
89 if ( rc != 0 )
90 goto error_out;
92 if (dsi.use_writable_pagetables)
93 xc_domain_setvmassist(xc_handle, dom, VMASST_CMD_enable,
94 VMASST_TYPE_writable_pagetables);
96 if (dsi.load_bsd_symtab)
97 loadelfsymtab(image, xc_handle, dom, NULL, &dsi);
99 if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
100 {
101 PERROR("Guest OS must load to a page boundary.\n");
102 goto error_out;
103 }
105 /*
106 * Why do we need this? The number of page-table frames depends on the
107 * size of the bootstrap address space. But the size of the address space
108 * depends on the number of page-table frames (since each one is mapped
109 * read-only). We have a pair of simultaneous equations in two unknowns,
110 * which we solve by exhaustive search.
111 */
112 vinitrd_start = round_pgup(dsi.v_end);
113 vinitrd_end = vinitrd_start + initrd_len;
114 vphysmap_start = round_pgup(vinitrd_end);
115 vphysmap_end = vphysmap_start + (nr_pages * sizeof(unsigned long));
116 vpt_start = round_pgup(vphysmap_end);
117 for ( nr_pt_pages = 2; ; nr_pt_pages++ )
118 {
119 vpt_end = vpt_start + (nr_pt_pages * PAGE_SIZE);
120 vstartinfo_start = vpt_end;
121 vstartinfo_end = vstartinfo_start + PAGE_SIZE;
122 vstack_start = vstartinfo_end;
123 vstack_end = vstack_start + PAGE_SIZE;
124 v_end = (vstack_end + (1<<22)-1) & ~((1<<22)-1);
125 if ( (v_end - vstack_end) < (512 << 10) )
126 v_end += 1 << 22; /* Add extra 4MB to get >= 512kB padding. */
127 if ( (((v_end - dsi.v_start + ((1<<L2_PAGETABLE_SHIFT)-1)) >>
128 L2_PAGETABLE_SHIFT) + 1) <= nr_pt_pages )
129 break;
130 }
132 printf("VIRTUAL MEMORY ARRANGEMENT:\n"
133 " Loaded kernel: %08lx->%08lx\n"
134 " Init. ramdisk: %08lx->%08lx\n"
135 " Phys-Mach map: %08lx->%08lx\n"
136 " Page tables: %08lx->%08lx\n"
137 " Start info: %08lx->%08lx\n"
138 " Boot stack: %08lx->%08lx\n"
139 " TOTAL: %08lx->%08lx\n",
140 dsi.v_kernstart, dsi.v_kernend,
141 vinitrd_start, vinitrd_end,
142 vphysmap_start, vphysmap_end,
143 vpt_start, vpt_end,
144 vstartinfo_start, vstartinfo_end,
145 vstack_start, vstack_end,
146 dsi.v_start, v_end);
147 printf(" ENTRY ADDRESS: %08lx\n", dsi.v_kernentry);
149 if ( (v_end - dsi.v_start) > (nr_pages * PAGE_SIZE) )
150 {
151 printf("Initial guest OS requires too much space\n"
152 "(%luMB is greater than %luMB limit)\n",
153 (v_end-dsi.v_start)>>20, (nr_pages<<PAGE_SHIFT)>>20);
154 goto error_out;
155 }
157 if ( (page_array = malloc(nr_pages * sizeof(unsigned long))) == NULL )
158 {
159 PERROR("Could not allocate memory");
160 goto error_out;
161 }
163 if ( xc_get_pfn_list(xc_handle, dom, page_array, nr_pages) != nr_pages )
164 {
165 PERROR("Could not get the page frame list");
166 goto error_out;
167 }
169 loadelfimage(image, xc_handle, dom, page_array, dsi.v_start);
171 if (dsi.load_bsd_symtab)
172 loadelfsymtab(image, xc_handle, dom, page_array, &dsi);
174 /* Load the initial ramdisk image. */
175 if ( initrd_len != 0 )
176 {
177 for ( i = (vinitrd_start - dsi.v_start);
178 i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
179 {
180 char page[PAGE_SIZE];
181 if ( gzread(initrd_gfd, page, PAGE_SIZE) == -1 )
182 {
183 PERROR("Error reading initrd image, could not");
184 goto error_out;
185 }
186 xc_copy_to_domain_page(xc_handle, dom,
187 page_array[i>>PAGE_SHIFT], page);
188 }
189 }
191 if ( (mmu = init_mmu_updates(xc_handle, dom)) == NULL )
192 goto error_out;
194 /* First allocate page for page dir. */
195 ppt_alloc = (vpt_start - dsi.v_start) >> PAGE_SHIFT;
196 l2tab = page_array[ppt_alloc++] << PAGE_SHIFT;
197 ctxt->pt_base = l2tab;
199 /* Initialise the page tables. */
200 if ( (vl2tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
201 PROT_READ|PROT_WRITE,
202 l2tab >> PAGE_SHIFT)) == NULL )
203 goto error_out;
204 memset(vl2tab, 0, PAGE_SIZE);
205 vl2e = &vl2tab[l2_table_offset(dsi.v_start)];
206 for ( count = 0; count < ((v_end-dsi.v_start)>>PAGE_SHIFT); count++ )
207 {
208 if ( ((unsigned long)vl1e & (PAGE_SIZE-1)) == 0 )
209 {
210 l1tab = page_array[ppt_alloc++] << PAGE_SHIFT;
211 if ( vl1tab != NULL )
212 munmap(vl1tab, PAGE_SIZE);
213 if ( (vl1tab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
214 PROT_READ|PROT_WRITE,
215 l1tab >> PAGE_SHIFT)) == NULL )
216 {
217 munmap(vl2tab, PAGE_SIZE);
218 goto error_out;
219 }
220 memset(vl1tab, 0, PAGE_SIZE);
221 vl1e = &vl1tab[l1_table_offset(dsi.v_start + (count<<PAGE_SHIFT))];
222 *vl2e++ = l1tab | L2_PROT;
223 }
225 *vl1e = (page_array[count] << PAGE_SHIFT) | L1_PROT;
226 if ( (count >= ((vpt_start-dsi.v_start)>>PAGE_SHIFT)) &&
227 (count < ((vpt_end -dsi.v_start)>>PAGE_SHIFT)) )
228 *vl1e &= ~_PAGE_RW;
229 vl1e++;
230 }
231 munmap(vl1tab, PAGE_SIZE);
232 munmap(vl2tab, PAGE_SIZE);
234 /* Write the phys->machine and machine->phys table entries. */
235 physmap_pfn = (vphysmap_start - dsi.v_start) >> PAGE_SHIFT;
236 physmap = physmap_e = xc_map_foreign_range(
237 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
238 page_array[physmap_pfn++]);
239 for ( count = 0; count < nr_pages; count++ )
240 {
241 if ( add_mmu_update(xc_handle, mmu,
242 (page_array[count] << PAGE_SHIFT) |
243 MMU_MACHPHYS_UPDATE, count) )
244 {
245 munmap(physmap, PAGE_SIZE);
246 goto error_out;
247 }
248 *physmap_e++ = page_array[count];
249 if ( ((unsigned long)physmap_e & (PAGE_SIZE-1)) == 0 )
250 {
251 munmap(physmap, PAGE_SIZE);
252 physmap = physmap_e = xc_map_foreign_range(
253 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
254 page_array[physmap_pfn++]);
255 }
256 }
257 munmap(physmap, PAGE_SIZE);
259 /*
260 * Pin down l2tab addr as page dir page - causes hypervisor to provide
261 * correct protection for the page
262 */
263 if ( add_mmu_update(xc_handle, mmu,
264 l2tab | MMU_EXTENDED_COMMAND, MMUEXT_PIN_L2_TABLE) )
265 goto error_out;
267 start_info = xc_map_foreign_range(
268 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
269 page_array[(vstartinfo_start-dsi.v_start)>>PAGE_SHIFT]);
270 memset(start_info, 0, sizeof(*start_info));
271 start_info->nr_pages = nr_pages;
272 start_info->shared_info = shared_info_frame << PAGE_SHIFT;
273 start_info->flags = flags;
274 start_info->pt_base = vpt_start;
275 start_info->nr_pt_frames = nr_pt_pages;
276 start_info->mfn_list = vphysmap_start;
277 start_info->domain_controller_evtchn = control_evtchn;
278 if ( initrd_len != 0 )
279 {
280 start_info->mod_start = vinitrd_start;
281 start_info->mod_len = initrd_len;
282 }
283 strncpy(start_info->cmd_line, cmdline, MAX_CMDLINE);
284 start_info->cmd_line[MAX_CMDLINE-1] = '\0';
285 munmap(start_info, PAGE_SIZE);
287 /* shared_info page starts its life empty. */
288 shared_info = xc_map_foreign_range(
289 xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, shared_info_frame);
290 memset(shared_info, 0, sizeof(shared_info_t));
291 /* Mask all upcalls... */
292 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
293 shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
294 n_vcpus = getenv("XEN_VCPUS");
295 if ( n_vcpus )
296 shared_info->n_vcpu = atoi(n_vcpus);
297 else
298 shared_info->n_vcpu = 1;
299 munmap(shared_info, PAGE_SIZE);
301 /* Send the page update requests down to the hypervisor. */
302 if ( finish_mmu_updates(xc_handle, mmu) )
303 goto error_out;
305 free(mmu);
306 free(page_array);
308 *pvsi = vstartinfo_start;
309 *pvke = dsi.v_kernentry;
311 return 0;
313 error_out:
314 if ( mmu != NULL )
315 free(mmu);
316 if ( page_array != NULL )
317 free(page_array);
318 return -1;
319 }
321 int xc_linux_build(int xc_handle,
322 u32 domid,
323 const char *image_name,
324 const char *ramdisk_name,
325 const char *cmdline,
326 unsigned int control_evtchn,
327 unsigned long flags)
328 {
329 dom0_op_t launch_op, op;
330 int initrd_fd = -1;
331 gzFile initrd_gfd = NULL;
332 int rc, i;
333 full_execution_context_t st_ctxt, *ctxt = &st_ctxt;
334 unsigned long nr_pages;
335 char *image = NULL;
336 unsigned long image_size, initrd_size=0;
337 unsigned long vstartinfo_start, vkern_entry;
339 if ( (nr_pages = xc_get_tot_pages(xc_handle, domid)) < 0 )
340 {
341 PERROR("Could not find total pages for domain");
342 goto error_out;
343 }
345 if ( (image = xc_read_kernel_image(image_name, &image_size)) == NULL )
346 goto error_out;
348 if ( (ramdisk_name != NULL) && (strlen(ramdisk_name) != 0) )
349 {
350 if ( (initrd_fd = open(ramdisk_name, O_RDONLY)) < 0 )
351 {
352 PERROR("Could not open the initial ramdisk image");
353 goto error_out;
354 }
356 initrd_size = xc_get_filesz(initrd_fd);
358 if ( (initrd_gfd = gzdopen(initrd_fd, "rb")) == NULL )
359 {
360 PERROR("Could not allocate decompression state for initrd");
361 goto error_out;
362 }
363 }
365 if ( mlock(&st_ctxt, sizeof(st_ctxt) ) )
366 {
367 PERROR("Unable to mlock ctxt");
368 return 1;
369 }
371 op.cmd = DOM0_GETDOMAININFO;
372 op.u.getdomaininfo.domain = (domid_t)domid;
373 op.u.getdomaininfo.exec_domain = 0;
374 op.u.getdomaininfo.ctxt = ctxt;
375 if ( (do_dom0_op(xc_handle, &op) < 0) ||
376 ((u16)op.u.getdomaininfo.domain != domid) )
377 {
378 PERROR("Could not get info on domain");
379 goto error_out;
380 }
381 if ( !(op.u.getdomaininfo.flags & DOMFLAGS_PAUSED) ||
382 (ctxt->pt_base != 0) )
383 {
384 ERROR("Domain is already constructed");
385 goto error_out;
386 }
388 if ( setup_guestos(xc_handle, domid, image, image_size,
389 initrd_gfd, initrd_size, nr_pages,
390 &vstartinfo_start, &vkern_entry,
391 ctxt, cmdline,
392 op.u.getdomaininfo.shared_info_frame,
393 control_evtchn, flags) < 0 )
394 {
395 ERROR("Error constructing guest OS");
396 goto error_out;
397 }
399 if ( initrd_fd >= 0 )
400 close(initrd_fd);
401 if ( initrd_gfd )
402 gzclose(initrd_gfd);
403 if ( image != NULL )
404 free(image);
406 ctxt->flags = 0;
408 /*
409 * Initial register values:
410 * DS,ES,FS,GS = FLAT_GUESTOS_DS
411 * CS:EIP = FLAT_GUESTOS_CS:start_pc
412 * SS:ESP = FLAT_GUESTOS_DS:start_stack
413 * ESI = start_info
414 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
415 * EFLAGS = IF | 2 (bit 1 is reserved and should always be 1)
416 */
417 ctxt->cpu_ctxt.ds = FLAT_GUESTOS_DS;
418 ctxt->cpu_ctxt.es = FLAT_GUESTOS_DS;
419 ctxt->cpu_ctxt.fs = FLAT_GUESTOS_DS;
420 ctxt->cpu_ctxt.gs = FLAT_GUESTOS_DS;
421 ctxt->cpu_ctxt.ss = FLAT_GUESTOS_DS;
422 ctxt->cpu_ctxt.cs = FLAT_GUESTOS_CS;
423 ctxt->cpu_ctxt.eip = vkern_entry;
424 ctxt->cpu_ctxt.esp = vstartinfo_start + 2*PAGE_SIZE;
425 ctxt->cpu_ctxt.esi = vstartinfo_start;
426 ctxt->cpu_ctxt.eflags = (1<<9) | (1<<2);
428 /* FPU is set up to default initial state. */
429 memset(ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));
431 /* Virtual IDT is empty at start-of-day. */
432 for ( i = 0; i < 256; i++ )
433 {
434 ctxt->trap_ctxt[i].vector = i;
435 ctxt->trap_ctxt[i].cs = FLAT_GUESTOS_CS;
436 }
437 ctxt->fast_trap_idx = 0;
439 /* No LDT. */
440 ctxt->ldt_ents = 0;
442 /* Use the default Xen-provided GDT. */
443 ctxt->gdt_ents = 0;
445 /* Ring 1 stack is the initial stack. */
446 ctxt->guestos_ss = FLAT_GUESTOS_DS;
447 ctxt->guestos_esp = vstartinfo_start + 2*PAGE_SIZE;
449 /* No debugging. */
450 memset(ctxt->debugreg, 0, sizeof(ctxt->debugreg));
452 /* No callback handlers. */
453 ctxt->event_callback_cs = FLAT_GUESTOS_CS;
454 ctxt->event_callback_eip = 0;
455 ctxt->failsafe_callback_cs = FLAT_GUESTOS_CS;
456 ctxt->failsafe_callback_eip = 0;
458 memset( &launch_op, 0, sizeof(launch_op) );
460 launch_op.u.builddomain.domain = (domid_t)domid;
461 launch_op.u.builddomain.ctxt = ctxt;
463 launch_op.cmd = DOM0_BUILDDOMAIN;
464 rc = do_dom0_op(xc_handle, &launch_op);
466 return rc;
468 error_out:
469 if ( initrd_gfd != NULL )
470 gzclose(initrd_gfd);
471 else if ( initrd_fd >= 0 )
472 close(initrd_fd);
473 if ( image != NULL )
474 free(image);
476 return -1;
477 }
479 static inline int is_loadable_phdr(Elf_Phdr *phdr)
480 {
481 return ((phdr->p_type == PT_LOAD) &&
482 ((phdr->p_flags & (PF_W|PF_X)) != 0));
483 }
485 static int parseelfimage(char *elfbase,
486 unsigned long elfsize,
487 struct domain_setup_info *dsi)
488 {
489 Elf_Ehdr *ehdr = (Elf_Ehdr *)elfbase;
490 Elf_Phdr *phdr;
491 Elf_Shdr *shdr;
492 unsigned long kernstart = ~0UL, kernend=0UL;
493 char *shstrtab, *guestinfo=NULL, *p;
494 int h;
496 if ( !IS_ELF(*ehdr) )
497 {
498 ERROR("Kernel image does not have an ELF header.");
499 return -EINVAL;
500 }
502 if ( (ehdr->e_phoff + (ehdr->e_phnum * ehdr->e_phentsize)) > elfsize )
503 {
504 ERROR("ELF program headers extend beyond end of image.");
505 return -EINVAL;
506 }
508 if ( (ehdr->e_shoff + (ehdr->e_shnum * ehdr->e_shentsize)) > elfsize )
509 {
510 ERROR("ELF section headers extend beyond end of image.");
511 return -EINVAL;
512 }
514 /* Find the section-header strings table. */
515 if ( ehdr->e_shstrndx == SHN_UNDEF )
516 {
517 ERROR("ELF image has no section-header strings table (shstrtab).");
518 return -EINVAL;
519 }
520 shdr = (Elf_Shdr *)(elfbase + ehdr->e_shoff +
521 (ehdr->e_shstrndx*ehdr->e_shentsize));
522 shstrtab = elfbase + shdr->sh_offset;
524 /* Find the special '__xen_guest' section and check its contents. */
525 for ( h = 0; h < ehdr->e_shnum; h++ )
526 {
527 shdr = (Elf_Shdr *)(elfbase + ehdr->e_shoff + (h*ehdr->e_shentsize));
528 if ( strcmp(&shstrtab[shdr->sh_name], "__xen_guest") != 0 )
529 continue;
531 guestinfo = elfbase + shdr->sh_offset;
533 if ( (strstr(guestinfo, "LOADER=generic") == NULL) &&
534 (strstr(guestinfo, "GUEST_OS=linux") == NULL) )
535 {
536 ERROR("Will only load images built for the generic loader "
537 "or Linux images");
538 ERROR("Actually saw: '%s'", guestinfo);
539 return -EINVAL;
540 }
542 if ( (strstr(guestinfo, "XEN_VER=2.0") == NULL) )
543 {
544 ERROR("Will only load images built for Xen v2.0");
545 ERROR("Actually saw: '%s'", guestinfo);
546 return -EINVAL;
547 }
549 break;
550 }
551 if ( guestinfo == NULL )
552 {
553 ERROR("Not a Xen-ELF image: '__xen_guest' section not found.");
554 return -EINVAL;
555 }
557 for ( h = 0; h < ehdr->e_phnum; h++ )
558 {
559 phdr = (Elf_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
560 if ( !is_loadable_phdr(phdr) )
561 continue;
562 if ( phdr->p_vaddr < kernstart )
563 kernstart = phdr->p_vaddr;
564 if ( (phdr->p_vaddr + phdr->p_memsz) > kernend )
565 kernend = phdr->p_vaddr + phdr->p_memsz;
566 }
568 if ( (kernstart > kernend) ||
569 (ehdr->e_entry < kernstart) ||
570 (ehdr->e_entry > kernend) )
571 {
572 ERROR("Malformed ELF image.");
573 return -EINVAL;
574 }
576 dsi->v_start = kernstart;
577 if ( (p = strstr(guestinfo, "VIRT_BASE=")) != NULL )
578 dsi->v_start = strtoul(p+10, &p, 0);
580 if ( (p = strstr(guestinfo, "PT_MODE_WRITABLE")) != NULL )
581 dsi->use_writable_pagetables = 1;
583 if ( (p = strstr(guestinfo, "BSD_SYMTAB")) != NULL )
584 dsi->load_bsd_symtab = 1;
586 dsi->v_kernstart = kernstart;
587 dsi->v_kernend = kernend;
588 dsi->v_kernentry = ehdr->e_entry;
590 dsi->v_end = dsi->v_kernend;
592 return 0;
593 }
595 static int
596 loadelfimage(
597 char *elfbase, int xch, u32 dom, unsigned long *parray,
598 unsigned long vstart)
599 {
600 Elf_Ehdr *ehdr = (Elf_Ehdr *)elfbase;
601 Elf_Phdr *phdr;
602 int h;
604 char *va;
605 unsigned long pa, done, chunksz;
607 for ( h = 0; h < ehdr->e_phnum; h++ )
608 {
609 phdr = (Elf_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize));
610 if ( !is_loadable_phdr(phdr) )
611 continue;
613 for ( done = 0; done < phdr->p_filesz; done += chunksz )
614 {
615 pa = (phdr->p_vaddr + done) - vstart;
616 va = xc_map_foreign_range(
617 xch, dom, PAGE_SIZE, PROT_WRITE, parray[pa>>PAGE_SHIFT]);
618 chunksz = phdr->p_filesz - done;
619 if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
620 chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
621 memcpy(va + (pa & (PAGE_SIZE-1)),
622 elfbase + phdr->p_offset + done, chunksz);
623 munmap(va, PAGE_SIZE);
624 }
626 for ( ; done < phdr->p_memsz; done += chunksz )
627 {
628 pa = (phdr->p_vaddr + done) - vstart;
629 va = xc_map_foreign_range(
630 xch, dom, PAGE_SIZE, PROT_WRITE, parray[pa>>PAGE_SHIFT]);
631 chunksz = phdr->p_memsz - done;
632 if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
633 chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
634 memset(va + (pa & (PAGE_SIZE-1)), 0, chunksz);
635 munmap(va, PAGE_SIZE);
636 }
637 }
639 return 0;
640 }
642 #define ELFROUND (ELFSIZE / 8)
644 static int
645 loadelfsymtab(
646 char *elfbase, int xch, u32 dom, unsigned long *parray,
647 struct domain_setup_info *dsi)
648 {
649 Elf_Ehdr *ehdr = (Elf_Ehdr *)elfbase, *sym_ehdr;
650 Elf_Shdr *shdr;
651 unsigned long maxva, symva;
652 char *p;
653 int h, i;
655 p = malloc(sizeof(int) + sizeof(Elf_Ehdr) +
656 ehdr->e_shnum * sizeof(Elf_Shdr));
657 if (p == NULL)
658 return 0;
660 maxva = (dsi->v_kernend + ELFROUND - 1) & ~(ELFROUND - 1);
661 symva = maxva;
662 maxva += sizeof(int);
663 dsi->symtab_addr = maxva;
664 dsi->symtab_len = 0;
665 maxva += sizeof(Elf_Ehdr) + ehdr->e_shnum * sizeof(Elf_Shdr);
666 maxva = (maxva + ELFROUND - 1) & ~(ELFROUND - 1);
668 shdr = (Elf_Shdr *)(p + sizeof(int) + sizeof(Elf_Ehdr));
669 memcpy(shdr, elfbase + ehdr->e_shoff, ehdr->e_shnum * sizeof(Elf_Shdr));
671 for ( h = 0; h < ehdr->e_shnum; h++ )
672 {
673 if ( shdr[h].sh_type == SHT_STRTAB )
674 {
675 /* Look for a strtab @i linked to symtab @h. */
676 for ( i = 0; i < ehdr->e_shnum; i++ )
677 if ( (shdr[i].sh_type == SHT_SYMTAB) &&
678 (shdr[i].sh_link == h) )
679 break;
680 /* Skip symtab @h if we found no corresponding strtab @i. */
681 if ( i == ehdr->e_shnum )
682 {
683 shdr[h].sh_offset = 0;
684 continue;
685 }
686 }
688 if ( (shdr[h].sh_type == SHT_STRTAB) ||
689 (shdr[h].sh_type == SHT_SYMTAB) )
690 {
691 if ( parray != NULL )
692 xc_map_memcpy(maxva, elfbase + shdr[h].sh_offset, shdr[h].sh_size,
693 xch, dom, parray, dsi->v_start);
695 /* Mangled to be based on ELF header location. */
696 shdr[h].sh_offset = maxva - dsi->symtab_addr;
698 dsi->symtab_len += shdr[h].sh_size;
699 maxva += shdr[h].sh_size;
700 maxva = (maxva + ELFROUND - 1) & ~(ELFROUND - 1);
701 }
703 shdr[h].sh_name = 0; /* Name is NULL. */
704 }
706 if ( dsi->symtab_len == 0 )
707 {
708 dsi->symtab_addr = 0;
709 goto out;
710 }
712 if ( parray != NULL )
713 {
714 *(int *)p = maxva - dsi->symtab_addr;
715 sym_ehdr = (Elf_Ehdr *)(p + sizeof(int));
716 memcpy(sym_ehdr, ehdr, sizeof(Elf_Ehdr));
717 sym_ehdr->e_phoff = 0;
718 sym_ehdr->e_shoff = sizeof(Elf_Ehdr);
719 sym_ehdr->e_phentsize = 0;
720 sym_ehdr->e_phnum = 0;
721 sym_ehdr->e_shstrndx = SHN_UNDEF;
723 /* Copy total length, crafted ELF header and section header table */
724 xc_map_memcpy(symva, p, sizeof(int) + sizeof(Elf_Ehdr) +
725 ehdr->e_shnum * sizeof(Elf_Shdr), xch, dom, parray,
726 dsi->v_start);
727 }
729 dsi->symtab_len = maxva - dsi->symtab_addr;
730 dsi->v_end = round_pgup(maxva);
732 out:
733 if ( p != NULL )
734 free(p);
736 return 0;
737 }