2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "ke/multiboot.h"
26 #include "ke/interrupts.h"
30 #include "ke/process.h"
31 #include "mm/memory.h"
32 #include "ke/module.h"
37 #include "sys/terminal.h"
38 #include "sys/serial.h"
44 static MultibootInfo
*mbinfo
;
46 void kernel_phys_start();
47 void kernel_phys_end();
49 static void keInit2(void)
51 keInitKernelSymbols(mbinfo
);
57 FsFileSystemDriver
*devfs
= fsGetDriver("devfs");
61 kePrint("devfs not available.\n");
64 fsMount(devfs
, "/dev/", 0, 0);
67 keInitModules(mbinfo
);
68 /*kePrint("Opening test file.\n");
69 FsFileHandle *testfile = fsOpen("/dev/cdrom0", 0);
72 kePrint("Could not open test file.\n");
76 unsigned char *test = malloc(512);
77 fsSeek(testfile, 0x8000, 0);
78 int read = fsRead(testfile, test, 512, 1);
79 kePrint("Read %d bytes.\n", read);
83 for (i = 0; i < 512; i++)
86 snprintf(buffer, 2, "%c", test[i]);
87 kePrint("%s", buffer);
93 kePrint("Closing test file.\n");
98 FsFileSystemDriver
*iso9660
= fsGetDriver("iso9660");
102 kePrint("iso9660 not available.\n");
105 fsMount(iso9660
, "/", "/dev/cdrom0", 0);
106 FsFileHandle
*testfile
= fsOpen("/boot/grub/menu.lst", FS_OPEN_READ
);
109 kePrint("Could not open test file.\n");
113 char *test
= malloc(20);
114 int read
= fsRead(testfile
, test
, 19, 1);
115 kePrint("Read %d bytes.\n", read
);
117 kePrint("Read: \"%s\"\n", test
);
123 FsFileSystemDriver
*ramdisk
= fsGetDriver("ramdisk");
127 kePrint("ramdisk not available.\n");
130 fsMount(ramdisk
, "/tmp/", 0, 0);
134 // Load initial process
135 FsFileHandle
*initfile
= fsOpen("/bin/shell", FS_OPEN_READ
);
138 kePrint("Could not open initial program.\n");
141 int size
= fsSeek(initfile
, 0, 2);
142 kePrint("File size: %d bytes.\n", size
);
143 fsSeek(initfile
, 0, 0);
144 char *program
= malloc(size
);
145 if (fsRead(initfile
, program
, size
, 1) != size
)
147 kePrint("Could not read whole program.\n");
152 // Start initial process
153 KeProcess
*process
= keCreateProcess();
154 FsFileHandle
*stdin
= fsOpen("/dev/tty0", FS_OPEN_READ
);
155 process
->fd
[0] = stdin
;
156 FsFileHandle
*stdout
= fsOpen("/dev/tty0", FS_OPEN_WRITE
);
157 process
->fd
[1] = stdout
;
158 FsFileHandle
*stderr
= fsOpen("/dev/tty0", FS_OPEN_WRITE
);
159 process
->fd
[2] = stderr
;
160 keElfMapProgram(&process
->memory
, program
, size
);
161 uintptr_t entry
= ((ElfHeader
*)program
)->e_entry
;
162 keCreateThread(process
, entry
, 0);
170 void keInit(int magic
, MultibootInfo
*info
)
172 char *vidmem
= (char*)0xC00B8000;
173 memset(vidmem
, 0, 160 * 25);
175 keInstallStartupGDT();
178 if (!CHECK_FLAG(info
->flags
, 5))
180 kePrint("Error: no symbols available.\n");
184 uintptr_t kernel_begin
= ((uintptr_t)kernel_phys_start
& ~0xFFF) + 0xC0000000;
185 uintptr_t kernel_end
= (((uintptr_t)kernel_phys_end
+ 4095) & ~0xFFF) + 0xC0000000;
186 mmInitMemoryManager(info
, kernel_begin
, kernel_end
);
187 // Remap multiboot struct
188 uintptr_t info_addr
= mmFindFreeKernelPages(MM_MAX_KERNEL_PAGE
,
189 MM_MIN_KERNEL_PAGE
, 1, 0x1000);
190 mmMapKernelMemory((uintptr_t)info
& ~0xFFF, info_addr
,
191 MM_MAP_READ
| MM_MAP_WRITE
);
192 info_addr
+= (uintptr_t)info
& 0xFFF;
193 info
= (void*)info_addr
;
200 keInitProcessManager();
202 // Initial kernel thread
203 keCreateKernelThread((uintptr_t)keInit2
, 0);
206 kePrint("Installing timer.\n");
208 kePrint("Starting APs.\n");
210 kePrint("Enabling interrupts.\n");
213 while (1) asm("hlt");