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.


f3cb4cadaacb59ad7d243a6c4195a5ff0353734a
[palacios.git] / palacios / src / palacios / vmm_cpu_mapper.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) 2013, Oscar Mondragon <omondrag@cs.unm.edu>
11  * Copyright (c) 2013, Patrick G. Bridges <bridges@cs.unm.edu>
12  * Copyright (c) 2013, The V3VEE Project <http://www.v3vee.org>
13  * All rights reserved.
14  *
15  * Author: Oscar Mondragon <omondrag@cs.unm.edu>
16  *         Patrick G. Bridges <bridges@cs.unm.edu>
17  *
18  * This is free software.  You are permitted to use,
19  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20  */
21
22 #include <palacios/vmm.h>
23 #include <palacios/vm_guest.h>
24 #include <palacios/vmm_cpu_mapper.h>
25 #include <palacios/vmm_hashtable.h>
26
27 #ifndef V3_CONFIG_DEBUG_CPU_MAPPER
28 #undef PrintDebug
29 #define PrintDebug(fmt, args...)
30 #endif
31
32 static char default_strategy[] = "default";
33 static struct hashtable * master_cpu_mapper_table = NULL;
34 static int create_default_cpu_mapper();
35 static int destroy_default_cpu_mapper();
36
37 static struct vm_cpu_mapper_impl *cpu_mapper = NULL;
38
39 static uint_t cpu_mapper_hash_fn(addr_t key) {
40     char * name = (char *)key;
41     return v3_hash_buffer((uint8_t *)name, strlen(name));
42 }
43
44 static int cpu_mapper_eq_fn(addr_t key1, addr_t key2) {
45     char * name1 = (char *)key1;
46     char * name2 = (char *)key2;
47
48     return (strcmp(name1, name2) == 0);
49 }
50
51 int V3_init_cpu_mapper() {
52
53      PrintDebug(VM_NONE, VCORE_NONE,"Initializing cpu_mapper");
54
55     master_cpu_mapper_table = v3_create_htable(0, cpu_mapper_hash_fn, cpu_mapper_eq_fn);
56     return create_default_cpu_mapper();
57 }
58
59
60 int V3_deinit_cpu_mapper() {
61
62     destroy_default_cpu_mapper();
63     v3_free_htable(master_cpu_mapper_table, 1, 1);
64     return 0;
65 }
66
67 int v3_register_cpu_mapper(struct vm_cpu_mapper_impl *s) {
68
69     PrintDebug(VM_NONE, VCORE_NONE,"Registering cpu_mapper (%s)\n", s->name);
70
71     if (v3_htable_search(master_cpu_mapper_table, (addr_t)(s->name))) {
72         PrintError(VM_NONE, VCORE_NONE, "Multiple instances of cpu_mapper (%s)\n", s->name);
73         return -1;
74     }
75
76     if (v3_htable_insert(master_cpu_mapper_table,
77                          (addr_t)(s->name),
78                          (addr_t)(s)) == 0) {
79         PrintError(VM_NONE, VCORE_NONE, "Could not register cpu_mapper (%s)\n", s->name);
80         return -1;
81     }
82
83     return 0;
84 }
85
86 struct vm_cpu_mapper_impl *v3_unregister_cpu_mapper(char *name) {
87
88     PrintDebug(VM_NONE, VCORE_NONE,"Unregistering cpu_mapper (%s)\n",name);
89
90     struct vm_cpu_mapper_impl *f = (struct vm_cpu_mapper_impl *) v3_htable_remove(master_cpu_mapper_table,(addr_t)(name),0);
91
92     if (!f) { 
93         PrintError(VM_NONE,VCORE_NONE,"Could not find cpu_mapper (%s)\n",name);
94         return NULL;
95     } else {
96         return f;
97     }
98 }
99
100
101 struct vm_cpu_mapper_impl *v3_cpu_mapper_lookup(char *name)
102 {
103     return (struct vm_cpu_mapper_impl *)v3_htable_search(master_cpu_mapper_table, (addr_t)(name));
104 }
105
106 int V3_enable_cpu_mapper() {
107     char *mapper_name;
108
109     cpu_mapper = NULL;
110     mapper_name = v3_lookup_option("cpu_mapper");
111
112     if (mapper_name) {
113         cpu_mapper = v3_cpu_mapper_lookup(mapper_name);
114     }
115
116     if (!cpu_mapper) {
117         cpu_mapper = v3_cpu_mapper_lookup(default_strategy);
118     }
119
120     if (!cpu_mapper) {
121         PrintError(VM_NONE, VCORE_NONE,"Specified Palacios cpu_mapper \"%s\" not found.\n", default_strategy);
122         return -1;
123     }
124
125     PrintDebug(VM_NONE, VCORE_NONE,"cpu_mapper %s found",cpu_mapper->name);
126
127     if (cpu_mapper->init) {
128         return cpu_mapper->init();
129     } else {
130         return 0;
131     }
132 }
133
134 int V3_disable_cpu_mapper()
135 {
136     if (cpu_mapper->deinit) { 
137         return cpu_mapper->deinit();
138     } else {
139         return 0;
140     }
141 }
142
143 int v3_cpu_mapper_register_vm(struct v3_vm_info *vm,unsigned int cpu_mask) {
144     if (cpu_mapper->vm_init) {
145         return cpu_mapper->vm_init(vm,cpu_mask);
146     } else {
147         return 0;
148     }
149 }
150
151 int v3_cpu_mapper_admit_vm(struct v3_vm_info *vm) {
152     if (cpu_mapper->admit) {
153         return cpu_mapper->admit(vm);
154     } else {
155         return 0;
156     }
157 }
158
159
160 int v3_cpu_mapper_admit_core(struct v3_vm_info * vm, int vcore_id, int target_cpu) {
161     if (cpu_mapper->admit_core) {
162         return cpu_mapper->admit_core(vm,vcore_id,target_cpu);
163     } else {
164         return 0;
165     }
166 }
167
168
169
170 int default_mapper_vm_init(struct v3_vm_info *vm, unsigned int cpu_mask)
171 {
172
173     PrintDebug(vm, VCORE_NONE,"mapper. default_mapper_init\n");
174
175     uint32_t i;
176     int vcore_id = 0;
177     uint8_t * core_mask = (uint8_t *)&cpu_mask;
178
179     for (i = 0, vcore_id = vm->num_cores - 1; vcore_id >= 0; i++) {
180
181         int major = 0;
182         int minor = 0;
183         struct guest_info * core = &(vm->cores[vcore_id]);
184         char * specified_cpu = v3_cfg_val(core->core_cfg_data, "target_cpu");
185         uint32_t core_idx = 0;
186
187         if (specified_cpu != NULL) {
188             core_idx = atoi(specified_cpu);
189
190             if (core_idx < 0) {
191                 PrintError(vm, VCORE_NONE, "Target CPU out of bounds (%d) \n", core_idx);
192             }
193
194             i--; // We reset the logical core idx. Not strictly necessary I guess...
195         } else {
196             core_idx = i;
197         }
198
199         major = core_idx / 8;
200         minor = core_idx % 8;
201
202         if ((core_mask[major] & (0x1 << minor)) == 0) {
203             PrintError(vm, VCORE_NONE, "Logical CPU %d not available for virtual core %d; not started\n",
204                        core_idx, vcore_id);
205
206             if (specified_cpu != NULL) {
207                 PrintError(vm, VCORE_NONE, "CPU was specified explicitly (%d). HARD ERROR\n", core_idx);
208                 v3_stop_vm(vm);
209                 return -1;
210             }
211
212             continue;
213
214         }
215
216     core->pcpu_id = core_idx;
217     vcore_id--;
218     }
219
220     if (vcore_id >= 0) {
221         v3_stop_vm(vm);
222         return -1;
223     }
224
225     return 0;
226
227 }
228
229 int default_mapper_admit_core(struct v3_vm_info * vm, int vcore_id, int target_cpu){
230     return 0;
231 }
232
233
234 int default_mapper_admit(struct v3_vm_info *vm){
235     return 0;
236 }
237
238
239 static struct vm_cpu_mapper_impl default_mapper_impl = {
240     .name = "default",
241     .init = NULL,
242     .deinit = NULL,
243     .vm_init = default_mapper_vm_init,
244     .vm_deinit = NULL,
245     .admit_core = default_mapper_admit_core,
246     .admit = default_mapper_admit
247
248 };
249
250 static int create_default_cpu_mapper()
251 {
252         v3_register_cpu_mapper(&default_mapper_impl);
253         return 0;
254 }
255
256 static int destroy_default_cpu_mapper()
257 {
258         v3_unregister_cpu_mapper(default_mapper_impl.name);
259         return 0;
260 }