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 V3_Print(fmt, args...) \
43 extern struct v3_os_hooks * os_hooks; \
44 if ((os_hooks) && (os_hooks)->print) { \
45 (os_hooks)->print((fmt), ##args); \
50 #define PrintDebug(fmt, args...) \
52 extern struct v3_os_hooks * os_hooks; \
53 if ((os_hooks) && (os_hooks)->print) { \
54 (os_hooks)->print((fmt), ##args); \
59 #define PrintError(fmt, args...) \
61 extern struct v3_os_hooks * os_hooks; \
62 if ((os_hooks) && (os_hooks)->print) { \
63 (os_hooks)->print("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
69 #define V3_AllocPages(num_pages) \
71 extern struct v3_os_hooks * os_hooks; \
73 if ((os_hooks) && (os_hooks)->allocate_pages) { \
74 ptr = (os_hooks)->allocate_pages(num_pages); \
80 #define V3_FreePage(page) \
82 extern struct v3_os_hooks * os_hooks; \
83 if ((os_hooks) && (os_hooks)->free_page) { \
84 (os_hooks)->free_page(page); \
89 #define V3_VAddr(addr) ({ \
90 extern struct v3_os_hooks * os_hooks; \
92 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { \
93 var = (os_hooks)->paddr_to_vaddr(addr); \
99 #define V3_PAddr(addr) ({ \
100 extern struct v3_os_hooks * os_hooks; \
102 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { \
103 var = (os_hooks)->vaddr_to_paddr(addr); \
110 #define V3_Malloc(size) ({ \
111 extern struct v3_os_hooks * os_hooks; \
113 if ((os_hooks) && (os_hooks)->malloc) { \
114 var = (os_hooks)->malloc(size); \
119 // We need to check the hook structure at runtime to ensure its SAFE
120 #define V3_Free(addr) \
122 extern struct v3_os_hooks * os_hooks; \
123 if ((os_hooks) && (os_hooks)->free) { \
124 (os_hooks)->free(addr); \
128 // uint_t V3_CPU_KHZ();
129 #define V3_CPU_KHZ() ({ \
130 unsigned int khz = 0; \
131 extern struct v3_os_hooks * os_hooks; \
132 if ((os_hooks) && (os_hooks)->get_cpu_khz) { \
133 khz = (os_hooks)->get_cpu_khz(); \
140 #define V3_CREATE_THREAD(fn, arg, name) \
142 extern struct v3_os_hooks * os_hooks; \
143 if ((os_hooks) && (os_hooks)->start_kernel_thread) { \
144 (os_hooks)->start_kernel_thread(fn, arg, name); \
149 #define V3_Hook_Interrupt(irq, opaque) ({ \
151 extern struct v3_os_hooks * os_hooks; \
152 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
153 ret = (os_hooks)->hook_interrupt(irq, opaque); \
159 #define V3_ACK_IRQ(irq) \
161 extern struct v3_os_hooks * os_hooks; \
162 if ((os_hooks) && (os_hooks)->ack_irq) { \
163 (os_hooks)->ack_irq(irq); \
173 #define V3_ASSERT(x) \
176 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
177 __func__, #x, __FILE__, __LINE__, \
178 (ulong_t) __builtin_return_address(0)); \
186 #define VMM_INVALID_CPU 0
187 #define VMM_VMX_CPU 1
188 #define VMM_SVM_CPU 2
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);
210 /* This will contain function pointers that provide OS services */
212 void (*print)(const char * format, ...)
213 __attribute__ ((format (printf, 1, 2)));
215 void *(*allocate_pages)(int numPages);
216 void (*free_page)(void * page);
218 void *(*malloc)(unsigned int size);
219 void (*free)(void * addr);
221 void *(*paddr_to_vaddr)(void *addr);
222 void *(*vaddr_to_paddr)(void *addr);
224 int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
226 int (*ack_irq)(int irq);
228 unsigned int (*get_cpu_khz)(void);
230 void (*start_kernel_thread)(int (*fn)(void * arg), void * arg, char * thread_name);
232 void (*yield_cpu)(void);
234 void *(*mutex_alloc)(void);
235 void (*mutex_free)(void * mutex);
236 void (*mutex_lock)(void * mutex, int must_spin);
237 void (*mutex_unlock)(void * mutex);
239 void (*interrupt_cpu)(struct guest_info * vm, int logical_cpu);
240 void (*call_on_cpu)(int logical_cpu, void (*fn)(void * arg), void * arg);
241 void (*start_thread_on_cpu)(int logical_cpu, int (*fn)(void * arg), void * arg, char * thread_name);
246 typedef enum {NONE, HARDDRIVE, CDROM, VIRTIO} v3_disk_type_t;
247 typedef enum {RAM, NETWORK} v3_disk_connection_t;
262 struct v3_vm_config {
264 unsigned long mem_size; // in bytes, var should be natural size of cpu
265 // so we can specify maximum physical address size
266 // (We're screwed if we want to do 32 bit host/64 bit guest)
268 int enable_telemetry;
269 int enable_nested_paging;
277 unsigned long schedule_freq; // in HZ
279 v3_disk_type_t pri_disk_type;
280 v3_disk_connection_t pri_disk_con;
281 union v3_disk_info pri_disk_info;
283 v3_disk_type_t sec_disk_type;
284 v3_disk_connection_t sec_disk_con;
285 union v3_disk_info sec_disk_info;
290 /* This will contain Function pointers that control the VMs */
292 struct guest_info *(*allocate_guest)(void);
294 int (*init_guest)(struct guest_info * info, struct v3_vm_config * config_ptr);
295 int (*start_guest)(struct guest_info * info);
296 // int (*stop_vm)(uint_t vm_id);
298 int (*has_nested_paging)(void);
300 // v3_cpu_arch_t (*get_cpu_arch)();
308 // This is the interrupt state that the VMM's interrupt handlers need to see
310 struct v3_interrupt {
314 unsigned int should_ack; // Should the vmm ack this interrupt, or will
315 // the host OS do it?
321 void Init_V3(struct v3_os_hooks * hooks, struct v3_ctrl_ops * vmm_ops, int num_cpus);
323 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);