1 #include <linux/kernel.h>
2 #include <linux/kthread.h>
3 #include <linux/spinlock.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>
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>
19 #include <palacios/vmm.h>
20 #include <palacios/vmm_host_events.h>
26 #include "lockcheck.h"
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
42 static struct v3_vm_info * irq_to_guest_map[256];
45 extern unsigned int cpu_khz;
47 extern int cpu_list[NR_CPUS];
48 extern int cpu_list_len;
51 static char *print_buffer[NR_CPUS];
53 static void deinit_print_buffers(void)
57 for (i=0;i<NR_CPUS;i++) {
58 if (print_buffer[i]) {
59 palacios_free(print_buffer[i]);
65 static int init_print_buffers(void)
69 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
71 #if !V3_PRINTK_OLD_STYLE_OUTPUT
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();
80 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
90 * Prints a message to the console.
92 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...) {
94 #if V3_PRINTK_OLD_STYLE_OUTPUT
108 unsigned int cpu = palacios_get_cpu();
109 struct v3_guest *guest = (struct v3_guest *)vm;
111 buf = print_buffer[cpu];
114 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
119 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
122 #if V3_PRINTK_CHECK_7BIT
126 for (i=0;i<strlen(buf);i++) {
133 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
140 printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
146 printk(KERN_INFO "palacios (pcore %u vm %s): %s",
152 printk(KERN_INFO "palacios (pcore %u): %s",
165 * Allocates a contiguous region of pages of the requested size.
166 * Returns the physical address of the first page in the region.
168 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
169 void * pg_addr = NULL;
171 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
174 ERROR("ALERT ALERT Page allocation has FAILED Warning\n");
178 pg_allocs += num_pages;
180 MEMCHECK_ALLOC_PAGES(pg_addr,num_pages*4096);
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.
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);
201 palacios_alloc_extended(unsigned int size, unsigned int flags) {
204 addr = kmalloc(size+2*ALLOC_PAD, flags);
207 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
214 memset(addr,0,size+2*ALLOC_PAD);
217 MEMCHECK_KMALLOC(addr+ALLOC_PAD,size+2*ALLOC_PAD);
219 return addr+ALLOC_PAD;
223 palacios_valloc(unsigned int size)
227 addr = vmalloc(size);
230 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
236 MEMCHECK_VMALLOC(addr,size);
241 void palacios_vfree(void *p)
249 * Allocates 'size' bytes of kernel memory.
250 * Returns the kernel virtual address of the memory allocated.
253 palacios_alloc(unsigned int size) {
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);
262 return palacios_alloc_extended(size,GFP_KERNEL);
268 * Frees memory that was previously allocated by palacios_alloc().
276 kfree(addr-ALLOC_PAD);
277 MEMCHECK_KFREE(addr-ALLOC_PAD);
281 * Converts a kernel virtual address to the corresponding physical address.
284 palacios_vaddr_to_paddr(
288 return (void*) __pa(vaddr);
293 * Converts a physical address to the corresponding kernel virtual address.
296 palacios_paddr_to_vaddr(
304 * Runs a function on the specified CPU.
309 void (*fn)(void *arg),
315 // We set wait to 1, but I'm not sure this is necessary
316 smp_call_function_single(cpu_id, fn, arg, 1);
322 #define MAX_THREAD_NAME 32
324 struct lnx_thread_arg {
325 int (*fn)(void * arg);
327 char name[MAX_THREAD_NAME];
330 static int lnx_thread_target(void * arg) {
331 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
334 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
336 daemonize(thread_info->name);
337 allow_signal(SIGKILL);
341 ret = thread_info->fn(thread_info->arg);
344 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
346 palacios_free(thread_info);
351 return 0; // should not get here.
355 * Creates a kernel thread.
358 palacios_start_kernel_thread(
359 int (*fn) (void * arg),
361 char * thread_name) {
363 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
366 ERROR("ALERT ALERT Unable to allocate thread\n");
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;
375 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
380 * Starts a kernel thread on the specified CPU.
383 palacios_start_thread_on_cpu(int cpu_id,
384 int (*fn)(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));
391 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
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;
400 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
402 if (IS_ERR(thread)) {
403 WARNING("Palacios error creating thread: %s\n", thread_info->name);
404 palacios_free(thread_info);
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);
415 wake_up_process(thread);
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
427 palacios_move_thread_to_cpu(int new_cpu_id,
429 struct task_struct * thread = (struct task_struct *)thread_ptr;
431 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
433 if (thread == NULL) {
438 * Bind to the specified CPU. When this call returns,
439 * the thread should be running on the target CPU.
441 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
446 * Returns the CPU ID that the caller is running on.
449 palacios_get_cpu(void)
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
457 unsigned int cpu_id = get_cpu();
463 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
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
474 #include <asm/apic.h>
477 palacios_interrupt_cpu(
478 struct v3_vm_info * vm,
484 smp_send_reschedule(cpu_id);
486 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
492 * Dispatches an interrupt to Palacios for handling.
495 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
496 struct v3_interrupt intr = {
498 .error = regs->orig_ax,
502 if (irq_to_guest_map[vector]) {
503 v3_deliver_irq(irq_to_guest_map[vector], &intr);
509 * Instructs the kernel to forward the specified IRQ to Palacios.
512 palacios_hook_interrupt(struct v3_vm_info * vm,
513 unsigned int vector ) {
514 INFO("hooking vector %d\n", vector);
516 if (irq_to_guest_map[vector]) {
518 "%s: Interrupt vector %u is already hooked.\n",
524 "%s: Hooking interrupt vector %u to vm %p.\n",
525 __func__, vector, vm);
527 irq_to_guest_map[vector] = vm;
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.
535 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
537 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
539 ERROR("unexpected vector for hooking\n");
547 DEBUG("hooking vector: %d\n", vector);
555 error = request_irq((vector - 32),
556 (void *)palacios_dispatch_interrupt,
558 "interrupt_for_palacios",
562 ERROR("error code for request_irq is %d\n", error);
563 ERROR("request vector %d failed", vector);
574 * Acknowledges an interrupt.
577 palacios_ack_interrupt(
582 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
587 * Returns the CPU frequency in kilohertz.
590 palacios_get_cpu_khz(void)
592 INFO("cpu_khz is %u\n", cpu_khz);
595 INFO("faking cpu_khz to 1000000\n");
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
609 palacios_yield_cpu(void)
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
620 void palacios_sleep_cpu(unsigned int us)
623 set_current_state(TASK_INTERRUPTIBLE);
625 unsigned int uspj = 1000000U/HZ;
626 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
627 schedule_timeout(jiffies);
634 void palacios_wakeup_cpu(void *thread)
636 wake_up_process(thread);
642 * Returns NULL on failure.
645 palacios_mutex_alloc(void)
647 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
650 spin_lock_init(lock);
651 LOCKCHECK_ALLOC(lock);
653 ERROR("ALERT ALERT Unable to allocate lock\n");
660 void palacios_mutex_init(void *mutex)
662 spinlock_t *lock = (spinlock_t*)mutex;
665 spin_lock_init(lock);
666 LOCKCHECK_ALLOC(lock);
675 palacios_mutex_free(void * mutex) {
676 palacios_free(mutex);
677 LOCKCHECK_FREE(mutex);
684 palacios_mutex_lock(void * mutex, int must_spin) {
685 spin_lock((spinlock_t *)mutex);
686 LOCKCHECK_LOCK(mutex);
691 * Locks a mutex, disabling interrupts on this core
694 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
698 spin_lock_irqsave((spinlock_t *)mutex,flags);
699 LOCKCHECK_LOCK_IRQSAVE(mutex,flags);
701 return (void *)flags;
709 palacios_mutex_unlock(
713 spin_unlock((spinlock_t *)mutex);
714 LOCKCHECK_UNLOCK(mutex);
719 * Unlocks a mutex and restores previous interrupt state on this core
722 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
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);
730 * Structure used by the Palacios hypervisor to interface with the host kernel.
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,
763 int palacios_vmm_init( char *options )
765 int num_cpus = num_online_cpus();
766 char * cpu_mask = NULL;
768 if (cpu_list_len > 0) {
773 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
776 ERROR("Cannot allocate cpu mask\n");
780 memset(cpu_mask, 0, (num_cpus / 8) + 1);
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]);
788 major = cpu_list[i] / 8;
789 minor = cpu_list[i] % 8;
791 *(cpu_mask + major) |= (0x1 << minor);
795 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
797 if (init_print_buffers()) {
798 ERROR("Cannot initialize print buffers\n");
799 palacios_free(cpu_mask);
803 INFO("palacios_init starting - calling init_v3\n");
805 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
812 int palacios_vmm_exit( void ) {
816 INFO("palacios shutdown complete\n");
818 deinit_print_buffers();