libsel4utils: Refactor sel4utils process and thread code to use arch specific directories instead of ifdefs. As a side effect, make sure the bloody stack is alligned on arm
diff --git a/libsel4utils/Makefile b/libsel4utils/Makefile
index 0be3595..d415e04 100644
--- a/libsel4utils/Makefile
+++ b/libsel4utils/Makefile
@@ -15,6 +15,7 @@
 CFILES := $(patsubst $(SOURCE_DIR)/%,%,$(wildcard $(SOURCE_DIR)/src/*.c))
 CFILES += $(patsubst $(SOURCE_DIR)/%,%,$(wildcard $(SOURCE_DIR)/src/vspace/*.c))
 CFILES += $(patsubst $(SOURCE_DIR)/%,%,$(wildcard $(SOURCE_DIR)/src/irq_server/*.c))
+CFILES += $(patsubst $(SOURCE_DIR)/%,%,$(wildcard $(SOURCE_DIR)/src/arch/${ARCH}/*.c))
 
 # Header files/directories this library provides
 HDRFILES := $(wildcard $(SOURCE_DIR)/include/*)
diff --git a/libsel4utils/src/arch/arm/arch.c b/libsel4utils/src/arch/arm/arch.c
new file mode 100644
index 0000000..69b8325
--- /dev/null
+++ b/libsel4utils/src/arch/arm/arch.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2014, NICTA
+ *
+ * This software may be distributed and modified according to the terms of
+ * the BSD 2-Clause license. Note that NO WARRANTY is provided.
+ * See "LICENSE_BSD2.txt" for details.
+ *
+ * @TAG(NICTA_BSD)
+ */
+#include <autoconf.h>
+#include <sel4/types.h>
+#include <sel4utils/thread.h>
+#include <stdbool.h>
+
+#include "../../helpers.h"
+
+int 
+sel4utils_arch_init_context(void *entry_point, void *arg0, void *arg1, void *arg2,
+                            bool local_stack, void *stack_top, seL4_UserContext *context,
+                            vka_t *vka, vspace_t *local_vspace, vspace_t *remote_vspace)
+{
+    
+    context->pc = (seL4_Word) entry_point;
+    context->sp = (seL4_Word) stack_top;
+    context->r0 = (seL4_Word) arg0;
+    context->r1 = (seL4_Word) arg1;
+    context->r2 = (seL4_Word) arg2;
+
+    if ((uintptr_t) stack_top % (sizeof(seL4_Word) * 2) != 0) {
+        ZF_LOGE("Stack %p not aligned on double word boundary", stack_top);
+        return -1;
+    }
+
+    return 0;
+}
+
+
diff --git a/libsel4utils/src/arch/x86/arch.c b/libsel4utils/src/arch/x86/arch.c
new file mode 100644
index 0000000..bbd8d1d
--- /dev/null
+++ b/libsel4utils/src/arch/x86/arch.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2014, NICTA
+ *
+ * This software may be distributed and modified according to the terms of
+ * the BSD 2-Clause license. Note that NO WARRANTY is provided.
+ * See "LICENSE_BSD2.txt" for details.
+ *
+ * @TAG(NICTA_BSD)
+ */
+#include <autoconf.h>
+#include <sel4/types.h>
+#include <sel4utils/thread.h>
+#include <stdbool.h>
+
+#include "../../helpers.h"
+
+int 
+sel4utils_arch_init_context(void *entry_point, void *arg0, void *arg1, void *arg2,
+                            bool local_stack, void *stack_top, seL4_UserContext *context, 
+                            vka_t *vka, vspace_t *local_vspace, vspace_t *remote_vspace)
+{
+    
+    if (local_stack) {
+        seL4_Word *stack_ptr = (seL4_Word *) stack_top;
+        stack_ptr[-5] = (seL4_Word) arg0;
+        stack_ptr[-4] = (seL4_Word) arg1;
+        stack_ptr[-3] = (seL4_Word) arg2;
+        stack_top = (void *) ((uintptr_t) stack_top - 24u);
+    } else if (local_vspace && remote_vspace && vka) {
+        seL4_Word stack_args[6] = {0, (seL4_Word) arg0, (seL4_Word) arg1, (seL4_Word) arg2, 0, 0};
+        int error = sel4utils_stack_write(local_vspace, remote_vspace, vka, stack_args, sizeof(stack_args), 
+                                          (uintptr_t *) &stack_top);
+        if (error) {
+            return -1;
+        }
+    }
+    
+    context->esp = (seL4_Word) stack_top;
+    context->edx = 0;
+    context->gs  = IPCBUF_GDT_SELECTOR;
+    context->eip = (seL4_Word) entry_point;
+
+    return 0;
+}
+
+
diff --git a/libsel4utils/src/arch/x86_64/arch.c b/libsel4utils/src/arch/x86_64/arch.c
new file mode 100644
index 0000000..50c9c9f
--- /dev/null
+++ b/libsel4utils/src/arch/x86_64/arch.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2014, NICTA
+ *
+ * This software may be distributed and modified according to the terms of
+ * the BSD 2-Clause license. Note that NO WARRANTY is provided.
+ * See "LICENSE_BSD2.txt" for details.
+ *
+ * @TAG(NICTA_BSD)
+ */
+#include <autoconf.h>
+#include <sel4/types.h>
+#include <sel4utils/thread.h>
+#include <stdbool.h>
+
+#include "../../helpers.h"
+
+int 
+sel4utils_arch_init_context(void *entry_point, void *arg0, void *arg1, void *arg2,
+                            bool local_stack, void *stack_top, seL4_UserContext *context,
+                            vka_t *vka, vspace_t *local_vspace, vspace_t *remote_vspace);
+{
+    
+    context->rdi = (seL4_Word) arg0;
+    context->rsi = (seL4_Word) arg1;
+    context->rdx = (seL4_Word) arg2;
+    context->esp = (seL4_Word) stack_top;
+    context->gs = IPCBUF_GDT_SELECTOR;
+    context->eip = (seL4_Word) entry_point;
+
+    return 0;
+}
+
+
diff --git a/libsel4utils/src/helpers.h b/libsel4utils/src/helpers.h
index aa09825..0e11a4c 100644
--- a/libsel4utils/src/helpers.h
+++ b/libsel4utils/src/helpers.h
@@ -10,18 +10,47 @@
 
 #ifndef SEL4UTILS_HELPERS_H
 #define SEL4UTILS_HELPERS_H
-/**
- * Start a thread.
- *
- * @param local_stack_top address of the stack in the current vspace, or NULL if arguments
- *                        have already been placed on the stack
- * @param dest_stack_top  stack pointer to set in the newly running thread. This is passed in
- *                        instead of being grabbed from thread in case values have been pushed
- *                        onto it. Only one of local_stack_top and dest_stack_top should be set
- * Other parameters see sel4utils_start_thread.
- */
-int sel4utils_internal_start_thread(sel4utils_thread_t *thread, void *entry_point, void *arg0,
-                                    void *arg1, int resume, void *local_stack_top, void *dest_stack_top);
 
+#include <sel4/types.h>
+#include <sel4utils/thread.h>
+#include <stdbool.h>
+#include <vka/vka.h>
+#include <vspace/vspace.h>
+
+#if CONFIG_WORD_SIZE == 64
+#define Elf_auxv_t Elf64_auxv_t
+#elif CONFIG_WORD_SIZE == 32
+#define Elf_auxv_t Elf32_auxv_t
+#else
+#error "Word size unsupported"
+#endif /* CONFIG_WORD_SIZE */
+
+/* write to a remote stack */
+int sel4utils_stack_write(vspace_t *current_vspace, vspace_t *target_vspace,
+                      vka_t *vka, void *buf, size_t len, uintptr_t *stack_top);
+/* 
+ * Initialise a threads user context for a specific architecture
+ * 
+ * @param local_stack true of the stack is mapped in the current address space. If local stack is 
+ *        false and we are running on x86 (32-bit) this function will not copy arg* unless vka, 
+ *        local_vspace and remote_vspace are provided.
+ *
+ * @return 0 on success.
+ */
+int sel4utils_arch_init_context(void *entry_point, void *arg0, void *arg1, void *arg2,
+                                bool local_stack, void *stack_top, seL4_UserContext *context, 
+                                vka_t *vka, vspace_t *local_vspace, vspace_t *remote_vspace);
+
+/* convenient wrappers */
+static inline int
+sel4utils_arch_init_local_context(void *entry_point, void *arg0, void *arg1, void *arg2,
+                                 void *stack_top, seL4_UserContext *context) {
+    return sel4utils_arch_init_context(entry_point, arg0, arg1, arg2, true, stack_top, context, NULL, NULL, NULL);
+}
+
+static inline int
+sel4utils_arch_init_context_without_args(void *entry_point, void *stack_top, seL4_UserContext *context) {
+    return sel4utils_arch_init_context(entry_point, NULL, NULL, NULL, false, stack_top, context, NULL, NULL, NULL);
+}
 
 #endif /* SEL4UTILS_HELPERS_H */
diff --git a/libsel4utils/src/process.c b/libsel4utils/src/process.c
index 2c2006c..0e8873c 100644
--- a/libsel4utils/src/process.c
+++ b/libsel4utils/src/process.c
@@ -141,7 +141,7 @@
     return dest.capPtr;
 }
 
-static int
+int
 sel4utils_stack_write(vspace_t *current_vspace, vspace_t *target_vspace,
                       vka_t *vka, void *buf, size_t len, uintptr_t *stack_top)
 {
@@ -220,23 +220,24 @@
         }
         new_process_argv = stack_top;
     }
