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>
25 #include "palacios-mm.h"
34 static struct v3_vm_info * irq_to_guest_map[256];
37 extern unsigned int cpu_khz;
41 * Prints a message to the console.
43 static void palacios_print(const char * fmt, ...) {
55 * Allocates a contiguous region of pages of the requested size.
56 * Returns the physical address of the first page in the region.
58 static void * palacios_allocate_pages(int num_pages, unsigned int alignment) {
59 void * pg_addr = NULL;
61 pg_addr = (void *)alloc_palacios_pgs(num_pages, alignment);
62 pg_allocs += num_pages;
69 * Frees a page previously allocated via palacios_allocate_page().
70 * Note that palacios_allocate_page() can allocate multiple pages with
71 * a single call while palacios_free_page() only frees a single page.
74 static void palacios_free_pages(void * page_paddr, int num_pages) {
75 pg_frees += num_pages;
76 free_palacios_pgs((uintptr_t)page_paddr, num_pages);
81 * Allocates 'size' bytes of kernel memory.
82 * Returns the kernel virtual address of the memory allocated.
85 palacios_alloc(unsigned int size) {
88 addr = kmalloc(size, GFP_KERNEL);
95 * Frees memory that was previously allocated by palacios_alloc().
108 * Converts a kernel virtual address to the corresponding physical address.
111 palacios_vaddr_to_paddr(
115 return (void*) __pa(vaddr);
120 * Converts a physical address to the corresponding kernel virtual address.
123 palacios_paddr_to_vaddr(
131 * Runs a function on the specified CPU.
134 // For now, do call only on local CPU
138 void (*fn)(void *arg),
144 // We set wait to 1, but I'm not sure this is necessary
145 smp_call_function_single(cpu_id, fn, arg, 1);
150 struct lnx_thread_arg {
151 int (*fn)(void * arg);
156 static int lnx_thread_target(void * arg) {
157 struct lnx_thread_arg * thread_info = (struct lnx_thread_arg *)arg;
160 printk("Daemonizing new Palacios thread (name=%s)\n", thread_info->name);
162 daemonize(thread_info->name);
163 allow_signal(SIGKILL);
167 thread_info->fn(thread_info->arg);
176 * Creates a kernel thread.
179 palacios_start_kernel_thread(
180 int (*fn) (void * arg),
182 char * thread_name) {
184 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
186 thread_info->fn = fn;
187 thread_info->arg = arg;
188 thread_info->name = thread_name;
190 kthread_run( lnx_thread_target, thread_info, thread_name );
196 * Starts a kernel thread on the specified CPU.
199 palacios_start_thread_on_cpu(int cpu_id,
200 int (*fn)(void * arg),
202 char * thread_name ) {
203 struct task_struct * thread = NULL;
204 struct lnx_thread_arg * thread_info = kmalloc(sizeof(struct lnx_thread_arg), GFP_KERNEL);
206 thread_info->fn = fn;
207 thread_info->arg = arg;
208 thread_info->name = thread_name;
211 thread = kthread_create( lnx_thread_target, thread_info, thread_name );
213 if (IS_ERR(thread)) {
214 printk("Palacios error creating thread: %s\n", thread_name);
218 kthread_bind(thread, cpu_id);
219 wake_up_process(thread);
225 * Returns the CPU ID that the caller is running on.
228 palacios_get_cpu(void)
231 /* We want to call smp_processor_id()
232 * But this is not safe if kernel preemption is possible
233 * We need to ensure that the palacios threads are bound to a give cpu
236 unsigned int cpu_id = get_cpu();
242 * Interrupts the physical CPU corresponding to the specified logical guest cpu.
245 * This is dependent on the implementation of xcall_reschedule(). Currently
246 * xcall_reschedule does not explicitly call schedule() on the destination CPU,
247 * but instead relies on the return to user space to handle it. Because
248 * palacios is a kernel thread schedule will not be called, which is correct.
249 * If it ever changes to induce side effects, we'll need to figure something
253 #include <asm/apic.h>
256 palacios_interrupt_cpu(
257 struct v3_vm_info * vm,
263 smp_send_reschedule(cpu_id);
265 apic->send_IPI_mask(cpumask_of(cpu_id), vector);
271 * Dispatches an interrupt to Palacios for handling.
274 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
275 struct v3_interrupt intr = {
277 .error = regs->orig_ax,
281 if (irq_to_guest_map[vector]) {
282 v3_deliver_irq(irq_to_guest_map[vector], &intr);
288 * Instructs the kernel to forward the specified IRQ to Palacios.
291 palacios_hook_interrupt(struct v3_vm_info * vm,
292 unsigned int vector ) {
293 printk("hooking vector %d\n", vector);
295 if (irq_to_guest_map[vector]) {
297 "%s: Interrupt vector %u is already hooked.\n",
303 "%s: Hooking interrupt vector %u to vm %p.\n",
304 __func__, vector, vm);
306 irq_to_guest_map[vector] = vm;
309 * NOTE: Normally PCI devices are supposed to be level sensitive,
310 * but we need them to be edge sensitive so that they are
311 * properly latched by Palacios. Leaving them as level
312 * sensitive would lead to an interrupt storm.
314 //ioapic_set_trigger_for_vector(vector, ioapic_edge_sensitive);
316 //set_idtvec_handler(vector, palacios_dispatch_interrupt);
318 panic("unexpected vector for hooking\n");
325 printk("hooking vector: %d\n", vector);
333 error = request_irq((vector - 32),
334 (void *)palacios_dispatch_interrupt,
336 "interrupt_for_palacios",
340 printk("error code for request_irq is %d\n", error);
341 panic("request vector %d failed",vector);
351 * Acknowledges an interrupt.
354 palacios_ack_interrupt(
359 printk("Pretending to ack interrupt, vector=%d\n",vector);
364 * Returns the CPU frequency in kilohertz.
367 palacios_get_cpu_khz(void)
369 printk("cpu_khz is %u\n",cpu_khz);
372 printk("faking cpu_khz to 1000000\n");
381 * Yield the CPU so other host OS tasks can run.
384 palacios_yield_cpu(void)
394 * Returns NULL on failure.
397 palacios_mutex_alloc(void)
399 spinlock_t *lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
402 spin_lock_init(lock);
412 palacios_mutex_free(void * mutex) {
420 palacios_mutex_lock(void * mutex, int must_spin) {
421 spin_lock((spinlock_t *)mutex);
428 palacios_mutex_unlock(
432 spin_unlock((spinlock_t *)mutex);
436 * Structure used by the Palacios hypervisor to interface with the host kernel.
438 static struct v3_os_hooks palacios_os_hooks = {
439 .print = palacios_print,
440 .allocate_pages = palacios_allocate_pages,
441 .free_pages = palacios_free_pages,
442 .malloc = palacios_alloc,
443 .free = palacios_free,
444 .vaddr_to_paddr = palacios_vaddr_to_paddr,
445 .paddr_to_vaddr = palacios_paddr_to_vaddr,
446 .hook_interrupt = palacios_hook_interrupt,
447 .ack_irq = palacios_ack_interrupt,
448 .get_cpu_khz = palacios_get_cpu_khz,
449 .start_kernel_thread = palacios_start_kernel_thread,
450 .yield_cpu = palacios_yield_cpu,
451 .mutex_alloc = palacios_mutex_alloc,
452 .mutex_free = palacios_mutex_free,
453 .mutex_lock = palacios_mutex_lock,
454 .mutex_unlock = palacios_mutex_unlock,
455 .get_cpu = palacios_get_cpu,
456 .interrupt_cpu = palacios_interrupt_cpu,
457 .call_on_cpu = palacios_xcall,
458 .start_thread_on_cpu = palacios_start_thread_on_cpu,
464 int palacios_vmm_init( void )
467 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
469 printk("palacios_init starting - calling init_v3\n");
471 Init_V3(&palacios_os_hooks, nr_cpu_ids);
478 int palacios_vmm_exit( void ) {