diff --git a/src/kernel/arch/aarch64/backtrace.cpp b/src/kernel/arch/aarch64/backtrace.cpp
index 30d1741cc..d1233305d 100644
--- a/src/kernel/arch/aarch64/backtrace.cpp
+++ b/src/kernel/arch/aarch64/backtrace.cpp
@@ -14,9 +14,9 @@
*/
#include "arch.h"
-#include "cpu.hpp"
-#include "sk_cstdio"
+#include "cpu/cpu.hpp"
#include "kernel_elf.hpp"
+#include "sk_cstdio"
#include "sk_libc.h"
int backtrace(void **buffer, int size) {
diff --git a/src/kernel/arch/aarch64/include/cpu/cpu.hpp b/src/kernel/arch/aarch64/include/cpu/cpu.hpp
new file mode 100644
index 000000000..0650d97d9
--- /dev/null
+++ b/src/kernel/arch/aarch64/include/cpu/cpu.hpp
@@ -0,0 +1,30 @@
+
+/**
+ * @file cpu.hpp
+ * @brief aarch64 cpu 相关定义
+ * @author Zone.N (Zone.Niuzh@hotmail.com)
+ * @version 1.0
+ * @date 2024-03-05
+ * @copyright MIT LICENSE
+ * https://github.com/Simple-XX/SimpleKernel
+ * @par change log:
+ *
+ * Date | Author | Description
+ * |
---|
2024-03-05 | Zone.N (Zone.Niuzh@hotmail.com) | 创建文件
+ * |
+ */
+
+#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_CPU_HPP_
+#define SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_CPU_HPP_
+
+#include
+#include
+#include
+#include
+
+#include "kernel_log.hpp"
+#include "sk_cstdio"
+#include "sk_iostream"
+#include "sr.hpp"
+
+#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_CPU_HPP_
diff --git a/src/kernel/arch/aarch64/include/cpu.hpp b/src/kernel/arch/aarch64/include/cpu/sr.hpp
similarity index 86%
rename from src/kernel/arch/aarch64/include/cpu.hpp
rename to src/kernel/arch/aarch64/include/cpu/sr.hpp
index f0e740793..eccd8a7ae 100644
--- a/src/kernel/arch/aarch64/include/cpu.hpp
+++ b/src/kernel/arch/aarch64/include/cpu/sr.hpp
@@ -1,7 +1,7 @@
/**
- * @file cpu.hpp
- * @brief aarch64 cpu 相关定义
+ * @file sr.hpp
+ * @brief aarch64 sr 相关定义
* @author Zone.N (Zone.Niuzh@hotmail.com)
* @version 1.0
* @date 2024-03-05
@@ -14,17 +14,17 @@
*
*/
-#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_HPP_
-#define SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_HPP_
+#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_SR_HPP_
+#define SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_SR_HPP_
#include
#include
#include
#include
+#include "kernel_log.hpp"
#include "sk_cstdio"
#include "sk_iostream"
-#include "kernel_log.hpp"
/**
* aarch64 cpu 相关定义
@@ -33,7 +33,7 @@
namespace cpu {
// 第一部分:寄存器定义
-namespace reginfo {
+namespace register_info {
struct RegInfoBase {
/// 寄存器数据类型
@@ -53,7 +53,7 @@ struct RegInfoBase {
/// 通用寄存器
struct X29Info : public RegInfoBase {};
-}; // namespace reginfo
+}; // namespace register_info
// 第二部分:读/写模版实现
namespace {
@@ -80,10 +80,10 @@ class ReadOnlyRegBase {
*/
static __always_inline RegInfo::DataType Read() {
typename RegInfo::DataType value{};
- if constexpr (std::is_same::value) {
+ if constexpr (std::is_same::value) {
__asm__ volatile("mov %0, x29" : "=r"(value) : :);
} else {
- log::Err("No Type\n");
+ klog::Err("No Type\n");
throw;
}
return value;
@@ -117,10 +117,10 @@ class WriteOnlyRegBase {
* @param value 要写的值
*/
static __always_inline void Write(RegInfo::DataType value) {
- if constexpr (std::is_same::value) {
+ if constexpr (std::is_same::value) {
__asm__ volatile("mv fp, %0" : : "r"(value) :);
} else {
- log::Err("No Type\n");
+ klog::Err("No Type\n");
throw;
}
}
@@ -146,7 +146,7 @@ class ReadWriteRegBase : public ReadOnlyRegBase,
};
// 第三部分:寄存器实例
-class X29 : public ReadWriteRegBase {
+class X29 : public ReadWriteRegBase {
public:
friend sk_std::ostream &operator<<(sk_std::ostream &os, const X29 &x29) {
printf("val: 0x%p", (void *)x29.Read());
@@ -166,4 +166,4 @@ struct AllXreg {
}; // namespace cpu
-#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_HPP_
+#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_AARCH64_INCLUDE_CPU_SR_HPP_
diff --git a/src/kernel/arch/riscv64/arch_main.cpp b/src/kernel/arch/riscv64/arch_main.cpp
index 1cbe6e87f..57f692e02 100644
--- a/src/kernel/arch/riscv64/arch_main.cpp
+++ b/src/kernel/arch/riscv64/arch_main.cpp
@@ -16,7 +16,7 @@
#include
#include "basic_info.hpp"
-#include "cpu.hpp"
+#include "cpu/cpu.hpp"
#include "kernel_elf.hpp"
#include "kernel_fdt.hpp"
#include "ns16550a.h"
@@ -78,7 +78,7 @@ uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
// 解析内核 elf 信息
kKernelElf.GetInstance() = KernelElf();
- log::Info("Hello riscv64 ArchInit\n");
+ klog::Info("Hello riscv64 ArchInit\n");
return 0;
}
diff --git a/src/kernel/arch/riscv64/backtrace.cpp b/src/kernel/arch/riscv64/backtrace.cpp
index 529188a9f..9aac9952a 100644
--- a/src/kernel/arch/riscv64/backtrace.cpp
+++ b/src/kernel/arch/riscv64/backtrace.cpp
@@ -15,10 +15,10 @@
*/
#include "arch.h"
-#include "cpu.hpp"
-#include "sk_cstdio"
+#include "cpu/cpu.hpp"
#include "kernel_elf.hpp"
#include "kernel_fdt.hpp"
+#include "sk_cstdio"
#include "sk_libc.h"
int backtrace(void **buffer, int size) {
diff --git a/src/kernel/arch/riscv64/include/cpu/cpu.hpp b/src/kernel/arch/riscv64/include/cpu/cpu.hpp
new file mode 100644
index 000000000..88f27f727
--- /dev/null
+++ b/src/kernel/arch/riscv64/include/cpu/cpu.hpp
@@ -0,0 +1,30 @@
+
+/**
+ * @file cpu.hpp
+ * @brief riscv64 cpu 相关定义
+ * @author Zone.N (Zone.Niuzh@hotmail.com)
+ * @version 1.0
+ * @date 2024-03-05
+ * @copyright MIT LICENSE
+ * https://github.com/Simple-XX/SimpleKernel
+ * @par change log:
+ *
+ * Date | Author | Description
+ * |
---|
2024-03-05 | Zone.N (Zone.Niuzh@hotmail.com) | 创建文件
+ * |
+ */
+
+#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_CPU_HPP_
+#define SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_CPU_HPP_
+
+#include
+#include
+#include
+#include
+
+#include "csr.hpp"
+#include "kernel_log.hpp"
+#include "sk_cstdio"
+#include "sk_iostream"
+
+#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_CPU_HPP_
diff --git a/src/kernel/arch/riscv64/include/cpu.hpp b/src/kernel/arch/riscv64/include/cpu/csr.hpp
similarity index 85%
rename from src/kernel/arch/riscv64/include/cpu.hpp
rename to src/kernel/arch/riscv64/include/cpu/csr.hpp
index 0741af11b..8b26d9627 100644
--- a/src/kernel/arch/riscv64/include/cpu.hpp
+++ b/src/kernel/arch/riscv64/include/cpu/csr.hpp
@@ -1,7 +1,7 @@
/**
- * @file cpu.hpp
- * @brief riscv64 cpu 相关定义
+ * @file csr.hpp
+ * @brief riscv64 csr 相关定义
* @author Zone.N (Zone.Niuzh@hotmail.com)
* @version 1.0
* @date 2024-03-05
@@ -14,8 +14,8 @@
*
*/
-#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_HPP_
-#define SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_HPP_
+#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_CSR_HPP_
+#define SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_CSR_HPP_
#include
#include
@@ -27,13 +27,13 @@
#include "sk_iostream"
/**
- * riscv64 cpu 相关定义
+ * riscv64 cpu Control and Status Registers 相关定义
* @note 寄存器读写设计见 arch/README.md
*/
namespace cpu {
// 第一部分:寄存器定义
-namespace reginfo {
+namespace register_info {
struct RegInfoBase {
/// 寄存器数据类型
@@ -53,7 +53,7 @@ struct RegInfoBase {
/// 通用寄存器
struct FpInfo : public RegInfoBase {};
-}; // namespace reginfo
+}; // namespace register_info
// 第二部分:读/写模版实现
namespace {
@@ -80,10 +80,10 @@ class ReadOnlyRegBase {
*/
static __always_inline RegInfo::DataType Read() {
typename RegInfo::DataType value{};
- if constexpr (std::is_same::value) {
+ if constexpr (std::is_same::value) {
__asm__ volatile("mv %0, fp" : "=r"(value) : :);
} else {
- log::Err("No Type\n");
+ klog::Err("No Type\n");
throw;
}
return value;
@@ -117,10 +117,10 @@ class WriteOnlyRegBase {
* @param value 要写的值
*/
static __always_inline void Write(RegInfo::DataType value) {
- if constexpr (std::is_same::value) {
+ if constexpr (std::is_same::value) {
__asm__ volatile("mv fp, %0" : : "r"(value) :);
} else {
- log::Err("No Type\n");
+ klog::Err("No Type\n");
throw;
}
}
@@ -146,7 +146,7 @@ class ReadWriteRegBase : public ReadOnlyRegBase,
};
// 第三部分:寄存器实例
-class Fp : public ReadWriteRegBase {
+class Fp : public ReadWriteRegBase {
public:
friend sk_std::ostream &operator<<(sk_std::ostream &os, const Fp &fp) {
printf("val: 0x%p", (void *)fp.Read());
@@ -166,4 +166,4 @@ struct AllXreg {
}; // namespace cpu
-#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_HPP_
+#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_RISCV64_INCLUDE_CPU_CSR_HPP_
diff --git a/src/kernel/arch/x86_64/arch_main.cpp b/src/kernel/arch/x86_64/arch_main.cpp
index ecc4aec2e..b34c13e66 100644
--- a/src/kernel/arch/x86_64/arch_main.cpp
+++ b/src/kernel/arch/x86_64/arch_main.cpp
@@ -17,11 +17,11 @@
#include
#include "basic_info.hpp"
-#include "cpu.hpp"
-#include "sk_cstdio"
-#include "sk_cstring"
+#include "cpu/cpu.hpp"
#include "kernel_elf.hpp"
#include "kernel_log.hpp"
+#include "sk_cstdio"
+#include "sk_cstring"
// printf_bare_metal 基本输出实现
/// @note 这里要注意,保证在 serial 初始化之前不能使用 printf
@@ -54,7 +54,7 @@ BasicInfo::BasicInfo(uint32_t argc, uint8_t *argv) {
uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
if (argc != 1) {
- log::Err("argc != 1 [%d]\n", argc);
+ klog::Err("argc != 1 [%d]\n", argc);
throw;
}
@@ -65,7 +65,7 @@ uint32_t ArchInit(uint32_t argc, uint8_t *argv) {
kKernelElf.GetInstance() = KernelElf(kBasicInfo.GetInstance().elf_addr,
kBasicInfo.GetInstance().elf_size);
- log::Info("Hello x86_64 ArchInit\n");
+ klog::Info("Hello x86_64 ArchInit\n");
return 0;
}
diff --git a/src/kernel/arch/x86_64/backtrace.cpp b/src/kernel/arch/x86_64/backtrace.cpp
index cb5f55b40..c4223fad3 100644
--- a/src/kernel/arch/x86_64/backtrace.cpp
+++ b/src/kernel/arch/x86_64/backtrace.cpp
@@ -14,9 +14,9 @@
*/
#include "arch.h"
-#include "cpu.hpp"
-#include "sk_cstdio"
+#include "cpu/cpu.hpp"
#include "kernel_elf.hpp"
+#include "sk_cstdio"
#include "sk_libc.h"
int backtrace(void **buffer, int size) {
diff --git a/src/kernel/arch/x86_64/include/cpu.hpp b/src/kernel/arch/x86_64/include/cpu/cpu.hpp
similarity index 55%
rename from src/kernel/arch/x86_64/include/cpu.hpp
rename to src/kernel/arch/x86_64/include/cpu/cpu.hpp
index e30fe565d..78187ffbc 100644
--- a/src/kernel/arch/x86_64/include/cpu.hpp
+++ b/src/kernel/arch/x86_64/include/cpu/cpu.hpp
@@ -14,17 +14,18 @@
*
*/
-#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_HPP_
-#define SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_HPP_
+#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_CPU_HPP_
+#define SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_CPU_HPP_
#include
#include
#include
#include
+#include "cr.hpp"
+#include "kernel_log.hpp"
#include "sk_cstdio"
#include "sk_iostream"
-#include "kernel_log.hpp"
/**
* x86_64 cpu 相关定义
@@ -181,139 +182,6 @@ class Serial {
return InByte(port_ + 5) & 0x20;
}
};
-
-// 第一部分:寄存器定义
-namespace reginfo {
-
-struct RegInfoBase {
- /// 寄存器数据类型
- using DataType = uint64_t;
- /// 起始位
- static constexpr uint64_t kBitOffset = 0;
- /// 位宽
- static constexpr uint64_t kBitWidth = 64;
- /// 掩码,(val & kBitMask) == 对应当前位的值
- static constexpr uint64_t kBitMask =
- (kBitWidth < 64) ? ((1ULL << kBitWidth) - 1) << kBitOffset : ~0ULL;
- /// 对应位置位掩码
- static constexpr uint64_t kAllSetMask =
- (kBitWidth < 64) ? ((1ULL << kBitWidth) - 1) : ~0ULL;
-};
-
-/// 通用寄存器
-struct RbpInfo : public RegInfoBase {};
-
-}; // namespace reginfo
-
-// 第二部分:读/写模版实现
-namespace {
-/**
- * 只读接口
- * @tparam 寄存器类型
- */
-template
-class ReadOnlyRegBase {
- public:
- /// @name 构造/析构函数
- /// @{
- ReadOnlyRegBase() = default;
- ReadOnlyRegBase(const ReadOnlyRegBase &) = delete;
- ReadOnlyRegBase(ReadOnlyRegBase &&) = delete;
- auto operator=(const ReadOnlyRegBase &) -> ReadOnlyRegBase & = delete;
- auto operator=(ReadOnlyRegBase &&) -> ReadOnlyRegBase & = delete;
- ~ReadOnlyRegBase() = default;
- /// @}
-
- /**
- * 读寄存器
- * @return RegInfo::DataType 寄存器的值
- */
- static __always_inline RegInfo::DataType Read() {
- typename RegInfo::DataType value{};
- if constexpr (std::is_same::value) {
- __asm__ volatile("mov %%rbp, %0" : "=r"(value) : :);
- } else {
- log::Err("No Type\n");
- throw;
- }
- return value;
- }
-
- /**
- * () 重载
- */
- static __always_inline RegInfo::DataType operator()() { return Read(); }
-};
-
-/**
- * 只写接口
- * @tparam 寄存器类型
- */
-template
-class WriteOnlyRegBase {
- public:
- /// @name 构造/析构函数
- /// @{
- WriteOnlyRegBase() = default;
- WriteOnlyRegBase(const WriteOnlyRegBase &) = delete;
- WriteOnlyRegBase(WriteOnlyRegBase &&) = delete;
- auto operator=(const WriteOnlyRegBase &) -> WriteOnlyRegBase & = delete;
- auto operator=(WriteOnlyRegBase &&) -> WriteOnlyRegBase & = delete;
- ~WriteOnlyRegBase() = default;
- /// @}
-
- /**
- * 写寄存器
- * @param value 要写的值
- */
- static __always_inline void Write(RegInfo::DataType value) {
- if constexpr (std::is_same::value) {
- __asm__ volatile("mv fp, %0" : : "r"(value) :);
- } else {
- log::Err("No Type\n");
- throw;
- }
- }
-};
-
-/**
- * 读写接口
- * @tparam 寄存器类型
- */
-template
-class ReadWriteRegBase : public ReadOnlyRegBase,
- public WriteOnlyRegBase {
- public:
- /// @name 构造/析构函数
- /// @{
- ReadWriteRegBase() = default;
- ReadWriteRegBase(const ReadWriteRegBase &) = delete;
- ReadWriteRegBase(ReadWriteRegBase &&) = delete;
- auto operator=(const ReadWriteRegBase &) -> ReadWriteRegBase & = delete;
- auto operator=(ReadWriteRegBase &&) -> ReadWriteRegBase & = delete;
- ~ReadWriteRegBase() = default;
- /// @}
-};
-
-// 第三部分:寄存器实例
-class Rbp : public ReadWriteRegBase {
- public:
- friend sk_std::ostream &operator<<(sk_std::ostream &os, const Rbp &rbp) {
- printf("val: 0x%p", (void *)rbp.Read());
- return os;
- }
-};
-
-/// 通用寄存器
-struct AllXreg {
- Rbp rbp;
-};
-
-}; // namespace
-
-// 第四部分:访问接口
-[[maybe_unused]] static AllXreg kAllXreg;
-
}; // namespace cpu
-#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_HPP_
+#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_CPU_HPP_
diff --git a/src/kernel/arch/x86_64/include/cpu/cr.hpp b/src/kernel/arch/x86_64/include/cpu/cr.hpp
new file mode 100644
index 000000000..4fa662204
--- /dev/null
+++ b/src/kernel/arch/x86_64/include/cpu/cr.hpp
@@ -0,0 +1,169 @@
+
+/**
+ * @file cr.hpp
+ * @brief x86_64 cr 相关定义
+ * @author Zone.N (Zone.Niuzh@hotmail.com)
+ * @version 1.0
+ * @date 2024-03-05
+ * @copyright MIT LICENSE
+ * https://github.com/Simple-XX/SimpleKernel
+ * @par change log:
+ *
+ * Date | Author | Description
+ * |
---|
2024-03-05 | Zone.N (Zone.Niuzh@hotmail.com) | 创建文件
+ * |
+ */
+
+#ifndef SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_CR_HPP_
+#define SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_CR_HPP_
+
+#include
+#include
+#include
+#include
+
+#include "kernel_log.hpp"
+#include "sk_cstdio"
+#include "sk_iostream"
+
+/**
+ * x86_64 cpu Control Registers 相关定义
+ * @note 寄存器读写设计见 arch/README.md
+ */
+namespace cpu {
+
+// 第一部分:寄存器定义
+namespace register_info {
+
+struct RegInfoBase {
+ /// 寄存器数据类型
+ using DataType = uint64_t;
+ /// 起始位
+ static constexpr uint64_t kBitOffset = 0;
+ /// 位宽
+ static constexpr uint64_t kBitWidth = 64;
+ /// 掩码,(val & kBitMask) == 对应当前位的值
+ static constexpr uint64_t kBitMask =
+ (kBitWidth < 64) ? ((1ULL << kBitWidth) - 1) << kBitOffset : ~0ULL;
+ /// 对应位置位掩码
+ static constexpr uint64_t kAllSetMask =
+ (kBitWidth < 64) ? ((1ULL << kBitWidth) - 1) : ~0ULL;
+};
+
+/// 通用寄存器
+struct RbpInfo : public RegInfoBase {};
+
+}; // namespace register_info
+
+// 第二部分:读/写模版实现
+namespace {
+/**
+ * 只读接口
+ * @tparam 寄存器类型
+ */
+template
+class ReadOnlyRegBase {
+ public:
+ /// @name 构造/析构函数
+ /// @{
+ ReadOnlyRegBase() = default;
+ ReadOnlyRegBase(const ReadOnlyRegBase &) = delete;
+ ReadOnlyRegBase(ReadOnlyRegBase &&) = delete;
+ auto operator=(const ReadOnlyRegBase &) -> ReadOnlyRegBase & = delete;
+ auto operator=(ReadOnlyRegBase &&) -> ReadOnlyRegBase & = delete;
+ ~ReadOnlyRegBase() = default;
+ /// @}
+
+ /**
+ * 读寄存器
+ * @return RegInfo::DataType 寄存器的值
+ */
+ static __always_inline RegInfo::DataType Read() {
+ typename RegInfo::DataType value{};
+ if constexpr (std::is_same::value) {
+ __asm__ volatile("mov %%rbp, %0" : "=r"(value) : :);
+ } else {
+ klog::Err("No Type\n");
+ throw;
+ }
+ return value;
+ }
+
+ /**
+ * () 重载
+ */
+ static __always_inline RegInfo::DataType operator()() { return Read(); }
+};
+
+/**
+ * 只写接口
+ * @tparam 寄存器类型
+ */
+template
+class WriteOnlyRegBase {
+ public:
+ /// @name 构造/析构函数
+ /// @{
+ WriteOnlyRegBase() = default;
+ WriteOnlyRegBase(const WriteOnlyRegBase &) = delete;
+ WriteOnlyRegBase(WriteOnlyRegBase &&) = delete;
+ auto operator=(const WriteOnlyRegBase &) -> WriteOnlyRegBase & = delete;
+ auto operator=(WriteOnlyRegBase &&) -> WriteOnlyRegBase & = delete;
+ ~WriteOnlyRegBase() = default;
+ /// @}
+
+ /**
+ * 写寄存器
+ * @param value 要写的值
+ */
+ static __always_inline void Write(RegInfo::DataType value) {
+ if constexpr (std::is_same::value) {
+ __asm__ volatile("mv fp, %0" : : "r"(value) :);
+ } else {
+ klog::Err("No Type\n");
+ throw;
+ }
+ }
+};
+
+/**
+ * 读写接口
+ * @tparam 寄存器类型
+ */
+template
+class ReadWriteRegBase : public ReadOnlyRegBase,
+ public WriteOnlyRegBase {
+ public:
+ /// @name 构造/析构函数
+ /// @{
+ ReadWriteRegBase() = default;
+ ReadWriteRegBase(const ReadWriteRegBase &) = delete;
+ ReadWriteRegBase(ReadWriteRegBase &&) = delete;
+ auto operator=(const ReadWriteRegBase &) -> ReadWriteRegBase & = delete;
+ auto operator=(ReadWriteRegBase &&) -> ReadWriteRegBase & = delete;
+ ~ReadWriteRegBase() = default;
+ /// @}
+};
+
+// 第三部分:寄存器实例
+class Rbp : public ReadWriteRegBase {
+ public:
+ friend sk_std::ostream &operator<<(sk_std::ostream &os, const Rbp &rbp) {
+ printf("val: 0x%p", (void *)rbp.Read());
+ return os;
+ }
+};
+
+/// 通用寄存器
+struct AllXreg {
+ Rbp rbp;
+};
+
+}; // namespace
+
+// 第四部分:访问接口
+[[maybe_unused]] static AllXreg kAllXreg;
+
+}; // namespace cpu
+
+#endif // SIMPLEKERNEL_SRC_KERNEL_ARCH_X86_64_INCLUDE_CPU_CR_HPP_
diff --git a/src/kernel/include/kernel_elf.hpp b/src/kernel/include/kernel_elf.hpp
index 5bfa84e89..f23cf577d 100644
--- a/src/kernel/include/kernel_elf.hpp
+++ b/src/kernel/include/kernel_elf.hpp
@@ -44,7 +44,7 @@ class KernelElf {
*/
explicit KernelElf(uint64_t elf_addr, size_t elf_size = 64) {
if (!elf_addr || !elf_size) {
- log::Err("Fatal Error: Invalid elf_addr or elf_size.\n");
+ klog::Err("Fatal Error: Invalid elf_addr or elf_size.\n");
throw;
}
@@ -53,7 +53,7 @@ class KernelElf {
// 检查 elf 头数据
auto check_elf_identity_ret = CheckElfIdentity();
if (!check_elf_identity_ret) {
- log::Err("KernelElf NOT valid ELF file.\n");
+ klog::Err("KernelElf NOT valid ELF file.\n");
throw;
}
@@ -106,15 +106,15 @@ class KernelElf {
[[nodiscard]] bool CheckElfIdentity() const {
if ((elf_[EI_MAG0] != ELFMAG0) || (elf_[EI_MAG1] != ELFMAG1) ||
(elf_[EI_MAG2] != ELFMAG2) || (elf_[EI_MAG3] != ELFMAG3)) {
- log::Err("Fatal Error: Invalid ELF header.\n");
+ klog::Err("Fatal Error: Invalid ELF header.\n");
throw;
}
if (elf_[EI_CLASS] == ELFCLASS32) {
- log::Err("Found 32bit executable but NOT SUPPORT.\n");
+ klog::Err("Found 32bit executable but NOT SUPPORT.\n");
throw;
}
if (elf_[EI_CLASS] != ELFCLASS64) {
- log::Err("Fatal Error: Invalid executable.\n");
+ klog::Err("Fatal Error: Invalid executable.\n");
throw;
}
return true;
diff --git a/src/kernel/include/kernel_fdt.hpp b/src/kernel/include/kernel_fdt.hpp
index 66dd6a2a5..afd535b63 100644
--- a/src/kernel/include/kernel_fdt.hpp
+++ b/src/kernel/include/kernel_fdt.hpp
@@ -48,13 +48,13 @@ class KernelFdt {
*/
explicit KernelFdt(uint64_t fdt_addr) : fdt_addr_((void *)fdt_addr) {
if (!fdt_addr_) {
- log::Err("Fatal Error: Invalid fdt_addr.\n");
+ klog::Err("Fatal Error: Invalid fdt_addr.\n");
throw;
}
// 检查 fdt 头数据
if (fdt_check_header(fdt_addr_) != 0) {
- log::Err("Invalid device tree blob\n");
+ klog::Err("Invalid device tree blob\n");
throw;
}
}
@@ -82,14 +82,14 @@ class KernelFdt {
// 找到 /memory 节点
auto offset = fdt_path_offset(fdt_addr_, "/memory");
if (offset < 0) {
- log::Err("Error finding /memory node: %s\n", fdt_strerror(offset));
+ klog::Err("Error finding /memory node: %s\n", fdt_strerror(offset));
throw;
}
// 获取 reg 属性
auto prop = fdt_get_property(fdt_addr_, offset, "reg", &len);
if (!prop) {
- log::Err("Error finding reg property: %s\n", fdt_strerror(len));
+ klog::Err("Error finding reg property: %s\n", fdt_strerror(len));
throw;
}
@@ -115,14 +115,14 @@ class KernelFdt {
// 找到 /memory 节点
auto offset = fdt_path_offset(fdt_addr_, "/soc/serial");
if (offset < 0) {
- log::Err("Error finding /soc/serial node: %s\n", fdt_strerror(offset));
+ klog::Err("Error finding /soc/serial node: %s\n", fdt_strerror(offset));
throw;
}
// 获取 reg 属性
auto prop = fdt_get_property(fdt_addr_, offset, "reg", &len);
if (!prop) {
- log::Err("Error finding reg property: %s\n", fdt_strerror(len));
+ klog::Err("Error finding reg property: %s\n", fdt_strerror(len));
throw;
}
diff --git a/src/kernel/include/kernel_log.hpp b/src/kernel/include/kernel_log.hpp
index 34c017f14..de9a0d3e2 100644
--- a/src/kernel/include/kernel_log.hpp
+++ b/src/kernel/include/kernel_log.hpp
@@ -86,7 +86,7 @@ class Logger : public sk_std::ostream {
} // namespace
-namespace log {
+namespace klog {
/**
* @brief 与 printf 类似,只是颜色不同
*/
diff --git a/src/kernel/main.cpp b/src/kernel/main.cpp
index d03aa65ca..89be68d96 100644
--- a/src/kernel/main.cpp
+++ b/src/kernel/main.cpp
@@ -38,14 +38,14 @@ uint32_t main(uint32_t argc, uint8_t *argv) {
[[maybe_unused]] auto arch_init_ret = ArchInit(argc, argv);
printf("Hello SimpleKernel\n");
- log::Debug("Hello SimpleKernel\n");
- log::Info("Hello SimpleKernel\n");
- log::Warn("Hello SimpleKernel\n");
- log::Err("Hello SimpleKernel\n");
- log::debug << "Hello SimpleKernel\n";
- log::info << "Hello SimpleKernel\n";
- log::warn << "Hello SimpleKernel\n";
- log::err << "Hello SimpleKernel\n";
+ klog::Debug("Hello SimpleKernel\n");
+ klog::Info("Hello SimpleKernel\n");
+ klog::Warn("Hello SimpleKernel\n");
+ klog::Err("Hello SimpleKernel\n");
+ klog::debug << "Hello SimpleKernel\n";
+ klog::info << "Hello SimpleKernel\n";
+ klog::warn << "Hello SimpleKernel\n";
+ klog::err << "Hello SimpleKernel\n";
DumpStack();
diff --git a/test/system_test/cxx_init_test/main.cpp b/test/system_test/cxx_init_test/main.cpp
index e1296e891..9d1c02a28 100644
--- a/test/system_test/cxx_init_test/main.cpp
+++ b/test/system_test/cxx_init_test/main.cpp
@@ -17,7 +17,7 @@
#include
#include "basic_info.hpp"
-#include "cpu.hpp"
+#include "cpu/cpu.hpp"
#include "sk_cstdio"
#include "sk_cstring"
#include "sk_libcxx.h"
diff --git a/test/system_test/gnu_efi_test/main.cpp b/test/system_test/gnu_efi_test/main.cpp
index c937366fc..12008af63 100644
--- a/test/system_test/gnu_efi_test/main.cpp
+++ b/test/system_test/gnu_efi_test/main.cpp
@@ -17,7 +17,7 @@
#include
#include "basic_info.hpp"
-#include "cpu.hpp"
+#include "cpu/cpu.hpp"
#include "sk_cstdio"
#include "sk_cstring"
diff --git a/test/unit_test/CMakeLists.txt b/test/unit_test/CMakeLists.txt
index 9646735c8..b447ccef7 100644
--- a/test/unit_test/CMakeLists.txt
+++ b/test/unit_test/CMakeLists.txt
@@ -21,6 +21,7 @@ add_executable(${PROJECT_NAME}
)
target_include_directories(${PROJECT_NAME} PRIVATE
+ ./
${CMAKE_SOURCE_DIR}/src/kernel/include
${CMAKE_SOURCE_DIR}/src/kernel/arch
)
@@ -35,7 +36,7 @@ target_link_options(${PROJECT_NAME} PRIVATE
target_link_libraries(${PROJECT_NAME} PRIVATE
${DEFAULT_TEST_LINK_LIB}
- ${dtc_BINARY_DIR}/libfdt/libfdt.a
+ dtc-lib
)
gtest_discover_tests(${PROJECT_NAME})
diff --git a/test/unit_test/aarch64_cpu_test.cpp b/test/unit_test/aarch64_cpu_test.cpp
index 90a9f5fec..964360f70 100644
--- a/test/unit_test/aarch64_cpu_test.cpp
+++ b/test/unit_test/aarch64_cpu_test.cpp
@@ -16,18 +16,18 @@
#include
-#include "aarch64/include/cpu.hpp"
+#include "aarch64/include/cpu/cpu.hpp"
TEST(Aarch64RegInfoBaseTest, ValueTest) {
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitOffset, 0);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitWidth, 64);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitMask, 0xFFFFFFFFFFFFFFFF);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitOffset, 0);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitWidth, 64);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
}
TEST(Aarch64X29InfoTest, ValueTest) {
- EXPECT_EQ(cpu::reginfo::X29Info::kBitOffset, 0);
- EXPECT_EQ(cpu::reginfo::X29Info::kBitWidth, 64);
- EXPECT_EQ(cpu::reginfo::X29Info::kBitMask, 0xFFFFFFFFFFFFFFFF);
- EXPECT_EQ(cpu::reginfo::X29Info::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::X29Info::kBitOffset, 0);
+ EXPECT_EQ(cpu::register_info::X29Info::kBitWidth, 64);
+ EXPECT_EQ(cpu::register_info::X29Info::kBitMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::X29Info::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
}
diff --git a/test/unit_test/riscv64_cpu_test.cpp b/test/unit_test/riscv64_cpu_test.cpp
index 97e43f7d5..f90ebf04c 100644
--- a/test/unit_test/riscv64_cpu_test.cpp
+++ b/test/unit_test/riscv64_cpu_test.cpp
@@ -16,18 +16,18 @@
#include
-#include "riscv64/include/cpu.hpp"
+#include "riscv64/include/cpu/cpu.hpp"
TEST(Riscv64RegInfoBaseTest, ValueTest) {
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitOffset, 0);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitWidth, 64);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitMask, 0xFFFFFFFFFFFFFFFF);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitOffset, 0);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitWidth, 64);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
}
TEST(Riscv64FpInfoTest, ValueTest) {
- EXPECT_EQ(cpu::reginfo::FpInfo::kBitOffset, 0);
- EXPECT_EQ(cpu::reginfo::FpInfo::kBitWidth, 64);
- EXPECT_EQ(cpu::reginfo::FpInfo::kBitMask, 0xFFFFFFFFFFFFFFFF);
- EXPECT_EQ(cpu::reginfo::FpInfo::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::FpInfo::kBitOffset, 0);
+ EXPECT_EQ(cpu::register_info::FpInfo::kBitWidth, 64);
+ EXPECT_EQ(cpu::register_info::FpInfo::kBitMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::FpInfo::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
}
diff --git a/test/unit_test/sk_cstdio b/test/unit_test/sk_cstdio
new file mode 100644
index 000000000..bbfe7e310
--- /dev/null
+++ b/test/unit_test/sk_cstdio
@@ -0,0 +1,22 @@
+
+/**
+ * @file sk_cstdio
+ * @brief 测试使用的 sk_cstdio
+ * @author Zone.N (Zone.Niuzh@hotmail.com)
+ * @version 1.0
+ * @date 2023-05-08
+ * @copyright MIT LICENSE
+ * https://github.com/Simple-XX/SimpleKernel
+ * @par change log:
+ *
+ * Date | Author | Description
+ * |
---|
2023-05-08 | Zone.N | 创建文件
+ * |
+ */
+
+#ifndef SIMPLEKERNEL_TEST_UNIT_TEST_SK_CSTDIO_
+#define SIMPLEKERNEL_TEST_UNIT_TEST_SK_CSTDIO_
+
+#include "cstdio"
+
+#endif /* SIMPLEKERNEL_TEST_UNIT_TEST_SK_CSTDIO_ */
diff --git a/test/unit_test/sk_cstring b/test/unit_test/sk_cstring
new file mode 100644
index 000000000..6d30ffda6
--- /dev/null
+++ b/test/unit_test/sk_cstring
@@ -0,0 +1,39 @@
+
+/**
+ * @file sk_cstring
+ * @brief 测试使用的 sk_cstring
+ * @author Zone.N (Zone.Niuzh@hotmail.com)
+ * @version 1.0
+ * @date 2023-05-08
+ * @copyright MIT LICENSE
+ * https://github.com/Simple-XX/SimpleKernel
+ * @par change log:
+ *
+ * Date | Author | Description
+ * |
---|
2023-05-08 | Zone.N | 创建文件
+ * |
+ */
+
+#ifndef SIMPLEKERNEL_TEST_UNIT_TEST_SK_CSTRING_
+#define SIMPLEKERNEL_TEST_UNIT_TEST_SK_CSTRING_
+
+#include "cstring"
+
+namespace sk_std {
+
+using ::memcmp;
+using ::memcpy;
+using ::memmove;
+using ::memset;
+using ::strcat;
+using ::strchr;
+using ::strcmp;
+using ::strcpy;
+using ::strlen;
+using ::strncmp;
+using ::strncpy;
+using ::strnlen;
+
+}; // namespace sk_std
+
+#endif /* SIMPLEKERNEL_TEST_UNIT_TEST_SK_CSTRING_ */
diff --git a/test/unit_test/sk_iostream b/test/unit_test/sk_iostream
new file mode 100644
index 000000000..e2d9e35f9
--- /dev/null
+++ b/test/unit_test/sk_iostream
@@ -0,0 +1,92 @@
+
+/**
+ * @file sk_iostream
+ * @brief 测试使用的 sk_iostream
+ * @author Zone.N (Zone.Niuzh@hotmail.com)
+ * @version 1.0
+ * @date 2021-09-18
+ * @copyright MIT LICENSE
+ * https://github.com/Simple-XX/SimpleKernel
+ * Based on https://github.com/MRNIU/MiniCRT
+ * @par change log:
+ *
+ * Date | Author | Description
+ * |
---|
2021-09-18 | digmouse233 | 迁移到 doxygen
+ * |
+ */
+
+#ifndef SIMPLEKERNEL_TEST_UNIT_TEST_SK_IOSTREAM_
+#define SIMPLEKERNEL_TEST_UNIT_TEST_SK_IOSTREAM_
+
+#include
+#include
+
+namespace sk_std {
+class ostream : public std::ostream {
+ public:
+ enum openmode : uint8_t {
+ in = 1,
+ out = 2,
+ binary = 4,
+ trunc = 8,
+ };
+
+ /// @name 构造/析构函数
+ /// @{
+ ostream() = default;
+ ostream(const ostream&) = default;
+ ostream(ostream&&) = default;
+ auto operator=(const ostream&) -> ostream& = default;
+ auto operator=(ostream&&) -> ostream& = default;
+ ~ostream() = default;
+ /// @}
+
+ virtual ostream& operator<<(int8_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(uint8_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(const char* val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(int16_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(uint16_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(int32_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(uint32_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(int64_t val) {
+ (void)val;
+ return *this;
+ }
+ virtual ostream& operator<<(uint64_t val) {
+ (void)val;
+ return *this;
+ }
+
+ virtual ostream& operator<<(ostream& (*manip)(ostream&)) {
+ return manip(*this);
+ }
+};
+
+inline ostream& endl(ostream& os) { return os << "\n"; }
+
+[[maybe_unused]] static ostream cout;
+
+}; // namespace sk_std
+
+#endif /* SIMPLEKERNEL_TEST_UNIT_TEST_SK_IOSTREAM_ */
diff --git a/test/unit_test/x86_64_cpu_test.cpp b/test/unit_test/x86_64_cpu_test.cpp
index 06f67b70b..a79e8af43 100644
--- a/test/unit_test/x86_64_cpu_test.cpp
+++ b/test/unit_test/x86_64_cpu_test.cpp
@@ -16,18 +16,18 @@
#include
-#include "x86_64/include/cpu.hpp"
+#include "x86_64/include/cpu/cpu.hpp"
TEST(x8664RegInfoBaseTest, ValueTest) {
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitOffset, 0);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitWidth, 64);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kBitMask, 0xFFFFFFFFFFFFFFFF);
- EXPECT_EQ(cpu::reginfo::RegInfoBase::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitOffset, 0);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitWidth, 64);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kBitMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RegInfoBase::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
}
TEST(x8664RbpInfoTest, ValueTest) {
- EXPECT_EQ(cpu::reginfo::RbpInfo::kBitOffset, 0);
- EXPECT_EQ(cpu::reginfo::RbpInfo::kBitWidth, 64);
- EXPECT_EQ(cpu::reginfo::RbpInfo::kBitMask, 0xFFFFFFFFFFFFFFFF);
- EXPECT_EQ(cpu::reginfo::RbpInfo::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RbpInfo::kBitOffset, 0);
+ EXPECT_EQ(cpu::register_info::RbpInfo::kBitWidth, 64);
+ EXPECT_EQ(cpu::register_info::RbpInfo::kBitMask, 0xFFFFFFFFFFFFFFFF);
+ EXPECT_EQ(cpu::register_info::RbpInfo::kAllSetMask, 0xFFFFFFFFFFFFFFFF);
}