<s>
The	O
task	B-Device
state	I-Device
segment	I-Device
(	O
TSS	O
)	O
is	O
a	O
structure	O
on	O
x86-based	O
computers	O
which	O
holds	O
information	O
about	O
a	O
task	B-General_Concept
.	O
</s>
<s>
It	O
is	O
used	O
by	O
the	O
operating	B-Operating_System
system	I-Operating_System
kernel	I-Operating_System
for	O
task	B-General_Concept
management	O
.	O
</s>
<s>
All	O
this	O
information	O
should	O
be	O
stored	O
at	O
specific	O
locations	O
within	O
the	O
TSS	O
as	O
specified	O
in	O
the	O
IA-32	B-Device
manuals	O
.	O
</s>
<s>
The	O
TSS	O
may	O
reside	O
anywhere	O
in	O
memory	B-General_Concept
.	O
</s>
<s>
A	O
segment	O
register	O
called	O
the	O
task	B-General_Concept
register	O
(	O
TR	O
)	O
holds	O
a	O
segment	B-Device
selector	I-Device
that	O
points	O
to	O
a	O
valid	O
TSS	O
segment	O
descriptor	O
which	O
resides	O
in	O
the	O
GDT	B-General_Concept
(	O
a	O
TSS	O
descriptor	O
may	O
not	O
reside	O
in	O
the	O
LDT	B-General_Concept
)	O
.	O
</s>
<s>
Therefore	O
,	O
to	O
use	O
a	O
TSS	O
the	O
following	O
must	O
be	O
done	O
by	O
the	O
operating	B-Operating_System
system	I-Operating_System
kernel	I-Operating_System
:	O
</s>
<s>
For	O
security	O
purposes	O
,	O
the	O
TSS	O
should	O
be	O
placed	O
in	O
memory	B-General_Concept
that	O
is	O
accessible	O
only	O
to	O
the	O
kernel	B-Operating_System
.	O
</s>
<s>
The	O
TR	O
register	O
is	O
a	O
16-bit	O
register	O
which	O
holds	O
a	O
segment	B-Device
selector	I-Device
for	O
the	O
TSS	O
.	O
</s>
<s>
It	O
may	O
be	O
loaded	O
through	O
the	O
LTR	B-Device
instruction	O
.	O
</s>
<s>
LTR	B-Device
is	O
a	O
privileged	O
instruction	O
and	O
acts	O
in	O
a	O
manner	O
similar	O
to	O
other	O
segment	O
register	O
loads	O
.	O
</s>
<s>
The	O
task	B-General_Concept
register	O
has	O
two	O
parts	O
:	O
a	O
portion	O
visible	O
and	O
accessible	O
by	O
the	O
programmer	O
and	O
an	O
invisible	O
one	O
that	O
is	O
automatically	O
loaded	O
from	O
the	O
TSS	O
descriptor	O
.	O
</s>
<s>
The	O
TSS	O
may	O
contain	O
saved	O
values	O
of	O
all	O
the	O
x86	B-Operating_System
registers	O
.	O
</s>
<s>
This	O
is	O
used	O
for	O
task	B-Operating_System
switching	I-Operating_System
.	O
</s>
<s>
The	O
operating	B-General_Concept
system	I-General_Concept
may	O
load	O
the	O
TSS	O
with	O
the	O
values	O
of	O
the	O
registers	O
that	O
the	O
new	O
task	B-General_Concept
needs	O
and	O
after	O
executing	O
a	O
hardware	O
task	B-General_Concept
switch	O
(	O
such	O
as	O
with	O
an	O
IRET	O
instruction	O
)	O
the	O
x86	B-Operating_System
CPU	O
will	O
load	O
the	O
saved	O
values	O
from	O
the	O
TSS	O
into	O
the	O
appropriate	O
registers	O
.	O
</s>
<s>
Note	O
that	O
some	O
modern	O
operating	B-General_Concept
systems	I-General_Concept
such	O
as	O
Windows	B-Application
and	O
Linux	B-Operating_System
do	O
not	O
use	O
these	O
fields	O
in	O
the	O
TSS	O
as	O
they	O
implement	O
software	O
task	B-Operating_System
switching	I-Operating_System
.	O
</s>
<s>
Note	O
that	O
during	O
a	O
hardware	O
task	B-General_Concept
switch	O
,	O
certain	O
fields	O
of	O
the	O
old	O
TSS	O
are	O
updated	O
with	O
the	O
CPU	O
's	O
current	O
register	O
contents	O
before	O
the	O
values	O
from	O
the	O
new	O
TSS	O
are	O
read	O
.	O
</s>
<s>
Read/Write	O
fields	O
:	O
read	O
and	O
written	O
during	O
a	O
hardware	O
task	B-General_Concept
switch	O
.	O
</s>
<s>
All	O
segment	B-Device
registers	I-Device
(	O
CS	O
,	O
DS	O
,	O
ES	O
,	O
FS	O
,	O
GS	O
,	O
SS	O
)	O
;	O
</s>
<s>
The	O
Link	O
field	O
in	O
the	O
new	O
TSS	O
,	O
if	O
the	O
task	B-General_Concept
switch	O
was	O
due	O
to	O
a	O
CALL	O
or	O
INT	O
rather	O
than	O
a	O
JMP	O
.	O
</s>
<s>
Read	O
during	O
a	O
hardware	O
task	B-General_Concept
switch	O
.	O
</s>
<s>
The	O
Local	B-General_Concept
Descriptor	I-General_Concept
Table	I-General_Concept
register	O
(	O
LDTR	O
)	O
;	O
</s>
<s>
Read	O
during	O
a	O
hardware	O
task	B-General_Concept
switch	O
.	O
</s>
<s>
The	O
PDBR	O
field	O
is	O
in	O
fact	O
the	O
very	O
first	O
one	O
read	O
out	O
of	O
the	O
new	O
TSS	O
:	O
since	O
a	O
hardware	O
task	B-General_Concept
switch	O
can	O
also	O
switch	O
to	O
a	O
completely	O
different	O
page	O
table	O
mapping	O
,	O
all	O
the	O
other	O
fields	O
(	O
especially	O
the	O
LDTR	O
)	O
are	O
relative	O
to	O
the	O
new	O
mapping	O
.	O
</s>
<s>
The	O
TSS	O
contains	O
a	O
16-bit	O
pointer	O
to	O
I/O	O
port	O
permissions	O
bitmap	O
for	O
the	O
current	O
task	B-General_Concept
.	O
</s>
<s>
This	O
bitmap	O
,	O
usually	O
set	O
up	O
by	O
the	O
operating	B-General_Concept
system	I-General_Concept
when	O
a	O
task	B-General_Concept
is	O
started	O
,	O
specifies	O
individual	O
ports	O
to	O
which	O
the	O
program	O
should	O
have	O
access	O
.	O
</s>
<s>
The	O
I/O	O
bitmap	O
is	O
a	O
bit	B-Data_Structure
array	I-Data_Structure
of	O
port	O
access	O
permissions	O
;	O
if	O
the	O
program	O
has	O
permission	O
to	O
access	O
a	O
port	O
,	O
a	O
"	O
0	O
"	O
is	O
stored	O
at	O
the	O
corresponding	O
bit	O
index	O
,	O
and	O
if	O
the	O
program	O
does	O
not	O
have	O
permission	O
,	O
a	O
"	O
1	O
"	O
is	O
stored	O
there	O
.	O
</s>
<s>
The	O
feature	O
operates	O
as	O
follows	O
:	O
when	O
a	O
program	O
issues	O
an	O
x86	B-Operating_System
I/O	O
port	O
instruction	O
such	O
as	O
IN	O
or	O
OUT	O
(	O
see	O
x86	B-Device
instruction	I-Device
listings	I-Device
-	O
and	O
note	O
that	O
there	O
are	O
byte-	O
,	O
word	O
-	O
and	O
dword-length	O
versions	O
)	O
,	O
the	O
hardware	O
will	O
do	O
an	O
I/O	O
privilege	O
level	O
(	O
IOPL	O
)	O
check	O
to	O
see	O
if	O
the	O
program	O
has	O
access	O
to	O
all	O
I/O	O
ports	O
.	O
</s>
<s>
If	O
(	O
any	O
of	O
)	O
the	O
relevant	O
bit(s )	O
is/are	O
set	O
-	O
or	O
if	O
(	O
any	O
of	O
)	O
the	O
bit(s )	O
is/are	O
past	O
the	O
TSS’	O
segment	O
limit	O
-	O
the	O
program	O
does	O
not	O
have	O
access	O
and	O
the	O
processor	O
generates	O
a	O
general	B-Operating_System
protection	I-Operating_System
fault	I-Operating_System
.	O
</s>
<s>
This	O
feature	O
allows	O
operating	B-General_Concept
systems	I-General_Concept
to	O
grant	O
selective	O
port	O
access	O
to	O
user	O
programs	O
.	O
</s>
<s>
The	O
field	O
SS0	O
contains	O
the	O
stack	O
segment	B-Device
selector	I-Device
for	O
CPL	O
=	O
0	O
,	O
and	O
the	O
field	O
ESP0/RSP0	O
contains	O
the	O
new	O
ESP/RSP	O
value	O
for	O
CPL	O
=	O
0	O
.	O
</s>
<s>
When	O
an	O
interrupt	O
happens	O
in	O
protected	O
(	O
32-bit	O
)	O
mode	O
,	O
the	O
x86	B-Operating_System
CPU	O
will	O
look	O
in	O
the	O
TSS	O
for	O
SS0	O
and	O
ESP0	O
and	O
load	O
their	O
values	O
into	O
SS	O
and	O
ESP	O
respectively	O
.	O
</s>
<s>
This	O
allows	O
for	O
the	O
kernel	B-Operating_System
to	O
use	O
a	O
different	O
stack	O
than	O
the	O
user	O
program	O
,	O
and	O
also	O
have	O
this	O
stack	O
be	O
unique	O
for	O
each	O
user	O
program	O
.	O
</s>
<s>
A	O
new	O
feature	O
introduced	O
in	O
the	O
AMD64	B-Device
extensions	O
is	O
called	O
the	O
Interrupt	O
Stack	O
Table	O
(	O
IST	O
)	O
,	O
which	O
also	O
resides	O
in	O
the	O
TSS	O
and	O
contains	O
logical	O
(	O
segment+offset	O
)	O
stack	O
pointers	O
.	O
</s>
<s>
If	O
an	O
interrupt	B-General_Concept
descriptor	I-General_Concept
table	I-General_Concept
specifies	O
an	O
IST	O
entry	O
to	O
use	O
(	O
there	O
are	O
7	O
)	O
,	O
the	O
processor	O
will	O
load	O
the	O
new	O
stack	O
from	O
the	O
IST	O
instead	O
.	O
</s>
<s>
This	O
allows	O
known-good	O
stacks	O
to	O
be	O
used	O
in	O
case	O
of	O
serious	O
errors	O
(	O
NMI	B-General_Concept
or	O
Double	B-General_Concept
fault	I-General_Concept
for	O
example	O
)	O
.	O
</s>
<s>
Previously	O
,	O
the	O
entry	O
for	O
the	O
exception	B-General_Concept
or	O
interrupt	O
in	O
the	O
IDT	O
pointed	O
to	O
a	O
task	B-Device
gate	I-Device
,	O
causing	O
the	O
processor	O
to	O
switch	O
to	O
the	O
task	B-General_Concept
that	O
is	O
pointed	O
by	O
the	O
task	B-Device
gate	I-Device
.	O
</s>
<s>
The	O
original	O
register	O
values	O
were	O
saved	O
in	O
the	O
TSS	O
current	O
at	O
the	O
time	O
the	O
interrupt	O
or	O
exception	B-General_Concept
occurred	O
.	O
</s>
<s>
The	O
problem	O
here	O
is	O
that	O
hardware	O
task	B-Operating_System
switching	I-Operating_System
is	O
not	O
supported	O
on	O
AMD64	B-Device
.	O
</s>
<s>
This	O
is	O
only	O
used	O
for	O
hardware	O
task	B-Operating_System
switching	I-Operating_System
.	O
</s>
<s>
See	O
the	O
IA-32	B-Device
manuals	O
for	O
details	O
.	O
</s>
<s>
Although	O
a	O
TSS	O
could	O
be	O
created	O
for	O
each	O
task	B-General_Concept
running	O
on	O
the	O
computer	O
,	O
Linux	B-Operating_System
kernel	I-Operating_System
only	O
creates	O
one	O
TSS	O
for	O
each	O
CPU	O
and	O
uses	O
them	O
for	O
all	O
tasks	O
.	O
</s>
<s>
This	O
approach	O
was	O
selected	O
as	O
it	O
provides	O
easier	O
portability	O
to	O
other	O
architectures	O
(	O
for	O
example	O
,	O
the	O
AMD64	B-Device
architecture	O
does	O
not	O
support	O
hardware	O
task	B-General_Concept
switches	O
)	O
,	O
and	O
improved	O
performance	O
and	O
flexibility	O
.	O
</s>
<s>
Linux	B-Operating_System
only	O
uses	O
the	O
I/O	O
port	O
permission	O
bitmap	O
and	O
inner	O
stack	O
features	O
of	O
the	O
TSS	O
;	O
the	O
other	O
features	O
are	O
only	O
needed	O
for	O
hardware	O
task	B-General_Concept
switches	O
,	O
which	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
does	O
not	O
use	O
.	O
</s>
<s>
The	O
x86	B-Operating_System
exception	B-General_Concept
vector	O
10	O
is	O
called	O
the	O
Invalid	O
TSS	O
exception	B-General_Concept
(	O
#TS	O
)	O
.	O
</s>
<s>
If	O
the	O
task	B-General_Concept
register	O
holds	O
a	O
bad	O
TSS	O
selector	O
,	O
a	O
#TS	O
fault	O
will	O
be	O
generated	O
.	O
</s>
<s>
The	O
Invalid	O
TSS	O
exception	B-General_Concept
should	O
never	O
happen	O
during	O
normal	O
operating	B-General_Concept
system	I-General_Concept
operation	O
and	O
is	O
always	O
related	O
to	O
kernel	B-Operating_System
bugs	O
or	O
hardware	O
failure	O
.	O
</s>
<s>
For	O
more	O
details	O
on	O
TSS	O
exceptions	O
,	O
see	O
Volume	O
3a	O
,	O
Chapter	O
6	O
of	O
the	O
IA-32	B-Device
manual	O
.	O
</s>
<s>
The	O
x86-64	B-Device
architecture	O
does	O
not	O
support	O
hardware	O
task	B-General_Concept
switches	O
.	O
</s>
<s>
Also	O
,	O
the	O
task	B-General_Concept
register	O
is	O
expanded	O
in	O
these	O
modes	O
to	O
be	O
able	O
to	O
hold	O
a	O
64-bit	O
base	O
address	O
.	O
</s>
