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/smp_lock.h>
19 #include <palacios/vmm.h>
20 #include <palacios/vmm_host_events.h>
26 #include "palacios-mm.h"
35 static struct v3_vm_info * irq_to_guest_map[256];
38 extern unsigned int cpu_khz;
42 * Prints a message to the console.
44 static void palacios_print(const char * fmt, ...) {
56 * Allocates a contiguous region of pages of the requested size.
57 * Returns the physical address of the first page in the region.
59 static void * palacios_allocate_pages(int num_pages, unsigned int alignment) {
60 void * pg_addr = NULL;
62 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
63 pg_allocs += num_pages;
70 * Frees a page previously allocated via palacios_allocate_page().
71 * Note that palacios_allocate_page() can allocate multiple pages with
72 * a single call while palacios_free_page() only frees a single page.
75 static void palacios_free_pages(void * page_paddr, int num_pages) {
76 pg_frees += num_pages;
77 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
82 * Allocates 'size' bytes of kernel memory.
83 * Returns the kernel virtual address of the memory allocated.
86 palacios_alloc(unsigned int size) {
89 addr = kmalloc(size, GFP_KERNEL);
96 * Frees memory that was previously allocated by palacios_alloc().
109 * Converts a kernel virtual address to the corresponding physical address.
112 palacios_vaddr_to_paddr(
116 return (void*) __pa(vaddr);
121 * Converts a physical address to the corresponding kernel virtual address.
124 palacios_paddr_to_vaddr(
132 * Runs a function on the specified CPU.
135 // For now, do call only on local CPU
139 void (*fn)(void *arg),
145 // We set wait to 1, but I'm not sure this is necessary
146 smp_call_function_single(cpu_id, fn, arg, 1);
151 struct lnx_thread_arg {
152 int (*fn)(void * arg);
157 static int lnx_thread_target(void * arg) {
158 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
162 printk("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
164 daemonize(thread_info->name);
166 allow_signal(SIGKILL);
170 thread_info->fn(thread_info->arg);
179 * Creates a kernel thread.
182 palacios_start_kernel_thread(
183 int (*fn) (void * arg),
185 char * thread_name) {
187 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
189 thread_info->fn = fn;
190 thread_info->arg = arg;
191 thread_info->name = thread_name;
193 kthread_run( lnx_thread_target, thread_info, thread_name );
199 * Starts a kernel thread on the specified CPU.
202 palacios_start_thread_on_cpu(int cpu_id,
203 int (*fn)(void * arg),
205 char * thread_name ) {
206 struct task_struct * thread = NULL;
207 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
209 thread_info->fn = fn;
210 thread_info->arg = arg;
211 thread_info->name = thread_name;
214 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
216 if (IS_ERR(thread)) {
217 printk("Palacios error creating thread: %s\n", thread_name);
221 kthread_bind(thread, cpu_id);
222 wake_up_process(thread);
228 * Returns the CPU ID that the caller is running on.
231 palacios_get_cpu(void)
234 /* We want to call smp_processor_id()
235 * But this is not safe if kernel preemption is possible
236 * We need to ensure that the palacios threads are bound to a give cpu
239 unsigned int cpu_id = get_cpu();
245 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
248 * This is dependent on the implementation of xcall_reschedule(). Currently
249 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
250 * but instead relies on the return to user space to handle it. Because
251 * palacios is a kernel thread schedule will not be called, which is correct.
252 * If it ever changes to induce side effects, we'll need to figure something
256 #include <asm/apic.h>
259 palacios_interrupt_cpu(
260 struct v3_vm_info * vm,
266 smp_send_reschedule(cpu_id);
268 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
274 * Dispatches an interrupt to Palacios for handling.
277 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
278 struct v3_interrupt intr = {
280 .error = regs->orig_ax,
284 if (irq_to_guest_map[vector]) {
285 v3_deliver_irq(irq_to_guest_map[vector], &intr);
291 * Instructs the kernel to forward the specified IRQ to Palacios.
294 palacios_hook_interrupt(struct v3_vm_info * vm,
295 unsigned int vector ) {
296 printk("hooking vector %d\n", vector);
298 if (irq_to_guest_map[vector]) {
300 "%s: Interrupt vector %u is already hooked.\n",
306 "%s: Hooking interrupt vector %u to vm %p.\n",
307 __func__, vector, vm);
309 irq_to_guest_map[vector] = vm;
312 * NOTE: Normally PCI devices are supposed to be level sensitive,
313 * but we need them to be edge sensitive so that they are
314 * properly latched by Palacios. Leaving them as level
315 * sensitive would lead to an interrupt storm.
317 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
319 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
321 panic("unexpected vector for hooking\n");
328 printk("hooking vector: %d\n", vector);
336 error = request_irq((vector - 32),
337 (void *)palacios_dispatch_interrupt,
339 "interrupt_for_palacios",
343 printk("error code for request_irq is %d\n", error);
344 panic("request vector %d failed",vector);
354 * Acknowledges an interrupt.
357 palacios_ack_interrupt(
362 printk("Pretending to ack interrupt, vector=%d\n",vector);
367 * Returns the CPU frequency in kilohertz.
370 palacios_get_cpu_khz(void)
372 printk("cpu_khz is %u\n",cpu_khz);
375 printk("faking cpu_khz to 1000000\n");
384 * Yield the CPU so other host OS tasks can run.
387 palacios_yield_cpu(void)
397 * Returns NULL on failure.
400 palacios_mutex_alloc(void)
402 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
405 spin_lock_init(lock);
415 palacios_mutex_free(void * mutex) {
423 palacios_mutex_lock(void * mutex, int must_spin) {
424 spin_lock((spinlock_t *)mutex);
431 palacios_mutex_unlock(
435 spin_unlock((spinlock_t *)mutex);
439 * Structure used by the Palacios hypervisor to interface with the host kernel.
441 static struct v3_os_hooks palacios_os_hooks = {
442 .print = palacios_print,
443 .allocate_pages = palacios_allocate_pages,
444 .free_pages = palacios_free_pages,
445 .malloc = palacios_alloc,
446 .free = palacios_free,
447 .vaddr_to_paddr = palacios_vaddr_to_paddr,
448 .paddr_to_vaddr = palacios_paddr_to_vaddr,
449 .hook_interrupt = palacios_hook_interrupt,
450 .ack_irq = palacios_ack_interrupt,
451 .get_cpu_khz = palacios_get_cpu_khz,
452 .start_kernel_thread = palacios_start_kernel_thread,
453 .yield_cpu = palacios_yield_cpu,
454 .mutex_alloc = palacios_mutex_alloc,
455 .mutex_free = palacios_mutex_free,
456 .mutex_lock = palacios_mutex_lock,
457 .mutex_unlock = palacios_mutex_unlock,
458 .get_cpu = palacios_get_cpu,
459 .interrupt_cpu = palacios_interrupt_cpu,
460 .call_on_cpu = palacios_xcall,
461 .start_thread_on_cpu = palacios_start_thread_on_cpu,
467 int palacios_vmm_init( void )
470 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
472 printk("palacios_init starting - calling init_v3\n");
474 Init_V3(&palacios_os_hooks, nr_cpu_ids);
481 int palacios_vmm_exit( void ) {