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;
35 typedef void *vnet_intr_flags_t;
38 struct vnet_host_hooks {
39 void *(*thread_start)(int (*fn)(void * arg),
42 v3_resource_control_t *resource_control);
44 void (*thread_sleep)(long timeout);
45 void (*thread_wakeup)(void * thread);
46 void (*thread_stop)(void * thread);
47 int (*thread_should_stop)(void);
49 void *(*timer_create)(unsigned long interval,
50 void (* timer_fun)(void * priv_data),
53 void (*timer_del)(void * timer);
54 void (*timer_start)(void * timer);
55 void (*timer_stop)(void * timer);
56 void (*timer_reset)(void * timer, unsigned long interval);
58 void (*udelay)(unsigned long usecs);
60 /* duplicate part from os_hooks */
61 void (*yield_cpu)(void);
62 void (*print)(void *vm , int core, const char * format, ...)
63 __attribute__ ((format (printf, 3, 4)));
65 void *(*allocate_pages)(int num_pages, unsigned int alignment, int node_id);
66 void (*free_pages)(void * page, int num_pages);
68 void *(*malloc)(unsigned int size);
69 void (*free)(void * addr);
71 void *(*paddr_to_vaddr)(void * addr);
72 void *(*vaddr_to_paddr)(void * addr);
74 void *(*mutex_alloc)(void);
75 void (*mutex_free)(void * mutex);
76 void (*mutex_lock)(void * mutex, int must_spin);
77 void (*mutex_unlock)(void * mutex);
78 vnet_intr_flags_t (*mutex_lock_irqsave)(void * mutex, int must_spin);
79 void (*mutex_unlock_irqrestore)(void * mutex, vnet_intr_flags_t flags);
86 extern struct vnet_host_hooks * host_hooks;
89 /* MEMORY ALLOCATE/DEALLOCATE */
91 #define PAGE_SIZE_4KB 4096
94 static inline void * Vnet_AllocPages(int num_pages){
95 if ((host_hooks) && host_hooks->allocate_pages) {
96 return host_hooks->allocate_pages(num_pages, PAGE_SIZE_4KB,-1);
102 static inline void Vnet_FreePages(void * page, int num_pages){
103 if ((host_hooks) && host_hooks->free_pages) {
104 host_hooks->free_pages(page, num_pages);
108 static inline void * Vnet_VAddr(void * addr) {
109 if ((host_hooks) && host_hooks->paddr_to_vaddr){
110 return host_hooks->paddr_to_vaddr(addr);
116 static inline void * Vnet_PAddr(void * addr) {
117 if ((host_hooks) && host_hooks->vaddr_to_paddr) {
118 return host_hooks->vaddr_to_paddr(addr);
124 static inline void * Vnet_Malloc(uint32_t size){
125 if ((host_hooks) && host_hooks->malloc) {
126 return host_hooks->malloc(size);
132 static inline void Vnet_Free(void * addr){
133 if ((host_hooks) && host_hooks->free) {
134 host_hooks->free(addr);
139 static inline void Vnet_Yield(void){
140 if ((host_hooks) && (host_hooks)->yield_cpu) {
141 host_hooks->yield_cpu();
145 /* THREAD FUNCTIONS */
146 struct vnet_thread * vnet_start_thread(int (*func)(void *), void *arg, char * name);
148 static inline void vnet_thread_sleep(long timeout){
149 if((host_hooks) && host_hooks->thread_sleep){
150 host_hooks->thread_sleep(timeout);
154 static inline void vnet_thread_wakeup(struct vnet_thread * thread){
155 if((host_hooks) && host_hooks->thread_wakeup){
156 host_hooks->thread_wakeup(thread->host_thread);
161 static inline void vnet_thread_stop(struct vnet_thread * thread){
162 if((host_hooks) && host_hooks->thread_stop){
163 host_hooks->thread_stop(thread->host_thread);
167 static inline int vnet_thread_should_stop(void){
168 if((host_hooks) && host_hooks->thread_should_stop){
169 return host_hooks->thread_should_stop();
175 static inline void vnet_udelay(unsigned long usecs){
176 if((host_hooks) && host_hooks->udelay){
177 host_hooks->udelay(usecs);
181 /* TIMER FUNCTIONS */
182 /* interval, in jittes */
183 struct vnet_timer * vnet_create_timer(unsigned long interval,
184 void (* timer_fun)(void * priv_data),
187 static inline void vnet_del_timer(struct vnet_timer * timer){
188 if((host_hooks) && host_hooks->timer_del){
189 host_hooks->timer_del(timer->host_timer);
194 static inline void vnet_start_timer(struct vnet_timer * timer){
195 if((host_hooks) && host_hooks->timer_start){
196 host_hooks->timer_start(timer->host_timer);
200 static inline void vnet_stop_timer(struct vnet_timer * timer){
201 if((host_hooks) && host_hooks->timer_stop){
202 host_hooks->timer_stop(timer->host_timer);
206 static inline void vnet_reset_timer(struct vnet_timer * timer,
207 unsigned long new_interval){
208 if((host_hooks) && host_hooks->timer_reset){
209 host_hooks->timer_reset(timer->host_timer, new_interval);
215 #define Vnet_Print(level, fmt, args...) \
217 extern int net_debug; \
218 if(level <= net_debug) { \
219 extern struct vnet_host_hooks * host_hooks; \
220 if ((host_hooks) && (host_hooks)->print) { \
221 (host_hooks)->print(0, -1, (fmt), ##args); \
227 #define Vnet_Debug(fmt, args...) \
229 extern struct vnet_host_hooks * host_hooks; \
230 if ((host_hooks) && (host_hooks)->print) { \
231 (host_hooks)->print(0, -1, (fmt), ##args); \
239 static inline int vnet_lock_init(vnet_lock_t * lock) {
240 if((host_hooks) && host_hooks->mutex_alloc){
241 *lock = (addr_t)(host_hooks->mutex_alloc());
249 static inline void vnet_lock_deinit(vnet_lock_t * lock) {
250 if (host_hooks && (host_hooks->mutex_free)) {
251 host_hooks->mutex_free((void *)*lock);
256 static inline void vnet_lock(vnet_lock_t lock) {
257 if (host_hooks && (host_hooks->mutex_lock)) {
258 host_hooks->mutex_lock((void *)lock,0);
262 static inline void vnet_unlock(vnet_lock_t lock) {
263 if (host_hooks && (host_hooks->mutex_lock)) {
264 host_hooks->mutex_unlock((void *)lock);
268 static inline vnet_intr_flags_t vnet_lock_irqsave(vnet_lock_t lock)
270 if (host_hooks && host_hooks->mutex_lock_irqsave) {
271 return (host_hooks->mutex_lock_irqsave((void *)lock, 1));
278 static inline void vnet_unlock_irqrestore(vnet_lock_t lock, vnet_intr_flags_t irq_state)
280 if (host_hooks && (host_hooks->mutex_unlock_irqrestore)) {
281 host_hooks->mutex_unlock_irqrestore((void *)lock,irq_state);
287 void init_vnet(struct vnet_host_hooks * hooks);
288 void deinit_vnet(void);