blob: 490dbf3fa0829733a90442c1078add5a8a996a18 [file] [log] [blame]
Orion Hodson563ada22018-09-04 11:28:31 +01001/*
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
21#include <sys/syscall.h>
22#include <unistd.h>
23#include "macros.h"
24
25#if defined(__BIONIC__)
26
27#include <atomic>
28#include <android/get_device_api_level.h>
29#include <linux/membarrier.h>
30
31#define CHECK_MEMBARRIER_CMD(art_value, membarrier_value) \
32 static_assert(static_cast<int>(art_value) == membarrier_value, "Bad value for " # art_value)
33CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kQuery, MEMBARRIER_CMD_QUERY);
34CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kGlobal, MEMBARRIER_CMD_SHARED);
35CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kPrivateExpedited, MEMBARRIER_CMD_PRIVATE_EXPEDITED);
36CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kRegisterPrivateExpedited,
37 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED);
38CHECK_MEMBARRIER_CMD(art::MembarrierCommand::kPrivateExpedited, MEMBARRIER_CMD_PRIVATE_EXPEDITED);
39#undef CHECK_MEMBARRIER_CMD
40
41#endif // __BIONIC
42
43namespace art {
44
45#if defined(__NR_membarrier)
46
47int membarrier(MembarrierCommand command) {
48#if defined(__BIONIC__)
49 // Avoid calling membarrier on older Android versions where membarrier may be barred by secomp
50 // causing the current process to be killed. The probing here could be considered expensive so
51 // endeavour not to repeat too often.
52 static int api_level = android_get_device_api_level();
53 if (api_level < __ANDROID_API_Q__) {
54 errno = ENOSYS;
55 return -1;
56 }
57#endif // __BIONIC__
58 return syscall(__NR_membarrier, static_cast<int>(command), 0);
59}
60
61#else // __NR_membarrier
62
63int membarrier(MembarrierCommand command ATTRIBUTE_UNUSED) {
64 // In principle this could be supported on linux, but Android's prebuilt glibc does not include
65 // the system call number defintions (b/111199492).
66 errno = ENOSYS;
67 return -1;
68}
69
70#endif // __NR_membarrier
71
72} // namespace art