新增rust ffi (#77)

* 引入cargo

* 取消对Cargo.lock的跟踪

* 解决vscode报错问题

* new: rust的代码能够调用c语言的printk_color

* 1、将原本run.sh的工作拆解,变为几个不同的make命令
2、在docker镜像中编译rust

* 更改workflow

* update workflow

* new: 解决workflow无法通过编译的问题
This commit is contained in:
login
2022-11-11 15:35:37 +08:00
committed by GitHub
parent 5e023cf791
commit 2813126e31
271 changed files with 609 additions and 307 deletions

13
kernel/src/time/Makefile Normal file
View File

@ -0,0 +1,13 @@
all: timer.o sleep.o
CFLAGS += -I .
timer.o: timer.c
$(CC) $(CFLAGS) -c timer.c -o timer.o
sleep.o: sleep.c
$(CC) $(CFLAGS) -c sleep.c -o sleep.o
clean:
echo "Done."

86
kernel/src/time/sleep.c Normal file
View File

@ -0,0 +1,86 @@
#include "sleep.h"
#include <common/errno.h>
#include <time/timer.h>
#include <process/process.h>
#include <sched/sched.h>
#include <mm/slab.h>
#include <common/cpu.h>
#include <common/glib.h>
/**
* @brief nanosleep定时事件到期后唤醒指定的进程
*
* @param pcb 待唤醒的进程的pcb
*/
void nanosleep_handler(void *pcb)
{
process_wakeup((struct process_control_block *)pcb);
}
/**
* @brief 休眠指定时间
*
* @param rqtp 指定休眠的时间
* @param rmtp 返回的剩余休眠时间
* @return int
*/
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
if (rqtp->tv_nsec < 0 || rqtp->tv_nsec >= 1000000000)
return -EINVAL;
// 对于小于500us的时间使用spin/rdtsc来进行定时
if (rqtp->tv_nsec < 500000)
{
uint64_t expired_tsc = rdtsc() + (((uint64_t)rqtp->tv_nsec) * Cpu_tsc_freq) / 1000000000;
while (rdtsc() < expired_tsc)
;
if (rmtp != NULL)
{
rmtp->tv_nsec = 0;
rmtp->tv_sec = 0;
}
return 0;
}
// 增加定时任务
struct timer_func_list_t *sleep_task = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
memset(sleep_task, 0, sizeof(struct timer_func_list_t));
timer_func_init_us(sleep_task, &nanosleep_handler, (void *)current_pcb, rqtp->tv_nsec / 1000);
timer_func_add(sleep_task);
current_pcb->state = PROC_INTERRUPTIBLE;
current_pcb->flags |= PF_NEED_SCHED;
sched();
// todo: 增加信号唤醒的功能后设置rmtp
if (rmtp != NULL)
{
rmtp->tv_nsec = 0;
rmtp->tv_sec = 0;
}
return 0;
}
/**
* @brief 睡眠指定时间
*
* @param usec 微秒
* @return int
*/
int usleep(useconds_t usec)
{
struct timespec ts = {
tv_sec : (long int)(usec / 1000000),
tv_nsec : (long int)(usec % 1000000) * 1000UL
};
return nanosleep(&ts, NULL);
}

23
kernel/src/time/sleep.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include <common/glib.h>
#include <process/ptrace.h>
#include <common/time.h>
/**
* @brief 休眠指定时间
*
* @param rqtp 指定休眠的时间
* @param rmtp 返回的剩余休眠时间
* @return int
*/
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
/**
* @brief 睡眠指定时间
*
* @param usec 微秒
* @return int
*/
int usleep(useconds_t usec);

178
kernel/src/time/timer.c Normal file
View File

