Clean up handling of stack frames.
[pintos-anon] / src / threads / thread.c
index 6793bb3a27e33f6f4b858134bd785eddc887db01..52f566ac0309802d602da4f6ceb8362fa0a62b74 100644 (file)
@@ -5,21 +5,29 @@
 #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;
 
-struct thread *thread_switch (struct thread *cur, struct thread *next);
-
 void
 thread_init (void) 
 {
   list_init (&run_queue);
 }
 
+struct thread_root_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) 
+thread_root (struct thread *cur UNUSED, struct thread *next UNUSED,
+             void (*function) (void *aux), void *aux) 
 {
   ASSERT (function != NULL);
   
@@ -27,50 +35,83 @@ thread_root (void (*function) (void *aux), void *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 thread_root_frame *rf;
+  struct switch_frame *sf;
 
-  ASSERT (name != NULL);
   ASSERT (function != NULL);
 
-  t = palloc_get (0);
-  if (t == NULL)
-    return NULL;
+  t = new_thread (name);
 
-  memset (t, 0, NBPG);
-  strlcpy (t->name, name, sizeof t->name);
+  /* Stack frame for thread_root(). */
+  rf = alloc_frame (t, sizeof *rf);
+  rf->eip = NULL;
+  rf->function = function;
+  rf->aux = aux;
 
-  /* Set up stack. */
-  t->stack = (uint32_t *) ((uint8_t *) t + NBPG);
-  *--t->stack = (uint32_t) aux;
-  *--t->stack = (uint32_t) function;
-  --t->stack;
-  *--t->stack = (uint32_t) thread_root;
-  t->stack -= 4;
+  /* Stack frame for thread_switch(). */
+  sf = alloc_frame (t, sizeof *sf);
+  sf->eip = (void (*) (void)) thread_root;
 
-  /* Add to run_queue. */
-  t->status = THREAD_BLOCKED;
+  /* 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) NBPG - 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
+bool
+thread_execute (const char *filename) 
+{
+  struct thread *t = new_thread (filename);
+  if (t == NULL)
+    return false;
+  
+  if (!addrspace_load (&t->addrspace, filename)) 
+    panic ("%s: program load failed", filename);
+  printk ("%s: loaded\n", filename);
+  return true;
+}
+#endif
+
 void
 thread_ready (struct thread *t) 
 {
@@ -112,6 +153,8 @@ thread_schedule (void)
 {
   struct thread *cur, *next, *prev;
 
+  ASSERT (intr_get_level () == IF_OFF);
+
   cur = thread_current ();
   ASSERT (cur->status != THREAD_RUNNING);
 
@@ -120,14 +163,27 @@ thread_schedule (void)
 
   next->status = THREAD_RUNNING;
   prev = thread_switch (cur, next);
+
+  /* Prevent GCC from reordering anything around the thread
+     switch. */
+  asm volatile ("" : : : "memory");
+
+#ifdef USERPROG
+  addrspace_activate (&cur->addrspace);
+#endif
+
   if (prev != NULL && prev->status == THREAD_DYING) 
     thread_destroy (prev);
+
+  intr_enable ();
 }
 
 void
 thread_yield (void) 
 {
   ASSERT (!intr_context ());
+
+  intr_disable ();
   thread_ready (thread_current ());
   thread_schedule ();
 }
@@ -135,6 +191,8 @@ thread_yield (void)
 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;
@@ -144,16 +202,53 @@ thread_start (struct thread *t)
 void
 thread_exit (void) 
 {
-  struct thread *t = thread_current ();
-  t->status = THREAD_DYING;
+  ASSERT (!intr_context ());
+
+  intr_disable ();
+  thread_current ()->status = THREAD_DYING;
   thread_schedule ();
 }
 
 void
 thread_sleep (void) 
 {
+  ASSERT (!intr_context ());
   ASSERT (intr_get_level () == IF_OFF);
 
   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); 
+}