new: kthread机制

This commit is contained in:
fslongjin
2022-09-30 15:30:50 +08:00
parent edef02286e
commit 02a6b3a303
21 changed files with 802 additions and 326 deletions

View File

@ -28,3 +28,19 @@ static inline long __must_check IS_ERR_OR_NULL(const void* ptr)
{
return !ptr || IS_ERR_VALUE((uint64_t)ptr);
}
/**
* @brief 将错误码转换为指针
*
* @param error 错误码
* @return void* 转换后的指针
*/
static inline void* __must_check ERR_PTR(long error)
{
return (void*)(error);
}
static inline long __must_check PTR_ERR(void * ptr)
{
return (long)ptr;
}

View File

@ -11,29 +11,13 @@
#include <common/stddef.h>
#include <arch/arch.h>
#include <common/compiler.h>
#include <common/list.h>
#define sti() __asm__ __volatile__("sti\n\t" :: \
: "memory") //开启外部中断
#define cli() __asm__ __volatile__("cli\n\t" :: \
: "memory") //关闭外部中断
#define nop() __asm__ __volatile__("nop\n\t")
#define hlt() __asm__ __volatile__("hlt\n\t")
#define pause() asm volatile("pause\n\t"); // 处理器等待一段时间
//内存屏障
#define io_mfence() __asm__ __volatile__("mfence\n\t" :: \
: "memory") // 在mfence指令前的读写操作必须在mfence指令后的读写操作前完成。
#define io_sfence() __asm__ __volatile__("sfence\n\t" :: \
: "memory") // 在sfence指令前的写操作必须在sfence指令后的写操作前完成
#define io_lfence() __asm__ __volatile__("lfence\n\t" :: \
: "memory") // 在lfence指令前的读操作必须在lfence指令后的读操作前完成。
#define rdtsc() ({ \
uint64_t tmp1 = 0, tmp2 = 0; \
asm volatile("rdtsc" \
: "=d"(tmp1), "=a"(tmp2)::"memory"); \
(tmp1 << 32 | tmp2); \
})
#if ARCH(I386) || ARCH(X86_64)
#include <arch/x86_64/asm.h>
#else
#error Arch not supported.
#endif
/**
* @brief 根据结构体变量内某个成员变量member的基地址计算出该结构体变量的基地址
@ -83,121 +67,7 @@ static __always_inline ul ALIGN(const ul addr, const ul _align)
return (ul)((addr + _align - 1) & (~(_align - 1)));
}
//链表数据结构
struct List
{
struct List *prev, *next;
};
//初始化循环链表
static inline void list_init(struct List *list)
{
list->next = list;
io_mfence();
list->prev = list;
}
/**
* @brief
* @param entry 给定的节点
* @param node 待插入的节点
**/
static inline void list_add(struct List *entry, struct List *node)
{
node->next = entry->next;
barrier();
node->prev = entry;
barrier();
node->next->prev = node;
barrier();
entry->next = node;
}
/**
* @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
* @param entry 列表的入口
* @param node 待添加的节点
*/
static inline void list_append(struct List *entry, struct List *node)
{
struct List *tail = entry->prev;
list_add(tail, node);
}
/**
* @brief 从列表中删除节点
* @param entry 待删除的节点
*/
static inline void list_del(struct List *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
/**
* @brief 将新的链表结点替换掉旧的链表结点并使得旧的结点的前后指针均为NULL
*
* @param old 要被替换的结点
* @param new 新的要换上去的结点
*/
static inline void list_replace(struct List* old, struct List * new)
{
if(old->prev!=NULL)
old->prev->next=new;
new->prev = old->prev;
if(old->next!=NULL)
old->next->prev = new;
new->next = old->next;
old->prev = NULL;
old->next = NULL;
}
static inline bool list_empty(struct List *entry)
{
/**
* @brief 判断循环链表是否为空
* @param entry 入口
*/
if (entry == entry->next && entry->prev == entry)
return true;
else
return false;
}
/**
* @brief 获取链表的上一个元素
*
* @param entry
* @return 链表的上一个元素
*/
static inline struct List *list_prev(struct List *entry)
{
if (entry->prev != NULL)
return entry->prev;
else
return NULL;
}
/**
* @brief 获取链表的下一个元素
*
* @param entry
* @return 链表的下一个元素
*/
static inline struct List *list_next(struct List *entry)
{
if (entry->next != NULL)
return entry->next;
else
return NULL;
}
void *memset(void *dst, unsigned char C, ul size)
{
@ -323,113 +193,6 @@ void io_out32(unsigned short port, unsigned int value)
__asm__ __volatile__("cld;rep;outsw;mfence;" ::"d"(port), "S"(buffer), "c"(nr) \
: "memory")
/**
* @brief 读取rsp寄存器的值存储了页目录的基地址
*
* @return unsigned* rsp的值的指针
*/
unsigned long *get_rsp()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rsp, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief 读取rbp寄存器的值存储了页目录的基地址
*
* @return unsigned* rbp的值的指针
*/
unsigned long *get_rbp()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rbp, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief 读取ds寄存器的值存储了页目录的基地址
*
* @return unsigned* ds的值的指针
*/
unsigned long *get_ds()
{
ul *tmp;
__asm__ __volatile__(
"movq %%ds, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief 读取rax寄存器的值存储了页目录的基地址
*
* @return unsigned* rax的值的指针
*/
unsigned long *get_rax()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rax, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief 读取rbx寄存器的值存储了页目录的基地址
*
* @return unsigned* rbx的值的指针
*/
unsigned long *get_rbx()
{
ul *tmp;
__asm__ __volatile__(
"movq %%rbx, %0\n\t"
: "=r"(tmp)::"memory");
return tmp;
}
// ========= MSR寄存器组操作 =============
/**
* @brief 向msr寄存器组的address处的寄存器写入值value
*
* @param address 地址
* @param value 要写入的值
*/
void wrmsr(ul address, ul value)
{
__asm__ __volatile__("wrmsr \n\t" ::"d"(value >> 32), "a"(value & 0xffffffff), "c"(address)
: "memory");
}
/**
* @brief 从msr寄存器组的address地址处读取值
* rdmsr返回高32bits在edx低32bits在eax
* @param address 地址
* @return ul address处的寄存器的值
*/
ul rdmsr(ul address)
{
unsigned int tmp0, tmp1;
__asm__ __volatile__("rdmsr \n\t"
: "=d"(tmp0), "=a"(tmp1)
: "c"(address)
: "memory");
return ((ul)tmp0 << 32) | tmp1;
}
uint64_t get_rflags()
{
unsigned long tmp = 0;
__asm__ __volatile__("pushfq \n\t"
"movq (%%rsp), %0 \n\t"
"popfq \n\t"
: "=r"(tmp)::"memory");
return tmp;
}
/**
* @brief 验证地址空间是否为用户地址空间

77
kernel/common/kthread.h Normal file
View File

@ -0,0 +1,77 @@
#pragma once
#include <common/numa.h>
#include <process/proc-types.h>
#include <common/err.h>
#include <process/process.h>
struct process_control_block *kthread_create_on_node(int (*thread_fn)(void *data),
void *data,
int node,
const char name_fmt[], ...);
/**
* @brief 在当前结点上创建一个内核线程
*
* @param thread_fn 该内核线程要执行的函数
* @param data 传递给 thread_fn 的参数数据
* @param name_fmt printf-style format string for the thread name
* @param arg name_fmt的参数
*
* 请注意,该宏会创建一个内核线程,并将其设置为停止状态
*/
#define kthread_create(thread_fn, data, name_fmt, arg...) \
kthread_create_on_node(thread_fn, data, NUMA_NO_NODE, name_fmt, ##arg)
/**
* @brief 创建内核线程,并将其唤醒
*
* @param thread_fn 该内核线程要执行的函数
* @param data 传递给 thread_fn 的参数数据
* @param name_fmt printf-style format string for the thread name
* @param arg name_fmt的参数
*/
#define kthread_run(thread_fn, data, name_fmt, ...) \
({ \
struct process_control_block *__kt = kthread_create(thread_fn, data, name_fmt, ##__VA_ARGS__); \
if (!IS_ERR(__kt)) \
process_wakeup(__kt); \
__kt; \
})
/**
* @brief 向kthread发送停止信号请求其结束
*
* @param pcb 内核线程的pcb
* @return int 错误码
*/
int kthread_stop(struct process_control_block * pcb);
/**
* @brief 内核线程调用该函数,检查自身的标志位,判断自己是否应该执行完任务后退出
*
* @return true 内核线程应该退出
* @return false 无需退出
*/
bool kthread_should_stop(void);
/**
* @brief 让当前内核线程退出并返回result参数给kthread_stop()函数
*
* @param result 返回值
*/
void kthread_exit(long result);
/**
* @brief 初始化kthread机制(只应被process_init调用)
*
* @return int 错误码
*/
int kthread_mechanism_init();
/**
* @brief 设置pcb中的worker_private字段只应被设置一次
*
* @param pcb pcb
* @return bool 成功或失败
*/
bool kthread_set_worker_private(struct process_control_block *pcb);

131
kernel/common/list.h Normal file
View File

@ -0,0 +1,131 @@
#pragma once
#include <common/stddef.h>
#if ARCH(I386) || ARCH(X86_64)
#include <arch/x86_64/asm.h>
#else
#error Arch not supported.
#endif
//链表数据结构
struct List
{
struct List *prev, *next;
};
//初始化循环链表
static inline void list_init(struct List *list)
{
list->next = list;
io_mfence();
list->prev = list;
}
/**
* @brief
* @param entry 给定的节点
* @param node 待插入的节点
**/
static inline void list_add(struct List *entry, struct List *node)
{
node->next = entry->next;
barrier();
node->prev = entry;
barrier();
node->next->prev = node;
barrier();
entry->next = node;
}
/**
* @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
* @param entry 列表的入口
* @param node 待添加的节点
*/
static inline void list_append(struct List *entry, struct List *node)
{
struct List *tail = entry->prev;
list_add(tail, node);
}
/**
* @brief 从列表中删除节点
* @param entry 待删除的节点
*/
static inline void list_del(struct List *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
/**
* @brief
*
*/
#define list_del_init(entry) \
list_del(entry); \
list_init(entry);
/**
* @brief 将新的链表结点替换掉旧的链表结点并使得旧的结点的前后指针均为NULL
*
* @param old 要被替换的结点
* @param new 新的要换上去的结点
*/
static inline void list_replace(struct List *old, struct List *new)
{
if (old->prev != NULL)
old->prev->next = new;
new->prev = old->prev;
if (old->next != NULL)
old->next->prev = new;
new->next = old->next;
old->prev = NULL;
old->next = NULL;
}
static inline bool list_empty(struct List *entry)
{
/**
* @brief 判断循环链表是否为空
* @param entry 入口
*/
if (entry == entry->next && entry->prev == entry)
return true;
else
return false;
}
/**
* @brief 获取链表的上一个元素
*
* @param entry
* @return 链表的上一个元素
*/
static inline struct List *list_prev(struct List *entry)
{
if (entry->prev != NULL)
return entry->prev;
else
return NULL;
}
/**
* @brief 获取链表的下一个元素
*
* @param entry
* @return 链表的下一个元素
*/
static inline struct List *list_next(struct List *entry)
{
if (entry->next != NULL)
return entry->next;
else
return NULL;
}

3
kernel/common/numa.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#define NUMA_NO_NODE (-1)