-    /* Some architectures want stack to be double word aligned */
-    stack_top = ROUND_DOWN(stack_top, sizeof(void*) * 2);
-
-    /* On arm we write the arguments to registers */
-#ifdef CONFIG_ARCH_IA32
-    /* Write 6 words to make the argument list */
-    uint32_t stack_args[6] = {0, (uint32_t)argc, (uint32_t)new_process_argv, process->thread.ipc_buffer, 0, 0};
-    error = sel4utils_stack_write(vspace, &process->vspace, vka, stack_args, sizeof(stack_args), &stack_top);
+    /* move the stack pointer down to a place we can write too, 
+     * move it two words to retain double word alignment which is required
+     * by some architectures. */
+    stack_top = (uintptr_t) stack_top - sizeof(seL4_Word) * 2;
+ 
+    seL4_UserContext context = {0};
+    size_t context_size = sizeof(seL4_UserContext) / sizeof(seL4_Word);
+   
+    error = sel4utils_arch_init_context(process->entry_point, (void *) argc, 
+                                        (void *) new_process_argv, 
+                                        (void *) process->thread.ipc_buffer_addr, false, 
+                                        (void *) stack_top,
+                                        &context, vka, vspace, &process->vspace);
     if (error) {
-        return -1;
-    }
-#endif
+        return error;
+        }
 
