禁用寻呼后,操作系统在远端跳转时复位。

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

我正在修改一个 常规 切换到和从realmode执行BIOS中断,但遇到了寻呼的问题。我之前在没有寻呼的情况下工作,但现在我的操作系统使用寻呼,我需要在进入realmode之前禁用它(并在之后启用它)。

我的问题是,当执行远端跳转以使页面禁用生效时,有些东西出了大问题,我得到了一个重启。

下图所示的代码是通过先使用页面表创建一个身份映射来工作的 boot_page_table1 这是一个简单的页表,标识映射前4 MiB。这必须要做,因为我正在使用分页从更高的内存中运行我的内核代码,而所有的内核代码的地址都是从 0xC0100000 而在加载时,从 0x00100000. 然后我刷新TLB并跳转到附近的标签,但这次使用的是较低内存中的地址。我的指令指针现在应该指向身份映射代码,应该可以安全地禁用分页。然后,分页位在 cr3,TLB又被刷新了,因为我很偏执,切换模式的代码继续。

代码的工作原理是将自己复制到16位内存的0x7c00处,然后跳转到该处,这样就可以在16位realmode中工作。

如果我不禁用分页位,而让其他一切都保持不变,则 jmpw CODE16:REBASE(p_mode16) 工作,并且进入跳转后的无限循环让我认为这个问题的发生是由于我如何禁用分页。我在禁用寻呼时是不是遗漏了什么? 我在其他网站上看到 员额 因为你所做的是非常不寻常的,你可能会遇到错误和模拟器的兼容性问题",但我还不确定是否只是我的代码出了问题。

这段代码是用英特尔语法和GAS汇编器写的。

.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 .-2p_mode16 标签,而发生重启。如果 jmp .-2 放在前面的是 jmpw 然后操作系统如期进入无限循环。我在QEMU 2.11.1版本上运行这个程序,并使用了 qemu-system-i386.

x86 paging gas osdev intel-syntax
1个回答
2
投票

问题是这样的。

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中(因为 "who-knows-what "不是一个有效的代码描述符)。

为了解决这个问题,你需要修改GDT的基础值,然后再在 sgdt [gdt_ptr] 执行(例如,也许只需使用 .4byte REBASE(gdt16_base) 而不是 .4byte gdt16_base 如果 gdt16_ptr 在其他地方没有使用)。)

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