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