-    error = sel4utils_internal_start_thread(&process->thread, process->entry_point,
-                                            (void *) argc, (void *) new_process_argv, resume, NULL, (void*)stack_top);
-
-    return error;
+    return seL4_TCB_WriteRegisters(process->thread.tcb.cptr, resume, 0, context_size, &context);
 }
 
 int
@@ -245,36 +246,45 @@
 {
     /* define an envp and auxp */
     int envc = 1;
-    char ipc_buf_env[30];
+    char ipc_buf_env[WORD_STRING_SIZE];
     sprintf(ipc_buf_env, "IPCBUFFER=0x%x", process->thread.ipc_buffer_addr);
     char *envp[] = {ipc_buf_env};
     int auxc = process->sysinfo ? 1 : 0;
-#if defined(CONFIG_ARCH_IA32) || defined(CONFIG_ARCH_ARM)
-    Elf32_auxv_t auxv[] = { {.a_type = AT_SYSINFO, .a_un = {process->sysinfo}}};
-#elif defined(CONFIG_X86_64)
-    Elf64_auxv_t auxv[] = { {.a_type = AT_SYSINFO, .a_un = {process->sysinfo}}};
-#else
-#error Not defined
-#endif
-    seL4_UserContext context;
-    memset(&context, 0, sizeof(context));
-    /* write all the strings into the stack */
-    uintptr_t stack_top = (uintptr_t)process->thread.stack_top - 4;
+    Elf_auxv_t auxv[] = { {.a_type = AT_SYSINFO, .a_un = {process->sysinfo}}};
+    seL4_UserContext context = {0};
+
+    uintptr_t stack_top = (uintptr_t) process->thread.stack_top - sizeof(seL4_Word);
     uintptr_t dest_argv[argc];
     uintptr_t dest_envp[envc];
     int error;
+    
+    /* write all the strings into the stack */
     /* Copy over the user arguments */
     error = sel4utils_stack_copy_args(vspace, &process->vspace, vka, argc, argv, dest_argv, &stack_top);
     if (error) {
         return -1;
     }
+
     /* copy the environment */
     error = sel4utils_stack_copy_args(vspace, &process->vspace, vka, envc, envp, dest_envp, &stack_top);
     if (error) {
         return -1;
     }
 
-#if defined(CONFIG_ARCH_IA32) || defined(CONFIG_ARCH_ARM) || defined(CONFIG_X86_64)
+    /* we need to make sure the stack is aligned to a double word boundary after we push on everything else 
+     * below this point. First, work out how much we are going to push */
+    size_t to_push = 5 * sizeof(seL4_Word) + /* constants */
+                    sizeof(auxv[0]) * auxc + /* aux */
+                    sizeof(dest_argv) + /* args */
+                    sizeof(dest_envp); /* env */
+    /* now if the stack is going to end up unaligned */
+    if (((stack_top - to_push) % (2 * sizeof(seL4_Word))) != 0) {
+        /* align it */
+        stack_top -= sizeof(seL4_Word);
+        /* sanity check */
+        assert (((stack_top - to_push) % (2 * sizeof(seL4_Word))) == 0);
+    }
+
     /* construct initial stack frame */
     /* Null terminate aux */
     error = sel4utils_stack_write_constant(vspace, &process->vspace, vka, 0, &stack_top);
@@ -315,32 +325,17 @@
     if (error) {
         return -1;
     }
-#else
-#error Not implemented yet
-#endif
 
-#if defined(CONFIG_ARCH_IA32)
-    /* No atexit pointer */
-    context.edx = 0;
-    context.esp = stack_top;
-    context.gs = IPCBUF_GDT_SELECTOR;
-    context.eip = (seL4_Word)process->entry_point;
-#elif defined(CONFIG_X86_64)
-    context.rdx = 0;
-    context.rsp = stack_top;
-    context.gs = IPCBUF_GDT_SELECTOR;
-    context.rip = (seL4_Word)process->entry_point;
-#elif defined(CONFIG_ARCH_ARM)
-    context.sp = stack_top;
-    context.pc = (seL4_Word)process->entry_point;
-#else
-#error Not implemented yet
-#endif
+    ZF_LOGD("Starting process at %p, stack %p\n", process->entry_point, (void *) stack_top);
+    assert(stack_top % (2 * sizeof(seL4_Word)) == 0);
+    error = sel4utils_arch_init_context_without_args(process->entry_point, (void *) stack_top, &context);
+    if (error) {
+        return error;
+    }
 
-    ZF_LOGD("Starting process at %p\n", process->entry_point);
     /* Write the registers */
-    error = seL4_TCB_WriteRegisters(process->thread.tcb.cptr, resume, 0, sizeof(context) / sizeof(seL4_Word), &context);
-    return error;
+    return seL4_TCB_WriteRegisters(process->thread.tcb.cptr, resume, 0, sizeof(context) / sizeof(seL4_Word), 
+                                  &context);
 }
 
 int
