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>
18 #include <palacios/vmm.h>
19 #include <palacios/vmm_host_events.h>
34 static struct v3_vm_info * irq_to_guest_map[256];
37 extern unsigned int cpu_khz;
39 extern int cpu_list[NR_CPUS];
40 extern int cpu_list_len;
43 static char *print_buffer[NR_CPUS];
45 static void deinit_print_buffers(void)
49 for (i=0;i<NR_CPUS;i++) {
50 if (print_buffer[i]) {
51 kfree(print_buffer[i]);
57 static int init_print_buffers(void)
61 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
63 #if !V3_PRINTK_OLD_STYLE_OUTPUT
65 for (i=0;i<NR_CPUS;i++) {
66 print_buffer[i] = kmalloc(V3_PRINTK_BUF_SIZE,GFP_KERNEL);
67 if (!print_buffer[i]) {
68 ERROR("Cannot allocate print buffer for cpu %d\n",i);
69 deinit_print_buffers();
72 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
82 * Prints a message to the console.
84 void palacios_print(const char *fmt, ...) {
86 #if V3_PRINTK_OLD_STYLE_OUTPUT
100 unsigned int cpu = palacios_get_cpu();
102 buf = print_buffer[cpu];
105 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
110 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
113 #if V3_PRINTK_CHECK_7BIT
117 for (i=0;i<strlen(buf);i++) {
124 printk(KERN_INFO "palacios (pcore %u): ALERT - 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
129 printk(KERN_INFO "palacios (pcore %u): %s",cpu,buf);
140 * Allocates a contiguous region of pages of the requested size.
141 * Returns the physical address of the first page in the region.
143 void *palacios_allocate_pages(int num_pages, unsigned int alignment) {
144 void * pg_addr = NULL;
146 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
147 pg_allocs += num_pages;
154 * Frees a page previously allocated via palacios_allocate_page().
155 * Note that palacios_allocate_page() can allocate multiple pages with
156 * a single call while palacios_free_page() only frees a single page.
159 void palacios_free_pages(void * page_paddr, int num_pages) {
160 pg_frees += num_pages;
161 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
166 * Allocates 'size' bytes of kernel memory.
167 * Returns the kernel virtual address of the memory allocated.
170 palacios_alloc(unsigned int size) {
173 if (irqs_disabled()) {
174 addr = kmalloc(size, GFP_ATOMIC);
176 addr = kmalloc(size, GFP_KERNEL);
187 * Frees memory that was previously allocated by palacios_alloc().
200 * Converts a kernel virtual address to the corresponding physical address.
203 palacios_vaddr_to_paddr(
207 return (void*) __pa(vaddr);
212 * Converts a physical address to the corresponding kernel virtual address.
215 palacios_paddr_to_vaddr(
223 * Runs a function on the specified CPU.
228 void (*fn)(void *arg),
234 // We set wait to 1, but I'm not sure this is necessary
235 smp_call_function_single(cpu_id, fn, arg, 1);
240 struct lnx_thread_arg {
241 int (*fn)(void * arg);
246 static int lnx_thread_target(void * arg) {
247 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
250 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
252 daemonize(thread_info->name);
253 allow_signal(SIGKILL);
257 ret = thread_info->fn(thread_info->arg);
260 INFO("Palacios Thread (%s) EXITING\n", thread_info->name);
267 return 0; // should not get here.
271 * Creates a kernel thread.
274 palacios_start_kernel_thread(
275 int (*fn) (void * arg),
277 char * thread_name) {
279 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
281 thread_info->fn = fn;
282 thread_info->arg = arg;
283 thread_info->name = thread_name;
285 return kthread_run( lnx_thread_target, thread_info, thread_name );
290 * Starts a kernel thread on the specified CPU.
293 palacios_start_thread_on_cpu(int cpu_id,
294 int (*fn)(void * arg),
296 char * thread_name ) {
297 struct task_struct * thread = NULL;
298 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
300 thread_info->fn = fn;
301 thread_info->arg = arg;
302 thread_info->name = thread_name;
305 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
307 if (IS_ERR(thread)) {
308 WARNING("Palacios error creating thread: %s\n", thread_name);
312 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
313 kthread_stop(thread);
317 wake_up_process(thread);
324 * Rebind a kernel thread to the specified CPU
325 * The thread will be running on target CPU on return
326 * non-zero return means failure
329 palacios_move_thread_to_cpu(int new_cpu_id,
331 struct task_struct * thread = (struct task_struct *)thread_ptr;
333 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
335 if (thread == NULL) {
340 * Bind to the specified CPU. When this call returns,
341 * the thread should be running on the target CPU.
343 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
348 * Returns the CPU ID that the caller is running on.
351 palacios_get_cpu(void)
354 /* We want to call smp_processor_id()
355 * But this is not safe if kernel preemption is possible
356 * We need to ensure that the palacios threads are bound to a give cpu
359 unsigned int cpu_id = get_cpu();
365 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
368 * This is dependent on the implementation of xcall_reschedule(). Currently
369 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
370 * but instead relies on the return to user space to handle it. Because
371 * palacios is a kernel thread schedule will not be called, which is correct.
372 * If it ever changes to induce side effects, we'll need to figure something
376 #include <asm/apic.h>
379 palacios_interrupt_cpu(
380 struct v3_vm_info * vm,
386 smp_send_reschedule(cpu_id);
388 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
394 * Dispatches an interrupt to Palacios for handling.
397 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
398 struct v3_interrupt intr = {
400 .error = regs->orig_ax,
404 if (irq_to_guest_map[vector]) {
405 v3_deliver_irq(irq_to_guest_map[vector], &intr);
411 * Instructs the kernel to forward the specified IRQ to Palacios.
414 palacios_hook_interrupt(struct v3_vm_info * vm,
415 unsigned int vector ) {
416 INFO("hooking vector %d\n", vector);
418 if (irq_to_guest_map[vector]) {
420 "%s: Interrupt vector %u is already hooked.\n",
426 "%s: Hooking interrupt vector %u to vm %p.\n",
427 __func__, vector, vm);
429 irq_to_guest_map[vector] = vm;
432 * NOTE: Normally PCI devices are supposed to be level sensitive,
433 * but we need them to be edge sensitive so that they are
434 * properly latched by Palacios. Leaving them as level
435 * sensitive would lead to an interrupt storm.
437 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
439 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
441 panic("unexpected vector for hooking\n");
448 DEBUG("hooking vector: %d\n", vector);
456 error = request_irq((vector - 32),
457 (void *)palacios_dispatch_interrupt,
459 "interrupt_for_palacios",
463 ERROR("error code for request_irq is %d\n", error);
464 panic("request vector %d failed", vector);
474 * Acknowledges an interrupt.
477 palacios_ack_interrupt(
482 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
487 * Returns the CPU frequency in kilohertz.
490 palacios_get_cpu_khz(void)
492 INFO("cpu_khz is %u\n", cpu_khz);
495 INFO("faking cpu_khz to 1000000\n");
504 * Yield the CPU so other host OS tasks can run.
505 * This will return immediately if there is no other thread that is runnable
506 * And there is no real bound on how long it will yield
509 palacios_yield_cpu(void)
516 * Yield the CPU so other host OS tasks can run.
517 * Given now immediately if there is no other thread that is runnable
518 * And there is no real bound on how long it will yield
520 void palacios_yield_cpu_timed(unsigned int us)
522 unsigned int jiffies = 1000000U * HZ / us;
524 set_current_state(TASK_INTERRUPTIBLE);
526 schedule_timeout(jiffies);
533 * Returns NULL on failure.
536 palacios_mutex_alloc(void)
538 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
541 spin_lock_init(lock);
551 palacios_mutex_free(void * mutex) {
559 palacios_mutex_lock(void * mutex, int must_spin) {
560 spin_lock((spinlock_t *)mutex);
565 * Locks a mutex, disabling interrupts on this core
568 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
572 spin_lock_irqsave((spinlock_t *)mutex,flags);
574 return (void *)flags;
582 palacios_mutex_unlock(
586 spin_unlock((spinlock_t *)mutex);
594 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
596 // This is correct, flags is opaque
597 spin_unlock_irqrestore((spinlock_t *)mutex,(unsigned long)flags);
601 * Structure used by the Palacios hypervisor to interface with the host kernel.
603 static struct v3_os_hooks palacios_os_hooks = {
604 .print = palacios_print,
605 .allocate_pages = palacios_allocate_pages,
606 .free_pages = palacios_free_pages,
607 .malloc = palacios_alloc,
608 .free = palacios_free,
609 .vaddr_to_paddr = palacios_vaddr_to_paddr,
610 .paddr_to_vaddr = palacios_paddr_to_vaddr,
611 .hook_interrupt = palacios_hook_interrupt,
612 .ack_irq = palacios_ack_interrupt,
613 .get_cpu_khz = palacios_get_cpu_khz,
614 .start_kernel_thread = palacios_start_kernel_thread,
615 .yield_cpu = palacios_yield_cpu,
616 .yield_cpu_timed = palacios_yield_cpu_timed,
617 .mutex_alloc = palacios_mutex_alloc,
618 .mutex_free = palacios_mutex_free,
619 .mutex_lock = palacios_mutex_lock,
620 .mutex_unlock = palacios_mutex_unlock,
621 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
622 .mutex_unlock_irqrestore= palacios_mutex_unlock_irqrestore,
623 .get_cpu = palacios_get_cpu,
624 .interrupt_cpu = palacios_interrupt_cpu,
625 .call_on_cpu = palacios_xcall,
626 .start_thread_on_cpu = palacios_start_thread_on_cpu,
627 .move_thread_to_cpu = palacios_move_thread_to_cpu,
633 int palacios_vmm_init( void )
635 int num_cpus = num_online_cpus();
636 char * cpu_mask = NULL;
638 if (cpu_list_len > 0) {
643 cpu_mask = kmalloc((num_cpus / 8) + 1, GFP_KERNEL);
646 ERROR("Cannot allocate cpu mask\n");
650 memset(cpu_mask, 0, (num_cpus / 8) + 1);
652 for (i = 0; i < cpu_list_len; i++) {
653 if (cpu_list[i] >= num_cpus) {
654 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
658 major = cpu_list[i] / 8;
659 minor = cpu_list[i] % 8;
661 *(cpu_mask + major) |= (0x1 << minor);
665 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
667 if (init_print_buffers()) {
668 ERROR("Cannot initialize print buffers\n");
673 INFO("palacios_init starting - calling init_v3\n");
675 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus);
682 int palacios_vmm_exit( void ) {
686 INFO("palacios shutdown complete\n");
688 deinit_print_buffers();