1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12#include <autoconf.h>
13#include <sel4utils/gen_config.h>
14#include <sel4/types.h>
15#include <sel4utils/thread.h>
16#include <sel4utils/helpers.h>
17#include <utils/zf_log.h>
18#include <utils/stack.h>
19#include <stdbool.h>
20
21int sel4utils_arch_init_context(void *entry_point, void *stack_top, seL4_UserContext *context)
22{
23    context->rsp = (seL4_Word) stack_top;
24    /* set edx to zero in case we are setting this when spawning a process as
25     * edx is the atexit parameter, which we currently do not use */
26    context->rdx = 0;
27    context->rip = (seL4_Word) entry_point;
28    return 0;
29}
30
31int sel4utils_arch_init_context_with_args(sel4utils_thread_entry_fn entry_point,
32                                          void *arg0, void *arg1, void *arg2,
33                                          bool local_stack, void *stack_top, seL4_UserContext *context,
34                                          vka_t *vka, vspace_t *local_vspace, vspace_t *remote_vspace)
35{
36
37    if (!IS_ALIGNED((uintptr_t)stack_top, STACK_CALL_ALIGNMENT_BITS)) {
38        ZF_LOGE("Initial stack pointer must be %d byte aligned", STACK_CALL_ALIGNMENT);
39        return -1;
40    }
41
42    /* align the stack pointer so that when we are in the function pointed to
43     * by entry_point, the stack pointer is aligned as if a return address had
44     * been pushed onto the stack, which is what the compiler assumes */
45    uintptr_t stack_top_after_call = (uintptr_t) stack_top - sizeof(uintptr_t);
46    sel4utils_arch_init_context(entry_point, (void *)stack_top_after_call, context);
47    context->rdi = (seL4_Word) arg0;
48    context->rsi = (seL4_Word) arg1;
49    /* if we are setting args then we must not be spawning a process, therefore
50     * even though rdx was set to zero by sel4utils_arch_init_context we can
51     * safely put the arg in here */
52    context->rdx = (seL4_Word) arg2;
53    return 0;
54}
55