mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
🐛 修复了无法切换进程、无法进行浮点运算的bug(将main.c中的init函数名进行修改)
This commit is contained in:
@ -1,30 +1,9 @@
|
||||
#include "process.h"
|
||||
|
||||
|
||||
#include "../exception/gate.h"
|
||||
#include "../common/printk.h"
|
||||
#include "../common/kprint.h"
|
||||
|
||||
|
||||
void test_mm()
|
||||
{
|
||||
kinfo("Testing memory management unit...");
|
||||
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1));
|
||||
kinfo("Try to allocate 64 memory pages.");
|
||||
struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
|
||||
|
||||
for (int i = 0; i <= 65; ++i)
|
||||
{
|
||||
printk("page%d\tattr:%#018lx\tphys_addr:%#018lx\t", i, page->attr, page->addr_phys);
|
||||
++page;
|
||||
if (((i + 1) % 2) == 0)
|
||||
printk("\n");
|
||||
}
|
||||
|
||||
|
||||
printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 切换进程
|
||||
*
|
||||
@ -33,22 +12,21 @@ void test_mm()
|
||||
* 由于程序在进入内核的时候已经保存了寄存器,因此这里不需要保存寄存器。
|
||||
* 这里切换fs和gs寄存器
|
||||
*/
|
||||
|
||||
void __switch_to(struct process_control_block *prev, struct process_control_block *next)
|
||||
{
|
||||
initial_tss[0].rsp0 = next->thread->rbp;
|
||||
set_TSS64(initial_tss[0].rsp0, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1,
|
||||
initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7);
|
||||
|
||||
__asm__ __volatile__("movq %%fs, %0 \n\t"
|
||||
__asm__ __volatile__("movq %%fs, %0 \n\t"
|
||||
: "=a"(prev->thread->fs));
|
||||
__asm__ __volatile__("movq %%gs, %0 \n\t"
|
||||
__asm__ __volatile__("movq %%gs, %0 \n\t"
|
||||
: "=a"(prev->thread->gs));
|
||||
|
||||
__asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
|
||||
__asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
|
||||
__asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
|
||||
__asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
|
||||
|
||||
printk("prev->thread->rbp=%#018lx\n", prev->thread->rbp);
|
||||
printk("next->thread->rbp=%#018lx\n", next->thread->rbp);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,7 +37,7 @@ void __switch_to(struct process_control_block *prev, struct process_control_bloc
|
||||
*/
|
||||
ul init(ul arg)
|
||||
{
|
||||
printk("initial proc running...\targ:%#018lx\n", arg);
|
||||
kinfo("initial proc running...\targ:%#018lx", arg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -82,35 +60,37 @@ ul do_exit(ul code)
|
||||
* 执行到这里时,rsp位于栈顶,然后弹出寄存器值
|
||||
* 弹出之后还要向上移动7个unsigned long的大小,从而弹出额外的信息(详见pt_regs)
|
||||
*/
|
||||
extern void kernel_thread_func(void);
|
||||
|
||||
__asm__(
|
||||
"kernel_thread_func: \n\t"
|
||||
" popq %r15 \n\t"
|
||||
" popq %r14 \n\t"
|
||||
" popq %r13 \n\t"
|
||||
" popq %r12 \n\t"
|
||||
" popq %r11 \n\t"
|
||||
" popq %r10 \n\t"
|
||||
" popq %r9 \n\t"
|
||||
" popq %r8 \n\t"
|
||||
" popq %rbx \n\t" // 在kernel_thread中,将程序执行地址保存在了rbx
|
||||
" popq %rcx \n\t"
|
||||
" popq %rdx \n\t"
|
||||
" popq %rsi \n\t"
|
||||
" popq %rdi \n\t"
|
||||
" popq %rbp \n\t"
|
||||
" popq %rax \n\t"
|
||||
" movq %rax, %ds\n\t"
|
||||
" popq %rax \n\t"
|
||||
" movq %rax, %es\n\t"
|
||||
" popq %rax \n\t"
|
||||
" addq $0x38, %rsp \n\t"
|
||||
// ======================= //
|
||||
" movq %rdx, %rdi \n\t"
|
||||
" callq *%rbx \n\t"
|
||||
" movq %rax, %rdi \n\t"
|
||||
" callq do_exit \n\t");
|
||||
extern void kernel_thread_func(void);
|
||||
__asm__ (
|
||||
"kernel_thread_func: \n\t"
|
||||
" popq %r15 \n\t"
|
||||
" popq %r14 \n\t"
|
||||
" popq %r13 \n\t"
|
||||
" popq %r12 \n\t"
|
||||
" popq %r11 \n\t"
|
||||
" popq %r10 \n\t"
|
||||
" popq %r9 \n\t"
|
||||
" popq %r8 \n\t"
|
||||
" popq %rbx \n\t"
|
||||
" popq %rcx \n\t"
|
||||
" popq %rdx \n\t"
|
||||
" popq %rsi \n\t"
|
||||
" popq %rdi \n\t"
|
||||
" popq %rbp \n\t"
|
||||
" popq %rax \n\t"
|
||||
" movq %rax, %ds \n\t"
|
||||
" popq %rax \n\t"
|
||||
" movq %rax, %es \n\t"
|
||||
" popq %rax \n\t"
|
||||
" addq $0x38, %rsp \n\t"
|
||||
/////////////////////////////////
|
||||
" movq %rdx, %rdi \n\t"
|
||||
" callq *%rbx \n\t"
|
||||
" movq %rax, %rdi \n\t"
|
||||
" callq do_exit \n\t"
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* @brief 初始化内核进程
|
||||
@ -120,9 +100,9 @@ __asm__(
|
||||
* @param flags
|
||||
* @return int
|
||||
*/
|
||||
int kernel_thread(unsigned long (* fn)(unsigned long), unsigned long arg, unsigned long flags)
|
||||
|
||||
int kernel_thread(unsigned long (*fn)(unsigned long), unsigned long arg, unsigned long flags)
|
||||
{
|
||||
//struct Page *page = alloc_pages(ZONE_NORMAL, 2, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
|
||||
struct pt_regs regs;
|
||||
memset(®s, 0, sizeof(regs));
|
||||
|
||||
@ -136,18 +116,20 @@ int kernel_thread(unsigned long (* fn)(unsigned long), unsigned long arg, unsign
|
||||
regs.cs = KERNEL_CS;
|
||||
regs.ss = KERNEL_DS;
|
||||
|
||||
|
||||
// 置位中断使能标志位
|
||||
regs.rflags = (1 << 9);
|
||||
|
||||
// rip寄存器指向内核线程的引导程序
|
||||
regs.rip = (ul)kernel_thread_func;
|
||||
|
||||
return (int)do_fork(®s, flags, 0, 0);
|
||||
return do_fork(®s, flags, 0, 0);
|
||||
}
|
||||
|
||||
void process_init()
|
||||
{
|
||||
|
||||
|
||||
|
||||
initial_mm.pgd = (pml4t_t *)global_CR3;
|
||||
|
||||
initial_mm.code_addr_start = memory_management_struct.kernel_code_start;
|
||||
@ -164,23 +146,23 @@ void process_init()
|
||||
|
||||
initial_mm.stack_start = _stack_start;
|
||||
|
||||
|
||||
// 初始化进程和tss
|
||||
set_TSS64(initial_thread.rbp, initial_tss[0].rsp1, initial_tss[0].rsp2, initial_tss[0].ist1, initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5, initial_tss[0].ist6, initial_tss[0].ist7);
|
||||
|
||||
|
||||
|
||||
initial_tss[0].rsp0 = initial_thread.rbp;
|
||||
|
||||
|
||||
// 初始化进程的循环链表
|
||||
list_init(&initial_proc_union.pcb.list);
|
||||
|
||||
test_mm();
|
||||
|
||||
kernel_thread(init, 10, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); // 初始化内核进程
|
||||
initial_proc_union.pcb.state = PROC_RUNNING;
|
||||
|
||||
// 获取新的进程的pcb
|
||||
struct process_control_block *p = container_of(list_next(¤t_pcb->list), struct process_control_block, list);
|
||||
|
||||
// 切换到新的内核线程
|
||||
switch_proc(current_pcb, p);
|
||||
}
|
||||
|
||||
@ -193,34 +175,29 @@ void process_init()
|
||||
* @param stack_size 堆栈大小
|
||||
* @return unsigned long
|
||||
*/
|
||||
|
||||
unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size)
|
||||
{
|
||||
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1));
|
||||
struct process_control_block *tsk = NULL;
|
||||
|
||||
//printk("alloc_pages,bmp %#018lx\n", *(memory_management_struct.bmp));
|
||||
|
||||
// 获取一个物理页并在这个物理页内初始化pcb
|
||||
struct Page *p = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
|
||||
printk("22\n");
|
||||
|
||||
//kinfo("alloc_pages,bmp:%#018lx", *(memory_management_struct.bmp));
|
||||
tsk = (struct process_control_block *)((unsigned long)(p->addr_phys) + (0xffff800000000000UL));
|
||||
|
||||
//printk("phys_addr\t%#018lx\n",p->addr_phys);
|
||||
printk("virt_addr\t%#018lx\n",(unsigned long)(p->addr_phys) + (0xffff800000000000UL));
|
||||
//kinfo("pcb addr:%#018lx", (ul)tsk);
|
||||
// 获取一个物理页并在这个物理页内初始化pcb
|
||||
struct Page *pp = alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
|
||||
|
||||
|
||||
tsk = (struct process_control_block *)phys_2_virt(pp->addr_phys);
|
||||
|
||||
|
||||
memset(tsk, 0, sizeof(*tsk));
|
||||
printk("33\n");
|
||||
|
||||
// 将当前进程的pcb复制到新的pcb内
|
||||
*tsk = *current_pcb;
|
||||
|
||||
// 将进程加入循环链表
|
||||
list_init(&tsk->list);
|
||||
printk("44\n");
|
||||
list_append(&initial_proc_union.pcb.list, &tsk->list);
|
||||
printk("5\n");
|
||||
|
||||
list_add(&initial_proc_union.pcb.list, &tsk->list);
|
||||
|
||||
|
||||
++(tsk->pid);
|
||||
tsk->state = PROC_UNINTERRUPTIBLE;
|
||||
@ -231,6 +208,7 @@ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned
|
||||
|
||||
// 将寄存器信息存储到进程的内核栈空间的顶部
|
||||
memcpy((void *)((ul)tsk + STACK_SIZE - sizeof(struct pt_regs)), regs, sizeof(struct pt_regs));
|
||||
|
||||
// 设置进程的内核栈
|
||||
thd->rbp = (ul)tsk + STACK_SIZE;
|
||||
thd->rip = regs->rip;
|
||||
@ -241,6 +219,6 @@ unsigned long do_fork(struct pt_regs *regs, unsigned long clone_flags, unsigned
|
||||
thd->rip = regs->rip = (ul)ret_from_intr;
|
||||
|
||||
tsk->state = PROC_RUNNING;
|
||||
printk("1111\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user