1 #include <nautilus/nautilus.h>
2 #include <nautilus/thread.h>
3 #include <nautilus/printk.h>
4 #include <nautilus/cpu.h>
5 #include <nautilus/mm.h>
6 #include <nautilus/vc.h>
10 #include <palacios/vmm.h>
17 This is a simple proof-of-concept implementation of the Palacios
18 host interface on top of Nautilus. It is sufficient to allow
19 us to boot a guest OS running Linux. A few things to note:
21 - Nautilus currently has a grand-unified allocator designed to help
22 support parallel run-time integration. All of alloc/valloc/page
23 allocation are built on top of that.
24 - For page allocation, constraints, NUMA, and filter expressions are
26 - thread migration is not supported currently
27 - hooking of host interrupts is not supported currently.
28 - Palacios can sleep, yield, wakeup, etc, but be aware
29 that Nautilus threads operate differently than those of
33 - Do Nautilus regular startup to bring all cores to idle
34 - From a kernel thread, ideally the init thread on core 0,
35 do palacios_vmm_init(memory_size_bytes,options)
36 - You can now use the Palacios v3_* functions, which are
38 - You need to keep the Nautilus VM state in sync with
39 the Palacios VM state. The protocol for this is:
40 1. before doing a VM creation, call
41 palacios_inform_new_vm_pre(name)
42 this will also select the new vm for
43 the creation and going forward
44 then, once v3_create is done, call
45 palacios_inform_new_vm_post(name, vm)
46 2. during execution, whenever you want to
47 manage a different VM, call
48 palacios_inform_select_vm(vm)
50 palacios_inform_select_vm_by_name(name)
51 It is OK to to select repeatedly, etc.
52 3. after doing a VM free, call
53 palacios_inform_free_vm(name)
55 palacios_inform_free_selected_vm()
56 - After you are done, do a palacios_vmm_deinit();
60 // The following can be used to track memory bugs
61 // zero memory after allocation (now applies to valloc and page alloc as well)
62 #define ALLOC_ZERO_MEM 1
63 // pad allocations by this many bytes on both ends of block (heap only)
65 #define MAX_THREAD_NAME 32
67 int run_nk_thread = 0;
69 static struct nk_vm_state vms[NR_VMS];
71 static struct nk_vm_state *selected_vm;
73 static struct v3_vm_info * irq_to_guest_map[256];
75 static unsigned int cpu_khz=-1;
77 static char *print_buffer[NR_CPUS];
79 static void deinit_print_buffers(void)
83 for (i=0;i<NR_CPUS;i++) {
84 if (print_buffer[i]) {
85 palacios_free(print_buffer[i]);
91 static int init_print_buffers(void)
95 memset(print_buffer,0,sizeof(char*)*NR_CPUS);
97 for (i=0;i<NR_CPUS;i++) {
98 print_buffer[i] = palacios_alloc(V3_PRINTK_BUF_SIZE);
99 if (!print_buffer[i]) {
100 ERROR("Cannot allocate print buffer for cpu %d\n",i);
101 deinit_print_buffers();
104 memset(print_buffer[i],0,V3_PRINTK_BUF_SIZE);
115 * Prints a message to the console.
117 void palacios_print_scoped(void * vm, int vcore, const char *fmt, ...)
121 unsigned int cpu = palacios_get_cpu();
122 char *buf = cpu < NR_CPUS ? print_buffer[cpu] : 0;
125 INFO_PRINT("palacios (pcore %u): output skipped - no allocated buffer\n",cpu);
131 vsnprintf(buf,V3_PRINTK_BUF_SIZE, fmt, ap);
136 INFO_PRINT("palacios (pcore %u vm %s vcore %u): %s",
142 INFO_PRINT(KERN_INFO "palacios (pcore %u vm %s): %s",
148 INFO_PRINT(KERN_INFO "palacios (pcore %u): %s",
159 * Allocates a contiguous region of pages of the requested size.
160 * Returns the physical address of the first page in the region.
162 void *palacios_allocate_pages(int num_pages, unsigned int alignment, int node_id, int (*filter_func)(void *paddr, void *filter_state), void *filter_state)
164 void * pg_addr = NULL;
167 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);
171 // malloc currently guarantees alignment to the size of
173 pg_addr = (void *)malloc(num_pages*4096);
176 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);
180 if ((uint64_t)pg_addr & 0xfff) {
181 ERROR("ALERT ALERT Page allocation has surprise offset\n");
186 memset(pg_addr,0,num_pages*4096);
189 //INFO("allocpages: %p (%llu pages) alignment=%u\n", pg_addr, num_pages, alignment);
196 * Frees a page previously allocated via palacios_allocate_page().
197 * Note that palacios_allocate_page() can allocate multiple pages with
198 * a single call while palacios_free_page() only frees a single page.
201 void palacios_free_pages(void * page_paddr, int num_pages) {
203 ERROR("Ignoring free pages: 0x%p (0x%lx)for %d pages\n", page_paddr, (uintptr_t)page_paddr, num_pages);
208 INFO("freepages: %p (%llu pages) alignment=%u\n", page_paddr, num_pages);
213 palacios_alloc_extended(unsigned int size, unsigned int flags, int node) {
217 ERROR("ALERT ALERT attempt to kmalloc zero bytes rejected\n");
222 addr = malloc(size+2*ALLOC_PAD);
224 // currently no numa-zone specific kmalloc
225 addr = malloc(size+2*ALLOC_PAD);
229 ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n");
234 memset(addr,0,size+2*ALLOC_PAD);
237 //INFO("malloc: 0x%p (%llu bytes)\n",addr+ALLOC_PAD,size);
239 return addr+ALLOC_PAD;
243 palacios_valloc(unsigned int size)
248 ERROR("ALERT ALERT attempt to vmalloc zero bytes rejected\n");
252 // currently no vmalloc
256 ERROR("ALERT ALERT vmalloc has FAILED FAILED FAILED\n");
264 //INFO("valloc: 0x%p (%llu bytes)\n",addr,size);
269 void palacios_vfree(void *p)
272 ERROR("Ignoring vfree: 0x%p\n",p);
275 // no vfree currently
278 //INFO("vfree: 0x%p\n",p);
282 * Allocates 'size' bytes of kernel memory.
283 * Returns the kernel virtual address of the memory allocated.
286 palacios_alloc(unsigned int size)
288 return palacios_alloc_extended(size,0,-1);
292 * Frees memory that was previously allocated by palacios_alloc().
295 palacios_free(void *addr)
299 ERROR("Ignoring free : 0x%p\n", addr);
303 free(addr-ALLOC_PAD);
304 //INFO("free: %p\n",addr-ALLOC_PAD);
308 * Converts a kernel virtual address to the corresponding physical address.
311 palacios_vaddr_to_paddr(
315 return vaddr; // our memory mapping is identity
320 * Converts a physical address to the corresponding kernel virtual address.
323 palacios_paddr_to_vaddr(
327 return paddr; // our memory mapping is identity
331 * Runs a function on the specified CPU.
336 void (*fn)(void *arg),
341 smp_xcall(cpu_id,fn,arg,1);
348 struct nautilus_thread_arg {
349 int (*fn)(void * arg);
351 char name[MAX_THREAD_NAME];
354 static void nautilus_thread_target(void * in, void ** out)
356 struct nautilus_thread_arg * thread_info = (struct nautilus_thread_arg *)in;
359 ret = thread_info->fn(thread_info->arg);
361 INFO("Palacios Thread (%s) EXITING with return code %d\n", thread_info->name, ret);
363 palacios_free(thread_info);
367 * Creates a kernel thread.
370 palacios_create_and_start_kernel_thread(
371 int (*fn) (void * arg),
374 v3_resource_control_t *rctl)
377 struct nautilus_thread_arg * thread_info = palacios_alloc(sizeof(struct nautilus_thread_arg));
378 nk_thread_id_t tid = 0;
381 ERROR("ALERT ALERT Unable to allocate thread\n");
385 thread_info->fn = fn;
386 thread_info->arg = arg;
387 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
388 thread_info->name[MAX_THREAD_NAME-1] =0;
390 nk_thread_start(nautilus_thread_target, thread_info, 0, 0, 0, &tid, CPU_ANY);
397 * Starts a kernel thread on the specified CPU.
400 palacios_create_thread_on_cpu(int cpu_id,
401 int (*fn)(void * arg),
404 v3_resource_control_t *rctl)
406 nk_thread_id_t newtid;
407 nk_thread_t * newthread = NULL;
408 struct nautilus_thread_arg * thread_info = palacios_alloc(sizeof(struct nautilus_thread_arg));
410 thread_info->fn = fn;
411 thread_info->arg = arg;
412 strncpy(thread_info->name, thread_name, MAX_THREAD_NAME);
413 thread_info->name[MAX_THREAD_NAME-1] = 0;
415 //INFO("CREATING A THREAD ON CPU ID: %d\n", cpu_id);
417 if (nk_thread_create(nautilus_thread_target, thread_info, 0, 0, 0, &newtid, cpu_id) < 0) {
418 ERROR("COULD NOT CREATE THREAD\n");
421 //INFO("newtid: %lu\n", newtid);
427 palacios_start_thread(void * th)
436 palacios_create_and_start_thread_on_cpu(int cpu_id,
437 int (*fn)(void * arg),
440 v3_resource_control_t *rctl )
445 struct nautilus_thread_arg * thread_info = palacios_alloc(sizeof(struct nautilus_thread_arg));
448 ERROR("ALERT ALERT Unable to allocate thread to start on cpu\n");
452 thread_info->fn = fn;
453 thread_info->arg = arg;
454 strncpy(thread_info->name,thread_name,MAX_THREAD_NAME);
455 thread_info->name[MAX_THREAD_NAME-1] =0;
457 nk_thread_start(nautilus_thread_target, thread_info, 0, 0, 0,&tid,cpu_id); //
465 * Rebind a kernel thread to the specified CPU
466 * The thread will be running on target CPU on return
467 * non-zero return means failure
470 palacios_move_thread_to_cpu(int new_cpu_id,
474 INFO("Moving thread (%p) to cpu %d\n", thread_ptr, new_cpu_id);
475 ERROR("NOT CURRENTLY SUPPORTED\n");
481 * Returns the CPU ID that the caller is running on.
484 palacios_get_cpu(void)
490 palacios_interrupt_cpu( struct v3_vm_info * vm,
494 apic_ipi(per_cpu_get(apic),cpu_id,vector); // find out apic_dev * and cpu to apic id mapping
501 * Dispatches an interrupt to Palacios for handling.
504 palacios_dispatch_interrupt( int vector, void * dev, struct pt_regs * regs ) {
505 struct v3_interrupt intr = {
507 .error = 0, //regs->orig_ax, /* TODO fix this */
511 if (irq_to_guest_map[vector]) {
512 v3_deliver_irq(irq_to_guest_map[vector], &intr);
518 * Instructs the kernel to forward the specified IRQ to Palacios.
521 palacios_hook_interrupt(struct v3_vm_info * vm,
522 unsigned int vector )
524 ERROR("UNSUPPORTED: PALACIOS_HOOK_INTERRUPT\n");
530 * Acknowledges an interrupt.
533 palacios_ack_interrupt(
537 ERROR("UNSUPPORTED: PALACIOS_ACK_INTERRUPT\n");
542 * Returns the CPU frequency in kilohertz.
545 palacios_get_cpu_khz(void)
548 uint32_t cpu = (uint32_t)my_cpu_id();
550 cpu_khz = nk_detect_cpu_freq(cpu);
552 INFO("CANNOT GET THE CPU FREQUENCY. FAKING TO 1000000\n");
556 INFO("Nautilus frequency at %u KHz\n",cpu_khz);
561 * Yield the CPU so other host OS tasks can run.
562 * This will return immediately if there is no other thread that is runnable
563 * And there is no real bound on how long it will yield
566 palacios_yield_cpu(void)
573 * Yield the CPU so other host OS tasks can run.
574 * Given now immediately if there is no other thread that is runnable
575 * And there is no real bound on how long it will yield
577 void palacios_sleep_cpu(unsigned int us)
579 // sleep not supported on Nautilus
585 void palacios_wakeup_cpu(void *thread)
587 // threads never go to sleep, so shouldn't happen
588 ERROR("ERROR ERROR: WAKEUP_CPU CALLED. THREADS ARE NEVER ASLEEP");
594 * Returns NULL on failure.
597 palacios_mutex_alloc(void)
599 spinlock_t *lock = palacios_alloc(sizeof(spinlock_t));
604 ERROR("ALERT ALERT Unable to allocate lock\n");
611 void palacios_mutex_init(void *mutex)
613 spinlock_t *lock = (spinlock_t*)mutex;
617 LOCKCHECK_ALLOC(lock);
622 void palacios_mutex_deinit(void *mutex)
624 spinlock_t *lock = (spinlock_t*)mutex;
627 spinlock_deinit(lock);
628 LOCKCHECK_FREE(lock);
637 palacios_mutex_free(void * mutex) {
638 palacios_free(mutex);
639 LOCKCHECK_FREE(mutex);
646 palacios_mutex_lock(void * mutex, int must_spin) {
647 LOCKCHECK_LOCK_PRE(mutex);
648 spin_lock((spinlock_t *)mutex);
649 LOCKCHECK_LOCK_POST(mutex);
654 * Locks a mutex, disabling interrupts on this core
657 palacios_mutex_lock_irqsave(void * mutex, int must_spin) {
661 LOCKCHECK_LOCK_IRQSAVE_PRE(mutex,flags);
662 flags = spin_lock_irq_save((spinlock_t *)mutex);
663 LOCKCHECK_LOCK_IRQSAVE_POST(mutex,flags);
665 //INFO("lock irqsave flags=%lu\n",flags);
666 return (void *)flags;
674 palacios_mutex_unlock(
678 LOCKCHECK_UNLOCK_PRE(mutex);
679 spin_unlock((spinlock_t *)mutex);
680 LOCKCHECK_UNLOCK_POST(mutex);
685 * Unlocks a mutex and restores previous interrupt state on this core
688 palacios_mutex_unlock_irqrestore(void *mutex, void *flags)
690 //INFO("unlock irqrestore flags=%lu\n",(unsigned long)flags);
691 LOCKCHECK_UNLOCK_IRQRESTORE_PRE(mutex,(unsigned long)flags);
692 // This is correct, flags is opaque
693 spin_unlock_irq_restore((spinlock_t *)mutex,(uint8_t) (unsigned long)flags);
694 LOCKCHECK_UNLOCK_IRQRESTORE_POST(mutex,(unsigned long)flags);
699 * Structure used by the Palacios hypervisor to interface with the host kernel.
701 static struct v3_os_hooks palacios_os_hooks = {
702 .print = palacios_print_scoped,
703 .allocate_pages = palacios_allocate_pages,
704 .free_pages = palacios_free_pages,
705 .vmalloc = palacios_valloc,
706 .vfree = palacios_vfree,
707 .malloc = palacios_alloc,
708 .free = palacios_free,
709 .vaddr_to_paddr = palacios_vaddr_to_paddr,
710 .paddr_to_vaddr = palacios_paddr_to_vaddr,
711 .hook_interrupt = palacios_hook_interrupt,
712 .ack_irq = palacios_ack_interrupt,
713 .get_cpu_khz = palacios_get_cpu_khz,
714 .start_kernel_thread = palacios_create_and_start_kernel_thread,
715 .yield_cpu = palacios_yield_cpu,
716 .sleep_cpu = palacios_sleep_cpu,
717 .wakeup_cpu = palacios_wakeup_cpu,
718 .mutex_alloc = palacios_mutex_alloc,
719 .mutex_free = palacios_mutex_free,
720 .mutex_lock = palacios_mutex_lock,
721 .mutex_unlock = palacios_mutex_unlock,
722 .mutex_lock_irqsave = palacios_mutex_lock_irqsave,
723 .mutex_unlock_irqrestore = palacios_mutex_unlock_irqrestore,
724 .get_cpu = palacios_get_cpu,
725 .interrupt_cpu = palacios_interrupt_cpu,
726 .call_on_cpu = palacios_xcall,
727 .create_thread_on_cpu = palacios_create_thread_on_cpu,
728 .start_thread = palacios_start_thread,
729 .move_thread_to_cpu = palacios_move_thread_to_cpu, // unsupported
733 int palacios_vmm_init(char * options)
735 int num_cpus = nautilus_info.sys.num_cpus;
736 char * cpu_mask = NULL;
743 cpu_mask = palacios_alloc((num_cpus / 8) + 1);
746 ERROR("Cannot allocate cpu mask\n");
750 memset(cpu_mask, 0, (num_cpus / 8) + 1);
752 for (i = 0; i < num_cpus; i++) {
757 *(cpu_mask + major) |= (0x1 << minor);
762 memset(irq_to_guest_map, 0, sizeof(struct v3_vm_info *) * 256);
764 memset(vms,0,sizeof(vms));
766 if (init_print_buffers()) {
767 INFO("Cannot initialize print buffers\n");
768 palacios_free(cpu_mask);
772 INFO("printbuffer init done\n");
774 INFO("NR_CPU: %d\n", NR_CPUS);
776 INFO("palacios_init starting - calling init_v3\n");
778 INFO("calling init_v3 = %p\n", Init_V3);
780 INFO("num_cpus: %d\ncpu_mask: %x\noptions: %s\n", num_cpus, *cpu_mask, options);
782 Init_V3(&palacios_os_hooks, cpu_mask, num_cpus, options);
784 INFO("init_v3 done\n");
786 #ifdef V3_CONFIG_CONSOLE
787 INFO("Initializing console\n");
788 nautilus_console_init();
797 int palacios_vmm_exit( void )
800 #ifdef V3_CONFIG_CONSOLE
801 nautilus_console_deinit();
806 INFO("palacios shutdown complete\n");
808 deinit_print_buffers();
814 void palacios_inform_new_vm_pre(char *name)
817 for (i=0;i<NR_VMS;i++) {
818 if (!vms[i].name[0]) {
819 strncpy(vms[i].name,name,MAX_VM_NAME);
820 selected_vm = &vms[i];
826 void palacios_inform_new_vm_post(char *name, struct v3_vm_info *vm)
828 struct nk_vm_state *n = palacios_find_vm_by_name(name);
832 INFO("Registered VM %p with name %s, node=%p, selected VM=%p\n",
833 vm, n->name, n, selected_vm);
835 ERROR("Cannot find VM with name \"%s\"\n",name);
839 void palacios_inform_free_vm(char *name)
841 struct nk_vm_state *n = palacios_find_vm_by_name(name);
843 if (n==selected_vm) {
855 void palacios_inform_free_selected_vm()
857 struct nk_vm_state *n = selected_vm;
869 struct nk_vm_state *palacios_find_vm_by_name(char *name)
872 for (i=0;i<NR_VMS;i++) {
873 if (!strncmp(vms[i].name,name,MAX_VM_NAME)) {
880 struct nk_vm_state *palacios_find_vm(struct v3_vm_info *vm)
883 for (i=0;i<NR_VMS;i++) {
884 if (vms[i].vm == vm) {
891 void palacios_select_vm(struct v3_vm_info *vm)
893 struct nk_vm_state *n = palacios_find_vm(vm);
899 void palacios_select_vm_by_name(char *name)
901 struct nk_vm_state *n = palacios_find_vm_by_name(name);
907 struct nk_vm_state *palacios_get_selected_vm()