direct-io.hg

view tools/libxc/xc_plan9_build.c @ 3326:f36de623daa0

bitkeeper revision 1.1159.187.81 (41cc934bUot13IO0-1LsmhYl0sW9Zw)

Add plan9 builder support.
author iap10@labyrinth.cl.cam.ac.uk
date Fri Dec 24 22:08:11 2004 +0000 (2004-12-24)
parents
children 6b1aa25ff284
line source
1 /******************************************************************************
2 * xc_plan9_build.c
3 * derived from xc_linux_build.c
4 */
6 #include "xc_private.h"
8 #include <zlib.h>
10 #define DEBUG 1
11 #ifdef DEBUG
12 #define DPRINTF(x) printf x; fflush(stdout);
13 #else
14 #define DPRINTF(x)
15 #endif
17 #include "plan9a.out.h"
19 /* really TOS which means stack starts at 0x2000, and uses page 1*/
20 #define STACKPAGE 2
21 struct Exec header, origheader;
23 typedef struct page {
24 char data[PAGE_SIZE];
25 } PAGE;
28 int
29 memcpy_toguest(int xc_handle, u32 dom, void *v, int size,
30 unsigned long *page_array, unsigned int to_page)
31 {
32 int ret;
33 unsigned char *cp = v;
34 unsigned int whichpage;
35 unsigned char *vaddr;
37 // DPRINTF(("memcpy_to_guest: to_page 0x%x, count %d\n", to_page, size));
38 for (ret = 0, whichpage = to_page; size > 0;
39 whichpage++, size -= PAGE_SIZE, cp += PAGE_SIZE) {
41 // DPRINTF (("map_pfn_writeable(%p, 0x%lx)\n", pm_handle,
42 // page_array[whichpage]));
43 vaddr = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
44 PROT_READ | PROT_WRITE,
45 page_array[whichpage]);
46 // DPRINTF (("vaddr is %p\n", vaddr));
47 if (vaddr == NULL) {
48 ret = -1;
49 ERROR("Couldn't map guest memory");
50 goto out;
51 }
52 // DPRINTF (("copy %p to %p, count 0x%x\n", cp, vaddr, 4096));
53 memcpy(vaddr, cp, 4096);
54 munmap(vaddr, PAGE_SIZE);
55 // DPRINTF (("Did %ud'th pages\n", whichpage));
56 }
57 out:
58 return ret;
59 }
61 /* this is a function which can go away. It dumps a hunk of
62 * guest pages to a file (/tmp/dumpit); handy for debugging
63 * your image builder.
64 * Xen guys, nuke this if you wish.
65 */
66 void
67 dumpit(int xc_handle, u32 dom,
68 int start_page, int tot, unsigned long *page_array)
69 {
70 int i, ofd;
71 unsigned char *vaddr;
73 ofd = open("/tmp/dumpit", O_RDWR);
74 for (i = start_page; i < tot; i++) {
75 vaddr = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
76 PROT_READ | PROT_WRITE,
77 page_array[i]);
78 if (!vaddr) {
79 fprintf(stderr, "Page %d\n", i);
80 perror("shit");
81 read(0, &i, 1);
82 return;
83 }
84 write(ofd, vaddr, 4096);
85 munmap(vaddr, PAGE_SIZE);
86 }
87 }
88 int
89 blah(char *b)
90 {
91 fprintf(stderr, "Error in xc_plan9_build!\n");
92 perror(b);
93 return errno;
94 }
96 /* swap bytes. For plan 9 headers */
97 void
98 swabby(unsigned long *s, char *name)
99 {
100 unsigned long it;
101 it = ((*s & 0xff000000) >> 24) | ((*s & 0xff0000) >> 8) |
102 ((*s & 0xff00) << 8) | ((*s & 0xff) << 24);
103 DPRINTF(("Item %s is 0x%lx\n", name, it));
104 *s = it;
105 }
107 void
108 plan9header(Exec * header)
109 {
110 /* header is big-endian */
111 swabby(&header->magic, "magic");
112 swabby(&header->text, "text");
113 swabby(&header->data, "data");
114 swabby(&header->bss, "bss");
115 swabby(&header->syms, "syms");
116 swabby(&header->entry, "entry");
117 swabby(&header->spsz, "spsz");
118 swabby(&header->pcsz, "pcsz");
120 }
122 static int
123 loadp9image(gzFile kernel_gfd, int xc_handle, u32 dom,
124 unsigned long *page_array,
125 unsigned long tot_pages, unsigned long *virt_load_addr,
126 unsigned long *ksize, unsigned long *symtab_addr,
127 unsigned long *symtab_len,
128 unsigned long *first_data_page, unsigned long *pdb_page);
130 #define P9ROUND (P9SIZE / 8)
132 #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
133 #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
135 static long
136 get_tot_pages(int xc_handle, u32 domid)
137 {
138 dom0_op_t op;
139 op.cmd = DOM0_GETDOMAININFO;
140 op.u.getdomaininfo.domain = (domid_t) domid;
141 op.u.getdomaininfo.ctxt = NULL;
142 return (do_dom0_op(xc_handle, &op) < 0) ?
143 -1 : op.u.getdomaininfo.tot_pages;
144 }
146 static int
147 get_pfn_list(int xc_handle,
148 u32 domid, unsigned long *pfn_buf, unsigned long max_pfns)
149 {
150 dom0_op_t op;
151 int ret;
152 op.cmd = DOM0_GETMEMLIST;
153 op.u.getmemlist.domain = (domid_t) domid;
154 op.u.getmemlist.max_pfns = max_pfns;
155 op.u.getmemlist.buffer = pfn_buf;
157 if (mlock(pfn_buf, max_pfns * sizeof (unsigned long)) != 0)
158 return -1;
160 ret = do_dom0_op(xc_handle, &op);
162 (void) munlock(pfn_buf, max_pfns * sizeof (unsigned long));
164 #if 0
165 #ifdef DEBUG
166 DPRINTF(("Ret for get_pfn_list is %d\n", ret));
167 if (ret >= 0) {
168 int i, j;
169 for (i = 0; i < op.u.getmemlist.num_pfns; i += 16) {
170 fprintf(stderr, "0x%x: ", i);
171 for (j = 0; j < 16; j++)
172 fprintf(stderr, "0x%lx ", pfn_buf[i + j]);
173 fprintf(stderr, "\n");
174 }
175 }
176 #endif
177 #endif
178 return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
179 }
181 static int
182 setup_guestos(int xc_handle,
183 u32 dom,
184 gzFile kernel_gfd,
185 unsigned long tot_pages,
186 unsigned long *virt_startinfo_addr,
187 unsigned long *virt_load_addr,
188 full_execution_context_t * ctxt,
189 const char *cmdline,
190 unsigned long shared_info_frame,
191 unsigned int control_evtchn,
192 int flags)
193 {
194 l1_pgentry_t *vl1e = NULL;
195 l2_pgentry_t *vl2tab = NULL, *vl2e = NULL;
196 unsigned long *cpage_array = NULL;
197 unsigned long *pte_array = NULL;
198 unsigned long l2tab;
199 unsigned long l1tab;
200 unsigned long count;
201 unsigned long symtab_addr = 0, symtab_len = 0;
202 start_info_t *start_info;
203 shared_info_t *shared_info;
204 unsigned long ksize;
205 mmu_t *mmu = NULL;
206 int i;
207 unsigned long first_page_after_kernel,
208 first_data_page,
209 page_array_page;
210 unsigned long cpu0pdb, cpu0pte, cpu0ptelast;
211 unsigned long /*last_pfn, */ tot_pte_pages;
213 DPRINTF(("tot pages is %ld\n", tot_pages));
214 if ((cpage_array = malloc(tot_pages * sizeof (unsigned long))) == NULL) {
215 PERROR("Could not allocate cpage array");
216 goto error_out;
217 }
219 if (get_pfn_list(xc_handle, dom, cpage_array, tot_pages) != tot_pages) {
220 PERROR("Could not get the page frame list");
221 goto error_out;
222 }
224 for (i = 0; i < 64; i++)
225 DPRINTF(("First %d page is 0x%lx\n", i, cpage_array[i]));
227 tot_pte_pages = tot_pages >> 10;
228 DPRINTF(("Page range is 0 to 0x%lx, which requires 0x%lx pte pages\n",
229 tot_pte_pages, tot_pte_pages));
231 if (loadp9image(kernel_gfd, xc_handle, dom, cpage_array, tot_pages,
232 virt_load_addr, &ksize, &symtab_addr, &symtab_len,
233 &first_data_page, &first_page_after_kernel))
234 goto error_out;
235 DPRINTF(("First data page is 0x%lx\n", first_data_page));
236 DPRINTF(("First page after kernel is 0x%lx\n",
237 first_page_after_kernel));
239 /*
240 NEED TO INCREMENT first page after kernel by:
241 + 1 (pdb)
242 + tot_pte_pages (pte)
243 + tot_pte_pages (page_array)
244 */
245 /* SO, have to copy the first kernel pages pfns right into the
246 * page_array, then do identity maps for the rest.
247 */
248 DPRINTF(("mapped kernel pages\n"));
250 /* now loop over all ptes and store into the page_array, so as
251 * to get the identity map.
252 */
253 if ((pte_array =
254 malloc(tot_pte_pages * 1024 * sizeof (unsigned long))) == NULL) {
255 PERROR("Could not allocate pte array");
256 goto error_out;
257 }
259 /* plan 9 on startup expects a "l2" (xen parlance) at 0x2000,
260 * this "l2" should have one PTE pointer for a va of 0x80000000.
261 * and an l1 (PTEs to you) at 0x3000. (physical).
262 * the PTEs should map the first 4M of memory.
263 */
264 /* get a physical address for the L2. This means take the PFN and
265 * shift left.
266 */
267 /* this terminology is plan 9 terminology.
268 * pdb is essentially the Xen L2. 'Page Directory Block'?
269 * I need to ask JMK.
270 * cpupte is the pte array.
271 * Plan 9 counts on these being set up for cpu0.
272 * SO: cpu0pdb (Xen L2)
273 * and cpupte (Xen L1)
274 */
275 /* cpu0pdb is right after kernel */
276 cpu0pdb = first_page_after_kernel;
277 /* cpu0pte comes right after cpu0pdb */
278 cpu0pte = cpu0pdb + 1;
279 /* number of the past cpu0pte page */
280 cpu0ptelast = cpu0pte + tot_pte_pages - 1;
281 /* first page of the page array (mfn) */
282 page_array_page = cpu0ptelast + 1;
284 DPRINTF(("cpu0pdb 0x%lx, cpu0pte 0x%lx cpu0ptelast 0x%lx\n", cpu0pdb,
285 cpu0pte, cpu0ptelast));
286 l2tab = cpage_array[cpu0pdb] << PAGE_SHIFT;
287 DPRINTF(("l2tab 0x%lx\n", l2tab));
288 ctxt->pt_base = l2tab;
290 /* get a physical address for the L1. This means take the PFN and
291 * shift left.
292 */
293 l1tab = cpage_array[cpu0pte] << PAGE_SHIFT;
294 DPRINTF(("l1tab 0x%lx\n", l1tab));
295 if ((mmu = init_mmu_updates(xc_handle, dom)) == NULL)
296 goto error_out;
297 DPRINTF(("now map in l2tab\n"));
299 /* Initialise the page tables. */
300 /* mmap in the l2tab */
301 if ((vl2tab = xc_map_foreign_range(xc_handle, dom,
302 PAGE_SIZE, PROT_READ | PROT_WRITE,
303 l2tab >> PAGE_SHIFT)) == NULL)
304 goto error_out;
305 DPRINTF(("vl2tab 0x%p\n", vl2tab));
306 /* now we have the cpu0pdb for the kernel, starting at 0x2000,
307 * so we can plug in the physical pointer to the 0x3000 pte
308 */
309 /* zero it */
310 memset(vl2tab, 0, PAGE_SIZE);
311 /* get a pointer in the l2tab for the virt_load_addr */
312 DPRINTF(("&vl2tab[l2_table_offset(*virt_load_addr)] is 0x%p[0x%lx]\n",
313 &vl2tab[l2_table_offset(*virt_load_addr)],
314 l2_table_offset(*virt_load_addr)));
316 vl2e = &vl2tab[l2_table_offset(*virt_load_addr)];
318 /* OK, for all the available PTE, set the PTE pointer up */
319 DPRINTF(("For i = %ld to %ld ...\n", cpu0pte, cpu0ptelast));
320 for (i = cpu0pte; i <= cpu0ptelast; i++) {
321 DPRINTF(("Index %d Set %p to 0x%lx\n", i, vl2e,
322 (cpage_array[i] << PAGE_SHIFT) | L2_PROT));
323 *vl2e++ = (cpage_array[i] << PAGE_SHIFT) | L2_PROT;
324 }
326 /* unmap it ... */
327 munmap(vl2tab, PAGE_SIZE);
329 /* for the pages from virt_load_pointer to the end of this
330 * set of PTEs, map in the PFN for that VA
331 */
332 for (vl1e = (l1_pgentry_t *) pte_array, count = 0;
333 count < tot_pte_pages * 1024; count++, vl1e++) {
335 *vl1e = cpage_array[count];
336 if (!cpage_array[count])
337 continue;
338 /* set in the PFN for this entry */
339 *vl1e = (cpage_array[count] << PAGE_SHIFT) | L1_PROT;
340 /*
341 DPRINTF (("vl1e # %d 0x%lx gets 0x%lx\n",
342 count, vl1e, *vl1e));
343 */
344 if ((count >= cpu0pdb) && (count <= cpu0ptelast)) {
345 //DPRINTF((" Fix up page %d as it is in pte ville: ", count));
346 *vl1e &= ~_PAGE_RW;
347 DPRINTF(("0x%lx\n", *vl1e));
348 }
349 if ((count >= (0x100000 >> 12))
350 && (count < (first_data_page >> 12))) {
351 //DPRINTF((" Fix up page %d as it is in text ", count));
352 *vl1e &= ~_PAGE_RW;
353 //DPRINTF (("0x%lx\n", *vl1e));
354 }
355 }
356 /* special thing. Pre-map the shared info page */
357 vl1e = &pte_array[2];
358 *vl1e = (shared_info_frame << PAGE_SHIFT) | L1_PROT;
359 DPRINTF(("v1l1 %p, has value 0x%lx\n", vl1e, *(unsigned long *) vl1e));
360 /* another special thing. VA 80005000 has to point to 80006000 */
361 /* this is a Plan 9 thing -- the 'mach' pointer */
362 /* 80005000 is the mach pointer per-cpu, and the actual
363 * mach pointers are 80006000, 80007000 etc.
364 */
365 vl1e = &pte_array[5];
366 *vl1e = (cpage_array[6] << PAGE_SHIFT) | L1_PROT;
368 /* OK, it's all set up, copy it in */
369 memcpy_toguest(xc_handle, dom, pte_array,
370 (tot_pte_pages * 1024 * sizeof (unsigned long) /**/),
371 cpage_array, cpu0pte);
373 /* We really need to have the vl1tab unmapped or the add_mmu_update
374 * below will fail bigtime.
375 */
376 /* Xen guys: remember my errors on domain exit? Something I'm doing
377 * wrong in here? We never did find out ...
378 */
379 /* get rid of the entries we can not use ... */
380 memcpy_toguest(xc_handle, dom, cpage_array,
381 (tot_pte_pages * 1024 * sizeof (unsigned long) /**/),
382 cpage_array, page_array_page);
383 /* last chance to dump all of memory */
384 // dumpit(xc_handle, dom, 0 /*0x100000>>12*/, tot_pages, cpage_array) ;
385 /*
386 * Pin down l2tab addr as page dir page - causes hypervisor to provide
387 * correct protection for the page
388 */
389 if (add_mmu_update(xc_handle, mmu,
390 l2tab | MMU_EXTENDED_COMMAND, MMUEXT_PIN_L2_TABLE))
391 goto error_out;
393 for (count = 0; count < tot_pages; count++) {
394 /*
395 DPRINTF (("add_mmu_update(0x%x, 0x%x, 0x%x, %d)\n", xc_handle, mmu,
396 (cpage_array[count]
397 << PAGE_SHIFT) |
398 MMU_MACHPHYS_UPDATE,
399 count));
400 */
401 if (add_mmu_update(xc_handle, mmu,
402 (cpage_array[count] << PAGE_SHIFT) |
403 MMU_MACHPHYS_UPDATE, count))
404 goto error_out;
405 //DPRINTF(("Do the next one\n"));
406 }
407 /*
408 */
410 //dumpit(pm_handle, 3, 4, page_array);
411 /* put the virt_startinfo_addr at KZERO */
412 /* just hard-code for now */
413 *virt_startinfo_addr = 0x80000000;
415 DPRINTF(("virt_startinfo_addr = 0x%lx\n", *virt_startinfo_addr));
416 start_info = xc_map_foreign_range(xc_handle, dom,
417 PAGE_SIZE, PROT_READ | PROT_WRITE,
418 cpage_array[0]);
419 DPRINTF(("startinfo = 0x%p\n", start_info));
420 DPRINTF(("shared_info_frame is %lx\n", shared_info_frame));
421 memset(start_info, 0, sizeof (*start_info));
422 start_info->pt_base = 0x80000000 | cpu0pdb << PAGE_SHIFT;
423 start_info->mfn_list = 0x80000000 | (page_array_page) << PAGE_SHIFT;
424 DPRINTF(("mfn_list 0x%lx\n", start_info->mfn_list));
425 start_info->mod_start = 0;
426 start_info->mod_len = 0;
427 start_info->nr_pages = tot_pte_pages * 1024;
428 start_info->nr_pt_frames = tot_pte_pages + 1;
429 start_info->shared_info = shared_info_frame;
430 start_info->flags = 0;
431 DPRINTF((" control event channel is %d\n", control_evtchn));
432 start_info->domain_controller_evtchn = control_evtchn;
433 strncpy(start_info->cmd_line, cmdline, MAX_CMDLINE);
434 start_info->cmd_line[MAX_CMDLINE - 1] = '\0';
435 munmap(start_info, PAGE_SIZE);
437 DPRINTF(("done setting up start_info\n"));
438 DPRINTF(("shared_info_frame = 0x%lx\n", shared_info_frame));
439 /* shared_info page starts its life empty. */
441 shared_info = xc_map_foreign_range(xc_handle, dom,
442 PAGE_SIZE, PROT_READ | PROT_WRITE,
443 shared_info_frame);
444 memset(shared_info, 0, PAGE_SIZE);
445 /* Mask all upcalls... */
446 DPRINTF(("mask all upcalls\n"));
447 for (i = 0; i < MAX_VIRT_CPUS; i++)
448 shared_info->vcpu_data[i].evtchn_upcall_mask = 1;
449 munmap(shared_info, PAGE_SIZE);
451 /* Send the page update requests down to the hypervisor. */
452 DPRINTF(("send page update reqs down.\n"));
453 if (finish_mmu_updates(xc_handle, mmu))
454 goto error_out;
456 //DPRINTF (("call dumpit.\n"));
457 //dumpit(pm_handle, 0x100000>>12, tot_pages, page_array) ;
458 //dumpit (pm_handle, 2, 0x100, page_array);
459 free(mmu);
461 /* we don't bother freeing anything at this point --
462 * we're exiting and it is pointless
463 */
464 return 0;
466 error_out:
467 /* oh well we still free some things -- I oughtta nuke this */
468 if (mmu != NULL)
469 free(mmu);
470 ;
471 return -1;
472 }
474 int
475 xc_plan9_build(int xc_handle,
476 u32 domid,
477 const char *image_name,
478 const char *cmdline,
479 unsigned int control_evtchn, unsigned long flags)
480 {
481 dom0_op_t launch_op, op;
482 unsigned long load_addr;
483 long tot_pages;
484 int kernel_fd = -1;
485 gzFile kernel_gfd = NULL;
486 int rc, i;
487 full_execution_context_t st_ctxt, *ctxt = &st_ctxt;
488 unsigned long virt_startinfo_addr;
490 if ((tot_pages = get_tot_pages(xc_handle, domid)) < 0) {
491 PERROR("Could not find total pages for domain");
492 return 1;
493 }
494 DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
496 kernel_fd = open(image_name, O_RDONLY);
497 if (kernel_fd < 0) {
498 PERROR("Could not open kernel image");
499 return 1;
500 }
502 if ((kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL) {
503 PERROR("Could not allocate decompression state for state file");
504 close(kernel_fd);
505 return 1;
506 }
508 DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
509 if (mlock(&st_ctxt, sizeof (st_ctxt))) {
510 PERROR("Unable to mlock ctxt");
511 return 1;
512 }
514 op.cmd = DOM0_GETDOMAININFO;
515 op.u.getdomaininfo.domain = (domid_t) domid;
516 op.u.getdomaininfo.ctxt = ctxt;
517 if ((do_dom0_op(xc_handle, &op) < 0) ||
518 ((u32) op.u.getdomaininfo.domain != domid)) {
519 PERROR("Could not get info on domain");
520 goto error_out;
521 }
522 DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
524 if (!(op.u.getdomaininfo.flags & DOMFLAGS_PAUSED)
525 || (op.u.getdomaininfo.ctxt->pt_base != 0)) {
526 ERROR("Domain is already constructed");
527 goto error_out;
528 }
530 DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
531 if (setup_guestos(xc_handle, domid, kernel_gfd, tot_pages,
532 &virt_startinfo_addr,
533 &load_addr, &st_ctxt, cmdline,
534 op.u.getdomaininfo.shared_info_frame,
535 control_evtchn, flags) < 0) {
536 ERROR("Error constructing guest OS");
537 goto error_out;
538 }
540 /* leave the leak in here for now
541 if ( kernel_fd >= 0 )
542 close(kernel_fd);
543 if( kernel_gfd )
544 gzclose(kernel_gfd);
545 */
546 ctxt->flags = 0;
548 /*
549 * Initial register values:
550 * DS,ES,FS,GS = FLAT_GUESTOS_DS
551 * CS:EIP = FLAT_GUESTOS_CS:start_pc
552 * SS:ESP = FLAT_GUESTOS_DS:start_stack
553 * ESI = start_info
554 * [EAX,EBX,ECX,EDX,EDI,EBP are zero]
555 * EFLAGS = IF | 2 (bit 1 is reserved and should always be 1)
556 */
557 ctxt->cpu_ctxt.ds = FLAT_GUESTOS_DS;
558 ctxt->cpu_ctxt.es = FLAT_GUESTOS_DS;
559 ctxt->cpu_ctxt.fs = FLAT_GUESTOS_DS;
560 ctxt->cpu_ctxt.gs = FLAT_GUESTOS_DS;
561 ctxt->cpu_ctxt.ss = FLAT_GUESTOS_DS;
562 ctxt->cpu_ctxt.cs = FLAT_GUESTOS_CS;
563 ctxt->cpu_ctxt.eip = load_addr;
564 ctxt->cpu_ctxt.eip = 0x80100020;
565 /* put stack at top of second page */
566 ctxt->cpu_ctxt.esp = 0x80000000 + (STACKPAGE << PAGE_SHIFT);
568 /* why is this set? */
569 ctxt->cpu_ctxt.esi = ctxt->cpu_ctxt.esp;
570 ctxt->cpu_ctxt.eflags = (1 << 9) | (1 << 2);
572 /* FPU is set up to default initial state. */
573 memset(ctxt->fpu_ctxt, 0, sizeof (ctxt->fpu_ctxt));
575 /* Virtual IDT is empty at start-of-day. */
576 for (i = 0; i < 256; i++) {
577 ctxt->trap_ctxt[i].vector = i;
578 ctxt->trap_ctxt[i].cs = FLAT_GUESTOS_CS;
579 }
580 ctxt->fast_trap_idx = 0;
582 /* No LDT. */
583 ctxt->ldt_ents = 0;
585 /* Use the default Xen-provided GDT. */
586 ctxt->gdt_ents = 0;
588 /* Ring 1 stack is the initial stack. */
589 /* put stack at top of second page */
590 ctxt->guestos_ss = FLAT_GUESTOS_DS;
591 ctxt->guestos_esp = ctxt->cpu_ctxt.esp;
593 /* No debugging. */
594 memset(ctxt->debugreg, 0, sizeof (ctxt->debugreg));
596 /* No callback handlers. */
597 ctxt->event_callback_cs = FLAT_GUESTOS_CS;
598 ctxt->event_callback_eip = 0;
599 ctxt->failsafe_callback_cs = FLAT_GUESTOS_CS;
600 ctxt->failsafe_callback_eip = 0;
602 memset(&launch_op, 0, sizeof (launch_op));
604 launch_op.u.builddomain.domain = (domid_t) domid;
605 // launch_op.u.builddomain.num_vifs = 1;
606 launch_op.u.builddomain.ctxt = ctxt;
607 launch_op.cmd = DOM0_BUILDDOMAIN;
608 rc = do_dom0_op(xc_handle, &launch_op);
610 fprintf(stderr, "RC is %d\n", rc);
611 return rc;
613 error_out:
614 if (kernel_fd >= 0)
615 close(kernel_fd);
616 if (kernel_gfd)
617 gzclose(kernel_gfd);
619 return -1;
620 }
622 /*
623 * Plan 9 memory layout (initial)
624 * ----------------
625 * | info from xen| @0
626 * ----------------
627 * | stack |
628 * ----------------<--- page 2
629 * | empty |
630 * ---------------<---- page 5 MACHADDR (always points to machp[cpuno]
631 * | aliased |
632 * ---------------<----- page 6 CPU0MACH
633 * | CPU0MACH |
634 * ----------------
635 * | empty |
636 * ---------------- *virt_load_addr = ehdr.e_entry (0x80100000)
637 * | kernel |
638 * | |
639 * ---------------- <----- page aligned boundary.
640 * | data |
641 * | |
642 * ----------------
643 * | bss |
644 * ----------------<--- end of kernel (page aligned)
645 * | PMD cpu0pdb |
646 * ----------------<--- page +1
647 * | PTE cpu0pte |
648 * ----------------<--- page (tot_pte_pages)/1024
649 * | page_array |
650 * ---------------- <--- page (tot_pte_pages)/1024
651 * | empty to TOM |
652 * ----------------
653 */
655 static int
656 loadp9image(gzFile kernel_gfd, int xc_handle, u32 dom,
657 unsigned long *page_array,
658 unsigned long tot_pages, unsigned long *virt_load_addr,
659 unsigned long *ksize, unsigned long *symtab_addr,
660 unsigned long *symtab_len,
661 unsigned long *first_data_page, unsigned long *pdb_page)
662 {
663 unsigned long datapage;
664 Exec ehdr;
666 char *p;
667 unsigned long maxva;
668 int curpos, ret;
669 PAGE *image = 0;
670 unsigned long image_tot_pages = 0;
671 unsigned long textround;
673 ret = -1;
675 p = NULL;
676 maxva = 0;
678 if (gzread(kernel_gfd, &ehdr, sizeof (Exec)) != sizeof (Exec)) {
679 PERROR("Error reading kernel image P9 header.");
680 goto out;
681 }
683 plan9header(&ehdr);
684 curpos = sizeof (Exec);
686 if (ehdr.magic != I_MAGIC) {
687 PERROR("Image does not have an P9 header.");
688 goto out;
689 }
691 textround = ((ehdr.text + 0x20 + 4095) >> 12) << 12;
692 *first_data_page = 0x100000 + textround;
693 DPRINTF(("ehrd.text is 0x%lx, textround is 0x%lx\n",
694 ehdr.text, textround));
696 image_tot_pages =
697 (textround + ehdr.data + ehdr.bss + PAGE_SIZE - 1) >> PAGE_SHIFT;
698 DPRINTF(("tot pages is %ld\n", image_tot_pages));
700 *virt_load_addr = 0x80100000;
702 if ((*virt_load_addr & (PAGE_SIZE - 1)) != 0) {
703 ERROR("We can only deal with page-aligned load addresses");
704 goto out;
705 }
707 if ((*virt_load_addr + (image_tot_pages << PAGE_SHIFT)) >
708 HYPERVISOR_VIRT_START) {
709 ERROR("Cannot map all domain memory without hitting Xen space");
710 goto out;
711 }
713 /* just malloc an image that is image_tot_pages in size. Then read in
714 * the image -- text, data, -- to page-rounded alignments.
715 * then copy into xen .
716 * this gets BSS zeroed for free
717 */
718 DPRINTF(("Allocate %ld bytes\n", image_tot_pages * sizeof (*image)));
719 image = calloc(image_tot_pages, sizeof (*image));
720 if (!image)
721 return blah("alloc data");
722 /* text starts at 0x20, after the header, just like Unix long ago */
723 if (gzread(kernel_gfd, &image[0].data[sizeof (Exec)], ehdr.text) <
724 ehdr.text)
725 return blah("read text");
726 DPRINTF(("READ TEXT %ld bytes\n", ehdr.text));
727 datapage = ((ehdr.text + sizeof (Exec)) / PAGE_SIZE) + 1;
728 if (gzread(kernel_gfd, image[datapage].data, ehdr.data) < ehdr.data)
729 return blah("read data");
730 DPRINTF(("READ DATA %ld bytes\n", ehdr.data));
732 /* nice contig stuff */
733 /* oops need to start at 0x100000 */
735 ret = memcpy_toguest(xc_handle, dom,
736 image, image_tot_pages * 4096, page_array, 0x100);
737 DPRINTF(("done copying kernel to guest memory\n"));
739 out:
740 if (image)
741 free(image);
742 *pdb_page = image_tot_pages + (0x100000 >> PAGE_SHIFT);
743 return ret;
744 }