• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
25 #endif
26 
27 #include "macros.h"
28 #include "utils.h"
29 
30 #if __has_include(<linux/membarrier.h>)
31 
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  // __has_include(<linux/membarrier.h>)
45 
46 namespace art {
47 
48 #if defined(__linux__)
49 
IsMemBarrierSupported()50 static bool IsMemBarrierSupported() {
51   // Check kernel version supports membarrier(2).
52   // MEMBARRIER_CMD_QUERY is supported since Linux 4.3.
53   // MEMBARRIER_CMD_PRIVATE_EXPEDITED is supported since Linux 4.14.
54   // MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE is supported since Linux 4.16.
55   // Lowest Linux version useful for ART is 4.14.
56   return IsKernelVersionAtLeast(4, 14);
57 }
58 
membarrier(MembarrierCommand command)59 int membarrier(MembarrierCommand command) {
60   static const bool membarrier_supported = IsMemBarrierSupported();
61   if (UNLIKELY(!membarrier_supported)) {
62     errno = ENOSYS;
63     return -1;
64   }
65   return syscall(__NR_membarrier, static_cast<int>(command), 0);
66 }
67 
68 #else  // __linux__
69 
70 int membarrier([[maybe_unused]] MembarrierCommand command) {
71   errno = ENOSYS;
72   return -1;
73 }
74 
75 #endif  // __linux__
76 
77 }  // namespace art
78