概述
最近在拜读《inux内核源码剖析》一书,以下内容皆为此书引出,为后续的阅读做个铺垫。
本篇讲述如何搭建与使用qemu对内核启动程序进行汇编级别调试,通过本篇可以了解如下内容:
- 使用qemu远程调试方式跟踪系统启动过程
- gdb调试8086程序的方法
环境准备
- ubuntu16
- 安装qemu与编译链接工具
apt get install qemu bin86
启动镜像制作
我们要实现系统启动后,在启动界面上打印hello world!
as86 汇编代码如下:
;
; display "hello world" in the boot screen
;
.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text
BOOTSEG = 0x07c0
entry start
start:
jmpi go, BOOTSEG
go:
mov ax, cs
mov ds, ax
mov es, ax
mov cx, #16 ;显示16个字节
mov dx, #1804 ;
mov bx, #0x000c ;字符为红色
mov bp, #msg1 ;字符串地址
mov ax, #0x1301 ;BIOS中断调用, 功能0x13, 子功能01
int 0x10
loop0: jmp loop0
msg1: .byte 13, 10
.ascii "Hello World!"
.byte 13, 10
.org 510
.word 0xAA55
.text
endtext:
.data
enddata:
.bss
endbss:
makefile如下
all:
as86 -0 -a -o boot.o boot.s
ld86 -0 -s -o boot boot.o
dd bs=32 if=boot of=./boot_image skip=1
执行make即可生成镜像文件boot_image.
开始调试
启动qemu
用qemu启动镜像文件,命令如下:
qemu-system-i386 -s -S -vnc :2 boot_image
其中-s是设置gdbserver的监听端口,-S则是让cpu加电后被挂起。
-s
Shorthand for -gdb tcp::1234, i.e. open a gdbserver on TCP port 1234 (see gdb_usage).
-S
Do not start CPU at startup (you must type ’c’ in the monitor).
启动gdb
- 输入
gdb -q
- 连接gdb server
target remote :1234
- 设置被调试环境架构
set architecture i8086
- 显示将要执行的汇编指令
display /5i $cs * 0x10 + $pc
- 打断点调试
qemu的 -S选项相当于将断点打在CPU加电后要执行的第一条指令处,也就是BIOS程序的第一条指令。
通过info reg命令看到,cpu加电后的寄存器值,cs值为0xF000, pc值为0xFFF0。
不要直接单步程序,加电后bios代码先运行,运行完后将系统引导程序加载到内存0x7c00开始的位置,之后跳转到0x7c00处。
所以断点应打在0x7c00处。输入continue,程序停在断点处。现在, 是不是看到了boot.s文件里的汇编指令?
注意事项
- 汇编代码每行必须要以回车结尾
以回车结尾包括最后一行,否则as86会报错(这个错误提示真让人摸不着头脑)
as: error reading input
- 关于加电后第一条指令地址
Intel手册上写了,上电后cpu加载的第一条指令的地址其实是0xFFFFFFF0,原文如下:
The address FFFFFFF0H is beyond the 1-MByte addressable range of the processor while in real-address mode. The
processor is initialized to this starting address as follows. The CS register has two parts: the visible segment
selector part and the hidden base address part. In real-address mode, the base address is normally formed by shifting the 16-bit segment selector value 4 bits to the left to produce a 20-bit base address. However, during a hardware reset, the segment selector in the CS register is loaded with F000H and the base address is loaded with FFFF0000H. The starting address is thus formed by adding the base address to the value in the EIP register (that
is, FFFF0000 + FFF0H = FFFFFFF0H).
只有在修改cs值后才会变成真正的实模式寻址
The first time the CS register is loaded with a new value after a hardware reset, the processor will follow the normal rule for address translation in real-address mode (that is, [CS base address = CS segment selector * 16]). To insure that the base address in the CS register remains unchanged until the EPROM based software-initialization code is completed, the code must not contain a far jump or far call or allow an interrupt to occur (which would cause the CS selector value to be changed).
总感觉还是没说明白,应该还有很多故事,有兴趣的可以挖一下。
参考文档
- https://qemu.weilnetz.de/doc/qemu-doc.html
- 《linux内核完全剖析》
- Intel® 64 and IA-32 Architectures Software Developer’s Manual
网友评论