将内核定位到高地址(存在bug,中断时会访问低地址)

This commit is contained in:
fslongjin
2022-04-10 21:30:16 +08:00
parent 3063a340e2
commit f5f36aafd8
23 changed files with 689 additions and 238 deletions

View File

@ -1,4 +1,6 @@
#include"../common/asm.h"
.code64
.section .text
R15 = 0x00
R14 = 0x08
@ -54,6 +56,7 @@ Restore_all:
ret_from_exception:
// === ===
.code64
ENTRY(ret_from_intr)
jmp Restore_all
@ -85,18 +88,18 @@ Err_Code:
pushq %r15
cld
movq ERRCODE(%rsp), %rsi // rsi
movq FUNC(%rsp), %rdx
movq $0x10, %rdi //
movq %rdi, %ds
movq %rdi, %es
movq %rsp, %rdi // rdi
callq *%rdx // *
callq %rdx // *
jmp ret_from_exception
//
@ -174,6 +177,7 @@ ENTRY(ret_from_system_call)
// 0 #DE
ENTRY(divide_error)
pushq $0 //#DE0
pushq %rax // rax
leaq do_divide_error(%rip), %rax //
@ -333,3 +337,7 @@ ENTRY(virtualization_exception)
leaq do_virtualization_exception(%rip), %rax //
xchgq %rax, (%rsp) // FUNC
jmp Err_Code
ENTRY(_stack_start)
.quad initial_proc_union + 32768

View File

@ -10,6 +10,7 @@
#define __GATE_H__
#include "../common/kprint.h"
#include <mm/mm.h>
//描述符表的结构体
struct desc_struct
@ -27,6 +28,18 @@ extern struct desc_struct GDT_Table[]; // GDT_Table是head.S中的GDT_Table
extern struct gate_struct IDT_Table[]; // IDT_Table是head.S中的IDT_Table
extern unsigned int TSS64_Table[26];
struct gdtr
{
uint16_t size;
uint64_t gdt_vaddr;
}__attribute__((packed));
struct idtr
{
uint16_t size;
uint64_t idt_vaddr;
}__attribute__((packed));
/**
* @brief 初始化中段描述符表内的门描述符每个16B
* @param gate_selector_addr IDT表项的地址
@ -86,8 +99,10 @@ void set_tss_descriptor(unsigned int n, void *addr)
{
unsigned long limit = 103;
*(unsigned long *)(&GDT_Table[n]) = (limit & 0xffff) | (((unsigned long)addr & 0xffff) << 16) | ((((unsigned long)addr >> 16) & 0xff) << 32) | ((unsigned long)0x89 << 40) | ((limit >> 16 & 0xf) << 48) | (((unsigned long)addr >> 24 & 0xff) << 56); /////89 is attribute
*(unsigned long *)(&GDT_Table[n + 1]) = (((unsigned long)addr >> 32) & 0xffffffff) | 0;
*(unsigned long *)(phys_2_virt(GDT_Table) + n) = (limit & 0xffff) | (((unsigned long)addr & 0xffff) << 16) | ((((unsigned long)addr >> 16) & 0xff) << 32) | ((unsigned long)0x89 << 40) | ((limit >> 16 & 0xf) << 48) | (((unsigned long)addr >> 24 & 0xff) << 56); /////89 is attribute
kdebug("1212");
*(unsigned long *)(phys_2_virt(GDT_Table) + n + 1) = (((unsigned long)addr >> 32) & 0xffffffff) | 0;
}
/**
@ -110,8 +125,9 @@ void set_tss_descriptor(unsigned int n, void *addr)
*/
void set_intr_gate(unsigned int n, unsigned char ist, void *addr)
{
_set_gate((IDT_Table + n), 0x8E, ist, addr); // p=1DPL=0, type=E
// set_gate((ul *)(IDT_Table + n), 0x8E, ist, (ul *)(addr)); // p=1DPL=0, type=E
_set_gate(phys_2_virt(IDT_Table + n), 0x8E, ist, addr); // p=1DPL=0, type=E
//set_gate((ul *)phys_2_virt(IDT_Table + n), 0x8E, ist, (ul *)(addr)); // p=1DPL=0, type=E
}
/**
@ -125,8 +141,8 @@ void set_trap_gate(unsigned int n, unsigned char ist, void *addr)
{
// kdebug("addr=%#018lx", (ul)(addr));
// set_gate((ul *)(IDT_Table + n), 0x8F, ist, (ul *)(addr)); // p=1DPL=0, type=F
_set_gate((IDT_Table + n), 0x8F, ist, addr); // p=1DPL=0, type=F
//set_gate((ul *)phys_2_virt(IDT_Table + n), 0x8F, ist, (ul *)(addr)); // p=1DPL=0, type=F
_set_gate(phys_2_virt(IDT_Table + n), 0x8F, ist, addr); // p=1DPL=0, type=F
}
/**
@ -140,8 +156,8 @@ void set_system_trap_gate(unsigned int n, unsigned char ist, void *addr)
{
// kdebug("addr=%#018lx", (ul)(addr));
// set_gate((ul *)(IDT_Table + n), 0xEF, ist, (ul *)(addr)); // p=1DPL=3, type=F
_set_gate((IDT_Table + n), 0xEF, ist, addr); // p=1DPL=3, type=F
//set_gate((ul *)phys_2_virt(IDT_Table + n), 0xEF, ist, (ul *)(addr)); // p=1DPL=3, type=F
_set_gate(phys_2_virt(IDT_Table + n), 0xEF, ist, addr); // p=1DPL=3, type=F
}
/**
@ -149,19 +165,19 @@ void set_system_trap_gate(unsigned int n, unsigned char ist, void *addr)
*
*/
void set_tss64(unsigned int * Table,unsigned long rsp0,unsigned long rsp1,unsigned long rsp2,unsigned long ist1,unsigned long ist2,unsigned long ist3,
unsigned long ist4,unsigned long ist5,unsigned long ist6,unsigned long ist7)
void set_tss64(unsigned int *Table, unsigned long rsp0, unsigned long rsp1, unsigned long rsp2, unsigned long ist1, unsigned long ist2, unsigned long ist3,
unsigned long ist4, unsigned long ist5, unsigned long ist6, unsigned long ist7)
{
*(unsigned long *)(Table+1) = rsp0;
*(unsigned long *)(Table+3) = rsp1;
*(unsigned long *)(Table+5) = rsp2;
*(unsigned long *)(Table + 1) = rsp0;
*(unsigned long *)(Table + 3) = rsp1;
*(unsigned long *)(Table + 5) = rsp2;
*(unsigned long *)(Table+9) = ist1;
*(unsigned long *)(Table+11) = ist2;
*(unsigned long *)(Table+13) = ist3;
*(unsigned long *)(Table+15) = ist4;
*(unsigned long *)(Table+17) = ist5;
*(unsigned long *)(Table+19) = ist6;
*(unsigned long *)(Table+21) = ist7;
*(unsigned long *)(Table + 9) = ist1;
*(unsigned long *)(Table + 11) = ist2;
*(unsigned long *)(Table + 13) = ist3;
*(unsigned long *)(Table + 15) = ist4;
*(unsigned long *)(Table + 17) = ist5;
*(unsigned long *)(Table + 19) = ist6;
*(unsigned long *)(Table + 21) = ist7;
}
#endif

