Upload
ramya
View
170
Download
13
Embed Size (px)
DESCRIPTION
基于 i386 体系结构的 Linux 启动代码分析. [email protected]. 启动代码文件. linux/arch/i386/boot/bootsect.S linux/arch/i386/boot/setup.S linux/arch/i386/boot/compressed/head.S linux/arch/i386/kernel/head.S linux/arch/i386/kernel/init_task.c linux/arch/init/main.c. 实模式. 实模式是为了兼容早期的 CPU 而设置的 系统总是始于实模式 实模式下 - PowerPoint PPT Presentation
Citation preview
[email protected] Embedded Operating Systems 2
启动代码文件 linux/arch/i386/boot/bootsect.S linux/arch/i386/boot/setup.S linux/arch/i386/boot/compressed/head.S linux/arch/i386/kernel/head.S linux/arch/i386/kernel/init_task.c linux/arch/init/main.c
[email protected] Embedded Operating Systems 3
实模式 实模式是为了兼容早期的 CPU 而设置的 系统总是始于实模式 实模式下
地址总线: 20 位 内存范围: 0~1MB 逻辑地址 = 段地址 + 段内偏移
段地址 = 段寄存器中的值 *16 (或左移 4 位) 段寄存器长度: 16bit
[email protected] Embedded Operating Systems 4
I386实模式下的内存布局图
RAM1-MB
ROM-BIOS
VIDEO-BIOS
VRAM0xA0000
0xC0000
0xF0000
0x00000
[email protected] Embedded Operating Systems 5
软盘和硬盘启动 软盘启动, bootsect.S
0x7c000x90000 0x7c00, BIOS 0x90000, lilo
堆栈, 0x3ff4(0x4000-12), 向下增长 磁盘参数表, 12Bytes , 0x3ff4~0x40
00 显示“ Loading” Setup0x90200 系统
小内核, 0x10000 ( 64KB 处),低装载
大内核, 0x100000 ( 1MB 处),高装载
setup
硬盘启动,两阶段引导 装载 LILO ( LInuxLOader )
第一个扇区 …
装载 LINUX Bootsect.S0x90000 Setup.S0x90200 系统
0x10000 0x100000
跳转到 setup
[email protected] Embedded Operating Systems 6
Setup
初始化硬件设备并为内核程序的执行建立环境 内存检测 键盘 视频 磁盘控制器 IBM 微通道总线 MCA PS/2 设备(总线鼠标) APM BIOS
若低装载,将系统移动到 0x1000 处( 4KB 处)否则,不必
临时 IDT 和临时 GDT FPU PIC, 16 个硬件中断中断
向量 32~47 实模式保护模式 Startup_32
[email protected] Embedded Operating Systems 7
Compressed/head.S
Startup_32 初始化段寄存器和一个临时堆栈 初始化 BSS 段 解压缩
高装载或低装载解压缩 0x100000 ( 1MB ) 跳转到 0x100000 处
[email protected] Embedded Operating Systems 8
Head.S
Startup_32 初始化段寄存器 建立进程 0 的内核堆栈 Setup_idt 拷贝系统参数 识别处理器 GDT 、 IDT Start_kernel
[email protected] Embedded Operating Systems 9
The system-segment registers
In protected-mode the CPU needs quick access to some important data-structures, such as: Memory-Segment Descriptors Interrupt-Gate Descriptors Call-Gate Descriptors Task-State Descriptors Page-Directory and Page-Table Descriptors
Special CPU registers locate these items
[email protected] Embedded Operating Systems 10
GDT and IDT
Two most vital system registers for protected-mode execution are: GDTR (Global Descriptor Table Register) IDTR (Interrupt Descriptor Table Register)
48-bits widebase-address + segment-limit (an array of descriptors, the GDT & the IDT)
Special instructions SGDT/LGDT, SIDT/LIDT
Segment Base-AddressSegment
Limit
47 16 15 0
16 bits32 bits
[email protected] Embedded Operating Systems 11
System Relationships
descriptordescriptordescriptordescriptordescriptordescriptordescriptor
descriptordescriptordescriptordescriptordescriptordescriptordescriptor
descriptordescriptordescriptordescriptordescriptordescriptor
Interrupt Descriptor Table
Global Descriptor Table
GDTR
IDTR
[email protected] Embedded Operating Systems 12
LDT and TSS
For protected-mode multitasking, the CPU needs to access two other data-structures: The current Local Descriptor Table (LDT) The current Task-State Segment (TSS)
Again, special registers tell the CPU where to find these data-structures in memory (assuming protected-mode is enabled)
Instructions: SLDT/LLDT, STR/LTR
[email protected] Embedded Operating Systems 14
CR0 CR0, MSW register (Machine Status Word, 32-bit version) Contains system control flags that control operating mode and states of
the processor
PE-bit (Protection Enabled)0 CPU is in real-mode, 1 CPU is in protected-mode
Instruction: lmsw LINUX’ setup.S:
movw $1, %ax lmsw %ax jmp flush_instr // why? flush_instr:
To turn on the PE-bit (enables protected-mode),
[email protected] Embedded Operating Systems 15
CR1、 CR2、 CR3 CR1
Reserved CR2
Contains the page-fault linear address (the linear address that caused a page fault)
CR3 Contains the physical address of the base of the page directory and two
flags (PCD & PWT)
[email protected] Embedded Operating Systems 16
Segment Descriptor Format
Base[31..24] G DRSV
AVL
Limit[19..16]
PDPL
S XC/D
R/
WA Base[23..16]
Base[15..0] Limit[15..0]
31 16
15 0
[email protected] Embedded Operating Systems 17
“Hidden” part of Segment Registers
selector Segment base Segment limitAccessrights
The programmer-visible part of a segment-register
The “invisible” parts of a segment-register
index TI RPL
2 1 015
Segment selector
[email protected] Embedded Operating Systems 18
Three VRAM zones
GRAPHICS
MONOCHROME TEXT
COLOR TEXT
64-KB
32-KB
32-KB
0xA0000
0xB0000
0xB8000
[email protected] Embedded Operating Systems 19
Array of picture-elements
Text-mode VRAM is organized as an array
Each array-element occupies one word
Word’s LSB holds ascii character-code
Word’s MSB holds a color-number pair
bgcolor fgcolor ASCII character-code
15 12 11 8 7 0
bytenybble nybble
[email protected] Embedded Operating Systems 20
Color-Attribute Byte
Blink R G B Intense G BR
foreground color attribute
background color attribute
[email protected] Embedded Operating Systems 21
Screen-element locations
80 columns
25rows
characters 0..79
characters 80..159
Video screen
characters 1920..1999
[email protected] Embedded Operating Systems 22
逻辑地址 段:偏移量
线性地址 32 位无符号整数, 0~4G-1, 即 0x00000000~0xFFFFFFFF
物理地址 芯片级内存单元寻址
地址转换过程
内存寻址
物理地址线性地址分段单元 分页单元逻辑地址
[email protected] Embedded Operating Systems 23
实模式?保护模式?分页? Real mode
Exists mostly to maintain processor compatibility with older models, &
To allow the OS to bootstrap
[email protected] Embedded Operating Systems 25
__KERNEL_CS __KERNEL_CS
0x10 = 0000 0000 0001 0000b
Index(0x2), TI(0), RPL(0, highest)
limit(0xFFFFF), base(0),
G(1, =4096), D(1,=32 位 ),
P(1, in main memory), DPL(0, hignest), S(1),
type(0xA) E(1), executable, code
C(0), not current
R(1), readable
A(0), unaccessed
gdt: .word 0, 0, 0, 0 # dummy
.word 0, 0, 0, 0 # unused
.word 0xFFFF # 4Gb .word 0 # base address = 0
.word 0x9A00 # code read/exec
.word 0x00CF # granularity(4096)
.word 0xFFFF # 4Gb
.word 0 # base address = 0
.word 0x9200 # data read/write
.word 0x00CF # granularity(4096)
[email protected] Embedded Operating Systems 27
[email protected] Embedded Operating Systems 28
逻辑地址的转换Segment selectorLogical Address: Segment-offset
Operand’s effective addressPhysical Address:
descriptor
descriptor
descriptor
descriptor
Segment Descriptor Table
+Segment Base-address
(also Segment-Limit and Access Rights)
Validity is checked by CPU
[email protected] Embedded Operating Systems 29
分页 PG
Paging (bit 31 of CR0). 1 Enables paging with PE=1,
Linear address space is divided into fixed-size pages (4KB, 2MB, or 4MB) that can be mapped into physical memory and/or disk storage using paging mechanism.
0 Disables paging, linear addresses = physical addresses.