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.


Resource control extensions to host OS interface
[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                           v3_resource_control_t *resource_control);
43
44     void (*thread_sleep)(long timeout);
45     void (*thread_wakeup)(void * thread);
46     void (*thread_stop)(void * thread);
47     int (*thread_should_stop)(void);
48
49     void *(*timer_create)(unsigned long interval, 
50                           void (* timer_fun)(void * priv_data), 
51                           void * data);
52
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);
57
58     void (*udelay)(unsigned long usecs);
59
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)));
64   
65     void *(*allocate_pages)(int num_pages, unsigned int alignment, int node_id);
66     void (*free_pages)(void * page, int num_pages);
67
68     void *(*malloc)(unsigned int size);
69     void (*free)(void * addr);
70
71     void *(*paddr_to_vaddr)(void * addr);
72     void *(*vaddr_to_paddr)(void * addr);
73
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);
80 };
81
82
83
84 #ifdef __V3VEE__
85
86 extern struct vnet_host_hooks * host_hooks;
87
88
89 /* MEMORY ALLOCATE/DEALLOCATE */
90
91 #define PAGE_SIZE_4KB 4096
92                 
93 /* 4KB-aligned */
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); 
97     }
98
99     return NULL;
100 }
101
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);
105     }
106
107
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);
111     }
112
113     return NULL;
114 }
115
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);
119     }
120
121     return NULL;
122 }
123
124 static inline void * Vnet_Malloc(uint32_t size){
125     if ((host_hooks) && host_hooks->malloc) {
126         return host_hooks->malloc(size);
127     }
128
129     return NULL;
130 }
131
132 static inline void Vnet_Free(void * addr){  
133     if ((host_hooks) && host_hooks->free) {
134         host_hooks->free(addr);
135     }
136 }
137
138
139 static inline void Vnet_Yield(void){
140     if ((host_hooks) && (host_hooks)->yield_cpu) {
141         host_hooks->yield_cpu();
142     }
143 }
144
145 /* THREAD FUNCTIONS */
146 struct vnet_thread * vnet_start_thread(int (*func)(void *), void *arg, char * name);
147
148 static inline void vnet_thread_sleep(long timeout){
149     if((host_hooks) && host_hooks->thread_sleep){
150         host_hooks->thread_sleep(timeout);
151     }
152 }
153
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);
157     }
158 }
159
160
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);
164     }
165 }
166
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();
170     }
171
172     return 0;
173 }
174
175 static inline void  vnet_udelay(unsigned long usecs){
176     if((host_hooks) && host_hooks->udelay){
177         host_hooks->udelay(usecs);
178     }
179 }
180
181 /* TIMER FUNCTIONS */
182 /* interval, in jittes */
183 struct vnet_timer * vnet_create_timer(unsigned long interval, 
184                                       void (* timer_fun)(void * priv_data), 
185                                       void * pri_data);
186
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);
190         Vnet_Free(timer);
191     }
192 }
193         
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);
197     }
198 }
199
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);
203     }
204 }
205
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);
210     }
211 }
212
213
214
215 #define Vnet_Print(level, fmt, args...)                                 \
216     do {                                                                \
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);                        \
222             }                                                           \
223         }                                                               \
224     } while (0) 
225
226
227 #define Vnet_Debug(fmt, args...)                                \
228     do {                                                                \
229             extern struct vnet_host_hooks * host_hooks;                 \
230             if ((host_hooks) && (host_hooks)->print) {                  \
231               (host_hooks)->print(0, -1, (fmt), ##args);                        \
232             }                                                           \
233     } while (0) 
234
235
236
237
238 /* Lock Utilities */
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());
242         if (*lock) {
243             return 0;
244         }
245     }
246     return -1;
247 }
248
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);
252         *lock = 0;
253     }
254 }
255
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);    
259     }
260 }
261
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);
265     }
266 }
267
268 static inline vnet_intr_flags_t vnet_lock_irqsave(vnet_lock_t lock) 
269 {
270     if (host_hooks && host_hooks->mutex_lock_irqsave) { 
271         return (host_hooks->mutex_lock_irqsave((void *)lock, 1));
272     } else {
273         return NULL;
274     }
275 }
276
277
278 static inline void vnet_unlock_irqrestore(vnet_lock_t lock, vnet_intr_flags_t irq_state) 
279 {
280     if (host_hooks && (host_hooks->mutex_unlock_irqrestore)) {
281         host_hooks->mutex_unlock_irqrestore((void *)lock,irq_state);
282     }
283 }
284 #endif
285
286
287 void init_vnet(struct vnet_host_hooks * hooks);
288 void deinit_vnet(void);
289
290
291 #endif
292
293