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.


virtual console works with stream-serial implementation
[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 struct guest_info;
29
30
31 #ifdef __V3VEE__
32
33 //#include <palacios/vmm_types.h>
34 #include <palacios/vmm_string.h>
35
36
37 //#include <palacios/vmm_paging.h>
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_FreePage(page)                               \
94     do {                                                \
95         extern struct v3_os_hooks * os_hooks;           \
96         if ((os_hooks) && (os_hooks)->free_page) {      \
97             (os_hooks)->free_page(page);                \
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 #define V3_CREATE_THREAD(fn, arg, name)                         \
155     do {                                                        \
156         extern struct v3_os_hooks * os_hooks;                   \
157         if ((os_hooks) && (os_hooks)->start_kernel_thread) {    \
158             (os_hooks)->start_kernel_thread(fn, arg, name);     \
159         }                                                       \
160     } while (0)
161
162
163
164
165
166 #define V3_Hook_Interrupt(vm, irq) ({                                   \
167             int ret = 0;                                                \
168             extern struct v3_os_hooks * os_hooks;                       \
169             if ((os_hooks) && (os_hooks)->hook_interrupt) {             \
170                 ret = (os_hooks)->hook_interrupt(vm, irq);              \
171             }                                                           \
172             ret;                                                        \
173         })                                                              \
174         
175
176 #define V3_Get_CPU() ({                                                 \
177             int ret = 0;                                                \
178             extern struct v3_os_hooks * os_hooks;                       \
179             if ((os_hooks) && (os_hooks)->get_cpu) {                    \
180                 ret = (os_hooks)->get_cpu();                            \
181             }                                                           \
182             ret;                                                        \
183         })
184
185 #define V3_Call_On_CPU(cpu, fn, arg)                    \
186     do {                                                \
187         extern struct v3_os_hooks * os_hooks;           \
188         if ((os_hooks) && (os_hooks)->call_on_cpu) {    \
189             (os_hooks)->call_on_cpu(cpu, fn, arg);      \
190         }                                               \
191     } while (0)
192
193
194
195 #define V3_CREATE_THREAD_ON_CPU(cpu, fn, arg, name) ({                  \
196             void * thread = NULL;                                       \
197             extern struct v3_os_hooks * os_hooks;                       \
198             if ((os_hooks) && (os_hooks)->start_thread_on_cpu) {        \
199                 thread = (os_hooks)->start_thread_on_cpu(cpu, fn, arg, name); \
200             }                                                           \
201             thread;                                                     \
202         })
203
204
205 #define V3_ACK_IRQ(irq)                                         \
206     do {                                                        \
207         extern struct v3_os_hooks * os_hooks;                   \
208         if ((os_hooks) && (os_hooks)->ack_irq) {                \
209             (os_hooks)->ack_irq(irq);                           \
210         }                                                       \
211     } while (0)
212
213
214 #define V3_Reparent_Threadd()                                   \
215     do {                                                        \
216         if((os_hooks) && (os_hooks)->reparent_threaded) {       \
217             (os_hooks)->reparent_threaded();                    \
218         }                                                       \
219     } while(0)
220
221 /* ** */
222
223 #define V3_ASSERT(x)                                                    \
224     do {                                                                \
225         extern struct v3_os_hooks * os_hooks;                           \
226         if (!(x)) {                                                     \
227             PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
228                        __func__, #x, __FILE__, __LINE__,                \
229                        (ulong_t) __builtin_return_address(0));          \
230             while(1){                                                   \
231                 if ((os_hooks) && (os_hooks)->yield_cpu) {              \
232                         (os_hooks)->yield_cpu();                        \
233                 }                                                       \
234             }                                                           \
235         }                                                               \
236     } while(0)                                                          \
237         
238
239
240
241
242 typedef enum v3_vm_class {V3_INVALID_VM, V3_PC_VM, V3_CRAY_VM} v3_vm_class_t;
243
244
245 // Maybe make this a define....
246 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;
247
248
249 v3_cpu_mode_t v3_get_host_cpu_mode();
250
251 void v3_yield(struct guest_info * info);
252 void v3_yield_cond(struct guest_info * info);
253 void v3_print_cond(const char * fmt, ...);
254
255
256 void v3_interrupt_cpu(struct v3_vm_info * vm, int logical_cpu, int vector);
257
258 unsigned int v3_get_cpu_id();
259
260 v3_cpu_arch_t v3_get_cpu_type(int cpu_id);
261
262
263 int v3_vm_enter(struct guest_info * info);
264
265
266 #endif //!__V3VEE__
267
268
269
270 struct v3_vm_info;
271
272 /* This will contain function pointers that provide OS services */
273 struct v3_os_hooks {
274     void (*print)(const char * format, ...)
275         __attribute__ ((format (printf, 1, 2)));
276   
277     void *(*allocate_pages)(int numPages, unsigned int alignment);
278     void (*free_page)(void * page);
279
280     void *(*malloc)(unsigned int size);
281     void (*free)(void * addr);
282
283     void *(*paddr_to_vaddr)(void *addr);
284     void *(*vaddr_to_paddr)(void *addr);
285
286     int (*hook_interrupt)(struct v3_vm_info * vm, unsigned int irq);
287     int (*ack_irq)(int irq);
288
289     unsigned int (*get_cpu_khz)(void);
290
291     void (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name); 
292
293     void (*yield_cpu)(void); 
294
295     void *(*mutex_alloc)(void);
296     void (*mutex_free)(void * mutex);
297     void (*mutex_lock)(void * mutex, int must_spin);
298     void (*mutex_unlock)(void * mutex);
299
300     unsigned int (*get_cpu)(void);
301     void (*interrupt_cpu)(struct v3_vm_info * vm, int logical_cpu, int vector);
302     void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
303     void * (*start_thread_on_cpu)(int cpu_id, int (*fn)(void * arg), void * arg, char * thread_name);
304     void (*reparent_threadd)(void);
305 };
306
307
308 //
309 //
310 // This is the interrupt state that the VMM's interrupt handlers need to see
311 //
312 struct v3_interrupt {
313     unsigned int irq;
314     unsigned int error;
315
316     unsigned int should_ack;  // Should the vmm ack this interrupt, or will
317     // the host OS do it?
318 };
319
320
321
322
323 void Init_V3(struct v3_os_hooks * hooks,  int num_cpus);
324
325
326 struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data);
327 int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask);
328
329
330 int v3_deliver_irq(struct v3_vm_info * vm, struct v3_interrupt * intr);
331
332
333
334 #endif