2018-10-16 12:01:38 +03:00
|
|
|
#include "types.h"
|
|
|
|
#include "VGA.h"
|
|
|
|
#include "kmalloc.h"
|
|
|
|
#include "i386.h"
|
|
|
|
#include "i8253.h"
|
|
|
|
#include "Keyboard.h"
|
2018-11-01 15:15:46 +03:00
|
|
|
#include "Process.h"
|
2018-10-16 12:01:38 +03:00
|
|
|
#include "system.h"
|
|
|
|
#include "Disk.h"
|
|
|
|
#include "PIC.h"
|
|
|
|
#include "StdLib.h"
|
|
|
|
#include "Syscall.h"
|
|
|
|
#include "CMOS.h"
|
2018-10-16 15:17:43 +03:00
|
|
|
#include "IDEDiskDevice.h"
|
2018-10-16 15:33:16 +03:00
|
|
|
#include <VirtualFileSystem/NullDevice.h>
|
|
|
|
#include <VirtualFileSystem/ZeroDevice.h>
|
|
|
|
#include <VirtualFileSystem/FullDevice.h>
|
|
|
|
#include <VirtualFileSystem/RandomDevice.h>
|
2018-10-17 11:55:43 +03:00
|
|
|
#include <VirtualFileSystem/Ext2FileSystem.h>
|
2018-10-17 12:44:06 +03:00
|
|
|
#include <VirtualFileSystem/VirtualFileSystem.h>
|
2018-11-07 13:37:54 +03:00
|
|
|
#include <VirtualFileSystem/FileDescriptor.h>
|
2018-10-16 15:33:16 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2018-10-18 00:13:55 +03:00
|
|
|
#include "MemoryManager.h"
|
2018-10-18 16:38:04 +03:00
|
|
|
#include <ELFLoader/ELFLoader.h>
|
2018-10-21 22:59:43 +03:00
|
|
|
#include "Console.h"
|
2018-10-23 12:57:38 +03:00
|
|
|
#include "ProcFileSystem.h"
|
2018-10-25 18:29:49 +03:00
|
|
|
#include "RTC.h"
|
2018-10-30 15:59:29 +03:00
|
|
|
#include "VirtualConsole.h"
|
2018-11-08 00:15:02 +03:00
|
|
|
#include "Scheduler.h"
|
2018-10-16 12:01:38 +03:00
|
|
|
|
2018-10-22 12:15:16 +03:00
|
|
|
#define TEST_VFS
|
2018-10-27 16:02:39 +03:00
|
|
|
#define KSYMS
|
2018-11-01 13:30:48 +03:00
|
|
|
#define SPAWN_MULTIPLE_SHELLS
|
2018-10-23 16:41:55 +03:00
|
|
|
//#define STRESS_TEST_SPAWNING
|
2018-10-22 16:42:39 +03:00
|
|
|
//#define TEST_ELF_LOADER
|
2018-10-16 12:01:38 +03:00
|
|
|
|
|
|
|
system_t system;
|
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
VirtualConsole* tty0;
|
|
|
|
VirtualConsole* tty1;
|
|
|
|
VirtualConsole* tty2;
|
2018-10-31 02:27:34 +03:00
|
|
|
VirtualConsole* tty3;
|
2018-10-30 17:33:37 +03:00
|
|
|
Keyboard* keyboard;
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2018-10-26 23:32:35 +03:00
|
|
|
static byte parseHexDigit(char nibble)
|
|
|
|
{
|
|
|
|
if (nibble >= '0' && nibble <= '9')
|
|
|
|
return nibble - '0';
|
|
|
|
ASSERT(nibble >= 'a' && nibble <= 'f');
|
|
|
|
return 10 + (nibble - 'a');
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
#ifdef KSYMS
|
2018-11-01 01:19:15 +03:00
|
|
|
static Vector<KSym, KmallocEternalAllocator>* s_ksyms;
|
2018-11-02 01:04:34 +03:00
|
|
|
static bool s_ksyms_ready;
|
2018-10-26 23:32:35 +03:00
|
|
|
|
2018-11-01 01:19:15 +03:00
|
|
|
Vector<KSym, KmallocEternalAllocator>& ksyms()
|
2018-10-26 23:32:35 +03:00
|
|
|
{
|
|
|
|
return *s_ksyms;
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
volatile bool ksyms_ready()
|
|
|
|
{
|
|
|
|
return s_ksyms_ready;
|
|
|
|
}
|
|
|
|
|
2018-10-27 01:14:24 +03:00
|
|
|
const KSym* ksymbolicate(dword address)
|
|
|
|
{
|
|
|
|
if (address < ksyms().first().address || address > ksyms().last().address)
|
|
|
|
return nullptr;
|
|
|
|
for (unsigned i = 0; i < ksyms().size(); ++i) {
|
|
|
|
if (address < ksyms()[i + 1].address)
|
|
|
|
return &ksyms()[i];
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-27 16:02:39 +03:00
|
|
|
static void loadKsyms(const ByteBuffer& buffer)
|
2018-10-26 23:32:35 +03:00
|
|
|
{
|
2018-11-01 01:19:15 +03:00
|
|
|
// FIXME: It's gross that this vector grows dynamically rather than being sized-to-fit.
|
|
|
|
// We're wasting that eternal kmalloc memory.
|
|
|
|
s_ksyms = new Vector<KSym, KmallocEternalAllocator>;
|
2018-10-26 23:32:35 +03:00
|
|
|
auto* bufptr = (const char*)buffer.pointer();
|
|
|
|
auto* startOfName = bufptr;
|
|
|
|
dword address = 0;
|
|
|
|
|
|
|
|
while (bufptr < buffer.endPointer()) {
|
|
|
|
for (unsigned i = 0; i < 8; ++i)
|
|
|
|
address = (address << 4) | parseHexDigit(*(bufptr++));
|
|
|
|
bufptr += 3;
|
|
|
|
startOfName = bufptr;
|
|
|
|
while (*(++bufptr)) {
|
|
|
|
if (*bufptr == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 01:19:15 +03:00
|
|
|
// FIXME: The Strings here should be eternally allocated too.
|
2018-10-26 23:32:35 +03:00
|
|
|
ksyms().append({ address, String(startOfName, bufptr - startOfName) });
|
|
|
|
++bufptr;
|
|
|
|
}
|
2018-11-02 01:04:34 +03:00
|
|
|
s_ksyms_ready = true;
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:48:07 +03:00
|
|
|
void dump_backtrace(bool use_ksyms)
|
2018-11-02 01:04:34 +03:00
|
|
|
{
|
2018-11-05 15:48:07 +03:00
|
|
|
if (!current) {
|
|
|
|
HANG;
|
2018-11-02 01:04:34 +03:00
|
|
|
return;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
2018-11-02 01:04:34 +03:00
|
|
|
extern volatile bool ksyms_ready();
|
2018-11-05 15:48:07 +03:00
|
|
|
if (use_ksyms && !ksyms_ready()) {
|
|
|
|
HANG;
|
2018-11-02 01:04:34 +03:00
|
|
|
return;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
2018-11-02 01:04:34 +03:00
|
|
|
struct RecognizedSymbol {
|
|
|
|
dword address;
|
|
|
|
const KSym* ksym;
|
|
|
|
};
|
|
|
|
Vector<RecognizedSymbol> recognizedSymbols;
|
2018-11-05 15:48:07 +03:00
|
|
|
if (use_ksyms) {
|
|
|
|
for (dword* stackPtr = (dword*)&use_ksyms; current->isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
|
|
|
|
dword retaddr = stackPtr[1];
|
|
|
|
if (auto* ksym = ksymbolicate(retaddr))
|
|
|
|
recognizedSymbols.append({ retaddr, ksym });
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
for (dword* stackPtr = (dword*)&use_ksyms; current->isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
|
|
|
|
dword retaddr = stackPtr[1];
|
|
|
|
kprintf("%x (next: %x)\n", retaddr, stackPtr ? (dword*)*stackPtr : 0);
|
|
|
|
}
|
|
|
|
return;
|
2018-11-02 01:04:34 +03:00
|
|
|
}
|
|
|
|
size_t bytesNeeded = 0;
|
|
|
|
for (auto& symbol : recognizedSymbols) {
|
|
|
|
bytesNeeded += symbol.ksym->name.length() + 8 + 16;
|
|
|
|
}
|
|
|
|
for (auto& symbol : recognizedSymbols) {
|
|
|
|
unsigned offset = symbol.address - symbol.ksym->address;
|
|
|
|
dbgprintf("%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
|
|
|
|
}
|
2018-10-26 23:32:35 +03:00
|
|
|
}
|
2018-11-02 01:04:34 +03:00
|
|
|
#endif
|
2018-10-26 23:32:35 +03:00
|
|
|
|
2018-11-01 16:41:49 +03:00
|
|
|
static void spawn_stress() NORETURN;
|
|
|
|
static void spawn_stress()
|
|
|
|
{
|
|
|
|
dword lastAlloc = sum_alloc;
|
|
|
|
|
2018-11-01 18:23:12 +03:00
|
|
|
for (unsigned i = 0; i < 10000; ++i) {
|
2018-11-01 16:41:49 +03:00
|
|
|
int error;
|
2018-11-03 12:49:13 +03:00
|
|
|
Process::create_user_process("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty0);
|
2018-11-09 03:25:31 +03:00
|
|
|
kprintf("malloc stats: alloc:%u free:%u page_aligned:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_page_aligned, kmalloc_sum_eternal);
|
|
|
|
kprintf("delta:%u\n", sum_alloc - lastAlloc);
|
2018-11-01 16:41:49 +03:00
|
|
|
lastAlloc = sum_alloc;
|
2018-11-01 18:23:12 +03:00
|
|
|
sleep(60);
|
2018-11-01 16:41:49 +03:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
asm volatile("hlt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 12:15:16 +03:00
|
|
|
static void init_stage2() NORETURN;
|
|
|
|
static void init_stage2()
|
2018-10-16 12:01:38 +03:00
|
|
|
{
|
|
|
|
Syscall::initialize();
|
|
|
|
|
|
|
|
Disk::initialize();
|
|
|
|
|
2018-10-21 23:11:46 +03:00
|
|
|
#ifdef TEST_VFS
|
2018-10-17 12:44:06 +03:00
|
|
|
auto vfs = make<VirtualFileSystem>();
|
|
|
|
|
|
|
|
auto dev_zero = make<ZeroDevice>();
|
2018-10-30 15:59:29 +03:00
|
|
|
vfs->registerCharacterDevice(*dev_zero);
|
2018-10-17 12:44:06 +03:00
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
auto dev_null = make<NullDevice>();
|
2018-10-30 15:59:29 +03:00
|
|
|
vfs->registerCharacterDevice(*dev_null);
|
2018-10-17 12:44:06 +03:00
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
auto dev_full = make<FullDevice>();
|
2018-10-30 15:59:29 +03:00
|
|
|
vfs->registerCharacterDevice(*dev_full);
|
2018-10-17 12:44:06 +03:00
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
auto dev_random = make<RandomDevice>();
|
2018-10-30 15:59:29 +03:00
|
|
|
vfs->registerCharacterDevice(*dev_random);
|
|
|
|
|
|
|
|
vfs->registerCharacterDevice(*keyboard);
|
2018-10-16 15:17:43 +03:00
|
|
|
|
2018-10-30 15:59:29 +03:00
|
|
|
vfs->registerCharacterDevice(*tty0);
|
|
|
|
vfs->registerCharacterDevice(*tty1);
|
|
|
|
vfs->registerCharacterDevice(*tty2);
|
2018-10-31 02:27:34 +03:00
|
|
|
vfs->registerCharacterDevice(*tty3);
|
2018-10-23 11:12:50 +03:00
|
|
|
|
2018-10-17 12:44:06 +03:00
|
|
|
auto dev_hd0 = IDEDiskDevice::create();
|
2018-10-17 11:55:43 +03:00
|
|
|
auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
|
2018-10-22 12:15:16 +03:00
|
|
|
e2fs->initialize();
|
2018-10-17 11:55:43 +03:00
|
|
|
|
2018-10-17 12:44:06 +03:00
|
|
|
vfs->mountRoot(e2fs.copyRef());
|
|
|
|
|
2018-10-27 16:02:39 +03:00
|
|
|
#ifdef KSYMS
|
2018-10-26 23:32:35 +03:00
|
|
|
{
|
2018-10-28 16:25:51 +03:00
|
|
|
int error;
|
2018-11-07 13:37:54 +03:00
|
|
|
auto descriptor = vfs->open("/kernel.map", error);
|
|
|
|
if (!descriptor) {
|
2018-10-26 23:32:35 +03:00
|
|
|
kprintf("Failed to open /kernel.map\n");
|
|
|
|
} else {
|
2018-11-07 13:37:54 +03:00
|
|
|
auto buffer = descriptor->readEntireFile();
|
2018-10-26 23:32:35 +03:00
|
|
|
ASSERT(buffer);
|
2018-10-27 16:02:39 +03:00
|
|
|
loadKsyms(buffer);
|
2018-10-26 23:32:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-26 18:42:12 +03:00
|
|
|
vfs->mount(ProcFileSystem::the(), "/proc");
|
2018-10-17 12:47:14 +03:00
|
|
|
|
2018-10-18 11:28:09 +03:00
|
|
|
#endif
|
2018-10-17 13:07:39 +03:00
|
|
|
|
2018-10-19 12:28:43 +03:00
|
|
|
#ifdef TEST_ELF_LOADER
|
2018-10-18 16:38:04 +03:00
|
|
|
{
|
2018-10-22 15:41:54 +03:00
|
|
|
auto testExecutable = vfs->open("/bin/id");
|
2018-10-18 16:38:04 +03:00
|
|
|
ASSERT(testExecutable);
|
|
|
|
auto testExecutableData = testExecutable->readEntireFile();
|
|
|
|
ASSERT(testExecutableData);
|
2018-10-22 12:15:16 +03:00
|
|
|
|
2018-10-18 16:38:04 +03:00
|
|
|
ExecSpace space;
|
|
|
|
space.loadELF(move(testExecutableData));
|
2018-11-04 15:52:53 +03:00
|
|
|
auto* elf_entry = space.symbol_ptr("_start");
|
2018-10-18 16:38:04 +03:00
|
|
|
ASSERT(elf_entry);
|
|
|
|
|
|
|
|
typedef int (*MainFunctionPtr)(void);
|
|
|
|
kprintf("elf_entry: %p\n", elf_entry);
|
|
|
|
int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
|
|
|
|
kprintf("it returned %d\n", rc);
|
|
|
|
}
|
2018-10-19 12:28:43 +03:00
|
|
|
#endif
|
|
|
|
|
2018-10-30 17:33:37 +03:00
|
|
|
int error;
|
2018-11-08 01:13:38 +03:00
|
|
|
Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty0);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef SPAWN_MULTIPLE_SHELLS
|
2018-11-08 01:13:38 +03:00
|
|
|
Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty1);
|
|
|
|
Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty2);
|
|
|
|
Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty3);
|
2018-11-01 13:30:48 +03:00
|
|
|
#endif
|
2018-10-30 17:33:37 +03:00
|
|
|
|
2018-11-01 16:41:49 +03:00
|
|
|
#ifdef STRESS_TEST_SPAWNING
|
2018-11-08 01:13:38 +03:00
|
|
|
Process::create_kernel_process(spawn_stress, "spawn_stress");
|
2018-11-01 16:41:49 +03:00
|
|
|
#endif
|
|
|
|
|
2018-11-08 01:13:38 +03:00
|
|
|
current->sys$exit(0);
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-22 12:15:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
cli();
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
#ifdef KSYMS
|
|
|
|
s_ksyms = nullptr;
|
|
|
|
s_ksyms_ready = false;
|
|
|
|
#endif
|
|
|
|
|
2018-10-22 12:15:16 +03:00
|
|
|
kmalloc_init();
|
|
|
|
vga_init();
|
|
|
|
|
2018-10-31 01:01:48 +03:00
|
|
|
auto console = make<Console>();
|
|
|
|
|
2018-10-25 18:29:49 +03:00
|
|
|
RTC::initialize();
|
2018-10-22 12:15:16 +03:00
|
|
|
PIC::initialize();
|
|
|
|
gdt_init();
|
|
|
|
idt_init();
|
|
|
|
|
2018-10-30 17:33:37 +03:00
|
|
|
keyboard = new Keyboard;
|
|
|
|
|
|
|
|
VirtualConsole::initialize();
|
|
|
|
tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
|
|
|
|
tty1 = new VirtualConsole(1);
|
|
|
|
tty2 = new VirtualConsole(2);
|
2018-10-31 02:27:34 +03:00
|
|
|
tty3 = new VirtualConsole(3);
|
2018-11-01 16:09:21 +03:00
|
|
|
VirtualConsole::switch_to(0);
|
2018-10-30 17:33:37 +03:00
|
|
|
|
2018-10-31 22:10:39 +03:00
|
|
|
kprintf("Starting Serenity Operating System...\n");
|
|
|
|
|
2018-10-22 12:15:16 +03:00
|
|
|
MemoryManager::initialize();
|
|
|
|
|
2018-10-22 14:10:08 +03:00
|
|
|
VirtualFileSystem::initializeGlobals();
|
|
|
|
StringImpl::initializeGlobals();
|
|
|
|
|
2018-10-22 12:15:16 +03:00
|
|
|
PIT::initialize();
|
|
|
|
|
|
|
|
memset(&system, 0, sizeof(system));
|
2018-10-25 18:29:49 +03:00
|
|
|
|
2018-10-22 12:15:16 +03:00
|
|
|
WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
|
|
|
|
WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
|
|
|
|
|
|
|
|
kprintf("%u kB base memory\n", base_memory);
|
|
|
|
kprintf("%u kB extended memory\n", ext_memory);
|
|
|
|
|
2018-10-26 18:42:12 +03:00
|
|
|
auto procfs = ProcFileSystem::create();
|
|
|
|
procfs->initialize();
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
Process::initialize();
|
2018-11-08 01:13:38 +03:00
|
|
|
Process::create_kernel_process(init_stage2, "init_stage2");
|
2018-10-23 16:41:55 +03:00
|
|
|
|
2018-11-08 00:15:02 +03:00
|
|
|
Scheduler::pick_next();
|
2018-10-22 12:15:16 +03:00
|
|
|
|
|
|
|
sti();
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
// This now becomes the idle process :^)
|
2018-10-16 12:01:38 +03:00
|
|
|
for (;;) {
|
|
|
|
asm("hlt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:54:11 +03:00
|
|
|
void log_try_lock(const char* where)
|
|
|
|
{
|
|
|
|
kprintf("[%u] >>> locking... (%s)\n", current->pid(), where);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_locked(const char* where)
|
|
|
|
{
|
|
|
|
kprintf("[%u] >>> locked() in %s\n", current->pid(), where);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_unlocked(const char* where)
|
|
|
|
{
|
|
|
|
kprintf("[%u] <<< unlocked()\n", current->pid(), where);
|
|
|
|
}
|