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 <sel4runtime/stdint.h>
14
15#ifdef CONFIG_FSGSBASE_INST
16static inline sel4runtime_uintptr_t sel4runtime_read_fs_base(void)
17{
18    sel4runtime_uintptr_t reg;
19    __asm__ __volatile__("rdfsbase %0" : "=r"(reg));
20    return reg;
21}
22
23static inline void sel4runtime_write_fs_base(sel4runtime_uintptr_t reg)
24{
25    __asm__ __volatile__("wrfsbase %0" :: "r"(reg));
26}
27
28static inline sel4runtime_uintptr_t sel4runtime_read_gs_base(void)
29{
30    sel4runtime_uintptr_t reg;
31    __asm__ __volatile__("rdgsbase %0" : "=r"(reg));
32    return reg;
33}
34
35static inline void sel4runtime_write_gs_base(sel4runtime_uintptr_t reg)
36{
37    __asm__ __volatile__("wrgsbase %0" :: "r"(reg));
38}
39
40/*
41 * Obtain the value of the TLS base for the current thread.
42 */
43static inline sel4runtime_uintptr_t sel4runtime_get_tls_base(void)
44{
45    return sel4runtime_read_fs_base();
46}
47
48/*
49 * Set the value of the TLS base for the current thread.
50 */
51static inline void sel4runtime_set_tls_base(sel4runtime_uintptr_t tls_base)
52{
53    sel4runtime_write_fs_base(tls_base);
54}
55
56#else
57
58/*
59 * Obtain the value of the TLS base for the current thread.
60 */
61static inline sel4runtime_uintptr_t sel4runtime_get_tls_base(void)
62{
63    sel4runtime_uintptr_t tp;
64    __asm__ __volatile__("mov %%fs:0,%0" : "=r"(tp));
65    return tp;
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 "Set TLS for x86_64 w/o FSGSBASE_INST not implemented"
78#endif /* CONFIG_SET_TLS_BASE_SELF */
79
80#endif
81
82