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 guest vm
[palacios.git] / misc / test_vm / src / geekos / main.c
1 /*
2  * GeekOS C code entry point
3  * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4  * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
5  * Copyright (c) 2004, Iulian Neamtiu <neamtiu@cs.umd.edu>
6  * $Revision: 1.1 $
7  * 
8  * This is free software.  You are permitted to use,
9  * redistribute, and modify it as specified in the file "COPYING".
10  */
11
12 #include <geekos/bootinfo.h>
13 #include <geekos/string.h>
14 #include <geekos/screen.h>
15 #include <geekos/mem.h>
16 #include <geekos/crc32.h>
17 #include <geekos/tss.h>
18 #include <geekos/int.h>
19 #include <geekos/kthread.h>
20 #include <geekos/trap.h>
21 #include <geekos/timer.h>
22 #include <geekos/keyboard.h>
23 #include <geekos/io.h>
24 #include <geekos/serial.h>
25 #include <geekos/reboot.h>
26 #include <geekos/mem.h>
27 #include <geekos/paging.h>
28 #include <geekos/ide.h>
29
30 #include <geekos/gdt.h>
31
32
33 /*
34   static inline unsigned int cpuid_ecx(unsigned int op)
35   {
36   unsigned int eax, ecx;
37   
38   __asm__("cpuid"
39   : "=a" (eax), "=c" (ecx)
40   : "0" (op)
41   : "bx", "dx" );
42   
43   return ecx;
44   }
45 */
46
47
48
49 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
50 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
51 extern uint_t Get_EIP();
52 extern uint_t Get_ESP();
53 extern uint_t Get_EBP();
54
55
56 int foo=42;
57
58 #define SPEAKER_PORT 0x61
59
60
61 void Buzz(unsigned delay, unsigned num)
62 {
63   volatile int x;
64   int i,j;
65   unsigned char init;
66   
67   init=In_Byte(SPEAKER_PORT);
68
69   for (i=0;i<num;i++) { 
70     Out_Byte(SPEAKER_PORT, init|0x2);
71     for (j=0;j<delay;j++) { 
72       x+=j;
73     }
74     Out_Byte(SPEAKER_PORT, init);
75     for (j=0;j<delay;j++) { 
76       x+=j;
77     }
78   }
79 }
80
81 inline void MyOut_Byte(ushort_t port, uchar_t value)
82 {
83     __asm__ __volatile__ (
84         "outb %b0, %w1"
85         :
86         : "a" (value), "Nd" (port)
87     );
88 }
89
90 /*
91  * Read a byte from an I/O port.
92  */
93 inline uchar_t MyIn_Byte(ushort_t port)
94 {
95     uchar_t value;
96
97     __asm__ __volatile__ (
98         "inb %w1, %b0"
99         : "=a" (value)
100         : "Nd" (port)
101     );
102
103     return value;
104 }
105
106
107 extern void MyBuzzVM();
108
109 #define MYBUZZVM_START MyBuzzVM
110 #define MYBUZZVM_LEN   0x3d
111
112 void BuzzVM()
113 {
114   int x;
115   int j;
116   unsigned char init;
117   
118   
119   init=MyIn_Byte(SPEAKER_PORT);
120
121   while (1) {
122     MyOut_Byte(SPEAKER_PORT, init|0x2);
123     for (j=0;j<1000000;j++) { 
124       x+=j;
125     }
126     MyOut_Byte(SPEAKER_PORT, init);
127     for (j=0;j<1000000;j++) { 
128       x+=j;
129     }
130   }
131 }
132
133
134
135 extern void RunVM();
136
137 int vmRunning = 0;
138
139 void RunVM() {
140   vmRunning = 1;
141
142   while(1);
143 }
144
145
146
147
148
149 void Buzzer(ulong_t arg) {
150   ulong_t *doIBuzz = (ulong_t*)arg;
151   while (1) {
152     // Quick and dirty hack to save my hearing...
153     // I'm not too worried about timing, so I'll deal with concurrency later...
154     if (*doIBuzz == 1) {
155       Buzz(1000000, 10);
156     }
157   }
158
159 }
160
161
162
163 void Hello(ulong_t arg)
164 {
165   char *b="hello ";
166   char byte;
167   short port=0xe9;
168   int i;
169   while(1){
170     for (i=0;i<6;i++) { 
171       byte=b[i];
172       __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
173     }
174   }
175 }
176
177 void Keyboard_Listener(ulong_t arg) {
178   ulong_t * doIBuzz = (ulong_t*)arg;
179   Keycode key_press;
180
181   Print("Press F4 to turn on/off the speaker\n");
182
183   while ((key_press = Wait_For_Key())) {    
184     if (key_press == KEY_F4) {
185       Print("\nToggling Speaker Port\n");
186       SerialPrintLevel(100,"\nToggling Speaker Port\n");
187       *doIBuzz = (*doIBuzz + 1) % 2;
188     } else if (key_press == KEY_F5) {
189       Print("\nMachine Restart\n");
190       SerialPrintLevel(100,"\nMachine Restart\n");
191       machine_real_restart();
192     }
193   }
194   return;
195 }
196
197
198
199 extern char BSS_START, BSS_END;
200
201 extern char end;
202
203
204
205
206 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
207 {
208   uint_t address;
209
210   for (address=start;address<start+length;address+=PAGE_SIZE) { 
211     void *page;
212     pte_t pte = template_pte;
213     
214     page=Alloc_Page();
215     KASSERT(page);
216     
217     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
218
219     KASSERT(MapPage((void*)address,&pte,1));
220   }
221   
222   return 0;
223 }
224     
225
226
227 /*
228  * Kernel C code entry point.
229  * Initializes kernel subsystems, mounts filesystems,
230  * and spawns init process.
231  */
232 void Main(struct Boot_Info* bootInfo)
233 {
234   struct Kernel_Thread * key_thread;
235   struct Kernel_Thread * spkr_thread;
236
237
238
239   ulong_t doIBuzz = 0;
240
241   Init_BSS();
242   Init_Screen();
243   InitSerial();
244   Init_Mem(bootInfo);
245   Init_CRC32();
246   Init_TSS();
247   Init_Interrupts();
248   Init_Scheduler();
249   Init_Traps();
250   Init_Timer();
251   Init_Keyboard();
252   Init_VM(bootInfo);
253   Init_Paging();
254
255   //  Init_IDE();
256
257
258
259
260
261
262
263
264   SerialPrint("\n\nHello, Welcome to this horrid output-only serial interface\n");
265   SerialPrint("Eventually, this will let us control the VMM\n\n");
266  
267   SerialPrint("\n\n===>");
268   
269
270
271   
272
273   
274   SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
275   
276   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
277   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
278
279
280
281
282
283   SerialPrintLevel(1000,"Next: setup GDT\n");
284
285
286   while(1);
287   
288   /* Now this thread is done. */
289   Exit(0);
290 }