Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added extension core init
[palacios.git] / palacios / src / palacios / vmm_config.c
1  /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
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>
34 #include <palacios/vmm_sprintf.h>
35
36
37
38
39
40 #include <palacios/vmm_host_events.h>
41
42 #include "vmm_config_class.h"
43
44 // This is used to access the configuration file index table
45 struct file_hdr {
46     uint32_t index;
47     uint32_t size;
48     uint64_t offset;
49 };
50
51 struct file_idx_table {
52     uint64_t num_files;
53     struct file_hdr hdrs[0];
54 };
55
56
57
58
59 static int setup_memory_map(struct v3_vm_info * vm, v3_cfg_tree_t * cfg);
60 static int setup_extensions(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);
62
63
64
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);
68     char * val = NULL;
69
70     if ((child_entry != NULL) && (attrib != NULL)) {
71         PrintError("Duplicate Configuration parameters present for %s\n", tag);
72         return NULL;
73     }
74
75     if (attrib == NULL) {
76         val = v3_xml_txt(child_entry);
77         
78         if ( val[0] == 0 )
79                 val = NULL;
80     } else {
81         val = attrib;
82     }
83     
84     return val;
85 }
86
87 v3_cfg_tree_t * v3_cfg_subtree(v3_cfg_tree_t * tree, char * tag) {
88     return v3_xml_child(tree, tag);
89 }
90
91 v3_cfg_tree_t * v3_cfg_next_branch(v3_cfg_tree_t * tree) {
92     return v3_xml_next(tree);
93 }
94
95
96
97 struct v3_cfg_file * v3_cfg_get_file(struct v3_vm_info * vm, char * tag) {
98     struct v3_cfg_file * file = NULL;
99
100     file = (struct v3_cfg_file *)v3_htable_search(vm->cfg_data->file_table, (addr_t)tag);
101
102     return file;
103 }
104
105
106 static uint_t file_hash_fn(addr_t key) {
107     char * name = (char *)key;
108     return v3_hash_buffer((uchar_t *)name, strlen(name));
109 }
110
111 static int file_eq_fn(addr_t key1, addr_t key2) {
112     char * name1 = (char *)key1;
113     char * name2 = (char *)key2;
114
115     return (strcmp(name1, name2) == 0);
116 }
117
118 static struct v3_config * parse_config(void * cfg_blob) {
119     struct v3_config * cfg = NULL;
120     int offset = 0;
121     uint_t xml_len = 0; 
122     struct file_idx_table * files = NULL;
123     v3_cfg_tree_t * file_tree = NULL;
124
125     V3_Print("cfg data at %p\n", cfg_blob);
126
127     if (memcmp(cfg_blob, "v3vee\0\0\0", 8) != 0) {
128         PrintError("Invalid Configuration Header\n");
129         return NULL;
130     }
131
132     offset += 8;
133
134     cfg = (struct v3_config *)V3_Malloc(sizeof(struct v3_config));
135     memset(cfg, 0, sizeof(struct v3_config));
136
137     cfg->blob = cfg_blob;
138     INIT_LIST_HEAD(&(cfg->file_list));
139     cfg->file_table = v3_create_htable(0, file_hash_fn, file_eq_fn);
140     
141     xml_len = *(uint32_t *)(cfg_blob + offset);
142     offset += 4;
143
144     cfg->cfg = (v3_cfg_tree_t *)v3_xml_parse((uint8_t *)(cfg_blob + offset));
145     offset += xml_len;
146    
147     offset += 8;
148
149     files = (struct file_idx_table *)(cfg_blob + offset);
150
151     V3_Print("Number of files in cfg: %d\n", (uint32_t)(files->num_files));
152
153     file_tree = v3_cfg_subtree(v3_cfg_subtree(cfg->cfg, "files"), "file");
154
155     while (file_tree) {
156         char * id = v3_cfg_val(file_tree, "id");
157         char * index = v3_cfg_val(file_tree, "index");
158         int idx = atoi(index);
159         struct file_hdr * hdr = &(files->hdrs[idx]);
160         struct v3_cfg_file * file = NULL;
161
162         file = (struct v3_cfg_file *)V3_Malloc(sizeof(struct v3_cfg_file));
163         
164         if (!file) {
165             PrintError("Could not allocate file structure\n");
166             return NULL;
167         }
168
169         V3_Print("File index=%d id=%s\n", idx, id);
170
171         strncpy(file->tag, id, 256);
172         file->size = hdr->size;
173         file->data = cfg_blob + hdr->offset;
174
175         V3_Print("Storing file data offset = %d, size=%d\n", (uint32_t)hdr->offset, hdr->size);
176         V3_Print("file data at %p\n", file->data);
177         list_add( &(file->file_node), &(cfg->file_list));
178
179         V3_Print("Keying file to name\n");
180         v3_htable_insert(cfg->file_table, (addr_t)(file->tag), (addr_t)(file));
181
182         V3_Print("Iterating to next file\n");
183
184         file_tree = v3_cfg_next_branch(file_tree);
185     }
186
187     V3_Print("Configuration parsed successfully\n");
188
189     return cfg;
190 }
191
192
193 static inline uint32_t get_alignment(char * align_str) {
194     // default is 4KB alignment
195     uint32_t alignment = PAGE_SIZE_4KB;
196
197     if (align_str != NULL) {
198         if (strcasecmp(align_str, "2MB") == 0) {
199             alignment = PAGE_SIZE_2MB;
200         } else if (strcasecmp(align_str, "4MB") == 0) {
201             alignment = PAGE_SIZE_4MB;
202         }
203     }
204     
205 #ifndef CONFIG_ALIGNED_PG_ALLOC
206     if (alignment != PAGE_SIZE_4KB) {
207         PrintError("Aligned page allocations are not supported in this host (requested alignment=%d)\n", alignment);
208         PrintError("Ignoring alignment request\n");
209     }
210 #endif 
211
212     return alignment;
213 }
214
215
216 static int pre_config_vm(struct v3_vm_info * vm, v3_cfg_tree_t * vm_cfg) {
217     char * memory_str = v3_cfg_val(vm_cfg, "memory");
218     char * schedule_hz_str = v3_cfg_val(vm_cfg, "schedule_hz");
219     char * vm_class = v3_cfg_val(vm_cfg, "class");
220     char * align_str = v3_cfg_val(v3_cfg_subtree(vm_cfg, "memory"), "alignment");
221     uint32_t sched_hz = 100;    // set the schedule frequency to 100 HZ
222
223
224     if (!memory_str) {
225         PrintError("Memory is a required configuration parameter\n");
226         return -1;
227     }
228     
229     PrintDebug("Memory=%s\n", memory_str);
230     if (align_str) {
231          PrintDebug("Alignment=%s\n", align_str);
232     } else {
233          PrintDebug("Alignment defaulted to 4KB.\n");
234     }
235
236     // Amount of ram the Guest will have, always in MB
237     vm->mem_size = (addr_t)atoi(memory_str) * 1024 * 1024;
238     vm->mem_align = get_alignment(align_str);
239
240
241     PrintDebug("Alignment for %lu bytes of memory computed as 0x%x\n", vm->mem_size, vm->mem_align);
242
243     if (strcasecmp(vm_class, "PC") == 0) {
244         vm->vm_class = V3_PC_VM;
245     } else {
246         PrintError("Invalid VM class\n");
247         return -1;
248     }
249
250 #ifdef CONFIG_TELEMETRY
251     {
252         char * telemetry = v3_cfg_val(vm_cfg, "telemetry");
253
254         // This should go first, because other subsystems will depend on the guest_info flag    
255         if ((telemetry) && (strcasecmp(telemetry, "enable") == 0)) {
256             vm->enable_telemetry = 1;
257         } else {
258             vm->enable_telemetry = 0;
259         }
260     }
261 #endif
262
263     if (v3_init_vm(vm) == -1) {
264         PrintError("Failed to initialize VM\n");
265         return -1;
266     }
267
268
269
270    if (schedule_hz_str) {
271         sched_hz = atoi(schedule_hz_str);
272     }
273
274     PrintDebug("CPU_KHZ = %d, schedule_freq=%p\n", V3_CPU_KHZ(), 
275                (void *)(addr_t)sched_hz);
276
277     vm->yield_cycle_period = (V3_CPU_KHZ() * 1000) / sched_hz;
278     
279     return 0;
280 }
281
282
283 static int determine_paging_mode(struct guest_info * info, v3_cfg_tree_t * core_cfg) {
284     extern v3_cpu_arch_t v3_cpu_types[];
285
286     v3_cfg_tree_t * vm_tree = info->vm_info->cfg_data->cfg;
287     v3_cfg_tree_t * pg_tree = v3_cfg_subtree(vm_tree, "paging");
288     char * pg_mode          = v3_cfg_val(pg_tree, "mode");
289     char * page_size        = v3_cfg_val(pg_tree, "page_size");
290     
291     PrintDebug("Paging mode specified as %s\n", pg_mode);
292
293     if (pg_mode) {
294         if ((strcasecmp(pg_mode, "nested") == 0)) {
295             // we assume symmetric cores, so if core 0 has nested paging they all do
296             if (v3_cpu_types[0] == V3_SVM_REV3_CPU) {
297                 info->shdw_pg_mode = NESTED_PAGING;
298             } else {
299                 PrintError("Nested paging not supported on this hardware. Defaulting to shadow paging\n");
300                 info->shdw_pg_mode = SHADOW_PAGING;
301             }
302         } else if ((strcasecmp(pg_mode, "shadow") == 0)) {
303             info->shdw_pg_mode = SHADOW_PAGING;
304         } else {
305             PrintError("Invalid paging mode (%s) specified in configuration. Defaulting to shadow paging\n", pg_mode);
306             info->shdw_pg_mode = SHADOW_PAGING;
307         }
308     } else {
309         PrintDebug("No paging type specified in configuration. Defaulting to shadow paging\n");
310         info->shdw_pg_mode = SHADOW_PAGING;
311     }
312
313
314     if (info->shdw_pg_mode == NESTED_PAGING) {
315         PrintDebug("Guest Paging Mode: NESTED_PAGING\n");
316         if (strcasecmp(page_size, "4kb") == 0) { /* TODO: this may not be an ideal place for this */
317             info->vm_info->paging_size = PAGING_4KB;
318         } else if (strcasecmp(page_size, "2mb") == 0) {
319             info->vm_info->paging_size = PAGING_2MB;
320         } else {
321             PrintError("Invalid VM paging size: '%s'\n", page_size);
322             return -1;
323         }
324         PrintDebug("VM page size=%s\n", page_size);
325     } else if (info->shdw_pg_mode == SHADOW_PAGING) {
326         PrintDebug("Guest Paging Mode: SHADOW_PAGING\n");
327     } else {
328         PrintError("Guest paging mode incorrectly set.\n");
329         return -1;
330     }
331
332     if (v3_cfg_val(pg_tree, "large_pages") != NULL) {
333         if (strcasecmp(v3_cfg_val(pg_tree, "large_pages"), "true") == 0) {
334             info->use_large_pages = 1;
335             PrintDebug("Use of large pages in memory virtualization enabled.\n");
336         }
337     }
338     return 0;
339 }
340
341 static int pre_config_core(struct guest_info * info, v3_cfg_tree_t * core_cfg) {
342     if (determine_paging_mode(info, core_cfg) != 0) {
343         return -1;
344     }
345
346     v3_init_core(info);
347
348     if (info->vm_info->vm_class == V3_PC_VM) {
349         if (pre_config_pc_core(info, core_cfg) == -1) {
350             PrintError("PC Post configuration failure\n");
351             return -1;
352         }
353     } else {
354         PrintError("Invalid VM Class\n");
355         return -1;
356     }
357
358     return 0;
359 }
360
361
362
363 static int post_config_vm(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
364     
365     vm->run_state = VM_STOPPED;
366
367     // Configure the memory map for the guest
368     if (setup_memory_map(vm, cfg) == -1) {
369         PrintError("Setting up guest memory map failed...\n");
370         return -1;
371     }
372
373     /* 
374      * Initialize configured extensions 
375      */
376     if (setup_extensions(vm, cfg) == -1) {
377         PrintError("Failed to setup extensions\n");
378         return -1;
379     }
380
381     /* 
382      * Initialize configured devices
383      */
384     if (setup_devices(vm, cfg) == -1) {
385         PrintError("Failed to setup devices\n");
386         return -1;
387     }
388
389
390     //    v3_print_io_map(info);
391     v3_print_msr_map(vm);
392
393
394     if (vm->vm_class == V3_PC_VM) {
395         if (post_config_pc(vm, cfg) == -1) {
396             PrintError("PC Post configuration failure\n");
397             return -1;
398         }
399     } else {
400         PrintError("Invalid VM Class\n");
401         return -1;
402     }
403
404     return 0;
405 }
406
407
408
409 static int post_config_core(struct guest_info * info, v3_cfg_tree_t * cfg) {
410
411     info->core_run_state = CORE_STOPPED;
412  
413     if (v3_init_core_extensions(info) == -1) {
414         PrintError("Error intializing extension core states\n");
415         return -1;
416     }
417
418     if (info->vm_info->vm_class == V3_PC_VM) {
419         if (post_config_pc_core(info, cfg) == -1) {
420             PrintError("PC Post configuration failure\n");
421             return -1;
422         }
423     } else {
424         PrintError("Invalid VM Class\n");
425         return -1;
426     }
427
428
429     return 0;
430 }
431
432
433
434 static struct v3_vm_info * allocate_guest(int num_cores) {
435     int guest_state_size = sizeof(struct v3_vm_info) + (sizeof(struct guest_info) * num_cores);
436     struct v3_vm_info * vm = V3_Malloc(guest_state_size);
437
438     memset(vm, 0, guest_state_size);
439
440     vm->num_cores = num_cores;
441
442     return vm;
443 }
444
445
446
447 struct v3_vm_info * v3_config_guest(void * cfg_blob, void * priv_data) {
448     v3_cpu_arch_t cpu_type = v3_get_cpu_type(V3_Get_CPU());
449     struct v3_config * cfg_data = NULL;
450     struct v3_vm_info * vm = NULL;
451     int num_cores = 0;
452     int i = 0;
453     v3_cfg_tree_t * cores_cfg = NULL;
454     v3_cfg_tree_t * per_core_cfg = NULL;
455
456     if (cpu_type == V3_INVALID_CPU) {
457         PrintError("Configuring guest on invalid CPU\n");
458         return NULL;
459     }
460
461     cfg_data = parse_config(cfg_blob);
462
463     if (!cfg_data) {
464         PrintError("Could not parse configuration\n");
465         return NULL;
466     }
467
468     cores_cfg = v3_cfg_subtree(cfg_data->cfg, "cores");
469
470     if (!cores_cfg) {
471         PrintError("Could not find core configuration (new config format required)\n");
472         return NULL;
473     }
474
475     num_cores = atoi(v3_cfg_val(cores_cfg, "count"));
476     if (num_cores == 0) {
477         PrintError("No cores specified in configuration\n");
478         return NULL;
479     }
480
481     V3_Print("Configuring %d cores\n", num_cores);
482
483     vm = allocate_guest(num_cores);    
484
485     if (!vm) {
486         PrintError("Could not allocate %d core guest\n", vm->num_cores);
487         return NULL;
488     }
489
490     vm->host_priv_data = priv_data;
491
492     vm->cfg_data = cfg_data;
493
494     V3_Print("Preconfiguration\n");
495
496     if (pre_config_vm(vm, vm->cfg_data->cfg) == -1) {
497         PrintError("Error in preconfiguration\n");
498         return NULL;
499     }
500
501     V3_Print("Per core configuration\n");
502     per_core_cfg = v3_cfg_subtree(cores_cfg, "core");
503
504     // per core configuration
505     for (i = 0; i < vm->num_cores; i++) {
506         struct guest_info * info = &(vm->cores[i]);
507
508         info->cpu_id = i;
509         info->vm_info = vm;
510         info->core_cfg_data = per_core_cfg;
511
512         if (pre_config_core(info, per_core_cfg) == -1) {
513             PrintError("Error in core %d preconfiguration\n", i);
514             return NULL;
515         }
516
517
518         per_core_cfg = v3_cfg_next_branch(per_core_cfg);
519     }
520
521
522     V3_Print("Post Configuration\n");
523
524     if (post_config_vm(vm, vm->cfg_data->cfg) == -1) {
525         PrintError("Error in postconfiguration\n");
526         return NULL;
527     }
528
529
530     per_core_cfg = v3_cfg_subtree(cores_cfg, "core");
531
532     // per core configuration
533     for (i = 0; i < vm->num_cores; i++) {
534         struct guest_info * info = &(vm->cores[i]);
535
536         post_config_core(info, per_core_cfg);
537
538         per_core_cfg = v3_cfg_next_branch(per_core_cfg);
539     }
540
541     V3_Print("Configuration successfull\n");
542
543     return vm;
544 }
545
546
547
548 int v3_free_config(struct v3_vm_info * vm) {
549    
550     v3_free_htable(vm->cfg_data->file_table, 1, 0);
551
552     v3_xml_free(vm->cfg_data->cfg);
553
554     V3_Free(vm->cfg_data);
555     return 0;
556 }
557
558
559
560
561 static int setup_memory_map(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
562     v3_cfg_tree_t * mem_region = v3_cfg_subtree(v3_cfg_subtree(cfg, "memmap"), "region");
563
564     while (mem_region) {
565         addr_t start_addr = atox(v3_cfg_val(mem_region, "start"));
566         addr_t end_addr = atox(v3_cfg_val(mem_region, "end"));
567         addr_t host_addr = atox(v3_cfg_val(mem_region, "host_addr"));
568
569     
570         if (v3_add_shadow_mem(vm, V3_MEM_CORE_ANY, start_addr, end_addr, host_addr) == -1) {
571             PrintError("Could not map memory region: %p-%p => %p\n", 
572                        (void *)start_addr, (void *)end_addr, (void *)host_addr);
573             return -1;
574         }
575
576         mem_region = v3_cfg_next_branch(mem_region);
577     }
578
579     return 0;
580 }
581
582
583 static int setup_extensions(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
584     v3_cfg_tree_t * extension = v3_cfg_subtree(v3_cfg_subtree(cfg, "extensions"), "extension");
585
586     while (extension) {
587         char * ext_name = v3_cfg_val(extension, "name");
588
589         V3_Print("Configuring extension %s\n", ext_name);
590
591         if (v3_add_extension(vm, ext_name, extension) == -1) {
592             PrintError("Error adding extension %s\n", ext_name);
593             return -1;
594         }
595
596         extension = v3_cfg_next_branch(extension);
597     }
598
599     return 0;
600 }
601
602
603 static int setup_devices(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
604     v3_cfg_tree_t * device = v3_cfg_subtree(v3_cfg_subtree(cfg, "devices"), "device");
605
606     
607     while (device) {
608         char * dev_class = v3_cfg_val(device, "class");
609
610         V3_Print("configuring device %s\n", dev_class);
611
612         if (v3_create_device(vm, dev_class, device) == -1) {
613             PrintError("Error creating device %s\n", dev_class);
614             return -1;
615         }
616         
617         device = v3_cfg_next_branch(device);
618     }
619
620     v3_print_dev_mgr(vm);
621
622     return 0;
623 }
624
625
626