956d16a750844e47efd39fddcb65dbc44853c239
[reactos.git] / reactos / ntoskrnl / ke / main.c
1 /* $Id: main.c,v 1.37 2000/02/27 02:10:09 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ke/main.c
6 * PURPOSE: Initalizes the kernel
7 * PROGRAMMER: David Welch (welch@cwcom.net)
8 * UPDATE HISTORY:
9 * 28/05/98: Created
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/ntoskrnl.h>
16 #include <reactos/buildno.h>
17 #include <internal/mm.h>
18 #include <string.h>
19 #include <internal/string.h>
20 #include <internal/module.h>
21 #include <internal/ldr.h>
22 #include <internal/ex.h>
23 #include <internal/ps.h>
24
25 #include <internal/mmhal.h>
26 #include <internal/i386/segment.h>
27
28 #define NDEBUG
29 #include <internal/debug.h>
30
31 /* DATA *********************************************************************/
32
33 USHORT NtBuildNumber = KERNEL_VERSION_BUILD; /* EXPORTED */
34
35 ULONG NtGlobalFlag = 0; /* FIXME: EXPORTED */
36
37 /* FUNCTIONS ****************************************************************/
38
39 void set_breakpoint(unsigned int i, unsigned int addr, unsigned int type,
40 unsigned int len)
41 /*
42 * FUNCTION: Sets a hardware breakpoint
43 * ARGUMENTS:
44 * i = breakpoint to set (0 to 3)
45 * addr = linear address to break on
46 * type = Type of access to break on
47 * len = length of the variable to watch
48 * NOTES:
49 * The variable to watch must be aligned to its length (i.e. a dword
50 * breakpoint must be aligned to a dword boundary)
51 *
52 * A fatal exception will be generated on the access to the variable.
53 * It is (at the moment) only really useful for catching undefined
54 * pointers if you know the variable effected but not the buggy
55 * routine.
56 *
57 * FIXME: Extend to call out to kernel debugger on breakpoint
58 * Add support for I/O breakpoints
59 * REFERENCES: See the i386 programmer manual for more details
60 */
61 {
62 unsigned int mask;
63
64 if (i>3)
65 {
66 DbgPrint("Invalid breakpoint index at %s:%d\n",__FILE__,__LINE__);
67 return;
68 }
69
70 /*
71 * Load the linear address
72 */
73 switch (i)
74 {
75 case 0:
76 __asm("movl %0,%%db0\n\t"
77 : /* no outputs */
78 : "d" (addr));
79 break;
80
81 case 1:
82 __asm__("movl %0,%%db1\n\t"
83 : /* no outputs */
84 : "d" (addr));
85 break;
86
87 case 2:
88 __asm__("movl %0,%%db2\n\t"
89 : /* no outputs */
90 : "d" (addr));
91 break;
92
93 case 3:
94 __asm__("movl %0,%%db3\n\t"
95 : /* no outputs */
96 : "d" (addr));
97 break;
98 }
99
100 /*
101 * Setup mask for dr7
102 */
103 mask = (len<<(16 + 2 + i*4)) + (type<<(16 + i*4)) + (1<<(i*2));
104 __asm__("movl %%db7,%%eax\n\t"
105 "orl %0,%%eax\n\t"
106 "movl %%eax,%%db7\n\t"
107 : /* no outputs */
108 : "d" (mask)
109 : "ax");
110 }
111
112 extern int edata;
113 extern int end;
114
115 #if 0
116 static char * INIData =
117 "[HKEY_LOCAL_MACHINE\\HARDWARE]\r\n"
118 "\r\n"
119 "[HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP]\r\n"
120 "\r\n"
121 "[HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\AtDisk]\r\n"
122 "\r\n"
123 "[HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\AtDisk\\Controller 0]\r\n"
124 "Controller Address=dword:000001f0\r\n"
125 "Controller Interrupt=dword:0000000e\r\n"
126 "\r\n"
127 "\r\n"
128 "\r\n"
129 "";
130 #endif
131
132 unsigned int old_idt[256][2];
133 //extern unsigned int idt[];
134 unsigned int old_idt_valid = 1;
135
136 asmlinkage void _main(boot_param* _bp)
137 /*
138 * FUNCTION: Called by the boot loader to start the kernel
139 * ARGUMENTS:
140 * _bp = Pointer to boot parameters initialized by the boot loader
141 * NOTE: The boot parameters are stored in low memory which will become
142 * invalid after the memory managment is initialized so we make a local copy.
143 */
144 {
145 unsigned int i;
146 unsigned int start;
147 unsigned int start1;
148 boot_param bp;
149 unsigned int last_kernel_address;
150
151 // memset((void *)&edata,0,((int)&end)-((int)&edata));
152
153 /*
154 * Copy the parameters to a local buffer because lowmem will go away
155 */
156 memcpy(&bp,_bp,sizeof(boot_param));
157
158 /*
159 * Initalize the hal (Phase 0)
160 */
161 HalInitSystem (0, &bp);
162
163 HalDisplayString("Starting ReactOS "KERNEL_VERSION_STR" (Build "KERNEL_VERSION_BUILD_STR")\n");
164
165 /*
166 * Initialize the debug output
167 */
168 KdInitSystem ();
169
170 start = KERNEL_BASE + PAGE_ROUND_UP(bp.module_length[0]);
171 if (start < ((int)&end))
172 {
173 DbgPrint("start %x end %x\n",start,(int)&end);
174 DbgPrint("Kernel booted incorrectly, aborting\n");
175 DbgPrint("Reduce the amount of uninitialized data\n");
176 for(;;);
177 }
178 start1 = start+PAGE_ROUND_UP(bp.module_length[1]);
179
180 last_kernel_address = KERNEL_BASE;
181 for (i=0; i<=bp.nr_files; i++)
182 {
183 last_kernel_address = last_kernel_address +
184 PAGE_ROUND_UP(bp.module_length[i]);
185 }
186
187 /*
188 * Initalize various critical subsystems
189 */
190 DPRINT("HalInitSystem()\n");
191 HalInitSystem (1, &bp);
192 DPRINT("MmInitialize()\n");
193 MmInitialize(&bp, last_kernel_address);
194 DPRINT("KeInit()\n");
195 KeInit();
196 DPRINT("ExInit()\n");
197 ExInit();
198 DPRINT("ObInit()\n");
199 ObInit();
200 DPRINT("PsInit()\n");
201 PiInitProcessManager();
202 DPRINT("IoInit()\n");
203 IoInit();
204 DPRINT("LdrInitModuleManagement()\n");
205 LdrInitModuleManagement();
206 CmInitializeRegistry();
207 NtInit();
208
209 memcpy(old_idt, KiIdt, sizeof(old_idt));
210 old_idt_valid = 0;
211
212 /*
213 * Initalize services loaded at boot time
214 */
215 DPRINT1("%d files loaded\n",bp.nr_files);
216
217 /* Pass 1: load registry chunks passed in */
218 start = KERNEL_BASE + PAGE_ROUND_UP(bp.module_length[0]);
219 for (i = 1; i < bp.nr_files; i++)
220 {
221 if (!strcmp ((PCHAR) start, "REGEDIT4"))
222 {
223 DPRINT1("process registry chunk at %08lx\n", start);
224 CmImportHive((PCHAR) start);
225 }
226 start = start + bp.module_length[i];
227 }
228
229 /* Pass 2: process boot loaded drivers */
230 start = KERNEL_BASE + PAGE_ROUND_UP(bp.module_length[0]);
231 start1 = start + bp.module_length[1];
232 for (i=1;i<bp.nr_files;i++)
233 {
234 if (strcmp ((PCHAR) start, "REGEDIT4"))
235 {
236 DPRINT1("process module at %08lx\n", start);
237 LdrProcessDriver((PVOID)start);
238 }
239 start = start + bp.module_length[i];
240 }
241
242 /*
243 * Load Auto configured drivers
244 */
245 CHECKPOINT;
246 LdrLoadAutoConfigDrivers();
247
248 /*
249 * Launch initial process
250 */
251 CHECKPOINT;
252 LdrLoadInitialProcess();
253
254 /*
255 * Enter idle loop
256 */
257 DbgPrint("Finished main()\n");
258 PsTerminateSystemThread(STATUS_SUCCESS);
259 }
260
261
262 /* EOF */