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 memory bugs
38 // zero memory after allocation (now applies to valloc and page alloc as well)
39 #define ALLOC_ZERO_MEM 1
40 // pad allocations by this many bytes on both ends of block (heap only)
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;
195 memset(__va(pg_addr),0,num_pages*4096);
198 MEMCHECK_ALLOC_PAGES(pg_addr,num_pages*4096);
205 * Frees a page previously allocated via palacios_allocate_page().
206 * Note that palacios_allocate_page() can allocate multiple pages with
207 * a single call while palacios_free_page() only frees a single page.
210 void palacios_free_pages(void * page_paddr, int num_pages) {
212 ERROR("Ignoring free pages: 0x%p (0x%lx)for %d pages\n", page_paddr, (uintptr_t)page_paddr, num_pages);
216 pg_frees += num_pages;
217 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
218 MEMCHECK_FREE_PAGES(page_paddr,num_pages*4096);
224 palacios_alloc_extended(unsigned int size, unsigned int flags, int node) {
228 // note that modern kernels will respond to a zero byte
229 // kmalloc and return the address 0x10... In Palacios,
230 // we will simply not allow 0 byte allocs at all, of any kind
231 ERROR("ALERT ALERT attempt to kmalloc zero bytes rejected\n");
236 addr = kmalloc(size+2*ALLOC_PAD, flags);
238 addr = kmalloc_node(size+2*ALLOC_PAD, flags, node);
241 if (!addr || IS_ERR(addr)) {
242 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
249 memset(addr,0,size+2*ALLOC_PAD);
252 MEMCHECK_KMALLOC(addr,size+2*ALLOC_PAD);
254 return addr+ALLOC_PAD;
258 palacios_valloc(unsigned int size)
263 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
267 addr = vmalloc(size);
269 if (!addr || IS_ERR(addr)) {
270 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
280 MEMCHECK_VMALLOC(addr,size);
285 void palacios_vfree(void *p)
288 ERROR("Ignoring vfree: 0x%p\n",p);
298 * Allocates 'size' bytes of kernel memory.
299 * Returns the kernel virtual address of the memory allocated.
302 palacios_alloc(unsigned int size) {
304 // It is very important that this test remains since
305 // this function is used extensively throughout palacios and the linux
306 // module, both in places where interrupts are off and where they are on
307 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
308 if (irqs_disabled()) {
309 return palacios_alloc_extended(size,GFP_ATOMIC,-1);
311 return palacios_alloc_extended(size,GFP_KERNEL,-1);
317 * Frees memory that was previously allocated by palacios_alloc().
325 ERROR("Ignoring free : 0x%p\n", addr);
330 kfree(addr-ALLOC_PAD);
331 MEMCHECK_KFREE(addr-ALLOC_PAD);
335 * Converts a kernel virtual address to the corresponding physical address.
338 palacios_vaddr_to_paddr(
342 return (void*) __pa(vaddr);
347 * Converts a physical address to the corresponding kernel virtual address.
350 palacios_paddr_to_vaddr(
358 * Runs a function on the specified CPU.
363 void (*fn)(void *arg),
369 // We set wait to 1, but I'm not sure this is necessary
370 smp_call_function_single(cpu_id, fn, arg, 1);
376 #define MAX_THREAD_NAME 32
378 struct lnx_thread_arg {
379 int (*fn)(void * arg);
381 char name[MAX_THREAD_NAME];
384 static int lnx_thread_target(void * arg) {
385 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
388 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
390 daemonize(thread_info->name);
391 allow_signal(SIGKILL);
394 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
395 // We are a kernel thread that needs FPU save/restore state
396 // vcores definitely need this, all the other threads get it too,
397 // but they just won't use it
398 fpu_alloc(&(current->thread.fpu));
401 ret = thread_info->fn(thread_info->arg);
403 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
405 palacios_free(thread_info);
408 // We rely on do_exit to free the fpu data
409 // since we could get switched at any point until the thread is done...
413 return 0; // should not get here.
417 * Creates a kernel thread.
420 palacios_create_and_start_kernel_thread(
421 int (*fn) (void * arg),
423 char * thread_name) {
425 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
428 ERROR("ALERT ALERT Unable to allocate thread\n");
432 thread_info->fn = fn;
433 thread_info->arg = arg;
434 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
435 thread_info->name[MAX_THREAD_NAME-1] =0;
437 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
442 * Starts a kernel thread on the specified CPU.
445 palacios_create_thread_on_cpu(int cpu_id,
446 int (*fn)(void * arg),
448 char * thread_name ) {
449 struct task_struct * thread = NULL;
450 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
453 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
457 thread_info->fn = fn;
458 thread_info->arg = arg;
459 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
460 thread_info->name[MAX_THREAD_NAME-1] =0;
462 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
464 if (!thread || IS_ERR(thread)) {
465 WARNING("Palacios error creating thread: %s\n", thread_info->name);
466 palacios_free(thread_info);
470 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
471 WARNING("Attempt to start thread on disallowed CPU\n");
472 kthread_stop(thread);
473 palacios_free(thread_info);
481 palacios_start_thread(void * th){
483 struct task_struct * thread = (struct task_struct *)th;
484 wake_up_process(thread);
492 palacios_create_and_start_thread_on_cpu(int cpu_id,
493 int (*fn)(void * arg),
495 char * thread_name ) {
497 void *t = palacios_create_thread_on_cpu(cpu_id, fn, arg, thread_name);
500 palacios_start_thread(t);
509 * Rebind a kernel thread to the specified CPU
510 * The thread will be running on target CPU on return
511 * non-zero return means failure
514 palacios_move_thread_to_cpu(int new_cpu_id,
516 struct task_struct * thread = (struct task_struct *)thread_ptr;
518 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
520 if (thread == NULL) {
525 * Bind to the specified CPU. When this call returns,
526 * the thread should be running on the target CPU.
528 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
533 * Returns the CPU ID that the caller is running on.
536 palacios_get_cpu(void)
539 /* We want to call smp_processor_id()
540 * But this is not safe if kernel preemption is possible
541 * We need to ensure that the palacios threads are bound to a give cpu
544 unsigned int cpu_id = get_cpu();
550 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
553 * This is dependent on the implementation of xcall_reschedule(). Currently
554 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
555 * but instead relies on the return to user space to handle it. Because
556 * palacios is a kernel thread schedule will not be called, which is correct.
557 * If it ever changes to induce side effects, we'll need to figure something
561 #include <asm/apic.h>
564 palacios_interrupt_cpu(
565 struct v3_vm_info * vm,
571 smp_send_reschedule(cpu_id);
573 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
579 * Dispatches an interrupt to Palacios for handling.
582 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
583 struct v3_interrupt intr = {
585 .error = regs->orig_ax,
589 if (irq_to_guest_map[vector]) {
590 v3_deliver_irq(irq_to_guest_map[vector], &intr);
596 * Instructs the kernel to forward the specified IRQ to Palacios.
599 palacios_hook_interrupt(struct v3_vm_info * vm,
600 unsigned int vector ) {
601 INFO("hooking vector %d\n", vector);
603 if (irq_to_guest_map[vector]) {
605 "%s: Interrupt vector %u is already hooked.\n",
611 "%s: Hooking interrupt vector %u to vm %p.\n",
612 __func__, vector, vm);
614 irq_to_guest_map[vector] = vm;
617 * NOTE: Normally PCI devices are supposed to be level sensitive,
618 * but we need them to be edge sensitive so that they are
619 * properly latched by Palacios. Leaving them as level
620 * sensitive would lead to an interrupt storm.
622 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
624 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
626 ERROR("unexpected vector for hooking\n");
634 DEBUG("hooking vector: %d\n", vector);
642 error = request_irq((vector - 32),
643 (void *)palacios_dispatch_interrupt,
645 "interrupt_for_palacios",
649 ERROR("error code for request_irq is %d\n", error);
650 ERROR("request vector %d failed", vector);
661 * Acknowledges an interrupt.
664 palacios_ack_interrupt(
669 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
674 * Returns the CPU frequency in kilohertz.
677 palacios_get_cpu_khz(void)
679 INFO("cpu_khz is %u\n", cpu_khz);
682 INFO("faking cpu_khz to 1000000\n");
691 * Yield the CPU so other host OS tasks can run.
692 * This will return immediately if there is no other thread that is runnable
693 * And there is no real bound on how long it will yield
696 palacios_yield_cpu(void)
703 * Yield the CPU so other host OS tasks can run.
704 * Given now immediately if there is no other thread that is runnable
705 * And there is no real bound on how long it will yield
707 void palacios_sleep_cpu(unsigned int us)
710 set_current_state(TASK_INTERRUPTIBLE);
712 unsigned int uspj = 1000000U/HZ;
713 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
714 schedule_timeout(jiffies);
721 void palacios_wakeup_cpu(void *thread)
723 wake_up_process(thread);
729 * Returns NULL on failure.
732 palacios_mutex_alloc(void)
734 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
737 spin_lock_init(lock);
738 LOCKCHECK_ALLOC(lock);
740 ERROR("ALERT ALERT Unable to allocate lock\n");
747 void palacios_mutex_init(void *mutex)
749 spinlock_t *lock = (spinlock_t*)mutex;
752 spin_lock_init(lock);
753 LOCKCHECK_ALLOC(lock);
757 void palacios_mutex_deinit(void *mutex)
759 spinlock_t *lock = (spinlock_t*)mutex;
762 // no actual spin_lock_deinit on linux
763 // our purpose here is to drive the lock checker
764 LOCKCHECK_FREE(lock);
773 palacios_mutex_free(void * mutex) {
774 palacios_free(mutex);
775 LOCKCHECK_FREE(mutex);
782 palacios_mutex_lock(void * mutex, int must_spin) {
784 LOCKCHECK_LOCK_PRE(mutex);
785 spin_lock((spinlock_t *)mutex);
786 LOCKCHECK_LOCK_POST(mutex);
791 * Locks a mutex, disabling interrupts on this core
794 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
798 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
799 spin_lock_irqsave((spinlock_t *)mutex,flags);
800 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
802 return (void *)flags;
810 palacios_mutex_unlock(
814 LOCKCHECK_UNLOCK_PRE(mutex);
815 spin_unlock((spinlock_t *)mutex);
816 LOCKCHECK_UNLOCK_POST(mutex);
821 * Unlocks a mutex and restores previous interrupt state on this core
824 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
826 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
827 // This is correct, flags is opaque
828 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
829 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
832 void palacios_used_fpu(void)
834 struct thread_info *cur = current_thread_info();
836 // We assume we are not preemptible here...
837 cur->status |= TS_USEDFPU;
839 // After this, FP Save should be handled by Linux if it
840 // switches to a different task and that task uses FPU
843 inline int ists(void)
845 return read_cr0() & X86_CR0_TS;
848 void palacios_need_fpu(void)
850 // We assume we are not preemptible here...
852 // we have been switched back to from somewhere else...
853 // Do a restore now - this will also do a clts()
854 math_state_restore();
860 * Structure used by the Palacios hypervisor to interface with the host kernel.
862 static struct v3_os_hooks palacios_os_hooks = {
863 .print = palacios_print_scoped,
864 .allocate_pages = palacios_allocate_pages,
865 .free_pages = palacios_free_pages,
866 .malloc = palacios_alloc,
867 .free = palacios_free,
868 .vaddr_to_paddr = palacios_vaddr_to_paddr,
869 .paddr_to_vaddr = palacios_paddr_to_vaddr,
870 .hook_interrupt = palacios_hook_interrupt,
871 .ack_irq = palacios_ack_interrupt,
872 .get_cpu_khz = palacios_get_cpu_khz,
873 .start_kernel_thread = palacios_create_and_start_kernel_thread,
874 .yield_cpu = palacios_yield_cpu,
875 .sleep_cpu = palacios_sleep_cpu,
876 .wakeup_cpu = palacios_wakeup_cpu,
877 .mutex_alloc = palacios_mutex_alloc,
878 .mutex_free = palacios_mutex_free,
879 .mutex_lock = palacios_mutex_lock,
880 .mutex_unlock = palacios_mutex_unlock,
881 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
882 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
883 .get_cpu = palacios_get_cpu,
884 .interrupt_cpu = palacios_interrupt_cpu,
885 .call_on_cpu = palacios_xcall,
886 .create_thread_on_cpu = palacios_create_thread_on_cpu,
887 .start_thread = palacios_start_thread,
888 .move_thread_to_cpu = palacios_move_thread_to_cpu,
892 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
893 // Note that this host interface is defined here since it's
894 // intertwined with thread creation...
895 static struct v3_lazy_fpu_iface palacios_fpu_hooks = {
896 .used_fpu = palacios_used_fpu,
897 .need_fpu = palacios_need_fpu
903 int palacios_vmm_init( char *options )
905 int num_cpus = num_online_cpus();
906 char * cpu_mask = NULL;
908 if (cpu_list_len > 0) {
913 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
916 ERROR("Cannot allocate cpu mask\n");
920 memset(cpu_mask, 0, (num_cpus / 8) + 1);
922 for (i = 0; i < cpu_list_len; i++) {
923 if (cpu_list[i] >= num_cpus) {
924 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
928 major = cpu_list[i] / 8;
929 minor = cpu_list[i] % 8;
931 *(cpu_mask + major) |= (0x1 << minor);
935 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
937 if (init_print_buffers()) {
938 ERROR("Cannot initialize print buffers\n");
939 palacios_free(cpu_mask);
943 INFO("palacios_init starting - calling init_v3\n");
945 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
947 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
948 V3_Init_Lazy_FPU(&palacios_fpu_hooks);
956 int palacios_vmm_exit( void ) {
960 INFO("palacios shutdown complete\n");
962 deinit_print_buffers();