direct-io.hg

view tools/libxc/xc_hvm_build.c @ 14183:1966270d89d7

[HVM] Builder must know which kind of vcpu context struct to use
when the tools are being run in a compat-mode domain
Signed-off-by: Tim Deegan <Tim.Deegan@xensource.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Thu Mar 01 11:04:23 2007 +0000 (2007-03-01)
parents aa1be6f5150e
children f3f5f2756d75
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.h>
22 #define SCRATCH_PFN 0xFFFFF
24 /* Need to provide the right flavour of vcpu context for Xen */
25 typedef union
26 {
27 vcpu_guest_context_x86_64_t c64;
28 vcpu_guest_context_x86_32_t c32;
29 vcpu_guest_context_t c;
30 } vcpu_guest_context_either_t;
33 int xc_set_hvm_param(
34 int handle, domid_t dom, int param, unsigned long value)
35 {
36 DECLARE_HYPERCALL;
37 xen_hvm_param_t arg;
38 int rc;
40 hypercall.op = __HYPERVISOR_hvm_op;
41 hypercall.arg[0] = HVMOP_set_param;
42 hypercall.arg[1] = (unsigned long)&arg;
43 arg.domid = dom;
44 arg.index = param;
45 arg.value = value;
46 if ( lock_pages(&arg, sizeof(arg)) != 0 )
47 return -1;
48 rc = do_xen_hypercall(handle, &hypercall);
49 unlock_pages(&arg, sizeof(arg));
50 return rc;
51 }
53 int xc_get_hvm_param(
54 int handle, domid_t dom, int param, unsigned long *value)
55 {
56 DECLARE_HYPERCALL;
57 xen_hvm_param_t arg;
58 int rc;
60 hypercall.op = __HYPERVISOR_hvm_op;
61 hypercall.arg[0] = HVMOP_get_param;
62 hypercall.arg[1] = (unsigned long)&arg;
63 arg.domid = dom;
64 arg.index = param;
65 if ( lock_pages(&arg, sizeof(arg)) != 0 )
66 return -1;
67 rc = do_xen_hypercall(handle, &hypercall);
68 unlock_pages(&arg, sizeof(arg));
69 *value = arg.value;
70 return rc;
71 }
73 static void build_e820map(void *e820_page, unsigned long long mem_size)
74 {
75 struct e820entry *e820entry =
76 (struct e820entry *)(((unsigned char *)e820_page) + E820_MAP_OFFSET);
77 unsigned long long extra_mem_size = 0;
78 unsigned char nr_map = 0;
80 /*
81 * Physical address space from HVM_BELOW_4G_RAM_END to 4G is reserved
82 * for PCI devices MMIO. So if HVM has more than HVM_BELOW_4G_RAM_END
83 * RAM, memory beyond HVM_BELOW_4G_RAM_END will go to 4G above.
84 */
85 if ( mem_size > HVM_BELOW_4G_RAM_END )
86 {
87 extra_mem_size = mem_size - HVM_BELOW_4G_RAM_END;
88 mem_size = HVM_BELOW_4G_RAM_END;
89 }
91 /* 0x0-0x9F000: Ordinary RAM. */
92 e820entry[nr_map].addr = 0x0;
93 e820entry[nr_map].size = 0x9F000;
94 e820entry[nr_map].type = E820_RAM;
95 nr_map++;
97 /*
98 * 0x9F000-0x9F800: SMBIOS tables.
99 * 0x9FC00-0xA0000: Extended BIOS Data Area (EBDA).
100 * TODO: SMBIOS tables should be moved higher (>=0xE0000).
101 * They are unusually low in our memory map: could cause problems?
102 */
103 e820entry[nr_map].addr = 0x9F000;
104 e820entry[nr_map].size = 0x1000;
105 e820entry[nr_map].type = E820_RESERVED;
106 nr_map++;
108 /*
109 * Following regions are standard regions of the PC memory map.
110 * They are not covered by e820 regions. OSes will not use as RAM.
111 * 0xA0000-0xC0000: VGA memory-mapped I/O. Not covered by E820.
112 * 0xC0000-0xE0000: 16-bit devices, expansion ROMs (inc. vgabios).
113 * TODO: hvmloader should free pages which turn out to be unused.
114 */
116 /*
117 * 0xE0000-0x0F0000: PC-specific area. We place ACPI tables here.
118 * We *cannot* mark as E820_ACPI, for two reasons:
119 * 1. ACPI spec. says that E820_ACPI regions below
120 * 16MB must clip INT15h 0x88 and 0xe801 queries.
121 * Our rombios doesn't do this.
122 * 2. The OS is allowed to reclaim ACPI memory after
123 * parsing the tables. But our FACS is in this
124 * region and it must not be reclaimed (it contains
125 * the ACPI global lock!).
126 * 0xF0000-0x100000: System BIOS.
127 * TODO: hvmloader should free pages which turn out to be unused.
128 */
129 e820entry[nr_map].addr = 0xE0000;
130 e820entry[nr_map].size = 0x20000;
131 e820entry[nr_map].type = E820_RESERVED;
132 nr_map++;
134 /* Low RAM goes here. Remove 3 pages for ioreq, bufioreq, and xenstore. */
135 e820entry[nr_map].addr = 0x100000;
136 e820entry[nr_map].size = mem_size - 0x100000 - PAGE_SIZE * 3;
137 e820entry[nr_map].type = E820_RAM;
138 nr_map++;
140 if ( extra_mem_size )
141 {
142 e820entry[nr_map].addr = (1ULL << 32);
143 e820entry[nr_map].size = extra_mem_size;
144 e820entry[nr_map].type = E820_RAM;
145 nr_map++;
146 }
148 *(((unsigned char *)e820_page) + E820_MAP_NR_OFFSET) = nr_map;
149 }
151 static int
152 loadelfimage(struct elf_binary *elf, int xch, uint32_t dom, unsigned long *parray)
153 {
154 privcmd_mmap_entry_t *entries = NULL;
155 int pages = (elf->pend - elf->pstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
156 int i, rc = -1;
158 /* map hvmloader address space */
159 entries = malloc(pages * sizeof(privcmd_mmap_entry_t));
160 if (NULL == entries)
161 goto err;
162 elf->dest = mmap(NULL, pages << PAGE_SHIFT, PROT_READ | PROT_WRITE,
163 MAP_SHARED, xch, 0);
164 if (MAP_FAILED == elf->dest)
165 goto err;
167 for (i = 0; i < pages; i++)
168 {
169 entries[i].va = (uintptr_t)elf->dest + (i << PAGE_SHIFT);
170 entries[i].mfn = parray[(elf->pstart >> PAGE_SHIFT) + i];
171 entries[i].npages = 1;
172 }
173 rc = xc_map_foreign_ranges(xch, dom, entries, pages);
174 if (rc < 0)
175 goto err;
177 /* load hvmloader */
178 elf_load_binary(elf);
179 rc = 0;
181 err:
182 /* cleanup */
183 if (elf->dest) {
184 munmap(elf->dest, pages << PAGE_SHIFT);
185 elf->dest = NULL;
186 }
187 if (entries)
188 free(entries);
190 return rc;
191 }
193 static int setup_guest(int xc_handle,
194 uint32_t dom, int memsize,
195 char *image, unsigned long image_size,
196 vcpu_guest_context_either_t *ctxt)
197 {
198 xen_pfn_t *page_array = NULL;
199 unsigned long i, nr_pages = (unsigned long)memsize << (20 - PAGE_SHIFT);
200 unsigned long shared_page_nr;
201 struct xen_add_to_physmap xatp;
202 struct shared_info *shared_info;
203 void *e820_page;
204 struct elf_binary elf;
205 uint64_t v_start, v_end;
206 int rc;
207 xen_capabilities_info_t caps;
209 if (0 != elf_init(&elf, image, image_size))
210 goto error_out;
211 elf_parse_binary(&elf);
212 v_start = 0;
213 v_end = (unsigned long long)memsize << 20;
215 if (xc_version(xc_handle, XENVER_capabilities, &caps) != 0)
216 {
217 PERROR("Could not get Xen capabilities\n");
218 goto error_out;
219 }
221 if ( (elf.pstart & (PAGE_SIZE - 1)) != 0 )
222 {
223 PERROR("Guest OS must load to a page boundary.\n");
224 goto error_out;
225 }
227 IPRINTF("VIRTUAL MEMORY ARRANGEMENT:\n"
228 " Loaded HVM loader: %016"PRIx64"->%016"PRIx64"\n"
229 " TOTAL: %016"PRIx64"->%016"PRIx64"\n"
230 " ENTRY ADDRESS: %016"PRIx64"\n",
231 elf.pstart, elf.pend,
232 v_start, v_end,
233 elf_uval(&elf, elf.ehdr, e_entry));
235 if ( (page_array = malloc(nr_pages * sizeof(xen_pfn_t))) == NULL )
236 {
237 PERROR("Could not allocate memory.\n");
238 goto error_out;
239 }
241 for ( i = 0; i < nr_pages; i++ )
242 page_array[i] = i;
243 for ( i = HVM_BELOW_4G_RAM_END >> PAGE_SHIFT; i < nr_pages; i++ )
244 page_array[i] += HVM_BELOW_4G_MMIO_LENGTH >> PAGE_SHIFT;
246 /* Allocate memory for HVM guest, skipping VGA hole 0xA0000-0xC0000. */
247 rc = xc_domain_memory_populate_physmap(
248 xc_handle, dom, (nr_pages > 0xa0) ? 0xa0 : nr_pages,
249 0, 0, &page_array[0x00]);
250 if ( (rc == 0) && (nr_pages > 0xc0) )
251 rc = xc_domain_memory_populate_physmap(
252 xc_handle, dom, nr_pages - 0xc0, 0, 0, &page_array[0xc0]);
253 if ( rc != 0 )
254 {
255 PERROR("Could not allocate memory for HVM guest.\n");
256 goto error_out;
257 }
259 loadelfimage(&elf, xc_handle, dom, page_array);
261 if ( (e820_page = xc_map_foreign_range(
262 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
263 E820_MAP_PAGE >> PAGE_SHIFT)) == NULL )
264 goto error_out;
265 memset(e820_page, 0, PAGE_SIZE);
266 build_e820map(e820_page, v_end);
267 munmap(e820_page, PAGE_SIZE);
269 /* Map and initialise shared_info page. */
270 xatp.domid = dom;
271 xatp.space = XENMAPSPACE_shared_info;
272 xatp.idx = 0;
273 xatp.gpfn = SCRATCH_PFN;
274 if ( (xc_memory_op(xc_handle, XENMEM_add_to_physmap, &xatp) != 0) ||
275 ((shared_info = xc_map_foreign_range(
276 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE,
277 SCRATCH_PFN)) == NULL) )
278 goto error_out;
279 memset(shared_info, 0, PAGE_SIZE);
280 /* NB. evtchn_upcall_mask is unused: leave as zero. */
281 memset(&shared_info->evtchn_mask[0], 0xff,
282 sizeof(shared_info->evtchn_mask));
283 shared_info->arch.max_pfn = page_array[nr_pages - 1];
284 munmap(shared_info, PAGE_SIZE);
286 if ( v_end > HVM_BELOW_4G_RAM_END )
287 shared_page_nr = (HVM_BELOW_4G_RAM_END >> PAGE_SHIFT) - 1;
288 else
289 shared_page_nr = (v_end >> PAGE_SHIFT) - 1;
291 /* Paranoia: clean pages. */
292 if ( xc_clear_domain_page(xc_handle, dom, shared_page_nr) ||
293 xc_clear_domain_page(xc_handle, dom, shared_page_nr-1) ||
294 xc_clear_domain_page(xc_handle, dom, shared_page_nr-2) )
295 goto error_out;
297 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_STORE_PFN, shared_page_nr-1);
298 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_BUFIOREQ_PFN, shared_page_nr-2);
299 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_IOREQ_PFN, shared_page_nr);
301 free(page_array);
303 /* Set [er]ip in the way that's right for Xen */
304 if ( strstr(caps, "x86_64") )
305 ctxt->c64.user_regs.rip = elf_uval(&elf, elf.ehdr, e_entry);
306 else
307 ctxt->c32.user_regs.eip = elf_uval(&elf, elf.ehdr, e_entry);
309 return 0;
311 error_out:
312 free(page_array);
313 return -1;
314 }
316 static int xc_hvm_build_internal(int xc_handle,
317 uint32_t domid,
318 int memsize,
319 char *image,
320 unsigned long image_size)
321 {
322 struct xen_domctl launch_domctl;
323 vcpu_guest_context_either_t ctxt;
324 int rc;
326 if ( (image == NULL) || (image_size == 0) )
327 {
328 ERROR("Image required");
329 goto error_out;
330 }
332 memset(&ctxt, 0, sizeof(ctxt));
334 if ( setup_guest(xc_handle, domid, memsize, image, image_size, &ctxt) < 0 )
335 {
336 goto error_out;
337 }
339 if ( lock_pages(&ctxt, sizeof(ctxt) ) )
340 {
341 PERROR("%s: ctxt mlock failed", __func__);
342 goto error_out;
343 }
345 memset(&launch_domctl, 0, sizeof(launch_domctl));
346 launch_domctl.domain = (domid_t)domid;
347 launch_domctl.u.vcpucontext.vcpu = 0;
348 set_xen_guest_handle(launch_domctl.u.vcpucontext.ctxt, &ctxt.c);
349 launch_domctl.cmd = XEN_DOMCTL_setvcpucontext;
350 rc = xc_domctl(xc_handle, &launch_domctl);
352 unlock_pages(&ctxt, sizeof(ctxt));
354 return rc;
356 error_out:
357 return -1;
358 }
360 static inline int is_loadable_phdr(Elf32_Phdr *phdr)
361 {
362 return ((phdr->p_type == PT_LOAD) &&
363 ((phdr->p_flags & (PF_W|PF_X)) != 0));
364 }
366 /* xc_hvm_build
367 *
368 * Create a domain for a virtualized Linux, using files/filenames
369 *
370 */
372 int xc_hvm_build(int xc_handle,
373 uint32_t domid,
374 int memsize,
375 const char *image_name)
376 {
377 char *image;
378 int sts;
379 unsigned long image_size;
381 if ( (image_name == NULL) ||
382 ((image = xc_read_image(image_name, &image_size)) == NULL) )
383 return -1;
385 sts = xc_hvm_build_internal(xc_handle, domid, memsize, image, image_size);
387 free(image);
389 return sts;
390 }
392 /* xc_hvm_build_mem
393 *
394 * Create a domain for a virtualized Linux, using buffers
395 *
396 */
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,
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 */