2 Palacios VNET Host Hooks Implementations
5 #include <linux/kernel.h>
6 #include <linux/kthread.h>
7 #include <linux/spinlock.h>
9 #include <linux/sched.h>
10 #include <linux/uaccess.h>
11 #include <linux/init.h>
12 #include <linux/smp_lock.h>
13 #include <asm/delay.h>
14 #include <linux/timer.h>
16 #include <vnet/vnet.h>
17 #include "palacios-mm.h"
18 #include "palacios-vnet.h"
20 static void host_print(const char * fmt, ...) {
31 static void * host_allocate_pages(int num_pages, unsigned int alignment){
33 struct page * pgs = NULL;
34 int order = get_order(num_pages * PAGE_SIZE);
36 pgs = alloc_pages(GFP_KERNEL, order);
38 WARN(!pgs, "Could not allocate pages\n");
40 addr = page_to_pfn(pgs) << PAGE_SHIFT;
46 static void host_free_pages(void * page_paddr, int num_pages) {
47 uintptr_t pg_addr = (uintptr_t)page_paddr;
49 __free_pages(pfn_to_page(pg_addr >> PAGE_SHIFT), get_order(num_pages * PAGE_SIZE));
54 host_alloc(unsigned int size) {
56 addr = kmalloc(size, GFP_KERNEL);
71 host_vaddr_to_paddr(void * vaddr)
73 return (void*) __pa(vaddr);
78 host_paddr_to_vaddr(void * paddr)
85 host_start_kernel_thread(
86 int (*fn)(void * arg),
89 struct task_struct * thread = NULL;
91 thread = kthread_run(fn, arg, thread_name );
96 static void host_kthread_sleep(long timeout){
97 set_current_state(TASK_INTERRUPTIBLE);
102 schedule_timeout(timeout);
108 static void host_kthread_wakeup(void * thread){
109 struct task_struct * kthread = (struct task_struct *)thread;
111 wake_up_process(kthread);
114 static void host_kthread_stop(void * thread){
115 struct task_struct * kthread = (struct task_struct *)thread;
117 kthread_stop(kthread);
120 static int host_kthread_should_stop(void){
121 return kthread_should_stop();
125 static void host_udelay(unsigned long usecs){
139 host_mutex_alloc(void)
141 spinlock_t * lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
144 spin_lock_init(lock);
159 host_mutex_lock(void * mutex,
162 spin_lock((spinlock_t *)mutex);
166 host_mutex_unlock(void * mutex)
168 spin_unlock((spinlock_t *)mutex);
175 struct timer_list timer;
176 unsigned long interval;
179 void (* timer_fun)(void * private_data);
184 void timeout_fn(unsigned long arg){
185 struct host_timer * timer = (struct host_timer *)arg;
188 timer->timer_fun(timer->pri_data);
190 mod_timer(&(timer->timer), timer->interval);
195 host_create_timer(unsigned long interval,
196 void (* timer_fun)(void * priv_data),
198 struct host_timer * timer = (struct host_timer *)kmalloc(sizeof(struct host_timer), GFP_KERNEL);
200 timer->interval = interval;
201 timer->timer_fun = timer_fun;
202 timer->pri_data = data;
204 init_timer(&(timer->timer));
206 timer->timer.data = (unsigned long)timer;
207 timer->timer.function = timeout_fn;
208 timer->timer.expires = interval;
214 host_start_timer(void * vnet_timer){
215 struct host_timer * timer = (struct host_timer *)vnet_timer;
218 add_timer(&(timer->timer));
222 host_reset_timer(void * vnet_timer, unsigned long interval){
223 struct host_timer * timer = (struct host_timer *)timer;
225 timer->interval = interval;
229 host_stop_timer(void * vnet_timer){
230 struct host_timer * timer = (struct host_timer *)vnet_timer;
233 del_timer(&(timer->timer));
237 host_del_timer(void * vnet_timer){
238 struct host_timer * timer = (struct host_timer *)vnet_timer;
240 del_timer(&(timer->timer));
249 static struct vnet_host_hooks vnet_host_hooks = {
250 .timer_create = host_create_timer,
251 .timer_del = host_del_timer,
252 .timer_start = host_start_timer,
253 .timer_stop = host_stop_timer,
254 .timer_reset = host_reset_timer,
256 .thread_start = host_start_kernel_thread,
257 .thread_sleep = host_kthread_sleep,
258 .thread_wakeup = host_kthread_wakeup,
259 .thread_stop = host_kthread_stop,
260 .thread_should_stop = host_kthread_should_stop,
261 .udelay = host_udelay,
263 .yield_cpu = host_yield_cpu,
264 .mutex_alloc = host_mutex_alloc,
265 .mutex_free = host_mutex_free,
266 .mutex_lock = host_mutex_lock,
267 .mutex_unlock = host_mutex_unlock,
270 .allocate_pages = host_allocate_pages,
271 .free_pages = host_free_pages,
272 .malloc = host_alloc,
274 .vaddr_to_paddr = host_vaddr_to_paddr,
275 .paddr_to_vaddr = host_paddr_to_vaddr,
280 int palacios_vnet_init( void ) {
281 init_vnet(&vnet_host_hooks);
286 printk("V3 VNET Inited\n");
292 void palacios_vnet_deinit( void ) {
295 vnet_bridge_deinit();
298 printk("V3 VNET Deinited\n");