Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


more fix on VNET
[palacios.git] / palacios / include / vnet / vnet_host.h
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2011, Lei Xia <lxia@northwestern.edu> 
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Lei Xia <lxia@northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #ifndef __VNET_HOST_H__
21 #define __VNET_HOST_H__
22
23 #include <vnet/vnet_vmm.h>
24
25 struct vnet_thread {
26     void * host_thread;
27 };
28
29 struct vnet_timer {
30     void * host_timer;
31 };
32
33 typedef unsigned long vnet_lock_t;
34
35
36
37 struct vnet_host_hooks {
38     void *(*thread_start)(int (*fn)(void * arg), void * arg, char * thread_name);
39     void (*thread_sleep)(long timeout);
40     void (*thread_wakeup)(void * thread);
41     void (*thread_stop)(void * thread);
42     int (*thread_should_stop)(void);
43
44     void *(*timer_create)(unsigned long interval, void (* timer_fun)(void * priv_data), void * data);
45     void (*timer_del)(void * timer);
46     void (*timer_start)(void * timer);
47     void (*timer_stop)(void * timer);
48     void (*timer_reset)(void * timer, unsigned long interval);
49
50     void (*udelay)(unsigned long usecs);
51
52     /* duplicate part from os_hooks */
53     void (*yield_cpu)(void); 
54     void (*print)(const char * format, ...)
55         __attribute__ ((format (printf, 1, 2)));
56   
57     void *(*allocate_pages)(int num_pages, unsigned int alignment);
58     void (*free_pages)(void * page, int num_pages);
59
60     void *(*malloc)(unsigned int size);
61     void (*free)(void * addr);
62
63     void *(*paddr_to_vaddr)(void * addr);
64     void *(*vaddr_to_paddr)(void * addr);
65
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);
70 };
71
72
73
74 #ifdef __V3VEE__
75
76 extern struct vnet_host_hooks * host_hooks;
77
78
79 /* MEMORY ALLOCATE/DEALLOCATE */
80
81 #define PAGE_SIZE_4KB 4096
82                 
83 /* 4KB-aligned */
84 static inline void * Vnet_AllocPages(int num_pages){
85     if ((host_hooks) && host_hooks->allocate_pages) {
86         return host_hooks->allocate_pages(num_pages, PAGE_SIZE_4KB);
87     }
88
89     return NULL;
90 }
91
92 static inline void Vnet_FreePages(void * page, int num_pages){
93     if ((host_hooks) && host_hooks->free_pages) {       
94         host_hooks->free_pages(page, num_pages);
95     }
96
97
98 static inline void * Vnet_VAddr(void * addr) {
99     if ((host_hooks) && host_hooks->paddr_to_vaddr){
100         return host_hooks->paddr_to_vaddr(addr);
101     }
102
103     return NULL;
104 }
105
106 static inline void * Vnet_PAddr(void * addr) {
107     if ((host_hooks) && host_hooks->vaddr_to_paddr) {
108         return host_hooks->vaddr_to_paddr(addr);
109     }
110
111     return NULL;
112 }
113
114 static inline void * Vnet_Malloc(uint32_t size){
115     if ((host_hooks) && host_hooks->malloc) {
116         return host_hooks->malloc(size);
117     }
118
119     return NULL;
120 }
121
122 static inline void Vnet_Free(void * addr){  
123     if ((host_hooks) && host_hooks->free) {
124         host_hooks->free(addr);
125     }
126 }
127
128
129 static inline void Vnet_Yield(void){
130     if ((host_hooks) && (host_hooks)->yield_cpu) {
131         host_hooks->yield_cpu();
132     }
133 }
134
135 /* THREAD FUNCTIONS */
136 struct vnet_thread * vnet_start_thread(int (*func)(void *), void * arg, char * name);
137
138 static inline void vnet_thread_sleep(long timeout){
139     if((host_hooks) && host_hooks->thread_sleep){
140         host_hooks->thread_sleep(timeout);
141     }
142 }
143
144 static inline void vnet_thread_wakeup(struct vnet_thread * thread){
145     if((host_hooks) && host_hooks->thread_wakeup){
146         host_hooks->thread_wakeup(thread->host_thread);
147     }
148 }
149
150
151 static inline void vnet_thread_stop(struct vnet_thread * thread){
152     if((host_hooks) && host_hooks->thread_stop){
153         host_hooks->thread_stop(thread->host_thread);
154     }
155 }
156
157 static inline int vnet_thread_should_stop(void){
158     if((host_hooks) && host_hooks->thread_should_stop){
159         return host_hooks->thread_should_stop();
160     }
161
162     return 0;
163 }
164
165 static inline void  vnet_udelay(unsigned long usecs){
166     if((host_hooks) && host_hooks->udelay){
167         host_hooks->udelay(usecs);
168     }
169 }
170
171 /* TIMER FUNCTIONS */
172 /* interval, in jittes */
173 struct vnet_timer * vnet_create_timer(unsigned long interval, void (* timer_fun)(void * priv_data), void * pri_data);
174
175 static inline void vnet_del_timer(struct vnet_timer * timer){
176     if((host_hooks) && host_hooks->timer_del){
177         host_hooks->timer_del(timer->host_timer);
178         Vnet_Free(timer);
179     }
180 }
181         
182 static inline void vnet_start_timer(struct vnet_timer * timer){
183     if((host_hooks) && host_hooks->timer_start){
184         host_hooks->timer_start(timer->host_timer);
185     }
186 }
187
188 static inline void vnet_stop_timer(struct vnet_timer * timer){
189     if((host_hooks) && host_hooks->timer_stop){
190         host_hooks->timer_stop(timer->host_timer);
191     }
192 }
193
194 static inline void vnet_reset_timer(struct vnet_timer * timer, unsigned long new_interval){
195     if((host_hooks) && host_hooks->timer_reset){
196         host_hooks->timer_reset(timer->host_timer, new_interval);
197     }
198 }
199
200
201
202 #define Vnet_Print(level, fmt, args...)                                 \
203     do {        \
204         extern int vnet_debug;  \
205         if(level <= vnet_debug) {   \
206             extern struct vnet_host_hooks * host_hooks;                 \
207             if ((host_hooks) && (host_hooks)->print) {                  \
208                 (host_hooks)->print((fmt), ##args);                     \
209             }                                                   \
210         }                                                       \
211     } while (0) 
212
213
214 #define Vnet_Debug(fmt, args...)                                        \
215     do {        \
216             extern struct vnet_host_hooks * host_hooks;                 \
217             if ((host_hooks) && (host_hooks)->print) {                  \
218                 (host_hooks)->print((fmt), ##args);                     \
219             }                                                                                           \
220     } while (0) 
221
222
223
224
225 /* Lock Utilities */
226 int vnet_lock_init(vnet_lock_t * lock);
227
228 static inline void vnet_lock_deinit(vnet_lock_t * lock) {
229     host_hooks->mutex_free((void *)*lock);
230     *lock = 0;
231 }
232
233 static inline void vnet_lock(vnet_lock_t lock) {
234     host_hooks->mutex_lock((void *)lock, 0);    
235 }
236
237 static inline void vnet_unlock(vnet_lock_t lock) {
238     host_hooks->mutex_unlock((void *)lock);
239 }
240
241 static inline unsigned long vnet_lock_irqsave(vnet_lock_t lock) {
242     //addr_t irq_state = v3_irq_save();
243     host_hooks->mutex_lock((void *)lock, 1);
244     return 0;
245 }
246
247
248 static inline void vnet_unlock_irqrestore(vnet_lock_t lock, addr_t irq_state) {
249     host_hooks->mutex_unlock((void *)lock);
250     //v3_irq_restore(irq_state);
251 }
252
253 #endif
254 \r
255
256 void init_vnet(struct vnet_host_hooks * hooks);
257
258
259 #endif
260
261