当前位置: 首页 > ds >正文

linux0.12 head.s代码解析

  1. 重新设置IDT和GDT,为256个中断门设置默认的中断处理函数
  2. 检查A20地址线是否启用
  3. 设置数学协处理器
  4. 将main函数相关的参数压栈
  5. 设置分页机制,将页表映射到0~16MB的物理内存上
  6. 返回main函数执行

源码详细注释如下:

/**  linux/boot/head.s**  (C) 1991  Linus Torvalds*//**  head.s contains the 32-bit startup code.** NOTE!!! Startup happens at absolute address 0x00000000, which is also where* the page directory will exist. The startup code will be overwritten by* the page directory.*/
.text
.globl _idt,_gdt,_pg_dir,_tmp_floppy_area
_pg_dir:
startup_32:
# 段寄存器先指向setup.S中的数据段movl $0x10,%eaxmov %ax,%dsmov %ax,%esmov %ax,%fsmov %ax,%gslss _stack_start,%esp # 设置栈指针和栈段寄存器
# 设置IDT和GDTcall setup_idtcall setup_gdt
# 重新加载段寄存器和栈movl $0x10,%eaxmov %ax,%dsmov %ax,%esmov %ax,%fsmov %ax,%gslss _stack_start,%esp
# 检查A20地址线是否启用xorl %eax,%eax
1:	incl %eax            # check that A20 really IS enabledmovl %eax,0x000000   # loop forever if it isn'tcmpl %eax,0x100000je 1b
/** NOTE! 486 should set bit 16, to check for write-protect in supervisor* mode. Then it would be unnecessary with the "verify_area()"-calls.* 486 users probably want to set the NE (#5) bit also, so as to use* int 16 for math errors.*/movl %cr0,%eax          # check math chipandl $0x80000011,%eax   # 清楚其他位,保留PG PE ET位orl $2,%eax             # set MP位,表示启用数学协处理器movl %eax,%cr0          # 写回CR0寄存器call check_x87jmp after_page_tables# 设置协处理器
check_x87:fninit                  # 初始化协处理器fstsw %ax               # 取协处理器状态字到ax寄存器cmpb $0,%alje 1f                   # 如果协处理器状态字为0,则有协处理器,跳转1movl %cr0,%eaxxorl $6,%eax            # 清除MP、设置EM位movl %eax,%cr0ret
.align 2
1:	.byte 0xDB,0xE4         # 等价于fsetpm,用于设置协处理器模式retsetup_idt:
/** %eax:* 位31-16: 0x0008 (段选择子)* 位15-0:  ignore_int地址的低16位* %edx:* 位31-16: 0x8E00 (中断门属性)* 位15-0:  ignore_int地址的高16位
*/lea ignore_int,%edxmovl $0x00080000,%eaxmovw %dx,%axmovw $0x8E00,%dxlea _idt,%edi          # edi指向IDT的基地址mov $256,%ecx          # 256个中断门
rp_sidt:movl %eax,(%edi)movl %edx,4(%edi)addl $8,%edi           # 移动到下一个中断门描述符dec %ecx               # 循环计数器减1jne rp_sidtlidt idt_descr         # 加载IDT描述符ret/**  setup_gdt**  This routines sets up a new gdt and loads it.*  Only two entries are currently built, the same*  ones that were built in init.s. The routine*  is VERY complicated at two whole lines, so this*  rather long comment is certainly needed :-).*  This routine will beoverwritten by the page tables.*/
setup_gdt:lgdt gdt_descrret/** 物理地址    内容          大小      用途* 0x0000   页目录(_pg_dir)  4KB      页目录表* 0x1000   页表0(pg0)      4KB       映射0-4MB物理内存* 0x2000   页表1(pg1)      4KB       映射4-8MB物理内存  * 0x3000   页表2(pg2)      4KB       映射8-12MB物理内存* 0x4000   页表3(pg3)      4KB       映射12-16MB物理内存* 0x5000   软盘缓冲区      4KB       软盘DMA缓冲区*/
.org 0x1000
pg0:
.org 0x2000
pg1:
.org 0x3000
pg2:
.org 0x4000
pg3:
.org 0x5000
/** tmp_floppy_area is used by the floppy-driver when DMA cannot* reach to a buffer-block. It needs to be aligned, so that it isn't* on a 64kB border.*/
_tmp_floppy_area:.fill 1024,1,0after_page_tables:pushl $0pushl $0pushl $0               # 这些是main函数的参数pushl $L6              # 如果main函数决定返回,则返回地址为L6pushl $_main           # main函数地址压栈jmp setup_paging       # 跳转设置分页机制
L6:jmp L6                 # main应该永远不会返回,以防万一,我们不知道会发生什么# 默认中断处理程序
int_msg:.asciz "Unknown interrupt\n\r"
.align 2
ignore_int:pushl %eaxpushl %ecxpushl %edxpush %dspush %espush %fsmovl $0x10,%eaxmov %ax,%dsmov %ax,%esmov %ax,%fspushl $int_msgcall _printkpopl %eaxpop %fspop %espop %dspopl %edxpopl %ecxpopl %eaxiret.align 2
setup_paging:
# 清零页目录和页表区域movl $1024*5,%ecxxorl %eax,%eaxxorl %edi,%edicld;rep;stosl
# 设置页目录项movl $pg0+7,_pg_dirmovl $pg1+7,_pg_dir+4movl $pg2+7,_pg_dir+8movl $pg3+7,_pg_dir+12
# 通过循环将页表映射到物理内存,从高地址向低地址填充movl $pg3+4092,%edimovl $0xfff007,%eaxstd
1:	stoslsubl $0x1000,%eaxjge 1bxorl %eax,%eaxmovl %eax,%cr3          # 设置CR3寄存器,指向页目录的起始地址movl %cr0,%eaxorl $0x80000000,%eaxmovl %eax,%cr0          # 启用分页机制ret                     # 由于之前压栈了main,返回main函数执行.align 2
.word 0
idt_descr:.word 256*8-1		# idt contains 256 entries.long _idt
.align 2
.word 0
gdt_descr:.word 256*8-1		# so does gdt (not that that's any.long _gdt		# magic number, but it works for me :^).align 3
_idt:	.fill 256,8,0		# idt is uninitialized_gdt:	.quad 0x0000000000000000	/* NULL descriptor */.quad 0x00c09a0000000fff	    /* 内核代码段 16Mb */.quad 0x00c0920000000fff	    /* 内核数据段 16Mb */.quad 0x0000000000000000	    /* 暂时未使用 */.fill 252,8,0			        /* space for LDT's and TSS's etc */
http://www.xdnf.cn/news/20061.html

