2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <lwk/palacios.h>
21 #include <lwk/types.h>
23 #include <lwk/string.h>
24 #include <lwk/cpuinfo.h>
25 #include <lwk/kernel.h>
26 #include <arch/page.h>
27 #include <arch/ptrace.h>
28 #include <arch/apic.h>
30 struct guest_info * g_vm_guest = NULL;
33 // This is the function the interface code should call to deliver
34 // the interrupt to the vmm for handling
35 //extern int v3_deliver_interrupt(struct guest_info * vm, struct v3_interrupt *intr);
38 struct guest_info * irq_to_guest_map[256];
44 void Init_Stubs(struct guest_info * info) {
45 memset(irq_to_guest_map, 0, sizeof(struct guest_info *) * 256);
50 void * kitten_pa_to_va(void *ptr)
52 return (void*) __va(ptr);
55 void * kitten_va_to_pa(void *ptr)
57 return (void*) __pa(ptr);
60 void * Allocate_VMM_Pages(int num_pages)
63 struct pmem_region result;
65 rc=pmem_alloc_umem(num_pages*PAGE_SIZE,PAGE_SIZE,&result);
74 void Free_VMM_Page(void * page)
77 struct pmem_region query;
78 struct pmem_region result;
80 pmem_region_unset_all(&query);
83 query.end=page+PAGE_SIZE;
85 rc=pmem_query(&query,&result);
98 void send_key_to_vmm(unsigned char status, unsigned char scancode) {
99 struct v3_keyboard_event evt;
102 evt.scan_code = scancode;
105 v3_deliver_keyboard_event(g_vm_guest, &evt);
110 void send_mouse_to_vmm(unsigned char packet[3]) {
111 struct v3_mouse_event evt;
113 memcpy(evt.data, packet, 3);
116 v3_deliver_mouse_event(g_vm_guest, &evt);
120 void send_tick_to_vmm(unsigned int period_us) {
121 struct v3_timer_event evt;
123 evt.period_us = period_us;
126 v3_deliver_timer_event(g_vm_guest, &evt);
131 void translate_intr_handler(struct pt_regs *regs, unsigned int vector)
133 struct v3_interrupt intr;
135 intr.irq = vector-32;
136 intr.error = regs->orig_rax;
139 // PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
141 v3_deliver_irq(irq_to_guest_map[intr.irq], &intr);
147 int kitten_hook_interrupt(struct guest_info * vm, unsigned int irq)
149 if (irq_to_guest_map[irq]) {
150 //PrintBoth("Attempt to hook interrupt that is already hooked\n");
153 //PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n", irq, vm);
154 irq_to_guest_map[irq] = vm;
157 set_idtvec_handler(irq,translate_intr_handler);
164 lapic_ack_interrupt();
171 unsigned int get_cpu_khz()
173 return cpu_info[0].arch.cur_cpu_khz;
177 struct v3_os_hooks v3vee_os_hooks = {
178 .print_debug = &printk, // serial print ideally
179 .print_info = &printk, // serial print ideally
180 .print_trace = &printk, // serial print ideally
181 .allocate_pages = &Allocate_VMM_Pages, // defined in vmm_stubs
182 .free_page = &Free_VMM_Page, // defined in vmm_stubs
183 .malloc = &kmem_alloc,
185 .vaddr_to_paddr = &kitten_va_to_pa,
186 .paddr_to_vaddr = &kitten_pa_to_va,
187 .hook_interrupt = &kitten_hook_interrupt,
189 .get_cpu_khz = &get_cpu_khz,