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)
48 for (i=0;i<NR_CPUS;i++) {
49 if (print_buffer[i]) {
50 kfree(print_buffer[i]);
56 static int init_print_buffers(int num_cpus)
60 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
62 #if !V3_PRINTK_OLD_STYLE_OUTPUT
64 for (i=0;i<num_cpus;i++) {
65 print_buffer[i] = kmalloc(V3_PRINTK_BUF_SIZE,GFP_KERNEL);
66 if (!print_buffer[i]) {
67 ERROR("Cannot allocate print buffer for cpu %d\n",i);
68 deinit_print_buffers();
71 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
81 * Prints a message to the console.
83 void palacios_print(const char *fmt, ...) {
85 #if V3_PRINTK_OLD_STYLE_OUTPUT
99 unsigned int cpu = palacios_get_cpu();
101 buf = print_buffer[cpu];
104 printk(KERN_INFO "palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
109 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
112 #if V3_PRINTK_CHECK_7BIT
116 for (i=0;i<strlen(buf);i++) {
123 printk(KERN_INFO "palacios (pcore %u): ALERT - 8 BIT CHAR (c=%d) DETECTED\n", cpu,c);
128 printk(KERN_INFO "palacios (pcore %u): %s",cpu,buf);
139 * Allocates a contiguous region of pages of the requested size.
140 * Returns the physical address of the first page in the region.
142 static void * palacios_allocate_pages(int num_pages, unsigned int alignment) {
143 void * pg_addr = NULL;
145 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
146 pg_allocs += num_pages;
153 * Frees a page previously allocated via palacios_allocate_page().
154 * Note that palacios_allocate_page() can allocate multiple pages with
155 * a single call while palacios_free_page() only frees a single page.
158 static void palacios_free_pages(void * page_paddr, int num_pages) {
159 pg_frees += num_pages;
160 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
165 * Allocates 'size' bytes of kernel memory.
166 * Returns the kernel virtual address of the memory allocated.
169 palacios_alloc(unsigned int size) {
172 if (irqs_disabled()) {
173 addr = kmalloc(size, GFP_ATOMIC);
175 addr = kmalloc(size, GFP_KERNEL);
184 * Frees memory that was previously allocated by palacios_alloc().
197 * Converts a kernel virtual address to the corresponding physical address.
200 palacios_vaddr_to_paddr(
204 return (void*) __pa(vaddr);
209 * Converts a physical address to the corresponding kernel virtual address.
212 palacios_paddr_to_vaddr(
220 * Runs a function on the specified CPU.
223 // For now, do call only on local CPU
227 void (*fn)(void *arg),
233 // We set wait to 1, but I'm not sure this is necessary
234 smp_call_function_single(cpu_id, fn, arg, 1);
239 struct lnx_thread_arg {
240 int (*fn)(void * arg);
245 static int lnx_thread_target(void * arg) {
246 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
249 INFO("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
251 daemonize(thread_info->name);
252 allow_signal(SIGKILL);
256 ret = thread_info->fn(thread_info->arg);
259 INFO("Palacios Thread (%s) EXITTING\n", thread_info->name);
266 return 0; // should not get here.
270 * Creates a kernel thread.
273 palacios_start_kernel_thread(
274 int (*fn) (void * arg),
276 char * thread_name) {
278 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
280 thread_info->fn = fn;
281 thread_info->arg = arg;
282 thread_info->name = thread_name;
284 return kthread_run( lnx_thread_target, thread_info, thread_name );
289 * Starts a kernel thread on the specified CPU.
292 palacios_start_thread_on_cpu(int cpu_id,
293 int (*fn)(void * arg),
295 char * thread_name ) {
296 struct task_struct * thread = NULL;
297 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
299 thread_info->fn = fn;
300 thread_info->arg = arg;
301 thread_info->name = thread_name;
304 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
306 if (IS_ERR(thread)) {
307 WARNING("Palacios error creating thread: %s\n", thread_name);
311 if (set_cpus_allowed_ptr(thread, cpumask_of(cpu_id)) != 0) {
312 kthread_stop(thread);
316 wake_up_process(thread);
323 * Rebind a kernel thread to the specified CPU
324 * The thread will be running on target CPU on return
325 * non-zero return means failure
328 palacios_move_thread_to_cpu(int new_cpu_id,
330 struct task_struct * thread = (struct task_struct *)thread_ptr;
332 INFO("Moving thread (%p) to cpu %d\n", thread, new_cpu_id);
334 if (thread == NULL) {
339 * Bind to the specified CPU. When this call returns,
340 * the thread should be running on the target CPU.
342 return set_cpus_allowed_ptr(thread, cpumask_of(new_cpu_id));
347 * Returns the CPU ID that the caller is running on.
350 palacios_get_cpu(void)
353 /* We want to call smp_processor_id()
354 * But this is not safe if kernel preemption is possible
355 * We need to ensure that the palacios threads are bound to a give cpu
358 unsigned int cpu_id = get_cpu();
364 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
367 * This is dependent on the implementation of xcall_reschedule(). Currently
368 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
369 * but instead relies on the return to user space to handle it. Because
370 * palacios is a kernel thread schedule will not be called, which is correct.
371 * If it ever changes to induce side effects, we'll need to figure something
375 #include <asm/apic.h>
378 palacios_interrupt_cpu(
379 struct v3_vm_info * vm,
385 smp_send_reschedule(cpu_id);
387 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
393 * Dispatches an interrupt to Palacios for handling.
396 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
397 struct v3_interrupt intr = {
399 .error = regs->orig_ax,
403 if (irq_to_guest_map[vector]) {
404 v3_deliver_irq(irq_to_guest_map[vector], &intr);
410 * Instructs the kernel to forward the specified IRQ to Palacios.
413 palacios_hook_interrupt(struct v3_vm_info * vm,
414 unsigned int vector ) {
415 INFO("hooking vector %d\n", vector);
417 if (irq_to_guest_map[vector]) {
419 "%s: Interrupt vector %u is already hooked.\n",
425 "%s: Hooking interrupt vector %u to vm %p.\n",
426 __func__, vector, vm);
428 irq_to_guest_map[vector] = vm;
431 * NOTE: Normally PCI devices are supposed to be level sensitive,
432 * but we need them to be edge sensitive so that they are
433 * properly latched by Palacios. Leaving them as level
434 * sensitive would lead to an interrupt storm.
436 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
438 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
440 panic("unexpected vector for hooking\n");
447 DEBUG("hooking vector: %d\n", vector);
455 error = request_irq((vector - 32),
456 (void *)palacios_dispatch_interrupt,
458 "interrupt_for_palacios",
462 ERROR("error code for request_irq is %d\n", error);
463 panic("request vector %d failed", vector);
473 * Acknowledges an interrupt.
476 palacios_ack_interrupt(
481 DEBUG("Pretending to ack interrupt, vector=%d\n", vector);
486 * Returns the CPU frequency in kilohertz.
489 palacios_get_cpu_khz(void)
491 INFO("cpu_khz is %u\n", cpu_khz);
494 INFO("faking cpu_khz to 1000000\n");
503 * Yield the CPU so other host OS tasks can run.
506 palacios_yield_cpu(void)
516 * Returns NULL on failure.
519 palacios_mutex_alloc(void)
521 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
524 spin_lock_init(lock);
534 palacios_mutex_free(void * mutex) {
542 palacios_mutex_lock(void * mutex, int must_spin) {
543 spin_lock((spinlock_t *)mutex);
550 palacios_mutex_unlock(
554 spin_unlock((spinlock_t *)mutex);
558 * Structure used by the Palacios hypervisor to interface with the host kernel.
560 static struct v3_os_hooks palacios_os_hooks = {
561 .print = palacios_print,
562 .allocate_pages = palacios_allocate_pages,
563 .free_pages = palacios_free_pages,
564 .malloc = palacios_alloc,
565 .free = palacios_free,
566 .vaddr_to_paddr = palacios_vaddr_to_paddr,
567 .paddr_to_vaddr = palacios_paddr_to_vaddr,
568 .hook_interrupt = palacios_hook_interrupt,
569 .ack_irq = palacios_ack_interrupt,
570 .get_cpu_khz = palacios_get_cpu_khz,
571 .start_kernel_thread = palacios_start_kernel_thread,
572 .yield_cpu = palacios_yield_cpu,
573 .mutex_alloc = palacios_mutex_alloc,
574 .mutex_free = palacios_mutex_free,
575 .mutex_lock = palacios_mutex_lock,
576 .mutex_unlock = palacios_mutex_unlock,
577 .get_cpu = palacios_get_cpu,
578 .interrupt_cpu = palacios_interrupt_cpu,
579 .call_on_cpu = palacios_xcall,
580 .start_thread_on_cpu = palacios_start_thread_on_cpu,
581 .move_thread_to_cpu = palacios_move_thread_to_cpu,
587 int palacios_vmm_init( void )
589 int num_cpus = num_online_cpus();
590 char * cpu_mask = NULL;
592 if (cpu_list_len > 0) {
597 cpu_mask = kmalloc((num_cpus / 8) + 1, GFP_KERNEL);
600 ERROR("Cannot allocate cpu mask\n");
604 memset(cpu_mask, 0, (num_cpus / 8) + 1);
606 for (i = 0; i < cpu_list_len; i++) {
607 if (cpu_list[i] >= num_cpus) {
608 WARNING("CPU (%d) exceeds number of available CPUs. Ignoring...\n", cpu_list[i]);
612 major = cpu_list[i] / 8;
613 minor = cpu_list[i] % 8;
615 *(cpu_mask + major) |= (0x1 << minor);
619 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
621 if (init_print_buffers(num_cpus)) {
622 ERROR("Cannot initialize print buffers\n");
627 INFO("palacios_init starting - calling init_v3\n");
629 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus);
636 int palacios_vmm_exit( void ) {
640 INFO("palacios shutdown complete\n");
642 deinit_print_buffers();