Professional Documents
Culture Documents
" "
:
:.
.
- .
.LATEX
c
.
/ .
.
) .(GNU
.I
Basics
.
.. . . . . . . . . . . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . . .
... ) :(- . . . . . .
... ) :(-
.
.. . . . . . . . . . . . . .
... . . . System Bus
... . . . . . . .
... . . .
.. . . . . . . . . . . . . . . . .
... . . . . .
... . CPU Modes
... . . Real Mode
... Protected Mode
... . . . . . x86
x86
.II
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Boo ng
. Bootloader
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . Bootloader
.. . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . .
... . int 0x13
.. . . . . . . . . . . . . . . . . . . . FAT12
... . . . . . . . . . . . . . . . . . FAT12
... FAT12 . . . . . . . . . . .
... . . . . . . . . . . . . . . . .
... . . . . . . . . . . FAT12
.
.
.
.
.
.
.
.
.
.
. -
.. . . . . . . . . . . . . . . . . . .
... . . . Global Descriptor Table
... . PMode Memory Addressing
... . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . A20
... 8042 . . . . . . . A20
... . . . . . . . . . . . . . . A20
.. . . . . . . . . . . . . . . . . . . . . . VGA
... . . . . . . . . . . VGA
... . . . . . . . . . . . . .
... . . . . . . . . . . . strings
... . . . . . . . . . . Hardware Cursor
... . . . . . . . . . . . Clear Screen
.. . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.III
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Kernel
.
.. . . . . .
...
.. . . . . .
... . .
.. . . . .
... Monolithic Kernel
... . . . MicroKernel
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
... . Hybrid Kernel
.. . . . . . . . . . . .
... . . . . PE
... ++
... . . . .
.. . . . . . . .
.. . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. Interrupts
.. . So ware Interrupts
... .
... .
... . . . . . . .
... GDT
.. Programmable Interrupt Controller
... . . . . . . . . Hardware Interrupts
... . . . . . . . . . . . . . . . . . PIC
.. . . . . . . . . . . . . . Programmable Interval Timer
... . . . . . . . . . . . . . . . . . . PIT
.. . . . . . . . . . . . . . . . . . . . . . . . HAL
... . . . . . . . . . . . . . . . . . . . . . PIC
... . . . . . . . . . . . . . . . . . . . . . PIT
... HAL . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
..
... . . . . . . . . .
... . . . . . Memory Map
... . . . . . . .
... . . . . . . . . .
.. Virtual Memory Management
.
.
.
.
.
.
..
Device Driver
Keyboard Driver
.
.
.
.
.
. . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . Assembly Language ..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . Smallest Bootloader
. . Welcom to OS World
. . Bios Parameter Block
. . . . . . BPB example
. Hex value of bootloader
. . . Complete Example
. . . Reset Floppy Drive
Read Floppy Disk Sectors
. . . . . . Hello Stage2
. . . Load Root directory
. Find Stage2 Bootloader
. . . . . Load FAT Table
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
. . . . . . . . . . . . . . . . . GDT ..
. . . . . . . . . Load GDT into GDTR ..
. . . . . Switching to Protected Mode ..
Enable A20 by System Control Port 0x92 ..
. . . . . . Enable A20 by BIOS int 0x15 ..
. . . . . . . . . . Wait Input/Output ..
. . . . . . . Enable A20 by Send 0xdd ..
Enable A20 by write to output port of Keyboard Controller ..
. . . . . . . . . . . . . . Print 'A' character on screen ..
. . . . . . . . . . . . . . . . . . . putch32 rou ne ..
. . . . . . . . . . . . . . . . . . . . puts32 rou ne ..
. . . . . . . . . . . . . . . . Move Hardware Cursor ..
. . . . . . . . . . . . . . . . . . . . . Clear Screen ..
. . . . . . . . . . . . . . . . . . . . . Hello Kernel ..
..............
..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
..
..
..
..
..
..
..
..
..
..
..
..
..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
..
..
..
..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
..
..
..
..
..
..
..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
..
..
..
..
..
..
..
..
..
..
..
..
..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.. . . . . . . . . . . . . . . . . . . . . .
.. FAT12 . . . . . . . . . . . . . . . . . . . . .
..
..
..
..
..
. . . . . . x86
. . . . . . . . . . . .
. . . . . . . . . . . . . .
Step Reckoner . . .
.
. . . . .
Z1
Atanaso Iowa State
. . . . . . . . . . . . . . . Harvard Mark I
. . . . . . . . .
colossus . . . . . . .
. . . . . . . . . . . . . . . . . . ENIAC
. . . . . . . . . . . . . . . . . . EDVAC
. . . . . . x86
x86
. . . . . . . . . .
. .
. . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.. . . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . .
..
..
..
..
..
..
. . . . . . PIC
. . . . 8253
. . . . . . . PIT
HAL
. .
8259A
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
..
..
..
. .
. . . . . . x86
. x86
.
...................
.
.
.
.
.
.
.
.
.
.
.
.
...........
...........
...........
EFLAGS
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . x86 Processor Excep ons Table ..
.. . . . . . . . . . . . . . . . . . . . x86
.. . . . . . . . . . . . . . . . . . . . . . . . IRR/ISR/IMR
.. . . . . . . . . . . . . . . . . . . . . . PIC
.. . . . . . . . . . . . . . . . . . . . . . . . . ICW1
ICW3 for Primary PIC . . . . . . . . . . .
ICW3 for Slave PIC . . . . . . . . . . . . .
ICW4 . . . . . . . . . . . . . . . . . . . . . . . . . .
.. . . . . . . . . . . . . . . . . . . . . . OCW2
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . OCW2
.. . . . . . . . . . . . . . . . . . . . . 8253 PIT
..
..
..
..
..
. .
. .
. .
. .
. .
..
..
..
. .
. .
. .
) (
/ .
.
.
. )
( .
) (Embedded Device
) (Orbiter .
.
:
++ ) (Assembly
.
.
) (Bad Sectors .
.
.
) (
!
.
) (
/.
.
//
.I
Basics
.
.
) (Virtual Machine .
.
) (Processor ) (
.
).(Instruc on Set
.
. .
OPCODE .
:..
x86
) . (Machine Language 0 1
. 31744 AX
.101110001100000000000111
.
.
.
.
.
.: Assembly Language
Example
.
++
.
) (Compiler
.
2
DRAM ) (Capacitors )(Transistors
) (Memory Cells )
( )(
) (1 ) (0
.
) (Decode
.
) (.
.
) (Layer
.
Unix .
. ) .(Opera ng System
) ( . .
) ( ]?[ .
:..
..
.
) (
.
...
) (Machine Language Level
) (Buer .
.
...
) (
) (Mul tasking
) (Memory Blocks
.
..
.
.
.
..
... ) :(-
. )
(Pascaline ) .(.
:..
Charles Babbage
) Dierence engine (.
.
.
) . (Method of Finite Dierences
:..
.
Analy cal Engine
) .(. ) ( :
) (Memory ) (Computa on Unit )
(Punched Card Reader ) ( .
) (Word
.
) (
.
) (
.
) ( Ada Lovelace
.
.
.
Konrad Zuse
) (Relay ) Z1 (.
.
..
:..
. Z1 .
Z1
. John
:.. Z1
.
:.. Atanaso
)(
Mark I
:..
Iowa State
)(
Harvard Mark I
... ) :(-
) Enigma Machine
.
.
..
:..
.Colossus
.
.
John Mauchley
ENIAC .Electronic Numerical Integrator And Computer
) (Vacuum Tubes ) (Relays
.
. .Switch
.
. .
.
:.. colossus
:..
ENIAC
EDSAC . JOHNIAC
. EDVAC
Electronic Discrete Variable Automa c Computer
) (. .
ILLIAC
..
:..
EDVAC
x86
x86
x86
..
...
System Bus
.Front-side Bus
x86
..
Data Bus
) (Lines .
) ( ) (Data )
(Control Unit .NorthBridge
).(Padding
.
Address Bus
. 8086
80286/80386
.
Control Bus
. )(
. WRITE
. READ
.
ACCESS
READ and
.WRITE
.
x86
...
) (Controllers .
.
.
) (Port
.
) (Interrupt .
)(NorthBridge
. .
) (Refresh
.
:..
.
...
) (SouthBridge
..
. ) ...(
.
) (Registers .
..
.
)ware Ports
(So
Ports
.
I/O ports in port address out port address
.
ACCESS ) (Control Bus
.
Memory Mapped I/O
.
0xa000:0x0
) (Memory Mapped .
x86 in/out
. port I/O
) (Port I/O
. .
..
.
) (
.
x86
:..
IVT
0x003
0x00000
0x004
0x00400
0x07b
0x00500
0x07d
0x07c00
0x9
0x07e00
Video RAM
0xa
0xa0000
Monochrome VRAM
0xb7777
0xb0000
Color VRAM
0xb
0xb8000
Video ROM BIOS
0xc7f
0xc0000
BIOS Shadow Area
0xe
0xc8000
0xf
0xf0000
x86
...
) (Clock ck .
) (Fetch
.
).(Decode
).(Execute
).(Write back
...
CPU Modes
) (Real Mode
) (hlt
) .(Mul tasking
.
..
x86
:..
0000-000f
0010-001F
0020-0021
0030-0031
0040-0043
0048-004B
0050-006F
0070-0071
0080-008B
0090-009F
00A0-00A1
00C0-00DE
00F0-00FF
0100-0167
0168-016F
0170-0177
01E8-01EF
01F0-01F7
0200-0207
0220-022F
0270-0273
0278-027A
0280-028F
02B0-02DF
02E0-02E7
02E8-02EF
02F8-02FF
0300-031F
0320-023F
0330-0331
0360-036F
0376-0377
0378-037A
0388-038B
03B0-03BB
03BC-03BF
03C0-03DF
03E0-03E7
03E8-03EF
03F0-03F6
03F7-03f7
03F8-03FF
0533-0537
x86
...
Real Mode
x86
) (Protected Mode
. ) (ax,bx,cx,dx,...etc
: ) (Segment:Oset -
- 1 )Virtual
(Memory .
: )(Segment:Oset Addressing
8086
) (
) (Address Bus
.
SRAM .
) (Segments
) (Segment Number or Address
)(Osets
) ( .
Segments
)
- (CS,SS,DS,ES - Oset
.216
..
)
.(0x
) (Logical Address
Segment:Oset
.
) (Oset
) (Segment
. :
0x0
.
0x07c0:0x0000 0x07c00
0x0000:0x7c00 .0x07c00
. .
Overlapping
.
...
Protected Mode
80286
) ( . ) (
) .(Descriptor Table 80386
. ) (Paging
) (Base Address ).(Oset
Daniel B. Sedory http://mirror.href.com/thestarman/asm/debug/
.Segments.html
x86
:..
.
.
CPU Rings
.
)(CPU Rings
. ) (Ring0 .(Ring3)
) (Kernel Mode
) (
. ) (User Mode
) cli (hlt
) (Virtual Address Space
. .
) (PMode
..
) (Kernel Mode
.
:..
...
x86
Instruc on Set
) ( ) . (
) (General Protec on Fault
) (Excep on Handler .
x86 ) (General Registers
) (Segments Registers
) (
. :
RAX (EAX(AX/AH/AL)), RBX (EBX(BX/BH/BL)), RCX (ECX(CX/CH/CL)), RDX :
)).(EDX(DX/DH/DL
:
. CS,SS,ES,DS,FS,GS:
RSI (ESI (SI)), RDI (EDI (DI)), RBP (EBP (BP)). RSP (ESP (SP)), RIP (EIP :
)).(IP
.RFLAGS (EFLAGS (FLAGS)) :
.DR0, DR1, DR2, DR3, DR4, DR5, DR6, DR7 :
x86
:..
LGDT GDTR
LLDT LDTR
LTR
MOV cr x
LMSW new Machine Status WORD
MOV dr x
CLTS Task Switch Flag
INVD
INVLPG
WBINVD
HLT
RDMSR
WRMSR
RDPMC
RDTSC
MSR
Performance Monitoring Counter
me Stamp Counter
MSR
mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7, xmm0, xmm1, xmm2, :
.xmm3, xmm4, xmm5, xmm6, xmm7, GDTR, LDTR, IDTR, MSR, and TR
) (
: ) (High Order Word )Low
(Order Word : )High
(Order Byte ) (Low Order Byte .
EAX ) ( AX
AH .AL
.
:EAX .
..
:EBX
.Base Address
:ECX .
:EDX .
Segment Registers
) (Segments
:
:CS .
:DS .
:SS .
:ES .
:FS .
:GS .
) (PMode
)
( -
.-
Oset Registers
x86
. :
:SI .
:DI .
:BP
.
x86
:SP .
Instruc on Pointer
) (IP ) (CS
CS:IP . ) (EIP
) (IP
MOV .
FLAGS Register
) (EFLAGS ) (FLAGS
) (
) (Status
) (Control ) .(System .
.
IOPL ) (CLI,STI,IN,OUT
Current Priviledge Level
IOPL
).(Ring0
Control Registers
CR8
) (Kernel Moder/Ring0 .
CR0
) (PMode )(Paging
CR0 .
Bit 0 (PE) : Puts the system into protected mode.
Bit 1 (MP) : Monitor Coprocessor Flag This controls the opera on of the WAIT instruc on.
Bit 2 (EM) : Emulate Flag. When set, coprocessor instruc ons will generate an excep on
) (Ring0 .
..
EFLAGS
:..
Carry Flag - Status bit
Parity Flag
CF
PF
AF
ZF
SF
TF
IF
DF
OF
IOPL
NT
RF
VM
AC
VIF
VIP
ID
-
0
1
2
3
4
5
6
7
9
9
10
11
12-13
14
15
16
17
18
19
20
21
22-31
x86
Bit 3 (TS) : Task Switched Flag This will be set when the processor switches to another task.
Bit 4 (ET) : ExtensionType Flag. This tells us what type of coprocesor is installed.
0 - 80287 is installed
1 - 80387 is installed.
Bit 5 (NE): Numeric Error
0 - Enable standard error repor ng
1 - Enable internal x87 FPU error repor ng
Bits 6-15 : Unused
Bit 16 (WP): Write Protect
Bit 17: Unused
Bit 18 (AM): Alignment Mask
0 - Alignment Check Disable
1 - Alignment Check Enabled (Also requires AC ag set in EFLAGS and ring 3)
Bits 19-28: Unused
Bit 29 (NW): Not Write-Through
Bit 30 (CD): Cache Disable
Bit 31 (PG) : Enables Memory Paging.
0 - Disable
1 - Enabled and use CR3 register
.II
Boo ng
Bootloader
.
FAT12
.
..
) (Boot-Strapping
) ( MotherBoard )Power Supply
.(Unit PSU
Power Good . BIOS
) ( Power on Self Test
POST ) ...(
. POST BIOS POST
BIOS 0xFFFF0 ) ( jump
POST CS:IP
. BIOS BIOS
) ( Interrupt Vector Table
BIOS Setup BIOS
) ( Halt
BIOS )
( 0x07c00 .
) ( bit 1 .
Bootloader
) ( BIOS
0x0
.
int 0x19
0x07c00 .
) (
0xAA55
.
Real Mode Protected Mode
- ) ( Excep on .
..
Bootloader
.
:
. 512 .
. : 1 0 0
.
. .
.
. Flat Binary
object code
header,symbol table,...etc
32-bit
) ( A20
.
. Mul -Stage Boot Loader
:
1
Safe Mode
..
0xAA55
510 . 511 ) (0x19
.
!
. 32-bit 16-bit
- - 16-bit
.
) ( ELF,PE,COFF,...etc
-
. -
Object Code .Flat Binary
) (Assembly 16-bit
)
( Backward Compa bility 16-bit
- - 32-bit
run- me library .Machine Language
!
) ( GRUB,NTLDR,LILO...etc
run- me loader
. main
..
. .
Bootloader
:..
0x07c00
) (
) (Memory Mapped
0xf0000
). (Memory Mapped
..
. NASM
. object code
oppy disk or CD
). (Virtual Machine
..
.: Smallest Bootloader
Example
bits 16
db
start:
cli
hlt
) times 510($$$
; if cli and hlt take 4 byte then time directive will fill
; 5104 = 506 zero's.
0x0000:0x7c00
) (real mode
cli hlt
)(garbage
) (Crash . 512
510
0xaa55 times 510
) (
.
...
.
int 0x10 . 0xe
Bootloader
Example
.: Welcom to OS World
;Hello Bootloader
bits 16
org
0x0
start:
jmp main
; data
;
hello msg
db
input:
es: pointer to data segment.
si: point to the string
puts16:
lodsb
; read character from ds:si to al ,and increment si if
df=0.
cmp al,0
; check end of string ?
je end puts16 ; yes jump to end.
mov ah,0xe
int 0x10
jmp puts16
end puts16:
..
ret
;
;
;
main:
;
; intit registers
;
mov ax,0x07c0
mov ds,ax
mov es,ax
cli
hlt
; clear interrupt.
; halt the system.
times 510($$$ )
db
; append zeros.
code segment
( 512 )
.
cs:ip far jump
data segment
Bootloader
.ds,es,ss,fs,gs
0x07c00 4000
0x07c0:0x0 0x0:0x7c00
.
) 0x07c0
( ) (label 0x0
0x0
org ) (reloca ng
0x0
0x0
0x7c00 0x0
org relocate . 0x7c00
...
boot sector
OEM
) BIOS Parameter Block (BPB
. 3
.
.: Bios Parameter Block
Example
"
OEM ID
db
"eqraOS
be 8 byte! no more no less.
0x200
0x1
0x1
dw
db
dw
; two fats.
; root dir has 224
0x2
0xe0
db
dw
0xb40
dw
reserved.
total fats
root directory
entries.
total sectors
volume.
..
media descriptor
sectors per fat
sectors per track
number of heads
hidden sectors
total sectors large
db
dw
dw
dw
dd
dd
0xf0
0x9
0x12
0x2
0x0
0x0
;
;
;
;
;
; Extended BPB.
drive number
db
db
signature
db
volume id
dd
when foramt the disk.
volume label
db
system id
db
flags
0x0
0x0
0x29
0x0
.: BPB example
;Hello Bootloader
bits 16
org
0x0
start:
jmp main
;
; OEM Id and BIOS Parameter Block (BPB)
instruction.
OEM ID
db
"eqraOS
be 8 byte! no more no less.
"
Bootloader
bytes per sector
sectors per cluster
reserved sectors
dw
db
dw
0x200
0x1
0x1
db
dw
0x2
0xe0
; two fats.
; root dir has 224
dw
0xb40
db
dw
dw
dw
dd
dd
0xf0
0x9
0x12
0x2
0x0
0x0
;
;
;
;
;
reserved.
total fats
root directory
entries.
total sectors
volume.
media descriptor
sectors per fat
sectors per track
number of heads
hidden sectors
total sectors large
; Extended BPB.
drive number
db
db
signature
db
volume id
dd
when foramt the disk.
volume label
db
system id
db
flags
0x0
0x0
0x29
0x0
; data
;
hello msg
db
input:
es: pointer to data segment.
si: point to the string
puts16:
..
lodsb
; read character from ds:si to al ,and increment si if
df=0.
cmp al,0
; check end of string ?
je end puts16 ; yes jump to end.
mov ah,0xe
int 0x10
jmp puts16
end puts16:
ret
;
;
;
main:
;
; intit registers
;
mov ax,0x07c0
mov ds,ax
mov es,ax
Bootloader
cli
hlt
; clear interrupt.
; halt the system.
times 510($$$ )
db
; append zeros.
Hex Editor .
) (
.
Example
O f f s e t ( h ) 00 01 02 03 04 05 06 07
00000000
00000008
00000010
00000018
00000020
00000028
00000030
00000038
00000040
00000048
00000050
00000058
00000060
00000068
00000070
00000078
00000080
00000088
000001F0
E9
53
02
12
00
00
4C
74
6C
20
20
79
45
AC
10
8E
E6
00
72
20
E0
00
00
00
4F
31
63
65
43
20
73
3C
E9
D8
FF
00
00
20
00
02
00
00
50
32
6F
71
6F
41
73
00
F4
8E
FA
00
65
00
40
00
00
4D
50
20
6D
72
64
68
61
74
FF
C0
F4
00
...
...
00 00 00 00
71
02
0B
00
00
4F
59
20
65
61
65
6D
6D
07
C3
BE
00
00
72
01
F0
00
00
53
20
20
20
4F
64
61
0A
B4
B8
3E
00
00
61
01
09
00
29
20
66
57
74
53
20
64
0D
0E
C0
00
00
00
4F
00
00
00
00
46
61
65
6F
2C
62
20
00
CD
07
E8
00
00
00 00 00 00
r . eqraO
S .....
. .@ . . .
........
......).
. . . MOS F
LOPPY f a
t12
We
lcome t o
eqraOS ,
Coded b
y Ahmad
Essam . . .
<. t . .
. .
..>.
. . . .
........
........
..
000001F8
00 00 00 00 00 00 55 AA
. . . . . . U
!
Disassembler
.
Example
.: Complete Example
;Hello Bootloader
bits 16
org
0x0
start:
jmp main
;
; OEM Id and BIOS Parameter Block (BPB)
instruction.
OEM ID
db
"eqraOS
be 8 byte! no more no less.
"
reserved.
total fats
root directory
entries.
total sectors
volume.
media descriptor
dw
db
dw
0x200
0x1
0x1
db
dw
0x2
0xe0
; two fats.
; root dir has 224
dw
0xb40
db
0xf0
.source code
Bootloader
sectors per fat
sectors per track
number of heads
hidden sectors
total sectors large
dw
dw
dw
dd
dd
0x9
0x12
0x2
0x0
0x0
;
;
;
;
; Extended BPB.
drive number
db
db
signature
db
volume id
dd
when foramt the disk.
volume label
db
system id
db
flags
0x0
0x0
0x29
0x0
; data
;
hello msg
db
input:
es: pointer to data segment.
si: point to the string
puts16:
lodsb
; read character from ds:si to al ,and increment si if
df=0.
cmp al,0
; check end of string ?
je end puts16 ; yes jump to end.
mov ah,0xe
int 0x10
..
jmp puts16
end puts16:
ret
;
;
;
main:
;
; intit registers
;
mov ax,0x07c0
mov ds,ax
mov es,ax
cli
hlt
; clear interrupt.
; halt the system.
times 510($$$ )
db
; append zeros.
Bootloader
...
int 0x13
.
. int 0x13
sector
0x0 int 0x13
.
:
.0x0 : ah
: dl .0x0
: ah.
0x1 : CF 0x0 .
:
.: Reset Floppy Drive
Example
reset floppy:
mov ah,0x0
mov dl,0x0
; call BIOS
int 0x13
jc reset floppy
..
sectors
) (sectors . RAM
:
:ah 0x2
:al .
int 0x13
0x2
:ch ) (Cylinder .
:cl 5 - 0
.disk
:dh .
: dl .0x0
: es:bx .
hard
:
: ah.
:al .
0x1 : CF 0x0 .
:
.: Read Floppy Disk Sectors
Example
read sectors:
reset floppy:
mov ah,0x0
mov dl,0x0
; call BIOS
int 0x13
jc reset floppy
Bootloader
; init buffer.
mov ax,0x1000
mov es,ax
xor bx,bx
read:
mov
mov
mov
mov
ah,0x2
; routine number.
al,1
?; how many sectors
ch,1
; cylinder or track number.
cl,2
; sector number "fisrt sector is 1 not 0",now we read
the second sector.
mov dh,0
; head number "starting with 0".
mov dl,0
; drive number ,floppy drive always zero.
; call BIOS.
; if error, try again.
int 0x13
jc read
..
jmp 0x1000:0x0
FAT12
bad sectors
Defragmenta on
.
!
!
:
).(Driver
Data Structure .
..
FAT12
FAT12
.
...
FAT12
FAT12
: FAT12
.Root Directory
) (Cluster 12 .212
12.
4077 .
16
0x01
)ons
MB
.32
.(Par
... FAT12
(Format) FAT12 : .
:.. FAT12
. FAT12
Bootloader
) (Boot Sector ) (
BPB and OEM id : 1 0
0 int 0x13
.
Absolute Sector
.
) (Logical Sector Addressing
LBA )(Boot Sector
0 1 .
) int 0x13
( -
.- ) (
Extra Reserved Sectors
FAT
reserved sectors BPB
) (
2 0 0 .1
. FAT
12 ) (Cluster
) 12( :
:0x00 .
: 0x01 .
0x02 : 0xfef )
(.
0xff0 :0xff6 .
0xff8 :0xfff .
..
FAT12
FAT
) Root Directory ( index
FAT 0x02 0xfef
index
. FAT 1 9
10
. FAT
backup Root Directory
224 32
index FAT
.
root directory :
:7-0 ) 8
(.
:10-8 ) (.
:11 :
:0 .
:1.
:2 .
:3 .Volume Label
:4 .
:5.
:6.
:7 .
:12 .
:13 .MS
Bootloader
:15-14 :
:4-0 ).(29-0
:10-5 ).(59-0
:15-11 ).(23-0
:17-16 :
:4-0 ).(2107=127 ;1980=0
:8-5 )=1; =12(.
:15-9 ).(23-0
:19-18 ) (.
.EA index :21-20
:23-22 ) .(15-14
:25-24 ) .(17-16
:27-26 .
:29-28 .
...
) Pla er ( ) (Tracks
.
1.44 MB 80 ) 0 (79 18
80 18 2 2880.
) ( : 1 0 0
: 2 0 0
18 0 0
: 1 0 1
...
.
..
...
FAT12
FAT12
) (
FAT12 ) Root
(directory
FAT12
FAT
. Root directory
Second Stage Bootloader
32.
:
FAT12
. Root Directory
.
. FAT .
. .
FAT12
Root Directory FAT
FAT12
. .
.: Hello Stage2
Example
Bootloader
bits 16
org
0x0
start:
jmp stage2
; include files:
%include "stdio.inc"
stage2:
push cs
pop ds
; ds = cs.
cli
hlt
; clear interrupt.
; halt the system.
stage2.asm
3 8
.Root Directory Spaces FAT12
(FAT12 )
.( )
stage2.sys
FAT12
..
Root Directory
Root Directory
( stage2.sys )
. "stage2 sys"
.( Buer)
.
Example
;
; Compute Root Directory Size
;
xor cx,cx
mov ax,32
; every root entry size are 32 byte.
mul word[root directory] ; dx:ax = 32224 bytes
div word[bytes per sector]
xchg ax,cx
; cx = number of sectors to load.
;
; Get start sector of root directory
;
;
; Load Root Dir at 0x07c0:0x0200 above bootloader.
;
mov bx,0x0200
; es:bs = 0x07c0:0x0200.
call read sectors
Bootloader
. .
Example
;
; Find stage2.sys
;
mov di,0x0200
; di point to first entry in root dir.
mov cx,word[root directory] ; loop 224 time.
find stage2:
rep cmpsb
pop di
je find successfully
mov di,32
pop cx
; no found ?
jmp find fail
find successfully:
;
; Get first Cluster.
;
mov ax,word[di+26]
; 27 byte in the di entry are cluster
number.
mov word[cluster number],ax
..
FAT12
FAT
FAT
Root Directory .
Root Directory FAT
.
.
.: Load FAT Table
Example
;
; Compute FAT size
;
xor cx,cx
xor ax,ax
xor dx,dx
mov bx,0x0200
call read sectors
Bootloader
Sector FAT12
Cluster Internel
Fragmenta on FAT12
.
Absolute Address
LBA .
Cluster LBA :
.: Convert Cluster number to LBA
Example
input:
;
ax: Cluster number.
; output:
;
ax: lba number.
;
cluster to lba:
; lba = (cluster 2) sectors per cluster
; the first cluster is always 2.
sub ax,2
xor cx,cx
]mov cl, byte[sectors per cluster
mul cx
2 FAT12 - 2
.-
LBA : Absolute Address
.: Convert LBA to CHS
Example
FAT12
;
;
;
;
;
;
;
..
input:
ax: LBA.
output:
absolute sector
absolute track
absolute head
lba to chs:
; absolute sector =
; absolute track =
; absolute head
=
xor
div
inc
mov
dx,dx
word[sectors per track]
dl
byte[absolute sector],dl
xor
div
mov
mov
dx,dx
word[number of heads]
byte[absolute track],al
byte[absolute head],dl
ret
Root Directory
Abolsute Address LBA LBA
. int 0x13
Example
.: Load Cluster
;
; Load all clusters(stage2.sys)
; At address 0x050:0x0
;
xor bx,bx
mov ax,0x0050
mov es,ax
load cluster:
Bootloader
xor cx,cx
mov cl,byte[sectors per cluster]
; cx = 1 sector
; load cluster.
int 0x13
.32-bit
Example
;
ne
input:
es:bx : Buffer to load sector.
ax:
first sector number ,LBA.
cx:
number of sectors.
begin:
mov di,5
load sector:
push ax
push bx
push cx
mov
mov
mov
mov
mov
mov
ah,0x2
; load sector routine number.
al,0x1
; 1 sector to read.
ch,byte[absolute track]
; absolute track number.
cl,byte[absolute sector] ; absolute sector number.
dh,byte[absolute head]
; absolute head number.
dl,byte[drive number]
; floppy drive number.
FAT12
..
int 0x13
; call BIOS.
jnc continue
; if no error jmp.
mov ah,0x0
mov dl,0x0
int 0x13
pop cx
pop bx
pop ax
dec di
jne load sector
; error.
int 0x18
continue:
pop cx
pop bx
pop ax
add ax,1
; next sector
add bx,word[bytes per sector] ; point to next empty block in
buffer.
loop begin
; cx time
ret
FAT
Bootloader
.
FAT 12
0 FAT 12
16 ) ( mask 4
) ( . 1
FAT 23-12 12 16
23-8 4.
:
16 Mask 4
4 .
.
.: Read FAT entry
Example
;
;
;
;
;
cx,ax
; cx = cluster number.
cx,1
; divide cx by 2.
cx,ax
; cx = ax + (ax/2).
di,cx
di,0x0200
dx,word[di] ; read 16bit form FAT.
mov
shr
add
mov
add
mov
FAT12
..
test ax,1
jne odd cluster
even cluster:
and dx,0x0fff
jmp next cluster
odd cluster:
shr dx,4
next cluster:
mov word[cluster number],dx
cmp dx,0x0ff0
jb load cluster
find fail:
mov ah,0x0
int 0x16
int 0x19
; wait keypress.
; warm boot.
retf
; cs:ip = 0x050:0x0
Bootloader
times 510($$$ )
db
; append zeros.
. -
stage 2
:
.PMode
A20 4 .
.Interrupt Handler
.Safe Mode
Boot
Mul .
..
Real Mode
1
Paging Virtual Memory .
cr0
Descriptor Table General Protec on
Fault GPF triple fault .
) (Global Descriptor Table GDT
.
. -
...
PMode
. ) (Code
) (Data .
GDT ) Descriptors 64( :
GDT
:Null Descriptor .
Descriptor :
:15-0 ) (15- 0 .
..
.Segment is in memory (Used with Virtual Memory) :47
. 19- 16 :51-48
. :52
. :53
:Segment type :54
. 16 :0
. 32 :1
:Granularity :55
.None :0
.Limit gets mul plied by 4K :1
.Base Address 32- 23 :63-56
.: GDT
;
; Global Descriptor Table
begin of gdt:
dd
dd
0x0
0x0
dw
dw
db
db
db
db
0xffff
0x0
;
0x0
;
10011010b
11001111b
0x0
;
; limit low.
base low.
base middle.
; access byte.
; granularity byte.
base high.
. -
; limit low.
base low.
base middle.
; access byte.
; granularity byte.
base high.
0xffff
0x0
;
0x0
;
10010010b
11001111b
0x0
;
dw
dw
db
db
db
db
end of gdt:
Null Descriptor 8
Code Descriptor
Kernel Mode .
.
Code Descriptor 0x8
CS 15-0 Segment Limit 0xffff
.0xffff
39-16 23-0 Base Address
0x0 0x0 . 0x
6 Access Byte
:
Access Bit :0 .0
:3 1 .Code Segment
:4 1 .
:6-5 0
Ring0 .Kernel Mode
:7 .
:1 1
.0xffff-0x0
on :2
expansion direc .0
..
7 granularity
:
:6 1 32.
:7 1
KB
.4
) (8 32-24
0x0 0x0 .
Code Descriptor
.Code Segment
Data Descriptor 0x10
43 .
) (GDT gdtr
GDT
) lgdt (Ring0 .
.: Load GDT into GDTR
Example
; real mode.
16
bits
;
; load gdt: Load GDT into GDTR.
;
load gdt:
cli
; clear interrupt.
pusha
; save registers
]lgdt [gdt ptr
; load gdt into gdtr
sti
; enable interrupt
popa
; restore registers.
. -
ret
;
; gdt ptr: data structure used by gdtr
gdt ptr:
...
Segment:Oset
) (Segments Registers
0x10 oset
.Address Bus
PMode Descriptor:Oset
) CS 0x8 DS
(0x10 oset Base Address
Segment Limit
.
32-bit 4 .
...
GDT GDTR
cr0
.
CS far jump
.
A20 .
..
Example
;
; Load gdt into gdtr.
;
cli
; important.
mov eax,cr0
or eax,0x1
mov cr0,eax
; entering pmode.
;
; Fix CS value
;
; select the code descriptor
jmp 0x8:stage3
;
; entry point of stage3
bits 32
stage3:
;;
; Set Registers.
;;
mov ax,0x10
mov ds,ax
mov ss,ax
. -
; stack begin from 0x90000.
mov es,ax
mov esp,0x90000
;;
; Hlat the system.
;;
cli
; clear interrupt.
hlt
; halt the system.
..
A20
... 8042
A20
) (PMode
. A20
in .out
20
..
A20
...
A20
:0 reset .
:1 0 A20 1.
:2 .
power on password bytes :3
:5-4 .
: HDD ac vity LED :7-6 o :0 :1
.on
.
.: Enable A20 by System Control Port 0x92
Example
;
; enable a20 port 0x92:
. -
push ax
; save register.
mov al,2
; set bit 2 to enable A20
out 0x92,al
pop ax
ret
; restore register.
.
int 0x15 0x2401 A20
.
.
0x2400
Example
;
; enable a20 bios:
pusha
; restore registers
popa
ret
: 0x60 ) buer
Buer Input Buer 0x64
Output
..
A20
) .(Status 0x64
) buer (0x60 .0x60
) (
) (
.
) (0x64
.
:
:0 :Output Buer
:0 Output Buer ) (.
:1 Output Buer ) (.
:1 :Input Buer
:0 Input Buer ) (.
:1 Input Buer ) (.
) (wait input
).(wait output
.: Wait Input/Output
Example
;
; wait output: wait output buffer to be full.
;
wait output:
in al,0x64
; read status
test al,0x1
?; is output buffer is empty
je wait output ; yes, hang.
; no,there is a result.
ret
;
; wait input: wait input buffer to be empty.
. -
wait input:
in al,0x64
; read status
test al,0x2
?; is input buffer is full
jne wait input ; yes, hang.
; no,command executed.
ret
0x64
.
:
:0xad .
:0xae .
:0xdf .A20
0xdd A20
.
.: Enable A20 by Send 0xdd
Example
;
; enable a20 keyboard controller:
;cli
..
; save register.
push ax
A20
mov al,0xdd
out 0x64,al
; restore register.
pop ax
ret
Output Port
0xd0 0xd1 .
) d0 ( :
:System Reset :0
.Reset Computer :0
.Normal Opera on :1
:1 :A20
:0.
:1.
:3-2 .
:Keyboard Clock :6
.High-Z :0
.Pull Clock Low :1
:Keyboard Data :7
.High-Z :0
.Pull Data Low :1
1 A20 or
. . 0xd1
A20 Output Port .
- .
Example
;
; enable a20 keyboard controller output port:
;
cli
pusha
;
; Disable Keyboard
;
mov al,0xad
; disable keyboard command.
out 0x64,al
call wait input
;
; send read output port command
;
mov al,0xd0
; read output port command
out 0x64,al
call wait output
; wait output to come.
; we don't need to wait input bescause when output came we know
that operation are executed.
;
; read input buffer
;
in al,0x60
push eax
; save data.
call wait input
;
; send write output port command.
;
..
mov al,0xd1
; write output port command.
out 0x64,al
call wait input
;
; enable a20.
;
pop eax
or al,2
; set bit 2.
out 0x60,al
call wait input
;
; Enable Keyboard.
;
mov al,0xae
; Enable Keyboard command.
out 0x64,al
call wait input
VGA
; restore registers
popa
sti
ret
..
VGA
. -
Graphics Controller , Sequencer unit , CRT Controller , Video DAC ,Video Buer
. Controller
Video Buer segment of memory Memory Mapped
0xa0000
Memory Mapping
Graphics Controller
.Video buer
VGA Text Mode APA Graphics
Mode Video buer .
All Point Addressable Graphics Mode
. .
Text Mode Characters Video
Controller two buers Character Map
Screen
Buer .
VGA Monochrome Display Adapter
MDA IBM 1981 MDA
) (Mode 7 80 24 ) .(25*80 IBM
) Color Graphics Adapter (CGA 16
. .Enhanced Graphics Adapter
VGA Backward Compa ble
) Mode 7 (MDA
80 25.
A ribute
...
VGA
0xa0000 0xbffff
) Video memroy (VGA :
A .
.
..
.: Print 'A' character on screen
VGA
Example
0xb8000
Memory.
; White chracter on black background.
0x7
; print A
; in white foreground black
background.
...
Video Memory
) (x,y
.Video Memoey ) (x,y ) (0,0
Video Memory
.(Color text Mode) 0xb8000 ) (x,y ) (0,1
0xb8001 ) (x,y Video
:Memory
videomemory = 0xb0000
videomemory+ = x + y 80
80 y . 80
). (4,4
address = x + y 80
address = 4 + 4 80 = 324
; now add the base address of video memory.
address = 324 + 0xb8000 = 0xb8144
0xb8144
) (.
Mode 7
. -
A
0xb8000 0xb8001
.
) ( :
:2-0 :
:0.
:1.
:2.
:3 ) 0 1 (.
:6-4 :
:0.
:1.
:2.
:7 ) 0 1 (.
4 :
0: Black.
1: Blue.
2: Green.
3: Cyan.
4: Red.
5: Magneta.
6: Brown.
7: Light gray.
8: Dark Gray.
9: Light Blue.
VGA
..
Mode 7
( underline ) Cursor
putch32 . 0xa
.PMode
Example
.: putch32 rou
ne
input:
bl: character to print.
bits
32
0xb8000
Memory.
%define COLUMNS
80
%define ROWS
25
%define CHAR ATTRIBUTE
x pos
y pos
db
db
0
0
; current x position.
; current y position.
putch32:
pusha
; Save Registers.
;
; Check if bl is new line ?
;
- .
cmp bl,0xa
je new row
; if character is newline ?
; yes, jmp at end.
;
; Calculate the memory offset
;
; because in text mode every character take 2 bytes: one for the
character and one for the attribute, we must calculate the
memory offset with the follwing formula:
; offset = x pos 2 + y pos COLUMNS2
xor eax,eax
mov al,2
mul byte[x pos]
push eax
; save the first section of formula.
xor eax,eax
xor ecx,ecx
mov ax,COLUMNS2
mov cl,byte[y pos]
mul ecx
; 802
pop ecx
add eax,ecx
;
; Print the chracter.
;
mov edi,eax
mov byte[edi],bl
; print the character,
mov byte[edi+1],CHAR ATTRIBUTE
; with respect to the
attribute.
;
; Update the postions.
..
;
VGA
new row:
putch32 end:
; Restore Registers.
popa
ret
) (bl
0xa x y
.
Mode 7
) (x,y .
videomemory = 0xb0000
videomemory+ = x 2 + y 80 2
eax
. ) (x,y
x
y x .
...
strings
puts32 .
ne
.: puts32 rou
Example
- .
input:
ebx: point to the string
bits
32
puts32:
pusha
; Save Registers.
mov edi,ebx
@loop:
mov bl,byte[edi]
; read character.
cmp bl,0x0
je puts32 end
; end of string ?
; yes, jmp to end.
call putch32
inc edi
jmp @loop
puts32 end:
;
; Update the Hardware Cursor.
;
; After print the string update the hardware cursor.
popa
; Restore Registers.
..
VGA
ret
)
(0x0 CRT Controller
.
...
Hardware Cursor
. CRT Controller
Data Register .Index Register
Index
Register Data Register x86
0x3d5 Index Register .0x3d4
.Index Register
0x0: Horizontal Total.
0x1: Horizontal Display Enable End.
0x2: Start Horizontal Blanking.
0x3: End Horizontal Blanking.
0x4: Start Horizontal Retrace Pulse.
0x5: End Horizontal Retrace.
0x6: Ver cal Total.
0x7: Overow.
0x8: Preset Row Scan.
0x9: Maximum Scan Line.
0xa: Cursor Start.
0xb: Cursor End.
0xc: Start Address High.
. -
0xd: Start Address Low.
Index Reigster
.Data Register 0xf x
0xe y. x,y
CRT
) (x,y .
videomemory = x + y 80
move cursor .
.: Move Hardware Cursor
Example
input:
bl: x pos.
bh: y pos.
;
;
;
;
32
bits
VGA
..
move cursor:
pusha
; Save Registers.
;
; Calculate the offset.
;
; offset = x pos + y pos COLUMNS
xor ecx,ecx
mov cl,byte[x pos]
mov eax,COLUMNS
mul byte[y pos]
add eax,ecx
mov ebx,eax
;
; Cursor Location Low.
;
mov al,0xf
mov dx,0x3d4
out dx,al
mov al,bl
mov dx,0x3d5
out dx,al
;
; Cursor Location High.
;
mov al,0xe
mov dx,0x3d4
out dx,al
mov al,bh
mov dx,0x3d5
out dx,al
- .
popa
; Restore Registers.
ret
Clear Screen
...
( Mode 7 25*80)
. . (x,y)
Example
.: Clear Screen
bits
32
clear screen:
pusha
cld
; Save Registers.
rep stosw
popa
; Restore Registers.
ret
..
..
Second Stage Bootloader
.
.
Pure Binary
++
.
PMode int 0x13
.
4
0x100000 1 .
0x100000 0x3000
0x100000 .
.
.: Hello Kernel
Example
org
0x100000
; PMode.
32
bits
kernel message
db
"0xa,0xa,0xa,
eqraOS v0.1
"Copyright (C) 2010 Ahmad Essam
db
0xa,0xa,
"
University of Khartoum Faculty
of Mathematical Sceinces.",0
- .
logo message
\ /
db
db
db
db
0xa,0xa,0xa,"
/"
0xa,
"
\ \ "
0xa,
"
/ "
0xa,
"
/ )
`/
/\ , / /
`/ / / / /
\ , /
//
/ /
",0
;
; Entry point.
kernel entry:
;
; Set Registers
;
mov
mov
mov
mov
mov
ax,0x10
; data selector.
ds,ax
es,ax
ss,ax
esp,0x90000
; set stack.
;
; Clear Screen and print message.
;
cli
..
hlt
int 0x13 1 .
Root Directory
index ) FAT
( index
.
stage2.asm
) oppy.inc .inc (
fat12.inc stdio.inc
A20 a20.inc
gdt.inc common.inc .
ng Kernel: Full Example
Example
bits 16
org 0x500
jmp stage2
start:
;
; include files:
;
;
;
;
;
;
; data and variable
.
example/ch3/boot/ .
- .
hello msg
db
db
reboot...",0
fail message
stage2:
;
; Set Registers.
;
cli
xor ax, ax
mov ds, ax
mov es, ax
sti
;
; Load gdt into gdtr.
;
..
;
mov si,hello msg
call puts16
;
; Load Root Directory
;
call load root
;
; Load Kernel
;
xor ebx,ebx
mov bp,KERNEL RMODE BASE
mov ah,0
int 0x16
int 0x19
cli
hlt
;
; Go to PMode.
;
enter stage3:
cli
; important.
- .
mov eax,cr0
or eax,0x1
mov cr0,eax
; entering pmode.
;
; Fix CS value
;
; select the code descriptor
jmp CODE DESCRIPTOR:stage3
;
; entry point of stage3
bits 32
stage3:
;;
; Set Registers.
;;
mov
mov
mov
mov
mov
ax,DATA DESCRIPTOR
; address of data descriptor.
ds,ax
ss,ax
es,ax
esp,0x90000
; stack begin from 0x90000.
;
; Clear Screen and print message.
;
..
;
; Copy Kernel at 1 MB.
;
mov eax,dword[kernel size]
movzx ebx,word[bytes per sector]
mul ebx
mov ebx,4
div ebx
cld
mov
mov
mov
rep
;
; Execute the kernel.
;
jmp CODE DESCRIPTOR:KERNEL PMODE BASE
;;
; Hlat the system.
;;
cli
; clear interrupt.
hlt
; halt the system.
. -
:..
:..
.III
Kernel
.
) (Kernel
.
.
++
.
..
.
)
( .
) (
!
.
.
.
.
...
.
:
.
.
.
:
Firmware
) (Firmware ) (EEPROM
.
.
: ) (
.
: ) (
) (
) (GDT .
..
: ) (
) (Applica on Progeamming Interface ).(API
..
) (User Program
.
.
...
) (Fragmenta on
.
)(Posi on Independent
) (Base Address .
binary
0x0
.
0x0
) (Virtual Address Space
0x0 .reloca on
) (VAS 0x0
.VAS
VAS )
( . .
)Memory
.
.
.(Management Unit Cache
- .-
..
) (Hardware Abstrac on
) (So ware Layer ) HAL
(Hardware Abstrac on Layer HAL
.
) (Por ng ) (SPARC,MIPS,...etc
HAL ) (Implementa on
) (Devcie Drivers .
...
Monolithic Kernel
Monoli c
) (System Process .
.
.
DOS
.NT
HAL /
!
Mono Lithic
.
..
...
MicroKernel
MicroKernel
) (IPC
) (
) (
.
) (Message Passing ) (Shared Memory Interprocess
.Communica on .
...
Hybrid Kernel
MicroKernel
Monolithic Kernel Hybrid Kernel .Modied MicroKernel
NT BeOS .Plane 9
..
) (Inline Assemlby )
(.
) dll( )
( .
) new
(.
++
) (RunTime Library ++
) (RunTime Library .
++ ++
) (Standard C/C++ Library printf scanf
.
malloc/new .free/delete
32 32
32 ++ . )(
) (Flat Binary .Headers,...etc
) (main rou ne .
++
.
++
++ )Portable
(Executable ))((main
++
) (Pure Virtual Func on
) (Excep ons . ++
...
PE
++ ) (Portable Executable
. ++ ) (g++
ELF / .
PE .
) (headers PE
. ++.
.: Portable Executable Header
Example
typedef struct
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
..
typedef struct
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
} IMAGE FILE
typedef struct
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
( kernel entry())
IMAGE OPTIONAL HEADER ( header)
.
e lfanew IMAGE DOS HEADER
IMAGE FILE HEADER
ImageBase oset AddressOfEntryPoint
oset
) .call
.(KERNEL PMODE BASE
Example
.: Ge
ng Kernel entry
add ebx,24
; ebx =
..
add ebx,16
; epb = AddressOfEntryPoint
]mov ebp,dword[ebx
add ebx,12
]add ebp,dword[ebx
cli
call ebp
... ++
++ )(startup
(C++ Run me Library) ++
) (
.
) (Global Object
) (new,delete RTTI ).(Excep ons
Global Operator
) (new ) (delete ++
. .
.: Global new/delete operator
};int size){return 0
};int size){return 0
}{)p
}{)p
Example
void
.
Pure virtual func on call handler
Example
int
.: Floa
Example
{ )(ftol2 sse
)declspec (naked
;int a
#ifdef i386
{ asm
]fistp [a
mov ebx, a
#endif
;fltused = 1
..
.: Object Ini
Example
;)cdecl PVFV)(void
( typedef void
//
;} x c a [] = { 0
PVFV
//
// Select the default data segment again (.data) for the rest of the
unit
)( #pragma data seg
.data
.code
.stack
.
// Now, move the CRT data into .data section so we can read/write to
it
#pragma comment(linker, "/merge:.CRT=.data")
..)
void
cdecl
initterm (
PVFV pfbegin,
PVFV pfend ) {
atexit init();
initterm( xc a ,
x c z );
(deini alizer array) ( Objects)
atexit (exit() )
++
.
.
Example
.: Delete Object
..
PVFV pf atexitlist = 0;
cdecl
atexit init(void) {
int
return 0;
cdecl exit () {
//! Go through the list, and execute all global exit routines
while (cur atexitlist entries) {
...
kernel entry() PE ( Parsing)
main() ( Stack)
.(Hang) main()
Example
ne
extern void
extern void
extern void
void
#ifdef i386
asm {
cli
#endif
..
#ifdef i386
asm cli
#endif
;);;(for
)( main .
..
) (Expandibility
HAL
.
) (Interface
.
) (Impelmenta on
.
eqraOS:
boot: rst-stage and second-stage bootloader.
core:
kernel:Kernel program PE executable le type.
Bootloader .
.
hal:Hardware abstrac on layer.
lib:Standard library run me and standard C/C++ library.
include:Standard include headers.
debug:Debug version of eqraOS.
release:Final release of eqraOS.
..
++
++
.
NULL
.(void*)0 0 NULL ++
Example
.: null.h:Deni
#ifndef NULL H
#define NULL H
> = 1020)
#pargma once
#endif
#ifdef NULL
#undev NULL
#endif
#ifdef
cplusplus
extern "C"
#endif
..
#define NULL 0
#ifdef
cplusplus
#else
/ C NULL definition
#endif
#endif //NULL H
++
.
cplusplus
size t
.: size t.h:Deni
on of size t in C/C++
#ifndef SIZE T H
#define SIZE T H
#ifdef
cplusplus
extern "C"
#endif
#ifdef
cplusplus
#endif
#endif //SIZE T H
( Data Types)
. ( typedef)
Example
#ifndef STDINT H
#define STDINT H`
#define
#define
need wint t
need wchar t
/
char
int8 t;
unsigned char
uint8 t;
short
int16 t;
unsigned short
uint16 t;
int
int32 t;
unsigned int
uint32 t;
long long
int64 t;
unsigned long long uint64 t;
// to be continue..
#endif //STDINT H
cstdint ++
. ++
Example
#ifndef CSTDINT H
. ++
..
#define CSTDINT H
#include
<stdint.h>
#endif //CSTDINT H
( )Macros) ctype.h
.(...
Example
#ifndef CTYPE H
#define CTYPE H
#ifdef
MSC VER
#ifdef
cplusplus
extern "C"
#endif
extern char
ctype[];
/ constants
#define CT UP
#define CT LOW
#define CT DIG
#define CT CTL
#define CT PUN
#define CT WHT
#define CT HEX
#define CT SP
.
/ macros
#define isalnum(c)
CT DIG) )
#define isalpha(c)
)
#define iscntrl(c)
// to be continue..
#ifdef
cplusplus
#endif
#endif // CTYPE H
Interrupts
) (Current Process
.
) (Interrupt
) (
) (Interrupt Handler ) (Interrupt Service Roun ne
)
(
. )(Hardware Interrupt
) (So ware Interrupt
.int n )
(Page Fault )(Excep ons
) (Error Handler .
..
So ware Interrupts
) (int n
) (Interrupt handler
) (Ring3 user mode )
(.
...
) )(Interrupt Request
(IRQ )Interrupt
(Vector Table 0x0 0x3ff
) (IR .
:
Interrupts
Base Address
0x000
0x004
0x008
0x00C
0x010
0x014
0x018
0x01C
0x020
0x024
0x028
0x02C
0x030
0x034
0x038
0x03C
0x040
0x044
0x048
0x05C
0x068 - 0x3FF
1024 ) 256
(
.
. IVT . ( )
So ware Interrupts
..
...
Interrupt Descriptor)
256 IVT IDT ( Table
8 ( 256 )
GDT ( selector type: code or data) ( IR)
.
Bits 0-15:
Interrupt / Trap Gate: Oset address Bits 0-15 of IR
Task Gate: Not used.
Bits 16-31:
Interrupt / Trap Gate: Segment Selector (Useually 0x10)
Task Gate: TSS Selector
Bits 31-35: Not used
Bits 36-38:
Interrupt / Trap Gate: Reserved. Must be 0.
Task Gate: Not used.
Bits 39-41:
Interrupt Gate: Of the format 0D110, where D determins size
01110 - 32 bit descriptor
00110 - 16 bit descriptor
Task Gate: Must be 00101
Trap Gate: Of the format 0D111, where D determins size
01111 - 32 bit descriptor
00111 - 16 bit descriptor
Bits 42-44: Descriptor Privedlge Level (DPL)
00: Ring 0
01: Ring 1
10: Ring 2
11: Ring 3
Interrupts
) Bit 45: Segment is present (1: Present, 0:Not present
Bits 46-62:
Interrupt / Trap Gate: Bits 16-31 of IR address
Task Gate: Not used
.
.: Example of interrupt descriptor
Example
idt descriptor:
; 010001110
dw
0x0
dw
0x8
db
0x0
db
0x8e
dw
0x0
baseLow
selector
reserved
flags
baseHi
baseLow 16 IR
baseHi 0x0 .0x0
)( ) (Data selector
0x8 ) (Code Selector ) .(GDT
flags 010001110b 32-bit
).(Ring0
) (
IDT idt start
idt end idt ptr
:
.: Value to put in IDTR
Example
idt ptr:
) IDTR ( lidt
][idt ptr
.lidt
idtr
) (CPL ag .
..
So ware Interrupts
) IDT (IDTR
int num * 8 IDT 8 .
. EFLAGS CS
IP ) (Stack )Error
(Code . 32-bit .
Bit 0: External event
0: Internal or so ware event triggered the error.
1: External or hardware event triggered the error.
Bit 1: Descrip on loca on
0: Index por on of error code refers to descriptor in GDT or current LDT.
1: Index por on of error code refers to gate descriptor in IDT.
Bit 2: GDT/LDT. Only use if the descriptor loca on is 0.
0: This indicates the index por on of the error code refers to a descriptor in the current GDT.
1: This indicates the index por on of the error code refers to a segment or gate descriptor in
the LDT.
Bits 3-15: Segment selector index. This is an index into the IDT, GDT, or current LDT to the segment
or gate selector bring refrenced by the error code.
iret iretd
) .(FLAGS .
...
:
:Fault
.
:Trap .
Interrupts
:Abort .
. .
)
(cli General Protec on Fault
cli
.
IDT .
Prpgrammable Interval Timer
PIT System Timer
.
..
So ware Interrupts
.
Programmable Interrupt Controller PIC
.
) (IDT 256
)(GDT
) ( PIC
.
...
GDT
HAL
HAL - -
HAL
. HAL
HAL
.
on Layer Interface
.: include/hal.h:Hardware Abstrac
Example
#ifndef HAL H
#define HAL H
#ifndef i386
" #error "HAL is not implemented in this platform
#endif
><stdint.h
#include
#ifdef
)declspec(naked
MSC VER
#define interrupt
#else
Interrupts
#define interrupt
#endif
#define far
#define near
Interface /
extern int
#endif // HAL H
HAL extern
) (Implementa on . )( hal init
)( hal close
gen interrupt
.
) (GDT
.
:
:gdt install
.GDTR
.
.: hal/gdt.cpp:Install GDT
...
.
.hal/gdt.h
Example
So ware Interrupts
..
#include
<string.h>
#include "gdt.h"
#ifdef
MSC VER
Interrupts
else
return & gdt[index];
// init gdtr
gdtr.limit = sizeof(struct gdt desc) MAX GDT DESC 1;
gdtr.base = (uint32 t)& gdt[0];
//
);
// install gdtr
gdt install();
return 0;
..
..
Programmable Interrupt
Controller
) (PMode
PIT
.
) (.
PIC Programmable Interrupt Controller
8259A .PIC
:..
...
8259A
Hardware Interrupts
PIC
) (Interrupt Driven .
) (loop
Polling
) (
)(Asynchronous
) .(Synchronous
ng
.Busy Wai
Interrupts
:..
)(
Timer
0x08
IRQ0
0x09
IRQ1
PIC
0x0a
IRQ2
0x0b
IRQ3
0x0c
IRQ4
0x0d
IRQ5
0x0e
IRQ6
0x0f
IRQ7
CMOS
0x70
IRQ8/IRQ0
x86
IRQ9/IRQ1
IRQ10/IRQ2
IRQ11/IRQ3
IRQ12/IRQ4
IRQ13/IRQ5
IRQ14/IRQ6
IRQ15/IRQ7
0x71
0x72
0x73
0x74
0x75
0x76
0x77
FPU
. .
.
...
PIC
PIC ) (Signals
PIC
PIC
. ) (IRQ
PIC ). (Secondary/Slave PIC
) (Primary PIC
). (SouthBridge
..
PIC's Pins
) (
) .(Digital Logic . .
D0-D7
PIC CAS0, CAS1, CAS2
PIC INT
INTR INTA
INTA
IF and TF INTR
PIC
:.. PIC
INT
PIC ) End Of
(Interrupt . IR0...IR7
)
( PIC
PIC PIC
.
IR0 .
PIC
PIC :
) :(Command Reigster
.EOI
) :(Status Register .
Interrupts
Bit Number
0
1
2
3
4
5
6
7
0x20
0x21
0xA0
0xA1
:.. IRR/ISR/IMR
)IRQ Number (Slave controller) IRQ Number (Primary controller
IRQ8
IRQ0
IRQ9
IRQ1
IRQ10
IRQ2
IRQ11
IRQ3
IRQ12
IRQ4
IRQ13
IRQ5
IRQ14
IRQ6
IRQ15
IRQ7
:..
PIC
. .x86
PIC
PIC
.
Ini aliza on Control Words ICW
PIC Opera on Control Words . OCW
PIC . ICW1
..
:..
ICW4
IC4
0
SNGL PIC
1
x86
ADI
2
LTIM
3
1
4
x86
0
5
x86
0
6
x86
0
7
ICW1
.
.
ICW4
ICW4
PIC Level Triggered
Mode Edge Triggered Mode
PIC .x86 .
PIC .
aliza on Control Words 1
.: Ini
Example
Interrupts
:..
S0-S7 IRQ
0-7
:..
ID0 IRQ
0-2
3-7
3-7
.: Ini
Example
ICW3 PIC
IRQ . x86
IRQ2
. . .
PIC
IRQ IRQ
IRQ2 ) 0000100b (0x4
IRQ )
(010 ) (. .
.
aliza on Control Words 3
.: Ini
Example
..
:..
x86
EOI
ICW4
0
1
2
3
4
5-7
uPM
AEOI
M/S
BUF
SFNM
0
..
. x86 . PIC .
ICW4
.: Ini
Example
mov al, 1
PIC
) ( .
OCW OCW2
. . . -
.. .
Interrupts
:..
L0/L1/L2
0
EOI
SL
R
0-2
3-4
5
6
7
R Bit
0
0
0
0
1
1
1
1
SL Bit
0
0
1
1
0
0
1
1
OCW2
:..
EOI Bit
0
1
0
1
0
1
0
1
OCW2
Descrip on
)Rotate in Automa c EOI mode (CLEAR
Non specic EOI command
No opera on
Specic EOI command
)Rotate in Automa c EOI mode (SET
Rotate on non specic EOI
Set priority command
Rotate on specic EOI
Example
) PIC PIT (IR0
IR0 PIC IRQ
) (Interrupt Request Register . IRR
IRQ
0 .IR0 PIC Interrupt
..
Mask Register
. PIC
INTA .
IF
) (Acnowledges INTR PIC INTA
IRQ D0-D7 In Service Register
.
CS and EIP
IVT
.
EOI .
..
Interrupts
:..
...
8253
PIT
PIT's Pins
) (Data Bus
D0...D7
.
) ( WR
RD
:.. PIT
.
CS
CS ) (Address Bus
) .(Control Bus - A0,A1
- . )(CLK, OUT, and GATE
) (CLK (Clock Input) and GATE ) (OUT
) ( PIC
) (IRQ0 PIC
.
..
PIT
PIT ) (
.
. ) (Control Word
. .
) (A0,A1
) ( .
:.. 8253 PIT
RD WR A0 A1
Counter 0
0x40
Counter 1
0x41
Counter 2
0x42
Control Word
0x43
1
0
1
0
1
0
1
0
0
1
0
1
0
1
0
1
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
0
1
1
2
2
Control Word
) ( .
Bit 0: (BCP) Binary Counter
0: Binary
) 1: Binary Coded Decimal (BCD
Bit 1-3: (M0, M1, M2) Opera ng Mode. See above sec ons for a descrip on of each.
000: Mode 0: Interrupt or Terminal Count
001: Mode 1: Programmable one-shot
010: Mode 2: Rate Generator
011: Mode 3: Square Wave Generator
100: Mode 4: So ware Triggered Strobe
101: Mode 5: Hardware Triggered Strobe
110: Undened; Don't use
Interrupts
(milliseconds )100Hz .
.
Example
.: PIT programming
; COUNT = input hz / frequency
; FIRST send the command word to the PIT. Sets binary counting,
; Mode 3, Read or Load LSB first then MSB, Channel 0
; Now we can write to channel 0. Because we set the "Load LSB first
then MSB" bit, that is
; the way we send it
mov ax, dx
out 0x40, al
xchg ah, al
out 0x40, al
;LSB
;MSB
..
..
HAL
HAL
HAL
. PIC
). (IRQ0
...
PIC
.. PIC
. PIC ) (hal/pic.h
) (hal/pic.cpp .
. )
( .
.: hal/pic.h: PIC Interface
Example
0
1
3
4
0
1
4
5
6
1
2
4
.. .
Interrupts
0x20
0x40
#define I386 PIC OCW2 MASK ROTATE 0x80
#define I386 PIC OCW2 MASK SL
1
2
PIC OCW3 MASK MODE
4
PIC OCW3 MASK SMM
0x20
PIC OCW3 MASK ESMM
0x40
PIC OCW3 MASK D7
0x80
0x20
0x20
0x21
0x21
0xa0
0xa0
0xa1
0xa1
0x1
0x2
0x4
0x8
0x10
0x1
0x2
#define I386 PIC ICW4 MASK MS
0x4
#define I386 PIC ICW4 MASK BUF
0x8
#define I386 PIC ICW4 MASK SFNM
0x10
#define I386 PIC ICW4 MASK AEOI
HAL
..
2
0
PIC ICW1 ADI CALLINTERVAL4
4
PIC ICW1 ADI CALLINTERVAL8
0
PIC ICW1 LTIM LEVELTRIGGERED 8
PIC ICW1 LTIM EDGETRIGGERED 0
PIC ICW1 INIT YES
0x10
PIC ICW1 INIT NO
0
1
0
2
0
4
0
8
0
0x10
0
. .
.
Example
on
Interrupts
uint8 t reg = (pic num == 1)?I386 PIC2 DATA REG:I386 PIC1 DATA REG;
return inportb(reg);
uint8 t reg = (pic num == 1)?I386 PIC2 DATA REG:I386 PIC1 DATA REG;
outportb(reg,data);
uint8 t icw = 0;
disable irq();
HAL
..
PIT
Example
0x40
0x41
0x42
0x43
0x1
0xe
0x30
0xc0
0x0
0x1
0x0
0x2
RATEGEN
0x4
SQUAREWAVEGEN
0x6
SOFTWARETRIG
0x8
HARDWARETRIG
0xa
...
Interrupts
#define I386 PIT OCW RL LATCH
0x0
0x10
0x20
0x30
0x0
0x40
0x80
uint8 t mode);
extern void cdecl i386 pit init();
extern bool cdecl i386 pit is initialized();
Example
on
pit ticks = 0;
pit is init = false;
void
uint8 t port;
HAL
..
outportb(port,uint8 t(data));
uint8 t port;
return inportb(port);
uint32 t prev =
pit ticks = i;
return prev;
pit ticks;
return
pit ticks;
mode) {
if (freq == 0)
return;
Interrupts
void
bool
void
asm {
add esp,12
pushad
pit ticks++;
int done(0);
asm {
popad
iretd
HAL ...
HAL
Example
HAL
..
extern int
extern int
Example
on
int
/ enable irq
enable irq();
return 0;
int
void
#ifdef
Interrupts
address:
int 0
#endif
void
if (n > 7)
/ send EOI to pic2 /
i386 pic send command(I386 PIC OCW2 MASK EOI,1);
void
unsigned char
#ifdef
asm {
mov dx,word ptr [port num]
in al,dx
mov byte ptr [port num],al
#endif
void
#ifdef
HAL
..
asm {
mov al,byte ptr[value]
mov dx,word ptr[port num]
out dx,al
#endif
asm sti
#endif
void
#ifdef
asm cli
#endif
void
#ifdef
void
cdecl set vector(unsigned int int num,void ( cdecl far & vect)
()) {
i386 idt install ir(int num,I386 IDT 32BIT | I386 IDT PRESENT /
10001110/,0x8 /code desc/,vect);
if (desc == 0)
return 0;
const char
Interrupts
int
Example
int
.: kernel/main.cpp
cdecl main()
hal init();
enable irq();
set
set
set
set
set
set
set
set
set
set
set
set
set
set
set
set
set
set
...
Example
/
.: kernel/excep
on.h
Execption Handler /
/ Divide by zero
extern void
HAL
..
/ Single step
extern void
/
cdecl single step trap(uint32 t cs,uint32 t eip,uint32 t
eflags);
/
cdecl nmi trap(uint32 t cs,uint32 t eip,uint32 t eflags)
/ Breakpoint hit
extern void
/
cdecl breakpoint trap(uint32 t cs,uint32 t eip,uint32 t
eflags);
/ Overflow trap
extern void
/
cdecl overflow trap(uint32 t cs,uint32 t eip,uint32 t
eflags);
/ Bounds check
extern void
/
cdecl invalid opcode fault(uint32 t cs,uint32 t eip,
uint32 t eflags);
extern void
eflags);
/ Double Fault
/
cdecl double fault abort(uint32 t cs,uint32 t err,
uint32 t eip,uint32 t eflags);
extern void
/ Invalid TSS
extern void
/
cdecl no segment fault(uint32 t cs,uint32 t err,uint32 t
eip,uint32 t eflags);
extern void
Interrupts
/ Stack fault
/
cdecl stack fault(uint32 t cs,uint32 t err,uint32 t eip,
uint32 t eflags);
extern void
extern void
/ Page Fault
/
cdecl page fault(uint32 t cs,uint32 t err,uint32 t eip,
uint32 t eflags);
extern void
/ FPU error
extern void
);
/ Alignment Check
/
cdecl alignment check fault(uint32 t cs,uint32 t err,
uint32 t eip,uint32 t eflags);
extern void
/ Machine Check
extern void
/
cdecl simd fpu fault(uint32 t cs,uint32 t eip,uint32 t
eflags);
Example
.: kernel/excep
on.cpp
/ Divide by zero
/
cdecl divide by zero fault(uint32 t cs,uint32 t eip,uint32 t
eflags) {
kernel panic("Divide by 0");
for (;;);
void
Example
.: kernel/panic.cpp
void
HAL
..
disable irq();
va
va
/
va
list args;
start(args,msg);
missing /
end(args);
kclear(0x1f);
kgoto xy(0,0);
kset color(0x1f);
kputs(panic);
kprintf(" STOP: %s",msg);
/ hang /
for (;;) ;
HAL
:..
Interrupts
:..
.
) (RAM
.
) (Memory Manager
.
.
..
) (Physical Memoery
)
(DRAM
) (North Bridge
0x0 0x1
.
.
). (Physical Address Space
n n-1 2n-1
) (Holes
.
.
RAM
...
.
.
.
int 0x12
) (0x413
. .
Example
;
; get conventional memory size
; ret ax=KB size from address 0
;
get conventional memory size:
int 0x12
ret
0x15
0xe801
:CF .
:EAX 1 MB .16 MB
:EBX 16 MB
.
..
push ecx
push edx
xor ecx,ecx
xor edx,edx
mov ax,0x801
int 0x15
jc .error
cmp ah,0x86
je .error
cmp ah,0x80
je .error
mov ax,cx
mov bx,dx
.use eax:
pop edx
pop ecx
ret
.error:
.
ax,1
bx,0
edx
ecx
mov
mov
pop
pop
ret
) (ax
bx
2*64 128.
...
Memory Map
)
(
. int 0x15
e820 :
:CF .
:ebx .
:ecx .
:es:di .
:eax .0xe820
:ebx .
:ecx ) (buer .
:es:di buer .
..
Buer
ebx . ..
.: Memory Map Entry Structure
Example
.
:
:1 .
:2 .
. .
.: Get Memory Map
Example
0x0000E820
continuation value or 0 to start at beginning of map
)size of buffer for result (Must be >= 20 bytes
)'0x534D4150h ('SMAP
= Buffer for result
= EAX
= EBX
= ECX
= EDX
ES:DI
;
;
;
;
;
;
; Return:
CF = clear if successful
.
;
;
;
;
;
;
pushad
xor ebx,ebx
xor bp,bp
mov edx,'PAMS'
; 0x534D4150
mov eax,0xe820
mov ecx,24
int 0x15
; BIOS get memory map.
jc .error
cmp eax,'PAMS'
jne .error
test ebx,ebx
je .error
jmp .start
.next
mov
mov
mov
int
entry:
edx,'PAMS'
; 0x534D4150
eax,0xe820
ecx,24
0x15
; BIOS get memory map.
.start:
jcxz .skip entry
.good entry:
..
inc bp
add di,24
entry:
ebx,0
.next entry
.done
.skip
cmp
jne
jmp
.error:
stc
.done:
popad
ret
endstruc
...
) (Bootloader
.
) (Mul boot Informa on .
.
.
) (Machine State
:
:eax 0x2BADB002
.
:ebx .
.
0x0 / :cs
.0x
:ds,es,fs,gs,ss
.0x
.a20
) ) ( 0 :cr0
.(
0x0
ebx
. .(stack)
.
Example
.: Mul
boot info:
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
at
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
multiboot
info.flags,
dd 0
info.mem low,
dd 0
info.mem high,
dd 0
info.boot device,
dd 0
info.cmd line,
dd 0
info.mods count,
dd 0
info.mods addr,
dd 0
info.sym0,
dd 0
info.sym1,
dd 0
info.sym2,
dd 0
info.mmap length,
dd 0
info.mmap addr,
dd 0
info.drives length,
dd 0
info.drives addr,
dd 0
info.config table,
dd 0
info.bootloader name,
dd 0
info.apm table,
dd 0
at
at
at
at
at
iend
multiboot
multiboot
multiboot
multiboot
multiboot
..
info.vbe
info.vbe
info.vbe
info.vbe
info.vbe
control info,
dd
mode info,
dw 0
interface seg, dw
interface off, dw
interface len, dw
0
0
0
0
ags
mem high mem low ags[0]
mem low ags .
mem high
mmap length mmap addr
. .
.
Example
...
; when stage2 begin started, BIOS put drive number where stage1 are
loaded from in dl
mov [boot info+multiboot info.boot device],dl
...
;
; Get Memory Size
xor eax,eax
xor ebx,ebx
call get memory size
...
;
; Pass MultiBoot Info to the Kernel
mov eax,0x2badb002
mov ebx,0
mov edx,[kernel size]
push dword boot info
call ebp
; Call Kernel
) (
. . .
Example
void
.: Kernel Entry
cdecl kernel entry (multiboot info boot info)
#ifdef i386
asm {
cli
mov
mov
mov
mov
mov
ax,
ds,
es,
fs,
gs,
10h
ax
ax
ax
ax
#endif
#ifdef i386
asm {
cli
hlt
..
#endif
for(;;);
...
Example
#ifndef PMM H
#define PMM H
#include
<stdint.h>
#endif // PMM H
Example
on
.
#include
<string.h>
#include "pmm.h"
#include "kdisplay.h"
4096
PMM PAGE SIZE
#define PMM BLOCK ALIGN
PMM BLOCK SIZE
#define PMM BLOCK SIZE
static uint32 t
pmm mmap[bit/32] =
pmm mmap[bit/32] =
{
return pmm mmap[bit/32] & (1 << (bit%32));
return 1;
..
if (s == 0)
return 1;
if (s == 1)
return mmap find first();
if (free bit == s)
return i32+j;
return 1;
pmm
pmm
pmm
pmm
.
void pmm init region(uint32 t base,size t size)
mmap set(0);
if (block == 1)
return 0;
mmap set(block);
..
return 0;
if (block == 1)
return 0;
return
return
.
uint32 t pmm get used block count()
return
return
#ifdef
MSC VER
asm {
mov eax,cr0
cmp [val],1
je enable
jmp disable
enable:
or eax,0x80000000
mov cr0,eax
jmp done
disable:
and eax,0x7fffffff // unset last bit
mov cr0,eax
done:
#endif
uint32 t val = 0;
#ifdef MSC VER
asm {
mov eax,cr0
..
mov [val],eax
else
true;
#endif
#ifdef
MSC VER
asm {
mov eax,[addr]
mov cr3,eax
#endif
MSC VER
asm {
mov eax,cr3
ret
#endif
#ifndef VMM H
#define VMM H
#include
<stdint.h>
..
};
};
addr);
extern uint32 t vmm vir to page dir table index(uint32 t addr);
extern void vmm page dir table clear(page dir table pd);
extern uint32 t vmm page dir table lookup entry(page dir table pd,
uint32 t addr);
#endif // VMM H
Example
on
#include "vmm.h"
#include
<string.h>
#include "vmm.h"
#include "pmm.h"
4096
..
for (int i=0, frame =0; i < 1024 ; ++i, frame += 4096) {
uint32 t page = 0;
pte add attrib(&page,I386 PTE PRESENT);
pte set frame(&page,frame);
if (!p)
return false;
return true;
if (p)
pmm dealloc(p);
if (!pd)
return false;
return
#ifdef
MSC VER
asm {
cli
invlpg addr
sti
#endif
..
if (pt)
memset(pt,0,sizeof(page table));
if (pt)
return &pt>pte[vmm vir to page table index(addr)];
else
return 0;
if (pd)
memset(pd,0,sizeof(page dir table));
uint32 t vmm page dir table lookup entry(page dir table pd,uint32 t
addr) {
if (pd)
return &pd>pde[vmm vir to page table index(addr)];
else
return 0;
Example
Device Driver
Keyboard Driver
..
#ifndef KEYBOARD H
#define KEYBOARD H
#include
<stdint.h>
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
SPACE
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
G
H
I
J
K
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
' ',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
Device Driver
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
RETURN
ESCAPE
BACKSPACE
UP
DOWN
LEFT
RIGHT
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
F13
F14
F15
DOT
COMMA
COLON
SEMICOLON
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'\r',
0x1001,
'\b',
0x1100,
0x1101,
0x1102,
0x1103,
0x1201,
0x1202,
0x1203,
0x1204,
0x1205,
0x1206,
0x1207,
0x1208,
0x1209,
0x120a,
0x120b,
0x120b,
0x120c,
0x120d,
0x120e,
'.',
',',
':',
';',
Keyboard Driver
..
KEY SLASH
KEY BACKSLASH
KEY PLUS
KEY MINUS
KEY ASTERISK
KEY EXCLAMATION
KEY QUESTION
KEY QUOTEDOUBLE
KEY QUOTE
KEY EQUAL
KEY HASH
KEY PERCENT
KEY AMPERSAND
KEY UNDERSCORE
KEY LEFTPARENTHESIS
KEY RIGHTPARENTHESIS
KEY LEFTBRACKET
KEY RIGHTBRACKET
KEY LEFTCURL
KEY RIGHTCURL
KEY DOLLAR
KEY POUND
KEY EURO
KEY LESS
KEY GREATER
KEY BAR
KEY GRAVE
KEY TILDE
KEY AT
KEY CARRET
KEY KP 0
KEY KP 1
KEY KP 2
KEY KP 3
KEY KP 4
KEY KP 5
KEY KP 6
KEY KP 7
KEY KP 8
KEY KP 9
KEY KP PLUS
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
'/',
' \\ ',
'+',
'',
'',
'!',
'?',
'\"',
'\'',
'=',
'#',
'%',
'&',
' ',
'(',
')',
'[',
']',
'{',
'}',
'$',
'',
'$',
'<',
'>',
' | ',
'`',
'',
'@',
'',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
Device Driver
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KEY
KP MINUS
KP DECIMAL
KP DIVIDE
KP ASTERISK
KP NUMLOCK
KP ENTER
TAB
CAPSLOCK
LSHIFT
LCTRL
LALT
LWIN
RSHIFT
RCTRL
RALT
RWIN
INSERT
DELETE
HOME
END
PAGEUP
PAGEDOWN
SCROLLLOCK
PAUSE
UNKNOWN,
NUMKEYCODES
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
'',
'.',
'/',
'',
0x300f,
0x3010,
0x4000,
0x4001,
0x4002,
0x4003,
0x4004,
0x4005,
0x4006,
0x4007,
0x4008,
0x4009,
0x400a,
0x400b,
0x400c,
0x400d,
0x400e,
0x400f,
0x4010,
0x4011,
};
Keyboard Driver
..
#endif // KEYBOARD H
Example
on
{
//
static bool
extended = false;
Device Driver
int code = 0;
Keyboard Driver
..
break;
else {
// this is a make code set the scan code
scancode = code;
Device Driver
capslock,
scrolllock);
.
.
.
. .
.
.
NASM
.
) GRUB
( .
.
First Fit
.Bit Map
Device Driver
.
FAT12,FAT16,FAT32,EXT3,EXT4,...etc .
:
.POSIX
.FAT12,FAT16,FAT32,EXT3,EXT4,...etc
Bibliography
[] William Stallings, Operating System: Internals and Design Principles. Prentice Hall, 5th
Edition, 2004.
[] Andrew S. Tanenbaum ,Albert S Woodhull, Operating Systems Design and Implementation.
Prentice Hall, 3rd Edition, 2006.
[] Michael Tischer, Bruno Jennrich, PC Intern: The Encyclopedia of System Programming.
Abacus Software, 6th Edition, 1996.
[] Hans-Peter Messmer, The Indispensable PC Hardware Book. Addison-Wesley Professional, 4th Edition, 2001.
[] Andrew S. Tanenbaum, Structured Computer Organization. Prentice Hall, 4th Edition,
1998.
[] Ytha Yu,Charles Marut, Asssembly Language Programming and Organization IBM PC.
McGraw-Hill/Irwin, 1st Edition, 1992.
[] Intel Manuals, Intel 64 and IA-32 Architectures Software Developer's Manuals. http:
//www.intel.com/products/processor/manuals/
[] OSDev: http://wiki.osdev.org
[] brokenthorn: http://brokenthorn.com
[] Computer Sciense Student's Community in Sudan: http://sudancs.com
[] Wikipedia: http://wikipedia.org
.
.
++ .
..
..