Compare commits

..

2 Commits

Author SHA1 Message Date
ad0f6546d7
Add a global initrd TarStream to make the initial ramdisk accessible everywhere
All checks were successful
continuous-integration/drone/push Build is passing
It's also mapped into virtual memory instead of directly going into the physical location!!
2022-12-23 11:33:23 +01:00
6ff92b1714
MemoryManager: Add get_kernel_mapping_for_frames()
This function allocates a continuous range of VM and maps the physical frames passed to said VM range.
2022-12-23 11:30:49 +01:00
8 changed files with 62 additions and 4 deletions

View File

@ -15,6 +15,7 @@ set(SOURCES
src/thread/Spinlock.cpp src/thread/Spinlock.cpp
src/thread/Thread.cpp src/thread/Thread.cpp
src/thread/Scheduler.cpp src/thread/Scheduler.cpp
src/InitRD.cpp
) )
if("${ARCH}" MATCHES "x86_64") if("${ARCH}" MATCHES "x86_64")

18
kernel/src/InitRD.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "InitRD.h"
#include "arch/MMU.h"
#include "boot/bootboot.h"
#include "memory/MemoryManager.h"
#include <luna/Alignment.h>
TarStream g_initrd;
extern const BOOTBOOT bootboot;
void InitRD::initialize()
{
u64 virtual_initrd_address =
MemoryManager::get_kernel_mapping_for_frames(
bootboot.initrd_ptr, get_blocks_from_size(bootboot.initrd_size, ARCH_PAGE_SIZE), MMU::NoExecute)
.expect_value("Unable to map the initial ramdisk into virtual memory");
g_initrd.initialize((void*)virtual_initrd_address, bootboot.initrd_size);
}

9
kernel/src/InitRD.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <luna/TarStream.h>
extern TarStream g_initrd;
namespace InitRD
{
void initialize();
}

View File

@ -1,4 +1,5 @@
#include "boot/Init.h" #include "boot/Init.h"
#include "InitRD.h"
#include "Log.h" #include "Log.h"
#include "arch/CPU.h" #include "arch/CPU.h"
#include "boot/bootboot.h" #include "boot/bootboot.h"
@ -28,6 +29,7 @@ void Init::early_init()
setup_log(log_debug_enabled(), log_serial_enabled(), true); setup_log(log_debug_enabled(), log_serial_enabled(), true);
MemoryManager::init(); MemoryManager::init();
InitRD::initialize();
CPU::platform_init(); CPU::platform_init();

View File

@ -1,3 +1,4 @@
#include "InitRD.h"
#include "Log.h" #include "Log.h"
#include "arch/CPU.h" #include "arch/CPU.h"
#include "arch/MMU.h" #include "arch/MMU.h"
@ -11,7 +12,6 @@
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/TarStream.h>
#include <luna/Units.h> #include <luna/Units.h>
extern const BOOTBOOT bootboot; extern const BOOTBOOT bootboot;
@ -67,9 +67,8 @@ Result<void> init()
kinfoln("Used memory: %s", to_dynamic_unit(MemoryManager::used()).release_value().chars()); kinfoln("Used memory: %s", to_dynamic_unit(MemoryManager::used()).release_value().chars());
kinfoln("Reserved memory: %s", to_dynamic_unit(MemoryManager::reserved()).release_value().chars()); kinfoln("Reserved memory: %s", to_dynamic_unit(MemoryManager::reserved()).release_value().chars());
TarStream stream((void*)bootboot.initrd_ptr, bootboot.initrd_size);
TarStream::Entry entry; TarStream::Entry entry;
while (TRY(stream.read_next_entry().try_set_value_with_specific_error(entry, 0))) while (TRY(g_initrd.read_next_entry().try_set_value_with_specific_error(entry, 0)))
{ {
if (entry.type == TarStream::EntryType::RegularFile) if (entry.type == TarStream::EntryType::RegularFile)
{ {
@ -78,7 +77,7 @@ Result<void> init()
if (!strcmp(entry.name, "sys/config")) if (!strcmp(entry.name, "sys/config"))
{ {
auto contents = TRY(stream.read_contents_as_string(entry, 0, entry.size)); auto contents = TRY(g_initrd.read_contents_as_string(entry, 0, entry.size));
kinfoln("%s", contents.chars()); kinfoln("%s", contents.chars());
} }

View File

@ -247,6 +247,32 @@ namespace MemoryManager
return start; return start;
} }
Result<u64> get_kernel_mapping_for_frames(u64 phys, usize count, int flags)
{
u64 start = TRY(KernelVM::alloc_several_pages(count));
usize pages_mapped = 0;
auto guard = make_scope_guard([=, &pages_mapped] {
KernelVM::free_several_pages(start, pages_mapped);
unmap_weak(start, pages_mapped);
});
u64 virt = start;
while (pages_mapped < count)
{
TRY(MMU::map(virt, phys, flags));
virt += ARCH_PAGE_SIZE;
phys += ARCH_PAGE_SIZE;
pages_mapped++;
}
guard.deactivate();
return start;
}
Result<void> unmap_owned(u64 virt, usize count) Result<void> unmap_owned(u64 virt, usize count)
{ {
CHECK_PAGE_ALIGNED(virt); CHECK_PAGE_ALIGNED(virt);

View File

@ -25,6 +25,8 @@ namespace MemoryManager
Result<u64> alloc_at(u64 virt, usize count, int flags); Result<u64> alloc_at(u64 virt, usize count, int flags);
Result<u64> alloc_for_kernel(usize count, int flags); Result<u64> alloc_for_kernel(usize count, int flags);
Result<u64> get_kernel_mapping_for_frames(u64 phys, usize count, int flags);
Result<void> unmap_owned(u64 virt, usize count); Result<void> unmap_owned(u64 virt, usize count);
Result<void> unmap_owned_and_free_vm(u64 virt, usize count); Result<void> unmap_owned_and_free_vm(u64 virt, usize count);
Result<void> unmap_weak(u64 virt, usize count); Result<void> unmap_weak(u64 virt, usize count);

View File

@ -24,6 +24,7 @@ class TarStream
friend class TarStream; friend class TarStream;
}; };
TarStream() = default;
TarStream(void* base, usize size); TarStream(void* base, usize size);
void initialize(void* base, usize size); void initialize(void* base, usize size);