1 #include "userprog/tss.h"
4 #include "userprog/gdt.h"
5 #include "threads/mmu.h"
6 #include "threads/palloc.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-v3] 6.2.1 for a description of the TSS and 5.12.1
47 for a description of when and how stack switching occurs
48 during an interrupt. */
51 uint16_t back_link, :16;
52 void *esp0; /* Ring 0 stack virtual address. */
53 uint16_t ss0, :16; /* Ring 0 stack segment selector. */
61 uint32_t eax, ecx, edx, ebx;
62 uint32_t esp, ebp, esi, edi;
70 uint16_t trace, bitmap;
74 static struct tss *tss;
76 /* Initializes the kernel TSS. */
80 /* Our TSS is never used in a call gate or task gate, so only a
81 few fields of it are ever referenced, and those are the only
82 ones we initialize. */
83 tss = palloc_get_page (PAL_ASSERT | PAL_ZERO);
84 tss->esp0 = ptov(0x20000);
89 /* Returns the kernel TSS. */
97 /* Sets the ring 0 stack pointer in the TSS to ESP0. */
99 tss_set_esp0 (uint8_t *esp0)
101 ASSERT (tss != NULL);