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