SimpleKernel 1.17.0
Loading...
Searching...
No Matches
kernel.h File Reference
#include <cstdint>
Include dependency graph for kernel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void _start (int argc, const char **argv)
 负责 crtbegin 的工作
 
auto main (int argc, const char **argv) -> int
 内核入口
 
auto MemoryInit () -> void
 内存子系统初始化
 
auto MemoryInitSMP () -> void
 多核内存子系统初始化
 
auto DeviceInit () -> void
 设备子系统初始化
 
auto FileSystemInit () -> void
 文件系统初始化
 

Function Documentation

◆ _start()

void _start ( int  argc,
const char **  argv 
)

负责 crtbegin 的工作

Parameters
argcriscv64: 启动核 id
argv参数指针 riscv64: dtb 地址
Returns
uint32_t 正常返回 0

Definition at line 134 of file main.cpp.

134 {
135 if (argv != nullptr) {
136 CppInit();
137 main(argc, argv);
138 } else {
139 main_smp(argc, argv);
140 }
141
142 while (true) {
144 }
145}
auto CppInit() -> void
构造 c++ 全局对象
void Pause()
Definition cpu_io.h:20
void main()
Definition main.cpp:33
Here is the call graph for this function:

◆ DeviceInit()

auto DeviceInit ( ) -> void

设备子系统初始化

设备子系统初始化

Definition at line 28 of file device.cpp.

28 {
29 DeviceManagerSingleton::create();
30 auto& dm = DeviceManagerSingleton::instance();
31
32 // 创建驱动单例
33 Ns16550aDriverSingleton::create();
34 Pl011DriverSingleton::create();
35 VirtioDriverSingleton::create();
36
37 for (const auto& get_entry : kBuiltinDrivers) {
38 const auto& entry = get_entry();
39 if (auto r = dm.GetRegistry().Register(entry); !r) {
40 klog::Err("DeviceInit: register driver '{}' failed: {}", entry.name,
41 r.error().message());
42 return;
43 }
44 }
45
46 PlatformBus platform_bus(KernelFdtSingleton::instance());
47 if (auto r = dm.RegisterBus(platform_bus); !r) {
48 klog::Err("DeviceInit: PlatformBus enumeration failed: {}",
49 r.error().message());
50 return;
51 }
52
53 if (auto r = dm.ProbeAll(); !r) {
54 klog::Err("DeviceInit: ProbeAll failed: {}", r.error().message());
55 return;
56 }
57
58 klog::Info("DeviceInit: complete");
59}
平台总线 — 从扁平设备树(FDT)枚举设备
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FileSystemInit()

auto FileSystemInit ( ) -> void

文件系统初始化

文件系统初始化

Definition at line 17 of file filesystem.cpp.

17 {
18 // 初始化 VFS
19 auto init_result = vfs::Init();
20 if (!init_result.has_value()) {
21 klog::Err("FileSystemInit: vfs::Init failed: {}",
22 init_result.error().message());
23 return;
24 }
25
26 // 创建 ramfs 根文件系统并挂载到 "/"
27 static ramfs::RamFs root_ramfs;
28 auto mount_result = vfs::GetMountTable().Mount("/", &root_ramfs, nullptr);
29 if (!mount_result.has_value()) {
30 klog::Err("FileSystemInit: failed to mount ramfs at /: {}",
31 mount_result.error().message());
32 return;
33 }
34
35 // Mount FatFS on the first available block device at /mnt/fat
36 DeviceNode* blk_nodes[4]{};
37 const size_t blk_count = DeviceManagerSingleton::instance().FindDevicesByType(
38 DeviceType::kBlock, blk_nodes, 4);
39
40 if (blk_count > 0 && blk_nodes[0]->block_device != nullptr) {
41 auto* blk = blk_nodes[0]->block_device;
43 auto fat_mount = fat_fs.Mount(blk);
44 if (!fat_mount.has_value()) {
45 klog::Err("FileSystemInit: FatFsFileSystem::Mount failed: {}",
46 fat_mount.error().message());
47 } else {
48 auto vfs_mount = vfs::GetMountTable().Mount("/mnt/fat", &fat_fs, blk);
49 if (!vfs_mount.has_value()) {
50 klog::Err("FileSystemInit: vfs mount at /mnt/fat failed: {}",
51 vfs_mount.error().message());
52 } else {
53 klog::Info("FileSystemInit: FatFS mounted at /mnt/fat (device: {})",
54 blk->GetName());
55 }
56 }
57 }
58
59 klog::Info("FileSystemInit: complete");
60}
FatFS VFS 适配器
Definition fatfs.hpp:25
ramfs 文件系统实现
Definition ramfs.hpp:16
auto Mount(vfs::BlockDevice *device) -> Expected< vfs::Inode * > override
挂载 ramfs
Definition ramfs.cpp:36
@ kBlock
块设备(磁盘等)
static constexpr uint8_t kRootFsDriveId
FatFS 逻辑驱动器号(对应 rootfs.img)
auto GetMountTable() -> MountTable &
获取全局挂载表实例
Definition mount.cpp:234
auto Init() -> Expected< void >
VFS 全局初始化
Definition vfs.cpp:80
单个设备的硬件资源描述。
vfs::BlockDevice * block_device
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

