SimpleKernel 1.17.0
Loading...
Searching...
No Matches
arch_main.cpp File Reference
#include <cpu_io.h>
#include <cstring>
#include "arch.h"
#include "basic_info.hpp"
#include "interrupt.h"
#include "kernel.h"
#include "kernel_elf.hpp"
#include "kernel_fdt.hpp"
#include "kstd_cstdio"
#include "per_cpu.hpp"
#include "sk_stdlib.h"
#include "virtual_memory.hpp"
Include dependency graph for arch_main.cpp:

Go to the source code of this file.

Functions

auto ArchInit (int argc, const char **argv) -> void
 体系结构相关初始化
 
auto ArchInitSMP (int argc, const char **argv) -> void
 从核的体系结构相关初始化
 
auto WakeUpOtherCores () -> void
 唤醒其余 core
 
auto InitTaskContext (cpu_io::CalleeSavedContext *task_context, void(*entry)(void *), void *arg, uint64_t stack_top) -> void
 初始化内核线程的任务上下文(重载1)
 
auto InitTaskContext (cpu_io::CalleeSavedContext *task_context, cpu_io::TrapContext *trap_context_ptr, uint64_t stack_top) -> void
 初始化用户线程的任务上下文(重载2)
 

Function Documentation

◆ ArchInit()

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

体系结构相关初始化

Parameters
argc在不同体系结构有不同含义,同 _start
argv在不同体系结构有不同含义,同 _start
Precondition
引导程序已完成基本硬件初始化
Postcondition
架构相关硬件(串口、内存、设备树等)已初始化

Definition at line 48 of file arch_main.cpp.

48 {
49 KernelFdtSingleton::create(strtoull(argv[2], nullptr, 16));
50
51 BasicInfoSingleton::create(argc, argv);
52
53 // 解析内核 elf 信息
54 KernelElfSingleton::create(BasicInfoSingleton::instance().elf_addr);
55
56 KernelFdtSingleton::instance().CheckPSCI().or_else(
57 [](Error err) -> Expected<void> {
58 klog::Err("CheckPSCI failed: {}", err.message());
59 return {};
60 });
61
62 klog::Info("Hello aarch64 ArchInit");
63}
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
#define strtoull
错误类型,用于 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:

◆ ArchInitSMP()

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

从核的体系结构相关初始化

Parameters
argc在不同体系结构有不同含义,同 _start
argv在不同体系结构有不同含义,同 _start
Precondition
主核已完成 ArchInit
Postcondition
从核的架构相关硬件已初始化

Definition at line 65 of file arch_main.cpp.

66 {}

◆ InitTaskContext() [1/2]

auto InitTaskContext ( cpu_io::CalleeSavedContext task_context,
cpu_io::TrapContext trap_context_ptr,
uint64_t  stack_top 
) -> void

初始化用户线程的任务上下文(重载2)

Parameters
task_context指向任务上下文的指针
trap_context_ptr指向 Trap 上下文的指针
stack_top内核栈顶地址
Precondition
task_context 不为 nullptr,trap_context_ptr 已填充用户态寄存器
Postcondition
task_context 已设置为经由 trap_return 返回用户态的状态

Definition at line 90 of file arch_main.cpp.

92 {
93 // 清零上下文
94 std::memset(task_context, 0, sizeof(cpu_io::CalleeSavedContext));
95
96 task_context->ReturnAddress() =
97 reinterpret_cast<uint64_t>(kernel_thread_entry);
98 task_context->EntryFunction() = reinterpret_cast<uint64_t>(trap_return);
99 task_context->EntryArgument() = reinterpret_cast<uint64_t>(trap_context_ptr);
100 task_context->StackPointer() = stack_top;
101}
auto kernel_thread_entry() -> void
Definition arch.cpp:44
auto trap_return(void *) -> void
Definition arch.cpp:45
__always_inline uint64_t & EntryFunction()
Definition cpu_io.h:208
__always_inline uint64_t & EntryArgument()
Definition cpu_io.h:209
__always_inline uint64_t & ReturnAddress()
Definition cpu_io.h:207
__always_inline uint64_t & StackPointer()
Definition cpu_io.h:210
Here is the call graph for this function:

◆ InitTaskContext() [2/2]

auto InitTaskContext ( cpu_io::CalleeSavedContext task_context,
void(*)(void *)  entry,
void *  arg,
uint64_t  stack_top 
) -> void

初始化内核线程的任务上下文(重载1)

Parameters
task_context指向任务上下文的指针
entry线程入口函数
arg传递给线程的参数
stack_top内核栈顶地址
Precondition
task_context 不为 nullptr,stack_top 已按架构要求对齐
Postcondition
task_context 已设置为可被 switch_to 恢复的状态

Definition at line 77 of file arch_main.cpp.

79 {
80 // 清零上下文
81 std::memset(task_context, 0, sizeof(cpu_io::CalleeSavedContext));
82
83 task_context->ReturnAddress() =
84 reinterpret_cast<uint64_t>(kernel_thread_entry);
85 task_context->EntryFunction() = reinterpret_cast<uint64_t>(entry);
86 task_context->EntryArgument() = reinterpret_cast<uint64_t>(arg);
87 task_context->StackPointer() = stack_top;
88}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ WakeUpOtherCores()

auto WakeUpOtherCores ( ) -> void

唤醒其余 core

Precondition
主核已完成初始化
Postcondition
所有从核开始执行 ArchInitSMP

Definition at line 68 of file arch_main.cpp.

68 {
69 for (size_t i = 0; i < BasicInfoSingleton::instance().core_count; i++) {
70 auto ret = cpu_io::psci::CpuOn(i, reinterpret_cast<uint64_t>(_boot), 0);
71 if ((ret != cpu_io::psci::SUCCESS) && (ret != cpu_io::psci::ALREADY_ON)) {
72 klog::Warn("hart {} start failed: {}", i, ret);
73 }
74 }
75}
void _boot()
内核入口,在 boot.S 中定义
auto Warn(etl::format_string< Args... > fmt, Args &&... args) -> void
以 WARN 级别记录日志
Here is the call graph for this function:
Here is the caller graph for this function: