* libcurses++, libc++ and liblightOS++ are installed into the crosscompiler directory
[lightOS.git] / kernel / kernel.cpp
blobda9a331567db5162db3ef29be337224b12936544
1 /*
2 lightOS kernel
3 Copyright (C) 2006-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <kernel/kernel.hpp>
20 #include <cstring>
21 #include <algorithm>
22 #include <iterator>
23 #include <memory>
24 #include <kernel/smp.hpp>
25 #include <kernel/acpi.hpp>
26 #include <kernel/elf.hpp>
27 #include <kernel/log.hpp>
28 #include <kernel/scheduler.hpp>
29 #include <kernel/io_port_manager.hpp>
30 #include <kernel/x86_shared/pic.hpp>
31 #include <kernel/x86_shared/apic.hpp>
32 #include <kernel/x86_shared/serial.hpp>
33 #include <kernel/x86_shared/gdt.hpp>
34 #include <kernel/x86_shared/interrupt_controller.hpp>
35 #include <kernel/range_allocator.hpp>
36 #include <kernel/virtual_memory.hpp>
37 #include <kernel/processor.hpp>
38 using namespace std;
39 using namespace kernel;
40 using kernel::virtual_memory;
42 void kernel_main(const kernel::boot_info &info)
44 // Go through the module list
45 kernel::module_info *Module = physical_address<kernel::module_info*>(reinterpret_cast<kernel::module_info*>(info.mod));
46 for (size_t i = 0;i < info.module_count;i++)
48 elf Elf(physical_address<void*>(reinterpret_cast<void*>(Module[i].start)),
49 Module[i].end - Module[i].start);
50 if (Elf.valid() == false)
52 FATAL(reinterpret_cast<char*>(Module[i].name) << ": Not a valid elf file.");
53 processor::halt();
56 if (Elf.is_library() == true)
58 /* library */
59 const char *name = physical_address<char*>(reinterpret_cast<char*>(Module[i].name));
60 Elf.create_library_image(name);
62 else
64 // Create the process image
65 string libName;
66 context Context;
67 vector<void*> Pages;
68 bool result = Elf.create_process_image(Context, Pages, libName);
70 string path(physical_address<char*>(reinterpret_cast<char*>(Module[i].name)));
71 if (result == true)
73 // Create the process
74 process::create(Context,
75 Pages,
76 path,
77 Elf.entry_point(),
78 lightOS::process::server);
80 else
82 if (libName.length() != 0)
84 ERROR("kernel: shared-library \"" << libName.c_str() << "\" for \"" << path.c_str() << "\" not loaded");
86 else
88 ERROR("kernel: invalid elf file \"" << path.c_str() << "\"");
93 // Free module space
94 range_allocator &RangeAllocator = range_allocator::instance();
95 size_t size = Module[i].end - Module[i].start + virtual_memory::page_size - 1;
96 RangeAllocator.free_range(Module[i].start, size / virtual_memory::page_size);
99 // Initialize the I/O Port Manager
100 io_port_manager &IOPortManager = io_port_manager::instance();
101 IOPortManager.init();
103 // Has this processor a local APIC?
104 if (processor::has_local_apic() == true)
106 #ifdef _LIGHTOS_SMP
107 // Initialize the APIC class
108 local_apic::initialize();
109 #endif
111 else
113 LOG("No local APIC found");
116 // Initialize the processor class
117 // NOTE Also calls initialize_this() for the BSP
118 processor::initialize();
120 // SMP compiled in?
121 #ifdef _LIGHTOS_SMP
122 if (processor::has_local_apic() == true)
124 // Initialize SMP
125 smp::initialize();
127 #endif
129 // ACPI compiled in?
130 #ifdef ACPI
131 // Initialize ACPI
132 acpi::initialize();
133 #endif
135 // Initialize the GDT
136 gdt &Gdt = gdt::instance();
137 Gdt.initialize();
138 Gdt.load();
140 // Load the Task Register
141 processor::load_taskregister();
143 // SMP compiled in?
144 #ifdef _LIGHTOS_SMP
145 if (smp::compatible() == true)
147 // Complete initialization & schedule!
148 smp::initialize2();
150 #endif
152 // Initialize the interrupt controller
153 interrupt_controller &InterruptController = interrupt_controller::instance();
154 InterruptController.init();
156 scheduler::schedule(0);