2023-03-27 03:38:55 +03:00
|
|
|
#include <kernel/ACPI.h>
|
2023-03-13 15:32:46 +02:00
|
|
|
#include <kernel/Arch.h>
|
2023-01-25 21:39:03 +02:00
|
|
|
#include <kernel/Debug.h>
|
2023-07-10 23:17:14 +03:00
|
|
|
#include <kernel/FS/DevFS/FileSystem.h>
|
2023-02-26 03:00:29 +02:00
|
|
|
#include <kernel/FS/VirtualFileSystem.h>
|
2023-03-01 21:21:08 +02:00
|
|
|
#include <kernel/GDT.h>
|
2022-11-16 19:49:09 +02:00
|
|
|
#include <kernel/IDT.h>
|
2023-03-29 03:05:16 +03:00
|
|
|
#include <kernel/Input/PS2Controller.h>
|
2023-01-23 20:13:57 +02:00
|
|
|
#include <kernel/InterruptController.h>
|
2022-12-10 00:33:03 +02:00
|
|
|
#include <kernel/kprint.h>
|
2023-04-14 15:16:38 +03:00
|
|
|
#include <kernel/Memory/Heap.h>
|
2023-04-14 13:30:21 +03:00
|
|
|
#include <kernel/Memory/kmalloc.h>
|
2023-05-29 21:06:09 +03:00
|
|
|
#include <kernel/Memory/PageTable.h>
|
2022-11-15 21:42:14 +02:00
|
|
|
#include <kernel/multiboot.h>
|
2023-02-26 03:00:29 +02:00
|
|
|
#include <kernel/PCI.h>
|
2022-12-07 02:41:18 +02:00
|
|
|
#include <kernel/PIC.h>
|
|
|
|
#include <kernel/PIT.h>
|
2023-03-16 12:17:04 +02:00
|
|
|
#include <kernel/Process.h>
|
2023-02-16 20:00:31 +02:00
|
|
|
#include <kernel/Scheduler.h>
|
2022-12-10 00:33:03 +02:00
|
|
|
#include <kernel/Serial.h>
|
2023-03-13 15:32:46 +02:00
|
|
|
#include <kernel/Syscall.h>
|
2023-04-05 00:56:09 +03:00
|
|
|
#include <kernel/Terminal/TTY.h>
|
|
|
|
#include <kernel/Terminal/VesaTerminalDriver.h>
|
2022-11-12 21:04:47 +02:00
|
|
|
|
2023-04-12 22:20:18 +03:00
|
|
|
#include <LibELF/ELF.h>
|
|
|
|
|
2023-01-10 17:50:24 +02:00
|
|
|
extern "C" const char g_kernel_cmdline[];
|
2022-11-14 00:27:11 +02:00
|
|
|
|
2022-12-20 11:57:09 +02:00
|
|
|
struct ParsedCommandLine
|
|
|
|
{
|
2023-01-25 21:44:09 +02:00
|
|
|
bool force_pic = false;
|
|
|
|
bool disable_serial = false;
|
2023-03-30 16:37:53 +03:00
|
|
|
BAN::StringView root;
|
2022-12-20 11:57:09 +02:00
|
|
|
};
|
|
|
|
|
2023-03-30 16:37:53 +03:00
|
|
|
static bool should_disable_serial()
|
2022-12-20 11:57:09 +02:00
|
|
|
{
|
2023-01-12 13:20:38 +02:00
|
|
|
if (!(g_multiboot_info->flags & 0x02))
|
2023-03-30 16:37:53 +03:00
|
|
|
return false;
|
2023-01-12 13:20:38 +02:00
|
|
|
|
|
|
|
const char* start = g_kernel_cmdline;
|
|
|
|
const char* current = g_kernel_cmdline;
|
2022-12-28 04:17:46 +02:00
|
|
|
while (true)
|
|
|
|
{
|
2023-01-12 13:20:38 +02:00
|
|
|
if (!*current || *current == ' ' || *current == '\t')
|
2022-12-28 04:17:46 +02:00
|
|
|
{
|
2023-01-25 21:44:09 +02:00
|
|
|
if (current - start == 8 && memcmp(start, "noserial", 8) == 0)
|
2023-03-30 16:37:53 +03:00
|
|
|
return true;
|
2023-01-12 13:20:38 +02:00
|
|
|
if (!*current)
|
2022-12-28 04:17:46 +02:00
|
|
|
break;
|
2023-01-12 13:20:38 +02:00
|
|
|
start = current + 1;
|
2022-12-28 04:17:46 +02:00
|
|
|
}
|
2023-01-12 13:20:38 +02:00
|
|
|
current++;
|
2022-12-28 04:17:46 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 16:37:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ParsedCommandLine cmdline;
|
|
|
|
|
|
|
|
static void parse_command_line()
|
|
|
|
{
|
|
|
|
if (!(g_multiboot_info->flags & 0x02))
|
|
|
|
return;
|
|
|
|
|
|
|
|
BAN::StringView full_command_line(g_kernel_cmdline);
|
|
|
|
auto arguments = MUST(full_command_line.split(' '));
|
|
|
|
|
|
|
|
for (auto argument : arguments)
|
|
|
|
{
|
|
|
|
if (argument == "noapic")
|
|
|
|
cmdline.force_pic = true;
|
|
|
|
else if (argument == "noserial")
|
|
|
|
cmdline.disable_serial = true;
|
|
|
|
else if (argument.size() > 5 && argument.substring(0, 5) == "root=")
|
|
|
|
cmdline.root = argument.substring(5);
|
|
|
|
}
|
2022-12-20 11:57:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 21:31:26 +02:00
|
|
|
struct Test
|
|
|
|
{
|
|
|
|
Test() { dprintln("construct (default)"); }
|
|
|
|
Test(const Test&) { dprintln("construct (copy)"); }
|
|
|
|
Test(Test&&) { dprintln("construct (move)"); }
|
|
|
|
~Test() { dprintln("destruct"); }
|
|
|
|
Test& operator=(const Test&) { dprintln("assign (copy)"); return *this; }
|
|
|
|
Test& operator=(Test&&) { dprintln("assign (move)"); return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace BAN::Formatter
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void print_argument(F putc, const Test& test, const ValueFormat& format)
|
|
|
|
{
|
|
|
|
print_argument(putc, &test, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-21 15:45:02 +03:00
|
|
|
extern "C" uint8_t g_userspace_start[];
|
|
|
|
extern "C" uint8_t g_userspace_end[];
|
2023-04-21 10:40:24 +03:00
|
|
|
|
2023-03-30 16:37:53 +03:00
|
|
|
static void init2(void*);
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-01-12 13:20:38 +02:00
|
|
|
extern "C" void kernel_main()
|
2022-11-15 00:32:07 +02:00
|
|
|
{
|
2023-02-01 01:53:35 +02:00
|
|
|
using namespace Kernel;
|
|
|
|
|
2022-11-15 00:32:07 +02:00
|
|
|
DISABLE_INTERRUPTS();
|
2022-11-14 00:27:11 +02:00
|
|
|
|
2023-03-30 16:37:53 +03:00
|
|
|
if (!should_disable_serial())
|
2023-06-12 22:37:34 +03:00
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
Serial::initialize();
|
2023-06-12 22:37:34 +03:00
|
|
|
dprintln("Serial output initialized");
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:20:38 +02:00
|
|
|
if (g_multiboot_magic != 0x2BADB002)
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
|
|
|
dprintln("Invalid multiboot magic number");
|
2022-12-10 00:33:03 +02:00
|
|
|
return;
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
2022-12-07 02:41:18 +02:00
|
|
|
|
2023-01-12 13:20:38 +02:00
|
|
|
kmalloc_initialize();
|
|
|
|
dprintln("kmalloc initialized");
|
2022-12-20 11:57:09 +02:00
|
|
|
|
2023-03-01 21:21:08 +02:00
|
|
|
GDT::initialize();
|
|
|
|
dprintln("GDT initialized");
|
|
|
|
|
2022-12-07 02:41:18 +02:00
|
|
|
IDT::initialize();
|
2023-05-28 18:08:26 +03:00
|
|
|
dprintln("IDT initialized");
|
2022-12-07 02:41:18 +02:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable::initialize();
|
|
|
|
dprintln("PageTable initialized");
|
2023-01-13 15:07:24 +02:00
|
|
|
|
2023-06-18 23:27:00 +03:00
|
|
|
Heap::initialize();
|
|
|
|
dprintln("Heap initialzed");
|
|
|
|
|
2023-04-14 23:55:05 +03:00
|
|
|
TerminalDriver* terminal_driver = VesaTerminalDriver::create();
|
|
|
|
ASSERT(terminal_driver);
|
|
|
|
dprintln("VESA initialized");
|
|
|
|
|
2023-07-10 23:17:14 +03:00
|
|
|
DevFileSystem::initialize();
|
|
|
|
dprintln("devfs initialized");
|
|
|
|
|
2023-04-14 23:55:05 +03:00
|
|
|
TTY* tty1 = new TTY(terminal_driver);
|
|
|
|
ASSERT(tty1);
|
2023-04-19 00:34:18 +03:00
|
|
|
dprintln("TTY initialized");
|
2023-04-14 23:55:05 +03:00
|
|
|
|
2023-03-30 16:37:53 +03:00
|
|
|
parse_command_line();
|
|
|
|
dprintln("command line parsed, root='{}'", cmdline.root);
|
|
|
|
|
2023-03-27 03:38:55 +03:00
|
|
|
MUST(ACPI::initialize());
|
|
|
|
dprintln("ACPI initialized");
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
InterruptController::initialize(cmdline.force_pic);
|
2023-01-23 20:13:57 +02:00
|
|
|
dprintln("Interrupt controller initialized");
|
2023-02-01 01:53:35 +02:00
|
|
|
|
2023-01-04 19:22:23 +02:00
|
|
|
PIT::initialize();
|
2023-01-13 15:07:24 +02:00
|
|
|
dprintln("PIT initialized");
|
2023-02-22 22:29:31 +02:00
|
|
|
|
2023-03-07 19:17:49 +02:00
|
|
|
MUST(Scheduler::initialize());
|
2023-07-10 23:17:14 +03:00
|
|
|
dprintln("Scheduler initialized");
|
|
|
|
|
2023-02-02 23:27:51 +02:00
|
|
|
Scheduler& scheduler = Scheduler::get();
|
2023-04-22 16:43:44 +03:00
|
|
|
Process::create_kernel(init2, tty1);
|
2023-03-16 12:17:04 +02:00
|
|
|
scheduler.start();
|
2023-04-19 00:41:24 +03:00
|
|
|
|
2023-04-12 17:51:36 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2023-03-16 12:17:04 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 23:55:05 +03:00
|
|
|
static void init2(void* tty1)
|
2023-03-16 12:17:04 +02:00
|
|
|
{
|
|
|
|
using namespace Kernel;
|
2023-03-29 03:05:16 +03:00
|
|
|
using namespace Kernel::Input;
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-07-10 23:17:14 +03:00
|
|
|
dprintln("Scheduler started");
|
2023-03-29 11:50:46 +03:00
|
|
|
|
2023-07-10 23:17:14 +03:00
|
|
|
DevFileSystem::get().initialize_device_updater();
|
|
|
|
|
|
|
|
PCI::initialize();
|
|
|
|
dprintln("PCI initialized");
|
|
|
|
|
2023-06-11 19:52:13 +03:00
|
|
|
VirtualFileSystem::initialize(cmdline.root);
|
2023-03-16 12:17:04 +02:00
|
|
|
|
2023-03-29 03:05:16 +03:00
|
|
|
if (auto res = PS2Controller::initialize(); res.is_error())
|
|
|
|
dprintln("{}", res.error());
|
|
|
|
|
2023-04-14 23:55:05 +03:00
|
|
|
((TTY*)tty1)->initialize_device();
|
2023-04-05 00:56:09 +03:00
|
|
|
|
2023-06-11 21:01:02 +03:00
|
|
|
MUST(Process::create_userspace({ 0, 0, 0, 0 }, "/usr/bin/init"sv));
|
2023-04-12 17:51:36 +03:00
|
|
|
}
|