xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/linux.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 //===-- linux.h -------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SCUDO_LINUX_H_
10 #define SCUDO_LINUX_H_
11 
12 #include "platform.h"
13 
14 #if SCUDO_LINUX
15 
16 namespace scudo {
17 
18 // MapPlatformData is unused on Linux, define it as a minimally sized structure.
19 struct MapPlatformData {};
20 
21 #if SCUDO_ANDROID
22 
23 #if defined(__aarch64__)
24 #define __get_tls()                                                            \
25   ({                                                                           \
26     void **__v;                                                                \
27     __asm__("mrs %0, tpidr_el0" : "=r"(__v));                                  \
28     __v;                                                                       \
29   })
30 #elif defined(__arm__)
31 #define __get_tls()                                                            \
32   ({                                                                           \
33     void **__v;                                                                \
34     __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__v));                         \
35     __v;                                                                       \
36   })
37 #elif defined(__i386__)
38 #define __get_tls()                                                            \
39   ({                                                                           \
40     void **__v;                                                                \
41     __asm__("movl %%gs:0, %0" : "=r"(__v));                                    \
42     __v;                                                                       \
43   })
44 #elif defined(__x86_64__)
45 #define __get_tls()                                                            \
46   ({                                                                           \
47     void **__v;                                                                \
48     __asm__("mov %%fs:0, %0" : "=r"(__v));                                     \
49     __v;                                                                       \
50   })
51 #else
52 #error "Unsupported architecture."
53 #endif
54 
55 // The Android Bionic team has allocated a TLS slot for sanitizers starting
56 // with Q, given that Android currently doesn't support ELF TLS. It is used to
57 // store sanitizer thread specific data.
58 static const int TLS_SLOT_SANITIZER = 6;
59 
60 ALWAYS_INLINE uptr *getAndroidTlsPtr() {
61   return reinterpret_cast<uptr *>(&__get_tls()[TLS_SLOT_SANITIZER]);
62 }
63 
64 #endif // SCUDO_ANDROID
65 
66 } // namespace scudo
67 
68 #endif // SCUDO_LINUX
69 
70 #endif // SCUDO_LINUX_H_
71