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"
19 static void host_print(const char * fmt, ...) {
30 static void * host_allocate_pages(int num_pages, unsigned int alignment){
32 struct page * pgs = NULL;
33 int order = get_order(num_pages * PAGE_SIZE);
35 pgs = alloc_pages(GFP_KERNEL, order);
37 WARN(!pgs, "Could not allocate pages\n");
39 addr = page_to_pfn(pgs) << PAGE_SHIFT;
45 static void host_free_pages(void * page_paddr, int num_pages) {
46 uintptr_t pg_addr = (uintptr_t)page_paddr;
48 __free_pages(pfn_to_page(pg_addr >> PAGE_SHIFT), get_order(num_pages * PAGE_SIZE));
53 host_alloc(unsigned int size) {
55 addr = kmalloc(size, GFP_KERNEL);
70 host_vaddr_to_paddr(void * vaddr)
72 return (void*) __pa(vaddr);
77 host_paddr_to_vaddr(void * paddr)
84 host_start_kernel_thread(
85 int (*fn)(void * arg),
88 struct task_struct * thread = NULL;
90 thread = kthread_run(fn, arg, thread_name );
95 static void host_kthread_sleep(long timeout){
96 set_current_state(TASK_INTERRUPTIBLE);
101 schedule_timeout(timeout);
107 static void host_kthread_wakeup(void * thread){
108 struct task_struct * kthread = (struct task_struct *)thread;
110 wake_up_process(kthread);
113 static void host_kthread_stop(void * thread){
114 struct task_struct * kthread = (struct task_struct *)thread;
116 kthread_stop(kthread);
119 static int host_kthread_should_stop(void){
120 return kthread_should_stop();
124 static void host_udelay(unsigned long usecs){
138 host_mutex_alloc(void)
140 spinlock_t * lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
143 spin_lock_init(lock);
158 host_mutex_lock(void * mutex,
161 spin_lock((spinlock_t *)mutex);
165 host_mutex_unlock(void * mutex)
167 spin_unlock((spinlock_t *)mutex);
174 struct timer_list timer;
175 unsigned long interval;
178 void (* timer_fun)(void * private_data);
183 void timeout_fn(unsigned long arg){
184 struct host_timer * timer = (struct host_timer *)arg;
187 timer->timer_fun(timer->pri_data);
189 mod_timer(&(timer->timer), timer->interval);
194 host_create_timer(unsigned long interval,
195 void (* timer_fun)(void * priv_data),
197 struct host_timer * timer = (struct host_timer *)kmalloc(sizeof(struct host_timer), GFP_KERNEL);
199 timer->interval = interval;
200 timer->timer_fun = timer_fun;
201 timer->pri_data = data;
203 init_timer(&(timer->timer));
205 timer->timer.data = (unsigned long)timer;
206 timer->timer.function = timeout_fn;
207 timer->timer.expires = interval;
213 host_start_timer(void * vnet_timer){
214 struct host_timer * timer = (struct host_timer *)vnet_timer;
217 add_timer(&(timer->timer));
221 host_reset_timer(void * vnet_timer, unsigned long interval){
222 struct host_timer * timer = (struct host_timer *)timer;
224 timer->interval = interval;
228 host_stop_timer(void * vnet_timer){
229 struct host_timer * timer = (struct host_timer *)vnet_timer;
232 del_timer(&(timer->timer));
236 host_del_timer(void * vnet_timer){
237 struct host_timer * timer = (struct host_timer *)vnet_timer;
239 del_timer(&(timer->timer));
248 static struct vnet_host_hooks vnet_host_hooks = {
249 .timer_create = host_create_timer,
250 .timer_del = host_del_timer,
251 .timer_start = host_start_timer,
252 .timer_stop = host_stop_timer,
253 .timer_reset = host_reset_timer,
255 .thread_start = host_start_kernel_thread,
256 .thread_sleep = host_kthread_sleep,
257 .thread_wakeup = host_kthread_wakeup,
258 .thread_stop = host_kthread_stop,
259 .thread_should_stop = host_kthread_should_stop,
260 .udelay = host_udelay,
262 .yield_cpu = host_yield_cpu,
263 .mutex_alloc = host_mutex_alloc,
264 .mutex_free = host_mutex_free,
265 .mutex_lock = host_mutex_lock,
266 .mutex_unlock = host_mutex_unlock,
269 .allocate_pages = host_allocate_pages,
270 .free_pages = host_free_pages,
271 .malloc = host_alloc,
273 .vaddr_to_paddr = host_vaddr_to_paddr,
274 .paddr_to_vaddr = host_paddr_to_vaddr,
279 int vnet_init( void ) {
280 printk("Host init VNET\n");
282 init_vnet(&vnet_host_hooks);