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"
19 #include "linux-exts.h"
21 static void host_print(const char * fmt, ...) {
32 static void * host_allocate_pages(int num_pages, unsigned int alignment){
34 struct page * pgs = NULL;
35 int order = get_order(num_pages * PAGE_SIZE);
37 pgs = alloc_pages(GFP_KERNEL, order);
39 WARN(!pgs, "Could not allocate pages\n");
41 addr = page_to_pfn(pgs) << PAGE_SHIFT;
47 static void host_free_pages(void * page_paddr, int num_pages) {
48 uintptr_t pg_addr = (uintptr_t)page_paddr;
50 __free_pages(pfn_to_page(pg_addr >> PAGE_SHIFT), get_order(num_pages * PAGE_SIZE));
55 host_alloc(unsigned int size) {
57 addr = kmalloc(size, GFP_KERNEL);
72 host_vaddr_to_paddr(void * vaddr)
74 return (void*) __pa(vaddr);
79 host_paddr_to_vaddr(void * paddr)
86 host_start_kernel_thread(
87 int (*fn)(void * arg),
90 struct task_struct * thread = NULL;
92 thread = kthread_run(fn, arg, thread_name );
97 static void host_kthread_sleep(long timeout){
98 set_current_state(TASK_INTERRUPTIBLE);
103 schedule_timeout(timeout);
109 static void host_kthread_wakeup(void * thread){
110 struct task_struct * kthread = (struct task_struct *)thread;
112 wake_up_process(kthread);
115 static void host_kthread_stop(void * thread){
116 struct task_struct * kthread = (struct task_struct *)thread;
118 kthread_stop(kthread);
121 static int host_kthread_should_stop(void){
122 return kthread_should_stop();
126 static void host_udelay(unsigned long usecs){
140 host_mutex_alloc(void)
142 spinlock_t * lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
145 spin_lock_init(lock);
160 host_mutex_lock(void * mutex,
163 spin_lock((spinlock_t *)mutex);
167 host_mutex_unlock(void * mutex)
169 spin_unlock((spinlock_t *)mutex);
176 struct timer_list timer;
177 unsigned long interval;
180 void (* timer_fun)(void * private_data);
185 void timeout_fn(unsigned long arg){
186 struct host_timer * timer = (struct host_timer *)arg;
189 timer->timer_fun(timer->pri_data);
191 mod_timer(&(timer->timer), timer->interval);
196 host_create_timer(unsigned long interval,
197 void (* timer_fun)(void * priv_data),
199 struct host_timer * timer = (struct host_timer *)kmalloc(sizeof(struct host_timer), GFP_KERNEL);
201 timer->interval = interval;
202 timer->timer_fun = timer_fun;
203 timer->pri_data = data;
205 init_timer(&(timer->timer));
207 timer->timer.data = (unsigned long)timer;
208 timer->timer.function = timeout_fn;
209 timer->timer.expires = interval;
215 host_start_timer(void * vnet_timer){
216 struct host_timer * timer = (struct host_timer *)vnet_timer;
219 add_timer(&(timer->timer));
223 host_reset_timer(void * vnet_timer, unsigned long interval){
224 struct host_timer * timer = (struct host_timer *)timer;
226 timer->interval = interval;
230 host_stop_timer(void * vnet_timer){
231 struct host_timer * timer = (struct host_timer *)vnet_timer;
234 del_timer(&(timer->timer));
238 host_del_timer(void * vnet_timer){
239 struct host_timer * timer = (struct host_timer *)vnet_timer;
241 del_timer(&(timer->timer));
250 static struct vnet_host_hooks vnet_host_hooks = {
251 .timer_create = host_create_timer,
252 .timer_del = host_del_timer,
253 .timer_start = host_start_timer,
254 .timer_stop = host_stop_timer,
255 .timer_reset = host_reset_timer,
257 .thread_start = host_start_kernel_thread,
258 .thread_sleep = host_kthread_sleep,
259 .thread_wakeup = host_kthread_wakeup,
260 .thread_stop = host_kthread_stop,
261 .thread_should_stop = host_kthread_should_stop,
262 .udelay = host_udelay,
264 .yield_cpu = host_yield_cpu,
265 .mutex_alloc = host_mutex_alloc,
266 .mutex_free = host_mutex_free,
267 .mutex_lock = host_mutex_lock,
268 .mutex_unlock = host_mutex_unlock,
271 .allocate_pages = host_allocate_pages,
272 .free_pages = host_free_pages,
273 .malloc = host_alloc,
275 .vaddr_to_paddr = host_vaddr_to_paddr,
276 .paddr_to_vaddr = host_paddr_to_vaddr,
281 static int vnet_init( void ) {
282 init_vnet(&vnet_host_hooks);
287 printk("V3 VNET Inited\n");
293 static int vnet_deinit( void ) {
296 vnet_bridge_deinit();
299 printk("V3 VNET Deinited\n");
304 static struct linux_ext vnet_ext = {
307 .deinit = vnet_deinit,
312 register_extension(&vnet_ext);