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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <geekos/vmm_stubs.h>
24 #include <geekos/debug.h>
25 #include <geekos/serial.h>
26 #include <geekos/vm.h>
27 #include <geekos/screen.h>
29 #include <palacios/vmm.h>
30 #include <palacios/vmm_io.h>
33 extern void * g_ramdiskImage;
34 extern ulong_t s_ramdiskSize;
37 #define SPEAKER_PORT 0x61
39 static inline void VM_Out_Byte(ushort_t port, uchar_t value)
41 __asm__ __volatile__ (
44 : "a" (value), "Nd" (port)
49 * Read a byte from an I/O port.
51 static inline uchar_t VM_In_Byte(ushort_t port)
55 __asm__ __volatile__ (
67 int IO_Read(ushort_t port, void * dst, uint_t length, void * priv_data) {
73 *(uchar_t*)dst = VM_In_Byte(port);
79 int IO_Write(ushort_t port, void * src, uint_t length, void * priv_data) {
85 VM_Out_Byte(port, *(uchar_t *)src);
91 int IO_Read_to_Serial(ushort_t port, void * dst, uint_t length, void * priv_data) {
92 PrintBoth("Input from Guest on port %d (0x%x) Length=%d\n", port, port, length);
98 char * bochs_debug_buf = NULL;
99 int bochs_debug_offset = 0;
101 char * bochs_info_buf = NULL;
102 int bochs_info_offset = 0;
105 int IO_BOCHS_debug(ushort_t port, void * src, uint_t length, void * priv_data) {
106 if (!bochs_debug_buf) {
107 bochs_debug_buf = (char*)Malloc(1024);
110 bochs_debug_buf[bochs_debug_offset++] = *(char*)src;
112 if ((*(char*)src == 0xa) || (bochs_debug_offset == 1023)) {
113 SerialPrint("BOCHSDEBUG>%s", bochs_debug_buf);
114 memset(bochs_debug_buf, 0, 1024);
115 bochs_debug_offset = 0;
121 int IO_BOCHS_info(ushort_t port, void * src, uint_t length, void * priv_data) {
122 if (!bochs_info_buf) {
123 bochs_info_buf = (char*)Malloc(1024);
126 bochs_info_buf[bochs_info_offset++] = *(char*)src;
128 if ((*(char*)src == 0xa) || (bochs_info_offset == 1023)) {
129 SerialPrint("BOCHSINFO>%s", bochs_info_buf);
130 memset(bochs_info_buf, 0, 1024);
131 bochs_info_offset = 0;
138 int IO_Write_to_Serial(ushort_t port, void * src, uint_t length, void * priv_data) {
139 SerialPrint("Output from Guest on port %d (0x%x) Length=%d\n", port, port, length);
143 SerialPrint(">0x%.2x\n", *(char*)src);
146 SerialPrint(">0x%.4x\n", *(ushort_t*)src);
149 SerialPrint(">0x%.8x\n", *(uint_t*)src);
155 // SerialMemDump(src, length);
168 __asm__ __volatile__ (
174 PrintBoth("Starting To Buzz\n");
176 init=VM_In_Byte(SPEAKER_PORT);
179 VM_Out_Byte(SPEAKER_PORT, init|0x2);
180 for (j=0;j<1000000;j++) {
183 VM_Out_Byte(SPEAKER_PORT, init);
184 for (j=0;j<1000000;j++) {
192 int passthrough_mem_read(void * guest_addr, void * dst, uint_t length, void * priv_data) {
193 memcpy(dst, (void*)guest_addr, length);
197 int passthrough_mem_write(void * guest_addr, void * src, uint_t length, void * priv_data) {
198 memcpy((void*)guest_addr, src, length);
204 /* We need a configuration mechanism, so we can wrap this completely inside the VMM code,
205 * with no pollution into the HOST OS
208 int RunVMM(struct Boot_Info * bootInfo) {
209 struct vmm_os_hooks os_hooks;
210 struct vmm_ctrl_ops vmm_ops;
211 struct guest_info * vm_info = 0;
212 struct v3_vm_config vm_config;
216 memset(&os_hooks, 0, sizeof(struct vmm_os_hooks));
217 memset(&vmm_ops, 0, sizeof(struct vmm_ctrl_ops));
218 memset(&vm_config, 0, sizeof(struct v3_vm_config));
221 os_hooks.print_debug = &SerialPrint;
222 os_hooks.print_info = &Print;
223 os_hooks.print_trace = &SerialPrint;
224 os_hooks.allocate_pages = &Allocate_VMM_Pages;
225 os_hooks.free_page = &Free_VMM_Page;
226 os_hooks.malloc = &VMM_Malloc;
227 os_hooks.free = &VMM_Free;
228 os_hooks.vaddr_to_paddr = &Identity;
229 os_hooks.paddr_to_vaddr = &Identity;
230 os_hooks.hook_interrupt = &geekos_hook_interrupt;
231 os_hooks.ack_irq = &ack_irq;
232 os_hooks.get_cpu_khz = &get_cpu_khz;
236 Init_V3(&os_hooks, &vmm_ops);
239 extern char _binary___palacios_vm_kernel_start;
240 PrintBoth(" Guest Load Addr: 0x%x\n", &_binary___palacios_vm_kernel_start);
242 vm_config.vm_kernel = &_binary___palacios_vm_kernel_start;
245 if (g_ramdiskImage != NULL) {
246 vm_config.use_ramdisk = 1;
247 vm_config.ramdisk = g_ramdiskImage;
248 vm_config.ramdisk_size = s_ramdiskSize;
254 vm_info = (vmm_ops).allocate_guest();
258 PrintBoth("Allocated Guest\n");
260 (vmm_ops).config_guest(vm_info, &vm_config);
262 PrintBoth("Configured guest\n");
265 //v3_hook_io_port(&vm_info, 0x05, &IO_Read, &IO_Write_to_Serial, NULL);
267 //v3_hook_io_port(vm_info, 0x61, &IO_Read, &IO_Write, NULL);
268 //v3_hook_io_port(vm_info, 0x400, &IO_Read, &IO_Write_to_Serial, NULL);
269 //v3_hook_io_port(vm_info, 0x401, &IO_Read, &IO_Write_to_Serial, NULL);
270 //v3_hook_io_port(vm_info, 0x402, &IO_Read, &IO_BOCHS_info, NULL);
271 //v3_hook_io_port(vm_info, 0x403, &IO_Read, &IO_BOCHS_debug, NULL);
274 (vmm_ops).init_guest(vm_info);
275 PrintBoth("Starting Guest\n");
278 (vmm_ops).start_guest(vm_info);