diff --git a/libsel4utils/src/thread.c b/libsel4utils/src/thread.c
index ab0a4f8..0052ab6 100644
--- a/libsel4utils/src/thread.c
+++ b/libsel4utils/src/thread.c
@@ -103,77 +103,21 @@
 }
 
 int
-sel4utils_internal_start_thread(sel4utils_thread_t *thread, void *entry_point, void *arg0,
-                                void *arg1, int resume, void *local_stack_top, void *dest_stack_top)
-{
-    seL4_UserContext context = {0};
-    size_t context_size = sizeof(seL4_UserContext) / sizeof(seL4_Word);
-    int error;
-    /* one of these must be set */
-    assert(local_stack_top || dest_stack_top);
-    /* but not both of them */
-    assert(!(local_stack_top && dest_stack_top));
-
-#ifdef ARCH_IA32
-
-#ifdef CONFIG_X86_64
-    context.rdi = (seL4_Word)arg0;
-    context.rsi = (seL4_Word)arg1;
-    context.rdx = (seL4_Word)thread->ipc_buffer_addr;
-    if (dest_stack_top) {
-        context.rsp = (seL4_Word) dest_stack_top;
-    } else {
-        context.rsp = (seL4_Word) thread->stack_top;
-    }
-    context.gs = IPCBUF_GDT_SELECTOR;
-    context.rip = (seL4_Word)entry_point;
-#else
-    if (local_stack_top) {
-        seL4_Word *stack_ptr = (seL4_Word *) local_stack_top;
-        stack_ptr[-5] = (seL4_Word) arg0;
-        stack_ptr[-4] = (seL4_Word) arg1;
-        stack_ptr[-3] = (seL4_Word) thread->ipc_buffer_addr;
-        context.esp = (seL4_Word) (thread->stack_top - 24);
-    } else {
-        context.esp = (seL4_Word) dest_stack_top;
-    }
-    context.gs = IPCBUF_GDT_SELECTOR;
-    context.eip = (seL4_Word)entry_point;
-#endif
-
-    /* check that stack is aligned correctly */
-    assert((seL4_Word) thread->stack_top % (sizeof(seL4_Word) * 2) == 0);
-    error = seL4_TCB_WriteRegisters(thread->tcb.cptr, resume, 0, context_size, &context);
-#elif ARCH_ARM /* ARCH_IA32 */
-    context.pc = (seL4_Word) entry_point;
-    if (dest_stack_top) {
-        context.sp = (seL4_Word) dest_stack_top;
-    } else {
-        context.sp = (seL4_Word) thread->stack_top;
-    }
-    context.r0 = (seL4_Word) arg0;
-    context.r1 = (seL4_Word) arg1;
-    context.r2 = (seL4_Word) thread->ipc_buffer_addr;
-
-    /* check that stack is aligned correctly */
-    assert((uint32_t) thread->stack_top % (sizeof(seL4_Word) * 2) == 0);
-    error = seL4_TCB_WriteRegisters(thread->tcb.cptr, resume, 0, context_size, &context);
-#endif /* ARCH_ARM */
-
-    if (error) {
-        LOG_ERROR("seL4_TCB_WriteRegisters failed with error %d", error);
-        return -1;
-    }
-
-    return 0;
-}
-
-
-int
 sel4utils_start_thread(sel4utils_thread_t *thread, void *entry_point, void *arg0, void *arg1,
                        int resume)
 {
-    return sel4utils_internal_start_thread(thread, entry_point, arg0, arg1, resume, thread->stack_top, NULL);
+    int error;
+    seL4_UserContext context = {0};
+    size_t context_size = sizeof(seL4_UserContext) / sizeof(seL4_Word);
+   
+    error = sel4utils_arch_init_local_context(entry_point, arg0, arg1, 
+                                              (void *) thread->ipc_buffer_addr,
+                                              thread->stack_top, &context);
+    if (error) {
+        return error;
+    }
+
+    return seL4_TCB_WriteRegisters(thread->tcb.cptr, resume, 0, context_size, &context);
 }
 
 void