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

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.dtcms.com/a/365629.html

相关文章:

  • Alpha World赞助Hello Blockchain Thailand,AWT成为全球共识焦点
  • 袋鼠云产品功能更新报告14期|实时开发,效率再升级!
  • 【IQA技术专题】NIQE代码讲解
  • VMWare上搭建分布式Hadoop集群
  • STM32F103按钮实验
  • 大语言模型领域最新进展
  • 笔记:卷积神经网络(CNN)
  • rust学习之开发环境
  • 从 0 到 1 吃透 Nacos:服务发现与配置中心的终极实践指南
  • 阅兵时刻,耐达讯自动化RS485 转 Profinet 网关助力矿山冶金连接迈向辉煌
  • BurpSuite_Pro_V2024.6使用教程-Burp Suite代理设置详解
  • 张琦《认知破局》读书笔记
  • 内存保护单元MPU
  • 用资产驱动方法构建汽车网络安全档案
  • 中科米堆CASAIM自动化三维测量设备测量汽车零部件尺寸质量控制
  • php:PHP 8 新特性深度解析与实战应用:提升开发效率的关键技巧
  • 2025全球及中国汽车VDA电池模组:头部企业市场排名与占有率独家揭晓
  • 视频打不开怎么办?教你一键修改默认打开方式
  • Java全栈工程师的面试实战:从基础到复杂问题的完整解析
  • Jira vs. GitLab Issues vs. Redmine:终极选型与成本分析
  • 金牛区好办公室国际数字影像产业园企业服务
  • 《深入解析:Kubernetes网络策略冲突导致的跨节点服务故障排查全过程》
  • Streamable HTTP
  • 《录井工程与管理》——第四章单井地质剖面建立录井技术
  • 新手向:JavaScript性能优化实战
  • 免费的PDF工具箱软件,免费PDF转word,PDF合并,PDF24下载,24个功能
  • JVM调优与常见参数(如 -Xms、-Xmx、-XX:+PrintGCDetails) 的必会知识点汇总
  • RPA行业的主要玩家有哪些?
  • 告别剪辑烦恼!3个超实用技巧,让你的视频瞬间高级起来
  • 计算机视觉与深度学习 | 深度学习图像匹配算法在不同纹理复杂度场景下的鲁棒性和计算效率评估方法