2018-10-16 12:01:38 +03:00
|
|
|
#include "types.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 "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-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-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-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-09 12:03:21 +03:00
|
|
|
bool ksyms_ready()
|
2018-11-02 01:04:34 +03:00
|
|
|
{
|
|
|
|
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;
|
2018-11-09 22:40:39 +03:00
|
|
|
dword ksym_count = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 8; ++i)
|
|
|
|
ksym_count = (ksym_count << 4) | parseHexDigit(*(bufptr++));
|
|
|
|
s_ksyms->ensureCapacity(ksym_count);
|
|
|
|
++bufptr; // skip newline
|
|
|
|
|
|
|
|
kprintf("Loading ksyms: \033[s");
|
2018-10-26 23:32:35 +03:00
|
|
|
|
|
|
|
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) });
|
2018-11-09 22:40:39 +03:00
|
|
|
|
2018-11-13 02:17:30 +03:00
|
|
|
if ((ksyms().size() % 10) == 0 || ksym_count == ksyms().size())
|
|
|
|
kprintf("\033[u\033[s%u/%u", ksyms().size(), ksym_count);
|
2018-10-26 23:32:35 +03:00
|
|
|
++bufptr;
|
|
|
|
}
|
2018-11-09 22:40:39 +03:00
|
|
|
kprintf("\n");
|
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
|
|
|
}
|
|
|
|
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-09 12:03:21 +03:00
|
|
|
#ifdef STRESS_TEST_SPAWNING
|
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-11-09 12:03:21 +03:00
|
|
|
#endif
|
2018-11-01 16:41:49 +03:00
|
|
|
|
2018-12-20 04:41:55 +03:00
|
|
|
static void syncd() NORETURN;
|
|
|
|
static void syncd()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
Syscall::sync();
|
|
|
|
sleep(10 * TICKS_PER_SECOND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2018-11-15 16:43:10 +03:00
|
|
|
auto vfs = make<VFS>();
|
2018-10-17 12:44:06 +03:00
|
|
|
|
|
|
|
auto dev_zero = make<ZeroDevice>();
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->register_character_device(*dev_zero);
|
2018-10-17 12:44:06 +03:00
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
auto dev_null = make<NullDevice>();
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->register_character_device(*dev_null);
|
2018-10-17 12:44:06 +03:00
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
auto dev_full = make<FullDevice>();
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->register_character_device(*dev_full);
|
2018-10-17 12:44:06 +03:00
|
|
|
|
2018-10-16 15:33:16 +03:00
|
|
|
auto dev_random = make<RandomDevice>();
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->register_character_device(*dev_random);
|
2018-10-30 15:59:29 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->register_character_device(*keyboard);
|
2018-10-16 15:17:43 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->register_character_device(*tty0);
|
|
|
|
vfs->register_character_device(*tty1);
|
|
|
|
vfs->register_character_device(*tty2);
|
|
|
|
vfs->register_character_device(*tty3);
|
2018-10-23 11:12:50 +03:00
|
|
|
|
2018-10-17 12:44:06 +03:00
|
|
|
auto dev_hd0 = IDEDiskDevice::create();
|
2018-11-15 19:13:10 +03:00
|
|
|
auto e2fs = Ext2FS::create(dev_hd0.copyRef());
|
2018-10-22 12:15:16 +03:00
|
|
|
e2fs->initialize();
|
2018-10-17 11:55:43 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
vfs->mount_root(e2fs.copyRef());
|
2018-10-17 12:44:06 +03:00
|
|
|
|
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-12-03 02:39:25 +03:00
|
|
|
auto buffer = descriptor->read_entire_file();
|
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-11-15 19:13:10 +03:00
|
|
|
vfs->mount(ProcFS::the(), "/proc");
|
2018-10-17 12:47:14 +03:00
|
|
|
|
2018-11-11 17:36:40 +03:00
|
|
|
Vector<String> environment;
|
|
|
|
environment.append("TERM=ansi");
|
|
|
|
|
2018-10-30 17:33:37 +03:00
|
|
|
int error;
|
2018-11-11 17:36:40 +03:00
|
|
|
Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), move(environment), 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
|
|
|
}
|
|
|
|
|
2018-11-09 12:03:21 +03:00
|
|
|
void init() NORETURN;
|
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();
|
|
|
|
|
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-11-15 17:10:12 +03:00
|
|
|
VFS::initialize_globals();
|
2018-10-22 14:10:08 +03:00
|
|
|
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-11-17 02:11:08 +03:00
|
|
|
word base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
|
|
|
|
word ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
|
2018-10-22 12:15:16 +03:00
|
|
|
|
|
|
|
kprintf("%u kB base memory\n", base_memory);
|
|
|
|
kprintf("%u kB extended memory\n", ext_memory);
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
auto procfs = ProcFS::create();
|
2018-10-26 18:42:12 +03:00
|
|
|
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-12-20 04:41:55 +03:00
|
|
|
Process::create_kernel_process(syncd, "syncd");
|
|
|
|
|
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);
|
|
|
|
}
|