@ -0,0 +1,178 @@
#include "timer.h"
#include <common/kprint.h>
#include <driver/timers/HPET/HPET.h>
#include <exception/softirq.h>
#include <mm/slab.h>
#include <process/process.h>
#include <sched/sched.h>
struct timer_func_list_t timer_func_head;
static spinlock_t sched_lock;
// 定时器循环阈值每次最大执行20个定时器任务
#define TIMER_RUN_CYCLE_THRESHOLD 20
void test_timer()
{
printk_color(ORANGE, BLACK, "(test_timer)");
}
void timer_init()
{
spin_init(&sched_lock);
timer_jiffies = 0;
timer_func_init(&timer_func_head, NULL, NULL, -1UL);
register_softirq(TIMER_SIRQ, &do_timer_softirq, NULL);
struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
timer_func_init(tmp, &test_timer, NULL, 5);
timer_func_add(tmp);
kdebug("timer func initialized.");
}
/**
* @brief 处理时间软中断
*
* @param data
*/
void do_timer_softirq(void *data)
{
// todo: 修改这里以及 softirq 的部分,使得 timer 具有并行性
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
int cycle_count = 0;
while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies))
{
spin_lock(&sched_lock);
timer_func_del(tmp);
tmp->func(tmp->data);
kfree(tmp);
spin_unlock(&sched_lock);
++cycle_count;
// kdebug("SOLVE SOFT IRQ %d", cycle_count);
// 当前定时器达到阈值
if (cycle_count == TIMER_RUN_CYCLE_THRESHOLD)
break;
tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
}
}
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_ms 定时时长(单位ms)
*/
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms)
{
list_init(&timer_func->list);
timer_func->func = func;
timer_func->data = data;
timer_func->expire_jiffies = cal_next_n_ms_jiffies(expire_ms); // 设置过期的时间片
}
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_us 定时时长(单位us)
*/
void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us)
{
list_init(&timer_func->list);
timer_func->func = func;
timer_func->data = data;
timer_func->expire_jiffies = cal_next_n_us_jiffies(expire_us); // 设置过期的时间片
// kdebug("timer_func->expire_jiffies=%ld",cal_next_n_us_jiffies(expire_us));
}
/**
* @brief 将定时功能添加到列表中
*
* @param timer_func 待添加的定时功能
*/
void timer_func_add(struct timer_func_list_t *timer_func)
{
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
if (list_empty(&timer_func_head.list) == false)
while (tmp->expire_jiffies < timer_func->expire_jiffies)
tmp = container_of(list_next(&tmp->list), struct timer_func_list_t, list);
list_add(&tmp->list, &(timer_func->list));
}
/**
* @brief 将定时功能从列表中删除
*
* @param timer_func
*/
void timer_func_del(struct timer_func_list_t *timer_func)
{
list_del(&timer_func->list);
}
uint64_t sys_clock(struct pt_regs *regs)
{
return timer_jiffies;
}
uint64_t clock()
{
return timer_jiffies;
}
/**
* @brief 辅助函数传进schedule_timeout函数中, 然后时间一到就唤醒 pcb 指向的进程(即自身)
*
* @param pcb process_control_block
*/
static void __wake_up_helper(void *pcb)
{
BUG_ON(pcb == NULL);
BUG_ON(process_wakeup((struct process_control_block *)pcb) != 0); // 正常唤醒,返回值为0
}
/**
* @brief 睡眠timeout的时间之后唤醒进程/线程
*
* @param timeout
* @return long
*/
long schedule_timeout_ms(long timeout)
{
if (timeout == MAX_TIMEOUT) // 无期停止, 意味着不会调用func
{
sched();
return MAX_TIMEOUT;
}
else if (timeout < 0)
{
BUG_ON(1);
return 0;
}
spin_lock(&sched_lock);
struct timer_func_list_t timer={0};
timer_func_init(&timer, &__wake_up_helper, current_pcb, timeout);
timer_func_add(&timer);
current_pcb->state &= ~(PROC_RUNNING);
spin_unlock(&sched_lock);
sched();
timeout -= timer_jiffies;
return timeout < 0 ? 0 : timeout;
}

76
kernel/src/time/timer.h Normal file
View File

@ -0,0 +1,76 @@
#pragma once
#include <common/glib.h>
#include <driver/timers/HPET/HPET.h>
#include <driver/timers/rtc/rtc.h>
// 定义LONG_MAX为最大超时时间 - 允许负数
#define MAX_TIMEOUT (int64_t)((1ul << 63) - 1)
uint64_t volatile timer_jiffies = 0; // 系统时钟计数
// 计算接下来n毫秒对应的系统时间片
#define cal_next_n_ms_jiffies(expire_ms) (timer_jiffies + 1000 * (expire_ms))
// 计算接下来n微秒对应的系统时间片
#define cal_next_n_us_jiffies(expire_us) (timer_jiffies + (expire_us))
void timer_init();
void do_timer_softirq(void *data);
/**
* @brief 定时功能队列
*
*/
struct timer_func_list_t
{
struct List list;
uint64_t expire_jiffies;
void (*func)(void *data);
void *data;
};
extern struct timer_func_list_t timer_func_head;
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_ms 定时时长(单位ms)
*/
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms);
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_us 定时时长(单位us)
*/
void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us);
/**
* @brief 将定时功能添加到列表中
*
* @param timer_func 待添加的定时功能
*/
void timer_func_add(struct timer_func_list_t *timer_func);
/**
* @brief 将定时功能从列表中删除
*
* @param timer_func
*/
void timer_func_del(struct timer_func_list_t *timer_func);
uint64_t clock();
/**
* @brief 睡眠timeout的时间之后唤醒进程/线程
*
* @param timeout
* @return long
*/
long schedule_timeout_ms(long timeout);