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>
30 //#include <palacios/vmm_types.h>
31 #include <palacios/vmm_string.h>
34 //#include <palacios/vmm_paging.h>
36 /* utility definitions */
39 #define PrintDebug(fmt, args...) \
41 extern struct v3_os_hooks * os_hooks; \
42 if ((os_hooks) && (os_hooks)->print_debug) { \
43 (os_hooks)->print_debug((fmt), ##args); \
47 #define PrintDebug(fmt,args ...)
52 #define PrintError(fmt, args...) \
54 extern struct v3_os_hooks * os_hooks; \
55 if ((os_hooks) && (os_hooks)->print_debug) { \
56 (os_hooks)->print_debug("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
63 #define PrintInfo(fmt, args...) \
65 extern struct v3_os_hooks * os_hooks; \
66 if ((os_hooks) && (os_hooks)->print_info) { \
67 (os_hooks)->print_info((fmt), ##args); \
71 #define PrintInfo(fmt, args...)
76 #define PrintTrace(fmt, args...) \
78 extern struct v3_os_hooks * os_hooks; \
79 if ((os_hooks) && (os_hooks)->print_trace) { \
80 (os_hooks)->print_trace(fmt, ##args); \
84 #define PrintTrace(fmt, args...)
88 #define V3_AllocPages(num_pages) \
90 extern struct v3_os_hooks * os_hooks; \
92 if ((os_hooks) && (os_hooks)->allocate_pages) { \
93 ptr = (os_hooks)->allocate_pages(num_pages); \
99 #define V3_FreePage(page) \
101 extern struct v3_os_hooks * os_hooks; \
102 if ((os_hooks) && (os_hooks)->free_page) { \
103 (os_hooks)->free_page(page); \
108 #define V3_VAddr(addr) ({ \
109 extern struct v3_os_hooks * os_hooks; \
111 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { \
112 var = (os_hooks)->paddr_to_vaddr(addr); \
118 #define V3_PAddr(addr) ({ \
119 extern struct v3_os_hooks * os_hooks; \
121 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { \
122 var = (os_hooks)->vaddr_to_paddr(addr); \
129 #define V3_Malloc(size) ({ \
130 extern struct v3_os_hooks * os_hooks; \
132 if ((os_hooks) && (os_hooks)->malloc) { \
133 var = (os_hooks)->malloc(size); \
138 // We need to check the hook structure at runtime to ensure its SAFE
139 #define V3_Free(addr) \
141 extern struct v3_os_hooks * os_hooks; \
142 if ((os_hooks) && (os_hooks)->free) { \
143 (os_hooks)->free(addr); \
148 // uint_t V3_CPU_KHZ();
149 #define V3_CPU_KHZ() \
151 unsigned int khz = 0; \
152 extern struct v3_os_hooks * os_hooks; \
153 if ((os_hooks) && (os_hooks)->get_cpu_khz) { \
154 khz = (os_hooks)->get_cpu_khz(); \
161 #define V3_Hook_Interrupt(irq, opaque) \
164 extern struct v3_os_hooks * os_hooks; \
165 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
166 ret = (os_hooks)->hook_interrupt(irq, opaque); \
171 #define V3_Yield(addr) \
173 extern struct v3_os_hooks * os_hooks; \
174 if ((os_hooks) && (os_hooks)->yield_cpu) { \
175 (os_hooks)->yield_cpu(); \
185 #define V3_ASSERT(x) \
188 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
189 __func__, #x, __FILE__, __LINE__, \
190 (ulong_t) __builtin_return_address(0)); \
198 #define VMM_INVALID_CPU 0
199 #define VMM_VMX_CPU 1
200 #define VMM_SVM_CPU 2
203 // Maybe make this a define....
204 typedef enum v3_cpu_arch {V3_INVALID_CPU, V3_SVM_CPU, V3_SVM_REV3_CPU, V3_VMX_CPU} v3_cpu_arch_t;
213 /* This will contain function pointers that provide OS services */
215 void (*print_info)(const char * format, ...)
216 __attribute__ ((format (printf, 1, 2)));
217 void (*print_debug)(const char * format, ...)
218 __attribute__ ((format (printf, 1, 2)));
219 void (*print_trace)(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 *s, int irq);
233 int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
235 int (*ack_irq)(int irq);
238 unsigned int (*get_cpu_khz)(void);
241 void (*start_kernel_thread)(void); // include pointer to function
243 void (*yield_cpu)(void);
248 struct v3_vm_config {
255 unsigned long mem_size; // in bytes, var should be natural size of cpu
256 // so we can specify maximum physical address size
257 // (We're screwed if we want to do 32 bit host/64 bit guest)
266 /* This will contain Function pointers that control the VMs */
268 struct guest_info *(*allocate_guest)(void);
270 int (*config_guest)(struct guest_info * info, struct v3_vm_config * config_ptr);
271 int (*init_guest)(struct guest_info * info);
272 int (*start_guest)(struct guest_info * info);
273 // int (*stop_vm)(uint_t vm_id);
275 int (*has_nested_paging)(void);
277 // v3_cpu_arch_t (*get_cpu_arch)();
285 // This is the interrupt state that the VMM's interrupt handlers need to see
287 struct v3_interrupt {
291 unsigned int should_ack; // Should the vmm ack this interrupt, or will
292 // the host OS do it?
298 void Init_V3(struct v3_os_hooks * hooks, struct v3_ctrl_ops * vmm_ops);
300 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);
301 int v3_deliver_keyboard_evt(struct guest_info * vm);