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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
24 /*#include <palacios/vm_guest.h>*/
30 #include <palacios/vmm_mem.h>
31 #include <palacios/vmm_types.h>
33 //#include <palacios/vmm_types.h>
34 #include <palacios/vmm_string.h>
39 /* utility definitions */
42 #define V3_Print(fmt, args...) \
44 extern struct v3_os_hooks * os_hooks; \
45 if ((os_hooks) && (os_hooks)->print) { \
46 (os_hooks)->print((fmt), ##args); \
51 #define PrintDebug(fmt, args...) \
53 extern struct v3_os_hooks * os_hooks; \
54 if ((os_hooks) && (os_hooks)->print) { \
55 (os_hooks)->print((fmt), ##args); \
60 #define PrintError(fmt, args...) \
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); \
71 #define V3_AllocPages(num_pages) \
73 extern struct v3_os_hooks * os_hooks; \
75 if ((os_hooks) && (os_hooks)->allocate_pages) { \
76 ptr = (os_hooks)->allocate_pages(num_pages,PAGE_SIZE_4KB); \
82 #define V3_AllocAlignedPages(num_pages, align) \
84 extern struct v3_os_hooks * os_hooks; \
86 if ((os_hooks) && (os_hooks)->allocate_pages) { \
87 ptr = (os_hooks)->allocate_pages(num_pages,align); \
93 #define V3_FreePages(page, num_pages) \
95 extern struct v3_os_hooks * os_hooks; \
96 if ((os_hooks) && (os_hooks)->free_pages) { \
97 (os_hooks)->free_pages(page, num_pages); \
102 #define V3_VAddr(addr) ({ \
103 extern struct v3_os_hooks * os_hooks; \
105 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { \
106 var = (os_hooks)->paddr_to_vaddr(addr); \
112 #define V3_PAddr(addr) ({ \
113 extern struct v3_os_hooks * os_hooks; \
115 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { \
116 var = (os_hooks)->vaddr_to_paddr(addr); \
123 #define V3_Malloc(size) ({ \
124 extern struct v3_os_hooks * os_hooks; \
126 if ((os_hooks) && (os_hooks)->malloc) { \
127 var = (os_hooks)->malloc(size); \
129 if (!var) PrintError("MALLOC FAILURE. Memory LEAK!!\n"); \
133 // We need to check the hook structure at runtime to ensure its SAFE
134 #define V3_Free(addr) \
136 extern struct v3_os_hooks * os_hooks; \
137 if ((os_hooks) && (os_hooks)->free) { \
138 (os_hooks)->free(addr); \
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(); \
155 #define V3_Hook_Interrupt(vm, irq) ({ \
157 extern struct v3_os_hooks * os_hooks; \
158 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
159 ret = (os_hooks)->hook_interrupt(vm, irq); \
165 #define V3_ACK_IRQ(irq) \
167 extern struct v3_os_hooks * os_hooks; \
168 if ((os_hooks) && (os_hooks)->ack_irq) { \
169 (os_hooks)->ack_irq(irq); \
175 #define V3_Get_CPU() ({ \
177 extern struct v3_os_hooks * os_hooks; \
178 if ((os_hooks) && (os_hooks)->get_cpu) { \
179 ret = (os_hooks)->get_cpu(); \
185 #ifdef CONFIG_MULTITHREAD_OS
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); \
197 #define V3_THREAD_SLEEP() \
199 extern struct v3_os_hooks * os_hooks; \
200 if ((os_hooks) && (os_hooks)->kernel_thread_sleep) { \
201 (os_hooks)->kernel_thread_sleep(); \
206 #define V3_THREAD_WAKEUP(thread) \
208 extern struct v3_os_hooks * os_hooks; \
209 if ((os_hooks) && (os_hooks)->kernel_thread_wakeup) { \
210 (os_hooks)->kernel_thread_wakeup(thread); \
216 #define V3_Call_On_CPU(cpu, fn, arg) \
218 extern struct v3_os_hooks * os_hooks; \
219 if ((os_hooks) && (os_hooks)->call_on_cpu) { \
220 (os_hooks)->call_on_cpu(cpu, fn, arg); \
226 #define V3_CREATE_THREAD_ON_CPU(cpu, fn, arg, name) ({ \
227 void * thread = NULL; \
228 extern struct v3_os_hooks * os_hooks; \
229 if ((os_hooks) && (os_hooks)->start_thread_on_cpu) { \
230 thread = (os_hooks)->start_thread_on_cpu(cpu, fn, arg, name); \
240 #define V3_ASSERT(x) \
242 extern struct v3_os_hooks * os_hooks; \
244 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
245 __func__, #x, __FILE__, __LINE__, \
246 (ulong_t) __builtin_return_address(0)); \
248 if ((os_hooks) && (os_hooks)->yield_cpu) { \
249 (os_hooks)->yield_cpu(); \
259 typedef enum v3_vm_class {V3_INVALID_VM, V3_PC_VM, V3_CRAY_VM} v3_vm_class_t;
262 // Maybe make this a define....
263 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;
266 v3_cpu_mode_t v3_get_host_cpu_mode();
268 void v3_yield(struct guest_info * info);
269 void v3_yield_cond(struct guest_info * info);
270 void v3_print_cond(const char * fmt, ...);
273 #ifdef CONFIG_MULTITHREAD_OS
274 void v3_interrupt_cpu(struct v3_vm_info * vm, int logical_cpu, int vector);
278 v3_cpu_arch_t v3_get_cpu_type(int cpu_id);
281 int v3_vm_enter(struct guest_info * info);
284 #endif /*!__V3VEE__ */
290 /* This will contain function pointers that provide OS services */
292 void (*print)(const char * format, ...)
293 __attribute__ ((format (printf, 1, 2)));
295 void *(*allocate_pages)(int num_pages, unsigned int alignment);
296 void (*free_pages)(void * page, int num_pages);
298 void *(*malloc)(unsigned int size);
299 void (*free)(void * addr);
301 void *(*paddr_to_vaddr)(void * addr);
302 void *(*vaddr_to_paddr)(void * addr);
304 int (*hook_interrupt)(struct v3_vm_info * vm, unsigned int irq);
305 int (*ack_irq)(int irq);
307 unsigned int (*get_cpu_khz)(void);
310 void (*yield_cpu)(void);
312 void *(*mutex_alloc)(void);
313 void (*mutex_free)(void * mutex);
314 void (*mutex_lock)(void * mutex, int must_spin);
315 void (*mutex_unlock)(void * mutex);
317 unsigned int (*get_cpu)(void);
321 void * (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name);
322 void (*kernel_thread_sleep)(void);
323 void (*kernel_thread_wakeup)(void * thread);
324 void (*interrupt_cpu)(struct v3_vm_info * vm, int logical_cpu, int vector);
325 void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
326 void * (*start_thread_on_cpu)(int cpu_id, int (*fn)(void * arg), void * arg, char * thread_name);
333 * This is the interrupt state that the VMM's interrupt handlers need to see
335 struct v3_interrupt {
339 unsigned int should_ack; /* Should the vmm ack this interrupt, or will
340 * the host OS do it? */
346 void Init_V3(struct v3_os_hooks * hooks, int num_cpus);
347 void Shutdown_V3( void );
350 struct v3_vm_info * v3_create_vm(void * cfg, void * priv_data, char * name);
351 int v3_start_vm(struct v3_vm_info * vm, unsigned int cpu_mask);
352 int v3_stop_vm(struct v3_vm_info * vm);
353 int v3_free_vm(struct v3_vm_info * vm);
355 int v3_deliver_irq(struct v3_vm_info * vm, struct v3_interrupt * intr);