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