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 defined(__BIONIC__)
30 
31 #include <atomic>
32 #include <linux/membarrier.h>
33 
34 #define CHECK_MEMBARRIER_CMD(art_value, membarrier_value) \
35   static_assert(static_cast<int>(art_value) == (membarrier_value), "Bad value for " # art_value)
36 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kQuery, MEMBARRIER_CMD_QUERY);
37 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kGlobal, MEMBARRIER_CMD_SHARED);
38 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kPrivateExpedited, MEMBARRIER_CMD_PRIVATE_EXPEDITED);
39 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kRegisterPrivateExpedited,
40                      MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED);
41 CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kPrivateExpedited, MEMBARRIER_CMD_PRIVATE_EXPEDITED);
42 #undef CHECK_MEMBARRIER_CMD
43 
44 #endif  // __BIONIC
45 
46 namespace art {
47 
48 #if defined(__NR_membarrier)
49 
membarrier(MembarrierCommand command)50 int membarrier(MembarrierCommand command) {
51   // Check kernel version supports membarrier(2).
52   static constexpr int kRequiredMajor = 4;
53   static constexpr int kRequiredMinor = 16;
54   struct utsname uts;
55   int major, minor;
56   if (uname(&uts) != 0 ||
57       strcmp(uts.sysname, "Linux") != 0 ||
58       sscanf(uts.release, "%d.%d", &major, &minor) != 2 ||
59       (major < kRequiredMajor || (major == kRequiredMajor && minor < kRequiredMinor))) {
60     errno = ENOSYS;
61     return -1;
62   }
63 #if defined(__BIONIC__)
64   // Avoid calling membarrier on older Android versions where membarrier may be barred by secomp
65   // causing the current process to be killed. The probing here could be considered expensive so
66   // endeavour not to repeat too often.
67   static int api_level = android_get_device_api_level();
68   if (api_level < __ANDROID_API_Q__) {
69     errno = ENOSYS;
70     return -1;
71   }
72 #endif  // __BIONIC__
73   return syscall(__NR_membarrier, static_cast<int>(command), 0);
74 }
75 
76 #else  // __NR_membarrier
77 
78 int membarrier(MembarrierCommand command ATTRIBUTE_UNUSED) {
79   // In principle this could be supported on linux, but Android's prebuilt glibc does not include
80   // the system call number defintions (b/111199492).
81   errno = ENOSYS;
82   return -1;
83 }
84 
85 #endif  // __NR_membarrier
86 
87 }  // namespace art
88