1/*
2 * Copyright 2018, 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 <utils/gen_config.h>
14#include <utils/stack.h>
15
16void *
17utils_run_on_stack(void *stack_top, void * (*func)(void *arg), void *arg)
18{
19    void *ret;
20    asm volatile (
21        "mv s0, sp\n\t"                /* Save sp - s0 is callee saved so we don't need to save it. */
22        "mv sp, %[new_stack]\n\t"      /* Switch to new stack. */
23        "mv a0, %[arg]\n\t"            /* Setup argument to func. */
24        "jalr %[func]\n\t"
25        "mv sp, s0\n\t"
26        "mv %[ret], a0\n\t"
27        : [ret] "=r" (ret)
28        : [new_stack] "r" (stack_top),
29        [func] "r" (func),
30        [arg] "r" (arg)
31        /* caller saved registers */
32        : "t0", "t1", "t2", "t3", "t4", "t5", "t6", "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "ra");
33    return ret;
34}
35