🐛 修复了无法切换进程、无法进行浮点运算的bug(将main.c中的init函数名进行修改)

This commit is contained in:
fslongjin
2022-02-12 22:14:51 +08:00
parent 26c23e0e65
commit d1671bc121
19 changed files with 501 additions and 391 deletions

View File

@ -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(&regs, 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(&regs, flags, 0, 0);
return do_fork(&regs, 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(&current_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;
}
}

View File

@ -16,7 +16,7 @@
#include "ptrace.h"
extern unsigned long _stack_start; // 导出内核层栈基地址定义在head.S
extern void ret_from_intr(); // 导出从中断返回的函数定义在entry.S
extern void ret_from_intr(); // 导出从中断返回的函数定义在entry.S
// 进程的内核栈大小 32K
#define STACK_SIZE 32768
@ -53,35 +53,35 @@ extern void ret_from_intr(); // 导出从中断返回的函数(定义在
*/
struct mm_struct
{
pml4t_t *pgd; // 内存页表指针
// 代码段空间
ul code_addr_start, code_addr_end;
// 数据段空间
ul data_addr_start, data_addr_end;
// 只读数据段空间
ul rodata_addr_start, rodata_addr_end;
// 动态内存分配区(堆区域)
ul brk_start, brk_end;
// 应用层栈基地址
ul stack_start;
pml4t_t *pgd; // 内存页表指针
// 代码段空间
ul code_addr_start, code_addr_end;
// 数据段空间
ul data_addr_start, data_addr_end;
// 只读数据段空间
ul rodata_addr_start, rodata_addr_end;
// 动态内存分配区(堆区域)
ul brk_start, brk_end;
// 应用层栈基地址
ul stack_start;
};
struct thread_struct
{
// 内核层栈基指针
ul rbp; // in tss rsp0
// 内核层代码指针
ul rip;
// 内核层栈指针
ul rsp;
// 内核层栈基指针
ul rbp; // in tss rsp0
// 内核层代码指针
ul rip;
// 内核层栈指针
ul rsp;
ul fs, gs;
ul fs, gs;
ul cr2;
// 异常号
ul trap_num;
// 错误码
ul err_code;
ul cr2;
// 异常号
ul trap_num;
// 错误码
ul err_code;
};
// 进程标志位
@ -93,43 +93,43 @@ struct thread_struct
*/
struct process_control_block
{
// 连接各个pcb的双向链表
struct List list;
// 连接各个pcb的双向链表
struct List list;
// 进程的状态
volatile long state;
// 进程标志:进程、线程、内核线程
unsigned long flags;
// 进程的状态
volatile long state;
// 进程标志:进程、线程、内核线程
unsigned long flags;
// 内存空间分布结构体, 记录内存页表和程序段信息
struct mm_struct *mm;
// 内存空间分布结构体, 记录内存页表和程序段信息
struct mm_struct *mm;
// 进程切换时保存的状态信息
struct thread_struct *thread;
// 进程切换时保存的状态信息
struct thread_struct *thread;
// 地址空间范围
// 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
// 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
ul addr_limit;
// 地址空间范围
// 用户空间: 0x0000 0000 0000 0000 ~ 0x0000 7fff ffff ffff
// 内核空间: 0xffff 8000 0000 0000 ~ 0xffff ffff ffff ffff
ul addr_limit;
// 进程id
long pid;
// 进程id
long pid;
// 可用时间片
long counter;
// 可用时间片
long counter;
// 信号
long signal;
// 信号
long signal;
// 优先级
long priority;
// 优先级
long priority;
};
// 将进程的pcb和内核栈融合到一起,8字节对齐
union proc_union
{
struct process_control_block pcb;
ul stack[STACK_SIZE / sizeof(ul)];
struct process_control_block pcb;
ul stack[STACK_SIZE / sizeof(ul)];
} __attribute__((aligned(8)));
struct mm_struct initial_mm;
@ -137,33 +137,33 @@ struct thread_struct initial_thread;
// 设置初始进程的PCB
#define INITIAL_PROC(proc) \
{ \
.state = PROC_UNINTERRUPTIBLE, \
.flags = PF_KTHREAD, \
.mm = &initial_mm, \
.thread = &initial_thread, \
.addr_limit = 0xffff800000000000, \
.pid = 0, \
.counter = 1, \
.signal = 0, \
.priority = 0 \
}
{ \
.state = PROC_UNINTERRUPTIBLE, \
.flags = PF_KTHREAD, \
.mm = &initial_mm, \
.thread = &initial_thread, \
.addr_limit = 0xffff800000000000, \
.pid = 0, \
.counter = 1, \
.signal = 0, \
.priority = 0 \
}
// 初始化 初始进程的union ,并将其链接到.data.init_proc段内
union proc_union initial_proc_union __attribute__((__section__(".data.init_proc"))) = {INITIAL_PROC(initial_proc_union.pcb)};
union proc_union initial_proc_union __attribute__((__section__(".data.init_proc_union"))) = {INITIAL_PROC(initial_proc_union.pcb)};
struct process_control_block *initial_proc[CPU_NUM] = {&initial_proc_union.pcb, 0};
struct mm_struct initial_mm = {0};
struct thread_struct initial_thread =
{
.rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
.rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
.fs = KERNEL_DS,
.gs = KERNEL_DS,
.cr2 = 0,
.trap_num = 0,
.err_code = 0};
{
.rbp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
.rsp = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)),
.fs = KERNEL_DS,
.gs = KERNEL_DS,
.cr2 = 0,
.trap_num = 0,
.err_code = 0};
/**
* @brief 任务状态段结构体
@ -171,86 +171,89 @@ struct thread_struct initial_thread =
*/
struct tss_struct
{
unsigned int reserved0;
ul rsp0;
ul rsp1;
ul rsp2;
ul reserved1;
ul ist1;
ul ist2;
ul ist3;
ul ist4;
ul ist5;
ul ist6;
ul ist7;
ul reserved2;
unsigned short reserved3;
// io位图基地址
unsigned short io_map_base_addr;
unsigned int reserved0;
ul rsp0;
ul rsp1;
ul rsp2;
ul reserved1;
ul ist1;
ul ist2;
ul ist3;
ul ist4;
ul ist5;
ul ist6;
ul ist7;
ul reserved2;
unsigned short reserved3;
// io位图基地址
unsigned short io_map_base_addr;
} __attribute__((packed)); // 使用packed表明是紧凑结构编译器不会对成员变量进行字节对齐。
// 设置初始进程的tss
#define INITIAL_TSS \
{ \
.reserved0 = 0, \
.rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
.rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
.rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
.reserved1 = 0, \
.ist1 = 0xffff800000007c00, \
.ist2 = 0xffff800000007c00, \
.ist3 = 0xffff800000007c00, \
.ist4 = 0xffff800000007c00, \
.ist5 = 0xffff800000007c00, \
.ist6 = 0xffff800000007c00, \
.ist7 = 0xffff800000007c00, \
.reserved2 = 0, \
.reserved3 = 0, \
.io_map_base_addr = 0 \
}
{ \
.reserved0 = 0, \
.rsp0 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
.rsp1 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
.rsp2 = (ul)(initial_proc_union.stack + STACK_SIZE / sizeof(ul)), \
.reserved1 = 0, \
.ist1 = 0xffff800000007c00, \
.ist2 = 0xffff800000007c00, \
.ist3 = 0xffff800000007c00, \
.ist4 = 0xffff800000007c00, \
.ist5 = 0xffff800000007c00, \
.ist6 = 0xffff800000007c00, \
.ist7 = 0xffff800000007c00, \
.reserved2 = 0, \
.reserved3 = 0, \
.io_map_base_addr = 0 \
}
// 为每个核心初始化初始进程的tss
struct tss_struct initial_tss[CPU_NUM] = {[0 ... CPU_NUM - 1] = INITIAL_TSS};
// 获取当前的pcb
struct process_control_block *get_current_pcb()
{
struct process_control_block *current = NULL;
// 利用了当前pcb和栈空间总大小为32k大小对齐将rsp低15位清空即可获得pcb的起始地址
__asm__ __volatile__("andq %%rsp, %0 \n\t"
: "=r"(current)
: "0"(~32767UL));
return current;
}
struct process_control_block *current = NULL;
// 利用了当前pcb和栈空间总大小为32k大小对齐将rsp低15位清空即可获得pcb的起始地址
__asm__ __volatile__("andq %%rsp, %0 \n\t"
: "=r"(current)
: "0"(~32767UL));
return current;
};
#define current_pcb get_current_pcb()
#define GET_CURRENT_PCB \
"movq %rsp, %rbx \n\t" \
"andq $-32768, %rbx\n\t"
"movq %rsp, %rbx \n\t" \
"andq $-32768, %rbx\n\t"
/**
* @brief 切换进程上下文
* 先把rbp和rax保存到栈中然后将rsp和rip保存到prev的thread结构体中
* 然后调用__switch_to切换栈配置其他信息最后恢复下一个进程的rax rbp。
*/
#define switch_proc(prev, next) \
do \
{ \
__asm__ __volatile__("pushq %%rbp \n\t" \
"pushq %%rax \n\t" \
"movq %%rsp, %0 \n\t" \
"movq %2, %%rax \n\t" \
"leaq 1f(%%rip), %%rax \n\t" \
"movq %%rax, %1 \n\t" \
"pushq %3 \n\t" \
"jmp __switch_to \n\t" \
"1: \n\t" \
"popq %%rax \n\t" \
"popq %%rbp \n\t" \
: "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
: "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
: "memory"); \
} while (0)
#define switch_proc(prev, next) \
do \
{ \
\
__asm__ __volatile__("pushq %%rbp \n\t" \
"pushq %%rax \n\t" \
"movq %%rsp, %0 \n\t" \
"movq %2, %%rsp \n\t" \
"leaq 1f(%%rip), %%rax \n\t" \
"movq %%rax, %1 \n\t" \
"pushq %3 \n\t" \
"jmp __switch_to \n\t" \
"1: \n\t" \
"popq %%rax \n\t" \
"popq %%rbp \n\t" \
: "=m"(prev->thread->rsp), "=m"(prev->thread->rip) \
: "m"(next->thread->rsp), "m"(next->thread->rip), "D"(prev), "S"(next) \
: "memory"); \
} while (0)
/**
* @brief 初始化系统的第一个进程

View File

@ -1,30 +1,52 @@
#pragma once
#include "../common/glib.h"
// 进程执行现场的寄存器状态
/***************************************************
* 版权声明
*
* 本操作系统名为MINE
* 该操作系统未经授权不得以盈利或非盈利为目的进行开发,
* 只允许个人学习以及公开交流使用
*
* 代码最终所有权及解释权归田宇所有;
*
* 本模块作者: 田宇
* EMail: 345538255@qq.com
*
*
***************************************************/
#ifndef __PTRACE_H__
#define __PTRACE_H__
/*
*/
struct pt_regs
{
ul r15;
ul r14;
ul r13;
ul r12;
ul r11;
ul r10;
ul r9;
ul r8;
ul rbx;
ul rcx;
ul rdx;
ul rsi;
ul rdi;
ul rbp;
ul ds;
ul es;
ul rax;
ul func;
ul err_code;
ul rip;
ul cs;
ul rflags;
ul rsp;
ul ss;
};
unsigned long r15;
unsigned long r14;
unsigned long r13;
unsigned long r12;
unsigned long r11;
unsigned long r10;
unsigned long r9;
unsigned long r8;
unsigned long rbx;
unsigned long rcx;
unsigned long rdx;
unsigned long rsi;
unsigned long rdi;
unsigned long rbp;
unsigned long ds;
unsigned long es;
unsigned long rax;
unsigned long func;
unsigned long errcode;
unsigned long rip;
unsigned long cs;
unsigned long rflags;
unsigned long rsp;
unsigned long ss;
};
#endif