MIT6.828 BootLoader代码分析

#include <inc/mmu.h>

;bootloader完成了16位模式的初始化,包括gdt,ds,es,ss等 , 然后进入保护模式并初始化, 然后call main.c
# Start the CPU: switch to 32-bit protected mode, jump into C.
# The BIOS loads this code from the first sector of the hard disk into
# memory at physical address 0x7c00 and starts executing in real mode
# with %cs=0 %ip=7c00.

.set PROT_MODE_CSEG, 0x8         # kernel code segment selector 内核代码段, .SET是给存在符号表中符号赋值的
.set PROT_MODE_DSEG, 0x10        # kernel data segment selector 内核数据段
.set CR0_PE_ON,      0x1         # protected mode enable flag 启动保护模式标识位


; /*
; .globl _start  系统复位位置,整个程序入口
; _start是GNU汇编器的默认入口标签,
; .globl将_start声明为外部程序可访问的标签,
; .globl是GNU汇编的保留关键字,前面加点是GNU汇编的语法
; */
.globl start
start:
  .code16                     # Assemble for 16-bit mode  使用 .code16 指令让汇编器将程序汇编成 16 位的代码
  cli                         # Disable interrupts [置中断标识位,不可被打断 , 相反的是STL]
  cld                         # String operations increment  
; /*
; 在计算机中,大部分数据存放在主存 中,8086CPU提供了一组处理主存中连续存放的数据串的指令——串操作指令。
; 串操作指令中,
; 源操作数用寄存器SI寻址,默认在数据段DS中,但允许段超越;目的操作数用寄存器DI寻址,默认在附加段ES中,不允许段超越。
; 每执行一次串操作指令,作为源地址指针的SI和作为目的地址指针的DI将自动修 改:+/-1(对于字节串)或+/-2(对于字串)。
; 地址指针是增加还是减少取决于方向标志DF。在系统初始化后或者执行指令CLD指令后,DF=0,此时地址指针增1或2;在执行指令STD后,DF=1,此时地址指针减1或2。
; */


  # Set up the important data segment registers (DS, ES, SS). [设置数据段]
  xorw    %ax,%ax             # Segment number zero [置0]
  movw    %ax,%ds             # -> Data Segment [数据段寄存器]
  movw    %ax,%es             # -> Extra Segment [附加段寄存器]
  movw    %ax,%ss             # -> Stack Segment [栈段]

  # Enable A20:
  #   For backwards compatibility with the earliest PCs, physical
  #   address line 20 is tied low, so that addresses higher than
  #   1MB wrap around to zero by default.  This code undoes this.
  ; //开启A20:通过将键盘控制器上的A20线置于高电位,全部32条地址线可用,可以访问4G的内存空间;
seta20.1:
  inb     $0x64,%al               # Wait for not busy
  testb   $0x2,%al
  jnz     seta20.1

  movb    $0xd1,%al               # 0xd1 -> port 0x64
  outb    %al,$0x64

seta20.2:
  inb     $0x64,%al               # Wait for not busy
  testb   $0x2,%al
  jnz     seta20.2

  movb    $0xdf,%al               # 0xdf -> port 0x60
  outb    %al,$0x60

  # Switch from real to protected mode, using a bootstrap GDT
  # and segment translation that makes virtual addresses 
  # identical to their physical addresses, so that the 
  # effective memory map does not change during the switch.
  lgdt    gdtdesc 
  ;载入全局描述符表GDT,GDTR寄存器中保存了GDT的32位基地址和16位偏移
  ;LGDT和SGDT指令用来分别装载和保存GDTR寄存器
  movl    %cr0, %eax
  orl     $CR0_PE_ON, %eax 
  ; 将cr0的PE置1 , 进入保护模式 
  ; cr0是控制寄存器,里面的32个标志位代表了控制信息 , 第0个位置PE就代表了是否开启保护模式
  ; PE为1代表启动保护模式,启动段机制;PG为分页机制启动位,当PE=PG=1时,启动分段分页机制,地址转换需要段处理和页处理
  movl    %eax, %cr0
  
  # Jump to next instruction, but in 32-bit code segment.
  # Switches processor into 32-bit mode.
  ljmp    $PROT_MODE_CSEG, $protcseg

  .code32                     # Assemble for 32-bit mode [按照32位编码]
protcseg:
  # Set up the protected-mode data segment registers 
  movw    $PROT_MODE_DSEG, %ax    # Our data segment selector
  movw    %ax, %ds                # -> DS: Data Segment
  movw    %ax, %es                # -> ES: Extra Segment
  movw    %ax, %fs                # -> FS
  movw    %ax, %gs                # -> GS
  movw    %ax, %ss                # -> SS: Stack Segment
  
  # Set up the stack pointer and call into C.
  movl    $start, %esp
  call bootmain ; 加载c代码

  # If bootmain returns (it shouldn't), loop.
spin:
  jmp spin

# Bootstrap GDT
.p2align 2                                # force 4 byte alignment
gdt:
  SEG_NULL				# null seg 空段
  SEG(STA_X|STA_R, 0x0, 0xffffffff)	# code seg 代码段
  SEG(STA_W, 0x0, 0xffffffff)	        # data seg 数据段

gdtdesc:
  .word   0x17                            # sizeof(gdt) - 1
  .long   gdt                             # address gdt
  ;.word就是在这个地方放一个值。相当于在这里定义一个数据变量,用.word定义了一个16bit的数据。  .long类似

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值