启用分页重启QEMU

问题描述 投票:0回答:1

在我的分页代码中,qemu 由于某些内联汇编而重新启动。可能是其他原因,但删除程序集会使程序正常工作。或者,某些较早的代码也可能导致分页初始化不正确。 这是分页初始化函数:

void paging_init(){
    //Enable Paging
    uint32 cr0;
    memset(g_page_directory, 0, sizeof(page_directory_t) * 1024);
    memset(g_page_tables, 0, sizeof(page_table_t) * 1024);
    
    // Set All page directories to have rw and u/s access (only by kernel)
    for(int i = 0; i < 1024; i++){
        g_page_directory[i].user_supervisor = 1;
        g_page_directory[i].read_write = 1;
    }
    
    // fill all the entries in page table to map all 4MB memory
    for (int i = 0; i < 1024; i++) {
        g_page_tables[i].present = 1;
        g_page_tables[i].read_write = 1;
        g_page_tables[i].user_supervisor = 1;
        g_page_tables[i].frame = (i * PAGE_SIZE) >> 12;
    }
    
    // set first page directory to be accessed with frame 0x11a(kernel region address)
    g_page_directory[0].present = 1;
    g_page_directory[0].accessed = 0;
    g_page_directory[0].user_supervisor = 1;
    g_page_directory[0].frame = 0x11a;
    //Register Page Fault Handler
    isr_register_interrupt_handler(14, page_fault_handler);

    // set cr3 point to page directory
    asm volatile("mov %0, %%cr3" ::"r"(g_page_directory));
    
    // set bit in cr0 to enable paging
    asm volatile("mov %%cr0, %0": "=r"(cr0));
    cr0 = cr0 | 0x80000000;
    asm volatile("mov %0, %%cr0" ::"r"(cr0));
}

QEMU 由于这些内联汇编行而重新启动:

    // set cr3 point to page directory
    asm volatile("mov %0, %%cr3" ::"r"(g_page_directory));
    
    // set bit in cr0 to enable paging
    asm volatile("mov %%cr0, %0": "=r"(cr0));
    cr0 = cr0 | 0x80000000;
    asm volatile("mov %0, %%cr0" ::"r"(cr0));

如何修复此错误?

编辑(这里是类型定义):

// Page Directory Struct
typedef struct {
    uint32 present :1;
    uint32 read_write :1;
    uint32 user_supervisor :1;
    uint32 write_through :1;
    uint32 cache_disable :1;
    uint32 accessed :1;
    uint32 dirty :1;
    uint32 page_size :1;
    uint32 global :1;
    uint32 available :3;
    uint32 frame :20;
} page_directory_t;

// Page Table
typedef struct {
    uint32 present :1;
    uint32 read_write :1;
    uint32 user_supervisor :1;
    uint32 write_through :1;
    uint32 cache_disable :1;
    uint32 accessed :1;
    uint32 dirty :1;
    uint32 page_size :1;
    uint32 global :1;
    uint32 available :3;
    uint32 frame :20;
} page_table_t;

编辑:带有分页代码的代码示例(某些结构的名称不同,但除此之外代码全部相同): https://filebin.net/hql1zrigj8dgbmto

c x86 paging inline-assembly osdev
1个回答
0
投票

好的,添加

g_page_directory[0].frame = ((uint32)g_page_tables >> 12);
有帮助,并且代码没有错误。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.