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.


information request api for the host - gives a more detailed view of VM and vcore...
[palacios.git] / palacios / include / palacios / vmm.h
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 #ifndef __VMM_H__
21 #define __VMM_H__
22
23
24 /*#include <palacios/vm_guest.h>*/
25
26 struct guest_info;
27
28
29 #ifdef __V3VEE__
30 #include <palacios/vmm_mem.h>
31 #include <palacios/vmm_types.h>
32
33 //#include <palacios/vmm_types.h>
34 #include <palacios/vmm_string.h>
35
36
37
38
39 /* utility definitions */
40
41
42 #define V3_Print(fmt, args...)                                  \
43     do {                                                        \
44         extern struct v3_os_hooks * os_hooks;                   \
45         if ((os_hooks) && (os_hooks)->print) {                  \
46             (os_hooks)->print((fmt), ##args);                   \
47         }                                                       \
48     } while (0) 
49
50
51 #define PrintDebug(fmt, args...)                        \
52     do {                                                \
53         extern struct v3_os_hooks * os_hooks;           \
54         if ((os_hooks) && (os_hooks)->print) {  \
55             (os_hooks)->print((fmt), ##args);   \
56         }                                               \
57     } while (0)                                         
58
59
60 #define PrintError(fmt, args...)                                        \
61     do {                                                                \
62         extern struct v3_os_hooks * os_hooks;                           \
63         if ((os_hooks) && (os_hooks)->print) {                  \
64             (os_hooks)->print("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
65         }                                                               \
66     } while (0)                                         
67
68
69
70 /* 4KB-aligned */
71 #define V3_AllocPages(num_pages)                                        \
72     ({                                                                  \
73         extern struct v3_os_hooks * os_hooks;                           \
74         void * ptr = 0;                                                 \
75         if ((os_hooks) && (os_hooks)->allocate_pages) {                 \
76             ptr = (os_hooks)->allocate_pages(num_pages,PAGE_SIZE_4KB);  \
77         }                                                               \
78         ptr;                                                            \
79     })
80
81
82 #define V3_AllocAlignedPages(num_pages, align)                          \
83     ({                                                                  \
84         extern struct v3_os_hooks * os_hooks;                           \
85         void * ptr = 0;                                                 \
86         if ((os_hooks) && (os_hooks)->allocate_pages) {                 \
87             ptr = (os_hooks)->allocate_pages(num_pages,align);          \
88         }                                                               \
89         ptr;                                                            \
90     })
91
92
93 #define V3_FreePages(page, num_pages)                   \
94     do {                                                \
95         extern struct v3_os_hooks * os_hooks;           \
96         if ((os_hooks) && (os_hooks)->free_pages) {     \
97             (os_hooks)->free_pages(page, num_pages);    \
98         }                                               \
99     } while(0)
100
101
102 #define V3_VAddr(addr) ({                                       \
103             extern struct v3_os_hooks * os_hooks;               \
104             void * var = 0;                                     \
105             if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {     \
106                 var = (os_hooks)->paddr_to_vaddr(addr);         \
107             }                                                   \
108             var;                                                \
109         })
110
111
112 #define V3_PAddr(addr) ({                                       \
113             extern struct v3_os_hooks * os_hooks;               \
114             void * var = 0;                                     \
115             if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {     \
116                 var = (os_hooks)->vaddr_to_paddr(addr);         \
117             }                                                   \
118             var;                                                \
119         })
120
121
122
123 #define V3_Malloc(size) ({                              \
124             extern struct v3_os_hooks * os_hooks;       \
125             void * var = 0;                             \
126             if ((os_hooks) && (os_hooks)->malloc) {     \
127                 var = (os_hooks)->malloc(size);         \
128             }                                           \
129             if (!var) PrintError("MALLOC FAILURE. Memory LEAK!!\n");    \
130             var;                                        \
131         })
132
133 // We need to check the hook structure at runtime to ensure its SAFE
134 #define V3_Free(addr)                           \
135     do {                                        \
136         extern struct v3_os_hooks * os_hooks;   \
137         if ((os_hooks) && (os_hooks)->free) {   \
138             (os_hooks)->free(addr);             \
139         }                                       \
140     } while (0)
141
142 // uint_t V3_CPU_KHZ();
143 #define V3_CPU_KHZ() ({                                                 \
144             unsigned int khz = 0;                                       \
145             extern struct v3_os_hooks * os_hooks;                       \
146             if ((os_hooks) && (os_hooks)->get_cpu_khz) {                \
147                 khz = (os_hooks)->get_cpu_khz();                        \
148             }                                                           \
149             khz;                                                        \
150         })                                                              \
151         
152
153
154
155 #define V3_Hook_Interrupt(vm, irq) ({                                   \
156             int ret = 0;                                                \
157             extern struct v3_os_hooks * os_hooks;                       \
158             if ((os_hooks) && (os_hooks)->hook_interrupt) {             \
159                 ret = (os_hooks)->hook_interrupt(vm, irq);              \
160             }                                                           \
161             ret;                                                        \
162         })                                                              \
163         
164
165 #define V3_ACK_IRQ(irq)                                         \
166     do {                                                        \
167         extern struct v3_os_hooks * os_hooks;                   \
168         if ((os_hooks) && (os_hooks)->ack_irq) {                \
169             (os_hooks)->ack_irq(irq);                           \
170         }                                                       \
171     } while (0)
172
173
174
175 #define V3_Get_CPU() ({                                                 \
176             int ret = 0;                                                \
177             extern struct v3_os_hooks * os_hooks;                       \
178             if ((os_hooks) && (os_hooks)->get_cpu) {                    \
179                 ret = (os_hooks)->get_cpu();                            \
180             }                                                           \
181             ret;                                                        \
182         })
183
184
185
186
187 #define V3_CREATE_THREAD(fn, arg, name) ({                              \
188             void * thread = NULL;                                       \
189             extern struct v3_os_hooks * os_hooks;                       \
190             if ((os_hooks) && (os_hooks)->start_kernel_thread) {        \
191                 thread = (os_hooks)->start_kernel_thread(fn, arg, name); \
192             }                                                           \
193             thread;                                                     \
194         })
195
196
197
198
199 #define V3_Call_On_CPU(cpu, fn, arg)                    \
200     do {                                                \
201         extern struct v3_os_hooks * os_hooks;           \
202         if ((os_hooks) && (os_hooks)->call_on_cpu) {    \
203             (os_hooks)->call_on_cpu(cpu, fn, arg);      \
204         }                                               \
205     } while (0)
206
207
208
209 #define V3_CREATE_THREAD_ON_CPU(cpu, fn, arg, name) ({                  \
210             void * thread = NULL;                                       \
211             extern struct v3_os_hooks * os_hooks;                       \
212             if ((os_hooks) && (os_hooks)->start_thread_on_cpu) {        \
213                 thread = (os_hooks)->start_thread_on_cpu(cpu, fn, arg, name); \
214             }                                                           \
215             thread;                                                     \
216         })
217
218 #define V3_MOVE_THREAD_TO_CPU(pcpu, thread) ({                          \
219         int ret = -1;                                                   \
220         extern struct v3_os_hooks * os_hooks;                           \
221         if((os_hooks) && (os_hooks)->move_thread_to_cpu) {              \
222             ret = (os_hooks)->move_thread_to_cpu(pcpu, thread);         \
223         }                                                               \
224         ret;                                                            \
225     })
226     
227
228 /* ** */
229
230
231 #define V3_ASSERT(x)                                                    \
232     do {                                                                \
233         extern struct v3_os_hooks * os_hooks;                           \
234         if (!(x)) {                                                     \
235             PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
236                        __func__, #x, __FILE__, __LINE__,                \
237                        (ulong_t) __builtin_return_address(0));          \
238             while(1){                                                   \
239                 if ((os_hooks) && (os_hooks)->yield_cpu) {              \
240                         (os_hooks)->yield_cpu();                        \
241                 }                                                       \
242             }                                                           \
243         }                                                               \
244     } while(0)                                                          \
245         
246
247
248 #define V3_Yield()                                      \
249     do {                                                \
250         extern struct v3_os_hooks * os_hooks;           \
251         if ((os_hooks) && (os_hooks)->yield_cpu) {      \
252             (os_hooks)->yield_cpu();                    \
253         }                                               \
254     } while (0)                                         \
255
256
257 #define V3_Sleep(usec)                          \
258     do {                                                \
259         extern struct v3_os_hooks * os_hooks;           \
260         if ((os_hooks) && (os_hooks)->sleep_cpu) {\
261             (os_hooks)->sleep_cpu(usec);                \
262         } else {                                        \
263             V3_Yield();                                 \
264         }                                               \
265     }  while (0)                                        \
266
267 #define V3_Wakeup(cpu)                                  \
268     do {                                                \
269         extern struct v3_os_hooks * os_hooks;           \
270         if ((os_hooks) && (os_hooks)->wakeup_cpu) {     \
271             (os_hooks)->wakeup_cpu(cpu);                        \
272         }                                               \
273     } while (0)                                         \
274
275
276 typedef enum v3_vm_class {V3_INVALID_VM, V3_PC_VM, V3_CRAY_VM} v3_vm_class_t;
277
278
279 // Maybe make this a define....
280 typedef enum v3_cpu_arch {V3_INVALID_CPU, V3_SVM_CPU, V3_SVM_REV3_CPU, V3_VMX_CPU, V3_VMX_EPT_CPU, V3_VMX_EPT_UG_CPU} v3_cpu_arch_t;
281
282
283 v3_cpu_mode_t v3_get_host_cpu_mode();
284
285 void v3_yield(struct guest_info * info, int usec);
286 void v3_yield_cond(struct guest_info * info, int usec);
287 void v3_print_cond(const char * fmt, ...);
288
289 void v3_interrupt_cpu(struct v3_vm_info * vm, int logical_cpu, int vector);
290
291
292
293 v3_cpu_arch_t v3_get_cpu_type(int cpu_id);
294
295
296 int v3_vm_enter(struct guest_info * info);
297 int v3_reset_vm_core(struct guest_info * core, addr_t rip);
298
299
300 #endif /*!__V3VEE__ */
301
302
303
304 struct v3_vm_info;
305
306 /* This will contain function pointers that provide OS services */
307 struct v3_os_hooks {
308     void (*print)(const char * format, ...)
309         __attribute__ ((format (printf, 1, 2)));
310   
311     void *(*allocate_pages)(int num_pages, unsigned int alignment);
312     void (*free_pages)(void * page, int num_pages);
313
314     void *(*malloc)(unsigned int size);
315     void (*free)(void * addr);
316
317     void *(*paddr_to_vaddr)(void * addr);
318     void *(*vaddr_to_paddr)(void * addr);
319
320     int (*hook_interrupt)(struct v3_vm_info * vm, unsigned int irq);
321     int (*ack_irq)(int irq);
322
323     unsigned int (*get_cpu_khz)(void);
324
325     void (*yield_cpu)(void); 
326     void (*sleep_cpu)(unsigned int usec);
327     void (*wakeup_cpu)(void *cpu);
328
329     void *(*mutex_alloc)(void);
330     void (*mutex_free)(void * mutex);
331     void (*mutex_lock)(void * mutex, int must_spin);
332     void (*mutex_unlock)(void * mutex);
333     void *(*mutex_lock_irqsave)(void * mutex, int must_spin);
334     void (*mutex_unlock_irqrestore)(void * mutex, void *flags);
335
336     unsigned int (*get_cpu)(void);
337
338
339
340     void * (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name); 
341     void (*interrupt_cpu)(struct v3_vm_info * vm, int logical_cpu, int vector);
342     void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
343     void * (*start_thread_on_cpu)(int cpu_id, int (*fn)(void * arg), void * arg, char * thread_name);
344     int (*move_thread_to_cpu)(int cpu_id,  void * thread);
345 };
346
347
348 /*
349  *
350  * This is the interrupt state that the VMM's interrupt handlers need to see
351  */
352 struct v3_interrupt {
353     unsigned int irq;
354     unsigned int error;
355
356     unsigned int should_ack;  /* Should the vmm ack this interrupt, or will
357                                * the host OS do it? */
358 };
359
360
361 typedef enum {V3_VM_UNKNOWN, V3_VM_INVALID, V3_VM_RUNNING, V3_VM_STOPPED, V3_VM_PAUSED, V3_VM_ERROR, V3_VM_SIMULATING} v3_vm_state_t;
362 typedef enum {V3_VCORE_UNKNOWN, V3_VCORE_INVALID, V3_VCORE_RUNNING, V3_VCORE_STOPPED} v3_vcore_state_t;
363 typedef enum {V3_VCORE_CPU_UNKNOWN, V3_VCORE_CPU_REAL, V3_VCORE_CPU_PROTECTED, V3_VCORE_CPU_PROTECTED_PAE, V3_VCORE_CPU_LONG, V3_VCORE_CPU_LONG_32_COMPAT, V3_VCORE_CPU_LONG_16_COMPAT} v3_vcore_cpu_mode_t;
364
365 typedef enum {V3_VCORE_MEM_STATE_UNKNOWN, V3_VCORE_MEM_STATE_SHADOW, V3_VCORE_MEM_STATE_NESTED} v3_vcore_mem_state_t;
366 typedef enum {V3_VCORE_MEM_MODE_UNKNOWN, V3_VCORE_MEM_MODE_PHYSICAL, V3_VCORE_MEM_MODE_VIRTUAL} v3_vcore_mem_mode_t;
367
368 struct v3_vcore_state {
369   v3_vcore_state_t state;
370   v3_vcore_cpu_mode_t cpu_mode;
371   v3_vcore_mem_state_t mem_state;
372   v3_vcore_mem_mode_t mem_mode;
373   unsigned long pcore;
374   void *   last_rip;
375   unsigned long long num_exits;
376 };
377
378 struct v3_vm_state {
379   v3_vm_state_t state;
380   void *   mem_base_paddr;
381   unsigned long long  mem_size;
382   unsigned long  num_vcores;
383   struct v3_vcore_state vcore[0];
384 };
385
386 void Init_V3(struct v3_os_hooks * hooks, char * cpus, int num_cpus);
387 void Shutdown_V3( void );
388
389
390 struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name);
391 int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask);
392 int v3_stop_vm(struct v3_vm_info * vm);
393 int v3_pause_vm(struct v3_vm_info * vm);
394 int v3_continue_vm(struct v3_vm_info * vm);
395 int v3_simulate_vm(struct v3_vm_info * vm, unsigned int msecs);
396
397 int v3_save_vm(struct v3_vm_info * vm, char * store, char * url);
398 int v3_load_vm(struct v3_vm_info * vm, char * store, char * url);
399
400 int v3_send_vm(struct v3_vm_info * vm, char * store, char * url);
401 int v3_receive_vm(struct v3_vm_info * vm, char * store, char * url);
402
403 int v3_move_vm_core(struct v3_vm_info * vm, int vcore_id, int target_cpu);
404
405 int v3_free_vm(struct v3_vm_info * vm);
406
407 int v3_get_state_vm(struct v3_vm_info *vm, struct v3_vm_state *out);
408
409 int v3_deliver_irq(struct v3_vm_info * vm, struct v3_interrupt * intr);
410
411
412 #endif