SimpleKernel 1.17.0
Loading...
Searching...
No Matches
arch.cpp File Reference
#include <cassert>
#include <cstring>
#include "cpu_io.h"
#include "per_cpu.hpp"
#include "task_manager.hpp"
#include "test_environment_state.hpp"
#include <stdio.h>
Include dependency graph for arch.cpp:

Go to the source code of this file.

Functions

void switch_to (cpu_io::CalleeSavedContext *prev_ctx, cpu_io::CalleeSavedContext *next_ctx)
 
void kernel_thread_entry ()
 
void trap_return (void *)
 
void trap_entry ()
 
void InitTaskContext (cpu_io::CalleeSavedContext *task_context, void(*entry)(void *), void *arg, uint64_t stack_top)
 初始化内核线程的任务上下文(重载1)
 
void InitTaskContext (cpu_io::CalleeSavedContext *task_context, cpu_io::TrapContext *trap_context_ptr, uint64_t stack_top)
 初始化用户线程的任务上下文(重载2)
 
void etl_putchar (int c)
 早期控制台字符输出
 

Function Documentation

◆ etl_putchar()

void etl_putchar ( int  c)

早期控制台字符输出

Parameters
c要输出的字符

早期控制台字符输出

Definition at line 77 of file arch.cpp.

77{ putchar(c); }

◆ InitTaskContext() [1/2]

void 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 返回用户态的状态
Todo:
x86_64 实现待补充

Definition at line 62 of file arch.cpp.

64 {
65 // 清零上下文
66 std::memset(task_context, 0, sizeof(cpu_io::CalleeSavedContext));
67
68 task_context->ReturnAddress() =
69 reinterpret_cast<uint64_t>(kernel_thread_entry);
70 task_context->EntryFunction() = reinterpret_cast<uint64_t>(trap_return);
71 task_context->EntryArgument() = reinterpret_cast<uint64_t>(trap_context_ptr);
72 task_context->StackPointer() = stack_top;
73}
void kernel_thread_entry()
Definition arch.cpp:44
void trap_return(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]

void 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 恢复的状态
Todo:
x86_64 实现待补充

Definition at line 50 of file arch.cpp.

51 {
52 // 清零上下文
53 std::memset(task_context, 0, sizeof(cpu_io::CalleeSavedContext));
54
55 task_context->ReturnAddress() =
56 reinterpret_cast<uint64_t>(kernel_thread_entry);
57 task_context->EntryFunction() = reinterpret_cast<uint64_t>(entry);
58 task_context->EntryArgument() = reinterpret_cast<uint64_t>(arg);
59 task_context->StackPointer() = stack_top;
60}
Here is the call graph for this function:

◆ kernel_thread_entry()

void kernel_thread_entry ( )

Definition at line 44 of file arch.cpp.

44{}
Here is the caller graph for this function:

◆ switch_to()

void switch_to ( cpu_io::CalleeSavedContext prev_ctx,
cpu_io::CalleeSavedContext next_ctx 
)

Definition at line 15 of file arch.cpp.

16 {
17 auto* env_state =
19 assert(env_state &&
20 "TestEnvironmentState not set for current thread. "
21 "Did you forget to call SetCurrentThreadEnvironment()?");
22
23 auto& core_env = env_state->GetCurrentCoreEnv();
24
25 // 从上下文指针查找对应的任务
26 auto* prev_task = env_state->FindTaskByContext(prev_ctx);
27 auto* next_task = env_state->FindTaskByContext(next_ctx);
28
29 // 获取 PerCpu 数据
31
32 // 记录切换事件到环境层
34 event.timestamp = (per_cpu.sched_data ? per_cpu.sched_data->local_tick : 0);
35 event.from = prev_task;
36 event.to = next_task;
37 event.core_id = core_env.core_id;
38 core_env.switch_history.push_back(event);
39
40 // 更新 PerCpu 的当前线程
41 per_cpu.running_task = next_task;
42}
static auto GetCurrentThreadEnvironment() -> TestEnvironmentState *
获取当前线程的环境实例指针(供 Mock 层调用)
static __always_inline auto GetCurrentCore() -> PerCpu &
获取当前核心的 PerCpu 数据
Definition per_cpu.hpp:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trap_entry()

void trap_entry ( )

Definition at line 46 of file arch.cpp.

46{}
Here is the caller graph for this function:

◆ trap_return()

void trap_return ( void *  )

Definition at line 45 of file arch.cpp.

45{}
Here is the caller graph for this function: