-#### 0(%esp) - old registers
-#### 16(%esp) - return address
-#### 20(%esp) - current thread
-#### 24(%esp) - new thread
+#include "switch.h"
.globl thread_switch
thread_switch:
mov thread_stack_ofs, %edx
# Save current stack pointer to old thread's stack, if any.
- movl 20(%esp), %eax
+ movl SWITCH_CUR(%esp), %eax
test %eax, %eax
jz 1f
movl %esp, (%eax,%edx,1)
1:
# Restore stack pointer from new thread's stack.
- movl 24(%esp), %ecx
+ movl SWITCH_NEXT(%esp), %ecx
movl (%ecx,%edx,1), %esp
# Restore caller's register state.
--- /dev/null
+#ifndef HEADER_SWITCH_H
+#define HEADER_SWITCH_H 1
+
+#ifndef __ASSEMBLER__
+/* thread_switch()'s stack frame. */
+struct switch_frame
+ {
+ uint32_t ebx; /* 0: Saved %ebx. */
+ uint32_t ebp; /* 4: Saved %ebp. */
+ uint32_t esi; /* 8: Saved %esi. */
+ uint32_t edi; /* 12: Saved %edi. */
+ void (*eip) (void); /* 16: Return address. */
+ struct thread *cur; /* 20: thread_switch()'s CUR argument. */
+ struct thread *next; /* 24: thread_switch()'s NEXT argument. */
+ };
+
+/* Switches from CUR, which must be the running thread, to NEXT,
+ which must also be running thread_switch(), returning CUR in
+ NEXT's context. */
+struct thread *thread_switch (struct thread *cur, struct thread *next);
+#endif
+
+/* Offsets used by switch.S. */
+#define SWITCH_CUR 20
+#define SWITCH_NEXT 24
+
+#endif /* switch.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);
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, PGSIZE);
- 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 + 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 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) 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 = new_thread (filename);
+ if (t == NULL)
+ return false;
if (!addrspace_load (&t->addrspace, filename))
panic ("%s: program load failed", filename);
- panic ("%s: loaded", filename);
+ printk ("%s: loaded\n", filename);
+ return true;
}
#endif
switch. */
asm volatile ("" : : : "memory");
+#ifdef USERPROG
+ addrspace_activate (&cur->addrspace);
+#endif
+
if (prev != NULL && prev->status == THREAD_DYING)
thread_destroy (prev);