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.


updated comments for compiler compatibility
[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 #define V3_AllocPages(num_pages)                                \
71     ({                                                          \
72         extern struct v3_os_hooks * os_hooks;                   \
73         void * ptr = 0;                                         \
74         if ((os_hooks) && (os_hooks)->allocate_pages) {         \
75             ptr = (os_hooks)->allocate_pages(num_pages);        \
76         }                                                       \
77         ptr;                                                    \
78     })
79
80
81 #define V3_FreePage(page)                               \
82     do {                                                \
83         extern struct v3_os_hooks * os_hooks;           \
84         if ((os_hooks) && (os_hooks)->free_page) {      \
85             (os_hooks)->free_page(page);                \
86         }                                               \
87     } while(0)
88
89
90 #define V3_VAddr(addr) ({                                       \
91             extern struct v3_os_hooks * os_hooks;               \
92             void * var = 0;                                     \
93             if ((os_hooks) && (os_hooks)->paddr_to_vaddr) {     \
94                 var = (os_hooks)->paddr_to_vaddr(addr);         \
95             }                                                   \
96             var;                                                \
97         })
98
99
100 #define V3_PAddr(addr) ({                                       \
101             extern struct v3_os_hooks * os_hooks;               \
102             void * var = 0;                                     \
103             if ((os_hooks) && (os_hooks)->vaddr_to_paddr) {     \
104                 var = (os_hooks)->vaddr_to_paddr(addr);         \
105             }                                                   \
106             var;                                                \
107         })
108
109
110
111 #define V3_Malloc(size) ({                              \
112             extern struct v3_os_hooks * os_hooks;       \
113             void * var = 0;                             \
114             if ((os_hooks) && (os_hooks)->malloc) {     \
115                 var = (os_hooks)->malloc(size);         \
116             }                                           \
117             if (!var) PrintError("MALLOC FAILURE. Memory LEAK!!\n");    \
118             var;                                        \
119         })
120
121 /* We need to check the hook structure at runtime to ensure its SAFE */
122 #define V3_Free(addr)                           \
123     do {                                        \
124         extern struct v3_os_hooks * os_hooks;   \
125         if ((os_hooks) && (os_hooks)->free) {   \
126             (os_hooks)->free(addr);             \
127         }                                       \
128     } while (0)
129
130 /* uint_t V3_CPU_KHZ(); */
131 #define V3_CPU_KHZ() ({                                                 \
132             unsigned int khz = 0;                                       \
133             extern struct v3_os_hooks * os_hooks;                       \
134             if ((os_hooks) && (os_hooks)->get_cpu_khz) {                \
135                 khz = (os_hooks)->get_cpu_khz();                        \
136             }                                                           \
137             khz;                                                        \
138         })                                                              \
139         
140
141
142 #define V3_CREATE_THREAD(fn, arg, name)                         \
143     do {                                                        \
144         extern struct v3_os_hooks * os_hooks;                   \
145         if ((os_hooks) && (os_hooks)->start_kernel_thread) {    \
146             (os_hooks)->start_kernel_thread(fn, arg, name);     \
147         }                                                       \
148     } while (0)
149
150
151 #define V3_Hook_Interrupt(irq, opaque) ({                               \
152             int ret = 0;                                                \
153             extern struct v3_os_hooks * os_hooks;                       \
154             if ((os_hooks) && (os_hooks)->hook_interrupt) {             \
155                 ret = (os_hooks)->hook_interrupt(irq, opaque);          \
156             }                                                           \
157             ret;                                                        \
158         })                                                              \
159         
160
161 #define V3_ACK_IRQ(irq)                                         \
162     do {                                                        \
163         extern struct v3_os_hooks * os_hooks;                   \
164         if ((os_hooks) && (os_hooks)->ack_irq) {                \
165             (os_hooks)->ack_irq(irq);                           \
166         }                                                       \
167     } while (0)
168
169
170
171
172
173 /* ** */
174
175 #define V3_ASSERT(x)                                                    \
176     do {                                                                \
177         if (!(x)) {                                                     \
178             PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
179                        __func__, #x, __FILE__, __LINE__,                \
180                        (ulong_t) __builtin_return_address(0));          \
181             while(1);                                                   \
182         }                                                               \
183     } while(0)                                                          \
184         
185
186
187
188
189 typedef enum v3_vm_class {V3_INVALID_VM, V3_PC_VM, V3_CRAY_VM} v3_vm_class_t;
190
191
192 /* Maybe make this a define.... */
193 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;
194
195
196 v3_cpu_mode_t v3_get_host_cpu_mode();
197
198 void v3_yield(struct guest_info * info);
199 void v3_yield_cond(struct guest_info * info);
200 void v3_print_cond(const char * fmt, ...);
201
202
203 void v3_interrupt_cpu(struct guest_info * vm, int logical_cpu);
204
205 unsigned int v3_get_cpu_id();
206
207 v3_cpu_arch_t v3_get_cpu_type(int cpu_id);
208
209
210 int v3_vm_enter(struct guest_info * info);
211
212
213 #endif /* !__V3VEE__ */
214
215
216
217 struct guest_info;
218
219 /* This will contain function pointers that provide OS services */
220 struct v3_os_hooks {
221     void (*print)(const char * format, ...)
222         __attribute__ ((format (printf, 1, 2)));
223   
224     void *(*allocate_pages)(int numPages);
225     void (*free_page)(void * page);
226
227     void *(*malloc)(unsigned int size);
228     void (*free)(void * addr);
229
230     void *(*paddr_to_vaddr)(void *addr);
231     void *(*vaddr_to_paddr)(void *addr);
232
233     int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
234
235     int (*ack_irq)(int irq);
236
237     unsigned int (*get_cpu_khz)(void);
238
239     void (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name); 
240
241     void (*yield_cpu)(void); 
242
243     void *(*mutex_alloc)(void);
244     void (*mutex_free)(void * mutex);
245     void (*mutex_lock)(void * mutex, int must_spin);
246     void (*mutex_unlock)(void * mutex);
247
248     unsigned int (*get_cpu)(void);
249     void (*interrupt_cpu)(struct guest_info * vm, int logical_cpu);
250     void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
251     void (*start_thread_on_cpu)(int logical_cpu, int (*fn)(void * arg), void * arg, char * thread_name);
252 };
253
254
255
256
257
258
259
260 /*
261  *
262  * This is the interrupt state that the VMM's interrupt handlers need to see
263  */
264 struct v3_interrupt {
265     unsigned int irq;
266     unsigned int error;
267
268     unsigned int should_ack;  /* Should the vmm ack this interrupt, or will
269                                * the host OS do it?
270                                */
271 };
272
273
274
275
276 void Init_V3(struct v3_os_hooks * hooks,  int num_cpus);
277
278
279 int v3_start_vm(struct guest_info * info, unsigned int cpu_mask);
280 struct guest_info * v3_create_vm(void * cfg);
281
282 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);
283
284
285
286 #endif