kernel的入口地址是/sys/i386/i386/locore.s中定义的btext:
200 /**********************************************************************
201 *
202 * This is where the bootblocks start us, set the ball rolling...
203 *
204 */
205 NON_GPROF_ENTRY(btext)
从/boot/kernel/kernel中可以读出btext的链接地址:
# readelf -a /boot/kernel/kernel | grep btext
6870: c0458a30 0 FUNC GLOBAL DEFAULT 5 btext
26381: c0458a30 0 FUNC GLOBAL DEFAULT 5 btext
因此,在物理地址0x458a30处设置断点,单步跟踪locore.s中的初始化代码。程序运行至此
的cpu主要寄存器的内容如下:
rax: 0x00000000:00458a30 rcx: 0x00000000:a0200000
rdx: 0x00000000:000488a0 rbx: 0x00000000:00458a30
rsp: 0x00000000:0009e844 rbp: 0x00000000:00094884
rsi: 0x00000000:000610e4 rdi: 0x00000000:0005b9cc
r8 : 0x00000000:00000000 r9 : 0x00000000:00000000
r10: 0x00000000:00000000 r11: 0x00000000:00000000
r12: 0x00000000:00000000 r13: 0x00000000:00000000
r14: 0x00000000:00000000 r15: 0x00000000:00000000
rip: 0x00000000:00458a30
eflags 0x00000002
首先是向0x472写入0x1234,告知bios下次为热引导:
216 /* Tell the bios to warmboot next time */
217 movw $0x1234,0x472
构建一个新的栈帧:
220 /* Set up a real frame in case the double return in newboot is executed. */
221 pushl %ebp
222 movl %esp, %ebp
此时cpu主要寄存器的内容如下:
rax: 0x00000000:00458a30 rcx: 0x00000000:a0200000
rdx: 0x00000000:000488a0 rbx: 0x00000000:00458a30
rsp: 0x00000000:0009e840 rbp: 0x00000000:0009e840
rsi: 0x00000000:000610e4 rdi: 0x00000000:0005b9cc
r8 : 0x00000000:00000000 r9 : 0x00000000:00000000
r10: 0x00000000:00000000 r11: 0x00000000:00000000
r12: 0x00000000:00000000 r13: 0x00000000:00000000
r14: 0x00000000:00000000 r15: 0x00000000:00000000
rip: 0x00000000:00458a3c
eflags 0x00000002
将PSL_KRENEL赋给eflags:
224 /* Don't trust what the BIOS gives for eflags. */
225 pushl $PSL_KERNEL
226 popfl
PSL_KERNEL是在/sys/i386/include/psl.h中定义的:
60 /*
61 * The i486 manual says that we are not supposed to change reserved flags,
62 * but this is too much trouble since the reserved flags depend on the cpu
63 * and setting them to their historical values works in practice.
64 */
65 #define PSL_RESERVED_DEFAULT 0x00000002
66
67 /*
68 * Initial flags for kernel and user mode. The kernel later inherits
69 * PSL_I and some other flags from user mode.
70 */
71 #define PSL_KERNEL PSL_RESERVED_DEFAULT
72 #define PSL_USER (PSL_RESERVED_DEFAULT | PSL_I)
将ds的内容赋给fs和gs:
228 /*
229 * Don't trust what the BIOS gives for %fs and %gs. Trust the bootstrap
230 * to set %cs, %ds, %es and %ss.
231 */
232 mov %ds, %ax
233 mov %ax, %fs
234 mov %ax, %gs
236 /*
237 * Clear the bss. Not all boot programs do it, and it is our job anyway.
238 *
239 * XXX we don't check that there is memory for our bss and page tables
240 * before using it.
241 *
242 * Note: we must be careful to not overwrite an active gdt or idt. They
243 * inactive from now until we switch to new ones, since we don't load any
244 * more segment registers or permit interrupts until after the switch.
245 */
246 movl $R(end),%ecx
247 movl $R(edata),%edi
248 subl %edi,%ecx
249 xorl %eax,%eax
250 cld
251 rep
252 stosb
根据readelf -a kernel的结果,end的地址是0xc0c06020,由于KERNBASE是
0xc0000000,此处赋给ecx的就是0xc06020。edata的地址是0xc0bab9a0,这实际上就是
.bss段的起始地址,此处赋给edi的就是0xbab9a0。ecx减去edi之后的内容是0x5a680,
这是从.bss段起始地址到end地址之间的字节数。随后将eax清0,作为后续清0操作的
写入值。cld保证edi递增变化。stosb将al的内容写入edi指向的位置。这段代码从
0xc0bab9a0开始连续写入0x5a680个字节的0,从而实现将.bss段清0的目的。
253
254 call recover_bootinfo
255
调用recover_bootinfo获取由loader传入的引导信息。
487 movl 28(%ebp),%ebx /* &bootinfo.version */
488 movl BI_VERSION(%ebx),%eax
489 cmpl $1,%eax /* We only understand version 1 */
490 je 1f
491 movl $1,%eax /* Return status */
492 leave
493 /*
494 * XXX this returns to our caller's caller (as is required) since
495 * we didn't set up a frame and our caller did.
496 */
497 ret
locore.s入口btext的调用格式为(*btext)(howto, bootdev, 0, 0, 0, &bootinfo),在bootinfo
之后压栈的有5个参数,占20个字节,在加上返回地址和在调用recover_bootinfo之前压栈的ebp,
一共有28个字节,因此从当前ebp位置上溯28个字节就是bootinfo结构体的起始地址。上述代码
从bootinfo结构体中取出bi_version字段的内容,判断其是否为1,仅当版本为1时才继续处理。
500 /*
501 * If we have a kernelname copy it in
502 */
503 movl BI_KERNELNAME(%ebx),%esi
推荐给好友 上一篇 | 下一篇
/sys/i386/i386/locore.s分析笔记
字号: 小 中 大 | 打印 发布: 2008-8-27 07:39 作者: 剑心通明 来源: 互联网 查看: 209次
