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.


548a2520dd6ff352aa049313c6e758bb32e6f72c
[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_base.h>
24 #include <vnet/vnet_vmm.h>
25
26 struct vnet_thread {
27     void * host_thread;
28 };
29
30 struct vnet_timer {
31     void * host_timer;
32 };
33
34 typedef unsigned long vnet_lock_t;
35 typedef void *vnet_intr_flags_t;
36
37
38 struct vnet_host_hooks {
39     void *(*thread_start)(int (*fn)(void * arg), 
40                           void * arg, 
41                           char * thread_name);
42
43     void (*thread_sleep)(long timeout);
44     void (*thread_wakeup)(void * thread);
45     void (*thread_stop)(void * thread);
46     int (*thread_should_stop)(void);
47
48     void *(*timer_create)(unsigned long interval, 
49                           void (* timer_fun)(void * priv_data), 
50                           void * data);
51
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);
56
57     void (*udelay)(unsigned long usecs);
58
59     /* duplicate part from os_hooks */
60     void (*yield_cpu)(void); 
61     void (*print)(void *vm , int core, const char * format, ...)
62         __attribute__ ((format (printf, 3, 4)));
63   
64     void *(*allocate_pages)(int num_pages, unsigned int alignment, int node_id, int constraints);
65     void (*free_pages)(void * page, int num_pages);
66
67     void *(*malloc)(unsigned int size);
68     void (*free)(void * addr);
69
70     void *(*paddr_to_vaddr)(void * addr);
71     void *(*vaddr_to_paddr)(void * addr);
72
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);
77     vnet_intr_flags_t (*mutex_lock_irqsave)(void * mutex, int must_spin);
78     void (*mutex_unlock_irqrestore)(void * mutex, vnet_intr_flags_t flags);
79 };
80
81
82
83 #ifdef __V3VEE__
84
85 extern struct vnet_host_hooks * host_hooks;
86
87
88 /* MEMORY ALLOCATE/DEALLOCATE */
89
90 #define PAGE_SIZE_4KB 4096
91                 
92 /* 4KB-aligned */
93 static inline void * Vnet_AllocPages(int num_pages){
94     if ((host_hooks) && host_hooks->allocate_pages) {
95         return host_hooks->allocate_pages(num_pages, PAGE_SIZE_4KB,-1,0); // any zone, no constraints
96     }
97
98     return NULL;
99 }
100
101 static inline void Vnet_FreePages(void * page, int num_pages){
102     if ((host_hooks) && host_hooks->free_pages) {       
103         host_hooks->free_pages(page, num_pages);
104     }
105
106
107 static inline void * Vnet_VAddr(void * addr) {
108     if ((host_hooks) && host_hooks->paddr_to_vaddr){
109         return host_hooks->paddr_to_vaddr(addr);
110     }
111
112     return NULL;
113 }
114
115 static inline void * Vnet_PAddr(void * addr) {
116     if ((host_hooks) && host_hooks->vaddr_to_paddr) {
117         return host_hooks->vaddr_to_paddr(addr);
118     }
119
120     return NULL;
121 }
122
123 static inline void * Vnet_Malloc(uint32_t size){
124     if ((host_hooks) && host_hooks->malloc) {
125         return host_hooks->malloc(size);
126     }
127
128     return NULL;
129 }
130
131 static inline void Vnet_Free(void * addr){  
132     if ((host_hooks) && host_hooks->free) {
133         host_hooks->free(addr);
134     }
135 }
136
137
138 static inline void Vnet_Yield(void){
139     if ((host_hooks) && (host_hooks)->yield_cpu) {
140         host_hooks->yield_cpu();
141     }
142 }
143
144 /* THREAD FUNCTIONS */
145 struct vnet_thread * vnet_start_thread(int (*func)(void *), void *arg, char * name);
146
147 static inline void vnet_thread_sleep(long timeout){
148     if((host_hooks) && host_hooks->thread_sleep){
149         host_hooks->thread_sleep(timeout);
150     }
151 }
152
153 static inline void vnet_thread_wakeup(struct vnet_thread * thread){
154     if((host_hooks) && host_hooks->thread_wakeup){
155         host_hooks->thread_wakeup(thread->host_thread);
156     }
157 }
158
159
160 static inline void vnet_thread_stop(struct vnet_thread * thread){
161     if((host_hooks) && host_hooks->thread_stop){
162         host_hooks->thread_stop(thread->host_thread);
163     }
164 }
165
166 static inline int vnet_thread_should_stop(void){
167     if((host_hooks) && host_hooks->thread_should_stop){
168         return host_hooks->thread_should_stop();
169     }
170
171     return 0;
172 }
173
174 static inline void  vnet_udelay(unsigned long usecs){
175     if((host_hooks) && host_hooks->udelay){
176         host_hooks->udelay(usecs);
177     }
178 }
179
180 /* TIMER FUNCTIONS */
181 /* interval, in jittes */
182 struct vnet_timer * vnet_create_timer(unsigned long interval, 
183                                       void (* timer_fun)(void * priv_data), 
184                                       void * pri_data);
185
186 static inline void vnet_del_timer(struct vnet_timer * timer){
187     if((host_hooks) && host_hooks->timer_del){
188         host_hooks->timer_del(timer->host_timer);
189         Vnet_Free(timer);
190     }
191 }
192         
193 static inline void vnet_start_timer(struct vnet_timer * timer){
194     if((host_hooks) && host_hooks->timer_start){
195         host_hooks->timer_start(timer->host_timer);
196     }
197 }
198
199 static inline void vnet_stop_timer(struct vnet_timer * timer){
200     if((host_hooks) && host_hooks->timer_stop){
201         host_hooks->timer_stop(timer->host_timer);
202     }
203 }
204
205 static inline void vnet_reset_timer(struct vnet_timer * timer, 
206                                     unsigned long new_interval){
207     if((host_hooks) && host_hooks->timer_reset){
208         host_hooks->timer_reset(timer->host_timer, new_interval);
209     }
210 }
211
212
213
214 #define Vnet_Print(level, fmt, args...)                                 \
215     do {                                                                \
216         extern int net_debug;                                           \
217         if(level <= net_debug) {                                        \
218             extern struct vnet_host_hooks * host_hooks;                 \
219             if ((host_hooks) && (host_hooks)->print) {                  \
220               (host_hooks)->print(0, -1, (fmt), ##args);                        \
221             }                                                           \
222         }                                                               \
223     } while (0) 
224
225
226 #define Vnet_Debug(fmt, args...)                                \
227     do {                                                                \
228             extern struct vnet_host_hooks * host_hooks;                 \
229             if ((host_hooks) && (host_hooks)->print) {                  \
230               (host_hooks)->print(0, -1, (fmt), ##args);                        \
231             }                                                           \
232     } while (0) 
233
234
235
236
237 /* Lock Utilities */
238 static inline int vnet_lock_init(vnet_lock_t * lock) {
239     if((host_hooks) && host_hooks->mutex_alloc){
240         *lock = (addr_t)(host_hooks->mutex_alloc());
241         if (*lock) {
242             return 0;
243         }
244     }
245     return -1;
246 }
247
248 static inline void vnet_lock_deinit(vnet_lock_t * lock) {
249     if (host_hooks && (host_hooks->mutex_free)) { 
250         host_hooks->mutex_free((void *)*lock);
251         *lock = 0;
252     }
253 }
254
255 static inline void vnet_lock(vnet_lock_t lock) {
256     if (host_hooks && (host_hooks->mutex_lock)) { 
257         host_hooks->mutex_lock((void *)lock,0);    
258     }
259 }
260
261 static inline void vnet_unlock(vnet_lock_t lock) {
262     if (host_hooks && (host_hooks->mutex_lock)) { 
263         host_hooks->mutex_unlock((void *)lock);
264     }
265 }
266
267 static inline vnet_intr_flags_t vnet_lock_irqsave(vnet_lock_t lock) 
268 {
269     if (host_hooks && host_hooks->mutex_lock_irqsave) { 
270         return (host_hooks->mutex_lock_irqsave((void *)lock, 1));
271     } else {
272         return NULL;
273     }
274 }
275
276
277 static inline void vnet_unlock_irqrestore(vnet_lock_t lock, vnet_intr_flags_t irq_state) 
278 {
279     if (host_hooks && (host_hooks->mutex_unlock_irqrestore)) {
280         host_hooks->mutex_unlock_irqrestore((void *)lock,irq_state);
281     }
282 }
283 #endif
284
285
286 void init_vnet(struct vnet_host_hooks * hooks);
287 void deinit_vnet(void);
288
289
290 #endif
291
292