569e8d183336657aa82e04f882cd9ebd80c65b0d
[pintos-anon] / src / userprog / tss.c
1 #include "userprog/tss.h"
2 #include <debug.h>
3 #include <stddef.h>
4 #include "userprog/gdt.h"
5 #include "threads/palloc.h"
6 #include "threads/vaddr.h"
7
8 /* The Task-State Segment (TSS).
9
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.
15
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.
23
24    When an interrupt is handled by an interrupt or trap gate
25    (which applies to all interrupts we handle), an x86 processor
26    works like this:
27
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.
33
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.)
45
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. */
50 struct tss
51   {
52     uint16_t back_link, :16;
53     void *esp0;                         /* Ring 0 stack virtual address. */
54     uint16_t ss0, :16;                  /* Ring 0 stack segment selector. */
55     void *esp1;
56     uint16_t ss1, :16;
57     void *esp2;
58     uint16_t ss2, :16;
59     uint32_t cr3;
60     void (*eip) (void);
61     uint32_t eflags;
62     uint32_t eax, ecx, edx, ebx;
63     uint32_t esp, ebp, esi, edi;
64     uint16_t es, :16;
65     uint16_t cs, :16;
66     uint16_t ss, :16;
67     uint16_t ds, :16;
68     uint16_t fs, :16;
69     uint16_t gs, :16;
70     uint16_t ldt, :16;
71     uint16_t trace, bitmap;
72   };
73
74 /* Kernel TSS. */
75 static struct tss *tss;
76
77 /* Initializes the kernel TSS. */
78 void
79 tss_init (void) 
80 {
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->ss0 = SEL_KDSEG;
86   tss->bitmap = 0xdfff;
87   tss_update ();
88 }
89
90 /* Returns the kernel TSS. */
91 struct tss *
92 tss_get (void) 
93 {
94   ASSERT (tss != NULL);
95   return tss;
96 }
97
98 /* Sets the ring 0 stack pointer in the TSS to point to the end
99    of the thread stack. */
100 void
101 tss_update (void) 
102 {
103   ASSERT (tss != NULL);
104   tss->esp0 = (uint8_t *) thread_current () + PGSIZE;
105 }