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>
11 #include <asm/thread_info.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/kthread.h>
16 #include <asm/uaccess.h>
17 #include <linux/smp.h>
18 #include <linux/vmalloc.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_host_events.h>
25 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
26 #include <interfaces/vmm_lazy_fpu.h>
34 #include "lockcheck.h"
38 // The following can be used to track memory bugs
39 // zero memory after allocation (now applies to valloc and page alloc as well)
40 #define ALLOC_ZERO_MEM 1
41 // pad allocations by this many bytes on both ends of block (heap only)
52 static struct v3_vm_info * irq_to_guest_map[256];
55 extern unsigned int cpu_khz;
57 extern int cpu_list[NR_CPUS];
58 extern int cpu_list_len;
61 static char *print_buffer[NR_CPUS];
63 static void deinit_print_buffers(void)
67 for (i=0;i<NR_CPUS;i++) {
68 if (print_buffer[i]) {
69 palacios_free(print_buffer[i]);
75 static int init_print_buffers(void)
79 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
81 #if !V3_PRINTK_OLD_STYLE_OUTPUT
83 for (i=0;i<NR_CPUS;i++) {
84 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
85 if (!print_buffer[i]) {
86 ERROR("Cannot allocate print buffer for cpu %d\n",i);
87 deinit_print_buffers();
90 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
100 * Prints a message to the console.
102 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...) {
104 #if V3_PRINTK_OLD_STYLE_OUTPUT
118 unsigned int cpu = palacios_get_cpu();
119 struct v3_guest *guest = (struct v3_guest *)vm;
121 buf = print_buffer[cpu];
124 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
129 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
132 #if V3_PRINTK_CHECK_7BIT
136 for (i=0;i<strlen(buf);i++) {
143 printk(KERN_INFO "palacios (pcore %u): ALERT ALERT 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
150 printk(KERN_INFO "palacios (pcore %u vm %s vcore %u): %s",
156 printk(KERN_INFO "palacios (pcore %u vm %s): %s",
162 printk(KERN_INFO "palacios (pcore %u): %s",
175 * Allocates a contiguous region of pages of the requested size.
176 * Returns the physical address of the first page in the region.
178 void *palacios_allocate_pages(int num_pages, unsigned int alignment, int node_id, int (*filter_func)(void *paddr, void *filter_state), void *filter_state) {
179 void * pg_addr = NULL;
182 ERROR("ALERT ALERT Attempt to allocate zero or fewer pages (%d pages, alignment %d, node %d, filter_func %p, filter_state %p)\n",num_pages, alignment, node_id, filter_func, filter_state);
186 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment, node_id, filter_func, filter_state);
189 ERROR("ALERT ALERT Page allocation has FAILED Warning (%d pages, alignment %d, node %d, filter_func %p, filter_state %p)\n",num_pages, alignment, node_id, filter_func, filter_state);
193 pg_allocs += num_pages;
196 memset(__va(pg_addr),0,num_pages*4096);
199 MEMCHECK_ALLOC_PAGES(pg_addr,num_pages*4096);
206 * Frees a page previously allocated via palacios_allocate_page().
207 * Note that palacios_allocate_page() can allocate multiple pages with
208 * a single call while palacios_free_page() only frees a single page.
211 void palacios_free_pages(void * page_paddr, int num_pages) {
213 ERROR("Ignoring free pages: 0x%p (0x%lx)for %d pages\n", page_paddr, (uintptr_t)page_paddr, num_pages);
217 pg_frees += num_pages;
218 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
219 MEMCHECK_FREE_PAGES(page_paddr,num_pages*4096);
225 palacios_alloc_extended(unsigned int size, unsigned int flags, int node) {
229 // note that modern kernels will respond to a zero byte
230 // kmalloc and return the address 0x10... In Palacios,
231 // we will simply not allow 0 byte allocs at all, of any kind
232 ERROR("ALERT ALERT attempt to kmalloc zero bytes rejected\n");
237 addr = kmalloc(size+2*ALLOC_PAD, flags);
239 addr = kmalloc_node(size+2*ALLOC_PAD, flags, node);
242 if (!addr || IS_ERR(addr)) {
243 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
250 memset(addr,0,size+2*ALLOC_PAD);
253 MEMCHECK_KMALLOC(addr,size+2*ALLOC_PAD);
255 return addr+ALLOC_PAD;
259 palacios_valloc(unsigned int size)
264 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
268 addr = vmalloc(size);
270 if (!addr || IS_ERR(addr)) {
271 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
281 MEMCHECK_VMALLOC(addr,size);
286 void palacios_vfree(void *p)
289 ERROR("Ignoring vfree: 0x%p\n",p);
299 * Allocates 'size' bytes of kernel memory.
300 * Returns the kernel virtual address of the memory allocated.
303 palacios_alloc(unsigned int size) {
305 // It is very important that this test remains since
306 // this function is used extensively throughout palacios and the linux
307 // module, both in places where interrupts are off and where they are on
308 // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK
309 if (irqs_disabled() || in_atomic()) {
310 return palacios_alloc_extended(size,GFP_ATOMIC,-1);
312 return palacios_alloc_extended(size,GFP_KERNEL,-1);
318 * Frees memory that was previously allocated by palacios_alloc().
326 ERROR("Ignoring free : 0x%p\n", addr);
331 kfree(addr-ALLOC_PAD);
332 MEMCHECK_KFREE(addr-ALLOC_PAD);
336 * Converts a kernel virtual address to the corresponding physical address.
339 palacios_vaddr_to_paddr(
343 return (void*) __pa(vaddr);
348 * Converts a physical address to the corresponding kernel virtual address.
351 palacios_paddr_to_vaddr(
359 * Runs a function on the specified CPU.
364 void (*fn)(void *arg),
370 // We set wait to 1, but I'm not sure this is necessary
371 smp_call_function_single(cpu_id, fn, arg, 1);
377 #define MAX_THREAD_NAME 32
379 struct lnx_thread_arg {
380 int (*fn)(void * arg);
382 char name[MAX_THREAD_NAME];
385 static int lnx_thread_target(void * arg) {
386 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
389 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
391 daemonize(thread_info->name);
392 allow_signal(SIGKILL);
395 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
396 // We are a kernel thread that needs FPU save/restore state
397 // vcores definitely need this, all the other threads get it too,
398 // but they just won't use it
399 fpu_alloc(&(current->thread.fpu));
402 ret = thread_info->fn(thread_info->arg);
404 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
406 palacios_free(thread_info);
409 // We rely on do_exit to free the fpu data
410 // since we could get switched at any point until the thread is done...
414 return 0; // should not get here.
418 * Creates a kernel thread.
421 palacios_create_and_start_kernel_thread(
422 int (*fn) (void * arg),
424 char * thread_name) {
426 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
429 ERROR("ALERT ALERT Unable to allocate thread\n");
433 thread_info->fn = fn;
434 thread_info->arg = arg;
435 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
436 thread_info->name[MAX_THREAD_NAME-1] =0;
438 return kthread_run( lnx_thread_target, thread_info, thread_info->name );
443 * Starts a kernel thread on the specified CPU.
446 palacios_create_thread_on_cpu(int cpu_id,
447 int (*fn)(void * arg),
449 char * thread_name ) {
450 struct task_struct * thread = NULL;
451 struct lnx_thread_arg * thread_info = palacios_alloc(sizeof(struct lnx_thread_arg));
454 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
458 thread_info->fn = fn;
459 thread_info->arg = arg;
460 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
461 thread_info->name[MAX_THREAD_NAME-1] =0;
463 thread = kthread_create( lnx_thread_target, thread_info, thread_info->name );
465 if (!thread || IS_ERR(thread)) {
466 WARNING("Palacios error creating thread: %s\n", thread_info->name);
467 palacios_free(thread_info);
471 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
472 WARNING("Attempt to start thread on disallowed CPU\n");
473 kthread_stop(thread);
474 palacios_free(thread_info);
482 palacios_start_thread(void * th){
484 struct task_struct * thread = (struct task_struct *)th;
485 wake_up_process(thread);
493 palacios_create_and_start_thread_on_cpu(int cpu_id,
494 int (*fn)(void * arg),
496 char * thread_name ) {
498 void *t = palacios_create_thread_on_cpu(cpu_id, fn, arg, thread_name);
501 palacios_start_thread(t);
510 * Rebind a kernel thread to the specified CPU
511 * The thread will be running on target CPU on return
512 * non-zero return means failure
515 palacios_move_thread_to_cpu(int new_cpu_id,
517 struct task_struct * thread = (struct task_struct *)thread_ptr;
519 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
521 if (thread == NULL) {
526 * Bind to the specified CPU. When this call returns,
527 * the thread should be running on the target CPU.
529 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
534 * Returns the CPU ID that the caller is running on.
537 palacios_get_cpu(void)
540 /* We want to call smp_processor_id()
541 * But this is not safe if kernel preemption is possible
542 * We need to ensure that the palacios threads are bound to a give cpu
545 unsigned int cpu_id = get_cpu();
551 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
554 * This is dependent on the implementation of xcall_reschedule(). Currently
555 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
556 * but instead relies on the return to user space to handle it. Because
557 * palacios is a kernel thread schedule will not be called, which is correct.
558 * If it ever changes to induce side effects, we'll need to figure something
562 #include <asm/apic.h>
565 palacios_interrupt_cpu(
566 struct v3_vm_info * vm,
572 smp_send_reschedule(cpu_id);
574 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
580 * Dispatches an interrupt to Palacios for handling.
583 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
584 struct v3_interrupt intr = {
586 .error = regs->orig_ax,
590 if (irq_to_guest_map[vector]) {
591 v3_deliver_irq(irq_to_guest_map[vector], &intr);
597 * Instructs the kernel to forward the specified IRQ to Palacios.
600 palacios_hook_interrupt(struct v3_vm_info * vm,
601 unsigned int vector ) {
602 INFO("hooking vector %d\n", vector);
604 if (irq_to_guest_map[vector]) {
606 "%s: Interrupt vector %u is already hooked.\n",
612 "%s: Hooking interrupt vector %u to vm %p.\n",
613 __func__, vector, vm);
615 irq_to_guest_map[vector] = vm;
618 * NOTE: Normally PCI devices are supposed to be level sensitive,
619 * but we need them to be edge sensitive so that they are
620 * properly latched by Palacios. Leaving them as level
621 * sensitive would lead to an interrupt storm.
623 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
625 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
627 ERROR("unexpected vector for hooking\n");
635 DEBUG("hooking vector: %d\n", vector);
643 error = request_irq((vector - 32),
644 (void *)palacios_dispatch_interrupt,
646 "interrupt_for_palacios",
650 ERROR("error code for request_irq is %d\n", error);
651 ERROR("request vector %d failed", vector);
662 * Acknowledges an interrupt.
665 palacios_ack_interrupt(
670 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
675 * Returns the CPU frequency in kilohertz.
678 palacios_get_cpu_khz(void)
680 INFO("cpu_khz is %u\n", cpu_khz);
683 INFO("faking cpu_khz to 1000000\n");
692 * Yield the CPU so other host OS tasks can run.
693 * This will return immediately if there is no other thread that is runnable
694 * And there is no real bound on how long it will yield
697 palacios_yield_cpu(void)
704 * Yield the CPU so other host OS tasks can run.
705 * Given now immediately if there is no other thread that is runnable
706 * And there is no real bound on how long it will yield
708 void palacios_sleep_cpu(unsigned int us)
711 set_current_state(TASK_INTERRUPTIBLE);
713 unsigned int uspj = 1000000U/HZ;
714 unsigned int jiffies = us/uspj + ((us%uspj) !=0); // ceiling
715 schedule_timeout(jiffies);
722 void palacios_wakeup_cpu(void *thread)
724 wake_up_process(thread);
730 * Returns NULL on failure.
733 palacios_mutex_alloc(void)
735 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
738 spin_lock_init(lock);
739 LOCKCHECK_ALLOC(lock);
741 ERROR("ALERT ALERT Unable to allocate lock\n");
748 void palacios_mutex_init(void *mutex)
750 spinlock_t *lock = (spinlock_t*)mutex;
753 spin_lock_init(lock);
754 LOCKCHECK_ALLOC(lock);
758 void palacios_mutex_deinit(void *mutex)
760 spinlock_t *lock = (spinlock_t*)mutex;
763 // no actual spin_lock_deinit on linux
764 // our purpose here is to drive the lock checker
765 LOCKCHECK_FREE(lock);
774 palacios_mutex_free(void * mutex) {
775 palacios_free(mutex);
776 LOCKCHECK_FREE(mutex);
783 palacios_mutex_lock(void * mutex, int must_spin) {
785 LOCKCHECK_LOCK_PRE(mutex);
786 spin_lock((spinlock_t *)mutex);
787 LOCKCHECK_LOCK_POST(mutex);
792 * Locks a mutex, disabling interrupts on this core
795 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
799 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
800 spin_lock_irqsave((spinlock_t *)mutex,flags);
801 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
803 return (void *)flags;
811 palacios_mutex_unlock(
815 LOCKCHECK_UNLOCK_PRE(mutex);
816 spin_unlock((spinlock_t *)mutex);
817 LOCKCHECK_UNLOCK_POST(mutex);
822 * Unlocks a mutex and restores previous interrupt state on this core
825 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
827 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
828 // This is correct, flags is opaque
829 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
830 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
833 void palacios_used_fpu(void)
835 // We assume we are not preemptible here...
837 struct task_struct *tsk = current;
838 tsk->thread.fpu.has_fpu = 1;
840 struct thread_info *cur = current_thread_info();
841 cur->status |= TS_USEDFPU;
844 // After this, FP Save should be handled by Linux if it
845 // switches to a different task and that task uses FPU
848 inline int ists(void)
850 return read_cr0() & X86_CR0_TS;
853 void palacios_need_fpu(void)
855 // We assume we are not preemptible here...
857 // we have been switched back to from somewhere else...
858 // Do a restore now - this will also do a clts()
859 math_state_restore();
865 * Structure used by the Palacios hypervisor to interface with the host kernel.
867 static struct v3_os_hooks palacios_os_hooks = {
868 .print = palacios_print_scoped,
869 .allocate_pages = palacios_allocate_pages,
870 .free_pages = palacios_free_pages,
871 .vmalloc = palacios_valloc,
872 .vfree = palacios_vfree,
873 .malloc = palacios_alloc,
874 .free = palacios_free,
875 .vaddr_to_paddr = palacios_vaddr_to_paddr,
876 .paddr_to_vaddr = palacios_paddr_to_vaddr,
877 .hook_interrupt = palacios_hook_interrupt,
878 .ack_irq = palacios_ack_interrupt,
879 .get_cpu_khz = palacios_get_cpu_khz,
880 .start_kernel_thread = palacios_create_and_start_kernel_thread,
881 .yield_cpu = palacios_yield_cpu,
882 .sleep_cpu = palacios_sleep_cpu,
883 .wakeup_cpu = palacios_wakeup_cpu,
884 .mutex_alloc = palacios_mutex_alloc,
885 .mutex_free = palacios_mutex_free,
886 .mutex_lock = palacios_mutex_lock,
887 .mutex_unlock = palacios_mutex_unlock,
888 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
889 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
890 .get_cpu = palacios_get_cpu,
891 .interrupt_cpu = palacios_interrupt_cpu,
892 .call_on_cpu = palacios_xcall,
893 .create_thread_on_cpu = palacios_create_thread_on_cpu,
894 .start_thread = palacios_start_thread,
895 .move_thread_to_cpu = palacios_move_thread_to_cpu,
899 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
900 // Note that this host interface is defined here since it's
901 // intertwined with thread creation...
902 static struct v3_lazy_fpu_iface palacios_fpu_hooks = {
903 .used_fpu = palacios_used_fpu,
904 .need_fpu = palacios_need_fpu
910 int palacios_vmm_init( char *options )
912 int num_cpus = num_online_cpus();
913 char * cpu_mask = NULL;
915 if (cpu_list_len > 0) {
920 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
923 ERROR("Cannot allocate cpu mask\n");
927 memset(cpu_mask, 0, (num_cpus / 8) + 1);
929 for (i = 0; i < cpu_list_len; i++) {
930 if (cpu_list[i] >= num_cpus) {
931 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
935 major = cpu_list[i] / 8;
936 minor = cpu_list[i] % 8;
938 *(cpu_mask + major) |= (0x1 << minor);
942 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
944 if (init_print_buffers()) {
945 ERROR("Cannot initialize print buffers\n");
946 palacios_free(cpu_mask);
950 INFO("palacios_init starting - calling init_v3\n");
952 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
954 #ifdef V3_CONFIG_HOST_LAZY_FPU_SWITCH
955 V3_Init_Lazy_FPU(&palacios_fpu_hooks);
963 int palacios_vmm_exit( void ) {
967 INFO("palacios shutdown complete\n");
969 deinit_print_buffers();