2016-05-02 07:25:08 +00:00
|
|
|
#include "console.hpp"
|
2016-05-03 14:11:36 +00:00
|
|
|
#include "pmm.hpp"
|
|
|
|
#include "numeric.hpp"
|
2016-05-03 15:18:30 +00:00
|
|
|
#include "pointer.hpp"
|
2016-05-04 08:17:56 +00:00
|
|
|
#include "multiboot.hpp"
|
2016-05-05 11:15:29 +00:00
|
|
|
#include "gdt.hpp"
|
2016-05-05 13:59:48 +00:00
|
|
|
#include "idt.hpp"
|
2016-05-01 18:22:12 +00:00
|
|
|
#include "compat.h"
|
2016-05-05 17:02:23 +00:00
|
|
|
#include "io.hpp"
|
2016-05-07 12:10:37 +00:00
|
|
|
#include "vmm.hpp"
|
2016-05-07 17:55:59 +00:00
|
|
|
#include "elf.hpp"
|
|
|
|
#include "bsod.hpp"
|
2016-05-01 18:22:12 +00:00
|
|
|
|
2016-05-18 08:31:05 +00:00
|
|
|
#include "asm.hpp"
|
|
|
|
|
2016-05-06 17:37:43 +00:00
|
|
|
#include "driver/timer.hpp"
|
2016-05-06 07:44:02 +00:00
|
|
|
#include "driver/keyboard.hpp"
|
|
|
|
|
2016-05-07 15:00:08 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <new>
|
|
|
|
|
2016-05-07 17:55:59 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-05-30 17:48:07 +00:00
|
|
|
namespace vm {
|
|
|
|
#include <vm.h>
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:40:11 +00:00
|
|
|
using namespace multiboot;
|
2016-05-04 15:42:09 +00:00
|
|
|
using namespace console_tools;
|
2016-05-04 08:40:11 +00:00
|
|
|
|
2016-05-04 16:00:16 +00:00
|
|
|
struct dummy;
|
|
|
|
|
|
|
|
// Symbols generated by linker, no useful content in there...
|
|
|
|
extern dummy kernelStartMarker;
|
|
|
|
extern dummy kernelEndMarker;
|
|
|
|
|
2016-05-06 17:37:43 +00:00
|
|
|
driver::Timer timer;
|
2016-05-06 07:44:02 +00:00
|
|
|
driver::Keyboard keyboardDriver;
|
|
|
|
|
2016-05-18 08:12:01 +00:00
|
|
|
VMMContext * kernelContext;
|
2016-05-07 15:00:08 +00:00
|
|
|
|
2016-05-30 17:48:07 +00:00
|
|
|
/*
|
2016-05-07 17:55:59 +00:00
|
|
|
static const uint32_t entryPointAddress = 0x40000000;
|
|
|
|
|
2016-05-07 15:00:08 +00:00
|
|
|
void run_program0(Module const & module)
|
|
|
|
{
|
2016-05-07 17:55:59 +00:00
|
|
|
using namespace elf;
|
|
|
|
|
|
|
|
const Header *header = module.start.data<elf::Header>();
|
|
|
|
|
|
|
|
ProgramHeader *ph;
|
|
|
|
int i;
|
|
|
|
if (header->magic != MAGIC) {
|
|
|
|
BSOD::die(Error::InvalidELFImage, "Keine gueltige ELF-Magic!\n");
|
|
|
|
return;
|
2016-05-07 15:00:08 +00:00
|
|
|
}
|
2016-05-07 17:55:59 +00:00
|
|
|
|
|
|
|
ph = (ProgramHeader*)(((char*) header) + header->ph_offset);
|
|
|
|
for (i = 0; i < header->ph_entry_count; i++, ph++) {
|
|
|
|
void* dest = (void*) ph->virt_addr;
|
|
|
|
void* src = ((char*) header) + ph->offset;
|
|
|
|
|
2016-05-30 17:48:07 +00:00
|
|
|
// Nur Program Header vom Typ LOAD laden
|
2016-05-07 17:55:59 +00:00
|
|
|
if (ph->type != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ph->virt_addr < entryPointAddress) {
|
|
|
|
BSOD::die(Error::InvalidELFImage, "A LOAD section tried to sneak into the kernel!");
|
|
|
|
}
|
|
|
|
|
|
|
|
for(uint32_t i = 0; i < ph->mem_size; i += 0x1000) {
|
|
|
|
kernelContext->provide(
|
|
|
|
virtual_t(ph->virt_addr + i),
|
|
|
|
VMMFlags::Writable | VMMFlags::UserSpace);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(dest, 0, ph->mem_size);
|
|
|
|
memcpy(dest, src, ph->file_size);
|
2016-05-07 15:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using EntryPoint = void (*)();
|
2016-05-07 17:55:59 +00:00
|
|
|
EntryPoint ep = (EntryPoint)entryPointAddress;
|
2016-05-07 15:00:08 +00:00
|
|
|
ep();
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:55:59 +00:00
|
|
|
static void dump_elf(elf::Header *header)
|
|
|
|
{
|
|
|
|
using namespace elf;
|
|
|
|
ProgramHeader *ph;
|
|
|
|
int i;
|
|
|
|
|
2016-05-30 17:48:07 +00:00
|
|
|
// Ist es ueberhaupt eine ELF-Datei?
|
2016-05-07 17:55:59 +00:00
|
|
|
if (header->magic != MAGIC) {
|
|
|
|
BSOD::die(Error::InvalidELFImage, "Keine gueltige ELF-Magic!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ph = (ProgramHeader*)(((char*) header) + header->ph_offset);
|
|
|
|
for (i = 0; i < header->ph_entry_count; i++, ph++) {
|
|
|
|
void* dest = (void*) ph->virt_addr;
|
|
|
|
void* src = ((char*) header) + ph->offset;
|
|
|
|
|
|
|
|
Console::main
|
|
|
|
<< "Header: " << ph->type << ", "
|
|
|
|
<< "Source: " << src << ", "
|
|
|
|
<< "Dest: " << dest << ", "
|
|
|
|
<< "Memsize: " << ph->mem_size << ", "
|
|
|
|
<< "Filesize: " << ph->file_size
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
2016-05-30 17:48:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void strcpy(char *dst, const char *src)
|
|
|
|
{
|
|
|
|
while((*dst++ = *src++));
|
|
|
|
}
|
2016-05-07 17:55:59 +00:00
|
|
|
|
2016-05-18 08:24:36 +00:00
|
|
|
static void initializePMM(Structure const & data)
|
2016-05-07 19:49:54 +00:00
|
|
|
{
|
2016-05-18 08:24:36 +00:00
|
|
|
for(auto &mmap : data.memoryMaps) {
|
|
|
|
if(mmap.length == 0) {
|
|
|
|
continue;
|
2016-05-04 14:33:59 +00:00
|
|
|
}
|
2016-05-05 09:35:08 +00:00
|
|
|
if(mmap.isFree() == false) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-18 08:24:36 +00:00
|
|
|
|
|
|
|
//Console::main
|
|
|
|
//<< "mmap: "
|
|
|
|
//<< "start: " << hex(mmap.base) << ", length: " << hex(mmap.length)
|
|
|
|
//<< ", " << mmap.entry_size
|
|
|
|
//<< ", " << sizeof(mmap)
|
|
|
|
//<< "\n";
|
2016-05-04 15:42:09 +00:00
|
|
|
if(mmap.base > 0xFFFFFFFF) {
|
2016-05-18 08:24:36 +00:00
|
|
|
//Console::main << "mmap out of 4 gigabyte range." << "\n";
|
2016-05-04 15:42:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-04 16:00:16 +00:00
|
|
|
if(mmap.isFree()) {
|
2016-05-04 15:42:09 +00:00
|
|
|
// Mark all free memory free...
|
2016-05-04 16:00:16 +00:00
|
|
|
physical_t lower = physical_t(mmap.base).alignUpper(4096);
|
|
|
|
physical_t upper = physical_t(mmap.base + mmap.length).alignLower(4096);
|
|
|
|
|
|
|
|
uint32_t ptr = lower.numeric();
|
|
|
|
while (ptr < upper.numeric()) {
|
|
|
|
PMM::markFree(physical_t(ptr));
|
|
|
|
ptr += 0x1000;
|
2016-05-04 15:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-04 09:51:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 16:00:16 +00:00
|
|
|
// Mark all memory used by the kernel used...
|
|
|
|
physical_t lower = physical_t(&kernelStartMarker).alignLower(4096);
|
|
|
|
physical_t upper = physical_t(&kernelEndMarker).alignUpper(4096);
|
|
|
|
|
|
|
|
uint32_t ptr = lower.numeric();
|
|
|
|
while (ptr < upper.numeric()) {
|
|
|
|
PMM::markUsed(physical_t(ptr));
|
|
|
|
ptr += 0x1000;
|
|
|
|
}
|
2016-05-18 08:24:36 +00:00
|
|
|
|
2016-05-07 12:10:37 +00:00
|
|
|
// nullptr is not valid.
|
|
|
|
PMM::markUsed(physical_t(nullptr));
|
2016-05-18 08:24:36 +00:00
|
|
|
|
|
|
|
// Mark the video memory as used.
|
|
|
|
PMM::markUsed(physical_t(0xB8000));
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void init(Structure const & data)
|
|
|
|
{
|
|
|
|
Console::main
|
|
|
|
<< "Hello World!\n"
|
|
|
|
<< FColor(Color::Yellow) << "Hello color!" << FColor() << "\n"
|
|
|
|
<< BColor(Color::Blue) << "Hello blue!" << BColor() << "\n"
|
|
|
|
<< "Hello default color.\n";
|
|
|
|
|
|
|
|
GDT::initialize();
|
|
|
|
|
|
|
|
Console::main
|
|
|
|
<< "bootloader name: " << data.bootLoaderName << "\n"
|
|
|
|
<< "command line: " << data.commandline << "\n"
|
|
|
|
<< "count of modules: " << data.modules.length << "\n"
|
|
|
|
<< "count of mmaps: " << data.memoryMaps.length << "\n";
|
|
|
|
|
|
|
|
initializePMM(data);
|
2016-05-04 16:00:16 +00:00
|
|
|
|
2016-05-05 09:35:08 +00:00
|
|
|
auto freeMemory = PMM::getFreeMemory();
|
|
|
|
Console::main
|
|
|
|
<< "Free: "
|
|
|
|
<< (freeMemory >> 20) << "MB, "
|
|
|
|
<< (freeMemory >> 10) << "KB, "
|
|
|
|
<< (freeMemory >> 0) << "B, "
|
|
|
|
<< (freeMemory >> 12) << "Pages\n";
|
2016-05-05 17:02:23 +00:00
|
|
|
|
2016-05-05 13:59:48 +00:00
|
|
|
IDT::initialize();
|
2016-05-07 12:10:37 +00:00
|
|
|
Console::main << "Interrupts set up.\n";
|
|
|
|
|
|
|
|
Console::main << "Creating VMM Context...\n";
|
2016-05-07 15:00:08 +00:00
|
|
|
kernelContext = new (PMM::alloc().data()) VMMContext();
|
2016-05-07 12:10:37 +00:00
|
|
|
|
|
|
|
Console::main << "Mapping memory...\n";
|
|
|
|
for(uint32_t addr = 0; addr < 4096 * 1024; addr += 0x1000) {
|
2016-05-07 15:00:08 +00:00
|
|
|
kernelContext->map(
|
2016-05-07 12:10:37 +00:00
|
|
|
virtual_t(addr),
|
|
|
|
physical_t(addr),
|
|
|
|
VMMFlags::Writable | VMMFlags::UserSpace);
|
|
|
|
}
|
2016-05-07 16:51:34 +00:00
|
|
|
kernelContext->map(
|
|
|
|
virtual_t(kernelContext),
|
|
|
|
physical_t(kernelContext),
|
|
|
|
VMMFlags::Writable);
|
2016-05-07 12:10:37 +00:00
|
|
|
Console::main << "Active Context...\n";
|
2016-05-07 15:00:08 +00:00
|
|
|
VMM::activate(*kernelContext);
|
2016-05-07 12:10:37 +00:00
|
|
|
|
|
|
|
Console::main << "Active Paging...\n";
|
|
|
|
VMM::enable();
|
|
|
|
Console::main << "Virtual Memory Management ready.\n";
|
2016-05-06 07:28:30 +00:00
|
|
|
|
2016-05-06 17:37:43 +00:00
|
|
|
timer.install();
|
2016-05-06 07:44:02 +00:00
|
|
|
keyboardDriver.install();
|
2016-05-07 19:49:54 +00:00
|
|
|
|
|
|
|
Console::main << "Drivers installed.\n";
|
2016-05-05 17:02:23 +00:00
|
|
|
|
2016-05-18 08:31:05 +00:00
|
|
|
//asm volatile("sti");
|
|
|
|
ASM::sti();
|
2016-05-05 17:02:23 +00:00
|
|
|
|
2016-05-07 12:10:37 +00:00
|
|
|
Console::main << "Interrupts enabled.\n";
|
|
|
|
|
2016-05-18 08:24:36 +00:00
|
|
|
if(data.modules.length > 0)
|
2016-05-07 15:00:08 +00:00
|
|
|
{
|
2016-05-30 17:48:07 +00:00
|
|
|
// Console::main << "ELF Modukle:\n";
|
|
|
|
// dump_elf(data.modules[0].start.data<elf::Header>());
|
|
|
|
// run_program0(data.modules[0]);
|
|
|
|
|
|
|
|
vm::Module module = {
|
|
|
|
.code = reinterpret_cast<vm::Instruction*>(data.modules[0].start.data()),
|
|
|
|
.length = data.modules[0].size() / sizeof(vm::Instruction),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Console::main << "Loaded instructions: " << module.length << "\n";
|
|
|
|
|
|
|
|
uint8_t page0[64];
|
|
|
|
uint8_t page1[64];
|
|
|
|
|
|
|
|
strcpy((char*)page0, "Hallo Welt!\nDies ist die erste Ausgabe durch die VM.");
|
|
|
|
|
|
|
|
uint8_t *pages[2];
|
|
|
|
pages[0] = page0;
|
|
|
|
pages[1] = page1;
|
|
|
|
|
|
|
|
|
|
|
|
vm::VirtualMemoryMap mmap = {
|
|
|
|
.pageSize = 64,
|
|
|
|
.length = 2,
|
|
|
|
.pages = pages,
|
|
|
|
};
|
|
|
|
|
|
|
|
vm::Process process = {
|
|
|
|
.module = &module,
|
|
|
|
.tag = (void*)1,
|
|
|
|
.codePointer = 0,
|
|
|
|
.stackPointer = 0,
|
|
|
|
.basePointer = 0,
|
|
|
|
.flags = 0,
|
|
|
|
.stack = { 0 },
|
|
|
|
.mmap = mmap,
|
|
|
|
};
|
|
|
|
|
|
|
|
auto *p = &process;
|
|
|
|
|
|
|
|
while(vm::vm_step_process(p) && p->tag) {
|
|
|
|
//Console::main << "?";
|
|
|
|
// dump_proc(p);
|
|
|
|
}
|
|
|
|
|
2016-05-07 15:00:08 +00:00
|
|
|
}
|
2016-05-07 12:10:37 +00:00
|
|
|
|
2016-05-05 17:02:23 +00:00
|
|
|
while(true);
|
2016-05-01 18:22:12 +00:00
|
|
|
}
|
2016-05-03 14:11:36 +00:00
|
|
|
|
2016-05-30 17:48:07 +00:00
|
|
|
static_assert(sizeof(void*) == 4, "Target platform is not 32 bit.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace vm
|
|
|
|
{
|
|
|
|
extern "C" void vm_assert(int assertion, const char *msg)
|
|
|
|
{
|
|
|
|
if(assertion != 0)
|
|
|
|
return;
|
|
|
|
BSOD::die(Error::VMError, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void vm_syscall(Process *p, CommandInfo *info)
|
|
|
|
{
|
|
|
|
switch(info->additional)
|
|
|
|
{
|
|
|
|
case 0: // EXIT
|
|
|
|
p->tag = NULL;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
Console::main << (char)info->input0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
Console::main << info->input0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Console::main << "[SYSCALL " << info->additional << "]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void vm_hwio(Process *p, CommandInfo *info)
|
|
|
|
{
|
|
|
|
BSOD::die(Error::VMError, "hwio not implemented yet.");
|
|
|
|
}
|
|
|
|
}
|