SimpleKernel 1.17.0
Loading...
Searching...
No Matches
kstd_libcxx.cpp
Go to the documentation of this file.
1
5#include <cpu_io.h>
6#include <stdint.h>
7
8#include <algorithm>
9#include <atomic>
10
11#include "kernel_log.hpp"
12
14using function_t = void (*)();
15// 在 link.ld 中定义
25void* __dso_handle = nullptr;
26
28static constexpr size_t kMaxAtExitFuncsCount = 128;
29
32 // 析构函数指针
33 void (*destructor_func)(void*);
34 // 要析构的对象
35 void* obj_ptr;
36 // 共享库对象,在内核中没有使用
38};
39
43static size_t atexit_func_count = 0;
44
51extern "C" auto __cxa_atexit(void (*destructor_func)(void*), void* obj_ptr,
52 void*) -> int {
54 return -1;
55 }
60 return 0;
61}
62
68extern "C" auto __cxa_finalize(void* destructor_func) -> void {
69 if (destructor_func == nullptr) {
70 // 如果 destructor_func 为 nullptr,调用所有析构函数
71 for (size_t i = atexit_func_count; i > 0; --i) {
72 size_t idx = i - 1;
73 if (atexit_funcs[idx].destructor_func != nullptr) {
74 (*atexit_funcs[idx].destructor_func)(atexit_funcs[idx].obj_ptr);
75 }
76 }
77 } else {
78 // 不为空时只调用对应的析构函数
79 for (size_t i = atexit_func_count; i > 0; --i) {
80 size_t idx = i - 1;
81 if (reinterpret_cast<void*>(atexit_funcs[idx].destructor_func) ==
82 destructor_func) {
83 (*atexit_funcs[idx].destructor_func)(atexit_funcs[idx].obj_ptr);
84 atexit_funcs[idx].destructor_func = nullptr;
85 }
86 }
87 }
88}
89
93
111struct GuardType {
113 std::atomic<uint64_t> guard{0};
114
115 static constexpr uint64_t kInitializedMask = 0x01;
116 static constexpr uint64_t kInUseMask = 0x100;
117};
118
119static_assert(sizeof(GuardType) == 8, "GuardType must be 64 bits per ABI");
120
127extern "C" auto __cxa_guard_acquire(GuardType* guard) -> int {
128 uint64_t expected = 0;
129
130 // 紧凑的 CAS 循环:尝试将 0 -> kInUseMask
131 // compare_exchange_weak 失败时会将当前值写入 expected
132 while (!guard->guard.compare_exchange_weak(expected, GuardType::kInUseMask,
133 std::memory_order_acq_rel,
134 std::memory_order_acquire)) {
135 // CAS 失败,检查失败原因
136 if ((expected & GuardType::kInitializedMask) != 0U) {
137 // 已被其他核心初始化完成
138 return 0;
139 }
140
141 // 其他核心正在初始化中,等待
143
144 // 重置 expected 以便下次 CAS 尝试
145 // 如果当前值仍有 kInUseMask,CAS 会再次失败并更新 expected
146 expected = 0;
147 }
148
149 // CAS 成功,当前核心负责初始化
150 return 1;
151}
152
158extern "C" auto __cxa_guard_release(GuardType* guard) -> void {
159 // 设置 initialized 位,清除 in_use 位
160 guard->guard.store(GuardType::kInitializedMask, std::memory_order_release);
161}
162
167extern "C" auto __cxa_guard_abort(GuardType* guard) -> void {
168 // 清除所有位
169 guard->guard.store(0, std::memory_order_release);
170}
171
173
177extern "C" auto __cxa_pure_virtual() -> void {
178 while (true) {
180 }
181}
182
186extern "C" auto abort() -> void {
187 while (true) {
189 }
190}
191
199extern "C" [[noreturn]] void __assert_fail(const char* assertion,
200 const char* file, unsigned int line,
201 const char* function) {
202 klog::RawPut("\n[ASSERT FAILED] ");
203 klog::RawPut(file);
204 etl_putchar(':');
205 char buf_line[12];
206 auto* end = etl::format_to_n(buf_line, sizeof(buf_line) - 1, "{}", line);
207 *end = '\0';
208 klog::RawPut(" in ");
209 klog::RawPut(function);
210 klog::RawPut("\n Expression: ");
211 klog::RawPut(assertion);
212 etl_putchar('\n');
213 while (1) {
215 }
216}
217
221auto CppInit() -> void {
222 // 调用构造函数
223 std::for_each(&__init_array_start, &__init_array_end,
224 [](function_t func) { (func)(); });
225}
226
230auto CppDeInit() -> void {
231 // 调用析构函数
232 std::for_each(&__fini_array_start, &__fini_array_end,
233 [](function_t func) { (func)(); });
234}
auto etl_putchar(int c) -> void
早期控制台字符输出
void * end[]
内核结束
void * __dso_handle
动态共享对象标识,内核使用静态链接,此变量在内核中没有使用
static size_t atexit_func_count
析构函数个数
function_t __fini_array_end
全局析构函数函数指针终点地址
void(*)() function_t
全局构造函数函数指针
auto CppDeInit() -> void
析构 c++ 全局对象
auto __cxa_pure_virtual() -> void
function_t __fini_array_start
全局析构函数函数指针起点地址
function_t __init_array_start
全局构造函数函数指针起点地址
auto __cxa_guard_release(GuardType *guard) -> void
static constexpr size_t kMaxAtExitFuncsCount
最大析构函数数量
function_t __init_array_end
全局构造函数函数指针终点地址
auto __cxa_guard_abort(GuardType *guard) -> void
void __assert_fail(const char *assertion, const char *file, unsigned int line, const char *function)
libc assert() 失败时调用的底层接口
auto __cxa_guard_acquire(GuardType *guard) -> int
auto abort() -> void
auto __cxa_finalize(void *destructor_func) -> void
auto CppInit() -> void
构造 c++ 全局对象
static atexit_func_entry_t atexit_funcs[kMaxAtExitFuncsCount]
析构函数数组
auto __cxa_atexit(void(*destructor_func)(void *), void *obj_ptr, void *) -> int
void Pause()
Definition cpu_io.h:20
__always_inline auto RawPut(const char *msg) -> void
绕过队列直接输出至串口(用于 panic 路径)
std::atomic< uint64_t > guard
原子守护变量:bit 0 = is_initialized, bit 8 = is_in_use
static constexpr uint64_t kInUseMask
static constexpr uint64_t kInitializedMask
析构函数结构
void * dso_handle
void(* destructor_func)(void *)
void * obj_ptr