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); \
49 #define PrintDebug(fmt,args ...)
54 #define PrintError(fmt, args...) \
56 extern struct v3_os_hooks * os_hooks; \
57 if ((os_hooks) && (os_hooks)->print_debug) { \
58 (os_hooks)->print_debug("%s(%d): " fmt, __FILE__, __LINE__, ##args); \
65 #define PrintInfo(fmt, args...) \
67 extern struct v3_os_hooks * os_hooks; \
68 if ((os_hooks) && (os_hooks)->print_info) { \
69 (os_hooks)->print_info((fmt), ##args); \
73 #define PrintInfo(fmt, args...)
78 #define PrintTrace(fmt, args...) \
80 extern struct v3_os_hooks * os_hooks; \
81 if ((os_hooks) && (os_hooks)->print_trace) { \
82 (os_hooks)->print_trace(fmt, ##args); \
86 #define PrintTrace(fmt, args...)
90 #define V3_AllocPages(num_pages) \
92 extern struct v3_os_hooks * os_hooks; \
94 if ((os_hooks) && (os_hooks)->allocate_pages) { \
95 ptr = (os_hooks)->allocate_pages(num_pages); \
101 #define V3_FreePage(page) \
103 extern struct v3_os_hooks * os_hooks; \
104 if ((os_hooks) && (os_hooks)->free_page) { \
105 (os_hooks)->free_page(page); \
110 #define V3_VAddr(addr) ({ \
111 extern struct v3_os_hooks * os_hooks; \
113 if ((os_hooks) && (os_hooks)->paddr_to_vaddr) { \
114 var = (os_hooks)->paddr_to_vaddr(addr); \
120 #define V3_PAddr(addr) ({ \
121 extern struct v3_os_hooks * os_hooks; \
123 if ((os_hooks) && (os_hooks)->vaddr_to_paddr) { \
124 var = (os_hooks)->vaddr_to_paddr(addr); \
131 #define V3_Malloc(size) ({ \
132 extern struct v3_os_hooks * os_hooks; \
134 if ((os_hooks) && (os_hooks)->malloc) { \
135 var = (os_hooks)->malloc(size); \
140 // We need to check the hook structure at runtime to ensure its SAFE
141 #define V3_Free(addr) \
143 extern struct v3_os_hooks * os_hooks; \
144 if ((os_hooks) && (os_hooks)->free) { \
145 (os_hooks)->free(addr); \
150 // uint_t V3_CPU_KHZ();
151 #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_Hook_Interrupt(irq, opaque) \
166 extern struct v3_os_hooks * os_hooks; \
167 if ((os_hooks) && (os_hooks)->hook_interrupt) { \
168 ret = (os_hooks)->hook_interrupt(irq, opaque); \
173 #define V3_Yield(addr) \
175 extern struct v3_os_hooks * os_hooks; \
176 if ((os_hooks) && (os_hooks)->yield_cpu) { \
177 (os_hooks)->yield_cpu(); \
187 #define V3_ASSERT(x) \
190 PrintDebug("Failed assertion in %s: %s at %s, line %d, RA=%lx\n", \
191 __func__, #x, __FILE__, __LINE__, \
192 (ulong_t) __builtin_return_address(0)); \
200 #define VMM_INVALID_CPU 0
201 #define VMM_VMX_CPU 1
202 #define VMM_SVM_CPU 2
205 // Maybe make this a define....
206 typedef enum v3_cpu_arch {V3_INVALID_CPU, V3_SVM_CPU, V3_SVM_REV3_CPU, V3_VMX_CPU} v3_cpu_arch_t;
215 /* This will contain function pointers that provide OS services */
217 void (*print_info)(const char * format, ...)
218 __attribute__ ((format (printf, 1, 2)));
219 void (*print_debug)(const char * format, ...)
220 __attribute__ ((format (printf, 1, 2)));
221 void (*print_trace)(const char * format, ...)
222 __attribute__ ((format (printf, 1, 2)));
224 void *(*allocate_pages)(int numPages);
225 void (*free_page)(void * page);
227 void *(*malloc)(unsigned int size);
228 void (*free)(void * addr);
230 void *(*paddr_to_vaddr)(void *addr);
231 void *(*vaddr_to_paddr)(void *addr);
233 // int (*hook_interrupt)(struct guest_info *s, int irq);
235 int (*hook_interrupt)(struct guest_info * vm, unsigned int irq);
237 int (*ack_irq)(int irq);
240 unsigned int (*get_cpu_khz)(void);
243 void (*start_kernel_thread)(void); // include pointer to function
245 void (*yield_cpu)(void);
250 struct v3_vm_config {
257 unsigned long mem_size; // in bytes, var should be natural size of cpu
258 // so we can specify maximum physical address size
259 // (We're screwed if we want to do 32 bit host/64 bit guest)
262 int enable_profiling;
271 /* This will contain Function pointers that control the VMs */
273 struct guest_info *(*allocate_guest)(void);
275 int (*config_guest)(struct guest_info * info, struct v3_vm_config * config_ptr);
276 int (*init_guest)(struct guest_info * info);
277 int (*start_guest)(struct guest_info * info);
278 // int (*stop_vm)(uint_t vm_id);
280 int (*has_nested_paging)(void);
282 // v3_cpu_arch_t (*get_cpu_arch)();
290 // This is the interrupt state that the VMM's interrupt handlers need to see
292 struct v3_interrupt {
296 unsigned int should_ack; // Should the vmm ack this interrupt, or will
297 // the host OS do it?
303 void Init_V3(struct v3_os_hooks * hooks, struct v3_ctrl_ops * vmm_ops);
305 int v3_deliver_irq(struct guest_info * vm, struct v3_interrupt * intr);
306 int v3_deliver_keyboard_evt(struct guest_info * vm);