🐛 修复了无法切换进程、无法进行浮点运算的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

@ -160,7 +160,7 @@ ENTRY(undefined_opcode)
jmp Err_Code
// 7 #NM FPU
ENTRY(dev_not_available)
ENTRY(dev_not_avaliable)
pushq $0
pushq %rax
leaq do_dev_not_avaliable(%rip), %rax //

View File

@ -32,6 +32,7 @@ extern unsigned int TSS64_Table[26];
* @param ist 中断栈表号
* @param code_addr 指向中断服务程序的指针的地址
*/
void set_gate(ul *gate_selector_addr, ul attr, unsigned char ist, ul *code_addr)
{
ul __d0=0, __d1=0;
@ -56,6 +57,10 @@ void set_gate(ul *gate_selector_addr, ul attr, unsigned char ist, ul *code_addr)
*(gate_selector_addr + 1) = __d1;
}
/**
* @brief 加载任务状态段寄存器
* @param n TSS基地址在GDT中的第几项

View File

@ -124,7 +124,7 @@ void init_irq()
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(ul rsp, ul number)
void do_IRQ(struct pt_regs *regs, ul number)
{
unsigned char x;
switch (number)

View File

@ -13,6 +13,7 @@
#include "../common/glib.h"
#include "../process/ptrace.h"
/**
* @brief 初始化中断模块
@ -26,4 +27,4 @@ void init_irq();
* @param rsp 中断栈指针
* @param number 中断号
*/
void do_IRQ(ul rsp, ul number);
void do_IRQ(struct pt_regs* rsp, ul number);

View File

@ -1,5 +1,6 @@
#include "trap.h"
#include "gate.h"
#include "../process/ptrace.h"
void init_sys_vector()
{
@ -10,7 +11,7 @@ void init_sys_vector()
set_system_trap_gate(4, 1, overflow);
set_system_trap_gate(5, 1, bounds);
set_trap_gate(6, 1, undefined_opcode);
set_trap_gate(7, 1, dev_not_available);
set_trap_gate(7, 1, dev_not_avaliable);
set_trap_gate(8, 1, double_fault);
set_trap_gate(9, 1, coprocessor_segment_overrun);
set_trap_gate(10, 1, invalid_TSS);
@ -30,132 +31,132 @@ void init_sys_vector()
}
// 0 #DE 除法错误
void do_divide_error(unsigned long rsp, unsigned long error_code)
void do_divide_error(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 1 #DB 调试异常
void do_debug(unsigned long rsp, unsigned long error_code)
void do_debug(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR / TRAP");
printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 2 不可屏蔽中断
void do_nmi(unsigned long rsp, unsigned long error_code)
void do_nmi(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(BLUE, BLACK, "INT");
printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 3 #BP 断点异常
void do_int3(unsigned long rsp, unsigned long error_code)
void do_int3(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(YELLOW, BLACK, "TRAP");
printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 4 #OF 溢出异常
void do_overflow(unsigned long rsp, unsigned long error_code)
void do_overflow(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(YELLOW, BLACK, "TRAP");
printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 5 #BR 越界异常
void do_bounds(unsigned long rsp, unsigned long error_code)
void do_bounds(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 6 #UD 无效/未定义的机器码
void do_undefined_opcode(unsigned long rsp, unsigned long error_code)
void do_undefined_opcode(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 7 #NM 设备异常FPU不存在
void do_dev_not_avaliable(unsigned long rsp, unsigned long error_code)
void do_dev_not_avaliable(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 8 #DF 双重错误
void do_double_fault(unsigned long rsp, unsigned long error_code)
void do_double_fault(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "Terminate");
printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 9 协处理器越界(保留)
void do_coprocessor_segment_overrun(unsigned long rsp, unsigned long error_code)
void do_coprocessor_segment_overrun(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 10 #TS 无效的TSS段
void do_invalid_TSS(unsigned long rsp, unsigned long error_code)
void do_invalid_TSS(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[");
printk_color(RED, BLACK, "ERROR");
printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
printk_color(YELLOW, BLACK, "Information:\n");
// 解析错误码
@ -181,43 +182,43 @@ void do_invalid_TSS(unsigned long rsp, unsigned long error_code)
}
// 11 #NP 段不存在
void do_segment_not_exists(unsigned long rsp, unsigned long error_code)
void do_segment_not_exists(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 12 #SS SS段错误
void do_stack_segment_fault(unsigned long rsp, unsigned long error_code)
void do_stack_segment_fault(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 13 #GP 通用保护性异常
void do_general_protection(unsigned long rsp, unsigned long error_code)
void do_general_protection(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
return;
while (1)
;
}
// 14 #PF 页故障
void do_page_fault(unsigned long rsp, unsigned long error_code)
void do_page_fault(struct pt_regs * regs, unsigned long error_code)
{
unsigned long cr2 = 0;
// 先保存cr2寄存器的值避免由于再次触发页故障而丢失值
@ -225,10 +226,10 @@ void do_page_fault(unsigned long rsp, unsigned long error_code)
__asm__ __volatile__("movq %%cr2, %0"
: "=r"(cr2)::"memory");
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_page_fault(14),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\tCR2:%#18lx\n", error_code, rsp, *rip, cr2);
printk(" ] do_page_fault(14),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\tCR2:%#18lx\n", error_code, regs->rsp, regs->rip, cr2);
printk_color(YELLOW, BLACK, "Information:\n");
if (!(error_code & 0x01))
@ -257,60 +258,60 @@ void do_page_fault(unsigned long rsp, unsigned long error_code)
// 15 Intel保留请勿使用
// 16 #MF x87FPU错误
void do_x87_FPU_error(unsigned long rsp, unsigned long error_code)
void do_x87_FPU_error(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 17 #AC 对齐检测
void do_alignment_check(unsigned long rsp, unsigned long error_code)
void do_alignment_check(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 18 #MC 机器检测
void do_machine_check(unsigned long rsp, unsigned long error_code)
void do_machine_check(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 19 #XM SIMD浮点异常
void do_SIMD_exception(unsigned long rsp, unsigned long error_code)
void do_SIMD_exception(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;
}
// 20 #VE 虚拟化异常
void do_virtualization_exception(unsigned long rsp, unsigned long error_code)
void do_virtualization_exception(struct pt_regs * regs, unsigned long error_code)
{
unsigned long *rip = (unsigned long *)(rsp + 0x98);
printk("[ ");
printk_color(RED, BLACK, "ERROR");
printk(" ] do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, rsp, *rip);
printk(" ] do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
while (1)
;

View File

@ -33,7 +33,7 @@ void bounds();
// 未定义的操作数
void undefined_opcode();
// 设备不可用
void dev_not_available();
void dev_not_avaliable();
void double_fault();
void coprocessor_segment_overrun();
void invalid_TSS();