Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added new copyright and license
[palacios.git] / palacios / src / geekos / vmm_stubs.c
1 /*
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <geekos/vmm_stubs.h>
21 #include <geekos/serial.h>
22 #include <palacios/vm_guest.h>
23 #include <geekos/debug.h>
24
25
26
27 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
28 {
29     __asm__ __volatile__ (
30         "outb %b0, %w1"
31         :
32         : "a" (value), "Nd" (port)
33     );
34 }
35
36 /*
37  * Read a byte from an I/O port.
38  */
39 static inline uchar_t VM_In_Byte(ushort_t port)
40 {
41     uchar_t value;
42
43     __asm__ __volatile__ (
44         "inb %w1, %b0"
45         : "=a" (value)
46         : "Nd" (port)
47     );
48
49     return value;
50 }
51
52
53
54
55 void * Identity(void *addr) { return addr; };
56
57 void * Allocate_VMM_Pages(int num_pages) {
58   void * start_page = Alloc_Page();
59   //SerialPrint("Starting by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages); 
60   int i = 1;
61
62   while (i < num_pages) {
63     void * tmp_page = Alloc_Page();
64     //SerialPrint("Allocating Page: %x (%d of %d)\n",tmp_page, i+1, num_pages); 
65     
66     if (tmp_page != start_page + (PAGE_SIZE * i)) {
67       //we have to start over...;
68       while (i >= 0) {
69         Free_Page(start_page + (PAGE_SIZE * i));
70         i--;
71       }
72       start_page = Alloc_Page();
73       //SerialPrint("Starting over by Allocating Page: %x (%d of %d)\n",start_page, 1, num_pages);
74       i = 1;
75       continue;
76     }
77     i++;
78   }
79
80   return start_page;
81 }
82
83 void Free_VMM_Page(void * page) {
84   Free_Page(page);
85 }
86
87
88 void * VMM_Malloc(unsigned int size) {
89   return Malloc((unsigned long) size);
90 }
91
92
93 void VMM_Free(void * addr) {
94   Free(addr);
95 }
96
97 //
98 //
99 // This is the interrupt state that the VMM's interrupt handlers need to see
100 //
101 struct vmm_intr_state {
102   uint_t irq;
103   uint_t error;
104
105   uint_t should_ack;  // Should the vmm ack this interrupt, or will
106                       // the host OS do it?
107
108   // This is the value given when the interrupt is hooked.
109   // This will never be NULL
110   void *opaque;
111 };
112
113 // This is the function the interface code should call to deliver
114 // the interrupt to the vmm for handling
115 extern void deliver_interrupt_to_vmm(struct vmm_intr_state *state);
116
117
118 struct guest_info * irq_map[256];
119
120 void *my_opaque[256];
121
122
123 static void translate_intr_handler(struct Interrupt_State *state)
124 {
125
126
127   struct vmm_intr_state mystate;
128
129   mystate.irq=state->intNum-32;
130   mystate.error=state->errorCode;
131   mystate.should_ack=0;
132   mystate.opaque=my_opaque[mystate.irq];
133
134   //  PrintBoth("translate_intr_handler: opaque=0x%x\n",mystate.opaque);
135
136   deliver_interrupt_to_vmm(&mystate);
137
138   End_IRQ(state);
139
140 }
141
142
143 /*
144 static void pic_intr_handler(struct Interrupt_State * state) {
145   Begin_IRQ(state);
146   struct guest_info * info =   irq_map[state->intNum - 32];
147   SerialPrint("Interrupt %d (IRQ=%d)\n", state->intNum, state->intNum - 32);
148
149   if (info) {
150     info->vm_ops.raise_irq(info, state->intNum - 32);
151   } else {
152     SerialPrint("Interrupt handler error: NULL pointer found, no action taken\n");
153     End_IRQ(state);
154     return;
155   }
156
157   // End_IRQ(state);
158 }
159 */
160 //
161 //
162 // I really don't know what the heck this is doing... PAD
163 //
164 /*
165 int hook_irq_stub(struct guest_info * info, int irq) {
166   if (irq_map[irq]) {
167     return -1;
168   }
169
170   SerialPrint("Hooking IRQ: %d (vm=0x%x)\n", irq, info);
171   irq_map[irq] = info;
172   volatile void *foo = pic_intr_handler;
173
174   // This is disabled for the time being 
175   foo = 0;
176
177
178   Disable_IRQ(irq);
179   Install_IRQ(irq, pic_intr_handler);
180   Enable_IRQ(irq);
181   return 0;
182 }
183 */
184
185 int geekos_hook_interrupt_new(uint_t irq, void * opaque)
186 {
187   if (my_opaque[irq]) { 
188     PrintBoth("Attempt to hook interrupt that is already hooked\n");
189     return -1;
190   } else {
191     PrintBoth("Hooked interrupt 0x%x with opaque 0x%x\n",irq,opaque);
192     my_opaque[irq]=opaque;
193   }
194
195   Disable_IRQ(irq);
196   Install_IRQ(irq,translate_intr_handler);
197   Enable_IRQ(irq);
198   return 0;
199 }
200
201
202 int ack_irq(int irq) {
203   End_IRQ_num(irq);
204   return 0;
205 }
206
207   
208 void Init_Stubs() {
209   memset(irq_map, 0, sizeof(struct guest_info *) * 256);
210 }
211
212
213 unsigned int get_cpu_khz() {
214   extern uint_t cpu_khz_freq;
215
216   unsigned long  print_khz = (unsigned long)(cpu_khz_freq & 0xffffffff);
217   
218   PrintBoth("Detected %lu.%lu MHz CPU\n", print_khz / 1000, print_khz % 1000);
219
220   return cpu_khz_freq;
221 }
222