首页 > 解决方案 > 禁用分页后操作系统在远跳转时重置

问题描述

我正在修改一个在实模式之间切换以执行 BIOS 中断的例程,但遇到了分页问题我之前没有分页,但现在我的操作系统使用分页,我需要在进入实模式之前禁用它(并在之后启用它)。

我的问题是,当执行远跳转导致页面禁用生效时,出现了严重错误,我重新启动。

下面显示的代码首先使用页表创建身份映射,该页表boot_page_table1只是身份映射前 4 MiB 的页表。这必须完成,因为我目前使用分页从更高的内存运行我的内核代码,并且所有内核代码都在0xC01000000x00100000. 然后我刷新 TLB 并跳转到附近的标签,但这次使用的是较低内存中的地址。我的指令指针现在应该指向标识映射代码,并且禁用分页应该是安全的。然后在 中禁用分页位cr3,TLB 再次刷新,因为我很偏执,并且切换模式的代码继续进行。

该代码通过将自身处理到 0x7c00 的 16 位内存中,然后跳转到该位置,以便它可以在 16 位实模式下工作。

如果我不禁用分页位并使其他所有内容保持不变,jmpw CODE16:REBASE(p_mode16)则进入跳转后的工作和无限循环让我认为这个问题是由于我禁用分页的方式而发生的。禁用分页时我错过了什么吗?我在其他帖子 上看到“因为您所做的事情非常不寻常,您可能会遇到模拟器的错误和兼容性问题”,但我不确定它是否只是我的代码错误。

该代码是使用带有 GAS 汇编器的 intel 语法编写的。

.intel_syntax noprefix

.code32

.global int32, _int32

#define regs16_t_size                          13*2
#define INT32_BASE                             0x00007C00
#define REBASE(x)                              (((x) - reloc) + INT32_BASE)
#define GDTENTRY(x)                            ((x) &lt;< 3)
#define CODE32                                 0x08
#define DATA32                                 0x10
#define CODE16                                 0x18
#define DATA16                                 0x20
#define STACK16                                (INT32_BASE - regs16_t_size)

.global reloc
.global int32_end

.section .text
    int32: .code32                             # by Napalm
    _int32:
        cli                                    # disable interrupts
        pusha                                  # save register state to 32bit stack

        # Enable identity mapping the first MiB, jump, then disable paging
        push [boot_page_directory] # Push first page directory entry to restore it after

        mov eax, (offset boot_page_table1) - 0xC0000000 + 0x003
        mov [boot_page_directory], eax

        mov ecx, cr3 # Reload crc3 to force a TLB flush so the changes to take effect.
        mov cr3, ecx

        mov eax, (offset napalm_switch_disable_paging) - 0xC0000000
        jmp eax
        napalm_switch_disable_paging:

        # Code is now running with the instruction pointer in lower memory,
        # but the code is still assembled as though its in higher memory. Because
        # of this, something like jmp INT32_BASE would fail since it would
        # assemble as a relative jump from an address around 0xC0100000 to 0x7C00
        # but will be running at an address around 0x00100000 causing it to jump to
        # 0x40007C00.

        # Disable paging bit
        mov eax, cr0
        and eax, ~0x80000000
        mov cr0, eax

        mov ecx, cr3 # Reload crc3 to force a TLB flush so the changes to take effect.
        mov cr3, ecx

        mov  esi, (offset reloc) - 0xC0000000  # set source to code below
        mov  edi, INT32_BASE                   # set destination to new base address
        mov  ecx, int32_end - reloc            # set copy size to our codes size
        cld                                    # clear direction flag (so we copy forward)
        rep  movsb                             # do the actual copy (relocate code to low 16bit space)
        mov eax, INT32_BASE
        jmp eax                         # jump to new code location
    reloc: .code32                             # by Napalm
        mov  [REBASE(stack32_ptr)], esp        # save 32bit stack pointer
        sidt [idt_ptr]               # save 32bit idt pointer
        sgdt [gdt_ptr]               # save 32bit gdt pointer
        lgdt [REBASE(gdt16_ptr)]               # load 16bit gdt pointer
        lea  esi, [esp+0x24]                   # set position of intnum on 32bit stack
        lodsd                                  # read intnum into eax
        mov  [REBASE(ib)], al                  # set intrrupt immediate byte from our arguments 
        mov  esi, [esi]                        # read regs pointer in esi as source
        mov  edi, STACK16                      # set destination to 16bit stack
        mov  ecx, regs16_t_size                # set copy size to our struct size
        mov  esp, edi                          # save destination to as 16bit stack offset
        rep  movsb                             # do the actual copy (32bit stack to 16bit stack)

        jmpw CODE16:REBASE(p_mode16)      # switch to 16bit selector (16bit protected mode)


    p_mode16: .code16
        jmp .-2

