1 #include "userprog/tss.h"
4 #include "userprog/gdt.h"
5 #include "threads/palloc.h"
6 #include "threads/vaddr.h"
8 /* The Task-State Segment (TSS).
10 Instances of the TSS, an x86-specific structure, are used to
11 define "tasks", a form of support for multitasking built right
12 into the processor. However, for various reasons including
13 portability, speed, and flexibility, most x86 OSes almost
14 completely ignore the TSS. We are no exception.
16 Unfortunately, there is one thing that can only be done using
17 a TSS: stack switching for interrupts that occur in user mode.
18 When an interrupt occurs in user mode (ring 3), the processor
19 consults the ss0 and esp0 members of the current TSS to
20 determine the stack to use for handling the interrupt. Thus,
21 we must create a TSS and initialize at least these fields, and
22 this is precisely what this file does.
24 When an interrupt is handled by an interrupt or trap gate
25 (which applies to all interrupts we handle), an x86 processor
28 - If the code interrupted by the interrupt is in the same
29 ring as the interrupt handler, then no stack switch takes
30 place. This is the case for interrupts that happen when
31 we're running in the kernel. The contents of the TSS are
32 irrelevant for this case.
34 - If the interrupted code is in a different ring from the
35 handler, then the processor switches to the stack
36 specified in the TSS for the new ring. This is the case
37 for interrupts that happen when we're in user space. It's
38 important that we switch to a stack that's not already in
39 use, to avoid corruption. Because we're running in user
40 space, we know that the current process's kernel stack is
41 not in use, so we can always use that. Thus, when the
42 scheduler switches threads, it also changes the TSS's
43 stack pointer to point to the new thread's kernel stack.
44 (The call is in schedule_tail() in thread.c.)
46 See [IA32-v3a] 6.2.1 "Task-State Segment (TSS)" for a
47 description of the TSS. See [IA32-v3a] 5.12.1 "Exception- or
48 Interrupt-Handler Procedures" for a description of when and
49 how stack switching occurs during an interrupt. */
52 uint16_t back_link, :16;
53 void *esp0; /* Ring 0 stack virtual address. */
54 uint16_t ss0, :16; /* Ring 0 stack segment selector. */
62 uint32_t eax, ecx, edx, ebx;
63 uint32_t esp, ebp, esi, edi;
71 uint16_t trace, bitmap;
75 static struct tss *tss;
77 /* Initializes the kernel TSS. */
81 /* Our TSS is never used in a call gate or task gate, so only a
82 few fields of it are ever referenced, and those are the only
83 ones we initialize. */
84 tss = palloc_get_page (PAL_ASSERT | PAL_ZERO);
85 tss->esp0 = ptov(0x20000);
90 /* Returns the kernel TSS. */
98 /* Sets the ring 0 stack pointer in the TSS to ESP0. */
100 tss_set_esp0 (uint8_t *esp0)
102 ASSERT (tss != NULL);