SimpleKernel 1.17.0
Loading...
Searching...
No Matches
memory.cpp
Go to the documentation of this file.
1
5#include <cpu_io.h>
6
7#include <algorithm>
8#include <bmalloc.hpp>
9#include <cstddef>
10
11#include "arch.h"
12#include "basic_info.hpp"
13#include "kernel.h"
14#include "kernel_elf.hpp"
15#include "kernel_log.hpp"
16#include "virtual_memory.hpp"
17
18namespace {
19
21struct BmallocLogger {
22 auto operator()(const char* format, ...) const -> int {
23 (void)format;
24 return 0;
25 }
26};
27
28bmalloc::Bmalloc<BmallocLogger>* allocator = nullptr;
29} // namespace
30
31extern "C" auto malloc(size_t size) -> void* {
32 if (allocator) {
33 return allocator->malloc(size);
34 }
35 return nullptr;
36}
37
38extern "C" auto free(void* ptr) -> void {
39 if (allocator) {
40 allocator->free(ptr);
41 }
42}
43
44extern "C" auto calloc(size_t num, size_t size) -> void* {
45 if (allocator) {
46 return allocator->calloc(num, size);
47 }
48 return nullptr;
49}
50
51extern "C" auto realloc(void* ptr, size_t new_size) -> void* {
52 if (allocator) {
53 return allocator->realloc(ptr, new_size);
54 }
55 return nullptr;
56}
57
58extern "C" auto aligned_alloc(size_t alignment, size_t size) -> void* {
59 if (allocator) {
60 return allocator->aligned_alloc(alignment, size);
61 }
62 return nullptr;
63}
64
65extern "C" auto aligned_free(void* ptr) -> void {
66 if (allocator) {
67 allocator->aligned_free(ptr);
68 }
69}
70
71auto MemoryInit() -> void {
72 auto allocator_addr =
73 reinterpret_cast<void*>(cpu_io::virtual_memory::PageAlignUp(
74 BasicInfoSingleton::instance().elf_addr +
75 KernelElfSingleton::instance().GetElfSize()));
76 auto allocator_size = BasicInfoSingleton::instance().physical_memory_addr +
77 BasicInfoSingleton::instance().physical_memory_size -
78 reinterpret_cast<uint64_t>(allocator_addr);
79
80 klog::Info("bmalloc address: {:#x}, size: {:#X}",
81 static_cast<uint64_t>(reinterpret_cast<uintptr_t>(allocator_addr)),
82 static_cast<uint64_t>(allocator_size));
83
84 static bmalloc::Bmalloc<BmallocLogger> bmallocator(allocator_addr,
85 allocator_size);
86 allocator = &bmallocator;
87
88 // 初始化当前核心的虚拟内存
89 VirtualMemorySingleton::create();
90 VirtualMemorySingleton::instance().InitCurrentCore();
91
92 // 重新映射早期控制台地址(如果有的话)
93 if (SIMPLEKERNEL_EARLY_CONSOLE_BASE != 0) {
94 VirtualMemorySingleton::instance()
95 .MapMMIO(SIMPLEKERNEL_EARLY_CONSOLE_BASE,
97 .or_else([](Error err) -> Expected<void*> {
98 klog::Warn("Failed to remap early console MMIO: {}", err.message());
99 return std::unexpected(err);
100 });
101 }
102
103 klog::Info("Memory initialization completed");
104}
105
106auto MemoryInitSMP() -> void {
107 VirtualMemorySingleton::instance().InitCurrentCore();
108 klog::Info("SMP Memory initialization completed");
109}
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto aligned_alloc(size_t alignment, size_t size) -> void *
Definition memory.cpp:58
auto aligned_free(void *ptr) -> void
Definition memory.cpp:65
auto calloc(size_t num, size_t size) -> void *
Definition memory.cpp:44
auto malloc(size_t size) -> void *
Definition memory.cpp:31
auto free(void *ptr) -> void
Definition memory.cpp:38
auto MemoryInit() -> void
内存子系统初始化
Definition memory.cpp:71
auto realloc(void *ptr, size_t new_size) -> void *
Definition memory.cpp:51
auto MemoryInitSMP() -> void
多核内存子系统初始化
Definition memory.cpp:106
static constexpr size_t kPageSize
Definition cpu_io.h:63
auto PageAlignUp(uint64_t addr) -> uint64_t
Definition cpu_io.h:161
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto Warn(etl::format_string< Args... > fmt, Args &&... args) -> void
以 WARN 级别记录日志
错误类型,用于 std::expected
Definition expected.hpp:343
constexpr auto message() const -> const char *
Definition expected.hpp:358