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.


code restructuring
[palacios.git] / palacios / src / geekos / vm.c
1 #include <geekos/vmm_stubs.h>
2 #include <palacios/vmm.h>
3 #include <geekos/debug.h>
4 #include <geekos/serial.h>
5
6
7 #define SPEAKER_PORT 0x61
8
9
10
11
12 inline void VM_Out_Byte(ushort_t port, uchar_t value)
13 {
14     __asm__ __volatile__ (
15         "outb %b0, %w1"
16         :
17         : "a" (value), "Nd" (port)
18     );
19 }
20
21 /*
22  * Read a byte from an I/O port.
23  */
24 inline uchar_t VM_In_Byte(ushort_t port)
25 {
26     uchar_t value;
27
28     __asm__ __volatile__ (
29         "inb %w1, %b0"
30         : "=a" (value)
31         : "Nd" (port)
32     );
33
34     return value;
35 }
36
37
38
39 int IO_Read(ushort_t port, void * dst, uint_t length) {
40   uchar_t * iter = dst;
41   uint_t i;
42
43   for (i = 0; i < length; i++) {
44     *iter = VM_In_Byte(port);    
45     iter++;
46   }
47   
48   return 0;
49 }
50
51
52
53 int IO_Write(ushort_t port, void * src, uint_t length) {
54   uchar_t * iter = src;
55   uint_t i;
56
57
58   for (i = 0; i < length; i++) {
59     VM_Out_Byte(port, *iter);    
60     iter++;
61   }
62
63   return 0;
64 }
65
66
67
68 int IO_Write_to_Serial(ushort_t port, void * src, uint_t length) {
69   PrintBoth("Output from Guest on port %d (0x%x) Length=%d\n", port, port, length);
70   switch (length) {
71
72   case 1:
73     PrintBoth(">0x%.2x\n", *(char*)src);
74     break;
75   case 2:
76     PrintBoth(">0x%.4x\n", *(ushort_t*)src);
77     break;
78   case 4:
79     PrintBoth(">0x%.8x\n", *(uint_t*)src);
80     break;
81   default:
82     break;
83   }
84
85   //  SerialMemDump(src, length);
86   return length;
87 }
88
89
90
91 void BuzzVM()
92 {
93   int x;
94   int j;
95   unsigned char init;
96
97 #if 0  
98   __asm__ __volatile__ (
99     "popf"
100     );
101     
102 #endif
103     
104   PrintBoth("Starting To Buzz\n");
105
106   init=VM_In_Byte(SPEAKER_PORT);
107
108   while (1) {
109     VM_Out_Byte(SPEAKER_PORT, init|0x2);
110     for (j=0;j<1000000;j++) { 
111       x+=j;
112     }
113     VM_Out_Byte(SPEAKER_PORT, init);
114     for (j=0;j<1000000;j++) { 
115       x+=j;
116     }
117   }
118 }
119
120
121
122
123
124
125 int RunVMM() {
126
127     struct vmm_os_hooks os_hooks;
128     struct vmm_ctrl_ops vmm_ops;
129     struct guest_info vm_info;
130     addr_t rsp;
131     addr_t rip;
132
133     memset(&os_hooks, 0, sizeof(struct vmm_os_hooks));
134     memset(&vmm_ops, 0, sizeof(struct vmm_ctrl_ops));
135     memset(&vm_info, 0, sizeof(struct guest_info));
136
137     os_hooks.print_debug = &PrintBoth;
138     os_hooks.print_info = &Print;
139     os_hooks.print_trace = &SerialPrint;
140     os_hooks.allocate_pages = &Allocate_VMM_Pages;
141     os_hooks.free_page = &Free_VMM_Page;
142     os_hooks.malloc = &VMM_Malloc;
143     os_hooks.free = &VMM_Free;
144     os_hooks.vaddr_to_paddr = &Identity;
145     os_hooks.paddr_to_vaddr = &Identity;
146
147
148     //   DumpGDT();
149     Init_VMM(&os_hooks, &vmm_ops);
150   
151     init_shadow_map(&(vm_info.mem_map));
152     init_shadow_page_state(&(vm_info.shdw_pg_state));
153     vm_info.page_mode = SHADOW_PAGING;
154
155     vm_info.cpu_mode = REAL;
156
157     init_vmm_io_map(&(vm_info.io_map));
158
159     
160     if (0) {
161       
162       //    add_shared_mem_range(&(vm_info.mem_layout), 0, 0x800000, 0x10000);    
163       //    add_shared_mem_range(&(vm_info.mem_layout), 0, 0x1000000, 0);
164       
165       rip = (ulong_t)(void*)&BuzzVM;
166       //  rip -= 0x10000;
167       //    rip = (addr_t)(void*)&exit_test;
168       //  rip -= 0x2000;
169       vm_info.rip = rip;
170       rsp = (addr_t)Alloc_Page();
171       
172       vm_info.vm_regs.rsp = (rsp +4092 );// - 0x2000;
173       
174             
175     } else {
176       //add_shared_mem_range(&(vm_info.mem_layout), 0x0, 0x1000, 0x100000);
177       //      add_shared_mem_range(&(vm_info.mem_layout), 0x0, 0x100000, 0x0);
178       
179       shadow_region_t *ent = Malloc(sizeof(shadow_region_t));;
180       init_shadow_region_physical(ent,0,0x100000,GUEST_REGION_PHYSICAL_MEMORY,
181                                   0x100000, HOST_REGION_PHYSICAL_MEMORY);
182       add_shadow_region(&(vm_info.mem_map),ent);
183
184       hook_io_port(&(vm_info.io_map), 0x61, &IO_Read, &IO_Write);
185       hook_io_port(&(vm_info.io_map), 0x05, &IO_Read, &IO_Write_to_Serial);
186       
187       /*
188       vm_info.cr0 = 0;
189       vm_info.cs.base=0xf000;
190       vm_info.cs.limit=0xffff;
191       */
192       //vm_info.rip = 0xfff0;
193
194       vm_info.rip = 0;
195       vm_info.vm_regs.rsp = 0x0;
196     }
197
198     PrintBoth("Initializing Guest (eip=0x%.8x) (esp=0x%.8x)\n", (uint_t)vm_info.rip,(uint_t)vm_info.vm_regs.rsp);
199     (vmm_ops).init_guest(&vm_info);
200     PrintBoth("Starting Guest\n");
201     (vmm_ops).start_guest(&vm_info);
202
203     return 0;
204
205 }