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.


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