2024-04-09 23:46:56 +08:00
|
|
|
#include "../include/task.h"
|
|
|
|
#include "../include/common.h"
|
2024-04-11 22:53:15 +08:00
|
|
|
#include "../include/graphics.h"
|
2024-04-14 17:14:26 +08:00
|
|
|
#include "../include/io.h"
|
2024-06-09 17:53:48 +08:00
|
|
|
#include "../include/description_table.h"
|
2024-04-09 23:46:56 +08:00
|
|
|
|
|
|
|
struct task_struct *running_proc_head = NULL;
|
|
|
|
struct task_struct *wait_proc_head = NULL;
|
|
|
|
struct task_struct *current = NULL;
|
|
|
|
|
2024-04-10 22:15:48 +08:00
|
|
|
extern page_directory_t *kernel_directory;
|
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
extern void switch_to(struct context *prev, struct context *next);
|
|
|
|
|
|
|
|
int now_pid = 0;
|
|
|
|
|
2024-04-15 00:24:36 +08:00
|
|
|
struct task_struct* get_current(){
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_proc_t(int *i,struct task_struct *base,struct task_struct *cur,int is_print){
|
2024-04-09 23:46:56 +08:00
|
|
|
if(cur->pid == base->pid){
|
2024-04-15 00:24:36 +08:00
|
|
|
if(is_print){
|
|
|
|
switch (cur->state) {
|
|
|
|
case TASK_RUNNABLE:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Running");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_SLEEPING:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Sleeping");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_UNINIT:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Init");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_ZOMBIE:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Zombie");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_DEATH:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Death");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
(*i)++;
|
|
|
|
} else{
|
2024-04-15 00:24:36 +08:00
|
|
|
if(is_print){
|
|
|
|
switch (cur->state) {
|
|
|
|
case TASK_RUNNABLE:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Running");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_SLEEPING:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Sleeping");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_UNINIT:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Init");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_ZOMBIE:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Zombie");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_DEATH:
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("%-17s %-2d %s\n",cur->name,cur->pid,"Death");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
(*i)++;
|
2024-04-15 00:24:36 +08:00
|
|
|
print_proc_t(i,base,cur->next,is_print);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 22:40:15 +08:00
|
|
|
int get_procs(){
|
|
|
|
int index = 0;
|
|
|
|
print_proc_t(&index,current,current->next,0);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
void print_proc(){
|
|
|
|
int index = 0;
|
2024-04-15 00:24:36 +08:00
|
|
|
print_proc_t(&index,current,current->next,1);
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("====---------------[Processes]----------------====\n");
|
|
|
|
printf("Name Pid Status [All Proc: %d]\n\n",index);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
|
2024-04-15 00:24:36 +08:00
|
|
|
static void found_task(int pid,struct task_struct *head,struct task_struct *base,struct task_struct **argv,int first){
|
2024-04-13 22:03:54 +08:00
|
|
|
struct task_struct *t = base;
|
|
|
|
if(t == NULL){
|
|
|
|
argv = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(t->pid == pid){
|
2024-04-15 00:24:36 +08:00
|
|
|
*argv = t;
|
2024-04-13 22:03:54 +08:00
|
|
|
return;
|
|
|
|
} else{
|
2024-04-13 22:22:25 +08:00
|
|
|
if(!first)
|
|
|
|
if(head->pid == t->pid){
|
|
|
|
argv = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
found_task(pid,head,t->next,argv,0);
|
2024-04-13 22:03:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 00:24:36 +08:00
|
|
|
struct task_struct* found_task_pid(int pid){
|
|
|
|
struct task_struct *argv = NULL;
|
|
|
|
found_task(pid,running_proc_head,running_proc_head,&argv,1);
|
|
|
|
if(argv == NULL){
|
|
|
|
printf("Cannot found task Pid:[%d].\n",pid);
|
2024-04-20 02:43:22 +08:00
|
|
|
return NULL;
|
2024-04-15 00:24:36 +08:00
|
|
|
}
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_task(struct task_struct *task){
|
|
|
|
task->state = TASK_SLEEPING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_task(struct task_struct *task){
|
|
|
|
task->state = TASK_RUNNABLE;
|
|
|
|
}
|
|
|
|
|
2024-04-13 22:03:54 +08:00
|
|
|
void task_kill(int pid){
|
2024-04-15 00:24:36 +08:00
|
|
|
struct task_struct *argv = found_task_pid(pid);
|
2024-04-13 22:03:54 +08:00
|
|
|
if(argv == NULL){
|
|
|
|
printf("Cannot found task Pid:[%d].\n",pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(argv->pid == 0){
|
|
|
|
printf("[\033kernel\036]: Taskkill cannot terminate kernel processes.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
argv->state = TASK_DEATH;
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("Taskkill process PID:%d Name:%s\n", current->pid, current->name);
|
2024-04-13 22:03:54 +08:00
|
|
|
printf("Task [%s] exit code: -130.\n",argv->name);
|
2024-04-14 17:14:26 +08:00
|
|
|
io_sti();
|
|
|
|
kfree(argv);
|
2024-04-13 22:03:54 +08:00
|
|
|
struct task_struct *head = running_proc_head;
|
|
|
|
struct task_struct *last = NULL;
|
|
|
|
while (1){
|
|
|
|
if(head->pid == argv->pid){
|
|
|
|
last->next = argv->next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last = head;
|
|
|
|
head = head->next;
|
|
|
|
}
|
2024-04-14 17:14:26 +08:00
|
|
|
io_cli();
|
2024-04-13 22:03:54 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
void schedule() {
|
|
|
|
if (current) {
|
2024-04-15 00:24:36 +08:00
|
|
|
if(current->next->state == TASK_SLEEPING){
|
|
|
|
change_task_to(current->next->next);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-13 22:03:54 +08:00
|
|
|
change_task_to(current->next);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void change_task_to(struct task_struct *next) {
|
|
|
|
if (current != next) {
|
|
|
|
struct task_struct *prev = current;
|
|
|
|
current = next;
|
2024-04-13 22:03:54 +08:00
|
|
|
|
2024-06-09 17:53:48 +08:00
|
|
|
page_flush(current->pgd_dir);
|
|
|
|
set_kernel_stack(current->stack);
|
2024-04-09 23:46:56 +08:00
|
|
|
switch_to(&(prev->context), &(current->context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t kernel_thread(int (*fn)(void *), void *arg,char* name) {
|
|
|
|
struct task_struct *new_task = (struct task_struct *) kmalloc(STACK_SIZE);
|
|
|
|
assert(new_task != NULL, "kern_thread: kmalloc error");
|
|
|
|
|
|
|
|
// 将栈低端结构信息初始化为 0
|
|
|
|
memset(new_task,0, sizeof(struct task_struct));
|
|
|
|
|
|
|
|
new_task->state = TASK_RUNNABLE;
|
|
|
|
new_task->stack = current;
|
|
|
|
new_task->pid = now_pid++;
|
2024-06-09 17:53:48 +08:00
|
|
|
new_task->pgd_dir = clone_directory(kernel_directory) ;
|
2024-04-10 22:15:48 +08:00
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
new_task->name = name;
|
|
|
|
|
|
|
|
uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE);
|
|
|
|
|
|
|
|
*(--stack_top) = (uint32_t) arg;
|
|
|
|
*(--stack_top) = (uint32_t) kthread_exit;
|
|
|
|
*(--stack_top) = (uint32_t) fn;
|
|
|
|
|
|
|
|
new_task->context.esp = (uint32_t) new_task + STACK_SIZE - sizeof(uint32_t) * 3;
|
|
|
|
|
|
|
|
// 设置新任务的标志寄存器未屏蔽中断,很重要
|
|
|
|
new_task->context.eflags = 0x200;
|
|
|
|
new_task->next = running_proc_head;
|
|
|
|
|
|
|
|
// 找到当前进任务队列,插入到末尾
|
|
|
|
struct task_struct *tail = running_proc_head;
|
|
|
|
assert(tail != NULL, "Must init sched!");
|
|
|
|
|
|
|
|
while (tail->next != running_proc_head) {
|
|
|
|
tail = tail->next;
|
|
|
|
}
|
|
|
|
tail->next = new_task;
|
|
|
|
|
|
|
|
return new_task->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kthread_exit() {
|
|
|
|
register uint32_t val asm ("eax");
|
2024-04-10 01:18:08 +08:00
|
|
|
printf("Task exited with value %d\n", val);
|
|
|
|
current->state = TASK_DEATH;
|
2024-04-09 23:46:56 +08:00
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
2024-04-14 17:14:26 +08:00
|
|
|
void kill_all_task(){
|
|
|
|
struct task_struct *head = running_proc_head;
|
|
|
|
while (1){
|
|
|
|
head = head->next;
|
|
|
|
if(head == NULL || head->pid == running_proc_head->pid){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(head->pid == current->pid) continue;
|
|
|
|
task_kill(head->pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
void init_sched() {
|
|
|
|
// 为当前执行流创建信息结构体 该结构位于当前执行流的栈最低端
|
|
|
|
current = (struct task_struct *) kmalloc(sizeof(struct task_struct));
|
|
|
|
|
|
|
|
current->state = TASK_RUNNABLE;
|
|
|
|
current->pid = now_pid++;
|
|
|
|
current->stack = current; // 该成员指向栈低地址
|
2024-04-10 22:15:48 +08:00
|
|
|
current->pgd_dir = kernel_directory;
|
2024-04-09 23:46:56 +08:00
|
|
|
current->name = "CPOS-System";
|
|
|
|
|
|
|
|
current->next = current;
|
|
|
|
|
|
|
|
running_proc_head = current;
|
|
|
|
}
|