Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 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> |
Wang Han | 3f43b93 | 2019-09-14 16:47:47 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 21 | |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 22 | #if !defined(_WIN32) |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 23 | #include <sys/syscall.h> |
Lokesh Gidra | 58520df | 2020-05-13 17:08:43 +0000 | [diff] [blame] | 24 | #include <sys/utsname.h> |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 25 | #include <unistd.h> |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 26 | #endif |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 27 | #include "macros.h" |
| 28 | |
| 29 | #if defined(__BIONIC__) |
| 30 | |
| 31 | #include <atomic> |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 32 | #include <linux/membarrier.h> |
| 33 | |
| 34 | #define CHECK_MEMBARRIER_CMD(art_value, membarrier_value) \ |
Andreas Gampe | 584771b | 2018-10-18 13:22:23 -0700 | [diff] [blame] | 35 | static_assert(static_cast<int>(art_value) == (membarrier_value), "Bad value for " # art_value) |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 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 | |
Vladimir Marko | c216744 | 2023-01-17 14:34:08 +0000 | [diff] [blame] | 48 | #if defined(__NR_membarrier) |
Zhen Zhang | 6c2275d | 2023-01-18 20:56:22 +0000 | [diff] [blame] | 49 | |
Vladimir Marko | c2bdabb | 2023-07-11 14:35:19 +0200 | [diff] [blame] | 50 | static bool IsMemBarrierSupported() { |
Wang Han | 3f43b93 | 2019-09-14 16:47:47 +0000 | [diff] [blame] | 51 | // Check kernel version supports membarrier(2). |
Vladimir Marko | c280a8a | 2023-07-31 13:33:12 +0200 | [diff] [blame] | 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. |
Lokesh Gidra | 58520df | 2020-05-13 17:08:43 +0000 | [diff] [blame] | 56 | static constexpr int kRequiredMajor = 4; |
Vladimir Marko | c280a8a | 2023-07-31 13:33:12 +0200 | [diff] [blame] | 57 | static constexpr int kRequiredMinor = 14; |
Lokesh Gidra | 58520df | 2020-05-13 17:08:43 +0000 | [diff] [blame] | 58 | struct utsname uts; |
| 59 | int major, minor; |
| 60 | if (uname(&uts) != 0 || |
| 61 | strcmp(uts.sysname, "Linux") != 0 || |
| 62 | sscanf(uts.release, "%d.%d", &major, &minor) != 2 || |
| 63 | (major < kRequiredMajor || (major == kRequiredMajor && minor < kRequiredMinor))) { |
Vladimir Marko | c2bdabb | 2023-07-11 14:35:19 +0200 | [diff] [blame] | 64 | return false; |
Wang Han | 3f43b93 | 2019-09-14 16:47:47 +0000 | [diff] [blame] | 65 | } |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 66 | #if defined(__BIONIC__) |
| 67 | // Avoid calling membarrier on older Android versions where membarrier may be barred by secomp |
| 68 | // causing the current process to be killed. The probing here could be considered expensive so |
| 69 | // endeavour not to repeat too often. |
Vladimir Marko | c2bdabb | 2023-07-11 14:35:19 +0200 | [diff] [blame] | 70 | int api_level = android_get_device_api_level(); |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 71 | if (api_level < __ANDROID_API_Q__) { |
Vladimir Marko | c2bdabb | 2023-07-11 14:35:19 +0200 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | #endif // __BIONIC__ |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | int membarrier(MembarrierCommand command) { |
| 79 | static const bool membarrier_supported = IsMemBarrierSupported(); |
| 80 | if (UNLIKELY(!membarrier_supported)) { |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 81 | errno = ENOSYS; |
| 82 | return -1; |
| 83 | } |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 84 | return syscall(__NR_membarrier, static_cast<int>(command), 0); |
Zhen Zhang | 6c2275d | 2023-01-18 20:56:22 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 87 | #else // __NR_membarrier |
Zhen Zhang | 6c2275d | 2023-01-18 20:56:22 +0000 | [diff] [blame] | 88 | |
Stefano Cianciulli | 78f3c72 | 2023-05-16 10:32:54 +0000 | [diff] [blame] | 89 | int membarrier([[maybe_unused]] MembarrierCommand command) { |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 90 | // In principle this could be supported on linux, but Android's prebuilt glibc does not include |
Vladimir Marko | c2bdabb | 2023-07-11 14:35:19 +0200 | [diff] [blame] | 91 | // the system call number definitions (b/111199492). |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 92 | errno = ENOSYS; |
| 93 | return -1; |
Vladimir Marko | c216744 | 2023-01-17 14:34:08 +0000 | [diff] [blame] | 94 | } |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 95 | |
Zhen Zhang | 6c2275d | 2023-01-18 20:56:22 +0000 | [diff] [blame] | 96 | #endif // __NR_membarrier |
| 97 | |
Orion Hodson | 563ada2 | 2018-09-04 11:28:31 +0100 | [diff] [blame] | 98 | } // namespace art |