... 
more of the routine thats not run due to the bug 
...

    stack32_ptr:                               # address in 32bit stack after we
        .4byte 0x00000000                          #   save all general purpose registers

    idt16_ptr:                                 # IDT table pointer for 16bit access
        .2byte 0x03FF                              # table limit (size)
        .4byte 0x00000000                          # table base address

    gdt16_base:                                # GDT descriptor table
        .null:                                 # 0x00 - null segment descriptor
            .4byte 0x00000000                      # must be left zero'd
            .4byte 0x00000000                      # must be left zero'd

        .code32:                               # 0x01 - 32bit code segment descriptor 0xFFFFFFFF
            .2byte 0xFFFF                          # limit  0:15
            .2byte 0x0000                          # base   0:15
            .byte 0x00                            # base  16:23
            .byte 0x9A                            # present, iopl/0, code, execute/read
            .byte 0xCF                            # 4Kbyte granularity, 32bit selector; limit 16:19
            .byte 0x00                            # base  24:31

        .data32:                               # 0x02 - 32bit data segment descriptor 0xFFFFFFFF
            .2byte 0xFFFF                          # limit  0:15
            .2byte 0x0000                          # base   0:15
            .byte 0x00                            # base  16:23
            .byte 0x92                            # present, iopl/0, data, read/write
            .byte 0xCF                            # 4Kbyte granularity, 32bit selector; limit 16:19
            .byte 0x00                            # base  24:31

        .code16:                               # 0x03 - 16bit code segment descriptor 0x000FFFFF
            .2byte 0xFFFF                          # limit  0:15
            .2byte 0x0000                          # base   0:15
            .byte 0x00                            # base  16:23
            .byte 0x9A                            # present, iopl/0, code, execute/read
            .byte 0x0F                            # 1Byte granularity, 16bit selector; limit 16:19
            .byte 0x00                            # base  24:31

        .data16:                               # 0x04 - 16bit data segment descriptor 0x000FFFFF
            .2byte 0xFFFF                          # limit  0:15
            .2byte 0x0000                          # base   0:15
            .byte 0x00                            # base  16:23
            .byte 0x92                            # present, iopl/0, data, read/write
            .byte 0x0F                            # 1Byte granularity, 16bit selector; limit 16:19
            .byte 0x00                            # base  24:31

    gdt16_ptr:                                 # GDT table pointer for 16bit access
        .2byte gdt16_ptr - gdt16_base - 1          # table limit (size)
        .4byte gdt16_base                          # table base address

    int32_end:                                 # end marker (so we can copy the code)
        .byte 0x00

永远不会到达带有jmp .-2atp_mode16标签的行,而是会重新启动。如果将jmp .-2放在 之前,jmpw则操作系统按预期进入无限循环。我在 QEMU 版本 2.11.1 上使用qemu-system-i386.

标签: x86paginggnu-assemblerosdevintel-syntax

解决方案


问题是这样的:

gdt16_ptr:                             # GDT table pointer for 16bit access
    .2byte gdt16_ptr - gdt16_base - 1  # table limit (size)
    .4byte gdt16_base                  # GDT base in higher half that WILL NOT WORK WHEN PAGING IS DISABLED

因为您告诉 CPU GDT 在上半部分,所以在禁用分页后,它无法正确访问 GDT 条目(它可能访问 0xC000 处的物理地址????而是读取谁知道什么 - 例如也许PCI 设备的寄存器,可能是“不是 RAM 或设备”等),因此当远跳转尝试加载CODE16到 CS 时它会崩溃(因为“谁知道什么”不是有效的代码描述符)。

要解决这个问题,您需要在sgdt [gdt_ptr]执行之前修改 GDT 基数的值(例如,可能只是使用.4byte REBASE(gdt16_base)而不是.4byte gdt16_baseifgdt16_ptr在其他地方没有使用)。


推荐阅读