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) 2011, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Lei Xia <lxia@northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #ifndef __VNET_HOST_H__
21 #define __VNET_HOST_H__
23 #include <vnet/vnet_vmm.h>
33 typedef addr_t vnet_lock_t;
37 struct vnet_host_hooks {
38 void *(*thread_start)(int (*fn)(void * arg), void * arg, char * thread_name);
39 void (*thread_sleep)(int timeout);
40 void (*thread_wakeup)(void * thread);
41 void (*thread_stop)(void * thread);
42 int (*thread_should_stop)();
44 void *(*timer_create)(unsigned long interval, void (* timer_fun)(void * priv_data), void * data);
45 void (*timer_del)(void * timer);
46 int (*timer_start)(void * timer);
47 int (*timer_stop)(void * timer);
48 void (*timer_reset)(void * timer, unsigned long interval);
50 void (*udelay)(unsigned long usecs);
52 /* duplicate part from os_hooks */
53 void (*yield_cpu)(void);
54 void (*print)(const char * format, ...)
55 __attribute__ ((format (printf, 1, 2)));
57 void *(*allocate_pages)(int num_pages, unsigned int alignment);
58 void (*free_pages)(void * page, int num_pages);
60 void *(*malloc)(unsigned int size);
61 void (*free)(void * addr);
63 void *(*paddr_to_vaddr)(void * addr);
64 void *(*vaddr_to_paddr)(void * addr);
66 void *(*mutex_alloc)(void);
67 void (*mutex_free)(void * mutex);
68 void (*mutex_lock)(void * mutex, int must_spin);
69 void (*mutex_unlock)(void * mutex);
74 extern struct vnet_host_hooks * host_hooks;
77 /* MEMORY ALLOCATE/DEALLOCATE */
79 #define PAGE_SIZE_4KB 4096
82 static inline void * Vnet_AllocPages(int num_pages){
83 if ((host_hooks) && host_hooks->allocate_pages) {
84 return host_hooks->allocate_pages(num_pages, PAGE_SIZE_4KB);
90 static inline void Vnet_FreePages(void * page, int num_pages){
91 if ((host_hooks) && host_hooks->free_pages) {
92 host_hooks->free_pages(page, num_pages);
96 static inline void * Vnet_VAddr(void * addr) {
97 if ((host_hooks) && host_hooks->paddr_to_vaddr){
98 return host_hooks->paddr_to_vaddr(addr);
104 static inline void * Vnet_PAddr(void *addr) {
105 if ((host_hooks) && host_hooks->vaddr_to_paddr) {
106 return host_hooks->vaddr_to_paddr(addr);
112 static inline void * Vnet_Malloc(uint32_t size){
113 if ((host_hooks) && host_hooks->malloc) {
114 return host_hooks->malloc(size);
120 static inline void Vnet_Free(void * addr){
121 if ((host_hooks) && host_hooks->free) {
122 host_hooks->free(addr);
127 static inline void Vnet_Yield(){
128 if ((host_hooks) && (host_hooks)->yield_cpu) {
129 host_hooks->yield_cpu();
133 /* THREAD FUNCTIONS */
134 struct vnet_thread * vnet_start_thread(int (*func)(void *), void *arg, char * name);
136 static inline void vnet_thread_sleep(long timeout){
137 if((host_hooks) && host_hooks->thread_sleep){
138 host_hooks->thread_sleep(timeout);
142 static inline void vnet_thread_wakeup(struct vnet_thread * thread){
143 if((host_hooks) && host_hooks->thread_wakeup){
144 host_hooks->thread_wakeup(thread->host_thread);
149 static inline void vnet_thread_stop(struct vnet_thread * thread){
150 if((host_hooks) && host_hooks->thread_stop){
151 host_hooks->thread_stop(thread->host_thread);
155 static inline int vnet_thread_should_stop(){
156 if((host_hooks) && host_hooks->thread_should_stop){
157 return host_hooks->thread_should_stop();
163 static inline void vnet_udelay(unsigned long usecs){
164 if((host_hooks) && host_hooks->udelay){
165 host_hooks->udelay(usecs);
169 /* TIMER FUNCTIONS */
170 /* interval, in jittes */
171 struct vnet_timer * vnet_create_timer(unsigned long interval, void (* timer_fun)(void * priv_data), void * pri_data);
173 static inline void vnet_del_timer(struct vnet_timer * timer){
174 if((host_hooks) && host_hooks->timer_del){
175 host_hooks->timer_del(timer->host_timer);
180 static inline int vnet_start_timer(struct vnet_timer * timer){
181 if((host_hooks) && host_hooks->timer_start){
182 return host_hooks->timer_start(timer->host_timer);
188 static inline int vnet_stop_timer(struct vnet_timer * timer){
189 if((host_hooks) && host_hooks->timer_stop){
190 return host_hooks->timer_stop(timer->host_timer);
196 static inline void vnet_reset_timer(struct vnet_timer * timer, unsigned long new_interval){
197 if((host_hooks) && host_hooks->timer_reset){
198 host_hooks->timer_reset(timer->host_timer, new_interval);
204 #define Vnet_Print(level, fmt, args...) \
206 extern int vnet_debug; \
207 if(level <= vnet_debug) { \
208 extern struct vnet_host_hooks * host_hooks; \
209 if ((host_hooks) && (host_hooks)->print) { \
210 (host_hooks)->print((fmt), ##args); \
216 #define Vnet_Debug(fmt, args...) \
218 extern struct vnet_host_hooks * host_hooks; \
219 if ((host_hooks) && (host_hooks)->print) { \
220 (host_hooks)->print((fmt), ##args); \
228 int vnet_lock_init(vnet_lock_t * lock);
230 static inline void vnet_lock_deinit(vnet_lock_t * lock) {
231 host_hooks->mutex_free((void *)*lock);
235 static inline void vnet_lock(vnet_lock_t lock) {
236 host_hooks->mutex_lock((void *)lock, 0);
239 static inline void vnet_unlock(vnet_lock_t lock) {
240 host_hooks->mutex_unlock((void *)lock);
243 static inline addr_t vnet_lock_irqsave(vnet_lock_t lock) {
244 addr_t irq_state = v3_irq_save();
245 host_hooks->mutex_lock((void *)lock, 1);
250 static inline void vnet_unlock_irqrestore(vnet_lock_t lock, addr_t irq_state) {
251 host_hooks->mutex_unlock((void *)lock);
252 v3_irq_restore(irq_state);
259 void init_vnet(struct vnet_host_hooks * hooks);