View File

@ -203,6 +203,10 @@ void irq_init()
#else
apic_init();
memset(interrupt_desc, 0, sizeof(irq_desc_t) * IRQ_NUM);
kdebug("interrupt_desc=%#018lx",(void*)interrupt_desc);
kdebug("irq_init()=%#018lx",(void*)irq_init);
memset((void*)interrupt_desc, 0, sizeof(irq_desc_t) * IRQ_NUM);
#endif
}

View File

@ -58,7 +58,6 @@ void sys_vector_init()
// 0 #DE 除法错误
void do_divide_error(struct pt_regs *regs, unsigned long error_code)
{
kerror("do_divide_error(0)");
//kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip);
@ -243,33 +242,35 @@ void do_general_protection(struct pt_regs *regs, unsigned long error_code)
// 14 #PF 页故障
void do_page_fault(struct pt_regs *regs, unsigned long error_code)
{
hlt();
unsigned long cr2 = 0;
// 先保存cr2寄存器的值避免由于再次触发页故障而丢失值
// cr2存储着触发异常的线性地址
__asm__ __volatile__("movq %%cr2, %0"
: "=r"(cr2)::"memory");
kerror("do_page_fault(14),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\tCR2:%#18lx\n", error_code, regs->rsp, regs->rip, cr2);
__asm__ __volatile__("movq %%cr2, %0":"=r"(cr2)::"memory");
kerror("do_page_fault(14),Error code :%#018lx,RSP:%#018lx,RIP:%#018lx\n",error_code , regs->rsp , regs->rip);
printk_color(YELLOW, BLACK, "Information:\n");
if (!(error_code & 0x01))
printk("Page does not exist.\n");
if(!(error_code & 0x01))
printk_color(RED,BLACK,"Page Not-Present,\t");
if (error_code & 0x02)
printk("Fault occurred during operation: writing\n");
else
printk("Fault occurred during operation: reading\n");
if(error_code & 0x02)
printk_color(RED,BLACK,"Write Cause Fault,\t");
else
printk_color(RED,BLACK,"Read Cause Fault,\t");
if (error_code & 0x04)
printk("Fault in user level(3).\n");
else
printk("Fault in supervisor level(0,1,2).\n");
if(error_code & 0x04)
printk_color(RED,BLACK,"Fault in user(3)\t");
else
printk_color(RED,BLACK,"Fault in supervisor(0,1,2)\t");
if (error_code & 0x08)
printk("Reserved bit caused the fault.\n");
if(error_code & 0x08)
printk_color(RED,BLACK,",Reserved Bit Cause Fault\t");
if (error_code & 0x10)
printk("Fault occurred during fetching instruction.\n");
if(error_code & 0x10)
printk_color(RED,BLACK,",Instruction fetch Cause Fault");
printk_color(RED,BLACK,"\n");
printk_color(RED,BLACK,"CR2:%#018lx\n",cr2);
while (1)
;