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>
32 //#include <palacios/vmm_types.h>
33 #include <palacios/vmm_string.h>
36 //#include <palacios/vmm_paging.h>
38 /* utility definitions */
41 #define PrintDebug(fmt, args...) \
43 extern struct v3_os_hooks * os_hooks; \
44 if ((os_hooks) && (os_hooks)->print_debug) { \
45 (os_hooks)->print_debug((fmt), ##args); \
51 #define PrintDebug(fmt,args ...)
56 #define PrintError(fmt, args...) \
58 extern struct v3_os_hooks * os_hooks; \
59 if ((os_hooks) && (os_hooks)->print_debug) { \
60 (os_hooks)->print_debug("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
67 #define PrintInfo(fmt, args...) \
69 extern struct v3_os_hooks * os_hooks; \
70 if ((os_hooks) && (os_hooks)->print_info) { \
71 (os_hooks)->print_info((fmt), ##args); \
75 #define PrintInfo(fmt, args...)
80 #define PrintTrace(fmt, args...) \
82 extern struct v3_os_hooks * os_hooks; \
83 if ((os_hooks) && (os_hooks)->print_trace) { \
84 (os_hooks)->print_trace(fmt, ##args); \
88 #define PrintTrace(fmt, args...)
92 #define V3_AllocPages(num_pages) \
94 extern struct v3_os_hooks * os_hooks; \
96 if ((os_hooks) && (os_hooks)->allocate_pages) { \
97 ptr = (os_hooks)->allocate_pages(num_pages); \
103 #define V3_FreePage(page) \
105 extern struct v3_os_hooks * os_hooks; \
106 if ((os_hooks) && (os_hooks)->free_page) { \
107 (os_hooks)->free_page(page); \
112 #define V3_VAddr(addr) ({ \
113 extern struct v3_os_hooks * os_hooks; \
115 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { \
116 var = (os_hooks)->paddr_to_vaddr(addr); \
122 #define V3_PAddr(addr) ({ \
123 extern struct v3_os_hooks * os_hooks; \
125 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { \
126 var = (os_hooks)->vaddr_to_paddr(addr); \
133 #define V3_Malloc(size) ({ \
134 extern struct v3_os_hooks * os_hooks; \
136 if ((os_hooks) && (os_hooks)->malloc) { \
137 var = (os_hooks)->malloc(size); \
142 // We need to check the hook structure at runtime to ensure its SAFE
143 #define V3_Free(addr) \
145 extern struct v3_os_hooks * os_hooks; \
146 if ((os_hooks) && (os_hooks)->free) { \
147 (os_hooks)->free(addr); \
151 // uint_t V3_CPU_KHZ();
152 #define V3_CPU_KHZ() ({ \
153 unsigned int khz = 0; \
154 extern struct v3_os_hooks * os_hooks; \
155 if ((os_hooks) && (os_hooks)->get_cpu_khz) { \
156 khz = (os_hooks)->get_cpu_khz(); \
163 #define V3_CREATE_THREAD(fn, arg, name) \
165 extern struct v3_os_hooks * os_hooks; \
166 if ((os_hooks) && (os_hooks)->start_kernel_thread) { \
167 (os_hooks)->start_kernel_thread(fn, arg, name); \
172 #define V3_Hook_Interrupt(irq, opaque) ({ \
174 extern struct v3_os_hooks * os_hooks; \
175 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
176 ret = (os_hooks)->hook_interrupt(irq, opaque); \
182 #define V3_ACK_IRQ(irq) \
184 extern struct v3_os_hooks * os_hooks; \
185 if ((os_hooks) && (os_hooks)->ack_irq) { \
186 (os_hooks)->ack_irq(irq); \
191 #define V3_Yield(addr) \
193 extern struct v3_os_hooks * os_hooks; \
194 if ((os_hooks) && (os_hooks)->yield_cpu) { \
195 (os_hooks)->yield_cpu(); \
205 #define V3_ASSERT(x) \
208 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
209 __func__, #x, __FILE__, __LINE__, \
210 (ulong_t) __builtin_return_address(0)); \
218 #define VMM_INVALID_CPU 0
219 #define VMM_VMX_CPU 1
220 #define VMM_SVM_CPU 2
223 // Maybe make this a define....
224 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;
227 v3_cpu_mode_t v3_get_host_cpu_mode();
236 /* This will contain function pointers that provide OS services */
238 void (*print_info)(const char * format, ...)
239 __attribute__ ((format (printf, 1, 2)));
240 void (*print_debug)(const char * format, ...)
241 __attribute__ ((format (printf, 1, 2)));
242 void (*print_trace)(const char * format, ...)
243 __attribute__ ((format (printf, 1, 2)));
245 void *(*allocate_pages)(int numPages);
246 void (*free_page)(void * page);
248 void *(*malloc)(unsigned int size);
249 void (*free)(void * addr);
251 void *(*paddr_to_vaddr)(void *addr);
252 void *(*vaddr_to_paddr)(void *addr);
254 int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
256 int (*ack_irq)(int irq);
258 unsigned int (*get_cpu_khz)(void);
260 void (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name);
262 void (*yield_cpu)(void);
264 void *(*mutex_alloc)(void);
265 void (*mutex_free)(void * mutex);
266 void (*mutex_lock)(void * mutex, int must_spin);
267 void (*mutex_unlock)(void * mutex);
272 typedef enum {NONE, HARDDRIVE, CDROM, VIRTIO} v3_disk_type_t;
273 typedef enum {RAM, NETWORK} v3_disk_connection_t;
288 struct v3_vm_config {
295 unsigned long mem_size; // in bytes, var should be natural size of cpu
296 // so we can specify maximum physical address size
297 // (We're screwed if we want to do 32 bit host/64 bit guest)
299 int enable_profiling;
300 int enable_nested_paging;
306 v3_disk_type_t pri_disk_type;
307 v3_disk_connection_t pri_disk_con;
308 union v3_disk_info pri_disk_info;
310 v3_disk_type_t sec_disk_type;
311 v3_disk_connection_t sec_disk_con;
312 union v3_disk_info sec_disk_info;
317 /* This will contain Function pointers that control the VMs */
319 struct guest_info *(*allocate_guest)(void);
321 int (*init_guest)(struct guest_info * info, struct v3_vm_config * config_ptr);
322 int (*start_guest)(struct guest_info * info);
323 // int (*stop_vm)(uint_t vm_id);
325 int (*has_nested_paging)(void);
327 // v3_cpu_arch_t (*get_cpu_arch)();
335 // This is the interrupt state that the VMM's interrupt handlers need to see
337 struct v3_interrupt {
341 unsigned int should_ack; // Should the vmm ack this interrupt, or will
342 // the host OS do it?
348 void Init_V3(struct v3_os_hooks * hooks, struct v3_ctrl_ops * vmm_ops);
350 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);