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>
29 //#include <palacios/vmm_types.h>
30 #include <palacios/vmm_string.h>
33 //#include <palacios/vmm_paging.h>
35 /* utility definitions */
38 #define PrintDebug(fmt, args...) \
40 extern struct vmm_os_hooks * os_hooks; \
41 if ((os_hooks) && (os_hooks)->print_debug) { \
42 (os_hooks)->print_debug((fmt), ##args); \
46 #define PrintDebug(fmt,args ...)
51 #define PrintError(fmt, args...) \
53 extern struct vmm_os_hooks * os_hooks; \
54 if ((os_hooks) && (os_hooks)->print_debug) { \
55 (os_hooks)->print_debug("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
62 #define PrintInfo(fmt, args...) \
64 extern struct vmm_os_hooks * os_hooks; \
65 if ((os_hooks) && (os_hooks)->print_info) { \
66 (os_hooks)->print_info((fmt), ##args); \
70 #define PrintInfo(fmt, args...)
75 #define PrintTrace(fmt, args...) \
77 extern struct vmm_os_hooks * os_hooks; \
78 if ((os_hooks) && (os_hooks)->print_trace) { \
79 (os_hooks)->print_trace(fmt, ##args); \
83 #define PrintTrace(fmt, args...)
87 #define V3_AllocPages(num_pages) \
89 extern struct vmm_os_hooks * os_hooks; \
91 if ((os_hooks) && (os_hooks)->allocate_pages) { \
92 ptr = (os_hooks)->allocate_pages(num_pages); \
98 #define V3_FreePage(page) \
100 extern struct vmm_os_hooks * os_hooks; \
101 if ((os_hooks) && (os_hooks)->free_page) { \
102 (os_hooks)->free_page(page); \
109 #define V3_Malloc(size) ({ \
110 extern struct vmm_os_hooks * os_hooks; \
112 if ((os_hooks) && (os_hooks)->malloc) { \
113 var = (os_hooks)->malloc(size); \
118 // We need to check the hook structure at runtime to ensure its SAFE
119 #define V3_Free(addr) \
121 extern struct vmm_os_hooks * os_hooks; \
122 if ((os_hooks) && (os_hooks)->free) { \
123 (os_hooks)->free(addr); \
128 // uint_t V3_CPU_KHZ();
129 #define V3_CPU_KHZ() \
131 unsigned int khz = 0; \
132 extern struct vmm_os_hooks * os_hooks; \
133 if ((os_hooks) && (os_hooks)->get_cpu_khz) { \
134 khz = (os_hooks)->get_cpu_khz(); \
141 #define V3_Hook_Interrupt(irq, opaque) \
144 extern struct vmm_os_hooks * os_hooks; \
145 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
146 ret = (os_hooks)->hook_interrupt(irq, opaque); \
151 #define V3_Yield(addr) \
153 extern struct vmm_os_hooks * os_hooks; \
154 if ((os_hooks) && (os_hooks)->yield_cpu) { \
155 (os_hooks)->yield_cpu(); \
165 #define V3_ASSERT(x) \
168 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
169 __func__, #x, __FILE__, __LINE__, \
170 (ulong_t) __builtin_return_address(0)); \
178 #define VMM_INVALID_CPU 0
179 #define VMM_VMX_CPU 1
180 #define VMM_SVM_CPU 2
183 // Maybe make this a define....
184 typedef enum v3_cpu_arch {V3_INVALID_CPU, V3_SVM_CPU, V3_SVM_REV3_CPU, V3_VMX_CPU} v3_cpu_arch_t;
193 // This is the interrupt state that the VMM's interrupt handlers need to see
195 struct vmm_intr_state {
199 unsigned int should_ack; // Should the vmm ack this interrupt, or will
200 // the host OS do it?
202 // This is the value given when the interrupt is hooked.
203 // This will never be NULL
207 void deliver_interrupt_to_vmm(struct vmm_intr_state * state);
210 /* This will contain function pointers that provide OS services */
211 struct vmm_os_hooks {
212 void (*print_info)(const char * format, ...);
213 void (*print_debug)(const char * format, ...);
214 void (*print_trace)(const char * format, ...);
216 void *(*allocate_pages)(int numPages);
217 void (*free_page)(void * page);
219 void *(*malloc)(unsigned int size);
220 void (*free)(void * addr);
222 void *(*paddr_to_vaddr)(void *addr);
223 void *(*vaddr_to_paddr)(void *addr);
225 // int (*hook_interrupt)(struct guest_info *s, int irq);
227 int (*hook_interrupt)(unsigned int irq, void *opaque);
229 int (*ack_irq)(int irq);
232 unsigned int (*get_cpu_khz)();
235 void (*start_kernel_thread)(); // include pointer to function
242 /* This will contain Function pointers that control the VMs */
243 struct vmm_ctrl_ops {
244 struct guest_info *(*allocate_guest)();
246 int (*config_guest)(struct guest_info * info, void * config_ptr);
247 int (*init_guest)(struct guest_info * info);
248 int (*start_guest)(struct guest_info * info);
249 // int (*stop_vm)(uint_t vm_id);
251 int (*has_nested_paging)();
253 // v3_cpu_arch_t (*get_cpu_arch)();
259 void Init_V3(struct vmm_os_hooks * hooks, struct vmm_ctrl_ops * vmm_ops);