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 emulator
[palacios.git] / palacios / src / geekos / vm.c
1 #include <geekos/vmm_stubs.h>
2
3 #include <geekos/debug.h>
4 #include <geekos/serial.h>
5 #include <geekos/vm.h>
6 #include <geekos/screen.h>
7
8 #include <palacios/vmm.h>
9 #include <palacios/vmm_io.h>
10
11
12
13 #define SPEAKER_PORT 0x61
14
15 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
16 {
17     __asm__ __volatile__ (
18         "outb %b0, %w1"
19         :
20         : "a" (value), "Nd" (port)
21     );
22 }
23
24 /*
25  * Read a byte from an I/O port.
26  */
27 static inline uchar_t VM_In_Byte(ushort_t port)
28 {
29     uchar_t value;
30
31     __asm__ __volatile__ (
32         "inb %w1, %b0"
33         : "=a" (value)
34         : "Nd" (port)
35     );
36
37     return value;
38 }
39
40
41
42
43 int IO_Read(ushort_t port, void * dst, uint_t length, void * priv_data) {
44
45   if (length != 1) {
46     return 0;
47   }
48
49   *(uchar_t*)dst = VM_In_Byte(port);    
50   return 1;
51 }
52
53
54
55 int IO_Write(ushort_t port, void * src, uint_t length, void * priv_data) {
56
57   if (length != 1) {
58     return 0;
59   }
60
61   VM_Out_Byte(port, *(uchar_t *)src);    
62
63   return 1;
64 }
65
66
67 int IO_Read_to_Serial(ushort_t port, void * dst, uint_t length, void * priv_data) {
68   PrintBoth("Input from Guest on port %d (0x%x) Length=%d\n", port, port, length);
69   
70   return 0;
71 }
72
73
74 char * bochs_debug_buf = NULL;
75 int bochs_debug_offset = 0;
76
77 char * bochs_info_buf = NULL;
78 int bochs_info_offset = 0;
79
80
81 int IO_BOCHS_debug(ushort_t port, void * src, uint_t length, void * priv_data) {
82   if (!bochs_debug_buf) {
83     bochs_debug_buf = (char*)Malloc(1024);
84   }
85
86   bochs_debug_buf[bochs_debug_offset++] = *(char*)src;
87
88   if ((*(char*)src == 0xa) ||  (bochs_debug_offset == 1023)) {
89     SerialPrint("BOCHSDEBUG>%s", bochs_debug_buf);
90     memset(bochs_debug_buf, 0, 1024);
91     bochs_debug_offset = 0;
92   }
93
94   return length;
95 }
96
97 int IO_BOCHS_info(ushort_t port, void * src, uint_t length, void * priv_data) {
98   if (!bochs_info_buf) {
99     bochs_info_buf = (char*)Malloc(1024);
100   }
101
102   bochs_info_buf[bochs_info_offset++] = *(char*)src;
103
104   if ((*(char*)src == 0xa) ||  (bochs_info_offset == 1023)) {
105     SerialPrint("BOCHSINFO>%s", bochs_info_buf);
106     memset(bochs_info_buf, 0, 1024);
107     bochs_info_offset = 0;
108   }
109
110   return length;
111 }
112
113
114 int IO_Write_to_Serial(ushort_t port, void * src, uint_t length, void * priv_data) {
115  SerialPrint("Output from Guest on port %d (0x%x) Length=%d\n", port, port, length);
116   switch (length) {
117
118   case 1:
119     SerialPrint(">0x%.2x\n", *(char*)src);
120     break;
121   case 2:
122     SerialPrint(">0x%.4x\n", *(ushort_t*)src);
123     break;
124   case 4:
125     SerialPrint(">0x%.8x\n", *(uint_t*)src);
126     break;
127   default:
128     break;
129   }
130
131   //  SerialMemDump(src, length);
132   return length;
133 }
134
135
136
137 void BuzzVM()
138 {
139   int x;
140   int j;
141   unsigned char init;
142
143 #if 0  
144   __asm__ __volatile__ (
145     "popf"
146     );
147     
148 #endif
149     
150   PrintBoth("Starting To Buzz\n");
151
152   init=VM_In_Byte(SPEAKER_PORT);
153
154   while (1) {
155     VM_Out_Byte(SPEAKER_PORT, init|0x2);
156     for (j=0;j<1000000;j++) { 
157       x+=j;
158     }
159     VM_Out_Byte(SPEAKER_PORT, init);
160     for (j=0;j<1000000;j++) { 
161       x+=j;
162     }
163   }
164 }
165
166
167 /*
168 int passthrough_mem_read(void * guest_addr, void * dst, uint_t length, void * priv_data) {
169   memcpy(dst, (void*)guest_addr, length);
170   return length;
171 }
172
173 int passthrough_mem_write(void * guest_addr, void * src, uint_t length, void * priv_data) {
174   memcpy((void*)guest_addr, src, length);
175   return length;
176 }
177 */
178
179
180 /* We need a configuration mechanism, so we can wrap this completely inside the VMM code, 
181  * with no pollution into the HOST OS
182  */
183
184 int RunVMM(struct Boot_Info * bootInfo) {
185   void * config_data;
186
187   struct vmm_os_hooks os_hooks;
188   struct vmm_ctrl_ops vmm_ops;
189   struct guest_info * vm_info = 0;
190   
191
192
193   
194   memset(&os_hooks, 0, sizeof(struct vmm_os_hooks));
195   memset(&vmm_ops, 0, sizeof(struct vmm_ctrl_ops));
196
197   
198   os_hooks.print_debug = &SerialPrint;
199   os_hooks.print_info = &Print;
200   os_hooks.print_trace = &SerialPrint;
201   os_hooks.allocate_pages = &Allocate_VMM_Pages;
202   os_hooks.free_page = &Free_VMM_Page;
203   os_hooks.malloc = &VMM_Malloc;
204   os_hooks.free = &VMM_Free;
205   os_hooks.vaddr_to_paddr = &Identity;
206   os_hooks.paddr_to_vaddr = &Identity;
207   os_hooks.hook_interrupt = &geekos_hook_interrupt_new;
208   os_hooks.ack_irq = &ack_irq;
209   os_hooks.get_cpu_khz = &get_cpu_khz;
210
211
212   
213   Init_V3(&os_hooks, &vmm_ops);
214
215   extern char _binary_vm_kernel_start;
216   PrintBoth(" Guest Load Addr: 0x%x\n", &_binary_vm_kernel_start);
217   
218   config_data = &_binary_vm_kernel_start;
219
220   vm_info = (vmm_ops).allocate_guest();
221
222   PrintBoth("Allocated Guest\n");
223
224   (vmm_ops).config_guest(vm_info, config_data);
225
226   PrintBoth("Configured guest\n");
227   
228   v3_hook_io_port(vm_info, 0x61, &IO_Read, &IO_Write, NULL);
229   //v3_hook_io_port(&vm_info, 0x05, &IO_Read, &IO_Write_to_Serial, NULL);
230   
231   
232   v3_hook_io_port(vm_info, 0x400, &IO_Read, &IO_Write_to_Serial, NULL);
233   v3_hook_io_port(vm_info, 0x401, &IO_Read, &IO_Write_to_Serial, NULL);
234   v3_hook_io_port(vm_info, 0x402, &IO_Read, &IO_BOCHS_info, NULL);
235   v3_hook_io_port(vm_info, 0x403, &IO_Read, &IO_BOCHS_debug, NULL);
236   
237
238   (vmm_ops).init_guest(vm_info);
239   PrintBoth("Starting Guest\n");
240   //Clear_Screen();
241   (vmm_ops).start_guest(vm_info);
242   
243     return 0;
244 }