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.


43987f0dc30bfaa87c0b75ad61d4fd8913be6a81
[palacios.git] / palacios / 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.47 $
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 #include <geekos/malloc.h>
30
31 #include <geekos/debug.h>
32
33
34 #include <geekos/vm.h>
35 #include <geekos/gdt.h>
36
37 #include <geekos/vmm_stubs.h>
38
39 #include <geekos/pci.h>
40
41
42 #include <geekos/net.h>
43
44
45 #define SPEAKER_PORT 0x61
46
47
48
49
50 void Spin()
51 {
52   // hack - competing thread
53   while (1) {};
54
55 }
56
57
58 void Buzz(unsigned delay, unsigned num)
59 {
60   volatile int x;
61   int i,j;
62   unsigned char init;
63   
64   init=In_Byte(SPEAKER_PORT);
65
66   for (i=0;i<num;i++) { 
67     Out_Byte(SPEAKER_PORT, init|0x2);
68     for (j=0;j<delay;j++) { 
69       x+=j;
70     }
71     Out_Byte(SPEAKER_PORT, init);
72     for (j=0;j<delay;j++) { 
73       x+=j;
74     }
75   }
76 }
77
78 inline void MyOut_Byte(ushort_t port, uchar_t value)
79 {
80     __asm__ __volatile__ (
81         "outb %b0, %w1"
82         :
83         : "a" (value), "Nd" (port)
84     );
85 }
86
87 /*
88  * Read a byte from an I/O port.
89  */
90 inline uchar_t MyIn_Byte(ushort_t port)
91 {
92     uchar_t value;
93
94     __asm__ __volatile__ (
95         "inb %w1, %b0"
96         : "=a" (value)
97         : "Nd" (port)
98     );
99
100     return value;
101 }
102
103
104
105
106
107
108
109 void Buzzer(ulong_t arg) {
110   ulong_t *doIBuzz = (ulong_t*)arg;
111   while (1) {
112     // Quick and dirty hack to save my hearing...
113     // I'm not too worried about timing, so I'll deal with concurrency later...
114     if (*doIBuzz == 1) {
115       Buzz(1000000, 10);
116     }
117   }
118
119 }
120
121
122
123
124
125 void Keyboard_Listener(ulong_t arg) {
126   ulong_t * doIBuzz = (ulong_t*)arg;
127   Keycode key_press;
128
129   Print("Press F4 to turn on/off the speaker\n");
130
131   while ((key_press = Wait_For_Key())) {    
132     if (key_press == KEY_F4) {
133       Print("\nToggling Speaker Port\n");
134       SerialPrintLevel(100,"\nToggling Speaker Port\n");
135       *doIBuzz = (*doIBuzz + 1) % 2;
136     } else if (key_press == KEY_F5) {
137       Print("\nMachine Restart\n");
138       SerialPrintLevel(100,"\nMachine Restart\n");
139       machine_real_restart();
140     }
141   }
142   return;
143 }
144
145
146
147 extern char BSS_START, BSS_END;
148
149 extern char end;
150
151
152 /* This is an ugly hack to get at the VM  memory */
153 ulong_t vm_range_start;
154 ulong_t vm_range_end;
155 ulong_t guest_kernel_start;
156 ulong_t guest_kernel_end;
157 /* ** */
158
159
160 int AllocateAndMapPagesForRange(uint_t start, uint_t length, pte_t template_pte)
161 {
162   uint_t address;
163
164   for (address=start;address<start+length;address+=PAGE_SIZE) { 
165     void *page;
166     pte_t pte = template_pte;
167     
168     page=Alloc_Page();
169     KASSERT(page);
170     
171     pte.pageBaseAddr=PAGE_ALLIGNED_ADDR(page);
172
173     KASSERT(MapPage((void*)address,&pte,1));
174   }
175   
176   return 0;
177 }
178     
179
180
181 /*
182  * Kernel C code entry point.
183  * Initializes kernel subsystems, mounts filesystems,
184  * and spawns init process.
185  */
186 void Main(struct Boot_Info* bootInfo)
187 {
188
189
190   //Out_Byte(0x1234,5);
191   //Out_Byte(0x1234,5);
192
193   Init_BSS();
194   Init_Screen();
195
196   Init_Serial();
197
198   /*  {
199     extern char BSS_START, BSS_END;
200
201     SerialPrint("BSS 0x%x->0x%x\n", &BSS_START, &BSS_END);
202
203     }*/
204
205
206   // SerialPrint("Guest Mem Dump at 0x%x\n", 0x100000);
207   //SerialMemDump((unsigned char *)(0x100000), 261 * 1024);
208
209   Init_Mem(bootInfo);
210   Init_CRC32();
211   Init_TSS();
212   Init_Interrupts();
213   Init_Scheduler();
214   Init_Traps();
215   Init_Timer();
216   Init_Keyboard();
217   Init_VM(bootInfo);
218   Init_Paging();
219   
220   //Init_PCI();
221
222   Init_Stubs();
223
224   Init_Network();
225
226
227   //  Init_IDE();
228
229   // Print("Done; stalling\n");
230
231
232   
233 #if 0
234   SerialPrint("Dumping VM kernel Code (first 128 bytes @ 0x%x)\n", 0x100000);
235   SerialMemDump((unsigned char *)0x100000, 256);
236   /*
237     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
238     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
239   */
240 #endif
241
242
243 #if 1
244   struct Kernel_Thread *spin_thread;
245
246   spin_thread=Start_Kernel_Thread(Spin,0,PRIORITY_NORMAL,false);
247
248 #endif
249
250 #if 0
251   {
252
253   struct Kernel_Thread * key_thread;
254   struct Kernel_Thread * spkr_thread;
255
256   ulong_t doIBuzz = 0;
257
258   SerialPrint("Dumping BIOS code ffff0-fffff\n\n");
259   SerialMemDump((unsigned char *)0x10fff0, 16);
260   /*
261     SerialPrint("Dumping kernel Code (first 512 bytes @ 0x%x)\n",KERNEL_START);
262     SerialMemDump((unsigned char *)VM_KERNEL_START, 512);
263   */
264
265   SerialPrint("Noisemaker and keyboard listener threads\n");
266   key_thread = Start_Kernel_Thread(Keyboard_Listener, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
267   spkr_thread = Start_Kernel_Thread(Buzzer, (ulong_t)&doIBuzz, PRIORITY_NORMAL, false);
268   }
269 #endif
270
271
272   {
273     RunVMM(bootInfo);
274   }
275
276
277   SerialPrint("RunVMM returned, spinning\n");
278   while (1) {} 
279
280
281   TODO("Write a Virtual Machine Monitor");
282   
283
284   Exit(0);
285 }