Skip to content

Commit

Permalink
feat(kernel): add Singleton
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Jun 13, 2024
1 parent 080ed31 commit 78e96ff
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ boot branch

- 全局对象

| 对象名 | 位置 | 用途 |
| :-----------------------------: | :----------------------------------: | :---------------------: |
| ostream cout | src/kernel/libcxx/include/iostream | 内核中的 std::cout 实现 |
| KernelElf kKernelElf | src/kernel/include/kernel_elf.hpp | 解析内核自身的 elf 信息 |
| KernelFdt kKernelFdt | src/kernel/include/kernel_fdt.hpp | 解析 dtb 信息 |
| BootInfo kBootInfo | src/kernel/arch/arch.h | 内核基本信息 |
| cpu::Serial kSerial(cpu::kCom1) | src/kernel/arch/x86_64/arch_main.cpp | X86_64 下的串口 |
| 对象名 | 位置 | 用途 |
| :------------------------------------: | :----------------------------------: | :---------------------: |
| static ostream cout | src/kernel/libcxx/include/iostream | 内核中的 std::cout 实现 |
| static Singleton<KernelElf> kKernelElf | src/kernel/include/kernel_elf.hpp | 解析内核自身的 elf 信息 |
| static Singleton<KernelFdt> kKernelFdt | src/kernel/include/kernel_fdt.hpp | 解析 dtb 信息 |
| static Singleton<BasicInfo> kBasicInfo | src/kernel/include/kernel.h | 内核基本信息 |
| static cpu::Serial kSerial(cpu::kCom1) | src/kernel/arch/x86_64/arch_main.cpp | X86_64 下的串口 |

- 基于 doxygen 的文档生成与自动部署

Expand Down
2 changes: 1 addition & 1 deletion src/kernel/arch/riscv64/arch_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
uart.PutChar('\n');

// 解析内核 elf 信息
kKernelElf = KernelElf();
kKernelElf.getInstance() = KernelElf();

printf("hello ArchInit\n");

Expand Down
42 changes: 25 additions & 17 deletions src/kernel/arch/x86_64/arch_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,36 @@ uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
return -1;
}

kBasicInfo = *reinterpret_cast<BasicInfo *>(argv);
printf("kBasicInfo.framebuffer.base: 0x%p.\n", kBasicInfo.framebuffer.base);
printf("kBasicInfo.framebuffer.size: %d.\n", kBasicInfo.framebuffer.size);
printf("kBasicInfo.framebuffer.height: %d.\n", kBasicInfo.framebuffer.height);
printf("kBasicInfo.framebuffer.width: %d.\n", kBasicInfo.framebuffer.width);
printf("kBasicInfo.framebuffer.pitch: %d.\n", kBasicInfo.framebuffer.pitch);

printf("kBasicInfo.elf_addr: 0x%X.\n", kBasicInfo.elf_addr);
printf("kBasicInfo.elf_size: %d.\n", kBasicInfo.elf_size);
kBasicInfo.getInstance() = *reinterpret_cast<BasicInfo *>(argv);
printf("kBasicInfo.framebuffer.base: 0x%p.\n",
kBasicInfo.getInstance().framebuffer.base);
printf("kBasicInfo.framebuffer.size: %d.\n",
kBasicInfo.getInstance().framebuffer.size);
printf("kBasicInfo.framebuffer.height: %d.\n",
kBasicInfo.getInstance().framebuffer.height);
printf("kBasicInfo.framebuffer.width: %d.\n",
kBasicInfo.getInstance().framebuffer.width);
printf("kBasicInfo.framebuffer.pitch: %d.\n",
kBasicInfo.getInstance().framebuffer.pitch);

printf("kBasicInfo.elf_addr: 0x%X.\n", kBasicInfo.getInstance().elf_addr);
printf("kBasicInfo.elf_size: %d.\n", kBasicInfo.getInstance().elf_size);

// 在屏幕上绘制矩形
Fillrect((uint8_t *)kBasicInfo.framebuffer.base, kBasicInfo.framebuffer.pitch,
255, 0, 255, 100, 100);
Fillrect((uint8_t *)kBasicInfo.getInstance().framebuffer.base,
kBasicInfo.getInstance().framebuffer.pitch, 255, 0, 255, 100, 100);

