第十五章 习题2解答:切换任务 core->A->B->core
第十五章 习题2解答:完整源码
准备文件.lst(.lst文件的汇编地址就是定断点时的段内偏移量)
-
.lst
文件根据同名.asm
文件由配书工具nasmide.exe生成
c13_mbr.lst
ex15-2_core.lst
ex15-2_A.lst
ex15-2_B.lst
Bochs 调试命令(以及配置方法)
-
s
: 单步执行 -
b
: 断点设置 -
c
: 继续执行 -
r
: 查看寄存器 -
sreg
: 查看段寄存器 -
xp
: 查看内存
调试思路
- 程序运行的整个阶段(按照逻辑顺序由先到后,断点设差不多都设置在call 或jmp所在代码行):
- 加载程序→内核程序;
- 内核程序→任务A;
- 任务A通过调用门切换到→任务B;
- 任务B通过调用门返回到→内核程序;
- 内核程序继续执行直到停机。
Bochs调试过程
-
0、
<bochs:n> ABCD
,ABCD
就是实际输入的bochs指令(按下回车出确认输入),其余的是bochs的输出信息(段选择子之类的); -
1、从加载程序mbr到内核程序core
<bochs:1> s
<bochs:2> b 0x7c00
<bochs:3> c
<bochs:4> b 0x7d38
<bochs:5> c
(0) Breakpoint 2, 0x0000000000007d38 in ?? ()
Next at t=17419248
(0) [0x000000007d38] 0010:0000000000000138 (unk. ctxt): jmpf ds:[edi+16] ; ff6f10
<bochs:6> s
Next at t=17419249
(0) [0x0000000416c6] 0038:00000000000003fa (unk. ctxt): mov ecx, 0x00000030 ; b930000000
- 2、从内核程序代码段(选择子
0x38
)切换到任务A
<bochs:7> b 0x0038:0x057c
<bochs:8> c
(0) Breakpoint 3, 0x0000000000041848 in ?? ()
Next at t=17454245
(0) [0x000000041848] 0038:000000000000057c (unk. ctxt): jmpf es:[ecx+20] ; 26ff6914
<bochs:9> s
Next at t=17454246
(0) [0x000000104d1c] 000f:0000000000000000 (unk. ctxt): mov ax, ds ; 8cd8
- 3、执行到任务A,执行调用门的语句
call far[fs:TaskSwitch]
<bochs:10> b 0x000f:0x002f
<bochs:11> c
(0) Breakpoint 4, 0x0000000000104d4b in ?? ()
Next at t=17462166
(0) [0x000000104d4b] 000f:000000000000002f (unk. ctxt): mov ebx, dword ptr fs:0x00000428 ; 648b1d28040000
<bochs:12> s
Next at t=17462167
(0) [0x000000104d52] 000f:0000000000000036 (unk. ctxt): callf fs:0x00000328 ; 64ff1d28030000
- 4、通过调用门进入到内核程序公用例程段的子程序
taskswitch
,并切换到任务B
<bochs:13> s
Next at t=17462168
(0) [0x000000040249] 0028:0000000000000231 (unk. ctxt): mov eax, 0x00000008 ; b808000000
<bochs:14> s
Next at t=17462169
(0) [0x00000004024e] 0028:0000000000000236 (unk. ctxt): mov es, ax ; 8ec0
<bochs:15> s
Next at t=17462170
(0) [0x000000040250] 0028:0000000000000238 (unk. ctxt): jmpf es:[ebx+20] ; 26ff6b14
<bochs:16> s
Next at t=17462171
(0) [0x0000001005cc] 000f:0000000000000000 (unk. ctxt): mov ax, ds ; 8cd8
- 5、执行到任务B的调用门返回语句
call far [fs:TerminateProgram]
,并返回到程序管理器core的子程序terminate_current_task
<bochs:17> b 0x000f:0x002f
<bochs:18> c
(0) Breakpoint 5, 0x00000000001005fb in ?? ()
Next at t=17470091
(0) [0x0000001005fb] 000f:000000000000002f (unk. ctxt): callf fs:0x00000128 ; 64ff1d28010000
<bochs:19> s
Next at t=17470092
(0) [0x000000040212] 0028:00000000000001fa (unk. ctxt): pushf ; 9c
- 6、单步调试
terminate_current_task
里面的语句,terminate_current_task
位于公用例程段(选择子0x28
)
<bochs:20> s
Next at t=17470093
(0) [0x000000040213] 0028:00000000000001fb (unk. ctxt): mov edx, dword ptr ss:[esp] ; 8b1424
<bochs:21> s
Next at t=17470094
(0) [0x000000040216] 0028:00000000000001fe (unk. ctxt): add esp, 0x00000004 ; 81c404000000
<bochs:22> s
Next at t=17470095
(0) [0x00000004021c] 0028:0000000000000204 (unk. ctxt): mov eax, 0x00000030 ; b830000000
<bochs:23>
Next at t=17470096
(0) [0x000000040221] 0028:0000000000000209 (unk. ctxt): mov ds, ax ; 8ed8
<bochs:24> s
Next at t=17470097
(0) [0x000000040223] 0028:000000000000020b (unk. ctxt): test dx, 0x4000 ; 66f7c20040
<bochs:25> s
Next at t=17470098
(0) [0x000000040228] 0028:0000000000000210 (unk. ctxt): jnz .+18 (0x0004023c) ; 7512
<bochs:26> s
Next at t=17470099
(0) [0x00000004022a] 0028:0000000000000212 (unk. ctxt): mov ebx, 0x00000ff1 ; bbf10f0000
<bochs:27> s
Next at t=17470100
(0) [0x00000004022f] 0028:0000000000000217 (unk. ctxt): callf 0x0028:00000000 ; 9a000000002800
<bochs:28> s
Next at t=17470101
(0) [0x000000040018] 0028:0000000000000000 (unk. ctxt): push ecx ; 51
<bochs:29> s
Next at t=17470102
(0) [0x000000040019] 0028:0000000000000001 (unk. ctxt): mov cl, byte ptr ds:[ebx] ; 8a0b
<bochs:30> s
Next at t=17470103
(0) [0x00000004001b] 0028:0000000000000003 (unk. ctxt): or cl, cl ; 08c9
<bochs:31> b 0x0028:0x021e
<bochs:32> c
(0) Breakpoint 6, 0x0000000000040236 in ?? ()
Next at t=17482903
(0) [0x000000040236] 0028:000000000000021e (unk. ctxt): jmpf ds:0x00000e2d ; ff2d2d0e0000
<bochs:33> s
Next at t=17482904
(0) [0x00000004184c] 0038:0000000000000580 (unk. ctxt): mov ebx, 0x00000f2f ; bb2f0f0000
<bochs:34> s
Next at t=17482905
(0) [0x000000041851] 0038:0000000000000585 (unk. ctxt): callf 0x0028:00000000 ; 9a000000002800
<bochs:35> s
Next at t=17482906
(0) [0x000000040018] 0028:0000000000000000 (unk. ctxt): push ecx ; 51
- 8、回到了内核代码段(选择子
0x38
),最终执行停机指令hlt
<bochs:36> b 0x0038:0x058c
<bochs:37> c
(0) Breakpoint 7, 0x0000000000041858 in ?? ()
Next at t=17487518
(0) [0x000000041858] 0038:000000000000058c (unk. ctxt): hlt ; f4
网友评论