1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "membarrier.h"
18 
19 #include <errno.h>
20 #include <stdio.h>
21 
22 #if !defined(_WIN32)
23 #include <sys/syscall.h>
24 #include <sys/utsname.h>
25 #include <unistd.h>
26 #endif
27 #include "macros.h"
28 
29 #if __has_include(<linux/membarrier.h>)
30 
31 #include <linux/membarrier.h>
32 
33 #define CHECK_MEMBARRIER_CMD(art_value, membarrier_value) \
34   static_assert(static_cast<int>(art_value) == (membarrier_value), "Bad value for " # art_value)
35 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kQuery, MEMBARRIER_CMD_QUERY);
36 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kGlobal, MEMBARRIER_CMD_SHARED);
37 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kPrivateExpedited, MEMBARRIER_CMD_PRIVATE_EXPEDITED);
38 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kRegisterPrivateExpedited,
39                      MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED);
40 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kPrivateExpedited, MEMBARRIER_CMD_PRIVATE_EXPEDITED);
41 #undef CHECK_MEMBARRIER_CMD
42 
43 #endif  // __has_include(<linux/membarrier.h>)
44 
45 namespace art {
46 
47 #if defined(__linux__)
48 
IsMemBarrierSupported()49 static bool IsMemBarrierSupported() {
50   // Check kernel version supports membarrier(2).
51   // MEMBARRIER_CMD_QUERY is supported since Linux 4.3.
52   // MEMBARRIER_CMD_PRIVATE_EXPEDITED is supported since Linux 4.14.
53   // MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE is supported since Linux 4.16.
54   // Lowest Linux version useful for ART is 4.14.
55   static constexpr int kRequiredMajor = 4;
56   static constexpr int kRequiredMinor = 14;
57   struct utsname uts;
58   int major, minor;
59   if (uname(&uts) != 0 ||
60       strcmp(uts.sysname, "Linux") != 0 ||
61       sscanf(uts.release, "%d.%d", &major, &minor) != 2 ||
62       (major < kRequiredMajor || (major == kRequiredMajor && minor < kRequiredMinor))) {
63     return false;
64   }
65 #if defined(__BIONIC__)
66   // Avoid calling membarrier on older Android versions where membarrier may be barred by seccomp
67   // causing the current process to be killed. The probing here could be considered expensive so
68   // endeavour not to repeat too often.
69   int api_level = android_get_device_api_level();
70   if (api_level < __ANDROID_API_Q__) {
71     return false;
72   }
73 #endif  // __BIONIC__
74   return true;
75 }
76 
membarrier(MembarrierCommand command)77 int membarrier(MembarrierCommand command) {
78   static const bool membarrier_supported = IsMemBarrierSupported();
79   if (UNLIKELY(!membarrier_supported)) {
80     errno = ENOSYS;
81     return -1;
82   }
83   return syscall(__NR_membarrier, static_cast<int>(command), 0);
84 }
85 
86 #else  // __linux__
87 
88 int membarrier([[maybe_unused]] MembarrierCommand command) {
89   errno = ENOSYS;
90   return -1;
91 }
92 
93 #endif  // __linux__
94 
95 }  // namespace art
96