1 #include <geekos/vmm_stubs.h>
2 #include <palacios/vmm.h>
3 #include <geekos/debug.h>
4 #include <geekos/serial.h>
7 #define SPEAKER_PORT 0x61
12 inline void VM_Out_Byte(ushort_t port, uchar_t value)
14 __asm__ __volatile__ (
17 : "a" (value), "Nd" (port)
22 * Read a byte from an I/O port.
24 inline uchar_t VM_In_Byte(ushort_t port)
28 __asm__ __volatile__ (
39 int IO_Read(ushort_t port, void * dst, uint_t length) {
43 for (i = 0; i < length; i++) {
44 *iter = VM_In_Byte(port);
53 int IO_Write(ushort_t port, void * src, uint_t length) {
58 for (i = 0; i < length; i++) {
59 VM_Out_Byte(port, *iter);
67 int IO_Read_to_Serial(ushort_t port, void * dst, uint_t length) {
68 PrintBoth("Input from Guest on port %d (0x%x) Length=%d\n", port, port, length);
75 int IO_Write_to_Serial(ushort_t port, void * src, uint_t length) {
76 PrintBoth("Output from Guest on port %d (0x%x) Length=%d\n", port, port, length);
80 PrintBoth(">0x%.2x\n", *(char*)src);
83 PrintBoth(">0x%.4x\n", *(ushort_t*)src);
86 PrintBoth(">0x%.8x\n", *(uint_t*)src);
92 // SerialMemDump(src, length);
105 __asm__ __volatile__ (
111 PrintBoth("Starting To Buzz\n");
113 init=VM_In_Byte(SPEAKER_PORT);
116 VM_Out_Byte(SPEAKER_PORT, init|0x2);
117 for (j=0;j<1000000;j++) {
120 VM_Out_Byte(SPEAKER_PORT, init);
121 for (j=0;j<1000000;j++) {
132 int RunVMM(struct Boot_Info * bootInfo) {
134 struct vmm_os_hooks os_hooks;
135 struct vmm_ctrl_ops vmm_ops;
136 struct guest_info vm_info;
140 memset(&os_hooks, 0, sizeof(struct vmm_os_hooks));
141 memset(&vmm_ops, 0, sizeof(struct vmm_ctrl_ops));
142 memset(&vm_info, 0, sizeof(struct guest_info));
144 os_hooks.print_debug = &PrintBoth;
145 os_hooks.print_info = &Print;
146 os_hooks.print_trace = &SerialPrint;
147 os_hooks.allocate_pages = &Allocate_VMM_Pages;
148 os_hooks.free_page = &Free_VMM_Page;
149 os_hooks.malloc = &VMM_Malloc;
150 os_hooks.free = &VMM_Free;
151 os_hooks.vaddr_to_paddr = &Identity;
152 os_hooks.paddr_to_vaddr = &Identity;
156 Init_VMM(&os_hooks, &vmm_ops);
158 init_shadow_map(&(vm_info.mem_map));
159 init_shadow_page_state(&(vm_info.shdw_pg_state));
160 vm_info.page_mode = SHADOW_PAGING;
162 vm_info.cpu_mode = REAL;
164 init_vmm_io_map(&(vm_info.io_map));
169 // add_shared_mem_range(&(vm_info.mem_layout), 0, 0x800000, 0x10000);
170 // add_shared_mem_range(&(vm_info.mem_layout), 0, 0x1000000, 0);
172 rip = (ulong_t)(void*)&BuzzVM;
174 // rip = (addr_t)(void*)&exit_test;
177 rsp = (addr_t)Alloc_Page();
179 vm_info.vm_regs.rsp = (rsp +4092 );// - 0x2000;
183 //add_shared_mem_range(&(vm_info.mem_layout), 0x0, 0x1000, 0x100000);
184 // add_shared_mem_range(&(vm_info.mem_layout), 0x0, 0x100000, 0x0);
186 shadow_region_t *ent = Malloc(sizeof(shadow_region_t));;
187 init_shadow_region_physical(ent,0,0x100000,GUEST_REGION_PHYSICAL_MEMORY,
188 0x100000, HOST_REGION_PHYSICAL_MEMORY);
189 add_shadow_region(&(vm_info.mem_map),ent);
191 hook_io_port(&(vm_info.io_map), 0x61, &IO_Read, &IO_Write);
192 hook_io_port(&(vm_info.io_map), 0x05, &IO_Read, &IO_Write_to_Serial);
196 vm_info.cs.base=0xf000;
197 vm_info.cs.limit=0xffff;
199 //vm_info.rip = 0xfff0;
202 vm_info.vm_regs.rsp = 0x0;
204 shadow_region_t *ent = Malloc(sizeof(shadow_region_t));
206 init_shadow_region_physical(ent,0xf0000,0x100000,GUEST_REGION_PHYSICAL_MEMORY,
207 0x100000, HOST_REGION_PHYSICAL_MEMORY);
208 add_shadow_region(&(vm_info.mem_map),ent);
209 ent = Malloc(sizeof(shadow_region_t));
211 void * guest_mem = Allocate_VMM_Pages(256);
213 PrintDebug("Guest Size: %lu\n", bootInfo->guest_size);
215 memcpy((void *)(guest_mem + 0xf0000), (void *)0x100000, bootInfo->guest_size);
218 SerialMemDump((unsigned char *)(guest_mem + 0xffff0), 16);
220 init_shadow_region_physical(ent, 0x0, 0x100000, GUEST_REGION_PHYSICAL_MEMORY,
221 (addr_t)guest_mem, HOST_REGION_PHYSICAL_MEMORY);
222 add_shadow_region(&(vm_info.mem_map),ent);
224 hook_io_port(&(vm_info.io_map), 0x61, &IO_Read, &IO_Write);
225 hook_io_port(&(vm_info.io_map), 0x05, &IO_Read, &IO_Write_to_Serial);
227 hook_io_port(&(vm_info.io_map), 0x20, &IO_Read, &IO_Write_to_Serial);
228 hook_io_port(&(vm_info.io_map), 0x21, &IO_Read, &IO_Write_to_Serial);
229 hook_io_port(&(vm_info.io_map), 0xa0, &IO_Read, &IO_Write_to_Serial);
230 hook_io_port(&(vm_info.io_map), 0xa1, &IO_Read, &IO_Write_to_Serial);
232 vm_info.rip = 0xfff0;
233 vm_info.vm_regs.rsp = 0x0;
236 PrintBoth("Initializing Guest (eip=0x%.8x) (esp=0x%.8x)\n", (uint_t)vm_info.rip,(uint_t)vm_info.vm_regs.rsp);
237 (vmm_ops).init_guest(&vm_info);
238 PrintBoth("Starting Guest\n");
239 (vmm_ops).start_guest(&vm_info);