【发布时间】:2011-05-04 03:12:42
【问题描述】:
我正在尝试编写一个内核,主要用于娱乐目的,我遇到了一个问题,我认为这是三重故障。在我尝试启用分页之前一切正常。正在破坏的代码是这样的:
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
为此,我一直在关注各种教程/文档,但我用来分页的那个是 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!
这是 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;
IDT 与此非常相似。
【问题讨论】:
-
那么当你执行有问题的行时究竟会发生什么?
-
虚拟机自行重启,并带回引导加载程序
-
我认为这里没有足够的信息:很多事情都可能出错,尤其是当您混合各种教程中的内容时。您是如何设置 GDT 的? CS、DS 和 SS(至少)是否都选择了合适的 GDT 条目? IDT 呢?是否为标识映射正确设置了页表?您在哪个虚拟机环境中运行它?
-
我添加了更多代码作为编辑,使其比注释更清晰。感谢您的回复,我不完全确定我应该发布什么可能有用的内容。我使用的虚拟机是qemu。已经为身份映射设置了页表,但我不完全确定我会调查的正确部分。
标签: c assembly operating-system kernel