1 #include <geekos/vmm_stubs.h>
2 #include <geekos/serial.h>
3 #include <palacios/vm_guest.h>
4 #include <geekos/debug.h>
8 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
10 __asm__ __volatile__ (
13 : "a" (value), "Nd" (port)
18 * Read a byte from an I/O port.
20 static inline uchar_t VM_In_Byte(ushort_t port)
24 __asm__ __volatile__ (
36 void * Identity(void *addr) { return addr; };
38 void * Allocate_VMM_Pages(int num_pages) {
39 void * start_page = Alloc_Page();
40 //SerialPrint("Starting by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
43 while (i < num_pages) {
44 void * tmp_page = Alloc_Page();
45 //SerialPrint("Allocating Page: %x (%d of %d)\n",tmp_page, i+1, num_pages);
47 if (tmp_page != start_page + (PAGE_SIZE * i)) {
48 //we have to start over...;
50 Free_Page(start_page + (PAGE_SIZE * i));
53 start_page = Alloc_Page();
54 //SerialPrint("Starting over by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
64 void Free_VMM_Page(void * page) {
69 void * VMM_Malloc(unsigned int size) {
70 return Malloc((unsigned long) size);
74 void VMM_Free(void * addr) {
80 // This is the interrupt state that the VMM's interrupt handlers need to see
82 struct vmm_intr_state {
86 uint_t should_ack; // Should the vmm ack this interrupt, or will
89 // This is the value given when the interrupt is hooked.
90 // This will never be NULL
94 // This is the function the interface code should call to deliver
95 // the interrupt to the vmm for handling
96 extern void deliver_interrupt_to_vmm(struct vmm_intr_state *state);
99 struct guest_info * irq_map[256];
101 void *my_opaque[256];
104 static void translate_intr_handler(struct Interrupt_State *state)
108 struct vmm_intr_state mystate;
110 mystate.irq=state->intNum-32;
111 mystate.error=state->errorCode;
112 mystate.should_ack=0;
113 mystate.opaque=my_opaque[mystate.irq];
115 // PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
117 deliver_interrupt_to_vmm(&mystate);
125 static void pic_intr_handler(struct Interrupt_State * state) {
127 struct guest_info * info = irq_map[state->intNum - 32];
128 SerialPrint("Interrupt %d (IRQ=%d)\n", state->intNum, state->intNum - 32);
131 info->vm_ops.raise_irq(info, state->intNum - 32);
133 SerialPrint("Interrupt handler error: NULL pointer found, no action taken\n");
143 // I really don't know what the heck this is doing... PAD
146 int hook_irq_stub(struct guest_info * info, int irq) {
151 SerialPrint("Hooking IRQ: %d (vm=0x%x)\n", irq, info);
153 volatile void *foo = pic_intr_handler;
155 // This is disabled for the time being
160 Install_IRQ(irq, pic_intr_handler);
166 int geekos_hook_interrupt_new(uint_t irq, void * opaque)
168 if (my_opaque[irq]) {
169 PrintBoth("Attempt to hook interrupt that is already hooked\n");
172 PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n",irq,opaque);
173 my_opaque[irq]=opaque;
177 Install_IRQ(irq,translate_intr_handler);
183 int ack_irq(int irq) {
190 memset(irq_map, 0, sizeof(struct guest_info *) * 256);
194 unsigned int get_cpu_khz() {
195 extern uint_t cpu_khz_freq;
197 unsigned long print_khz = (unsigned long)(cpu_khz_freq & 0xffffffff);
199 PrintBoth("Detected %lu.%lu MHz CPU\n", print_khz / 1000, print_khz % 1000);