Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | |
Steven Moreland | c4dd210 | 2017-02-23 13:57:21 -0800 | [diff] [blame] | 17 | #define LOG_TAG "hw-ProcessState" |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 18 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 19 | #include <hwbinder/ProcessState.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 20 | |
Steven Moreland | 5e67788 | 2018-02-23 14:59:21 -0800 | [diff] [blame] | 21 | #include <cutils/atomic.h> |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 22 | #include <hwbinder/BpHwBinder.h> |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 23 | #include <hwbinder/IPCThreadState.h> |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 24 | #include <hwbinder/binder_kernel.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | #include <utils/String8.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 27 | #include <utils/threads.h> |
| 28 | |
| 29 | #include <private/binder/binder_module.h> |
Martijn Coenen | e01f4f2 | 2016-05-12 12:33:28 +0200 | [diff] [blame] | 30 | #include <hwbinder/Static.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 31 | |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <unistd.h> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include <sys/stat.h> |
Philip Cuadra | 788e005 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 40 | #include <sys/types.h> |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 41 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 42 | #define DEFAULT_BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2) |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 43 | #define DEFAULT_MAX_BINDER_THREADS 0 |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 44 | |
Philip Cuadra | 788e005 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 45 | // ------------------------------------------------------------------------- |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 48 | namespace hardware { |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 49 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 50 | class PoolThread : public Thread |
| 51 | { |
| 52 | public: |
Chih-Hung Hsieh | 1f555e9 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 53 | explicit PoolThread(bool isMain) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 54 | : mIsMain(isMain) |
| 55 | { |
| 56 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 57 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 58 | protected: |
| 59 | virtual bool threadLoop() |
| 60 | { |
| 61 | IPCThreadState::self()->joinThreadPool(mIsMain); |
| 62 | return false; |
| 63 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 64 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 65 | const bool mIsMain; |
| 66 | }; |
| 67 | |
| 68 | sp<ProcessState> ProcessState::self() |
| 69 | { |
Mathias Agopian | b9a7d6a | 2012-04-16 19:30:56 -0700 | [diff] [blame] | 70 | Mutex::Autolock _l(gProcessMutex); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 71 | if (gProcess != nullptr) { |
Mathias Agopian | b9a7d6a | 2012-04-16 19:30:56 -0700 | [diff] [blame] | 72 | return gProcess; |
| 73 | } |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 74 | gProcess = new ProcessState(DEFAULT_BINDER_VM_SIZE); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 75 | return gProcess; |
| 76 | } |
| 77 | |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 78 | sp<ProcessState> ProcessState::selfOrNull() { |
| 79 | Mutex::Autolock _l(gProcessMutex); |
| 80 | return gProcess; |
| 81 | } |
| 82 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 83 | sp<ProcessState> ProcessState::initWithMmapSize(size_t mmap_size) { |
| 84 | Mutex::Autolock _l(gProcessMutex); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 85 | if (gProcess != nullptr) { |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 86 | LOG_ALWAYS_FATAL_IF(mmap_size != gProcess->getMmapSize(), |
| 87 | "ProcessState already initialized with a different mmap size."); |
| 88 | return gProcess; |
| 89 | } |
| 90 | |
| 91 | gProcess = new ProcessState(mmap_size); |
| 92 | return gProcess; |
| 93 | } |
| 94 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 95 | void ProcessState::setContextObject(const sp<IBinder>& object) |
| 96 | { |
| 97 | setContextObject(object, String16("default")); |
| 98 | } |
| 99 | |
Colin Cross | f048798 | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 100 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 101 | { |
Jeff Brown | c0f143d | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 102 | return getStrongProxyForHandle(0); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name) |
| 106 | { |
| 107 | AutoMutex _l(mLock); |
| 108 | mContexts.add(name, object); |
| 109 | } |
| 110 | |
| 111 | sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller) |
| 112 | { |
| 113 | mLock.lock(); |
| 114 | sp<IBinder> object( |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 115 | mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : nullptr); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 116 | mLock.unlock(); |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 117 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 118 | //printf("Getting context object %s for %p\n", String8(name).string(), caller.get()); |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 119 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 120 | if (object != nullptr) return object; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 121 | |
| 122 | // Don't attempt to retrieve contexts if we manage them |
| 123 | if (mManagesContexts) { |
Steve Block | eafc621 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 124 | ALOGE("getContextObject(%s) failed, but we manage the contexts!\n", |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 125 | String8(name).string()); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 126 | return nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 127 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 128 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 129 | IPCThreadState* ipc = IPCThreadState::self(); |
| 130 | { |
| 131 | Parcel data, reply; |
| 132 | // no interface token on this magic transaction |
| 133 | data.writeString16(name); |
| 134 | data.writeStrongBinder(caller); |
| 135 | status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0); |
| 136 | if (result == NO_ERROR) { |
| 137 | object = reply.readStrongBinder(); |
| 138 | } |
| 139 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 140 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 141 | ipc->flushCommands(); |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 142 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 143 | if (object != nullptr) setContextObject(object, name); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 144 | return object; |
| 145 | } |
| 146 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 147 | void ProcessState::startThreadPool() |
| 148 | { |
| 149 | AutoMutex _l(mLock); |
| 150 | if (!mThreadPoolStarted) { |
| 151 | mThreadPoolStarted = true; |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 152 | if (mSpawnThreadOnStart) { |
| 153 | spawnPooledThread(true); |
| 154 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | bool ProcessState::isContextManager(void) const |
| 159 | { |
| 160 | return mManagesContexts; |
| 161 | } |
| 162 | |
| 163 | bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData) |
| 164 | { |
| 165 | if (!mManagesContexts) { |
| 166 | AutoMutex _l(mLock); |
| 167 | mBinderContextCheckFunc = checkFunc; |
| 168 | mBinderContextUserData = userData; |
Jeff Brown | c0f143d | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 169 | |
Steven Moreland | c149dca | 2019-01-09 18:01:02 -0800 | [diff] [blame] | 170 | flat_binder_object obj { |
| 171 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, |
| 172 | }; |
| 173 | |
| 174 | status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); |
| 175 | |
| 176 | // fallback to original method |
| 177 | if (result != 0) { |
| 178 | android_errorWriteLog(0x534e4554, "121035042"); |
| 179 | |
| 180 | int dummy = 0; |
| 181 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy); |
| 182 | } |
| 183 | |
Jeff Brown | c0f143d | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 184 | if (result == 0) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 185 | mManagesContexts = true; |
Jeff Brown | c0f143d | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 186 | } else if (result == -1) { |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 187 | mBinderContextCheckFunc = nullptr; |
| 188 | mBinderContextUserData = nullptr; |
Steve Block | eafc621 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 189 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | return mManagesContexts; |
| 193 | } |
| 194 | |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 195 | // Get references to userspace objects held by the kernel binder driver |
| 196 | // Writes up to count elements into buf, and returns the total number |
| 197 | // of references the kernel has, which may be larger than count. |
| 198 | // buf may be NULL if count is 0. The pointers returned by this method |
| 199 | // should only be used for debugging and not dereferenced, they may |
| 200 | // already be invalid. |
| 201 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) { |
| 202 | binder_node_debug_info info = {}; |
| 203 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 204 | uintptr_t* end = buf ? buf + buf_count : nullptr; |
Colin Cross | dd6dafb | 2017-06-22 12:29:13 -0700 | [diff] [blame] | 205 | size_t count = 0; |
| 206 | |
| 207 | do { |
| 208 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); |
| 209 | if (result < 0) { |
| 210 | return -1; |
| 211 | } |
| 212 | if (info.ptr != 0) { |
| 213 | if (buf && buf < end) *buf++ = info.ptr; |
| 214 | count++; |
| 215 | if (buf && buf < end) *buf++ = info.cookie; |
| 216 | count++; |
| 217 | } |
| 218 | } while (info.ptr != 0); |
| 219 | |
| 220 | return count; |
| 221 | } |
| 222 | |
Martijn Coenen | 320e1d3 | 2018-09-07 10:41:33 +0200 | [diff] [blame] | 223 | // Queries the driver for the current strong reference count of the node |
| 224 | // that the handle points to. Can only be used by the servicemanager. |
| 225 | // |
| 226 | // Returns -1 in case of failure, otherwise the strong reference count. |
| 227 | ssize_t ProcessState::getStrongRefCountForNodeByHandle(int32_t handle) { |
| 228 | binder_node_info_for_ref info; |
| 229 | memset(&info, 0, sizeof(binder_node_info_for_ref)); |
| 230 | |
| 231 | info.handle = handle; |
| 232 | |
| 233 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info); |
| 234 | |
| 235 | if (result != OK) { |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | return info.strong_count; |
| 240 | } |
| 241 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 242 | size_t ProcessState::getMmapSize() { |
| 243 | return mMmapSize; |
| 244 | } |
| 245 | |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 246 | void ProcessState::setCallRestriction(CallRestriction restriction) { |
| 247 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull(), "Call restrictions must be set before the threadpool is started."); |
| 248 | |
| 249 | mCallRestriction = restriction; |
| 250 | } |
| 251 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 252 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) |
| 253 | { |
| 254 | const size_t N=mHandleToObject.size(); |
| 255 | if (N <= (size_t)handle) { |
| 256 | handle_entry e; |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 257 | e.binder = nullptr; |
| 258 | e.refs = nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 259 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 260 | if (err < NO_ERROR) return nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 261 | } |
| 262 | return &mHandleToObject.editItemAt(handle); |
| 263 | } |
| 264 | |
| 265 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) |
| 266 | { |
| 267 | sp<IBinder> result; |
| 268 | |
| 269 | AutoMutex _l(mLock); |
| 270 | |
| 271 | handle_entry* e = lookupHandleLocked(handle); |
| 272 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 273 | if (e != nullptr) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 274 | // We need to create a new BpHwBinder if there isn't currently one, OR we |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 275 | // are unable to acquire a weak reference on this current one. See comment |
| 276 | // in getWeakProxyForHandle() for more info about this. |
| 277 | IBinder* b = e->binder; |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 278 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 279 | b = new BpHwBinder(handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 280 | e->binder = b; |
| 281 | if (b) e->refs = b->getWeakRefs(); |
| 282 | result = b; |
| 283 | } else { |
| 284 | // This little bit of nastyness is to allow us to add a primary |
| 285 | // reference to the remote proxy when this team doesn't have one |
| 286 | // but another team is sending the handle to us. |
| 287 | result.force_set(b); |
| 288 | e->refs->decWeak(this); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return result; |
| 293 | } |
| 294 | |
| 295 | wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle) |
| 296 | { |
| 297 | wp<IBinder> result; |
| 298 | |
| 299 | AutoMutex _l(mLock); |
| 300 | |
| 301 | handle_entry* e = lookupHandleLocked(handle); |
| 302 | |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 303 | if (e != nullptr) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 304 | // We need to create a new BpHwBinder if there isn't currently one, OR we |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 305 | // are unable to acquire a weak reference on this current one. The |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 306 | // attemptIncWeak() is safe because we know the BpHwBinder destructor will always |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 307 | // call expungeHandle(), which acquires the same lock we are holding now. |
| 308 | // We need to do this because there is a race condition between someone |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 309 | // releasing a reference on this BpHwBinder, and a new reference on its handle |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 310 | // arriving from the driver. |
| 311 | IBinder* b = e->binder; |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 312 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 313 | b = new BpHwBinder(handle); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 314 | result = b; |
| 315 | e->binder = b; |
| 316 | if (b) e->refs = b->getWeakRefs(); |
| 317 | } else { |
| 318 | result = b; |
| 319 | e->refs->decWeak(this); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return result; |
| 324 | } |
| 325 | |
| 326 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) |
| 327 | { |
| 328 | AutoMutex _l(mLock); |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 329 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 330 | handle_entry* e = lookupHandleLocked(handle); |
| 331 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 332 | // This handle may have already been replaced with a new BpHwBinder |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 333 | // (if someone failed the AttemptIncWeak() above); we don't want |
| 334 | // to overwrite it. |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 335 | if (e && e->binder == binder) e->binder = nullptr; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 338 | String8 ProcessState::makeBinderThreadName() { |
| 339 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); |
Philip Cuadra | 788e005 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 340 | pid_t pid = getpid(); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 341 | String8 name; |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 342 | name.appendFormat("HwBinder:%d_%X", pid, s); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 343 | return name; |
| 344 | } |
| 345 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 346 | void ProcessState::spawnPooledThread(bool isMain) |
| 347 | { |
| 348 | if (mThreadPoolStarted) { |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 349 | String8 name = makeBinderThreadName(); |
| 350 | ALOGV("Spawning new pooled thread, name=%s\n", name.string()); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 351 | sp<Thread> t = new PoolThread(isMain); |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 352 | t->run(name.string()); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 356 | status_t ProcessState::setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool) { |
Steven Moreland | 2aa3707 | 2018-10-25 16:32:17 -0700 | [diff] [blame] | 357 | // if the caller joins the pool, then there will be one thread which is impossible. |
| 358 | LOG_ALWAYS_FATAL_IF(maxThreads == 0 && callerJoinsPool, |
| 359 | "Binder threadpool must have a minimum of one thread if caller joins pool."); |
| 360 | |
| 361 | size_t threadsToAllocate = maxThreads; |
| 362 | |
| 363 | // If the caller is going to join the pool it will contribute one thread to the threadpool. |
| 364 | // This is part of the API's contract. |
| 365 | if (callerJoinsPool) threadsToAllocate--; |
| 366 | |
| 367 | // If we can, spawn one thread from userspace when the threadpool is started. This ensures |
| 368 | // that there is always a thread available to start more threads as soon as the threadpool |
| 369 | // is started. |
| 370 | bool spawnThreadOnStart = threadsToAllocate > 0; |
| 371 | if (spawnThreadOnStart) threadsToAllocate--; |
| 372 | |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 373 | // the BINDER_SET_MAX_THREADS ioctl really tells the kernel how many threads |
| 374 | // it's allowed to spawn, *in addition* to any threads we may have already |
Steven Moreland | 2aa3707 | 2018-10-25 16:32:17 -0700 | [diff] [blame] | 375 | // spawned locally. |
| 376 | size_t kernelMaxThreads = threadsToAllocate; |
| 377 | |
| 378 | AutoMutex _l(mLock); |
| 379 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &kernelMaxThreads) == -1) { |
| 380 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); |
| 381 | return -errno; |
Mathias Agopian | 8297f50 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 382 | } |
Steven Moreland | 2aa3707 | 2018-10-25 16:32:17 -0700 | [diff] [blame] | 383 | |
| 384 | mMaxThreads = maxThreads; |
| 385 | mSpawnThreadOnStart = spawnThreadOnStart; |
| 386 | |
| 387 | return NO_ERROR; |
Mathias Agopian | 8297f50 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Martijn Coenen | 420d4bb | 2017-10-24 11:43:55 +0200 | [diff] [blame] | 390 | size_t ProcessState::getMaxThreads() { |
| 391 | return mMaxThreads; |
| 392 | } |
| 393 | |
Mathias Agopian | d191ed7 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 394 | void ProcessState::giveThreadPoolName() { |
| 395 | androidSetThreadName( makeBinderThreadName().string() ); |
| 396 | } |
| 397 | |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 398 | static int open_driver() |
| 399 | { |
Martijn Coenen | a660cbc | 2016-05-12 11:29:23 +0200 | [diff] [blame] | 400 | int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 401 | if (fd >= 0) { |
Jin Wei | 023b468 | 2012-10-18 17:00:48 +0800 | [diff] [blame] | 402 | int vers = 0; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 403 | status_t result = ioctl(fd, BINDER_VERSION, &vers); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 404 | if (result == -1) { |
Steve Block | eafc621 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 405 | ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 406 | close(fd); |
| 407 | fd = -1; |
| 408 | } |
| 409 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { |
Greg Hartman | d521774 | 2017-01-03 16:56:05 -0800 | [diff] [blame] | 410 | ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)!", vers, BINDER_CURRENT_PROTOCOL_VERSION); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 411 | close(fd); |
| 412 | fd = -1; |
| 413 | } |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 414 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 415 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); |
| 416 | if (result == -1) { |
Steve Block | eafc621 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 417 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 418 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 419 | } else { |
Iliyan Malchev | e3256a6 | 2016-09-19 16:00:32 -0700 | [diff] [blame] | 420 | ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 421 | } |
| 422 | return fd; |
| 423 | } |
| 424 | |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 425 | ProcessState::ProcessState(size_t mmap_size) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 426 | : mDriverFD(open_driver()) |
| 427 | , mVMStart(MAP_FAILED) |
Wale Ogunwale | 2e604f0 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 428 | , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) |
| 429 | , mThreadCountDecrement(PTHREAD_COND_INITIALIZER) |
| 430 | , mExecutingThreadsCount(0) |
| 431 | , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) |
Colin Cross | b1dc654 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 432 | , mStarvationStartTimeMs(0) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 433 | , mManagesContexts(false) |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 434 | , mBinderContextCheckFunc(nullptr) |
| 435 | , mBinderContextUserData(nullptr) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 436 | , mThreadPoolStarted(false) |
Martijn Coenen | ab00b6a | 2016-11-22 15:03:13 +0100 | [diff] [blame] | 437 | , mSpawnThreadOnStart(true) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 438 | , mThreadPoolSeq(1) |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 439 | , mMmapSize(mmap_size) |
Steven Moreland | 1460300 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 440 | , mCallRestriction(CallRestriction::NONE) |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 441 | { |
| 442 | if (mDriverFD >= 0) { |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 443 | // mmap the binder, providing a chunk of virtual address space to receive transactions. |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 444 | mVMStart = mmap(nullptr, mMmapSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 445 | if (mVMStart == MAP_FAILED) { |
| 446 | // *sigh* |
Jiyong Park | 1268c28 | 2019-01-28 16:22:49 +0900 | [diff] [blame] | 447 | ALOGE("Mmapping /dev/hwbinder failed: %s\n", strerror(errno)); |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 448 | close(mDriverFD); |
| 449 | mDriverFD = -1; |
| 450 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 451 | } |
Iliyan Malchev | b7e2233 | 2016-09-26 00:08:55 -0700 | [diff] [blame] | 452 | else { |
| 453 | ALOGE("Binder driver could not be opened. Terminating."); |
| 454 | } |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | ProcessState::~ProcessState() |
| 458 | { |
zhongjie | 8e8a025 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 459 | if (mDriverFD >= 0) { |
| 460 | if (mVMStart != MAP_FAILED) { |
Martijn Coenen | 32507d3 | 2018-03-22 17:12:57 +0100 | [diff] [blame] | 461 | munmap(mVMStart, mMmapSize); |
zhongjie | 8e8a025 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 462 | } |
| 463 | close(mDriverFD); |
| 464 | } |
| 465 | mDriverFD = -1; |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 466 | } |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 467 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 468 | }; // namespace hardware |
Mathias Agopian | 7922fa2 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 469 | }; // namespace android |