【发布时间】:2014-01-12 06:03:00
【问题描述】:
我一直在看内核开发教程,尤其是bran's kernel,我使用 qemu 进行测试,使用 -kernel 或 -cdrom 我遇到了崩溃。据我了解,这里会发生什么:
一旦我启用了中断 (sti),我的 irq 处理程序就会捕获坑中断。我重用了 Bran 示例中的代码:
.intel_syntax noprefix
.global irq00
.extern_default_handler
irq00:
cli
push 0
push 32
jmp irq_common_stub
irq_common_stub:
pushad
push ds
push es
push fs
push gs
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
push eax
mov eax, irq_default_handler
call eax
etc...
add esp, 8
sti
iret
现在当我启用 qemu 跟踪选项 (-d cpu,exec,in_asm) 时,我看到了:
IN:
0x0010121b: mov %eax, %fs
0x0010121d: mov %eax, %gs
0x0010121f: mov %esp, %eax
0x00101222: mov 0x100e81, %eax
0x00101227: call *%eax
Trace 0xb3aa3360 [0010121b]
EAX=83535657 EBX=00009500 ECX=0000000f EDX=00000000
ESI=00000000 EDI=0010a000 EBP=00000000 ESP=00107fc8
EIP=83535657 EFL=00000046 [---Z-P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
CS =0008 00000000 ffffffff 00cf9300 DPL=0 CS32 [-R-]
SS =0008 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
DS =0008 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
FS =0008 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
GS =0008 00000000 ffffffff 00cf9300 DPL=0 DS [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT= 00108060 0000001f
IDT= 001080a0 0000071f
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=SUBL
EFER=0000000000000000
qemu fatal: trying to execute code outside RAM or ROM at 0x83535657
最后一个objdump -d kernel.bin 让我检查一下确实是我的
void irq_default_handler(isr_stack_frame_t sf);
C 函数在我期望的地址:
00100e81 <irq_default_handler>
eax 和 eip 的值看起来很奇怪,但前面的 mov 显示了 C 函数的正确地址。 也许最后缺少的信息是以下结构:
typedef struct {
uint32_t gs, ds, es, ds;
uint32_t edi, esi, ebp, ebx, edx, ecx, eax;
uint32_t int_number, err_code;
uint32_t eip, cs, eflags, useresp, ss;
} isr_stack_frame_t;
所以从那里不知道去哪里。不知道我如何从我的电话中得到一个不正确的 eip 值。特别是因为我也尝试直接调用 C 函数。欢迎任何建议或评论。
【问题讨论】:
-
Bran 的示例在我看来就像 Nasm 语法。对于“.intel_syntax noprefix”,请尝试
mov eax, offset irq_default_handler。如果 Gas 对此抱怨,请尝试mov eax, offset flat: irq_default_handler- 或使用与示例相同的汇编程序。 -
该教程使用 NASM。 GAS 和 NASM 不一样。您应该这样做,或者理解程序集并翻译成 AT&T 语法。 -1 表示 GAS 中的 Intel 语法,其中数据从右向左流动。并为一个好问题 +1。