相关文章:

  • Langchain4j 整合MongoDB 实现会话持久化存储详解
  • Day34 UDP套接字编程 可靠文件传输与实时双向聊天系统
  • HTML5圣诞网站源码
  • Python基础(①①Ctypes)
  • Web安全——JWT
  • 厦门创客匠人靠谱嘛?从内容交付能力看其核心优势
  • el-tree 点击父节点无效,只能选中子节点
  • [BUUCTF-OGeek2019]babyrop详解(包含思考过程)
  • C++:类和对象(上)
  • 微软rStar2-Agent:新的GRPO-RoC算法让14B模型在复杂推理时超越了前沿大模型
  • 卷积操作原来分3种
  • 2025年工科生转型必考的十大高含金量证书!
  • 腾讯云建站多少钱?2025年最新价格曝光,0基础也能做出专业网站?实测真假
  • flutter专栏--深入剖析你的第一个flutter应用
  • 从一次Crash分析Chromium/360浏览器的悬空指针检测机制:raw_ref与BackupRefPtr揭秘
  • 留学第一天,语言不通怎么办?同声传译工具推荐来了
  • 常用假设检验方法及 Python 实现
  • 亚马逊云代理商:配置安全组规则步骤
  • kafka Partition(分区)详解
  • nestjs 阿里云服务端签名
  • 深度学习篇---SGD+Momentum优化器
  • Photoshop - Photoshop 触控手势
  • 电表连网不用跑现场!耐达讯自动化RS485转Profinet网关 远程配置+技术支持,真能做到!
  • ASP.NET 实战:用 SqlCommand 打造一个安全的用户注册功能
  • SIC8833芯片智能充气泵设计方案
  • 原创未发表!POD-PINN本征正交分解结合物理信息神经网络多变量回归预测模型,Matlab实现
  • 第二家公司虽然用PowerBI ,可能更适合用以前的QuickBI
  • pip completion工具作用(生成命令行自动补全脚本)(与pip-bash-completion区别)
  • 东土智建 | 让塔吊更聪明的“四大绝技”工地安全效率双升级
  • EasyMeeting-注册登录