Userspace almost (?) working.
[pintos-anon] / src / threads / thread.c
1 #include "thread.h"
2 #include <stddef.h>
3 #include "debug.h"
4 #include "interrupt.h"
5 #include "intr-stubs.h"
6 #include "lib.h"
7 #include "mmu.h"
8 #include "palloc.h"
9 #include "random.h"
10 #include "switch.h"
11
12 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
13
14 static struct list run_queue;
15
16 void
17 thread_init (void) 
18 {
19   list_init (&run_queue);
20 }
21
22 struct thread_root_frame 
23   {
24     void *eip;                  /* Return address. */
25     void (*function) (void *);  /* Function to call. */
26     void *aux;                  /* Auxiliary data for function. */
27   };
28
29 static void
30 thread_root (void (*function) (void *aux), void *aux) 
31 {
32   ASSERT (function != NULL);
33   
34   function (aux);
35   thread_exit ();
36 }
37
38 static struct thread *
39 new_thread (const char *name) 
40 {
41   struct thread *t;
42
43   ASSERT (name != NULL);
44   
45   t = palloc_get (PAL_ZERO);
46   if (t != NULL)
47     {
48       strlcpy (t->name, name, sizeof t->name);
49       t->stack = (uint8_t *) t + PGSIZE;
50       t->status = THREAD_BLOCKED;
51     }
52   
53   return t;
54 }
55
56 static void *
57 alloc_frame (struct thread *t, size_t size) 
58 {
59   ASSERT (size % sizeof (uint32_t) == 0);
60
61   t->stack -= size;
62   return t->stack;
63 }
64
65 struct thread *
66 thread_create (const char *name, void (*function) (void *aux), void *aux) 
67 {
68   struct thread *t;
69   struct thread_root_frame *rf;
70   struct switch_thunk_frame *tf;
71   struct switch_frame *sf;
72
73   ASSERT (function != NULL);
74
75   t = new_thread (name);
76
77   /* Stack frame for thread_root(). */
78   rf = alloc_frame (t, sizeof *rf);
79   rf->eip = NULL;
80   rf->function = function;
81   rf->aux = aux;
82
83   /* Stack frame for switch_thunk(). */
84   tf = alloc_frame (t, sizeof *tf);
85   tf->eip = (void (*) (void)) thread_root;
86
87   /* Stack frame for thread_switch(). */
88   sf = alloc_frame (t, sizeof *sf);
89   sf->eip = (void (*) (void)) switch_thunk;
90
91   /* Add to run queue. */
92   thread_ready (t);
93
94   return t;
95 }
96
97 struct thread *
98 thread_current (void) 
99 {
100   uint32_t *esp;
101   asm ("movl %%esp, %0\n" : "=g" (esp));
102   return pg_round_down (esp);
103 }
104
105 #ifdef USERPROG
106 bool
107 thread_execute (const char *filename) 
108 {
109   struct thread *t;
110   struct intr_frame *if_;
111   struct switch_thunk_frame *tf;
112   struct switch_frame *sf;
113   void (*start) (void);
114
115   ASSERT (filename != NULL);
116
117   t = new_thread (filename);
118   if (t == NULL)
119     return false;
120   
121   if (!addrspace_load (&t->addrspace, filename, &start)) 
122     panic ("%s: program load failed", filename);
123
124   /* Interrupt frame. */
125   if_ = alloc_frame (t, sizeof *if_);
126   if_->es = SEL_UDSEG;
127   if_->ds = SEL_UDSEG;
128   if_->eip = start;
129   if_->cs = SEL_UCSEG;
130   if_->eflags = FLAG_IF | 2;
131   if_->esp = PHYS_BASE;
132   if_->ss = SEL_UDSEG;
133   
134   /* Stack frame for switch_thunk(). */
135   tf = alloc_frame (t, sizeof *tf);
136   tf->eip = (void (*) (void)) intr_exit;
137
138   /* Stack frame for thread_switch(). */
139   sf = alloc_frame (t, sizeof *sf);
140   sf->eip = (void (*) (void)) switch_thunk;
141
142   /* Add to run queue. */
143   thread_ready (t);
144
145   return true;
146 }
147 #endif
148
149 void
150 thread_ready (struct thread *t) 
151 {
152   if (t->status != THREAD_READY) 
153     {
154       list_push_back (&run_queue, &t->rq_elem);
155       t->status = THREAD_READY;
156     }
157 }
158
159 static struct thread *
160 find_next_to_run (void) 
161 {
162   if (list_empty (&run_queue))
163     return NULL;
164   else
165     return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
166 }
167
168 static void
169 idle (void) 
170 {
171   static int idle = 0;
172   if (idle++ == 0)
173     printk ("idle\n");
174 }
175
176 void
177 thread_destroy (struct thread *t) 
178 {
179   ASSERT (t->status == THREAD_DYING);
180   ASSERT (t != thread_current ());
181
182   palloc_free (t);
183 }
184
185 void
186 thread_schedule (void) 
187 {
188   struct thread *cur, *next, *prev;
189
190   ASSERT (intr_get_level () == IF_OFF);
191
192   cur = thread_current ();
193   ASSERT (cur->status != THREAD_RUNNING);
194
195   while ((next = find_next_to_run ()) == NULL)
196     idle ();
197
198   next->status = THREAD_RUNNING;
199   prev = switch_threads (cur, next);
200
201   /* Prevent GCC from reordering anything around the thread
202      switch. */
203   asm volatile ("" : : : "memory");
204
205 #ifdef USERPROG
206   addrspace_activate (&cur->addrspace);
207 #endif
208
209   if (prev != NULL && prev->status == THREAD_DYING) 
210     thread_destroy (prev);
211
212   intr_enable ();
213 }
214
215 void
216 thread_yield (void) 
217 {
218   ASSERT (!intr_context ());
219
220   intr_disable ();
221   thread_ready (thread_current ());
222   thread_schedule ();
223 }
224
225 void
226 thread_start (struct thread *t) 
227 {
228   ASSERT (intr_get_level () == IF_OFF);
229
230   if (t->status == THREAD_READY) 
231     list_remove (&t->rq_elem);
232   t->status = THREAD_RUNNING;
233   switch_threads (NULL, t);
234 }
235
236 void
237 thread_exit (void) 
238 {
239   ASSERT (!intr_context ());
240
241   intr_disable ();
242   thread_current ()->status = THREAD_DYING;
243   thread_schedule ();
244 }
245
246 void
247 thread_sleep (void) 
248 {
249   ASSERT (!intr_context ());
250   ASSERT (intr_get_level () == IF_OFF);
251
252   thread_current ()->status = THREAD_BLOCKED;
253   thread_schedule ();
254 }
255
256 static void
257 tfunc (void *aux UNUSED) 
258 {
259   for (;;) 
260     {
261       size_t count, i;
262       if (random_ulong () % 5 == 0)
263         {
264           printk ("%s exiting\n", thread_current ()->name);
265           break;
266         }
267       count = random_ulong () % 25 * 10000;
268       printk ("%s waiting %zu: ", thread_current ()->name, count);
269       for (i = 0; i < count; i++);
270       printk ("%s\n", thread_current ()->name);
271     }
272 }
273
274 void
275 thread_self_test (void)
276 {
277   struct thread *t;
278   int i;
279     
280   for (i = 0; i < 4; i++) 
281     {
282       char name[2];
283       name[0] = 'a' + i;
284       name[1] = 0;
285       t = thread_create (name, tfunc, NULL); 
286     }
287   thread_start (t); 
288 }