12

私は学習目的でおもちゃのカーネルを書いていますが、少し問題があります。フロッピーディスク(32ビットコードで記述されている)からセグメントをロードする単純なブートローダーを作成しました。次に、ブートローダーはA20ゲートを有効にし、プロテクトモードをオンにします。アセンブラで記述すれば32ビットコードにうまくジャンプできますが、Cで記述するとトリプルフォールトになります。Cコードを分解すると、最初の2つの命令には新しいスタックフレームのセットアップが含まれていることがわかります。これは、動作中のASMコードと失敗したCコードの主な違いです。ASMコードにはNASMv2.10.05を使用し、CコードにはDJGPP4.72コレクションのGCCを使用しています。

これはブートローダーコードです:

org 7c00h
BITS 16

entry:
mov [drive], dl         ;Save the current drive


cli
mov ax,cs               ; Setup segment registers
mov ds,ax               ; Make DS correct
mov ss,ax               ; Make SS correct        

mov bp,0fffeh
mov sp,0fffeh           ;Setup a temporary stack
sti

;Set video mode to text
;===================
mov ah, 0
mov al, 3
int 10h
;===================

;Set current page to 0
;==================
mov ah, 5
mov al, 0
int 10h
;==================

;Load the sector
;=============
call load_image
;=============

;Clear interrupts
;=============
cli
;=============

;Disable NMIs
;============
in ax, 70h
and ax, 80h ;Set the high bit to 1
out 70h, ax
;============

;Enable A20:
;===========
mov ax, 02401h
int 15h
;===========

;Load the GDT
;===========
lgdt [gdt_pointer]
;===========

;Clear interrupts
;=============
cli
;=============

;Enter protected mode
;==================
mov eax, cr0
or eax, 1       ;Set the low bit to 1
mov cr0, eax
;==================

jmp 08h:clear_pipe  ;Far jump to clear the instruction queue


;======================================================
load_image:
reset_drive:
    mov ah, 00h
    ; DL contains *this* drive, given to us by the BIOS
    int 13h
    jc reset_drive

read_sectors:
    mov ah, 02h
    mov al, 01h
    mov ch, 00h
    mov cl, 02h
    mov dh, 00h
    ; DL contains *this* drive, given to us by the BIOS

    mov bx, 7E0h
    mov es, bx
    mov bx, 0

    int 13h
    jc read_sectors

ret
;======================================================

BITS 32 ;Protected mode now!
clear_pipe:

mov ax, 10h             ; Save data segment identifier
mov ds, ax              ; Move a valid data segment into the data segment register
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax              ; Move a valid data segment into the stack segment register
mov esp, 90000h        ; Move the stack pointer to 90000h
mov ebp, esp

jmp 08h:7E00h           ;Jump to the kernel proper

;===============================================
;========== GLOBAL DESCRIPTOR TABLE ==========
;===============================================

gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
    dd 0
    dd 0

gdt_code:               ; Code segment, read/execute, nonconforming
    dw 0FFFFh       ;   LIMIT, low 16 bits
    dw 0            ;   BASE, low 16 bits           
    db 0            ;   BASE, middle 8 bits
    db 10011010b    ;   ACCESS byte
    db 11001111b    ;   GRANULARITY byte
    db 0            ;   BASE, low 8 bits

gdt_data:               ; Data segment, read/write, expand down
    dw 0FFFFh
    dw 0
    db 0
    db 10010010b
    db 11001111b
    db 0

gdt_end:                ; Used to calculate the size of the GDT



gdt_pointer:                       ; The GDT descriptor
    dw gdt_end - gdt - 1    ; Limit (size)
    dd gdt                  ; Address of the GDT
;===============================================
;===============================================

drive: db 00    ;A byte to store the current drive in
times 510-($-$$) db 00
db 055h
db 0AAh

そしてこれはカーネルコードです:

void main()
{
    asm("mov byte ptr [0x8000], 'T'");
    asm("mov byte ptr [0x8001], 'e'");
    asm("mov byte ptr [0x8002], 's'");
    asm("mov byte ptr [0x8003], 't'");
}

カーネルは、これらの4バイトをメモリに挿入するだけです。これは、VMPlayer仮想マシンでコードを実行しているときに確認できます。バイトが表示されれば、コードが機能していることがわかります。次のようなコードをASMで作成すると、プログラムは機能します。

org 7E00h
BITS 32

main:
mov byte [8000h], 'T'
mov byte [8001h], 'e'
mov byte [8002h], 's'
mov byte [8003h], 't'

hang:
    jmp hang

したがって、唯一の違いは、分解されたCコードで見つけた2つのスタック操作です。これらは次のとおりです。

push ebp
mov ebp, esp

この問題に関する助けをいただければ幸いです。私は、この種のことが可能であることを知っているので、ここでは、比較的マイナーですが重要な何かが欠けていると思います。

4

2 に答える 2

3

ここでテクニックを使ってみてください:

生のバイナリを出力する gcc を取得する方法はありますか?

オブジェクト ファイルから .text セクションのフラット バイナリを生成します。

于 2013-02-11T05:09:13.600 に答える
1

プロテクトモードスタックのアドレスを別の場所(たとえば0x80000)に移動してみます。これは(これによると:http://wiki.osdev.org/Memory_Map_(x86))無料で使用できることが保証されているRAMです(現在使用しているアドレス(0x90000)とは対照的に、テスト環境として使用しているものによっては、明らかに存在しない可能性があります)。

于 2013-02-10T12:00:45.250 に答える