Clean up threads.c.
[pintos-anon] / src / threads / thread.c
index 9d75d18c1fb183cf222512188f087fbfc9b86e50..54f803f1d146e651cedaf73467fb525b61a941e9 100644 (file)
 #include <stddef.h>
 #include "debug.h"
 #include "interrupt.h"
+#include "intr-stubs.h"
 #include "lib.h"
 #include "mmu.h"
 #include "palloc.h"
 #include "random.h"
+#include "switch.h"
 
 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
 
 static struct list run_queue;
+static struct thread *idle_thread;
 
-struct thread *thread_switch (struct thread *cur, struct thread *next);
+static void
+idle (void *aux UNUSED) 
+{
+  for (;;) 
+    {
+      /* Wait for an interrupt. */
+      asm ("hlt");
+
+      /* Let someone else run. */
+      intr_disable ();
+      thread_sleep ();
+      intr_enable ();
+    }
+}
 
 void
-thread_init (void
+thread_init (const char *name, void (*function) (void *aux), void *aux
 {
+  struct thread *initial_thread;
+
+  ASSERT (intr_get_level () == IF_OFF);
+
   list_init (&run_queue);
+  idle_thread = thread_create ("idle", idle, NULL);
+
+  initial_thread = thread_create (name, function, aux);
+  list_remove (&initial_thread->rq_elem);
+  initial_thread->status = THREAD_RUNNING;
+  switch_threads (NULL, initial_thread);
+
+  NOT_REACHED ();
 }
 
+struct kernel_thread_frame 
+  {
+    void *eip;                  /* Return address. */
+    void (*function) (void *);  /* Function to call. */
+    void *aux;                  /* Auxiliary data for function. */
+  };
+
 static void
-thread_root (void (*function) (void *aux), void *aux) 
+kernel_thread (void (*function) (void *aux), void *aux) 
 {
   ASSERT (function != NULL);
-  
+
+  intr_enable ();
   function (aux);
   thread_exit ();
 }
 
+static struct thread *
+new_thread (const char *name) 
+{
+  struct thread *t;
+
+  ASSERT (name != NULL);
+  
+  t = palloc_get (PAL_ZERO);
+  if (t != NULL)
+    {
+      strlcpy (t->name, name, sizeof t->name);
+      t->stack = (uint8_t *) t + PGSIZE;
+      t->status = THREAD_BLOCKED;
+    }
+  
+  return t;
+}
+
+static void *
+alloc_frame (struct thread *t, size_t size) 
+{
+  ASSERT (size % sizeof (uint32_t) == 0);
+
+  t->stack -= size;
+  return t->stack;
+}
+
 struct thread *
 thread_create (const char *name, void (*function) (void *aux), void *aux) 
 {
   struct thread *t;
+  struct kernel_thread_frame *kf;
+  struct switch_entry_frame *ef;
+  struct switch_threads_frame *sf;
 
-  ASSERT (name != NULL);
   ASSERT (function != NULL);
 
-  t = palloc_get (0);
-  if (t == NULL)
-    return NULL;
+  t = new_thread (name);
 
-  memset (t, 0, PGSIZE);
-  strlcpy (t->name, name, sizeof t->name);
+  /* Stack frame for kernel_thread(). */
+  kf = alloc_frame (t, sizeof *kf);
+  kf->eip = NULL;
+  kf->function = function;
+  kf->aux = aux;
 
-  /* Set up stack. */
-  t->stack = (uint32_t *) ((uint8_t *) t + PGSIZE);
-  *--t->stack = (uint32_t) aux;
-  *--t->stack = (uint32_t) function;
-  --t->stack;
-  *--t->stack = (uint32_t) thread_root;
-  t->stack -= 4;
+  /* Stack frame for switch_entry(). */
+  ef = alloc_frame (t, sizeof *ef);
+  ef->eip = (void (*) (void)) kernel_thread;
 
-  /* Add to run_queue. */
-  t->status = THREAD_BLOCKED;
+  /* Stack frame for thread_switch(). */
+  sf = alloc_frame (t, sizeof *sf);
+  sf->eip = switch_entry;
+
+  /* Add to run queue. */
   thread_ready (t);
 
   return t;
 }
 
-static struct thread *
-stack_to_thread (uint32_t *stack) 
-{
-  return (struct thread *) ((uint32_t) (stack - 1) & ~((uint32_t) PGSIZE - 1));
-}
-
 struct thread *
 thread_current (void) 
 {
   uint32_t *esp;
   asm ("movl %%esp, %0\n" : "=g" (esp));
-  return stack_to_thread (esp);
+  return pg_round_down (esp);
 }
 
 #ifdef USERPROG
-void
+bool
 thread_execute (const char *filename) 
 {
-  struct thread *t = thread_current ();
+  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);
-  panic ("%s: loaded", 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
 
@@ -103,14 +194,6 @@ find_next_to_run (void)
     return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
 }
 
-static void
-idle (void) 
-{
-  static int idle = 0;
-  if (idle++ == 0)
-    printk ("idle\n");
-}
-
 void
 thread_destroy (struct thread *t) 
 {
@@ -120,7 +203,22 @@ thread_destroy (struct thread *t)
   palloc_free (t);
 }
 
+void schedule_tail (struct thread *prev);
+
 void
+schedule_tail (struct thread *prev) 
+{
+  ASSERT (intr_get_level () == IF_OFF);
+
+#ifdef USERPROG
+  addrspace_activate (&thread_current ()->addrspace);
+#endif
+
+  if (prev != NULL && prev->status == THREAD_DYING) 
+    thread_destroy (prev);
+}
+
+static void
 thread_schedule (void) 
 {
   struct thread *cur, *next, *prev;
@@ -130,41 +228,34 @@ thread_schedule (void)
   cur = thread_current ();
   ASSERT (cur->status != THREAD_RUNNING);
 
-  while ((next = find_next_to_run ()) == NULL)
-    idle ();
+  next = find_next_to_run ();
+  if (next == NULL)
+    next = idle_thread;
 
   next->status = THREAD_RUNNING;
-  prev = thread_switch (cur, next);
+  if (cur != next)
+    {
+      prev = switch_threads (cur, next);
 
-  /* Prevent GCC from reordering anything around the thread
-     switch. */
-  asm volatile ("" : : : "memory");
+      /* 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
 thread_yield (void) 
 {
+  enum if_level old_level;
+  
   ASSERT (!intr_context ());
 
-  intr_disable ();
+  old_level = intr_disable ();
   thread_ready (thread_current ());
   thread_schedule ();
-}
-
-void
-thread_start (struct thread *t) 
-{
-  ASSERT (intr_get_level () == IF_OFF);
-
-  if (t->status == THREAD_READY) 
-    list_remove (&t->rq_elem);
-  t->status = THREAD_RUNNING;
-  thread_switch (NULL, t);
+  intr_set_level (old_level);
 }
 
 void
@@ -175,6 +266,7 @@ thread_exit (void)
   intr_disable ();
   thread_current ()->status = THREAD_DYING;
   thread_schedule ();
+  NOT_REACHED ();
 }
 
 void
@@ -186,37 +278,3 @@ thread_sleep (void)
   thread_current ()->status = THREAD_BLOCKED;
   thread_schedule ();
 }
-
-static void
-tfunc (void *aux UNUSED) 
-{
-  for (;;) 
-    {
-      size_t count, i;
-      if (random_ulong () % 5 == 0)
-        {
-          printk ("%s exiting\n", thread_current ()->name);
-          break;
-        }
-      count = random_ulong () % 25 * 10000;
-      printk ("%s waiting %zu: ", thread_current ()->name, count);
-      for (i = 0; i < count; i++);
-      printk ("%s\n", thread_current ()->name);
-    }
-}
-
-void
-thread_self_test (void)
-{
-  struct thread *t;
-  int i;
-    
-  for (i = 0; i < 4; i++) 
-    {
-      char name[2];
-      name[0] = 'a' + i;
-      name[1] = 0;
-      t = thread_create (name, tfunc, NULL); 
-    }
-  thread_start (t); 
-}