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 *  @LICENSE(DATA61_BSD)
11 */
12
13#include <sel4debug/stack.h>
14#include <utils/stack.h>
15
16/* A simple spin lock that we will use to protect usage of the emergency stack.
17 * Note that this is not advisable in the presence of threads of different
18 * priorities, but we assume callers are only invoking this as a last ditch
19 * debugging effort anyway. If you use this functionality with threads of
20 * different priorities you will likely livelock your system.
21 */
22static int stack_lock;
23
24/* An emergency stack that we will switch to in order to run the caller's
25 * function. 8K is more or less empirically chosen as "enough to run a trivial
26 * printf."
27 */
28static unsigned char emergency_stack[8 * 1024]
29    __attribute__((aligned(sizeof(long long))));
30
31void *debug_run_on_emergency_stack(void *(*f)(void *), void *arg) {
32    /* Take lock */
33    while (!__sync_bool_compare_and_swap(&stack_lock, 0, 1));
34
35    void *ret = (void*)utils_run_on_stack(
36        (void*)emergency_stack + sizeof(emergency_stack), f, arg);
37
38    /* Release lock */
39    while (!__sync_bool_compare_and_swap(&stack_lock, 1, 0));
40
41    return ret;
42}
43