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.


9d48319fc7da03c54dd5651d765372bd66bcce8f
[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.2 $
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
57 extern void Invalidate_PG(void * addr);
58
59
60 int foo=42;
61
62 #define SPEAKER_PORT 0x61
63
64
65 void Buzz(unsigned delay, unsigned num)
66 {
67   volatile int x;
68   int i,j;
69   unsigned char init;
70   
71   init=In_Byte(SPEAKER_PORT);
72
73   for (i=0;i<num;i++) { 
74     Out_Byte(SPEAKER_PORT, init|0x2);
75     for (j=0;j<delay;j++) { 
76       x+=j;
77     }
78     Out_Byte(SPEAKER_PORT, init);
79     for (j=0;j<delay;j++) { 
80       x+=j;
81     }
82   }
83 }
84
85 inline void MyOut_Byte(ushort_t port, uchar_t value)
86 {
87     __asm__ __volatile__ (
88         "outb %b0, %w1"
89         :
90         : "a" (value), "Nd" (port)
91     );
92 }
93
94 /*
95  * Read a byte from an I/O port.
96  */
97 inline uchar_t MyIn_Byte(ushort_t port)
98 {
99     uchar_t value;
100
101     __asm__ __volatile__ (
102         "inb %w1, %b0"
103         : "=a" (value)
104         : "Nd" (port)
105     );
106
107     return value;
108 }
109
110
111 extern void MyBuzzVM();
112
113 #define MYBUZZVM_START MyBuzzVM
114 #define MYBUZZVM_LEN   0x3d
115
116 void BuzzVM()
117 {
118   int x;
119   int j;
120   unsigned char init;
121   
122   
123   init=MyIn_Byte(SPEAKER_PORT);
124
125   while (1) {
126     MyOut_Byte(SPEAKER_PORT, init|0x2);
127     for (j=0;j<1000000;j++) { 
128       x+=j;
129     }
130     MyOut_Byte(SPEAKER_PORT, init);
131     for (j=0;j<1000000;j++) { 
132       x+=j;
133     }
134   }
135 }
136
137
138
139 extern void RunVM();
140
141 int vmRunning = 0;
142
143 void RunVM() {
144   vmRunning = 1;
145
146   while(1);
147 }
148
149
150
151
152
153 void Buzzer(ulong_t arg) {
154   ulong_t *doIBuzz = (ulong_t*)arg;
155   while (1) {
156     // Quick and dirty hack to save my hearing...
157     // I'm not too worried about timing, so I'll deal with concurrency later...
158     if (*doIBuzz == 1) {
159       Buzz(1000000, 10);
160     }
161   }
162
163 }
164
165
166
167 void Hello(ulong_t arg)
168 {
169   char *b="hello ";
170   char byte;
171   short port=0xe9;
172   int i;
173   while(1){
174     for (i=0;i<6;i++) { 
175       byte=b[i];
176       __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
177     }
178   }
179 }
180
181 void Keyboard_Listener(ulong_t arg) {
182   ulong_t * doIBuzz = (ulong_t*)arg;
183   Keycode key_press;
184
185   Print("Press F4 to turn on/off the speaker\n");
186
187   while ((key_press = Wait_For_Key())) {    
188     if (key_press == KEY_F4) {
189       Print("\nToggling Speaker Port\n");
190       SerialPrintLevel(100,"\nToggling Speaker Port\n");
191       *doIBuzz = (*doIBuzz + 1) % 2;
192     } else if (key_press == KEY_F5) {
193       Print("\nMachine Restart\n");
194       SerialPrintLevel(100,"\nMachine Restart\n");
195       machine_real_restart();
196     }
197   }
198   return;
199 }
200
201
202
203 extern char BSS_START, BSS_END;
204
205 extern char end;
206
207
208
209
210 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
211 {
212   uint_t address;
213
214   for (address=start;address<start+length;address+=PAGE_SIZE) { 
215     void *page;
216     pte_t pte = template_pte;
217     
218     page=Alloc_Page();
219     KASSERT(page);
220     
221     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
222
223     KASSERT(MapPage((void*)address,&pte,1));
224   }
225   
226   return 0;
227 }
228     
229
230
231 /*
232  * Kernel C code entry point.
233  * Initializes kernel subsystems, mounts filesystems,
234  * and spawns init process.
235  */
236 void Main(struct Boot_Info* bootInfo)
237 {
238   struct Kernel_Thread * key_thread;
239   struct Kernel_Thread * spkr_thread;
240
241
242
243   ulong_t doIBuzz = 0;
244
245   Init_BSS();
246   Init_Screen();
247   InitSerial();
248   Init_Mem(bootInfo);
249   Init_CRC32();
250   Init_TSS();
251   Init_Interrupts();
252   Init_Scheduler();
253   Init_Traps();
254   //  Init_Timer();
255   Init_Keyboard();
256   Init_VM(bootInfo);
257   Init_Paging();
258
259   //  Init_IDE();
260
261
262
263
264
265
266
267
268   SerialPrint("\n\nHello, Welcome to this horrid output-only serial interface\n");
269   SerialPrint("Eventually, this will let us control the VMM\n\n");
270  
271   SerialPrint("\n\n===>");
272   
273
274
275   
276
277   
278   SerialPrintLevel(1000,"Launching Noisemaker and keyboard listener threads\n");
279   
280   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
281   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
282
283
284
285
286
287   SerialPrintLevel(1000,"Next: setup GDT\n");
288
289   {
290     int i = 0;
291     for (i = 0; i < 1024; i++) {
292       uint_t * addr = (uint_t *)0xa00000;
293       uint_t foo = *addr;
294
295       SerialPrint("Read From 0x%x=%d\n", (uint_t)addr, foo);
296     }
297
298   }
299   //  Invalidate_PG((void *)0x2000);
300
301   //  VM_Test(bootInfo, 32);  
302   //VM_Test(bootInfo, 1536);
303
304   while(1);
305   
306   /* Now this thread is done. */
307   Exit(0);
308 }