Introduction To The x86 Microprocessor
Introduction To The x86 Microprocessor
Introduction To The x86 Microprocessor
Compiled code/
Programming Language level Assembly code
Advanced Addressing
modes
Sophisticated
Instruction set
Support for Memory
Assembly Language level Management
and Task Management
Multiuser OS - Protection,
Virtual
Memory, Context Switching
Computer
Microprogramming level biography
interesting
Intel
Architecture
Digital Logic level
Memory Management Ensured
by
Multi User Operating Systems Segmenta
Ease of Programming tion
Process Mobility in the Address Space
Multiprocess Context switching
Protection across Processes Ensured
• Intra process protection: Separation of Code, by
Data and Stack Paging
• Inter process protection
Virtual Memory
4GB address space for every process
Main Memory
Code_Segment: 0000
if (j>k) Code and Data
segments are
max = j mov EAX, [0] Operating System
separate
mov EBX, [4] and both (Kernel)
else
assumed
max = k cmp EAX,EBX to start from 0 0700
Multiple Segments
Process 1
C CS
S Process 1
DS
Process 1 D Process 2
in S CS
Execution Process 2
SS
S
Process 2
S DS
Process 2 Process 1
SS
in
Execution
Multiprocess Context switching
Other Registers
EFLAGS – 32 Bit Register
VM RF NT IO IO OF DF IF TF SF ZF AF PF CF
PL PL
Descriptor Table
Linear
Address
•A process always executes from Code segment. It should
not execute by accessing from adjoining Data or stack area
or any other code area too.
•A stack should not overgrow into adjoining segments
500
C Every segment is specified a
S 1000
start address and limit.
ES 1500 Architecture checks if limit
is not exceeded.
SS 2000
jmp
mov jmp
PUSH
CS:501
POP
PUSH CS:250
[ES:498],
POP
mov
EAX
EAX
AX
[ES:498],
//This
AX //Let
//Let
//Let
EAXis SP
aSP
SP//This
AX
//This
violation
be
be
be
//This
2,498,
2,
498,itaisasfine
isViolation!!!
it
is
violation!!!
violation
is
fine
limit
fineis 500
Interprocess Protection
Privilege levels and Protection
Every segment has an associated privilege level
and hence any code segment will have an
associated privilege level.
The CPL (Current Privilege Level) of a process is
the privilege level of the code segment, the code
stored in which, it is executing.
A process can access segments that have privilege
levels numerically greater than or equal to (less
privileged than) its CPL.
Protection Implementation
Every segment is associated with a descriptor
stored in a descriptor table.
The privilege level of any segment is stored in its
descriptor.
The descriptor table is maintained in memory and
the starting location of the table is pointed to by a
Descriptor Table Register (DTR).
The segment register stores an offset into this
table.
Structure of a Descriptor
Updating Segment registers
Segment registers (DS, ES, SS, GS and FS) are
updated by normal MOV instructions.
MOV AX, 0x10 ; MOV DS, AX
The above command is successful if and only if
the descriptor stored at the offset 0x10 in the
descriptor table has a privilege level numerically
greater than or equal to the CPL.
A process with CPL = 3 cannot load the segment
descriptor of CPL <= 2, and hence cannot access
the segments.
Updating segment registers
The code segment register is updated by normal
jump/call operations.
jmp 0x20:0x1000
This updates the CS by 0x20, provided the descriptor
stored at offset 0x20 has a privilege level numerically
greater than or equal to CPL
Other modes of updating CS register
Numerically higher to lower Privilege Levels using
CALL gates – useful for system calls.
Any privilege level to any other privilege level using
task switch.
Descriptor Tables
There are two descriptor tables
Global Descriptor Tables
Local Descriptor Tables
The global descriptor table’s base address is stored
in GDTR
The local descriptor table’s base address is stored
in LDTR
The two privileged instructions LGDT and LLDT
loads the GDTR and LDTR.
Structure of a Selector
15 2 0
Index T1
T1 = 0 GDT
= 1 LDT
LDTR LDTR
If at all each process should access memory, it has to use the descriptors in its LDTR
only and it cannot change the LDTR/LDT/GDTR/GDT contents as they would be
maintained in a higher privileged memory area.
Did You Note!!
There is an 100 % degradation in Memory
access time – because every memory access
is two accesses now, one for getting the
base address and another for actually
accessing the data.
A solution indeed: Along with the segment
registers, keep a shadow registers which
stores additional necessary information.
Visible part Hidden part
DS
ES
FS
GS
Be Careful add [DS:20],eax
Logical
Address
0x10 20
Linear address
Descriptor Table will still be 120
Have to execute
mov DS,0x10
again to get the
answer as 220,
Base
Base = 200
= 100 Base Address
as this would
update the
hidden part
Linear
Address 120
Changing Base
Virtual Memory and Paging
It is always enough if the next instruction to
be executed and the data needed to execute
the same are available in the memory.
The complete code and data segment need
not be available.
Use of paging to realize the stuff!
By using segmentation the processor
calculates an 32-bit effective address.
Paging fundamentals
Each page is 4096 bytes
Physical RAM has page frames like photo frames,
which is also 4096 bytes.
A page is copied into the page frame, if needed
and removed to accommodate some other page.
By this, a 4 GB code can run on a 128MB physical
memory
This is also called demand paging.
Protected Mode Addressing with paging
10 10 12
DIR TABLE OFFSET
PAGE FRAME
Limit
Limit
Addressa
ble area
•Not only the selector for the target code segment, but
also the offset in the code segment from which you
should start executing is specified. The source code
segment can only use it like a black-box
Calling Higher privileged code
Code Desc
Correct Incorrect
Call Gates
Are defined like segment descriptors
Occupy a slot in the descriptor tables
Provide the only means to alter the current
privilege level
Define entry points to other privilege levels
Must be invoked using a CALL Instruction
Call Gate accessibility
Target DPL <= Max (RPL, CPL) <= Gate
DPL
For eg. CPL = 2 and the target PL = 0, you
should use a Gate with PL = 2 or 3
Privilege levels and Stacks
The stack PL = CPL always
When changing the CPL, the processor
automatically changes the stack!!!
How – using the Task State Segment (TSS)
The base of the TSS is stored in a Task register
(TR) which is updated by the privileged
instruction LTR
The TSS associates a stack for each code for each
of the privilege levels 0, 1 and 2
Task Switching process