89 lines
2.2 KiB
C++
89 lines
2.2 KiB
C++
|
|
#include "os/tty.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include "boot.hpp"
|
|
|
|
extern "C" int gKernelEnd;
|
|
|
|
namespace
|
|
{
|
|
void initHeapFromEfiMemoryMap(const EfiMemoryMap& memoryMap) noexcept
|
|
{
|
|
#if 0
|
|
const uint64_t minAddr = reinterpret_cast<uint64_t>(&gKernelEnd);
|
|
for (uint32_t addr = firstEntryAddr; addr < firstEntryAddr + bufferLength;)
|
|
{
|
|
multiboot_memory_map_t entry = *reinterpret_cast<multiboot_memory_map_t*>(addr);
|
|
|
|
if(entry.type == MULTIBOOT_MEMORY_AVAILABLE)
|
|
{
|
|
std::printf("Start Addr: %X | Length: %b\n",
|
|
static_cast<unsigned>(entry.addr), static_cast<size_t>(entry.len));
|
|
|
|
// anything before the kernel we ignore
|
|
if (entry.addr < minAddr)
|
|
{
|
|
// if the entire entry is before the kernel, continue
|
|
if (entry.addr + entry.len <= minAddr)
|
|
{
|
|
addr += sizeof(entry.size) + entry.size;
|
|
continue;
|
|
}
|
|
// otherwise shrink it and use the rest
|
|
entry.len -= (minAddr - entry.addr);
|
|
entry.addr = minAddr;
|
|
}
|
|
__ba_registerAllocatableMemory(reinterpret_cast<void*>(entry.addr), entry.len);
|
|
}
|
|
addr += sizeof(entry.size) + entry.size;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
using initfunc_t = void *();
|
|
extern "C" initfunc_t* start_ctors;
|
|
extern "C" initfunc_t* end_ctors;
|
|
|
|
void initGlobals()
|
|
{
|
|
for (initfunc_t** initFunc = &start_ctors; initFunc != &end_ctors; ++initFunc)
|
|
{
|
|
// TODO: this confuses me
|
|
if (*initFunc == reinterpret_cast<initfunc_t*>(0xffffffffffffffff)) {
|
|
break;
|
|
}
|
|
(*initFunc)();
|
|
}
|
|
}
|
|
}
|
|
|
|
extern "C"
|
|
{
|
|
EfiMemoryMap* gMemoryMap;
|
|
|
|
void _init();
|
|
void _fini();
|
|
void main();
|
|
|
|
void kernel_main()
|
|
{
|
|
initGlobals();
|
|
|
|
/* Initialize terminal interface */
|
|
tty::initialize();
|
|
|
|
std::printf("Kernel End: %p\n", &gKernelEnd);
|
|
|
|
/* Initialize the heap */
|
|
// assert(gMultibootHeader->flags & MULTIBOOT_INFO_MEM_MAP);
|
|
// initHeapFromMultibootHeader(gMultibootHeader->mmap_addr, gMultibootHeader->mmap_length);
|
|
initHeapFromEfiMemoryMap(*gMemoryMap);
|
|
|
|
main();
|
|
|
|
_fini();
|
|
}
|
|
} // extern "C"
|