学习笔记
《x86汇编语言:从实模式到保护模式》
https://www.jianshu.com/p/d481cb547e9f
完整原版源码见配书代码包
c14_core.asm
内核程序 c14_core.asm 程序流程

内核程序 c14_core.asm 流程部分(增加注释)
start:
mov ecx,core_data_seg_sel ;使ds指向核心数据段
mov ds,ecx
mov ebx,message_1
call sys_routine_seg_sel:put_string
;显示处理器品牌信息
mov eax,0x80000002
cpuid
mov [cpu_brand + 0x00],eax
mov [cpu_brand + 0x04],ebx
mov [cpu_brand + 0x08],ecx
mov [cpu_brand + 0x0c],edx
mov eax,0x80000003
cpuid
mov [cpu_brand + 0x10],eax
mov [cpu_brand + 0x14],ebx
mov [cpu_brand + 0x18],ecx
mov [cpu_brand + 0x1c],edx
mov eax,0x80000004
cpuid
mov [cpu_brand + 0x20],eax
mov [cpu_brand + 0x24],ebx
mov [cpu_brand + 0x28],ecx
mov [cpu_brand + 0x2c],edx
mov ebx,cpu_brnd0 ;显示处理器品牌信息
call sys_routine_seg_sel:put_string
mov ebx,cpu_brand
call sys_routine_seg_sel:put_string
mov ebx,cpu_brnd1
call sys_routine_seg_sel:put_string
;安装 为整个系统服务的调用门 设置调用门的特权级 DPL=3
mov edi,salt ;C-SALT表的起始位置
mov ecx,salt_items ;C-SALT表的条目数量
.b3:
push ecx
mov eax,[edi+256] ;组成偏移地址
mov bx,[edi+260] ;组成段选择子的入口地址
mov cx,1_11_0_1100_000_00000B ;调用门描述符 特权级 DPL=3
call sys_routine_seg_sel:make_gate_descriptor
call sys_routine_seg_sel:set_up_gdt_descriptor
mov [edi+260],cx ;将返回的门描述符选择子回填
add edi,salt_item_len
pop ecx
loop .b3
;测试门的安装
mov ebx,message_2
call far [salt_1+256] ;内核程序CPL=0 本条语句指向的选择子RPL=3 对应的门描述符DPL=3
mov ebx,message_3
call sys_routine_seg_sel:put_string
;TCB 任务控制块
mov ecx,0x46
call sys_routine_seg_sel:allocate_memory
call append_to_tcb_link ;将任务控制块追加到TCB链表
;调用子程序
push dword 50 ;用户程序位于硬盘LBA逻辑扇区号50
push ecx ;压入任务控制块起始线性地址
call load_relocate_program
mov ebx,do_status
call sys_routine_seg_sel:put_string
mov eax,mem_0_4_gb_seg_sel
mov ds,eax
ltr [ecx+0x18] ;加载LSS
lldt [ecx+0x10] ;加载LDT
;切换到用户程序头部段
mov eax,[ecx+0x44]
mov ds,eax
;假装从调用门返回
push dword [0x08] ;SS
push dword 0 ;eip=0
push dword [0x14] ;CS
push dword [0x10] ;eip
retf
return_point: ;用户程序返回点
mov eax,core_data_seg_sel ;因为c14.asm是以JMP的方式使用调
mov ds,eax ;用门@TerminateProgram,回到这
;里时,特权级为3,会导致异常。
mov ebx,message_6
call sys_routine_seg_sel:put_string
hlt
调用门描述符格式
安装调用门后的GDT布局

子程序 load_relocate_program 流程以及源码
- 流程
- 源码
网友评论