Make userspace actually work.
[pintos-anon] / src / threads / thread.c
index 04b446f9f475d0225b45b4f6e6d797a12defbd87..dd98be0d2bbf846b851c86346c63c884a9191ccd 100644 (file)
@@ -2,6 +2,7 @@
 #include <stddef.h>
 #include "debug.h"
 #include "interrupt.h"
+#include "intr-stubs.h"
 #include "lib.h"
 #include "mmu.h"
 #include "palloc.h"
@@ -66,8 +67,8 @@ thread_create (const char *name, void (*function) (void *aux), void *aux)
 {
   struct thread *t;
   struct thread_root_frame *rf;
-  struct switch_thunk_frame *tf;
-  struct switch_frame *sf;
+  struct switch_entry_frame *ef;
+  struct switch_threads_frame *sf;
 
   ASSERT (function != NULL);
 
@@ -79,13 +80,13 @@ thread_create (const char *name, void (*function) (void *aux), void *aux)
   rf->function = function;
   rf->aux = aux;
 
-  /* Stack frame for switch_thunk(). */
-  tf = alloc_frame (t, sizeof *tf);
-  tf->eip = (void (*) (void)) thread_root;
+  /* Stack frame for switch_entry(). */
+  ef = alloc_frame (t, sizeof *ef);
+  ef->eip = (void (*) (void)) thread_root;
 
   /* Stack frame for thread_switch(). */
   sf = alloc_frame (t, sizeof *sf);
-  sf->eip = (void (*) (void)) switch_thunk;
+  sf->eip = switch_entry;
 
   /* Add to run queue. */
   thread_ready (t);
@@ -105,13 +106,42 @@ thread_current (void)
 bool
 thread_execute (const char *filename) 
 {
-  struct thread *t = new_thread (filename);
+  struct thread *t;
+  struct intr_frame *if_;
+  struct switch_entry_frame *ef;
+  struct switch_threads_frame *sf;
+  void (*start) (void);
+
+  ASSERT (filename != NULL);
+
+  t = new_thread (filename);
   if (t == NULL)
     return false;
   
-  if (!addrspace_load (&t->addrspace, filename)) 
+  if (!addrspace_load (&t->addrspace, filename, &start)) 
     panic ("%s: program load failed", filename);
-  printk ("%s: loaded\n", filename);
+
+  /* Interrupt frame. */
+  if_ = alloc_frame (t, sizeof *if_);
+  if_->es = SEL_UDSEG;
+  if_->ds = SEL_UDSEG;
+  if_->eip = start;
+  if_->cs = SEL_UCSEG;
+  if_->eflags = FLAG_IF | 2;
+  if_->esp = PHYS_BASE;
+  if_->ss = SEL_UDSEG;
+
+  /* Stack frame for switch_entry(). */
+  ef = alloc_frame (t, sizeof *ef);
+  ef->eip = intr_exit;
+
+  /* Stack frame for thread_switch(). */
+  sf = alloc_frame (t, sizeof *sf);
+  sf->eip = switch_entry;
+
+  /* Add to run queue. */
+  thread_ready (t);
+
   return true;
 }
 #endif
@@ -152,6 +182,23 @@ thread_destroy (struct thread *t)
   palloc_free (t);
 }
 
+void schedule_tail (struct thread *prev);
+
+void
+schedule_tail (struct thread *prev) 
+{
+  struct thread *cur = thread_current ();
+
+#ifdef USERPROG
+  addrspace_activate (&cur->addrspace);
+#endif
+
+  if (prev != NULL && prev->status == THREAD_DYING) 
+    thread_destroy (prev);
+
+  intr_enable ();
+}
+
 void
 thread_schedule (void) 
 {
@@ -166,20 +213,16 @@ thread_schedule (void)
     idle ();
 
   next->status = THREAD_RUNNING;
-  prev = switch_threads (cur, next);
-
-  /* Prevent GCC from reordering anything around the thread
-     switch. */
-  asm volatile ("" : : : "memory");
+  if (cur != next)
+    {
+      prev = switch_threads (cur, next);
 
-#ifdef USERPROG
-  addrspace_activate (&cur->addrspace);
-#endif
+      /* Prevent GCC from reordering anything around the thread
+         switch. */
+      asm volatile ("" : : : "memory");
 
-  if (prev != NULL && prev->status == THREAD_DYING) 
-    thread_destroy (prev);
-
-  intr_enable ();
+      schedule_tail (prev); 
+    }
 }
 
 void
@@ -211,6 +254,7 @@ thread_exit (void)
   intr_disable ();
   thread_current ()->status = THREAD_DYING;
   thread_schedule ();
+  NOT_REACHED ();
 }
 
 void