本土内核的三重故障

bsc*_*r13 9 c assembly operating-system kernel

我正在尝试编写一个内核,主要是出于娱乐目的,我遇到了一个问题我相信它是三重故障.在我尝试启用分页之前,一切正常.破解的代码是这样的:

void switch_page_directory(page_directory_t *dir){

 current_directory = dir;
 asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical));

 u32int cr0;
 asm volatile("mov %%cr0, %0": "=r"(cr0));

 cr0 |= 0x80000000;//enable paging
 asm volatile("mov %0, %%cr0":: "r"(cr0)); //this line breaks


}//switch page directory
Run Code Online (Sandbox Code Playgroud)

我一直在关注各种教程/文档,但我用于分页的是http://www.jamesmolloy.co.uk/tutorial_html/6.-Paging.html.我不确定其他代码对于解决这个问题会有什么用处,但如果有更多代码我应该提供,我将非常乐意这样做.

编辑=====

我相信CS,DS和SS正在选择正确的条目,这是用于设置它们的代码

global gdt_flush     
extern gp            
gdt_flush:

    lgdt [gp]        ; Load the GDT with our 'gp' which is a special pointer
    mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment

    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax

    jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!

flush2:
    ret               ; Returns back to the C code!
Run Code Online (Sandbox Code Playgroud)

这是gdt结构本身

struct gdt_entry{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    unsigned char access;
    unsigned char granularity;
    unsigned char base_high;
} __attribute__((packed));

struct gdt_ptr{
    unsigned short limit;
    unsigned int base;
} __attribute__((packed));

struct gdt_entry gdt[5];
struct gdt_ptr gp;
Run Code Online (Sandbox Code Playgroud)

IDT与此非常相似.

Mat*_*ery 6

GDT:您没有说明GDT条目的内容是什么,但您显示的内容与您链接的教程前一部分非常相似,如果您以相同的方式设置条目,那么一切都应该是好的(即用于CS的环0代码段的平坦段映射,用于其他所有的环0数据段,都具有0的基数和4GB的限制).

IDT:如果中断被禁用并且你还没有(还)期望导致页面错误,那么无论如何都可能无关紧要.

页表:不正确的页表看起来似乎是最可能的嫌疑人.确保您的身份映射涵盖了您正在使用的所有代码,数据和堆栈内存(至少).

在底部链接到源代码http://www.jamesmolloy.co.uk/tutorial_html/6.-Paging.html肯定建立的东西,确实既QEMU和Bochs的正常工作,所以希望你可以比较你"重新做正在做的事情,找出问题所在.

QEMU一般情况良好,但我建议Bochs开发真正的低级内容 - 它包括(或可以配置为包含)非常方便的内部调试器.例如,在配置文件reset_on_triple_fault=0cpu:行上设置,在switch_page_directory()代码中设置断点,运行到断点,然后单步指令,看看会发生什么......