2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_config.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_debug.h>
23 #include <palacios/vmm_msr.h>
24 #include <palacios/vmm_decoder.h>
25 #include <palacios/vmm_telemetry.h>
26 #include <palacios/vmm_mem.h>
27 #include <palacios/vmm_hypercall.h>
28 #include <palacios/vmm_dev_mgr.h>
29 #include <palacios/vmm_cpuid.h>
30 #include <palacios/vmm_xml.h>
31 #include <palacios/vmm_io.h>
32 #include <palacios/vmm_msr.h>
33 #include <palacios/vmm_mptable.h>
40 #include <palacios/vmm_host_events.h>
41 #include <palacios/vmm_socket.h>
43 #include "vmm_config_class.h"
45 // This is used to access the configuration file index table
52 struct file_idx_table {
54 struct file_hdr hdrs[0];
60 static int setup_memory_map(struct v3_vm_info * vm, v3_cfg_tree_t * cfg);
61 static int setup_devices(struct v3_vm_info * vm, v3_cfg_tree_t * cfg);
65 char * v3_cfg_val(v3_cfg_tree_t * tree, char * tag) {
66 char * attrib = (char *)v3_xml_attr(tree, tag);
67 v3_cfg_tree_t * child_entry = v3_xml_child(tree, tag);
70 if ((child_entry != NULL) && (attrib != NULL)) {
71 PrintError("Duplicate Configuration parameters present for %s\n", tag);
75 val = (attrib == NULL) ? v3_xml_txt(child_entry): attrib;
80 v3_cfg_tree_t * v3_cfg_subtree(v3_cfg_tree_t * tree, char * tag) {
81 return v3_xml_child(tree, tag);
84 v3_cfg_tree_t * v3_cfg_next_branch(v3_cfg_tree_t * tree) {
85 return v3_xml_next(tree);
90 struct v3_cfg_file * v3_cfg_get_file(struct v3_vm_info * vm, char * tag) {
91 struct v3_cfg_file * file = NULL;
93 file = (struct v3_cfg_file *)v3_htable_search(vm->cfg_data->file_table, (addr_t)tag);
99 static uint_t file_hash_fn(addr_t key) {
100 char * name = (char *)key;
101 return v3_hash_buffer((uchar_t *)name, strlen(name));
104 static int file_eq_fn(addr_t key1, addr_t key2) {
105 char * name1 = (char *)key1;
106 char * name2 = (char *)key2;
108 return (strcmp(name1, name2) == 0);
111 static struct v3_config * parse_config(void * cfg_blob) {
112 struct v3_config * cfg = NULL;
115 struct file_idx_table * files = NULL;
116 v3_cfg_tree_t * file_tree = NULL;
118 V3_Print("cfg data at %p\n", cfg_blob);
120 if (memcmp(cfg_blob, "v3vee\0\0\0", 8) != 0) {
121 PrintError("Invalid Configuration Header\n");
127 cfg = (struct v3_config *)V3_Malloc(sizeof(struct v3_config));
128 memset(cfg, 0, sizeof(struct v3_config));
130 cfg->blob = cfg_blob;
131 INIT_LIST_HEAD(&(cfg->file_list));
132 cfg->file_table = v3_create_htable(0, file_hash_fn, file_eq_fn);
134 xml_len = *(uint32_t *)(cfg_blob + offset);
137 cfg->cfg = (v3_cfg_tree_t *)v3_xml_parse((uint8_t *)(cfg_blob + offset));
142 files = (struct file_idx_table *)(cfg_blob + offset);
144 V3_Print("Number of files in cfg: %d\n", (uint32_t)(files->num_files));
146 file_tree = v3_cfg_subtree(v3_cfg_subtree(cfg->cfg, "files"), "file");
149 char * id = v3_cfg_val(file_tree, "id");
150 char * index = v3_cfg_val(file_tree, "index");
151 int idx = atoi(index);
152 struct file_hdr * hdr = &(files->hdrs[idx]);
153 struct v3_cfg_file * file = NULL;
155 file = (struct v3_cfg_file *)V3_Malloc(sizeof(struct v3_cfg_file));
158 PrintError("Could not allocate file structure\n");
163 V3_Print("File index=%d id=%s\n", idx, id);
165 strncpy(file->tag, id, 256);
166 file->size = hdr->size;
167 file->data = cfg_blob + hdr->offset;
169 V3_Print("Storing file data offset = %d, size=%d\n", (uint32_t)hdr->offset, hdr->size);
170 V3_Print("file data at %p\n", file->data);
171 list_add( &(file->file_node), &(cfg->file_list));
173 V3_Print("Keying file to name\n");
174 v3_htable_insert(cfg->file_table, (addr_t)(file->tag), (addr_t)(file));
176 V3_Print("Iterating to next file\n");
178 file_tree = v3_cfg_next_branch(file_tree);
181 V3_Print("Configuration parsed successfully\n");
187 static inline uint32_t get_alignment(char * align_str) {
188 // default is 4KB alignment
189 uint32_t alignment = PAGE_SIZE_4KB;
191 if (align_str != NULL) {
192 if (strcasecmp(align_str, "2MB") == 0) {
193 alignment = PAGE_SIZE_2MB;
194 } else if (strcasecmp(align_str, "4MB") == 0) {
195 alignment = PAGE_SIZE_4MB;
199 #ifndef CONFIG_ALIGNED_PG_ALLOC
200 if (alignment != PAGE_SIZE_4KB) {
201 PrintError("Aligned page allocations are not supported in this host (requested alignment=%d)\n", alignment);
202 PrintError("Ignoring alignment request\n");
208 static int pre_config_vm(struct v3_vm_info * vm, v3_cfg_tree_t * vm_cfg) {
211 char * memory_str = v3_cfg_val(vm_cfg, "memory");
212 char * schedule_hz_str = v3_cfg_val(vm_cfg, "schedule_hz");
213 char * vm_class = v3_cfg_val(vm_cfg, "class");
214 char * align_str = v3_cfg_val(v3_cfg_subtree(vm_cfg, "memory"), "alignment");
215 uint32_t sched_hz = 100; // set the schedule frequency to 100 HZ
218 PrintError("Memory is a required configuration parameter\n");
222 PrintDebug("Memory=%s\n", memory_str);
224 PrintDebug("Alignment=%s\n", align_str);
226 PrintDebug("Alignment defaulted to 4KB.\n");
229 // Amount of ram the Guest will have, always in MB
230 vm->mem_size = atoi(memory_str) * 1024 * 1024;
231 vm->mem_align = get_alignment(align_str);
233 PrintDebug("Alignment computed as 0x%x\n", vm->mem_align);
235 if (strcasecmp(vm_class, "PC") == 0) {
236 vm->vm_class = V3_PC_VM;
238 PrintError("Invalid VM class\n");
243 #ifdef CONFIG_TELEMETRY
245 char * telemetry = v3_cfg_val(vm_cfg, "telemetry");
247 // This should go first, because other subsystems will depend on the guest_info flag
248 if ((telemetry) && (strcasecmp(telemetry, "enable") == 0)) {
249 vm->enable_telemetry = 1;
251 vm->enable_telemetry = 0;
256 if (v3_init_vm(vm) == -1) {
257 PrintError("Failed to initialize VM\n");
263 if (schedule_hz_str) {
264 sched_hz = atoi(schedule_hz_str);
267 PrintDebug("CPU_KHZ = %d, schedule_freq=%p\n", V3_CPU_KHZ(),
268 (void *)(addr_t)sched_hz);
270 vm->yield_cycle_period = (V3_CPU_KHZ() * 1000) / sched_hz;
275 static int determine_paging_mode(struct guest_info *info, v3_cfg_tree_t * core_cfg)
277 extern v3_cpu_arch_t v3_cpu_types[];
279 v3_cfg_tree_t *vm_tree = info->vm_info->cfg_data->cfg;
280 v3_cfg_tree_t *pg_tree = v3_cfg_subtree(vm_tree, "paging");
281 char *pg_mode = v3_cfg_val(pg_tree, "mode");
283 PrintDebug("Paging mode specified as %s\n", pg_mode);
286 if ((strcasecmp(pg_mode, "nested") == 0)) {
287 if (v3_cpu_types[info->cpu_id] == V3_SVM_REV3_CPU) {
288 info->shdw_pg_mode = NESTED_PAGING;
290 PrintError("Nested paging not supported on this hardware. Defaulting to shadow paging\n");
291 info->shdw_pg_mode = SHADOW_PAGING;
293 } else if ((strcasecmp(pg_mode, "shadow") == 0)) {
294 info->shdw_pg_mode = SHADOW_PAGING;
296 PrintError("Invalid paging mode (%s) specified in configuration. Defaulting to shadow paging\n", pg_mode);
297 info->shdw_pg_mode = SHADOW_PAGING;
300 PrintDebug("No paging mode specified in configuration.\n");
301 info->shdw_pg_mode = SHADOW_PAGING;
305 if (info->shdw_pg_mode == NESTED_PAGING) {
306 PrintDebug("Guest Paging Mode: NESTED_PAGING\n");
307 } else if (info->shdw_pg_mode == SHADOW_PAGING) {
308 PrintDebug("Guest Paging Mode: SHADOW_PAGING\n");
310 PrintError("Guest paging mode incorrectly set.\n");
314 if (strcasecmp(v3_cfg_val(pg_tree, "large_pages"), "true") == 0) {
315 info->use_large_pages = 1;
316 PrintDebug("Use of large pages in memory virtualization enabled.\n");
322 static int pre_config_core(struct guest_info * info, v3_cfg_tree_t * core_cfg) {
324 if (determine_paging_mode(info, core_cfg))
329 if (info->vm_info->vm_class == V3_PC_VM) {
330 if (pre_config_pc_core(info, core_cfg) == -1) {
331 PrintError("PC Post configuration failure\n");
335 PrintError("Invalid VM Class\n");
344 static int post_config_vm(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
346 vm->run_state = VM_STOPPED;
348 // Configure the memory map for the guest
349 if (setup_memory_map(vm, cfg) == -1) {
350 PrintError("Setting up guest memory map failed...\n");
354 //v3_hook_io_port(info, 1234, &IO_Read, NULL, info);
356 if (setup_devices(vm, cfg) == -1) {
357 PrintError("Failed to setup devices\n");
362 // v3_print_io_map(info);
363 v3_print_msr_map(vm);
366 if (vm->vm_class == V3_PC_VM) {
367 if (post_config_pc(vm, cfg) == -1) {
368 PrintError("PC Post configuration failure\n");
372 PrintError("Invalid VM Class\n");
376 if (v3_inject_mptable(vm)==-1) {
377 PrintError("Failed to inject mptable during configuration\n");
386 static int post_config_core(struct guest_info * info, v3_cfg_tree_t * cfg) {
390 if (info->vm_info->vm_class == V3_PC_VM) {
391 if (post_config_pc_core(info, cfg) == -1) {
392 PrintError("PC Post configuration failure\n");
396 PrintError("Invalid VM Class\n");
406 static struct v3_vm_info * allocate_guest(int num_cores) {
407 int guest_state_size = sizeof(struct v3_vm_info) + (sizeof(struct guest_info) * num_cores);
408 struct v3_vm_info * vm = V3_Malloc(guest_state_size);
410 memset(vm, 0, guest_state_size);
412 vm->num_cores = num_cores;
419 struct v3_vm_info * v3_config_guest(void * cfg_blob) {
420 v3_cpu_arch_t cpu_type = v3_get_cpu_type(v3_get_cpu_id());
421 struct v3_config * cfg_data = NULL;
422 struct v3_vm_info * vm = NULL;
425 v3_cfg_tree_t * cores_cfg = NULL;
426 v3_cfg_tree_t * per_core_cfg = NULL;
428 if (cpu_type == V3_INVALID_CPU) {
429 PrintError("Configuring guest on invalid CPU\n");
433 cfg_data = parse_config(cfg_blob);
436 PrintError("Could not parse configuration\n");
440 cores_cfg = v3_cfg_subtree(cfg_data->cfg, "cores");
443 PrintError("Could not find core configuration (new config format required)\n");
447 num_cores = atoi(v3_cfg_val(cores_cfg, "count"));
449 if (num_cores == 0) {
450 PrintError("No cores specified in configuration\n");
454 V3_Print("Configuring %d cores\n", num_cores);
456 vm = allocate_guest(num_cores);
459 PrintError("Could not allocate %d core guest\n", vm->num_cores);
463 vm->cfg_data = cfg_data;
465 V3_Print("Preconfiguration\n");
467 if (pre_config_vm(vm, vm->cfg_data->cfg) == -1) {
468 PrintError("Error in preconfiguration\n");
473 V3_Print("Per core configuration\n");
474 per_core_cfg = v3_cfg_subtree(cores_cfg, "core");
476 // per core configuration
477 for (i = 0; i < vm->num_cores; i++) {
478 struct guest_info * info = &(vm->cores[i]);
483 if (pre_config_core(info, per_core_cfg) == -1) {
484 PrintError("Error in core %d preconfiguration\n", i);
488 per_core_cfg = v3_cfg_next_branch(per_core_cfg);
492 V3_Print("Post Configuration\n");
494 if (post_config_vm(vm, vm->cfg_data->cfg) == -1) {
495 PrintError("Error in postconfiguration\n");
500 per_core_cfg = v3_cfg_subtree(cores_cfg, "core");
502 // per core configuration
503 for (i = 0; i < vm->num_cores; i++) {
504 struct guest_info * info = &(vm->cores[i]);
506 post_config_core(info, per_core_cfg);
508 per_core_cfg = v3_cfg_next_branch(per_core_cfg);
511 V3_Print("Configuration successfull\n");
520 static int setup_memory_map(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
521 v3_cfg_tree_t * mem_region = v3_cfg_subtree(v3_cfg_subtree(cfg, "memmap"), "region");
524 addr_t start_addr = atox(v3_cfg_val(mem_region, "start"));
525 addr_t end_addr = atox(v3_cfg_val(mem_region, "end"));
526 addr_t host_addr = atox(v3_cfg_val(mem_region, "host_addr"));
529 if (v3_add_shadow_mem(vm, V3_MEM_CORE_ANY, start_addr, end_addr, host_addr) == -1) {
530 PrintError("Could not map memory region: %p-%p => %p\n",
531 (void *)start_addr, (void *)end_addr, (void *)host_addr);
535 mem_region = v3_cfg_next_branch(mem_region);
544 static int setup_devices(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
545 v3_cfg_tree_t * device = v3_cfg_subtree(v3_cfg_subtree(cfg, "devices"), "device");
549 char * dev_class = v3_cfg_val(device, "class");
551 V3_Print("configuring device %s\n", dev_class);
553 if (v3_create_device(vm, dev_class, device) == -1) {
554 PrintError("Error creating device %s\n", dev_class);
558 device = v3_cfg_next_branch(device);
561 v3_print_dev_mgr(vm);