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.


hardware multicore support
[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 #include <palacios/vmm_mem.h>
26 #include <palacios/vmm_types.h>
27
28
29
30 #ifdef __V3VEE__
31
32 //#include <palacios/vmm_types.h>
33 #include <palacios/vmm_string.h>
34
35
36 //#include <palacios/vmm_paging.h>
37
38 /* utility definitions */
39
40
41 #define V3_Print(fmt, args...)                                  \
42     do {                                                        \
43         extern struct v3_os_hooks * os_hooks;                   \
44         if ((os_hooks) && (os_hooks)->print) {                  \
45             (os_hooks)->print((fmt), ##args);                   \
46         }                                                       \
47     } while (0) 
48
49
50 #define PrintDebug(fmt, args...)                        \
51     do {                                                \
52         extern struct v3_os_hooks * os_hooks;           \
53         if ((os_hooks) && (os_hooks)->print) {  \
54             (os_hooks)->print((fmt), ##args);   \
55         }                                               \
56     } while (0)                                         
57
58
59 #define PrintError(fmt, args...)                                        \
60     do {                                                                \
61         extern struct v3_os_hooks * os_hooks;                           \
62         if ((os_hooks) && (os_hooks)->print) {                  \
63             (os_hooks)->print("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
64         }                                                               \
65     } while (0)                                         
66
67
68
69 #define V3_AllocPages(num_pages)                                \
70     ({                                                          \
71         extern struct v3_os_hooks * os_hooks;                   \
72         void * ptr = 0;                                         \
73         if ((os_hooks) && (os_hooks)->allocate_pages) {         \
74             ptr = (os_hooks)->allocate_pages(num_pages);        \
75         }                                                       \
76         ptr;                                                    \
77     })
78
79
80 #define V3_FreePage(page)                               \
81     do {                                                \
82         extern struct v3_os_hooks * os_hooks;           \
83         if ((os_hooks) && (os_hooks)->free_page) {      \
84             (os_hooks)->free_page(page);                \
85         }                                               \
86     } while(0)
87
88
89 #define V3_VAddr(addr) ({                                       \
90             extern struct v3_os_hooks * os_hooks;               \
91             void * var = 0;                                     \
92             if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {     \
93                 var = (os_hooks)->paddr_to_vaddr(addr);         \
94             }                                                   \
95             var;                                                \
96         })
97
98
99 #define V3_PAddr(addr) ({                                       \
100             extern struct v3_os_hooks * os_hooks;               \
101             void * var = 0;                                     \
102             if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {     \
103                 var = (os_hooks)->vaddr_to_paddr(addr);         \
104             }                                                   \
105             var;                                                \
106         })
107
108
109
110 #define V3_Malloc(size) ({                              \
111             extern struct v3_os_hooks * os_hooks;       \
112             void * var = 0;                             \
113             if ((os_hooks) && (os_hooks)->malloc) {     \
114                 var = (os_hooks)->malloc(size);         \
115             }                                           \
116             var;                                        \
117         })
118
119 // We need to check the hook structure at runtime to ensure its SAFE
120 #define V3_Free(addr)                           \
121     do {                                        \
122         extern struct v3_os_hooks * os_hooks;   \
123         if ((os_hooks) && (os_hooks)->free) {   \
124             (os_hooks)->free(addr);             \
125         }                                       \
126     } while (0)
127
128 // uint_t V3_CPU_KHZ();
129 #define V3_CPU_KHZ() ({                                                 \
130             unsigned int khz = 0;                                       \
131             extern struct v3_os_hooks * os_hooks;                       \
132             if ((os_hooks) && (os_hooks)->get_cpu_khz) {                \
133                 khz = (os_hooks)->get_cpu_khz();                        \
134             }                                                           \
135             khz;                                                        \
136         })                                                              \
137         
138
139
140 #define V3_CREATE_THREAD(fn, arg, name)                         \
141     do {                                                        \
142         extern struct v3_os_hooks * os_hooks;                   \
143         if ((os_hooks) && (os_hooks)->start_kernel_thread) {    \
144             (os_hooks)->start_kernel_thread(fn, arg, name);     \
145         }                                                       \
146     } while (0)
147
148
149 #define V3_Hook_Interrupt(irq, opaque) ({                               \
150             int ret = 0;                                                \
151             extern struct v3_os_hooks * os_hooks;                       \
152             if ((os_hooks) && (os_hooks)->hook_interrupt) {             \
153                 ret = (os_hooks)->hook_interrupt(irq, opaque);          \
154             }                                                           \
155             ret;                                                        \
156         })                                                              \
157         
158
159 #define V3_ACK_IRQ(irq)                                         \
160     do {                                                        \
161         extern struct v3_os_hooks * os_hooks;                   \
162         if ((os_hooks) && (os_hooks)->ack_irq) {                \
163             (os_hooks)->ack_irq(irq);                           \
164         }                                                       \
165     } while (0)
166
167
168
169
170
171 /* ** */
172
173 #define V3_ASSERT(x)                                                    \
174     do {                                                                \
175         if (!(x)) {                                                     \
176             PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
177                        __func__, #x, __FILE__, __LINE__,                \
178                        (ulong_t) __builtin_return_address(0));          \
179             while(1);                                                   \
180         }                                                               \
181     } while(0)                                                          \
182         
183
184
185
186 #define VMM_INVALID_CPU 0
187 #define VMM_VMX_CPU 1
188 #define VMM_SVM_CPU 2
189
190
191 // Maybe make this a define....
192 typedef enum v3_cpu_arch {V3_INVALID_CPU, V3_SVM_CPU, V3_SVM_REV3_CPU, V3_VMX_CPU, V3_VMX_EPT_CPU} v3_cpu_arch_t;
193
194
195 v3_cpu_mode_t v3_get_host_cpu_mode();
196
197 void v3_yield(struct guest_info * info);
198 void v3_yield_cond(struct guest_info * info);
199
200
201 void v3_interrupt_cpu(struct guest_info * vm, int logical_cpu);
202
203
204 #endif //!__V3VEE__
205
206
207
208 struct guest_info;
209
210 /* This will contain function pointers that provide OS services */
211 struct v3_os_hooks {
212     void (*print)(const char * format, ...)
213         __attribute__ ((format (printf, 1, 2)));
214   
215     void *(*allocate_pages)(int numPages);
216     void (*free_page)(void * page);
217
218     void *(*malloc)(unsigned int size);
219     void (*free)(void * addr);
220
221     void *(*paddr_to_vaddr)(void *addr);
222     void *(*vaddr_to_paddr)(void *addr);
223
224     int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
225
226     int (*ack_irq)(int irq);
227
228     unsigned int (*get_cpu_khz)(void);
229
230     void (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name); 
231
232     void (*yield_cpu)(void); 
233
234     void *(*mutex_alloc)(void);
235     void (*mutex_free)(void * mutex);
236     void (*mutex_lock)(void * mutex, int must_spin);
237     void (*mutex_unlock)(void * mutex);
238
239     void (*interrupt_cpu)(struct guest_info * vm, int logical_cpu);
240     void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
241     void (*start_thread_on_cpu)(int logical_cpu, int (*fn)(void * arg), void * arg, char * thread_name);
242 };
243
244
245
246 typedef enum {NONE, HARDDRIVE, CDROM, VIRTIO} v3_disk_type_t;
247 typedef enum {RAM, NETWORK} v3_disk_connection_t;
248
249 union v3_disk_info {
250     struct {
251         void * data_ptr;
252         int size;
253     } ram;
254
255     struct {
256         char * ip_str;
257         int port;
258         char * disk_name;
259     } net;
260 };
261
262 struct v3_vm_config {
263
264     unsigned long mem_size; // in bytes, var should be natural size of cpu
265     // so we can specify maximum physical address size
266     // (We're screwed if we want to do 32 bit host/64 bit guest)
267
268     int enable_telemetry;
269     int enable_nested_paging;
270
271     int enable_pci;
272
273     int enable_swap;
274
275     int guest_cpu;
276
277     unsigned long schedule_freq; // in HZ
278
279     v3_disk_type_t pri_disk_type;
280     v3_disk_connection_t pri_disk_con;
281     union v3_disk_info pri_disk_info;
282    
283     v3_disk_type_t sec_disk_type;
284     v3_disk_connection_t sec_disk_con;
285     union v3_disk_info sec_disk_info;
286 };
287
288
289
290 /* This will contain Function pointers that control the VMs */
291 struct v3_ctrl_ops {
292     struct guest_info *(*allocate_guest)(void);
293
294     int (*init_guest)(struct guest_info * info, struct v3_vm_config * config_ptr);
295     int (*start_guest)(struct guest_info * info);
296     //  int (*stop_vm)(uint_t vm_id);
297
298     int (*has_nested_paging)(void);
299
300     //  v3_cpu_arch_t (*get_cpu_arch)();
301 };
302
303
304
305
306 //
307 //
308 // This is the interrupt state that the VMM's interrupt handlers need to see
309 //
310 struct v3_interrupt {
311     unsigned int irq;
312     unsigned int error;
313
314     unsigned int should_ack;  // Should the vmm ack this interrupt, or will
315     // the host OS do it?
316 };
317
318
319
320
321 void Init_V3(struct v3_os_hooks * hooks, struct v3_ctrl_ops * vmm_ops, int num_cpus);
322
323 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);
324
325
326
327 #endif