libsel4utils: Split context initialization into args and non args portions for clarity of use
diff --git a/libsel4utils/src/arch/arm/arch.c b/libsel4utils/src/arch/arm/arch.c
index 69b8325..8e709f1 100644
--- a/libsel4utils/src/arch/arm/arch.c
+++ b/libsel4utils/src/arch/arm/arch.c
@@ -14,24 +14,29 @@
 
 #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)
+int
+sel4utils_arch_init_context(void *entry_point, void *stack_top, seL4_UserContext *context)
 {
-    
     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;
 }
 
+int
+sel4utils_arch_init_context_with_args(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->r0 = (seL4_Word) arg0;
+    context->r1 = (seL4_Word) arg1;
+    context->r2 = (seL4_Word) arg2;
+
+    return sel4utils_init_context(entry_point, stack_top, context);
+}
+
 
diff --git a/libsel4utils/src/arch/x86/arch.c b/libsel4utils/src/arch/x86/arch.c
index bbd8d1d..ead6b75 100644
--- a/libsel4utils/src/arch/x86/arch.c
+++ b/libsel4utils/src/arch/x86/arch.c
@@ -14,12 +14,25 @@
 
 #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, 
+int
+sel4utils_arch_init_context(void *entry_point, void *stack_top, seL4_UserContext *context)
+{
+    context->esp = (seL4_Word) stack_top;
+    /* set edx to zero in case we are setting this when spawning a process as
+     * edx is the atexit parameter, which we currently do not use */
+    context->edx = 0;
+    context->gs  = IPCBUF_GDT_SELECTOR;
+    context->eip = (seL4_Word) entry_point;
+
+    return 0;
+}
+
+int
+sel4utils_arch_init_context_with_args(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;
@@ -28,19 +41,14 @@
         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), 
+        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;
+    return sel4utils_arch_init_context(entry_point, stack_top, context);
 }
 
 
diff --git a/libsel4utils/src/arch/x86_64/arch.c b/libsel4utils/src/arch/x86_64/arch.c
index 50c9c9f..5b465ac 100644
--- a/libsel4utils/src/arch/x86_64/arch.c
+++ b/libsel4utils/src/arch/x86_64/arch.c
@@ -14,20 +14,35 @@
 
 #include "../../helpers.h"
 
-int 
-sel4utils_arch_init_context(void *entry_point, void *arg0, void *arg1, void *arg2,
+int
+sel4utils_arch_init_context(void *entry_point, void *stack_top, seL4_userContext *context)
+{
+    context->rsp = (seL4_Word) stack_top;
+    context->rip = (seL4_Word) entry_point;
+    /* set edx to zero in case we are setting this when spawning a process as
+     * rdx is the atexit parameter, which we currently do not use */
+    context->rdx = 0;
+    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;
+}
+
+int
+sel4utils_arch_init_context_with_args(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;
+    /* if we are setting args then we must not be spawning a process, therefore
+     * even though rdx was set to zero by sel4utils_arch_init_context we can
+     * safely put the arg in here */
     context->rdx = (seL4_Word) arg2;
-    context->esp = (seL4_Word) stack_top;
-    context->gs = IPCBUF_GDT_SELECTOR;
-    context->eip = (seL4_Word) entry_point;
 
-    return 0;
+    return sel4utils_arch_init_context(entry_point, stack_top, context);
 }
 
 
diff --git a/libsel4utils/src/helpers.h b/libsel4utils/src/helpers.h
index 0e11a4c..b2cc315 100644
--- a/libsel4utils/src/helpers.h
+++ b/libsel4utils/src/helpers.h
@@ -28,29 +28,33 @@
 /* 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, 
+
+/*
+ * Initialize a threads user context for a specific architecture
+ *
+ * @return 0 on success.
+ */
+int sel4utils_arch_init_context(void *entry_point, void *stack_top, seL4_UserContext *context);
+
+/*
+ * Legacy function to initialise a threads user context for a specific architecture, and put
+ * some arguments into registers/stack
+ *
+ * @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, 
+int sel4utils_arch_init_context_with_args(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);
+    return sel4utils_arch_init_context_with_args(entry_point, arg0, arg1, arg2, true, stack_top, context, NULL, NULL, NULL);
 }
 
 #endif /* SEL4UTILS_HELPERS_H */
diff --git a/libsel4utils/src/process.c b/libsel4utils/src/process.c
index 0e8873c..3e9d973 100644
--- a/libsel4utils/src/process.c
+++ b/libsel4utils/src/process.c
@@ -220,17 +220,17 @@
         }
         new_process_argv = stack_top;
     }
-    /* move the stack pointer down to a place we can write too, 
+    /* 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, 
+
+    error = sel4utils_arch_init_context_with_args(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) {
@@ -328,7 +328,7 @@
 
     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);
+    error = sel4utils_arch_init_context(process->entry_point, (void *) stack_top, &context);
     if (error) {
         return error;
     }
diff --git a/libsel4utils/src/thread.c b/libsel4utils/src/thread.c
index 0052ab6..fc08422 100644
--- a/libsel4utils/src/thread.c
+++ b/libsel4utils/src/thread.c
@@ -109,8 +109,8 @@
     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, 
+
+    error = sel4utils_arch_init_local_context(entry_point, arg0, arg1,
                                               (void *) thread->ipc_buffer_addr,
                                               thread->stack_top, &context);
     if (error) {