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.


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