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.


minor test kernel updates
[palacios.git] / test / geekos_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/debug.h>
13 #include <geekos/bootinfo.h>
14 #include <geekos/string.h>
15 #include <geekos/screen.h>
16 #include <geekos/mem.h>
17 #include <geekos/crc32.h>
18 #include <geekos/tss.h>
19 #include <geekos/int.h>
20 #include <geekos/kthread.h> 
21 #include <geekos/trap.h>
22 #include <geekos/timer.h>
23 #include <geekos/keyboard.h>
24 #include <geekos/io.h>
25 #include <geekos/serial.h>
26 #include <geekos/reboot.h>
27 #include <geekos/mem.h>
28 #include <geekos/paging.h>
29 #include <geekos/ide.h>
30 #include <geekos/vm_cons.h>
31 #include <geekos/pci.h>
32 #include <geekos/gdt.h>
33
34
35
36 #define TEST_PAGING 0
37 #define TEST_PCI 1
38
39 /*
40   static inline unsigned int cpuid_ecx(unsigned int op)
41   {
42   unsigned int eax, ecx;
43   
44   __asm__("cpuid"
45   : "=a" (eax), "=c" (ecx)
46   : "0" (op)
47   : "bx", "dx" );
48   
49   return ecx;
50   }
51 */
52
53
54
55 extern void Get_MSR(ulong_t msr, unsigned int *val1, unsigned int *val2);
56 extern void Set_MSR(ulong_t msr, ulong_t val1, ulong_t val2);
57 extern uint_t Get_EIP();
58 extern uint_t Get_ESP();
59 extern uint_t Get_EBP();
60
61
62
63 extern void Invalidate_PG(void * addr);
64
65
66 int foo=42;
67
68 #define SPEAKER_PORT 0x61
69
70
71 void Buzz(unsigned delay, unsigned num)
72 {
73   volatile int x;
74   int i,j;
75   unsigned char init;
76   
77   init=In_Byte(SPEAKER_PORT);
78
79   for (i=0;i<num;i++) { 
80     Out_Byte(SPEAKER_PORT, init|0x2);
81     for (j=0;j<delay;j++) { 
82       x+=j;
83     }
84     Out_Byte(SPEAKER_PORT, init);
85     for (j=0;j<delay;j++) { 
86       x+=j;
87     }
88   }
89 }
90
91 inline void MyOut_Byte(ushort_t port, uchar_t value)
92 {
93     __asm__ __volatile__ (
94         "outb %b0, %w1"
95         :
96         : "a" (value), "Nd" (port)
97     );
98 }
99
100 /*
101  * Read a byte from an I/O port.
102  */
103 inline uchar_t MyIn_Byte(ushort_t port)
104 {
105     uchar_t value;
106
107     __asm__ __volatile__ (
108         "inb %w1, %b0"
109         : "=a" (value)
110         : "Nd" (port)
111     );
112
113     return value;
114 }
115
116
117 extern void MyBuzzVM();
118
119 #define MYBUZZVM_START MyBuzzVM
120 #define MYBUZZVM_LEN   0x3d
121
122 void BuzzVM()
123 {
124   int x;
125   int j;
126   unsigned char init;
127   
128   
129   init=MyIn_Byte(SPEAKER_PORT);
130
131   while (1) {
132     MyOut_Byte(SPEAKER_PORT, init|0x2);
133     for (j=0;j<1000000;j++) { 
134       x+=j;
135     }
136     MyOut_Byte(SPEAKER_PORT, init);
137     for (j=0;j<1000000;j++) { 
138       x+=j;
139     }
140   }
141 }
142
143
144
145 extern void RunVM();
146
147 int vmRunning = 0;
148
149 void RunVM() {
150   vmRunning = 1;
151
152   while(1);
153 }
154
155
156
157
158
159 void Buzzer(ulong_t arg) {
160   ulong_t *doIBuzz = (ulong_t*)arg;
161   while (1) {
162     // Quick and dirty hack to save my hearing...
163     // I'm not too worried about timing, so I'll deal with concurrency later...
164     if (*doIBuzz == 1) {
165       Buzz(1000000, 10);
166     }
167   }
168
169 }
170
171
172
173 void Hello(ulong_t arg)
174 {
175   char *b="hello ";
176   char byte;
177   short port=0xe9;
178   int i;
179   while(1){
180     for (i=0;i<6;i++) { 
181       byte=b[i];
182       __asm__ __volatile__ ("outb %b0, %w1" : : "a"(byte), "Nd"(port) );
183     }
184   }
185 }
186
187 void Keyboard_Listener(ulong_t arg) {
188   ulong_t * doIBuzz = (ulong_t*)arg;
189   Keycode key_press;
190
191   Print("Press F4 to turn on/off the speaker\n");
192
193   while ((key_press = Wait_For_Key())) {    
194     if (key_press == KEY_F4) {
195       PrintBoth("\nToggling Speaker Port\n");
196       *doIBuzz = (*doIBuzz + 1) % 2;
197     } else if (key_press == KEY_F5) {
198       PrintBoth("\nMachine Restart\n");
199       machine_real_restart();
200     }
201   }
202   return;
203 }
204
205
206
207 extern char BSS_START, BSS_END;
208
209 extern char end;
210
211
212
213
214 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
215 {
216   uint_t address;
217
218   for (address=start;address<start+length;address+=PAGE_SIZE) { 
219     void *page;
220     pte_t pte = template_pte;
221     
222     page=Alloc_Page();
223     KASSERT(page);
224     
225     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
226
227     KASSERT(MapPage((void*)address,&pte,1));
228   }
229   
230   return 0;
231 }
232     
233
234
235 /*
236  * Kernel C code entry point.
237  * Initializes kernel subsystems, mounts filesystems,
238  * and spawns init process.
239  */
240 void Main(struct Boot_Info* bootInfo)
241 {
242   struct Kernel_Thread * key_thread;
243   struct Kernel_Thread * spkr_thread;
244
245
246   //  VMConsPutLineN("hello\n", 6);
247
248   ulong_t doIBuzz = 0;
249
250   Init_BSS();
251   Init_Screen();
252   InitSerial();
253
254   Init_VMCons();
255   Init_Mem(bootInfo);
256   Init_CRC32();
257   Init_TSS();
258   Init_Interrupts();
259   Init_Scheduler();
260   Init_Traps();
261   //  Init_Timer();
262   Init_Keyboard();
263   Init_VM(bootInfo);
264   Init_Paging();
265
266   //  Init_IDE();
267
268
269
270
271
272
273
274
275   PrintBoth("\n\nHello, Welcome to this horrid output-only serial interface\n");
276   PrintBoth("Eventually, this will let us control the VMM\n\n");
277  
278   PrintBoth("\n\n===>");
279   
280
281
282   
283
284   
285   PrintBoth("Launching Noisemaker and keyboard listener threads\n");
286   
287   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
288   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
289
290
291
292
293
294   PrintBoth("Next: setup GDT\n");
295   {
296     uint_t addr = 0xe0000;
297     uint_t hi = 0;
298
299
300    
301     //    wrmsr(SYMBIOTIC_MSR, hi, addr);
302     {
303         uint_t msr_num = 0x0000001B;
304         __asm__ __volatile__ ("rdmsr" : : "c"(msr_num) : "%eax","%edx","memory");
305     }
306     {
307         uint_t msr_num = 0x0000001c;
308         __asm__ __volatile__ ("rdmsr" : : "c"(msr_num) : "%eax","%edx","memory");
309     }
310
311
312     {
313         uint_t msr_num = 0x0000001B;
314         __asm__ __volatile__ ("wrmsr" : : "c"(msr_num), "a"(hi), "d"(addr) : "memory");
315     }
316
317
318
319     {
320         uint_t msr_num = 0x535;
321         __asm__ __volatile__ ("wrmsr" : : "c"(msr_num), "a"(hi), "d"(addr) : "memory");
322     }
323
324
325     while (1) {}
326
327   }
328   if (TEST_PAGING) {
329       int i = 0;
330       for (i = 0; i < 1024; i++) {
331           uint_t * addr = (uint_t *)0xa00000;
332           uint_t foo = *addr;
333           
334           PrintBoth("Read From 0x%x=%d\n", (uint_t)addr, foo);
335       }
336
337       
338       //  Invalidate_PG((void *)0x2000);
339        
340       //  VM_Test(bootInfo, 32);  
341       //VM_Test(bootInfo, 1536);
342   }
343
344
345   if (TEST_PCI) {
346       Init_PCI();
347
348
349   }
350
351   while(1);
352   
353   /* Now this thread is done. */
354   Exit(0);
355 }