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_base.h>
24 #include <vnet/vnet_vmm.h>
34 typedef unsigned long vnet_lock_t;
38 struct vnet_host_hooks {
39 void *(*thread_start)(int (*fn)(void * arg),
43 void (*thread_sleep)(long timeout);
44 void (*thread_wakeup)(void * thread);
45 void (*thread_stop)(void * thread);
46 int (*thread_should_stop)(void);
48 void *(*timer_create)(unsigned long interval,
49 void (* timer_fun)(void * priv_data),
52 void (*timer_del)(void * timer);
53 void (*timer_start)(void * timer);
54 void (*timer_stop)(void * timer);
55 void (*timer_reset)(void * timer, unsigned long interval);
57 void (*udelay)(unsigned long usecs);
59 /* duplicate part from os_hooks */
60 void (*yield_cpu)(void);
61 void (*print)(const char * format, ...)
62 __attribute__ ((format (printf, 1, 2)));
64 void *(*allocate_pages)(int num_pages, unsigned int alignment);
65 void (*free_pages)(void * page, int num_pages);
67 void *(*malloc)(unsigned int size);
68 void (*free)(void * addr);
70 void *(*paddr_to_vaddr)(void * addr);
71 void *(*vaddr_to_paddr)(void * addr);
73 void *(*mutex_alloc)(void);
74 void (*mutex_free)(void * mutex);
75 void (*mutex_lock)(void * mutex, int must_spin);
76 void (*mutex_unlock)(void * mutex);
83 extern struct vnet_host_hooks * host_hooks;
86 /* MEMORY ALLOCATE/DEALLOCATE */
88 #define PAGE_SIZE_4KB 4096
91 static inline void * Vnet_AllocPages(int num_pages){
92 if ((host_hooks) && host_hooks->allocate_pages) {
93 return host_hooks->allocate_pages(num_pages, PAGE_SIZE_4KB);
99 static inline void Vnet_FreePages(void * page, int num_pages){
100 if ((host_hooks) && host_hooks->free_pages) {
101 host_hooks->free_pages(page, num_pages);
105 static inline void * Vnet_VAddr(void * addr) {
106 if ((host_hooks) && host_hooks->paddr_to_vaddr){
107 return host_hooks->paddr_to_vaddr(addr);
113 static inline void * Vnet_PAddr(void * addr) {
114 if ((host_hooks) && host_hooks->vaddr_to_paddr) {
115 return host_hooks->vaddr_to_paddr(addr);
121 static inline void * Vnet_Malloc(uint32_t size){
122 if ((host_hooks) && host_hooks->malloc) {
123 return host_hooks->malloc(size);
129 static inline void Vnet_Free(void * addr){
130 if ((host_hooks) && host_hooks->free) {
131 host_hooks->free(addr);
136 static inline void Vnet_Yield(void){
137 if ((host_hooks) && (host_hooks)->yield_cpu) {
138 host_hooks->yield_cpu();
142 /* THREAD FUNCTIONS */
143 struct vnet_thread * vnet_start_thread(int (*func)(void *),
144 void * arg, char * name);
146 static inline void vnet_thread_sleep(long timeout){
147 if((host_hooks) && host_hooks->thread_sleep){
148 host_hooks->thread_sleep(timeout);
152 static inline void vnet_thread_wakeup(struct vnet_thread * thread){
153 if((host_hooks) && host_hooks->thread_wakeup){
154 host_hooks->thread_wakeup(thread->host_thread);
159 static inline void vnet_thread_stop(struct vnet_thread * thread){
160 if((host_hooks) && host_hooks->thread_stop){
161 host_hooks->thread_stop(thread->host_thread);
165 static inline int vnet_thread_should_stop(void){
166 if((host_hooks) && host_hooks->thread_should_stop){
167 return host_hooks->thread_should_stop();
173 static inline void vnet_udelay(unsigned long usecs){
174 if((host_hooks) && host_hooks->udelay){
175 host_hooks->udelay(usecs);
179 /* TIMER FUNCTIONS */
180 /* interval, in jittes */
181 struct vnet_timer * vnet_create_timer(unsigned long interval,
182 void (* timer_fun)(void * priv_data),
185 static inline void vnet_del_timer(struct vnet_timer * timer){
186 if((host_hooks) && host_hooks->timer_del){
187 host_hooks->timer_del(timer->host_timer);
192 static inline void vnet_start_timer(struct vnet_timer * timer){
193 if((host_hooks) && host_hooks->timer_start){
194 host_hooks->timer_start(timer->host_timer);
198 static inline void vnet_stop_timer(struct vnet_timer * timer){
199 if((host_hooks) && host_hooks->timer_stop){
200 host_hooks->timer_stop(timer->host_timer);
204 static inline void vnet_reset_timer(struct vnet_timer * timer,
205 unsigned long new_interval){
206 if((host_hooks) && host_hooks->timer_reset){
207 host_hooks->timer_reset(timer->host_timer, new_interval);
213 #define Vnet_Print(level, fmt, args...) \
215 extern int net_debug; \
216 if(level <= net_debug) { \
217 extern struct vnet_host_hooks * host_hooks; \
218 if ((host_hooks) && (host_hooks)->print) { \
219 (host_hooks)->print((fmt), ##args); \
225 #define Vnet_Debug(fmt, args...) \
227 extern struct vnet_host_hooks * host_hooks; \
228 if ((host_hooks) && (host_hooks)->print) { \
229 (host_hooks)->print((fmt), ##args); \
237 int vnet_lock_init(vnet_lock_t * lock);
239 static inline void vnet_lock_deinit(vnet_lock_t * lock) {
240 host_hooks->mutex_free((void *)*lock);
244 static inline void vnet_lock(vnet_lock_t lock) {
245 host_hooks->mutex_lock((void *)lock, 0);
248 static inline void vnet_unlock(vnet_lock_t lock) {
249 host_hooks->mutex_unlock((void *)lock);
252 static inline unsigned long vnet_lock_irqsave(vnet_lock_t lock) {
253 //addr_t irq_state = v3_irq_save();
254 host_hooks->mutex_lock((void *)lock, 1);
259 static inline void vnet_unlock_irqrestore(vnet_lock_t lock, addr_t irq_state) {
260 host_hooks->mutex_unlock((void *)lock);
261 //v3_irq_restore(irq_state);
267 void init_vnet(struct vnet_host_hooks * hooks);
268 void deinit_vnet(void);