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>
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_host_events.h>
24 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
25 #include <interfaces/vmm_lazy_fpu.h>
33 #include "lockcheck.h"
37 // The following can be used to track heap bugs
38 // zero memory after allocation
39 #define ALLOC_ZERO_MEM 0
40 // pad allocations by this many bytes on both ends of block
51 static struct v3_vm_info * irq_to_guest_map[256];
54 extern unsigned int cpu_khz;
56 extern int cpu_list[NR_CPUS];
57 extern int cpu_list_len;
60 static char *print_buffer[NR_CPUS];
62 static void deinit_print_buffers(void)
66 for (i=0;i<NR_CPUS;i++) {
67 if (print_buffer[i]) {
68 palacios_free(print_buffer[i]);
74 static int init_print_buffers(void)
78 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
80 #if !V3_PRINTK_OLD_STYLE_OUTPUT
82 for (i=0;i<NR_CPUS;i++) {
83 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
84 if (!print_buffer[i]) {
85 ERROR("Cannot allocate print buffer for cpu %d\n",i);
86 deinit_print_buffers();
89 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
99 * Prints a message to the console.
101 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...) {
103 #if V3_PRINTK_OLD_STYLE_OUTPUT
117 unsigned int cpu = palacios_get_cpu();
118 struct v3_guest *guest = (struct v3_guest *)vm;
120 buf = print_buffer[cpu];
123 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
128 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
131 #if V3_PRINTK_CHECK_7BIT
135 for (i=0;i<strlen(buf);i++) {
142 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
149 printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
155 printk(KERN_INFO "palacios (pcore %u vm %s): %s",
161 printk(KERN_INFO "palacios (pcore %u): %s",
174 * Allocates a contiguous region of pages of the requested size.
175 * Returns the physical address of the first page in the region.
177 void *palacios_allocate_pages(int num_pages, unsigned int alignment, int node_id, int constraints) {
178 void * pg_addr = NULL;
181 ERROR("ALERT ALERT Attempt to allocate zero or fewer pages (%d pages, alignment %d, node %d, constraints 0x%x)\n",num_pages, alignment, node_id, constraints);
185 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment, node_id, constraints);
188 ERROR("ALERT ALERT Page allocation has FAILED Warning (%d pages, alignment %d, node %d, constraints 0x%x)\n",num_pages, alignment, node_id, constraints);
192 pg_allocs += num_pages;
194 MEMCHECK_ALLOC_PAGES(pg_addr,num_pages*4096);
201 * Frees a page previously allocated via palacios_allocate_page().
202 * Note that palacios_allocate_page() can allocate multiple pages with
203 * a single call while palacios_free_page() only frees a single page.
206 void palacios_free_pages(void * page_paddr, int num_pages) {
208 ERROR("Ignoring free pages: 0x%p (0x%lx)for %d pages\n", page_paddr, (uintptr_t)page_paddr, num_pages);
211 pg_frees += num_pages;
212 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
213 MEMCHECK_FREE_PAGES(page_paddr,num_pages*4096);
219 palacios_alloc_extended(unsigned int size, unsigned int flags, int node) {
223 // note that modern kernels will respond to a zero byte
224 // kmalloc and return the address 0x10... In Palacios,
225 // we will simply not allow 0 byte allocs at all, of any kind
226 ERROR("ALERT ALERT attempt to kmalloc zero bytes rejected\n");
231 addr = kmalloc(size+2*ALLOC_PAD, flags);
233 addr = kmalloc_node(size+2*ALLOC_PAD, flags, node);
237 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
244 memset(addr,0,size+2*ALLOC_PAD);
247 MEMCHECK_KMALLOC(addr,size+2*ALLOC_PAD);
249 return addr+ALLOC_PAD;
253 palacios_valloc(unsigned int size)
258 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
262 addr = vmalloc(size);
265 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
271 MEMCHECK_VMALLOC(addr,size);
276 void palacios_vfree(void *p)
284 * Allocates 'size' bytes of kernel memory.
285 * Returns the kernel virtual address of the memory allocated.
288 palacios_alloc(unsigned int size) {
290 // It is very important that this test remains since
291 // this function is used extensively throughout palacios and the linux
292 // module, both in places where interrupts are off and where they are on
293 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
294 if (irqs_disabled()) {
295 return palacios_alloc_extended(size,GFP_ATOMIC,-1);
297 return palacios_alloc_extended(size,GFP_KERNEL,-1);
303 * Frees memory that was previously allocated by palacios_alloc().
311 ERROR("Ignoring free : 0x%p\n", addr);
315 kfree(addr-ALLOC_PAD);
316 MEMCHECK_KFREE(addr-ALLOC_PAD);
320 * Converts a kernel virtual address to the corresponding physical address.
323 palacios_vaddr_to_paddr(
327 return (void*) __pa(vaddr);
332 * Converts a physical address to the corresponding kernel virtual address.
335 palacios_paddr_to_vaddr(
343 * Runs a function on the specified CPU.
348 void (*fn)(void *arg),
354 // We set wait to 1, but I'm not sure this is necessary
355 smp_call_function_single(cpu_id, fn, arg, 1);
361 #define MAX_THREAD_NAME 32
363 struct lnx_thread_arg {
364 int (*fn)(void * arg);
366 char name[MAX_THREAD_NAME];
369 static int lnx_thread_target(void * arg) {
370 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
373 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
375 daemonize(thread_info->name);
376 allow_signal(SIGKILL);
379 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
380 // We are a kernel thread that needs FPU save/restore state
381 // vcores definitely need this, all the other threads get it too,
382 // but they just won't use it
383 fpu_alloc(&(current->thread.fpu));
386 ret = thread_info->fn(thread_info->arg);
388 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
390 palacios_free(thread_info);
393 // We rely on do_exit to free the fpu data
394 // since we could get switched at any point until the thread is done...
398 return 0; // should not get here.
402 * Creates a kernel thread.
405 palacios_start_kernel_thread(
406 int (*fn) (void * arg),
408 char * thread_name) {
410 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
413 ERROR("ALERT ALERT Unable to allocate thread\n");
417 thread_info->fn = fn;
418 thread_info->arg = arg;
419 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
420 thread_info->name[MAX_THREAD_NAME-1] =0;
422 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
427 * Starts a kernel thread on the specified CPU.
430 palacios_start_thread_on_cpu(int cpu_id,
431 int (*fn)(void * arg),
433 char * thread_name ) {
434 struct task_struct * thread = NULL;
435 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
438 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
442 thread_info->fn = fn;
443 thread_info->arg = arg;
444 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
445 thread_info->name[MAX_THREAD_NAME-1] =0;
447 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
449 if (IS_ERR(thread)) {
450 WARNING("Palacios error creating thread: %s\n", thread_info->name);
451 palacios_free(thread_info);
455 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
456 WARNING("Attempt to start thread on disallowed CPU\n");
457 kthread_stop(thread);
458 palacios_free(thread_info);
462 wake_up_process(thread);
469 * Rebind a kernel thread to the specified CPU
470 * The thread will be running on target CPU on return
471 * non-zero return means failure
474 palacios_move_thread_to_cpu(int new_cpu_id,
476 struct task_struct * thread = (struct task_struct *)thread_ptr;
478 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
480 if (thread == NULL) {
485 * Bind to the specified CPU. When this call returns,
486 * the thread should be running on the target CPU.
488 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
493 * Returns the CPU ID that the caller is running on.
496 palacios_get_cpu(void)
499 /* We want to call smp_processor_id()
500 * But this is not safe if kernel preemption is possible
501 * We need to ensure that the palacios threads are bound to a give cpu
504 unsigned int cpu_id = get_cpu();
510 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
513 * This is dependent on the implementation of xcall_reschedule(). Currently
514 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
515 * but instead relies on the return to user space to handle it. Because
516 * palacios is a kernel thread schedule will not be called, which is correct.
517 * If it ever changes to induce side effects, we'll need to figure something
521 #include <asm/apic.h>
524 palacios_interrupt_cpu(
525 struct v3_vm_info * vm,
531 smp_send_reschedule(cpu_id);
533 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
539 * Dispatches an interrupt to Palacios for handling.
542 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
543 struct v3_interrupt intr = {
545 .error = regs->orig_ax,
549 if (irq_to_guest_map[vector]) {
550 v3_deliver_irq(irq_to_guest_map[vector], &intr);
556 * Instructs the kernel to forward the specified IRQ to Palacios.
559 palacios_hook_interrupt(struct v3_vm_info * vm,
560 unsigned int vector ) {
561 INFO("hooking vector %d\n", vector);
563 if (irq_to_guest_map[vector]) {
565 "%s: Interrupt vector %u is already hooked.\n",
571 "%s: Hooking interrupt vector %u to vm %p.\n",
572 __func__, vector, vm);
574 irq_to_guest_map[vector] = vm;
577 * NOTE: Normally PCI devices are supposed to be level sensitive,
578 * but we need them to be edge sensitive so that they are
579 * properly latched by Palacios. Leaving them as level
580 * sensitive would lead to an interrupt storm.
582 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
584 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
586 ERROR("unexpected vector for hooking\n");
594 DEBUG("hooking vector: %d\n", vector);
602 error = request_irq((vector - 32),
603 (void *)palacios_dispatch_interrupt,
605 "interrupt_for_palacios",
609 ERROR("error code for request_irq is %d\n", error);
610 ERROR("request vector %d failed", vector);
621 * Acknowledges an interrupt.
624 palacios_ack_interrupt(
629 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
634 * Returns the CPU frequency in kilohertz.
637 palacios_get_cpu_khz(void)
639 INFO("cpu_khz is %u\n", cpu_khz);
642 INFO("faking cpu_khz to 1000000\n");
651 * Yield the CPU so other host OS tasks can run.
652 * This will return immediately if there is no other thread that is runnable
653 * And there is no real bound on how long it will yield
656 palacios_yield_cpu(void)
663 * Yield the CPU so other host OS tasks can run.
664 * Given now immediately if there is no other thread that is runnable
665 * And there is no real bound on how long it will yield
667 void palacios_sleep_cpu(unsigned int us)
670 set_current_state(TASK_INTERRUPTIBLE);
672 unsigned int uspj = 1000000U/HZ;
673 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
674 schedule_timeout(jiffies);
681 void palacios_wakeup_cpu(void *thread)
683 wake_up_process(thread);
689 * Returns NULL on failure.
692 palacios_mutex_alloc(void)
694 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
697 spin_lock_init(lock);
698 LOCKCHECK_ALLOC(lock);
700 ERROR("ALERT ALERT Unable to allocate lock\n");
707 void palacios_mutex_init(void *mutex)
709 spinlock_t *lock = (spinlock_t*)mutex;
712 spin_lock_init(lock);
713 LOCKCHECK_ALLOC(lock);
717 void palacios_mutex_deinit(void *mutex)
719 spinlock_t *lock = (spinlock_t*)mutex;
722 // no actual spin_lock_deinit on linux
723 // our purpose here is to drive the lock checker
724 LOCKCHECK_FREE(lock);
733 palacios_mutex_free(void * mutex) {
734 palacios_free(mutex);
735 LOCKCHECK_FREE(mutex);
742 palacios_mutex_lock(void * mutex, int must_spin) {
744 LOCKCHECK_LOCK_PRE(mutex);
745 spin_lock((spinlock_t *)mutex);
746 LOCKCHECK_LOCK_POST(mutex);
751 * Locks a mutex, disabling interrupts on this core
754 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
758 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
759 spin_lock_irqsave((spinlock_t *)mutex,flags);
760 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
762 return (void *)flags;
770 palacios_mutex_unlock(
774 LOCKCHECK_UNLOCK_PRE(mutex);
775 spin_unlock((spinlock_t *)mutex);
776 LOCKCHECK_UNLOCK_POST(mutex);
781 * Unlocks a mutex and restores previous interrupt state on this core
784 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
786 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
787 // This is correct, flags is opaque
788 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
789 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
792 void palacios_used_fpu(void)
794 struct thread_info *cur = current_thread_info();
796 // We assume we are not preemptible here...
797 cur->status |= TS_USEDFPU;
799 // After this, FP Save should be handled by Linux if it
800 // switches to a different task and that task uses FPU
803 inline int ists(void)
805 return read_cr0() & X86_CR0_TS;
808 void palacios_need_fpu(void)
810 // We assume we are not preemptible here...
812 // we have been switched back to from somewhere else...
813 // Do a restore now - this will also do a clts()
814 math_state_restore();
820 * Structure used by the Palacios hypervisor to interface with the host kernel.
822 static struct v3_os_hooks palacios_os_hooks = {
823 .print = palacios_print_scoped,
824 .allocate_pages = palacios_allocate_pages,
825 .free_pages = palacios_free_pages,
826 .malloc = palacios_alloc,
827 .free = palacios_free,
828 .vaddr_to_paddr = palacios_vaddr_to_paddr,
829 .paddr_to_vaddr = palacios_paddr_to_vaddr,
830 .hook_interrupt = palacios_hook_interrupt,
831 .ack_irq = palacios_ack_interrupt,
832 .get_cpu_khz = palacios_get_cpu_khz,
833 .start_kernel_thread = palacios_start_kernel_thread,
834 .yield_cpu = palacios_yield_cpu,
835 .sleep_cpu = palacios_sleep_cpu,
836 .wakeup_cpu = palacios_wakeup_cpu,
837 .mutex_alloc = palacios_mutex_alloc,
838 .mutex_free = palacios_mutex_free,
839 .mutex_lock = palacios_mutex_lock,
840 .mutex_unlock = palacios_mutex_unlock,
841 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
842 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
843 .get_cpu = palacios_get_cpu,
844 .interrupt_cpu = palacios_interrupt_cpu,
845 .call_on_cpu = palacios_xcall,
846 .start_thread_on_cpu = palacios_start_thread_on_cpu,
847 .move_thread_to_cpu = palacios_move_thread_to_cpu,
851 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
852 // Note that this host interface is defined here since it's
853 // intertwined with thread creation...
854 static struct v3_lazy_fpu_iface palacios_fpu_hooks = {
855 .used_fpu = palacios_used_fpu,
856 .need_fpu = palacios_need_fpu
862 int palacios_vmm_init( char *options )
864 int num_cpus = num_online_cpus();
865 char * cpu_mask = NULL;
867 if (cpu_list_len > 0) {
872 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
875 ERROR("Cannot allocate cpu mask\n");
879 memset(cpu_mask, 0, (num_cpus / 8) + 1);
881 for (i = 0; i < cpu_list_len; i++) {
882 if (cpu_list[i] >= num_cpus) {
883 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
887 major = cpu_list[i] / 8;
888 minor = cpu_list[i] % 8;
890 *(cpu_mask + major) |= (0x1 << minor);
894 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
896 if (init_print_buffers()) {
897 ERROR("Cannot initialize print buffers\n");
898 palacios_free(cpu_mask);
902 INFO("palacios_init starting - calling init_v3\n");
904 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
906 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
907 V3_Init_Lazy_FPU(&palacios_fpu_hooks);
915 int palacios_vmm_exit( void ) {
919 INFO("palacios shutdown complete\n");
921 deinit_print_buffers();