for (uint32_t i = 0; i < kBasicInfo.memory_map_count; i++) {
for (uint32_t i = 0; i < kBasicInfo.getInstance().memory_map_count; i++) {
printf(
"kBasicInfo.memory_map[%d].base_addr: 0x%p, length: 0x%X, type: %d.\n",
i, kBasicInfo.memory_map[i].base_addr, kBasicInfo.memory_map[i].length,
kBasicInfo.memory_map[i].type);
i, kBasicInfo.getInstance().memory_map[i].base_addr,
kBasicInfo.getInstance().memory_map[i].length,
kBasicInfo.getInstance().memory_map[i].type);
}

// 解析内核 elf 信息
kKernelElf = KernelElf(kBasicInfo.elf_addr, kBasicInfo.elf_size);
kKernelElf.getInstance() = KernelElf(kBasicInfo.getInstance().elf_addr,
kBasicInfo.getInstance().elf_size);

printf("hello ArchInit\n");

Expand All @@ -86,10 +93,11 @@ void DumpStack() {
rbp = (uint64_t *)*rbp;

// 打印函数名
for (auto i : kKernelElf.symtab_) {
for (auto i : kKernelElf.getInstance().symtab_) {
if ((ELF64_ST_TYPE(i.st_info) == STT_FUNC) && (*rip >= i.st_value) &&
(*rip <= i.st_value + i.st_size)) {
printf("[%s] 0x%p\n", kKernelElf.strtab_ + i.st_name, *rip);
printf("[%s] 0x%p\n", kKernelElf.getInstance().strtab_ + i.st_name,
*rip);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/kernel/include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <cstddef>
#include <cstdint>

#include "singleton.hpp"

struct BasicInfo {
/// 最大内存表数量
static constexpr const uint32_t kMemoryMapMaxCount = 256;
Expand Down Expand Up @@ -92,6 +94,6 @@ extern "C" [[maybe_unused]] [[noreturn]] void _start(uint32_t argc,
uint32_t main(uint32_t argc, uint8_t* argv);

/// 保存内核基本信息
[[maybe_unused]] static BasicInfo kBasicInfo;
[[maybe_unused]] static Singleton<BasicInfo> kBasicInfo;

#endif /* SIMPLEKERNEL_SRC_KERNEL_INCLUDE_KERNEL_H_ */
3 changes: 2 additions & 1 deletion src/kernel/include/kernel_elf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "cstdio"
#include "cstring"
#include "singleton.hpp"

/**
* elf 文件相关
Expand Down Expand Up @@ -121,6 +122,6 @@ class KernelElf {
};

/// 全局 elf 对象,需要在相应体系结构初始化时重新初始化
[[maybe_unused]] static KernelElf kKernelElf;
[[maybe_unused]] static Singleton<KernelElf> kKernelElf;

#endif /* SIMPLEKERNEL_SRC_KERNEL_INCLUDE_KERNEL_ELF_HPP_ */
4 changes: 0 additions & 4 deletions src/kernel/include/singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#ifndef SIMPLEKERNEL_SRC_KERNEL_INCLUDE_SINGLETON_HPP_
#define SIMPLEKERNEL_SRC_KERNEL_INCLUDE_SINGLETON_HPP_

#include <atomic>
#include <cstddef>
#include <cstdint>

// 单例模板类
template <typename T>
class Singleton {
Expand Down
8 changes: 5 additions & 3 deletions test/system_test/gnu_efi_test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ void DumpStack() {
rbp = (uint64_t *)*rbp;

// 打印函数名
for (auto i : kKernelElf.symtab_) {
for (auto i : kKernelElf.getInstance().symtab_) {
if ((ELF64_ST_TYPE(i.st_info) == STT_FUNC) && (*rip >= i.st_value) &&
(*rip <= i.st_value + i.st_size)) {
printf("[%s] 0x%p\n", kKernelElf.strtab_ + i.st_name, *rip);
printf("[%s] 0x%p\n", kKernelElf.getInstance().strtab_ + i.st_name,
*rip);
}
}
}
Expand Down Expand Up @@ -140,7 +141,8 @@ uint32_t main(uint32_t argc, uint8_t *argv) {
printf("%c\n", inst_class.val);

// 解析内核 elf 信息
kKernelElf = KernelElf(basic_info.elf_addr, basic_info.elf_size);
kKernelElf.getInstance() =
KernelElf(basic_info.elf_addr, basic_info.elf_size);

DumpStack();

Expand Down

0 comments on commit 78e96ff

Please sign in to comment.