feat: start adding physical memory allocator
This commit is contained in:
parent
8dc53f662e
commit
e2ea22ad53
15 changed files with 440 additions and 38 deletions
|
|
@ -34,6 +34,34 @@ enum class tag_type : uint32_t {
|
|||
load_base_addr = 24,
|
||||
};
|
||||
|
||||
inline constexpr const char* get_tag_name(tag_type type) {
|
||||
switch (type) {
|
||||
case tag_type::end: return "end";
|
||||
case tag_type::cmdline: return "cmdline";
|
||||
case tag_type::boot_loader_name: return "boot_loader_name";
|
||||
case tag_type::module: return "module";
|
||||
case tag_type::basic_meminfo: return "basic_meminfo";
|
||||
case tag_type::bootdev: return "bootdev";
|
||||
case tag_type::mmap: return "mmap";
|
||||
case tag_type::vbe: return "vbe";
|
||||
case tag_type::framebuffer: return "framebuffer";
|
||||
case tag_type::elf_sections: return "elf_sections";
|
||||
case tag_type::apm: return "apm";
|
||||
case tag_type::efi32: return "efi32";
|
||||
case tag_type::efi64: return "efi64";
|
||||
case tag_type::smbios: return "smbios";
|
||||
case tag_type::acpi_old: return "acpi_old";
|
||||
case tag_type::acpi_new: return "acpi_new";
|
||||
case tag_type::network: return "network";
|
||||
case tag_type::efi_mmap: return "efi_mmap";
|
||||
case tag_type::efi_bs: return "efi_bs";
|
||||
case tag_type::efi32_ih: return "efi32_ih";
|
||||
case tag_type::efi64_ih: return "efi64_ih";
|
||||
case tag_type::load_base_addr: return "load_base_addr";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
enum class framebuffer_type : uint8_t {
|
||||
indexed = 0,
|
||||
rgb = 1,
|
||||
|
|
@ -48,6 +76,17 @@ enum class mem_type : uint32_t {
|
|||
badram = 5,
|
||||
};
|
||||
|
||||
inline constexpr const char* get_mem_type_name(mem_type type) {
|
||||
switch (type) {
|
||||
case mem_type::available: return "available";
|
||||
case mem_type::reserved: return "reserved";
|
||||
case mem_type::acpi_reclaimable: return "acpi_reclaimable";
|
||||
case mem_type::nvs: return "nvs";
|
||||
case mem_type::badram: return "badram";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
struct color {
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
|
|
|
|||
37
kernel/include/memory/allocator.h
Normal file
37
kernel/include/memory/allocator.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include "memory/pointer.h"
|
||||
#include "process/process.h"
|
||||
#include "util/optional.h"
|
||||
#include "init/multiboot2.h"
|
||||
|
||||
constexpr const size_t page_size = 4096;
|
||||
|
||||
struct PhysicalAllocator {
|
||||
public:
|
||||
struct allocation_t {
|
||||
paddr_t ptr;
|
||||
size_t page_count;
|
||||
};
|
||||
struct entry_t {
|
||||
uint64_t page_count;
|
||||
pid_t owner;
|
||||
};
|
||||
static void init(const multiboot::tag_mmap* multiboot_info);
|
||||
static PhysicalAllocator& getInstance();
|
||||
allocation_t allocPages(size_t page_count, pid_t owner);
|
||||
void free(allocation_t ptr);
|
||||
void force(allocation_t ptr, pid_t owner);
|
||||
entry_t getOwner(paddr_t from);
|
||||
private:
|
||||
struct Page {
|
||||
optional<Page*> next;
|
||||
entry_t entries[255];
|
||||
void forceEntry(entry_t entry);
|
||||
void cleanup();
|
||||
void compact();
|
||||
};
|
||||
static_assert(sizeof(Page) == page_size);
|
||||
Page* storage;
|
||||
};
|
||||
|
||||
|
|
@ -7,13 +7,12 @@ constexpr uint64_t high_base = 0xFFFF800000000000;
|
|||
#define read_symbol(symbol) [](){uint64_t res; asm volatile("movabsq $" symbol ", %0": "=r"(res)); return res;}()
|
||||
|
||||
struct paddr_t {
|
||||
uint64_t address;
|
||||
|
||||
template<typename T>
|
||||
T* access() {
|
||||
return reinterpret_cast<T*>(address+high_base);
|
||||
}
|
||||
uint64_t address;
|
||||
|
||||
template<typename T>
|
||||
T* access() {
|
||||
return reinterpret_cast<T*>(address+high_base);
|
||||
}
|
||||
};
|
||||
|
||||
inline void* operator new(size_t, void* p) {
|
||||
|
|
|
|||
|
|
@ -17,3 +17,14 @@ void for_each_reserved_section(Fn&& fn) {
|
|||
for (auto* s = __reserved_ranges_start__; s < __reserved_ranges_end__; ++s)
|
||||
fn(*s);
|
||||
}
|
||||
|
||||
inline bool is_in_reserved_section(paddr_t ptr, size_t size = 1) {
|
||||
bool res = false;
|
||||
for_each_reserved_section([&](const KernelSection& section) {
|
||||
if (ptr.address < section.phys_end().address &&
|
||||
section.phys_start().address < ptr.address + size) {
|
||||
res = true;
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
10
kernel/include/process/process.h
Normal file
10
kernel/include/process/process.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "util/number.h"
|
||||
|
||||
using pid_t = uint64_t;
|
||||
|
||||
constexpr const pid_t pid_free = 0;
|
||||
constexpr const pid_t pid_reserved = 1;
|
||||
constexpr const pid_t pid_mmio = 2;
|
||||
constexpr const pid_t pid_kernel = 3;
|
||||
144
kernel/include/util/optional.h
Normal file
144
kernel/include/util/optional.h
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
#pragma once
|
||||
|
||||
#include "memory/pointer.h"
|
||||
#include "util/number.h"
|
||||
#include "util/utility.h"
|
||||
|
||||
// Ported from legacy/CrackOS3 (include/features/optional.h), adapted to the
|
||||
// PrOSess freestanding environment: uses the in-tree move/forward (util/utility.h)
|
||||
// instead of <utility>. The CrackOS3 value_or_panic() helper is omitted because
|
||||
// there is no global panic() in this tree yet.
|
||||
|
||||
template<typename T>
|
||||
struct optional {
|
||||
private:
|
||||
char buffer[sizeof(T)];
|
||||
bool initialized;
|
||||
|
||||
void destroy() {
|
||||
if (initialized) {
|
||||
((T*) buffer)->~T();
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
constexpr optional() : initialized(false) {}
|
||||
optional(const T& value) : initialized(true) {
|
||||
new (buffer) T(value);
|
||||
}
|
||||
optional(T&& value) : initialized(true) {
|
||||
new (buffer) T(move(value));
|
||||
}
|
||||
|
||||
optional(const optional& other) : initialized(other.initialized) {
|
||||
if (initialized) {
|
||||
new (buffer) T(*other);
|
||||
}
|
||||
}
|
||||
|
||||
optional(optional&& other) : initialized(other.initialized) {
|
||||
if (initialized) {
|
||||
new (buffer) T(move(*other));
|
||||
}
|
||||
other.initialized = false;
|
||||
}
|
||||
|
||||
~optional() {
|
||||
destroy();
|
||||
}
|
||||
|
||||
T* operator->() {
|
||||
return (T*) buffer;
|
||||
}
|
||||
|
||||
T& operator*() {
|
||||
return *((T*) buffer);
|
||||
}
|
||||
|
||||
const T* operator->() const {
|
||||
return (const T*) buffer;
|
||||
}
|
||||
|
||||
const T& operator*() const {
|
||||
return *((const T*) buffer);
|
||||
}
|
||||
|
||||
optional& operator=(const optional& other) {
|
||||
if (this == &other) return *this;
|
||||
destroy();
|
||||
if (other.initialized) {
|
||||
new (buffer) T(*other);
|
||||
initialized = true;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
optional& operator=(optional&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
destroy();
|
||||
if (other.initialized) {
|
||||
new (buffer) T(move(*other));
|
||||
initialized = true;
|
||||
}
|
||||
other.initialized = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_value() const {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
T& value() {
|
||||
return *((T*) buffer);
|
||||
}
|
||||
|
||||
const T& value() const {
|
||||
return *((const T*) buffer);
|
||||
}
|
||||
|
||||
template<typename Func, typename... ArgT>
|
||||
T value_or_create(Func func, ArgT... args) {
|
||||
if (initialized) {
|
||||
return value();
|
||||
} else {
|
||||
return func(args...);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Func, typename... ArgT>
|
||||
T value_or_create(Func func, ArgT... args) const {
|
||||
if (initialized) {
|
||||
return value();
|
||||
} else {
|
||||
return func(args...);
|
||||
}
|
||||
}
|
||||
|
||||
const T& value_or(const T& default_value) const {
|
||||
if (initialized) {
|
||||
return *((const T*) buffer);
|
||||
} else {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
bool operator!() const {
|
||||
return !initialized;
|
||||
}
|
||||
|
||||
template<typename R, typename Func, typename... ArgT>
|
||||
optional<R> map(Func func, ArgT... args) {
|
||||
if (initialized) {
|
||||
return func(value(), forward<ArgT>(args)...);
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
};
|
||||
28
kernel/include/util/utility.h
Normal file
28
kernel/include/util/utility.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
// Minimal freestanding replacements for <utility> (no stdlib available).
|
||||
|
||||
template<typename T>
|
||||
struct remove_reference { using type = T; };
|
||||
template<typename T>
|
||||
struct remove_reference<T&> { using type = T; };
|
||||
template<typename T>
|
||||
struct remove_reference<T&&> { using type = T; };
|
||||
|
||||
template<typename T>
|
||||
using remove_reference_t = typename remove_reference<T>::type;
|
||||
|
||||
template<typename T>
|
||||
constexpr remove_reference_t<T>&& move(T&& value) {
|
||||
return static_cast<remove_reference_t<T>&&>(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr T&& forward(remove_reference_t<T>& value) {
|
||||
return static_cast<T&&>(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr T&& forward(remove_reference_t<T>&& value) {
|
||||
return static_cast<T&&>(value);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue