2 * Palacios VNET Host Hooks Implementations
6 #include <linux/kernel.h>
7 #include <linux/kthread.h>
8 #include <linux/spinlock.h>
10 #include <linux/sched.h>
11 #include <linux/uaccess.h>
12 #include <linux/init.h>
13 #include <linux/smp_lock.h>
14 #include <asm/delay.h>
15 #include <linux/timer.h>
17 #include <vnet/vnet.h>
19 #include "palacios-vnet.h"
20 #include "linux-exts.h"
22 static void host_print(const char * fmt, ...) {
33 static void * host_allocate_pages(int num_pages, unsigned int alignment){
35 struct page * pgs = NULL;
36 int order = get_order(num_pages * PAGE_SIZE);
38 pgs = alloc_pages(GFP_KERNEL, order);
40 WARN(!pgs, "Could not allocate pages\n");
42 addr = page_to_pfn(pgs) << PAGE_SHIFT;
48 static void host_free_pages(void * page_paddr, int num_pages) {
49 uintptr_t pg_addr = (uintptr_t)page_paddr;
51 __free_pages(pfn_to_page(pg_addr >> PAGE_SHIFT), get_order(num_pages * PAGE_SIZE));
56 host_alloc(unsigned int size) {
58 addr = kmalloc(size, GFP_KERNEL);
73 host_vaddr_to_paddr(void * vaddr)
75 return (void*) __pa(vaddr);
80 host_paddr_to_vaddr(void * paddr)
87 host_start_kernel_thread(
88 int (*fn)(void * arg),
91 struct task_struct * thread = NULL;
93 thread = kthread_run(fn, arg, thread_name );
98 static void host_kthread_sleep(long timeout){
99 set_current_state(TASK_INTERRUPTIBLE);
104 schedule_timeout(timeout);
110 static void host_kthread_wakeup(void * thread){
111 struct task_struct * kthread = (struct task_struct *)thread;
113 wake_up_process(kthread);
116 static void host_kthread_stop(void * thread){
117 struct task_struct * kthread = (struct task_struct *)thread;
119 kthread_stop(kthread);
122 static int host_kthread_should_stop(void){
123 return kthread_should_stop();
127 static void host_udelay(unsigned long usecs){
141 host_mutex_alloc(void)
143 spinlock_t * lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
146 spin_lock_init(lock);
161 host_mutex_lock(void * mutex,
164 spin_lock((spinlock_t *)mutex);
168 host_mutex_unlock(void * mutex)
170 spin_unlock((spinlock_t *)mutex);
177 struct timer_list timer;
178 unsigned long interval;
181 void (* timer_fun)(void * private_data);
186 void timeout_fn(unsigned long arg){
187 struct host_timer * timer = (struct host_timer *)arg;
190 timer->timer_fun(timer->pri_data);
192 mod_timer(&(timer->timer), timer->interval);
197 host_create_timer(unsigned long interval,
198 void (* timer_fun)(void * priv_data),
200 struct host_timer * timer = (struct host_timer *)kmalloc(sizeof(struct host_timer), GFP_KERNEL);
202 timer->interval = interval;
203 timer->timer_fun = timer_fun;
204 timer->pri_data = data;
206 init_timer(&(timer->timer));
208 timer->timer.data = (unsigned long)timer;
209 timer->timer.function = timeout_fn;
210 timer->timer.expires = interval;
216 host_start_timer(void * vnet_timer){
217 struct host_timer * timer = (struct host_timer *)vnet_timer;
220 add_timer(&(timer->timer));
224 host_reset_timer(void * vnet_timer, unsigned long interval){
225 struct host_timer * timer = (struct host_timer *)timer;
227 timer->interval = interval;
231 host_stop_timer(void * vnet_timer){
232 struct host_timer * timer = (struct host_timer *)vnet_timer;
235 del_timer(&(timer->timer));
239 host_del_timer(void * vnet_timer){
240 struct host_timer * timer = (struct host_timer *)vnet_timer;
242 del_timer(&(timer->timer));
251 static struct vnet_host_hooks vnet_host_hooks = {
252 .timer_create = host_create_timer,
253 .timer_del = host_del_timer,
254 .timer_start = host_start_timer,
255 .timer_stop = host_stop_timer,
256 .timer_reset = host_reset_timer,
258 .thread_start = host_start_kernel_thread,
259 .thread_sleep = host_kthread_sleep,
260 .thread_wakeup = host_kthread_wakeup,
261 .thread_stop = host_kthread_stop,
262 .thread_should_stop = host_kthread_should_stop,
263 .udelay = host_udelay,
265 .yield_cpu = host_yield_cpu,
266 .mutex_alloc = host_mutex_alloc,
267 .mutex_free = host_mutex_free,
268 .mutex_lock = host_mutex_lock,
269 .mutex_unlock = host_mutex_unlock,
272 .allocate_pages = host_allocate_pages,
273 .free_pages = host_free_pages,
274 .malloc = host_alloc,
276 .vaddr_to_paddr = host_vaddr_to_paddr,
277 .paddr_to_vaddr = host_paddr_to_vaddr,
282 static int vnet_init( void ) {
283 init_vnet(&vnet_host_hooks);
288 printk("V3 VNET Inited\n");
294 static int vnet_deinit( void ) {
297 vnet_bridge_deinit();
300 printk("V3 VNET Deinited\n");
305 static struct linux_ext vnet_ext = {
308 .deinit = vnet_deinit,
313 register_extension(&vnet_ext);