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.


Minor bug fix
[palacios.git] / linux_module / palacios-stubs.c
1 #include <linux/kernel.h>
2 #include <linux/kthread.h>
3 #include <linux/spinlock.h>
4 #include <linux/gfp.h>
5 #include <linux/interrupt.h>
6 #include <linux/linkage.h>
7 #include <linux/sched.h>
8 #include <linux/uaccess.h>
9 #include <asm/irq_vectors.h>
10 #include <asm/io.h>
11
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kthread.h>
15 #include <asm/uaccess.h>
16 #include <linux/smp.h>
17 #include <linux/vmalloc.h>
18
19 #include <palacios/vmm.h>
20 #include <palacios/vmm_host_events.h>
21 #include "palacios.h"
22
23 #include "mm.h"
24
25 #include "memcheck.h"
26 #include "lockcheck.h"
27
28 // The following can be used to track heap bugs
29 // zero memory after allocation
30 #define ALLOC_ZERO_MEM 0
31 // pad allocations by this many bytes on both ends of block
32 #define ALLOC_PAD      0
33
34
35 u32 pg_allocs = 0;
36 u32 pg_frees = 0;
37 u32 mallocs = 0;
38 u32 frees = 0;
39 u32 vmallocs = 0;
40 u32 vfrees = 0;
41
42 static struct v3_vm_info * irq_to_guest_map[256];
43
44
45 extern unsigned int cpu_khz;
46
47 extern int cpu_list[NR_CPUS];
48 extern int cpu_list_len;
49
50
51 static char *print_buffer[NR_CPUS];
52
53 static void deinit_print_buffers(void)
54 {
55     int i;
56
57     for (i=0;i<NR_CPUS;i++) {
58         if (print_buffer[i]) { 
59             palacios_free(print_buffer[i]);
60             print_buffer[i]=0;
61         }
62     }
63 }
64
65 static int init_print_buffers(void)
66 {
67     int i;
68     
69     memset(print_buffer,0,sizeof(char*)*NR_CPUS);
70
71 #if !V3_PRINTK_OLD_STYLE_OUTPUT
72
73     for (i=0;i<NR_CPUS;i++) { 
74         print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
75         if (!print_buffer[i]) { 
76             ERROR("Cannot allocate print buffer for cpu %d\n",i);
77             deinit_print_buffers();
78             return -1;
79         }
80         memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
81     }
82
83 #endif
84     
85     return 0;
86
87 }
88  
89 /**
90  * Prints a message to the console.
91  */
92 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...) {
93
94 #if V3_PRINTK_OLD_STYLE_OUTPUT
95
96   va_list ap;
97
98   va_start(ap, fmt);
99   vprintk(fmt, ap);
100   va_end(ap);
101
102   return
103
104 #else 
105
106   va_list ap;
107   char *buf;
108   unsigned int cpu = palacios_get_cpu();
109   struct v3_guest *guest = (struct v3_guest *)vm;
110
111   buf = print_buffer[cpu];
112
113   if (!buf) { 
114       printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
115       return;
116   } 
117
118   va_start(ap, fmt);
119   vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
120   va_end(ap);
121
122 #if V3_PRINTK_CHECK_7BIT
123   {
124       char c=0;
125       int i;
126       for (i=0;i<strlen(buf);i++) { 
127           if (buf[i] < 0) {
128               c=buf[i];
129               break;
130           }
131       }
132       if (c!=0) { 
133           printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
134       }
135   }
136 #endif
137
138   if (guest) {
139     if (vcore>=0) { 
140       printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
141              cpu,
142              guest->name,
143              vcore,
144              buf);
145     } else {
146        printk(KERN_INFO "palacios (pcore %u vm %s): %s",
147              cpu,
148              guest->name,
149              buf);
150     }
151   } else {
152     printk(KERN_INFO "palacios (pcore %u): %s",
153            cpu,
154            buf);
155   }
156     
157   return;
158
159 #endif
160
161 }
162
163
164 /*
165  * Allocates a contiguous region of pages of the requested size.
166  * Returns the physical address of the first page in the region.
167  */
168 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
169     void * pg_addr = NULL;
170
171     pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
172
173     if (!pg_addr) { 
174         ERROR("ALERT ALERT  Page allocation has FAILED Warning\n");
175         return NULL;
176     }
177
178     pg_allocs += num_pages;
179
180     MEMCHECK_ALLOC_PAGES(pg_addr,num_pages*4096);
181
182     return pg_addr;
183 }
184
185
186 /**
187  * Frees a page previously allocated via palacios_allocate_page().
188  * Note that palacios_allocate_page() can allocate multiple pages with
189  * a single call while palacios_free_page() only frees a single page.
190  */
191
192 void palacios_free_pages(void * page_paddr, int num_pages) {
193     pg_frees += num_pages;
194     free_palacios_pgs((uintptr_t)page_paddr, num_pages);
195     MEMCHECK_FREE_PAGES(page_paddr,num_pages*4096);
196
197 }
198
199
200 void *
201 palacios_alloc_extended(unsigned int size, unsigned int flags) {
202     void * addr = NULL;
203
204     addr = kmalloc(size+2*ALLOC_PAD, flags);
205
206     if (!addr) { 
207        ERROR("ALERT ALERT  kmalloc has FAILED FAILED FAILED\n");
208        return NULL;
209     }   
210
211     mallocs++;
212
213 #if ALLOC_ZERO_MEM
214     memset(addr,0,size+2*ALLOC_PAD);
215 #endif
216
217     MEMCHECK_KMALLOC(addr,size+2*ALLOC_PAD);
218
219     return addr+ALLOC_PAD;
220 }
221
222 void *
223 palacios_valloc(unsigned int size)
224 {
225     void * addr = NULL;
226
227     addr = vmalloc(size);
228
229     if (!addr) { 
230        ERROR("ALERT ALERT  vmalloc has FAILED FAILED FAILED\n");
231        return NULL;
232     }   
233
234     vmallocs++;
235
236     MEMCHECK_VMALLOC(addr,size);
237
238     return addr;
239 }
240
241 void palacios_vfree(void *p)
242 {
243   vfree(p);
244   vfrees++;
245   MEMCHECK_VFREE(p);
246 }
247
248 /**
249  * Allocates 'size' bytes of kernel memory.
250  * Returns the kernel virtual address of the memory allocated.
251  */
252 void *
253 palacios_alloc(unsigned int size) {
254
255     // It is very important that this test remains since 
256     // this function is used extensively throughout palacios and the linux
257     // module, both in places where interrupts are off and where they are on
258     // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
259     if (irqs_disabled()) {
260         return palacios_alloc_extended(size,GFP_ATOMIC);
261     } else {
262         return palacios_alloc_extended(size,GFP_KERNEL);
263     }
264
265 }
266
267 /**
268  * Frees memory that was previously allocated by palacios_alloc().
269  */
270 void
271 palacios_free(
272         void *                  addr
273 )
274 {
275     frees++;
276     kfree(addr-ALLOC_PAD);
277     MEMCHECK_KFREE(addr-ALLOC_PAD);
278 }
279
280 /**
281  * Converts a kernel virtual address to the corresponding physical address.
282  */
283 void *
284 palacios_vaddr_to_paddr(
285         void *                  vaddr
286 )
287 {
288     return (void*) __pa(vaddr);
289
290 }
291
292 /**
293  * Converts a physical address to the corresponding kernel virtual address.
294  */
295 void *
296 palacios_paddr_to_vaddr(
297         void *                  paddr
298 )
299 {
300   return __va(paddr);
301 }
302
303 /**
304  * Runs a function on the specified CPU.
305  */
306 static void 
307 palacios_xcall(
308         int                     cpu_id, 
309         void                    (*fn)(void *arg),
310         void *                  arg
311 )
312 {
313
314
315     // We set wait to 1, but I'm not sure this is necessary
316     smp_call_function_single(cpu_id, fn, arg, 1);
317     
318     return;
319 }
320
321
322 #define MAX_THREAD_NAME 32
323
324 struct lnx_thread_arg {
325     int (*fn)(void * arg);
326     void * arg;
327     char name[MAX_THREAD_NAME];
328 };
329
330 static int lnx_thread_target(void * arg) {
331     struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
332     int ret = 0;
333     /*
334       INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
335
336       daemonize(thread_info->name);
337       allow_signal(SIGKILL);
338     */
339
340
341     ret = thread_info->fn(thread_info->arg);
342
343
344     INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
345
346     palacios_free(thread_info);
347     // handle cleanup 
348
349     do_exit(ret);
350     
351     return 0; // should not get here.
352 }
353
354 /**
355  * Creates a kernel thread.
356  */
357 void *
358 palacios_start_kernel_thread(
359         int (*fn)               (void * arg),
360         void *                  arg,
361         char *                  thread_name) {
362
363     struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
364
365     if (!thread_info) { 
366         ERROR("ALERT ALERT Unable to allocate thread\n");
367         return NULL;
368     }
369
370     thread_info->fn = fn;
371     thread_info->arg = arg;
372     strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
373     thread_info->name[MAX_THREAD_NAME-1] =0;
374
375     return kthread_run( lnx_thread_target, thread_info, thread_info->name );
376 }
377
378
379 /**
380  * Starts a kernel thread on the specified CPU.
381  */
382 void * 
383 palacios_start_thread_on_cpu(int cpu_id, 
384                              int (*fn)(void * arg), 
385                              void * arg, 
386                              char * thread_name ) {
387     struct task_struct * thread = NULL;
388     struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
389
390     if (!thread_info) { 
391         ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
392         return NULL;
393     }
394
395     thread_info->fn = fn;
396     thread_info->arg = arg;
397     strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
398     thread_info->name[MAX_THREAD_NAME-1] =0;
399
400     thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
401
402     if (IS_ERR(thread)) {
403         WARNING("Palacios error creating thread: %s\n", thread_info->name);
404         palacios_free(thread_info);
405         return NULL;
406     }
407
408     if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
409         WARNING("Attempt to start thread on disallowed CPU\n");
410         kthread_stop(thread);
411         palacios_free(thread_info);
412         return NULL;
413     }
414
415     wake_up_process(thread);
416
417     return thread;
418 }
419
420
421 /**
422  * Rebind a kernel thread to the specified CPU
423  * The thread will be running on target CPU on return
424  * non-zero return means failure
425  */
426 int
427 palacios_move_thread_to_cpu(int new_cpu_id, 
428                             void * thread_ptr) {
429     struct task_struct * thread = (struct task_struct *)thread_ptr;
430
431     INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
432
433     if (thread == NULL) {
434         thread = current;
435     }
436
437     /*
438      * Bind to the specified CPU.  When this call returns,
439      * the thread should be running on the target CPU.
440      */
441     return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
442 }
443
444
445 /**
446  * Returns the CPU ID that the caller is running on.
447  */
448 unsigned int 
449 palacios_get_cpu(void) 
450 {
451
452     /* We want to call smp_processor_id()
453      * But this is not safe if kernel preemption is possible 
454      * We need to ensure that the palacios threads are bound to a give cpu
455      */
456
457     unsigned int cpu_id = get_cpu(); 
458     put_cpu();
459     return cpu_id;
460 }
461
462 /**
463  * Interrupts the physical CPU corresponding to the specified logical guest cpu.
464  *
465  * NOTE: 
466  * This is dependent on the implementation of xcall_reschedule().  Currently
467  * xcall_reschedule does not explicitly call schedule() on the destination CPU,
468  * but instead relies on the return to user space to handle it. Because
469  * palacios is a kernel thread schedule will not be called, which is correct.
470  * If it ever changes to induce side effects, we'll need to figure something
471  * else out...
472  */
473
474 #include <asm/apic.h>
475
476 static void
477 palacios_interrupt_cpu(
478         struct v3_vm_info *     vm, 
479         int                     cpu_id, 
480         int                     vector
481 )
482 {
483     if (vector == 0) {
484         smp_send_reschedule(cpu_id);
485     } else {
486         apic->send_IPI_mask(cpumask_of(cpu_id), vector);
487     }
488     return;
489 }
490
491 /**
492  * Dispatches an interrupt to Palacios for handling.
493  */
494 static void
495 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
496     struct v3_interrupt intr = {
497         .irq            = vector,
498         .error          = regs->orig_ax,
499         .should_ack     = 1,
500     };
501     
502     if (irq_to_guest_map[vector]) {
503         v3_deliver_irq(irq_to_guest_map[vector], &intr);
504     }
505     
506 }
507
508 /**
509  * Instructs the kernel to forward the specified IRQ to Palacios.
510  */
511 static int
512 palacios_hook_interrupt(struct v3_vm_info *     vm,
513                         unsigned int            vector ) {
514     INFO("hooking vector %d\n", vector);        
515
516     if (irq_to_guest_map[vector]) {
517         WARNING(
518                "%s: Interrupt vector %u is already hooked.\n",
519                __func__, vector);
520         return -1;
521     }
522
523     DEBUG(
524            "%s: Hooking interrupt vector %u to vm %p.\n",
525            __func__, vector, vm);
526
527     irq_to_guest_map[vector] = vm;
528
529     /*
530      * NOTE: Normally PCI devices are supposed to be level sensitive,
531      *       but we need them to be edge sensitive so that they are
532      *       properly latched by Palacios.  Leaving them as level
533      *       sensitive would lead to an interrupt storm.
534      */
535     //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
536     
537     //set_idtvec_handler(vector, palacios_dispatch_interrupt);
538     if (vector < 32) {
539         ERROR("unexpected vector for hooking\n");
540         return -1;
541     } else {
542         int device_id = 0;              
543         
544         int flag = 0;
545         int error;
546                 
547         DEBUG("hooking vector: %d\n", vector);          
548
549         if (vector == 32) {
550             flag = IRQF_TIMER;
551         } else {
552             flag = IRQF_SHARED;
553         }
554
555         error = request_irq((vector - 32),
556                             (void *)palacios_dispatch_interrupt,
557                             flag,
558                             "interrupt_for_palacios",
559                             &device_id);
560         
561         if (error) {
562             ERROR("error code for request_irq is %d\n", error);
563             ERROR("request vector %d failed", vector);
564             return -1;
565         }
566     }
567         
568     return 0;
569 }
570
571
572
573 /**
574  * Acknowledges an interrupt.
575  */
576 static int
577 palacios_ack_interrupt(
578         int                     vector
579
580 {
581   ack_APIC_irq(); 
582   DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
583   return 0;
584 }
585   
586 /**
587  * Returns the CPU frequency in kilohertz.
588  */
589 unsigned int
590 palacios_get_cpu_khz(void) 
591 {
592     INFO("cpu_khz is %u\n", cpu_khz);
593
594     if (cpu_khz == 0) { 
595         INFO("faking cpu_khz to 1000000\n");
596         return 1000000;
597     } else {
598         return cpu_khz;
599     }
600   //return 1000000;
601 }
602
603 /**
604  * Yield the CPU so other host OS tasks can run.
605  * This will return immediately if there is no other thread that is runnable
606  * And there is no real bound on how long it will yield
607  */
608 void
609 palacios_yield_cpu(void)
610 {
611     schedule();
612     return;
613 }
614
615 /**
616  * Yield the CPU so other host OS tasks can run.
617  * Given now immediately if there is no other thread that is runnable
618  * And there is no real bound on how long it will yield
619  */
620 void palacios_sleep_cpu(unsigned int us)
621 {
622
623     set_current_state(TASK_INTERRUPTIBLE);
624     if (us) {
625         unsigned int uspj = 1000000U/HZ;
626         unsigned int jiffies = us/uspj + ((us%uspj) !=0);  // ceiling 
627         schedule_timeout(jiffies);
628     } else {
629         schedule();
630     }
631     return;
632 }
633
634 void palacios_wakeup_cpu(void *thread)
635 {
636     wake_up_process(thread);
637     return;
638 }
639
640 /**
641  * Allocates a mutex.
642  * Returns NULL on failure.
643  */
644 void *
645 palacios_mutex_alloc(void)
646 {
647     spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
648
649     if (lock) {
650         spin_lock_init(lock);
651         LOCKCHECK_ALLOC(lock);
652     } else {
653         ERROR("ALERT ALERT Unable to allocate lock\n");
654         return NULL;
655     }
656     
657     return lock;
658 }
659
660 void palacios_mutex_init(void *mutex)
661 {
662   spinlock_t *lock = (spinlock_t*)mutex;
663   
664   if (lock) {
665     spin_lock_init(lock);
666     LOCKCHECK_ALLOC(lock);
667   }
668 }
669
670
671 /**
672  * Frees a mutex.
673  */
674 void
675 palacios_mutex_free(void * mutex) {
676     palacios_free(mutex);
677     LOCKCHECK_FREE(mutex);
678 }
679
680 /**
681  * Locks a mutex.
682  */
683 void 
684 palacios_mutex_lock(void * mutex, int must_spin) {
685     spin_lock((spinlock_t *)mutex);
686     LOCKCHECK_LOCK(mutex);
687 }
688
689
690 /**
691  * Locks a mutex, disabling interrupts on this core
692  */
693 void *
694 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
695     
696     unsigned long flags; 
697     
698     spin_lock_irqsave((spinlock_t *)mutex,flags);
699     LOCKCHECK_LOCK_IRQSAVE(mutex,flags);
700
701     return (void *)flags;
702 }
703
704
705 /**
706  * Unlocks a mutex.
707  */
708 void 
709 palacios_mutex_unlock(
710         void *                  mutex
711
712 {
713     spin_unlock((spinlock_t *)mutex);
714     LOCKCHECK_UNLOCK(mutex);
715 }
716
717
718 /**
719  * Unlocks a mutex and restores previous interrupt state on this core
720  */
721 void 
722 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
723 {
724     // This is correct, flags is opaque
725     spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
726     LOCKCHECK_UNLOCK_IRQRESTORE(mutex,(unsigned long)flags);
727 }
728
729 /**
730  * Structure used by the Palacios hypervisor to interface with the host kernel.
731  */
732 static struct v3_os_hooks palacios_os_hooks = {
733         .print                  = palacios_print_scoped,
734         .allocate_pages         = palacios_allocate_pages,
735         .free_pages             = palacios_free_pages,
736         .malloc                 = palacios_alloc,
737         .free                   = palacios_free,
738         .vaddr_to_paddr         = palacios_vaddr_to_paddr,
739         .paddr_to_vaddr         = palacios_paddr_to_vaddr,
740         .hook_interrupt         = palacios_hook_interrupt,
741         .ack_irq                = palacios_ack_interrupt,
742         .get_cpu_khz            = palacios_get_cpu_khz,
743         .start_kernel_thread    = palacios_start_kernel_thread,
744         .yield_cpu              = palacios_yield_cpu,
745         .sleep_cpu              = palacios_sleep_cpu,
746         .wakeup_cpu             = palacios_wakeup_cpu,
747         .mutex_alloc            = palacios_mutex_alloc,
748         .mutex_free             = palacios_mutex_free,
749         .mutex_lock             = palacios_mutex_lock, 
750         .mutex_unlock           = palacios_mutex_unlock,
751         .mutex_lock_irqsave     = palacios_mutex_lock_irqsave, 
752         .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
753         .get_cpu                = palacios_get_cpu,
754         .interrupt_cpu          = palacios_interrupt_cpu,
755         .call_on_cpu            = palacios_xcall,
756         .start_thread_on_cpu    = palacios_start_thread_on_cpu,
757         .move_thread_to_cpu     = palacios_move_thread_to_cpu,
758 };
759
760
761
762
763 int palacios_vmm_init( char *options )
764 {
765     int num_cpus = num_online_cpus();
766     char * cpu_mask = NULL;
767
768     if (cpu_list_len > 0) {
769         int major = 0;
770         int minor = 0;
771         int i = 0;
772
773         cpu_mask = palacios_alloc((num_cpus / 8) + 1);
774
775         if (!cpu_mask) { 
776             ERROR("Cannot allocate cpu mask\n");
777             return -1;
778         }
779
780         memset(cpu_mask, 0, (num_cpus / 8) + 1);
781         
782         for (i = 0; i < cpu_list_len; i++) {
783             if (cpu_list[i] >= num_cpus) {
784                 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
785                 continue;
786             }
787
788             major = cpu_list[i] / 8;
789             minor = cpu_list[i] % 8;
790     
791             *(cpu_mask + major) |= (0x1 << minor);
792         }
793     }
794
795     memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
796
797     if (init_print_buffers()) {
798         ERROR("Cannot initialize print buffers\n");
799         palacios_free(cpu_mask);
800         return -1;
801     }
802
803     INFO("palacios_init starting - calling init_v3\n");
804
805     Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
806
807     return 0;
808
809 }
810
811
812 int palacios_vmm_exit( void ) {
813
814     Shutdown_V3();
815
816     INFO("palacios shutdown complete\n");
817
818     deinit_print_buffers();
819
820     return 0;
821 }