- Fixed memory management.
[planlOS.git] / system / kernel / ke / start.c
bloba895683863e5a463f4df35e30c945590b0dcde6f
1 /*
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.
22 #include "ke/debug.h"
23 #include "ke/gdt.h"
24 #include "ke/apic.h"
25 #include "ke/multiboot.h"
26 #include "ke/interrupts.h"
27 #include "ke/smp.h"
28 #include "ke/timer.h"
29 #include "ke/pci.h"
30 #include "ke/process.h"
31 #include "mm/memory.h"
32 #include "ke/module.h"
33 #include "fs/fs.h"
34 #include "fs/devfs.h"
35 #include "ke/elf.h"
36 #include "sys/pipe.h"
37 #include "sys/terminal.h"
38 #include "sys/serial.h"
39 #include "net/net.h"
40 #include <string.h>
41 #include <stdio.h>
42 #include <stdlib.h>
44 static MultibootInfo *mbinfo;
46 void kernel_phys_start();
47 void kernel_phys_end();
49 static void keInit2(void)
51 keInitKernelSymbols(mbinfo);
52 keInitPCI();
53 fsInit();
54 fsInitDevFS();
55 sysInitPipe();
56 netInit();
57 FsFileSystemDriver *devfs = fsGetDriver("devfs");
58 if (!devfs)
60 // TODO: Panic
61 kePrint("devfs not available.\n");
62 while (1);
64 fsMount(devfs, "/dev/", 0, 0);
65 sysInitSerial();
66 sysInitTerminals();
67 keInitModules(mbinfo);
68 /*kePrint("Opening test file.\n");
69 FsFileHandle *testfile = fsOpen("/dev/cdrom0", 0);
70 if (!testfile)
72 kePrint("Could not open test file.\n");
74 else
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);
80 if (read != -1)
82 uint32_t i;
83 for (i = 0; i < 512; i++)
85 char buffer[2];
86 snprintf(buffer, 2, "%c", test[i]);
87 kePrint("%s", buffer);
89 kePrint("\n");
91 free(test);
93 kePrint("Closing test file.\n");
94 fsClose(testfile);
95 }*/
97 // Map CDROM
98 FsFileSystemDriver *iso9660 = fsGetDriver("iso9660");
99 if (!iso9660)
101 // TODO: Panic
102 kePrint("iso9660 not available.\n");
103 while (1);
105 fsMount(iso9660, "/", "/dev/cdrom0", 0);
106 FsFileHandle *testfile = fsOpen("/boot/grub/menu.lst", FS_OPEN_READ);
107 if (!testfile)
109 kePrint("Could not open test file.\n");
111 else
113 char *test = malloc(20);
114 int read = fsRead(testfile, test, 19, 1);
115 kePrint("Read %d bytes.\n", read);
116 test[read] = 0;
117 kePrint("Read: \"%s\"\n", test);
118 fsClose(testfile);
119 free(test);
122 // Map /tmp
123 FsFileSystemDriver *ramdisk = fsGetDriver("ramdisk");
124 if (!ramdisk)
126 // TODO: Panic
127 kePrint("ramdisk not available.\n");
128 while (1);
130 fsMount(ramdisk, "/tmp/", 0, 0);
132 kePrint("Done.\n");
134 // Load initial process
135 FsFileHandle *initfile = fsOpen("/bin/shell", FS_OPEN_READ);
136 if (!initfile)
138 kePrint("Could not open initial program.\n");
139 while (1);
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");
148 fsClose(initfile);
149 while (1);
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);
164 free(program);
165 fsClose(initfile);
167 keExitThread();
170 void keInit(int magic, MultibootInfo *info)
172 char *vidmem = (char*)0xC00B8000;
173 memset(vidmem, 0, 160 * 25);
175 keInstallStartupGDT();
176 kePrintInit();
177 keInitPIC();
178 if (!CHECK_FLAG(info->flags, 5))
180 kePrint("Error: no symbols available.\n");
181 while (1);
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;
194 mbinfo = info;
196 keAPICInit();
197 keInitSMP();
198 keInstallGDT();
199 keInitTimer(100);
200 keInitProcessManager();
202 // Initial kernel thread
203 keCreateKernelThread((uintptr_t)keInit2, 0);
205 // Start kernel
206 kePrint("Installing timer.\n");
207 keInstallTimer();
208 kePrint("Starting APs.\n");
209 keStartAPs();
210 kePrint("Enabling interrupts.\n");
211 asm("sti");
213 while (1) asm("hlt");