<s>
x86	B-Device
memory	I-Device
segmentation	I-Device
refers	O
to	O
the	O
implementation	O
of	O
memory	B-General_Concept
segmentation	I-General_Concept
in	O
the	O
Intel	B-Operating_System
x86	I-Operating_System
computer	O
instruction	B-General_Concept
set	I-General_Concept
architecture	I-General_Concept
.	O
</s>
<s>
Segmentation	B-General_Concept
was	O
introduced	O
on	O
the	O
Intel	B-General_Concept
8086	I-General_Concept
in	O
1978	O
as	O
a	O
way	O
to	O
allow	O
programs	O
to	O
address	O
more	O
than	O
64KB	O
(	O
65,536	O
bytes	O
)	O
of	O
memory	O
.	O
</s>
<s>
The	O
Intel	B-General_Concept
80286	I-General_Concept
introduced	O
a	O
second	O
version	O
of	O
segmentation	B-General_Concept
in	O
1982	O
that	O
added	O
support	O
for	O
virtual	B-Architecture
memory	I-Architecture
and	O
memory	B-General_Concept
protection	I-General_Concept
.	O
</s>
<s>
At	B-Operating_System
this	O
point	O
the	O
original	O
mode	O
was	O
renamed	O
to	O
real	B-Application
mode	I-Application
,	O
and	O
the	O
new	O
version	O
was	O
named	O
protected	B-Application
mode	I-Application
.	O
</s>
<s>
The	O
x86-64	B-Device
architecture	O
,	O
introduced	O
in	O
2003	O
,	O
has	O
largely	O
dropped	O
support	O
for	O
segmentation	B-General_Concept
in	O
64-bit	O
mode	O
.	O
</s>
<s>
In	O
both	O
real	O
and	O
protected	B-Application
modes	I-Application
,	O
the	O
system	O
uses	O
16-bit	O
segment	B-General_Concept
registers	I-General_Concept
to	O
derive	O
the	O
actual	O
memory	B-General_Concept
address	I-General_Concept
.	O
</s>
<s>
In	O
real	B-Application
mode	I-Application
,	O
the	O
registers	O
CS	O
,	O
DS	O
,	O
SS	O
,	O
and	O
ES	O
point	O
to	O
the	O
currently	O
used	O
program	O
code	B-General_Concept
segment	I-General_Concept
(	O
CS	O
)	O
,	O
the	O
current	O
data	B-General_Concept
segment	I-General_Concept
(	O
DS	O
)	O
,	O
the	O
current	O
stack	B-General_Concept
segment	I-General_Concept
(	O
SS	O
)	O
,	O
and	O
one	O
extra	O
segment	O
determined	O
by	O
the	O
programmer	O
(	O
ES	O
)	O
.	O
</s>
<s>
The	O
Intel	B-General_Concept
80386	I-General_Concept
,	O
introduced	O
in	O
1985	O
,	O
adds	O
two	O
additional	O
segment	B-General_Concept
registers	I-General_Concept
,	O
FS	B-Device
and	I-Device
GS	I-Device
,	O
with	O
no	O
specific	O
uses	O
defined	O
by	O
the	O
hardware	O
.	O
</s>
<s>
The	O
way	O
in	O
which	O
the	O
segment	B-General_Concept
registers	I-General_Concept
are	O
used	O
differs	O
between	O
the	O
two	O
modes	O
.	O
</s>
<s>
Instructions	O
are	O
always	O
fetched	O
from	O
the	O
code	B-General_Concept
segment	I-General_Concept
.	O
</s>
<s>
Any	O
stack	B-General_Concept
push	O
or	O
pop	O
or	O
any	O
data	O
reference	O
referring	O
to	O
the	O
stack	B-General_Concept
uses	O
the	O
stack	B-General_Concept
segment	I-General_Concept
.	O
</s>
<s>
All	O
other	O
references	O
to	O
data	O
use	O
the	O
data	B-General_Concept
segment	I-General_Concept
.	O
</s>
<s>
FS	B-Device
and	I-Device
GS	I-Device
have	O
no	O
hardware-assigned	O
uses	O
.	O
</s>
<s>
The	O
instruction	O
format	O
allows	O
an	O
optional	O
segment	O
prefix	O
byte	B-Application
which	O
can	O
be	O
used	O
to	O
override	O
the	O
default	O
segment	O
for	O
selected	O
instructions	O
if	O
desired	O
.	O
</s>
<s>
In	O
real	B-Application
mode	I-Application
or	O
V86	B-Application
mode	I-Application
,	O
the	O
size	O
of	O
a	O
segment	O
can	O
range	O
from	O
1	O
byte	B-Application
up	O
to	O
65,536	O
bytes	O
(	O
using	O
16-bit	O
offsets	O
)	O
.	O
</s>
<s>
The	O
16-bit	O
segment	B-Device
selector	I-Device
in	O
the	O
segment	B-General_Concept
register	I-General_Concept
is	O
interpreted	O
as	O
the	O
most	O
significant	O
16	O
bits	O
of	O
a	O
linear	O
20-bit	O
address	O
,	O
called	O
a	O
segment	O
address	O
,	O
of	O
which	O
the	O
remaining	O
four	O
least	O
significant	O
bits	O
are	O
all	O
zeros	O
.	O
</s>
<s>
The	O
segment	O
address	O
is	O
always	O
added	O
to	O
a	O
16-bit	O
offset	O
in	O
the	O
instruction	O
to	O
yield	O
a	O
linear	B-General_Concept
address	I-General_Concept
,	O
which	O
is	O
the	O
same	O
as	O
physical	B-General_Concept
address	I-General_Concept
in	O
this	O
mode	O
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
segmented	B-Device
address	I-Device
06EFh:1234h	O
(	O
here	O
the	O
suffix	O
"	O
h	O
"	O
means	O
hexadecimal	O
)	O
has	O
a	O
segment	B-Device
selector	I-Device
of	O
06EFh	O
,	O
representing	O
a	O
segment	O
address	O
of	O
06EF0h	O
,	O
to	O
which	O
the	O
offset	O
is	O
added	O
,	O
yielding	O
the	O
linear	B-General_Concept
address	I-General_Concept
06EF0h+1234h	O
=	O
08124h	O
.	O
</s>
<s>
Because	O
of	O
the	O
way	O
the	O
segment	O
address	O
and	O
offset	O
are	O
added	O
,	O
a	O
single	O
linear	B-General_Concept
address	I-General_Concept
can	O
be	O
mapped	O
to	O
up	O
to	O
212	O
=	O
4096	O
distinct	O
segment:offset	B-General_Concept
pairs	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
linear	B-General_Concept
address	I-General_Concept
08124h	O
can	O
have	O
the	O
segmented	O
addresses	O
06EFh:1234h	O
,	O
0812h:0004h	O
,	O
0000h:8124h	O
,	O
etc	O
.	O
</s>
<s>
While	O
real	B-Application
mode	I-Application
segments	O
are	O
always	O
64KB	O
long	O
,	O
the	O
practical	O
effect	O
is	O
only	O
that	O
no	O
segment	O
can	O
be	O
longer	O
than	O
64KB	O
,	O
rather	O
than	O
that	O
every	O
segment	O
must	O
be	O
64KB	O
long	O
.	O
</s>
<s>
Because	O
there	O
is	O
no	O
protection	O
or	O
privilege	O
limitation	O
in	O
real	B-Application
mode	I-Application
,	O
even	O
if	O
a	O
segment	O
could	O
be	O
defined	O
to	O
be	O
smaller	O
than	O
64KB	O
,	O
it	O
would	O
still	O
be	O
entirely	O
up	O
to	O
the	O
programs	O
to	O
coordinate	O
and	O
keep	O
within	O
the	O
bounds	O
of	O
their	O
segments	O
,	O
as	O
any	O
program	O
can	O
always	O
access	O
any	O
memory	O
(	O
since	O
it	O
can	O
arbitrarily	O
set	O
segment	B-Device
selectors	I-Device
to	O
change	O
segment	O
addresses	O
with	O
absolutely	O
no	O
supervision	O
)	O
.	O
</s>
<s>
Therefore	O
,	O
real	B-Application
mode	I-Application
can	O
just	O
as	O
well	O
be	O
imagined	O
as	O
having	O
a	O
variable	O
length	O
for	O
each	O
segment	O
,	O
in	O
the	O
range	O
1	O
to	O
65,536	O
bytes	B-Application
,	O
that	O
is	O
just	O
not	O
enforced	O
by	O
the	O
CPU	O
.	O
</s>
<s>
(	O
The	O
leading	O
zeros	O
of	O
the	O
linear	B-General_Concept
address	I-General_Concept
,	O
segmented	O
addresses	O
,	O
and	O
the	O
segment	O
and	O
offset	O
fields	O
are	O
shown	O
here	O
for	O
clarity	O
.	O
</s>
<s>
The	O
effective	O
20-bit	O
address	B-General_Concept
space	I-General_Concept
of	O
real	B-Application
mode	I-Application
limits	O
the	O
addressable	B-General_Concept
memory	I-General_Concept
to	O
220bytes	O
,	O
or	O
1,048,576	O
bytes	O
(	O
1MB	O
)	O
.	O
</s>
<s>
This	O
derived	O
directly	O
from	O
the	O
hardware	O
design	O
of	O
the	O
Intel8086	O
(	O
and	O
,	O
subsequently	O
,	O
the	O
closely	O
related	O
8088	O
)	O
,	O
which	O
had	O
exactly	O
20	O
address	B-Architecture
pins	I-Architecture
.	O
</s>
<s>
Each	O
segment	O
begins	O
at	B-Operating_System
a	O
multiple	O
of	O
16bytes	O
,	O
called	O
a	O
paragraph	O
,	O
from	O
the	O
beginning	O
of	O
the	O
linear	O
(	O
flat	O
)	O
address	B-General_Concept
space	I-General_Concept
.	O
</s>
<s>
That	O
is	O
,	O
at	B-Operating_System
16byte	O
intervals	O
.	O
</s>
<s>
Since	O
all	O
segments	O
are	O
64KB	O
long	O
,	O
this	O
explains	O
how	O
overlap	O
can	O
occur	O
between	O
segments	O
and	O
why	O
any	O
location	O
in	O
the	O
linear	O
memory	B-General_Concept
address	I-General_Concept
space	O
can	O
be	O
accessed	O
with	O
many	O
segment:offset	B-General_Concept
pairs	O
.	O
</s>
<s>
The	O
actual	O
location	O
of	O
the	O
beginning	O
of	O
a	O
segment	O
in	O
the	O
linear	B-General_Concept
address	I-General_Concept
space	I-General_Concept
can	O
be	O
calculated	O
with	O
segment×16	O
.	O
</s>
<s>
A	O
segment	O
value	O
of	O
0Ch	O
(	O
12	O
)	O
would	O
give	O
a	O
linear	B-General_Concept
address	I-General_Concept
at	B-Operating_System
C0h	O
(	O
192	O
)	O
in	O
the	O
linear	B-General_Concept
address	I-General_Concept
space	I-General_Concept
.	O
</s>
<s>
0Ch:0Fh	O
(	O
12:15	O
)	O
would	O
be	O
C0h+0Fh	O
=	O
CFh	O
(	O
192+15	O
=	O
207	O
)	O
,	O
CFh	O
(	O
207	O
)	O
being	O
the	O
linear	B-General_Concept
address	I-General_Concept
.	O
</s>
<s>
Such	O
address	O
translations	O
are	O
carried	O
out	O
by	O
the	O
segmentation	B-General_Concept
unit	O
of	O
the	O
CPU	O
.	O
</s>
<s>
The	O
last	O
segment	O
,	O
FFFFh	O
(	O
65535	O
)	O
,	O
begins	O
at	B-Operating_System
linear	B-General_Concept
address	I-General_Concept
FFFF0h	O
(	O
1048560	O
)	O
,	O
16bytes	O
before	O
the	O
end	O
of	O
the	O
20bit	O
address	B-General_Concept
space	I-General_Concept
,	O
and	O
thus	O
,	O
can	O
access	O
,	O
with	O
an	O
offset	O
of	O
up	O
to	O
65,536	O
bytes	B-Application
,	O
up	O
to	O
65,520	O
(	O
65536−16	O
)	O
bytes	B-Application
past	O
the	O
end	O
of	O
the	O
20bit	O
8088	O
address	B-General_Concept
space	I-General_Concept
.	O
</s>
<s>
On	O
the	O
8088	O
,	O
these	O
address	O
accesses	O
were	O
wrapped	O
around	O
to	O
the	O
beginning	O
of	O
the	O
address	B-General_Concept
space	I-General_Concept
such	O
that	O
65535:16	O
would	O
access	O
address	O
0	O
and	O
65533:1000	O
would	O
access	O
address952	O
of	O
the	O
linear	B-General_Concept
address	I-General_Concept
space	I-General_Concept
.	O
</s>
<s>
The	O
use	O
of	O
this	O
feature	O
by	O
programmers	O
led	O
to	O
the	O
Gate	O
A20	O
compatibility	O
issues	O
in	O
later	O
CPU	O
generations	O
,	O
where	O
the	O
linear	B-General_Concept
address	I-General_Concept
space	I-General_Concept
was	O
expanded	O
past	O
20bits	O
.	O
</s>
<s>
In	O
16-bit	O
real	B-Application
mode	I-Application
,	O
enabling	O
applications	O
to	O
make	O
use	O
of	O
multiple	O
memory	B-General_Concept
segments	I-General_Concept
(	O
in	O
order	O
to	O
access	O
more	O
memory	O
than	O
available	O
in	O
any	O
one	O
64K-segment	O
)	O
is	O
quite	O
complex	O
,	O
but	O
was	O
viewed	O
as	O
a	O
necessary	O
evil	O
for	O
all	O
but	O
the	O
smallest	O
tools	O
(	O
which	O
could	O
do	O
with	O
less	O
memory	O
)	O
.	O
</s>
<s>
The	O
memory	B-Device
model	I-Device
concept	O
derives	O
from	O
the	O
setup	O
of	O
the	O
segment	B-General_Concept
registers	I-General_Concept
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
tiny	O
model	O
CS	O
=D	O
S	O
=SS	O
,	O
that	O
is	O
the	O
program	O
's	O
code	O
,	O
data	O
,	O
and	O
stack	B-General_Concept
are	O
all	O
contained	O
within	O
a	O
single	O
64KB	O
segment	O
.	O
</s>
<s>
In	O
the	O
small	O
memory	B-Device
model	I-Device
DS	O
=SS	O
,	O
so	O
both	O
data	O
and	O
stack	B-General_Concept
reside	O
in	O
the	O
same	O
segment	O
;	O
CS	O
points	O
to	O
a	O
different	O
code	B-General_Concept
segment	I-General_Concept
of	O
up	O
to	O
64KB	O
.	O
</s>
<s>
The	O
80286	B-General_Concept
's	O
protected	B-Application
mode	I-Application
extends	O
the	O
processor	O
's	O
address	B-General_Concept
space	I-General_Concept
to	O
224	O
bytes	B-Application
(	O
16	O
megabytes	O
)	O
,	O
but	O
not	O
by	O
adjusting	O
the	O
shift	O
value	O
.	O
</s>
<s>
Instead	O
,	O
the	O
16-bit	O
segment	B-General_Concept
registers	I-General_Concept
now	O
contain	O
an	O
index	O
into	O
a	O
table	O
of	O
segment	B-Operating_System
descriptors	I-Operating_System
containing	O
24-bit	O
base	O
addresses	O
to	O
which	O
the	O
offset	O
is	O
added	O
.	O
</s>
<s>
To	O
support	O
old	O
software	O
,	O
the	O
processor	O
starts	O
up	O
in	O
"	O
real	B-Application
mode	I-Application
"	O
,	O
a	O
mode	O
in	O
which	O
it	O
uses	O
the	O
segmented	B-General_Concept
addressing	I-General_Concept
model	O
of	O
the	O
8086	B-General_Concept
.	O
</s>
<s>
There	O
is	O
a	O
small	O
difference	O
though	O
:	O
the	O
resulting	O
physical	B-General_Concept
address	I-General_Concept
is	O
no	O
longer	O
truncated	O
to	O
20bits	O
,	O
so	O
real	B-Application
mode	I-Application
pointers	O
(	O
but	O
not	O
8086	B-General_Concept
pointers	O
)	O
can	O
now	O
refer	O
to	O
addresses	O
between	O
10000016	O
and	O
10FFEF16	O
.	O
</s>
<s>
This	O
roughly	O
64-kilobyte	O
region	O
of	O
memory	O
was	O
known	O
as	O
the	O
High	B-Device
Memory	I-Device
Area	I-Device
(	O
HMA	O
)	O
,	O
and	O
later	O
versions	O
of	O
DOS	B-Device
could	O
use	O
it	O
to	O
increase	O
the	O
available	O
"	O
conventional	O
"	O
memory	O
(	O
i.e.	O
</s>
<s>
With	O
the	O
addition	O
of	O
the	O
HMA	O
,	O
the	O
total	O
address	B-General_Concept
space	I-General_Concept
is	O
approximately	O
1.06MB	O
.	O
</s>
<s>
Though	O
the	O
80286	B-General_Concept
does	O
not	O
truncate	O
real-mode	B-Application
addresses	O
to	O
20bits	O
,	O
a	O
system	O
containing	O
an	O
80286	B-General_Concept
can	O
do	O
so	O
with	O
hardware	O
external	O
to	O
the	O
processor	O
,	O
by	O
gating	O
off	O
the	O
21st	O
address	O
line	O
,	O
the	O
A20	B-Device
line	I-Device
.	O
</s>
<s>
The	O
IBMPCAT	O
provided	O
the	O
hardware	O
to	O
do	O
this	O
(	O
for	O
full	O
backward	O
compatibility	O
with	O
software	O
for	O
the	O
original	O
IBM	O
PC	O
and	O
PC/XT	B-Device
models	O
)	O
,	O
and	O
so	O
all	O
subsequent	O
"	O
AT-class	O
"	O
PC	O
clones	O
did	O
as	O
well	O
.	O
</s>
<s>
286	B-Application
protected	I-Application
mode	I-Application
was	O
seldom	O
used	O
as	O
it	O
would	O
have	O
excluded	O
the	O
large	O
body	O
of	O
users	O
with	O
8086/88	O
machines	O
.	O
</s>
<s>
Moreover	O
,	O
it	O
still	O
necessitated	O
dividing	O
memory	O
into	O
64k	O
segments	O
like	O
was	O
done	O
in	O
real	B-Application
mode	I-Application
.	O
</s>
<s>
This	O
limitation	O
can	O
be	O
worked	O
around	O
on	O
32-bit	O
CPUs	O
which	O
permit	O
the	O
use	O
of	O
memory	O
pointers	O
greater	O
than	O
64k	O
in	O
size	O
,	O
however	O
as	O
the	O
Segment	O
Limit	O
field	O
is	O
only	O
24-bit	O
long	O
,	O
the	O
maximum	O
segment	O
size	O
that	O
can	O
be	O
created	O
is	O
16MB	O
(	O
although	O
paging	B-Architecture
can	O
be	O
used	O
to	O
allocate	O
more	O
memory	O
,	O
no	O
individual	O
segment	O
may	O
exceed	O
16MB	O
)	O
.	O
</s>
<s>
This	O
method	O
was	O
commonly	O
used	O
on	O
Windows	O
3.x	O
applications	O
to	O
produce	O
a	O
flat	O
memory	O
space	O
,	O
although	O
as	O
the	B-Operating_System
OS	I-Operating_System
itself	O
was	O
still	O
16-bit	O
,	O
API	O
calls	O
could	O
not	O
be	O
made	O
with	O
32-bit	O
instructions	O
.	O
</s>
<s>
Once	O
286	B-Application
protected	I-Application
mode	I-Application
is	O
invoked	O
,	O
it	O
could	O
not	O
be	O
exited	O
except	O
by	O
performing	O
a	O
hardware	O
reset	O
.	O
</s>
<s>
Machines	O
following	O
the	O
rising	O
IBM	B-Operating_System
PC/AT	I-Operating_System
standard	O
could	O
feign	O
a	O
reset	O
to	O
the	O
CPU	O
via	O
the	O
standardised	O
keyboard	O
controller	O
,	O
but	O
this	O
was	O
significantly	O
sluggish	O
.	O
</s>
<s>
Windows	O
3.x	O
worked	O
around	O
both	O
of	O
these	O
problems	O
by	O
intentionally	O
triggering	O
a	O
triple	B-Error_Name
fault	I-Error_Name
in	O
the	O
interrupt-handling	O
mechanisms	O
of	O
the	O
CPU	O
,	O
which	O
would	O
cause	O
the	O
CPU	O
to	O
drop	O
back	O
into	O
real	B-Application
mode	I-Application
,	O
nearly	O
instantly	O
.	O
</s>
<s>
A	O
logical	O
address	O
consists	O
of	O
a	O
16-bit	O
segment	B-Device
selector	I-Device
(	O
supplying	O
13+1	O
address	O
bits	O
)	O
and	O
a	O
16-bit	O
offset	O
.	O
</s>
<s>
The	O
segment	B-Device
selector	I-Device
must	O
be	O
located	O
in	O
one	O
of	O
the	O
segment	B-General_Concept
registers	I-General_Concept
.	O
</s>
<s>
That	O
selector	O
consists	O
of	O
a	O
2-bit	O
Requested	O
Privilege	B-Device
Level	I-Device
(	O
RPL	O
)	O
,	O
a	O
1-bit	O
Table	O
Indicator	O
(	O
TI	O
)	O
,	O
and	O
a	O
13-bit	O
index	O
.	O
</s>
<s>
When	O
attempting	O
address	O
translation	O
of	O
a	O
given	O
logical	O
address	O
,	O
the	O
processor	O
reads	O
the	O
64-bit	O
segment	B-Operating_System
descriptor	I-Operating_System
structure	O
from	O
either	O
the	O
Global	B-General_Concept
Descriptor	I-General_Concept
Table	I-General_Concept
when	O
TI	O
=	O
0	O
or	O
the	O
Local	B-General_Concept
Descriptor	I-General_Concept
Table	I-General_Concept
when	O
TI	O
=	O
1	O
.	O
</s>
<s>
where	O
CPL	O
is	O
the	O
current	O
privilege	B-Device
level	I-Device
(	O
found	O
in	O
the	O
lower	O
2bits	O
of	O
the	O
CS	O
register	O
)	O
,	O
RPL	O
is	O
the	O
requested	O
privilege	B-Device
level	I-Device
from	O
the	O
segment	B-Device
selector	I-Device
,	O
and	O
DPL	O
is	O
the	O
descriptor	O
privilege	B-Device
level	I-Device
of	O
the	O
segment	O
(	O
found	O
in	O
the	O
descriptor	O
)	O
.	O
</s>
<s>
All	O
privilege	B-Device
levels	I-Device
are	O
integers	O
in	O
the	O
range	O
0	O
–	O
3	O
,	O
where	O
the	O
lowest	O
number	O
corresponds	O
to	O
the	O
highest	O
privilege	O
.	O
</s>
<s>
The	O
processor	O
then	O
takes	O
the	O
32-bit	O
or	O
16-bit	O
offset	O
and	O
compares	O
it	O
against	O
the	O
segment	O
limit	O
specified	O
in	O
the	O
segment	B-Operating_System
descriptor	I-Operating_System
.	O
</s>
<s>
Otherwise	O
,	O
the	O
processor	O
adds	O
the	O
24-bit	O
segment	O
base	O
,	O
specified	O
in	O
descriptor	O
,	O
to	O
the	O
offset	O
,	O
creating	O
a	O
linear	O
physical	B-General_Concept
address	I-General_Concept
.	O
</s>
<s>
The	O
privilege	O
check	O
is	O
done	O
only	O
when	O
the	O
segment	B-General_Concept
register	I-General_Concept
is	O
loaded	O
,	O
because	O
segment	B-Operating_System
descriptors	I-Operating_System
are	O
cached	O
in	O
hidden	O
parts	O
of	O
the	O
segment	B-General_Concept
registers	I-General_Concept
.	O
</s>
<s>
In	O
the	O
Intel	B-General_Concept
80386	I-General_Concept
and	O
later	O
,	O
protected	B-Application
mode	I-Application
retains	O
the	O
segmentation	B-General_Concept
mechanism	O
of	O
80286	B-General_Concept
protected	B-Application
mode	I-Application
,	O
but	O
a	O
paging	B-Architecture
unit	O
has	O
been	O
added	O
as	O
a	O
second	O
layer	O
of	O
address	O
translation	O
between	O
the	O
segmentation	B-General_Concept
unit	O
and	O
the	O
physical	O
bus	O
.	O
</s>
<s>
Also	O
,	O
importantly	O
,	O
address	O
offsets	O
are	O
32-bit	O
(	O
instead	O
of	O
16-bit	O
)	O
,	O
and	O
the	O
segment	O
base	O
in	O
each	O
segment	B-Operating_System
descriptor	I-Operating_System
is	O
also	O
32-bit	O
(	O
instead	O
of	O
24-bit	O
)	O
.	O
</s>
<s>
The	O
general	O
operation	O
of	O
the	O
segmentation	B-General_Concept
unit	O
is	O
otherwise	O
unchanged	O
.	O
</s>
<s>
The	O
paging	B-Architecture
unit	O
may	O
be	O
enabled	O
or	O
disabled	O
;	O
if	O
disabled	O
,	O
operation	O
is	O
the	O
same	O
as	O
on	O
the	O
80286	B-General_Concept
.	O
</s>
<s>
If	O
the	O
paging	B-Architecture
unit	O
is	O
enabled	O
,	O
addresses	O
in	O
a	O
segment	O
are	O
now	O
virtual	O
addresses	O
,	O
rather	O
than	O
physical	O
addresses	O
as	O
they	O
were	O
on	O
the	O
80286	B-General_Concept
.	O
</s>
<s>
That	O
is	O
,	O
the	O
segment	O
starting	O
address	O
,	O
the	O
offset	O
,	O
and	O
the	O
final	O
32-bit	O
address	O
the	O
segmentation	B-General_Concept
unit	O
derived	O
by	O
adding	O
the	O
two	O
are	O
all	O
virtual	O
(	O
or	O
logical	O
)	O
addresses	O
when	O
the	O
paging	B-Architecture
unit	O
is	O
enabled	O
.	O
</s>
<s>
When	O
the	O
segmentation	B-General_Concept
unit	O
generates	O
and	O
validates	O
these	O
32-bit	O
virtual	O
addresses	O
,	O
the	O
enabled	O
paging	B-Architecture
unit	O
finally	O
translates	O
these	O
virtual	O
addresses	O
into	O
physical	O
addresses	O
.	O
</s>
<s>
The	O
physical	O
addresses	O
are	O
32-bit	O
on	O
the	O
386	B-General_Concept
,	O
but	O
can	O
be	O
larger	O
on	O
newer	O
processors	O
which	O
support	O
Physical	B-General_Concept
Address	I-General_Concept
Extension	I-General_Concept
.	O
</s>
<s>
The	O
80386	B-General_Concept
also	O
introduced	O
two	O
new	O
general-purpose	O
data	B-General_Concept
segment	B-General_Concept
registers	I-General_Concept
,	O
FS	B-Device
and	I-Device
GS	I-Device
,	O
to	O
the	O
original	O
set	O
of	O
four	O
segment	B-General_Concept
registers	I-General_Concept
(	O
CS	O
,	O
DS	O
,	O
ES	O
,	O
and	O
SS	O
)	O
.	O
</s>
<s>
A	O
386	B-General_Concept
CPU	O
can	O
be	O
put	O
back	O
into	O
real	B-Application
mode	I-Application
by	O
clearing	O
a	O
bit	O
in	O
the	O
CR0	O
control	O
register	O
,	O
however	O
this	O
is	O
a	O
privileged	O
operation	O
in	O
order	O
to	O
enforce	O
security	O
and	O
robustness	O
.	O
</s>
<s>
By	O
way	O
of	O
comparison	O
,	O
a	O
286	B-General_Concept
could	O
only	O
be	O
returned	O
to	O
real	B-Application
mode	I-Application
by	O
forcing	O
a	O
processor	O
reset	O
,	O
e.g.	O
</s>
<s>
by	O
a	O
triple	B-Error_Name
fault	I-Error_Name
or	O
using	O
external	O
hardware	O
.	O
</s>
<s>
The	O
x86-64	B-Device
architecture	O
does	O
not	O
use	O
segmentation	B-General_Concept
in	O
long	O
mode	O
(	O
64-bit	O
mode	O
)	O
.	O
</s>
<s>
Four	O
of	O
the	O
segment	B-General_Concept
registers	I-General_Concept
,	O
CS	O
,	O
SS	O
,	O
DS	O
,	O
and	O
ES	O
,	O
are	O
forced	O
to	O
base	O
address	O
0	O
,	O
and	O
the	O
limit	O
to	O
264	O
.	O
</s>
<s>
The	O
segment	B-General_Concept
registers	I-General_Concept
FS	B-Device
and	I-Device
GS	I-Device
can	O
still	O
have	O
a	O
nonzero	O
base	O
address	O
.	O
</s>
<s>
Unlike	O
the	O
global	B-General_Concept
descriptor	I-General_Concept
table	I-General_Concept
mechanism	O
used	O
by	O
legacy	O
modes	O
,	O
the	O
base	O
address	O
of	O
these	O
segments	O
is	O
stored	O
in	O
a	O
model-specific	B-General_Concept
register	I-General_Concept
.	O
</s>
<s>
The	O
x86-64	B-Device
architecture	O
further	O
provides	O
the	O
special	O
SWAPGS	O
instruction	O
,	O
which	O
allows	O
swapping	O
the	O
kernel	B-Operating_System
mode	O
and	O
user	B-Operating_System
mode	I-Operating_System
base	O
addresses	O
.	O
</s>
<s>
For	O
instance	O
,	O
Microsoft	B-Application
Windows	I-Application
on	O
x86-64	B-Device
uses	O
the	O
GSsegment	O
to	O
point	O
to	O
the	O
Thread	B-Operating_System
Environment	I-Operating_System
Block	I-Operating_System
,	O
a	O
small	O
data	O
structure	O
for	O
each	O
thread	B-Operating_System
,	O
which	O
contains	O
information	O
about	O
exception	O
handling	O
,	O
thread-local	O
variables	O
,	O
and	O
other	O
per-thread	O
state	O
.	O
</s>
<s>
Similarly	O
,	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
uses	O
the	O
GSsegment	O
to	O
store	O
per-CPU	O
data	O
.	O
</s>
<s>
GS/FS	O
are	O
also	O
used	O
in	O
gcc	B-Application
's	O
thread-local	O
storage	O
and	O
canary-based	O
stack	B-General_Concept
protector	O
.	O
</s>
<s>
Logical	O
addresses	O
can	O
be	O
explicitly	O
specified	O
in	O
x86	B-Language
assembly	I-Language
language	I-Language
,	O
e.g.	O
</s>
<s>
(	O
AT&T	O
syntax	O
)	O
:	O
</s>
<s>
However	O
,	O
segment	B-General_Concept
registers	I-General_Concept
are	O
usually	O
used	O
implicitly	O
.	O
</s>
<s>
All	O
CPU	O
instructions	O
are	O
implicitly	O
fetched	O
from	O
the	O
code	B-General_Concept
segment	I-General_Concept
specified	O
by	O
the	O
segment	B-Device
selector	I-Device
held	O
in	O
the	O
CSregister	O
.	O
</s>
<s>
Most	O
memory	O
references	O
come	O
from	O
the	O
data	B-General_Concept
segment	I-General_Concept
specified	O
by	O
the	O
segment	B-Device
selector	I-Device
held	O
in	O
the	O
DSregister	O
.	O
</s>
<s>
These	O
may	O
also	O
come	O
from	O
the	O
extra	O
segment	O
specified	O
by	O
the	O
segment	B-Device
selector	I-Device
held	O
in	O
the	O
ESregister	O
,	O
if	O
a	O
segment-override	O
prefix	O
precedes	O
the	O
instruction	O
that	O
makes	O
the	O
memory	O
reference	O
.	O
</s>
<s>
Processor	O
stack	B-General_Concept
references	O
,	O
either	O
implicitly	O
(	O
e.g.	O
</s>
<s>
push	O
and	O
pop	O
instructions	O
)	O
or	O
explicitly	O
(	O
memory	O
accesses	O
using	O
the	O
(	O
E	O
)	O
SP	O
or	O
(	O
E	O
)	O
BP	O
registers	O
)	O
use	O
the	O
stack	B-General_Concept
segment	I-General_Concept
specified	O
by	O
the	O
segment	B-Device
selector	I-Device
held	O
in	O
the	O
SSregister	O
.	O
</s>
<s>
stos	O
,	O
movs	O
)	O
,	O
along	O
with	O
data	B-General_Concept
segment	I-General_Concept
,	O
also	O
use	O
the	O
extra	O
segment	O
specified	O
by	O
the	O
segment	B-Device
selector	I-Device
held	O
in	O
the	O
ESregister	O
.	O
</s>
<s>
Segmentation	B-General_Concept
cannot	O
be	O
turned	O
off	O
on	O
x86-32	B-Device
processors	O
(	O
this	O
is	O
true	O
for	O
64-bit	O
mode	O
as	O
well	O
,	O
but	O
beyond	O
the	O
scope	O
of	O
discussion	O
)	O
,	O
so	O
many	O
32-bit	O
operating	O
systems	O
simulate	O
a	O
flat	B-General_Concept
memory	I-General_Concept
model	I-General_Concept
by	O
setting	O
all	O
segments	O
 '	O
bases	O
to	O
0	O
in	O
order	O
to	O
make	O
segmentation	B-General_Concept
neutral	O
to	O
programs	O
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
sets	O
up	O
only	O
4	O
general	O
purpose	O
segments	O
:	O
</s>
<s>
Since	O
the	O
base	O
is	O
set	O
to	O
0	O
in	O
all	O
cases	O
and	O
the	O
limit	O
4GiB	O
,	O
the	O
segmentation	B-General_Concept
unit	O
does	O
not	O
affect	O
the	O
addresses	O
the	O
program	O
issues	O
before	O
they	O
arrive	O
at	B-Operating_System
the	O
paging	B-Architecture
unit	O
.	O
</s>
<s>
(	O
This	O
,	O
of	O
course	O
,	O
refers	O
to	O
80386	B-General_Concept
and	O
later	O
processors	O
,	O
as	O
the	O
earlier	O
x86	B-Operating_System
processors	O
do	O
not	O
have	O
a	O
paging	B-Architecture
unit	O
.	O
)	O
</s>
<s>
Current	O
Linux	B-Operating_System
also	O
uses	O
GS	O
to	O
point	O
to	O
thread-local	O
storage	O
.	O
</s>
<s>
In	O
protected	B-Application
mode	I-Application
,	O
code	O
may	O
always	O
modify	O
all	O
segment	B-General_Concept
registers	I-General_Concept
except	O
CS	O
(	O
the	O
code	B-General_Concept
segment	I-General_Concept
selector	O
)	O
.	O
</s>
<s>
This	O
is	O
because	O
the	O
current	O
privilege	B-Device
level	I-Device
(	O
CPL	O
)	O
of	O
the	O
processor	O
is	O
stored	O
in	O
the	O
lower	O
2bits	O
of	O
the	O
CSregister	O
.	O
</s>
<s>
The	O
only	O
ways	O
to	O
raise	O
the	O
processor	O
privilege	B-Device
level	I-Device
(	O
and	O
reload	O
CS	O
)	O
are	O
through	O
the	O
lcall	O
(	O
far	O
call	O
)	O
and	O
int	B-Device
(	O
interrupt	O
)	O
instructions	O
.	O
</s>
<s>
Similarly	O
,	O
the	O
only	O
ways	O
to	O
lower	O
the	O
privilege	B-Device
level	I-Device
(	O
and	O
reload	O
CS	O
)	O
are	O
through	O
lret	O
(	O
far	O
return	O
)	O
and	O
iret	O
(	O
interrupt	O
return	O
)	O
instructions	O
.	O
</s>
<s>
In	O
real	B-Application
mode	I-Application
,	O
code	O
may	O
also	O
modify	O
the	O
CSregister	O
by	O
making	O
a	O
far	O
jump	O
(	O
or	O
using	O
an	O
undocumented	O
POP	O
CS	O
instruction	O
on	O
the	O
8086	B-General_Concept
or	O
8088	O
)	O
)	O
.	O
</s>
<s>
Of	O
course	O
,	O
in	O
real	B-Application
mode	I-Application
,	O
there	O
are	O
no	O
privilege	B-Device
levels	I-Device
;	O
all	O
programs	O
have	O
absolute	O
unchecked	O
access	O
to	O
all	O
of	O
memory	O
and	O
all	O
CPU	O
instructions	O
.	O
</s>
<s>
For	O
more	O
information	O
about	O
segmentation	B-General_Concept
,	O
see	O
the	O
IA-32	B-Device
manuals	O
freely	O
available	O
on	O
the	O
AMD	O
or	O
Intel	O
websites	O
.	O
</s>
