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
);
70 FsFileSystemDriver
*iso9660
= fsGetDriver("iso9660");
74 kePrint("iso9660 not available.\n");
77 fsMount(iso9660
, "/", "/dev/cdrom0", 0);
78 FsFileHandle
*testfile
= fsOpen("/boot/grub/menu.lst", FS_OPEN_READ
);
81 kePrint("Could not open test file.\n");
85 char *test
= malloc(20);
86 int read
= fsRead(testfile
, test
, 19, 1);
87 kePrint("Read %d bytes.\n", read
);
89 kePrint("Read: \"%s\"\n", test
);
95 FsFileSystemDriver
*ramdisk
= fsGetDriver("ramdisk");
99 kePrint("ramdisk not available.\n");
102 fsMount(ramdisk
, "/tmp/", 0, 0);
106 // Load initial process
107 FsFileHandle
*initfile
= fsOpen("/bin/shell", FS_OPEN_READ
);
110 kePrint("Could not open initial program.\n");
113 int size
= fsSeek(initfile
, 0, 2);
114 kePrint("File size: %d bytes.\n", size
);
115 fsSeek(initfile
, 0, 0);
116 char *program
= malloc(size
);
117 if (fsRead(initfile
, program
, size
, 1) != size
)
119 kePrint("Could not read whole program.\n");
124 // Start initial processes
127 for (i
= 0; i
< 8; i
++)
129 snprintf(terminal
, 11, "/dev/tty%d", i
);
130 KeProcess
*process
= keCreateProcess();
131 FsFileHandle
*stdin
= fsProcessOpen(process
, terminal
, FS_OPEN_READ
);
132 process
->fd
[0] = stdin
;
133 FsFileHandle
*stdout
= fsProcessOpen(process
, terminal
, FS_OPEN_WRITE
);
134 process
->fd
[1] = stdout
;
135 FsFileHandle
*stderr
= fsProcessOpen(process
, terminal
, FS_OPEN_WRITE
);
136 process
->fd
[2] = stderr
;
137 keElfMapProgram(&process
->memory
, program
, size
);
138 uintptr_t entry
= ((ElfHeader
*)program
)->e_entry
;
139 keCreateThread(process
, entry
, 4, 0, 0, 0, 0);
148 void keInit(int magic
, MultibootInfo
*info
)
150 char *vidmem
= (char*)0xC00B8000;
151 memset(vidmem
, 0, 160 * 25);
153 keInstallStartupGDT();
156 if (!CHECK_FLAG(info
->flags
, 5))
158 kePrint("Error: no symbols available.\n");
162 uintptr_t kernel_begin
= ((uintptr_t)kernel_phys_start
& ~0xFFF) + 0xC0000000;
163 uintptr_t kernel_end
= (((uintptr_t)kernel_phys_end
+ 4095) & ~0xFFF) + 0xC0000000;
164 mmInitMemoryManager(info
, kernel_begin
, kernel_end
);
165 // Remap multiboot struct
166 uintptr_t info_addr
= mmFindFreeKernelPages(MM_MAX_KERNEL_PAGE
,
167 MM_MIN_KERNEL_PAGE
, 1, 0x1000);
168 mmMapKernelMemory((uintptr_t)info
& ~0xFFF, info_addr
,
169 MM_MAP_READ
| MM_MAP_WRITE
);
170 info_addr
+= (uintptr_t)info
& 0xFFF;
171 info
= (void*)info_addr
;
178 keInitProcessManager();
180 // Initial kernel thread
181 keCreateKernelThread((uintptr_t)keInit2
, 0);
184 kePrint("Installing timer.\n");
186 kePrint("Starting APs.\n");
188 kePrint("Enabling interrupts.\n");
191 while (1) asm("hlt");