auto main ( int  argc,
const char **  argv 
) -> int

内核入口

Parameters
argc同 _start
argv同 _start
Returns
int 正常返回 0

Definition at line 147 of file main.cpp.

147 {
148 // 初始化当前核心的 per_cpu 数据
149 per_cpu::PerCpuArraySingleton::create();
151
152 // 架构相关初始化
153 ArchInit(argc, argv);
154 // 内存相关初始化
155 MemoryInit();
156 // 中断相关初始化
157 InterruptInit(argc, argv);
158 // 设备管理器初始化
159 DeviceInit();
160 // 文件系统初始化
162 // 初始化任务管理器 (设置主线程)
163 TaskManagerSingleton::create();
164 TaskManagerSingleton::instance().InitCurrentCore();
165
166 TimerInit();
167
168 // 唤醒其余 core
170
171 DumpStack();
172
173 klog::Info("Hello SimpleKernel");
174
175 klog::Info("Initializing test tasks...");
176
177 // 为主核心创建测试任务
178 create_test_tasks();
179
180 klog::Info("Main: Starting scheduler...");
181
182 // 启动调度器,不再返回
183 TaskManagerSingleton::instance().Schedule();
184
185 // UNREACHABLE: Schedule() 不应返回
186 __builtin_unreachable();
187}
auto WakeUpOtherCores() -> void
唤醒其余 core
Definition arch_main.cpp:68
auto ArchInit(int argc, const char **argv) -> void
体系结构相关初始化
Definition arch_main.cpp:48
auto DumpStack() -> void
打印调用栈
Definition backtrace.cpp:34
auto InterruptInit(int, const char **) -> void
体系结构相关中断初始化
auto TimerInit() -> void
初始化定时器
Definition timer.cpp:47
auto DeviceInit() -> void
设备子系统初始化入口
Definition device.cpp:28
auto FileSystemInit() -> void
文件系统子系统初始化入口
auto MemoryInit() -> void
内存子系统初始化
Definition memory.cpp:71
auto GetCurrentCoreId() -> size_t
Definition cpu_io.h:26
static __always_inline auto GetCurrentCore() -> PerCpu &
获取当前核心的 PerCpu 数据
Definition per_cpu.hpp:55
每个 CPU 核心的局部数据
Definition per_cpu.hpp:23
Here is the call graph for this function:

◆ MemoryInit()

auto MemoryInit ( ) -> void

内存子系统初始化

Definition at line 71 of file memory.cpp.

71 {
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}
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
static constexpr size_t kPageSize
Definition cpu_io.h:63
auto PageAlignUp(uint64_t addr) -> uint64_t
Definition cpu_io.h:161
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MemoryInitSMP()

auto MemoryInitSMP ( ) -> void

多核内存子系统初始化

Definition at line 106 of file memory.cpp.

106 {
107 VirtualMemorySingleton::instance().InitCurrentCore();
108 klog::Info("SMP Memory initialization completed");
109}
Here is the call graph for this function: