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
13/*-
14 * Copyright (c) 2014 Antti Kantee.  All Rights Reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <sel4utils/thread.h>
39#include <sel4/helpers.h>
40#include <sel4/kernel.h>
41#include <sel4runtime.h>
42
43#include <bmk-core/platform.h>
44#include <bmk-core/printf.h>
45#include <bmk-core/sched.h>
46
47void
48bmk_platform_cpu_sched_settls(struct bmk_tcb *next)
49{
50    arch_cpu_sched_settls(next->btcb_tp);
51}
52
53void
54bmk_platform_cpu_sched_initcurrent(
55    void *tlsarea,
56    struct bmk_thread *value
57) {
58    sel4runtime_set_tls_variable(
59        (uintptr_t)tlsarea,
60        bmk_current,
61        value
62    );
63    sel4runtime_set_tls_variable(
64        (uintptr_t)tlsarea,
65        __sel4_ipc_buffer,
66        __sel4_ipc_buffer
67    );
68}
69
70
71/*
72 * splhigh()/spl0() internally track depth
73 */
74unsigned long
75bmk_platform_splhigh(void)
76{
77    /* Return immediately if called from the interrupt handler */
78    if (env.mask_the_mask) {
79        return 0;
80    }
81
82    // block interrupts.
83    if (env.spldepth == 0) {
84        sync_bin_sem_wait(&env.spl_semaphore);
85    }
86    env.spldepth++;
87    return 0;
88}
89
90void
91bmk_platform_splx(unsigned long x)
92{
93    if (env.mask_the_mask) {
94        return;
95    }
96    //enable interrupts
97    if (env.spldepth == 0) {
98        bmk_platform_halt("out of interrupt depth!");
99    }
100    if (--env.spldepth == 0) {
101        sync_bin_sem_post(&env.spl_semaphore);
102
103    }
104}
105
106void NORETURN
107bmk_platform_halt(const char *panicstring)
108{
109    if (panicstring) {
110        bmk_printf("PANIC: %s\n", panicstring);
111    }
112    bmk_printf("All is well in the universe.\n");
113    abort();
114    for (;;);
115}
116