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>
25 #include <palacios/vmm_mem.h>
26 #include <palacios/vmm_types.h>
33 //#include <palacios/vmm_types.h>
34 #include <palacios/vmm_string.h>
37 //#include <palacios/vmm_paging.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); \
70 #define V3_AllocPages(num_pages) \
72 extern struct v3_os_hooks * os_hooks; \
74 if ((os_hooks) && (os_hooks)->allocate_pages) { \
75 ptr = (os_hooks)->allocate_pages(num_pages); \
81 #define V3_FreePage(page) \
83 extern struct v3_os_hooks * os_hooks; \
84 if ((os_hooks) && (os_hooks)->free_page) { \
85 (os_hooks)->free_page(page); \
90 #define V3_VAddr(addr) ({ \
91 extern struct v3_os_hooks * os_hooks; \
93 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { \
94 var = (os_hooks)->paddr_to_vaddr(addr); \
100 #define V3_PAddr(addr) ({ \
101 extern struct v3_os_hooks * os_hooks; \
103 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { \
104 var = (os_hooks)->vaddr_to_paddr(addr); \
111 #define V3_Malloc(size) ({ \
112 extern struct v3_os_hooks * os_hooks; \
114 if ((os_hooks) && (os_hooks)->malloc) { \
115 var = (os_hooks)->malloc(size); \
120 // We need to check the hook structure at runtime to ensure its SAFE
121 #define V3_Free(addr) \
123 extern struct v3_os_hooks * os_hooks; \
124 if ((os_hooks) && (os_hooks)->free) { \
125 (os_hooks)->free(addr); \
129 // uint_t V3_CPU_KHZ();
130 #define V3_CPU_KHZ() ({ \
131 unsigned int khz = 0; \
132 extern struct v3_os_hooks * os_hooks; \
133 if ((os_hooks) && (os_hooks)->get_cpu_khz) { \
134 khz = (os_hooks)->get_cpu_khz(); \
141 #define V3_CREATE_THREAD(fn, arg, name) \
143 extern struct v3_os_hooks * os_hooks; \
144 if ((os_hooks) && (os_hooks)->start_kernel_thread) { \
145 (os_hooks)->start_kernel_thread(fn, arg, name); \
150 #define V3_Hook_Interrupt(irq, opaque) ({ \
152 extern struct v3_os_hooks * os_hooks; \
153 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
154 ret = (os_hooks)->hook_interrupt(irq, opaque); \
160 #define V3_ACK_IRQ(irq) \
162 extern struct v3_os_hooks * os_hooks; \
163 if ((os_hooks) && (os_hooks)->ack_irq) { \
164 (os_hooks)->ack_irq(irq); \
174 #define V3_ASSERT(x) \
177 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
178 __func__, #x, __FILE__, __LINE__, \
179 (ulong_t) __builtin_return_address(0)); \
188 typedef enum v3_vm_class {V3_INVALID_VM, V3_PC_VM, V3_CRAY_VM} v3_vm_class_t;
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;
195 v3_cpu_mode_t v3_get_host_cpu_mode();
197 void v3_yield(struct guest_info * info);
198 void v3_yield_cond(struct guest_info * info);
201 void v3_interrupt_cpu(struct guest_info * vm, int logical_cpu);
203 unsigned int v3_get_cpu_id();
205 v3_cpu_arch_t v3_get_cpu_type(int cpu_id);
208 int v3_vm_enter(struct guest_info * info);
217 /* This will contain function pointers that provide OS services */
219 void (*print)(const char * format, ...)
220 __attribute__ ((format (printf, 1, 2)));
222 void *(*allocate_pages)(int numPages);
223 void (*free_page)(void * page);
225 void *(*malloc)(unsigned int size);
226 void (*free)(void * addr);
228 void *(*paddr_to_vaddr)(void *addr);
229 void *(*vaddr_to_paddr)(void *addr);
231 int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
233 int (*ack_irq)(int irq);
235 unsigned int (*get_cpu_khz)(void);
237 void (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name);
239 void (*yield_cpu)(void);
241 void *(*mutex_alloc)(void);
242 void (*mutex_free)(void * mutex);
243 void (*mutex_lock)(void * mutex, int must_spin);
244 void (*mutex_unlock)(void * mutex);
246 unsigned int (*get_cpu)(void);
247 void (*interrupt_cpu)(struct guest_info * vm, int logical_cpu);
248 void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
249 void (*start_thread_on_cpu)(int logical_cpu, int (*fn)(void * arg), void * arg, char * thread_name);
260 // This is the interrupt state that the VMM's interrupt handlers need to see
262 struct v3_interrupt {
266 unsigned int should_ack; // Should the vmm ack this interrupt, or will
267 // the host OS do it?
273 void Init_V3(struct v3_os_hooks * hooks, int num_cpus);
276 int v3_start_vm(struct guest_info * info, unsigned int cpu_mask);
277 struct guest_info * v3_create_vm(void * cfg);
279 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);