1/*
2 * Copyright 2019, 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 <sel4/arch/constants.h>
14#include <sel4runtime/stdint.h>
15
16#if ((__ARM_ARCH_6K__ || __ARM_ARCH_6ZK__) && !__thumb__) \
17 || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH >= 7
18
19static inline sel4runtime_uintptr_t sel4runtime_read_tpidr_el0(void)
20{
21    sel4runtime_uintptr_t reg;
22    __asm__ __volatile__("mrc p15,0,%0,c13,c0,2" : "=r"(reg));
23    return reg;
24}
25
26static inline void sel4runtime_write_tpidr_el0(sel4runtime_uintptr_t reg)
27{
28    __asm__ __volatile__("mcr p15,0,%0,c13,c0,2" :: "r"(reg));
29}
30
31static inline sel4runtime_uintptr_t sel4runtime_read_tpidrro_el0(void)
32{
33    sel4runtime_uintptr_t reg;
34    __asm__ __volatile__("mrc p15,0,%0,c13,c0,3" : "=r"(reg));
35    return reg;
36}
37
38/*
39 * Set the value of the TLS base for the current thread.
40 */
41static inline void sel4runtime_set_tls_base(sel4runtime_uintptr_t tls_base)
42{
43    sel4runtime_write_tpidr_el0(tls_base);
44}
45
46#elif defined(CONFIG_KERNEL_GLOBALS_FRAME)
47
48/*
49 * In the case of early versions of ARMv6, there are no hardware
50 * registers provided for thread-local identifiers. seL4 resolves this
51 * by placing the IPC buffer address and thread pointer in a
52 * `GlobalsFrame` mapped at the same address in all virtual address
53 * spaces. The IPC buffer and thread pointer occupy the first two words
54 * in this frame respectively.
55 */
56static inline sel4runtime_uintptr_t sel4runtime_read_tpidr_el0(void)
57{
58    void **globals_frame = (void **)seL4_GlobalsFrame;
59    return (sel4runtime_uintptr_t)globals_frame[0];
60}
61
62static inline sel4runtime_uintptr_t sel4runtime_read_tpidrro_el0(void)
63{
64    void **globals_frame = (void **)seL4_GlobalsFrame;
65    return (sel4runtime_uintptr_t)globals_frame[1];
66}
67
68#ifdef CONFIG_SET_TLS_BASE_SELF
69/*
70 * Set the value of the TLS base for the current thread.
71 */
72static inline void sel4runtime_set_tls_base(sel4runtime_uintptr_t tls_base)
73{
74    seL4_SetTLSBase(tls_base);
75}
76#else
77#error "No way to set TLS base provided."
78#endif
79#endif /* CONFIG_SET_TLS_BASE_SELF */
80
81/*
82 * Obtain the value of the TLS base for the current thread.
83 */
84static inline sel4runtime_uintptr_t sel4runtime_get_tls_base(void)
85{
86    return sel4runtime_read_tpidr_el0();
87}
88
89#define TLS_ABOVE_TP
90#define GAP_ABOVE_TP 8
91