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-07-14 00:54:27 +08:00
|
|
|
#include "../include/vfs.h"
|
|
|
|
#include "../include/timer.h"
|
|
|
|
#include "../include/shell.h"
|
|
|
|
#include "../include/heap.h"
|
|
|
|
#include "../include/elf.h"
|
|
|
|
|
|
|
|
#define SA_RPL_MASK 0xFFFC
|
|
|
|
#define SA_TI_MASK 0xFFFB
|
|
|
|
#define GET_SEL(cs, rpl) ((cs & SA_RPL_MASK & SA_TI_MASK) | (rpl))
|
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);
|
|
|
|
|
2024-07-30 22:50:03 +08:00
|
|
|
extern void taskX_switch(struct context *prev, struct context *next);
|
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
int now_pid = 0;
|
2024-07-14 00:54:27 +08:00
|
|
|
int can_sche = 1;
|
2024-04-09 23:46:56 +08:00
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
struct task_struct *get_current() {
|
2024-04-15 00:24:36 +08:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2024-08-19 17:27:13 +08:00
|
|
|
static uint32_t padding_up(uint32_t num, uint32_t size) {
|
|
|
|
return (num + size - 1) / size;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
void print_proc_t(int *i, struct task_struct *base, struct task_struct *cur, int is_print) {
|
|
|
|
if (cur->pid == base->pid) {
|
|
|
|
if (is_print) {
|
2024-04-15 00:24:36 +08:00
|
|
|
switch (cur->state) {
|
|
|
|
case TASK_RUNNABLE:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Running");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_SLEEPING:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Sleeping");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_UNINIT:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Init");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_ZOMBIE:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Zombie");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_DEATH:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\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-07-14 00:54:27 +08:00
|
|
|
} else {
|
|
|
|
if (is_print) {
|
2024-04-15 00:24:36 +08:00
|
|
|
switch (cur->state) {
|
|
|
|
case TASK_RUNNABLE:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Running");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_SLEEPING:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Sleeping");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_UNINIT:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Init");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_ZOMBIE:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\n", cur->name, cur->pid, "Zombie");
|
2024-04-15 00:24:36 +08:00
|
|
|
break;
|
|
|
|
case TASK_DEATH:
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("%-17s %-2d %s %d\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-07-14 00:54:27 +08:00
|
|
|
print_proc_t(i, base, cur->next, is_print);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
int get_procs() {
|
2024-05-21 22:40:15 +08:00
|
|
|
int index = 0;
|
2024-07-14 00:54:27 +08:00
|
|
|
print_proc_t(&index, current, current->next, 0);
|
2024-05-21 22:40:15 +08:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
void print_proc() {
|
2024-04-09 23:46:56 +08:00
|
|
|
int index = 0;
|
2024-07-14 00:54:27 +08:00
|
|
|
print_proc_t(&index, current, current->next, 1);
|
2024-05-19 18:05:00 +08:00
|
|
|
printf("====---------------[Processes]----------------====\n");
|
2024-07-14 00:54:27 +08:00
|
|
|
printf("Name Pid Status MemUsage [All Proc: %d]\n\n", index);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +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;
|
2024-07-14 00:54:27 +08:00
|
|
|
if (t == NULL) {
|
2024-04-13 22:03:54 +08:00
|
|
|
argv = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2024-07-14 00:54:27 +08:00
|
|
|
if (t->pid == pid) {
|
2024-04-15 00:24:36 +08:00
|
|
|
*argv = t;
|
2024-04-13 22:03:54 +08:00
|
|
|
return;
|
2024-07-14 00:54:27 +08:00
|
|
|
} else {
|
|
|
|
if (!first)
|
|
|
|
if (head->pid == t->pid) {
|
2024-04-13 22:22:25 +08:00
|
|
|
argv = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2024-07-14 00:54:27 +08:00
|
|
|
found_task(pid, head, t->next, argv, 0);
|
2024-04-13 22:03:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
struct task_struct *found_task_pid(int pid) {
|
2024-04-15 00:24:36 +08:00
|
|
|
struct task_struct *argv = NULL;
|
2024-07-14 00:54:27 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
void wait_task(struct task_struct *task) {
|
2024-04-15 00:24:36 +08:00
|
|
|
task->state = TASK_SLEEPING;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
void start_task(struct task_struct *task) {
|
2024-04-15 00:24:36 +08:00
|
|
|
task->state = TASK_RUNNABLE;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
void task_kill(int pid) {
|
|
|
|
io_cli();
|
2024-04-15 00:24:36 +08:00
|
|
|
struct task_struct *argv = found_task_pid(pid);
|
2024-07-14 00:54:27 +08:00
|
|
|
if (argv == NULL) {
|
|
|
|
printf("Cannot found task Pid:[%d].\n", pid);
|
2024-04-13 22:03:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-07-14 00:54:27 +08:00
|
|
|
if (argv->pid == 0) {
|
|
|
|
printf("\033ff3030;[kernel]: Taskkill cannot terminate kernel processes.\033c6c6c6;\n");
|
2024-04-13 22:03:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
argv->state = TASK_DEATH;
|
2024-08-14 21:58:29 +08:00
|
|
|
printf("Taskkill process PID:%d Name:%s\n", argv->pid, argv->name);
|
2024-09-01 11:56:39 +08:00
|
|
|
free_tty(argv);
|
2024-04-13 22:03:54 +08:00
|
|
|
struct task_struct *head = running_proc_head;
|
|
|
|
struct task_struct *last = NULL;
|
2024-07-14 00:54:27 +08:00
|
|
|
while (1) {
|
|
|
|
if (head->pid == argv->pid) {
|
2024-04-13 22:03:54 +08:00
|
|
|
last->next = argv->next;
|
2024-08-31 23:46:21 +08:00
|
|
|
kfree(argv);
|
2024-07-14 00:54:27 +08:00
|
|
|
io_sti();
|
2024-04-13 22:03:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
last = head;
|
|
|
|
head = head->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 22:50:03 +08:00
|
|
|
void schedule(registers_t *reg) {
|
|
|
|
io_cli();
|
2024-07-14 00:54:27 +08:00
|
|
|
if (current && can_sche) {
|
2024-07-30 22:50:03 +08:00
|
|
|
change_task_to(reg,current->next);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 22:50:03 +08:00
|
|
|
void change_task_to(registers_t *reg,struct task_struct *next) {
|
2024-04-09 23:46:56 +08:00
|
|
|
if (current != next) {
|
|
|
|
struct task_struct *prev = current;
|
|
|
|
current = next;
|
2024-06-28 22:43:56 +08:00
|
|
|
page_switch(current->pgd_dir);
|
2024-07-30 22:50:03 +08:00
|
|
|
set_kernel_stack(current->stack + STACK_SIZE);
|
|
|
|
|
|
|
|
prev->context.eip = reg->eip;
|
|
|
|
prev->context.ds = reg->ds;
|
|
|
|
prev->context.cs = reg->cs;
|
|
|
|
prev->context.eax = reg->eax;
|
2024-08-14 21:58:29 +08:00
|
|
|
prev->context.ss = reg->ss;
|
2024-04-09 23:46:56 +08:00
|
|
|
switch_to(&(prev->context), &(current->context));
|
2024-07-30 22:50:03 +08:00
|
|
|
reg->ds = current->context.ds;
|
|
|
|
reg->cs = current->context.cs;
|
|
|
|
reg->eip = current->context.eip;
|
|
|
|
reg->eax = current->context.eax;
|
2024-08-14 21:58:29 +08:00
|
|
|
reg->ss = current->context.ss;
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-14 00:54:27 +08:00
|
|
|
|
2024-08-14 21:58:29 +08:00
|
|
|
int32_t user_process(char *path, char *name){ // 用户进程创建
|
2024-07-14 00:54:27 +08:00
|
|
|
can_sche = 0;
|
|
|
|
if(path == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t size = vfs_filesize(path);
|
|
|
|
if(size == -1){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
io_cli();
|
|
|
|
|
|
|
|
struct task_struct *new_task = (struct task_struct *) kmalloc(STACK_SIZE);
|
|
|
|
assert(new_task != NULL, "user_pcb: 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++;
|
|
|
|
|
|
|
|
page_directory_t *page = clone_directory(kernel_directory);
|
|
|
|
new_task->pgd_dir = page;
|
|
|
|
new_task->mem_size = 0;
|
2024-09-01 20:09:41 +08:00
|
|
|
new_task->program_break = USER_START + 0xf0000;
|
2024-07-14 00:54:27 +08:00
|
|
|
new_task->program_break_end = USER_HEAP_END;
|
|
|
|
new_task->name = name;
|
|
|
|
new_task->isUser = 1;
|
2024-08-31 23:46:21 +08:00
|
|
|
new_task->tty = kmalloc(sizeof(tty_t));
|
2024-09-01 11:56:39 +08:00
|
|
|
init_default_tty(new_task);
|
2024-07-14 00:54:27 +08:00
|
|
|
|
2024-08-31 18:25:48 +08:00
|
|
|
extern char root_disk;
|
|
|
|
vfs_change_disk(new_task,root_disk);
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
io_sti();
|
|
|
|
|
|
|
|
page_switch(page);
|
|
|
|
|
2024-08-19 17:27:13 +08:00
|
|
|
for (int i = USER_START; i < USER_END + 0x1000;i++) { //用户堆以及用户栈映射
|
2024-07-14 00:54:27 +08:00
|
|
|
page_t *pg = get_page(i,1,page, false);
|
|
|
|
alloc_frame(pg,0,1);
|
|
|
|
}
|
|
|
|
|
2024-08-31 01:23:06 +08:00
|
|
|
for (int i = USER_EXEC_FILE_START; i < USER_EXEC_FILE_START + size; i++) {
|
|
|
|
page_t *pg = get_page(i,1,page, false);
|
|
|
|
alloc_frame(pg,0,1);
|
|
|
|
}
|
|
|
|
|
2024-08-31 23:46:21 +08:00
|
|
|
char* buffer = USER_EXEC_FILE_START;
|
2024-07-14 00:54:27 +08:00
|
|
|
|
|
|
|
memset(buffer,0,size);
|
2024-08-19 17:27:13 +08:00
|
|
|
vfs_readfile(path,buffer);
|
2024-07-14 00:54:27 +08:00
|
|
|
|
2024-08-30 16:12:44 +08:00
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
Elf32_Ehdr *ehdr = buffer;
|
|
|
|
if(!elf32Validate(ehdr)){
|
|
|
|
printf("Unknown exec file format.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-08-29 22:00:28 +08:00
|
|
|
uint32_t main = ehdr->e_entry;
|
|
|
|
load_elf(ehdr,page);
|
2024-07-14 00:54:27 +08:00
|
|
|
|
2024-08-19 17:27:13 +08:00
|
|
|
|
2024-08-14 21:58:29 +08:00
|
|
|
uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE);
|
2024-07-14 00:54:27 +08:00
|
|
|
|
|
|
|
*(--stack_top) = (uint32_t) main;
|
2024-08-30 16:12:44 +08:00
|
|
|
//*(--stack_top) = (uint32_t) buffer;
|
2024-07-14 00:54:27 +08:00
|
|
|
*(--stack_top) = (uint32_t) kthread_exit;
|
|
|
|
*(--stack_top) = (uint32_t) switch_to_user_mode;
|
|
|
|
|
|
|
|
new_task->context.esp = (uint32_t) new_task + STACK_SIZE - sizeof(uint32_t) * 3;
|
|
|
|
|
|
|
|
// 设置新任务的标志寄存器未屏蔽中断,很重要
|
2024-07-30 22:50:03 +08:00
|
|
|
new_task->context.eflags = (0 << 12 | 0b10 | 1 << 9);
|
2024-07-14 00:54:27 +08:00
|
|
|
new_task->next = running_proc_head;
|
|
|
|
|
|
|
|
page_switch(kernel_directory);
|
|
|
|
|
|
|
|
// 找到当前进任务队列,插入到末尾
|
|
|
|
struct task_struct *tailt = running_proc_head;
|
|
|
|
assert(tailt != NULL, "Must init sched!");
|
|
|
|
|
|
|
|
while (tailt->next != running_proc_head) {
|
|
|
|
tailt = tailt->next;
|
|
|
|
}
|
|
|
|
tailt->next = new_task;
|
|
|
|
can_sche = 1;
|
|
|
|
io_sti();
|
|
|
|
return new_task->pid;
|
|
|
|
}
|
2024-04-09 23:46:56 +08:00
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
|
2024-08-14 21:58:29 +08:00
|
|
|
int32_t kernel_thread(int (*fn)(void *), void *arg, char *name) { // 内核进程(线程) 创建
|
2024-07-14 00:54:27 +08:00
|
|
|
io_cli();
|
2024-04-09 23:46:56 +08:00
|
|
|
struct task_struct *new_task = (struct task_struct *) kmalloc(STACK_SIZE);
|
|
|
|
assert(new_task != NULL, "kern_thread: kmalloc error");
|
|
|
|
|
|
|
|
// 将栈低端结构信息初始化为 0
|
2024-07-14 00:54:27 +08:00
|
|
|
memset(new_task, 0, sizeof(struct task_struct));
|
2024-04-09 23:46:56 +08:00
|
|
|
|
|
|
|
new_task->state = TASK_RUNNABLE;
|
|
|
|
new_task->stack = current;
|
|
|
|
new_task->pid = now_pid++;
|
2024-07-14 00:54:27 +08:00
|
|
|
new_task->pgd_dir = kernel_directory;
|
|
|
|
new_task->mem_size = 0;
|
|
|
|
new_task->isUser = 0;
|
|
|
|
|
|
|
|
extern header_t *head;
|
|
|
|
extern header_t *tail;
|
|
|
|
extern void *program_break;
|
|
|
|
extern void *program_break_end;
|
|
|
|
current->head = head;
|
|
|
|
current->tail = tail;
|
|
|
|
current->program_break = program_break;
|
|
|
|
current->program_break_end = program_break_end;
|
2024-04-09 23:46:56 +08:00
|
|
|
new_task->name = name;
|
|
|
|
|
2024-09-01 11:56:39 +08:00
|
|
|
new_task->tty = kmalloc(sizeof(tty_t));
|
|
|
|
init_default_tty(new_task);
|
|
|
|
|
2024-04-09 23:46:56 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
// 找到当前进任务队列,插入到末尾
|
2024-07-14 00:54:27 +08:00
|
|
|
struct task_struct *tailt = running_proc_head;
|
|
|
|
assert(tailt != NULL, "Must init sched!");
|
2024-04-09 23:46:56 +08:00
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
while (tailt->next != running_proc_head) {
|
|
|
|
tailt = tailt->next;
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|
2024-07-14 00:54:27 +08:00
|
|
|
tailt->next = new_task;
|
|
|
|
io_sti();
|
2024-04-09 23:46:56 +08:00
|
|
|
return new_task->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kthread_exit() {
|
|
|
|
register uint32_t val asm ("eax");
|
2024-07-30 22:50:03 +08:00
|
|
|
printf("Task [PID: %d] exited with value %d\n", current->pid,val);
|
|
|
|
task_kill(current->pid);
|
2024-04-09 23:46:56 +08:00
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
void kill_all_task() {
|
2024-04-14 17:14:26 +08:00
|
|
|
struct task_struct *head = running_proc_head;
|
2024-07-14 00:54:27 +08:00
|
|
|
while (1) {
|
2024-04-14 17:14:26 +08:00
|
|
|
head = head->next;
|
2024-07-14 00:54:27 +08:00
|
|
|
if (head == NULL || head->pid == running_proc_head->pid) {
|
2024-04-14 17:14:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-07-14 00:54:27 +08:00
|
|
|
if (head->pid == current->pid) continue;
|
2024-04-14 17:14:26 +08:00
|
|
|
task_kill(head->pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:54:27 +08:00
|
|
|
#define SA_RPL3 3
|
|
|
|
|
|
|
|
void switch_to_user_mode(uint32_t func) {
|
|
|
|
io_cli();
|
|
|
|
set_kernel_stack(current->stack + STACK_SIZE);
|
|
|
|
unsigned esp = USER_END;
|
|
|
|
current->context.eflags = (0 << 12 | 0b10 | 1 << 9);
|
|
|
|
intr_frame_t iframe;
|
|
|
|
iframe.edi = 1;
|
|
|
|
iframe.esi = 2;
|
|
|
|
iframe.ebp = 3;
|
|
|
|
iframe.esp_dummy = 4;
|
|
|
|
iframe.ebx = 5;
|
|
|
|
iframe.edx = 6;
|
|
|
|
iframe.ecx = 7;
|
|
|
|
iframe.eax = 8;
|
|
|
|
|
|
|
|
iframe.gs = GET_SEL(4 * 8, SA_RPL3);
|
|
|
|
iframe.ds = GET_SEL(4 * 8, SA_RPL3);
|
|
|
|
iframe.es = GET_SEL(4 * 8, SA_RPL3);
|
|
|
|
iframe.fs = GET_SEL(4 * 8, SA_RPL3);
|
|
|
|
|
|
|
|
iframe.ss = GET_SEL(4 * 8, SA_RPL3);
|
|
|
|
iframe.cs = GET_SEL(3 * 8, SA_RPL3);
|
2024-07-30 22:50:03 +08:00
|
|
|
//set_tss_ss0(iframe.ss);
|
2024-07-14 00:54:27 +08:00
|
|
|
|
|
|
|
iframe.eip = func; //用户可执行程序入口
|
|
|
|
iframe.eflags = (0 << 12 | 0b10 | 1 << 9);
|
|
|
|
iframe.esp = esp; // 设置用户态堆栈
|
|
|
|
|
|
|
|
intr_frame_t *a = &iframe;
|
|
|
|
asm volatile("movl %0, %%esp\n"
|
|
|
|
"popa\n"
|
|
|
|
"pop %%gs\n"
|
|
|
|
"pop %%fs\n"
|
|
|
|
"pop %%es\n"
|
|
|
|
"pop %%ds\n"
|
|
|
|
"iret" ::"m"(a));
|
|
|
|
}
|
|
|
|
|
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";
|
2024-07-14 00:54:27 +08:00
|
|
|
current->mem_size = 0;
|
2024-04-09 23:46:56 +08:00
|
|
|
current->next = current;
|
2024-07-14 00:54:27 +08:00
|
|
|
current->isUser = 0;
|
2024-09-02 16:02:08 +08:00
|
|
|
init_default_tty(current);
|
2024-07-14 00:54:27 +08:00
|
|
|
|
|
|
|
extern header_t *head;
|
|
|
|
extern header_t *tail;
|
|
|
|
extern void *program_break;
|
|
|
|
extern void *program_break_end;
|
|
|
|
current->head = head;
|
|
|
|
current->tail = tail;
|
|
|
|
current->program_break = program_break;
|
|
|
|
current->program_break_end = program_break_end;
|
2024-04-09 23:46:56 +08:00
|
|
|
|
|
|
|
running_proc_head = current;
|
2024-07-14 00:54:27 +08:00
|
|
|
klogf(true,"Load task schedule. | KernelTaskName: %s PID: %d\n",current->name,current->pid);
|
2024-04-09 23:46:56 +08:00
|
|
|
}
|