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.


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