blob: 9ce070c0822bdba433cb14b7a82d1a05d981895d [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
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 Morelandc4dd2102017-02-23 13:57:21 -080017#define LOG_TAG "hw-ProcessState"
Mathias Agopian7922fa22009-05-18 15:08:03 -070018
Martijn Coenen4080edc2016-05-04 14:17:02 +020019#include <hwbinder/ProcessState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070020
21#include <utils/Atomic.h>
Yifan Hong1e118d22017-01-12 14:42:28 -080022#include <hwbinder/BpHwBinder.h>
Martijn Coenen4080edc2016-05-04 14:17:02 +020023#include <hwbinder/IPCThreadState.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070024#include <utils/Log.h>
25#include <utils/String8.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070026#include <utils/String8.h>
27#include <utils/threads.h>
28
29#include <private/binder/binder_module.h>
Martijn Coenene01f4f22016-05-12 12:33:28 +020030#include <hwbinder/Static.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070031
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 Cuadra788e0052016-04-08 10:29:14 -070040#include <sys/types.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070041
Rebecca Schultz Zavinba3e5e22009-10-30 18:39:55 -070042#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
Martijn Coenenab00b6a2016-11-22 15:03:13 +010043#define DEFAULT_MAX_BINDER_THREADS 0
Mathias Agopian7922fa22009-05-18 15:08:03 -070044
Philip Cuadra788e0052016-04-08 10:29:14 -070045// -------------------------------------------------------------------------
Mathias Agopian7922fa22009-05-18 15:08:03 -070046
47namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020048namespace hardware {
Wale Ogunwale2e604f02015-04-13 16:16:10 -070049
Mathias Agopian7922fa22009-05-18 15:08:03 -070050class PoolThread : public Thread
51{
52public:
Chih-Hung Hsieh1f555e92016-04-25 15:41:05 -070053 explicit PoolThread(bool isMain)
Mathias Agopian7922fa22009-05-18 15:08:03 -070054 : mIsMain(isMain)
55 {
56 }
Yifan Hong1e118d22017-01-12 14:42:28 -080057
Mathias Agopian7922fa22009-05-18 15:08:03 -070058protected:
59 virtual bool threadLoop()
60 {
61 IPCThreadState::self()->joinThreadPool(mIsMain);
62 return false;
63 }
Yifan Hong1e118d22017-01-12 14:42:28 -080064
Mathias Agopian7922fa22009-05-18 15:08:03 -070065 const bool mIsMain;
66};
67
68sp<ProcessState> ProcessState::self()
69{
Mathias Agopianb9a7d6a2012-04-16 19:30:56 -070070 Mutex::Autolock _l(gProcessMutex);
71 if (gProcess != NULL) {
72 return gProcess;
73 }
74 gProcess = new ProcessState;
Mathias Agopian7922fa22009-05-18 15:08:03 -070075 return gProcess;
76}
77
Mathias Agopian7922fa22009-05-18 15:08:03 -070078void ProcessState::setContextObject(const sp<IBinder>& object)
79{
80 setContextObject(object, String16("default"));
81}
82
Colin Crossf0487982014-02-05 17:42:44 -080083sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/)
Mathias Agopian7922fa22009-05-18 15:08:03 -070084{
Jeff Brownc0f143d2011-07-08 18:52:57 -070085 return getStrongProxyForHandle(0);
Mathias Agopian7922fa22009-05-18 15:08:03 -070086}
87
88void ProcessState::setContextObject(const sp<IBinder>& object, const String16& name)
89{
90 AutoMutex _l(mLock);
91 mContexts.add(name, object);
92}
93
94sp<IBinder> ProcessState::getContextObject(const String16& name, const sp<IBinder>& caller)
95{
96 mLock.lock();
97 sp<IBinder> object(
98 mContexts.indexOfKey(name) >= 0 ? mContexts.valueFor(name) : NULL);
99 mLock.unlock();
Yifan Hong1e118d22017-01-12 14:42:28 -0800100
Mathias Agopian7922fa22009-05-18 15:08:03 -0700101 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
Yifan Hong1e118d22017-01-12 14:42:28 -0800102
Mathias Agopian7922fa22009-05-18 15:08:03 -0700103 if (object != NULL) return object;
104
105 // Don't attempt to retrieve contexts if we manage them
106 if (mManagesContexts) {
Steve Blockeafc6212012-01-06 19:20:56 +0000107 ALOGE("getContextObject(%s) failed, but we manage the contexts!\n",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700108 String8(name).string());
109 return NULL;
110 }
Yifan Hong1e118d22017-01-12 14:42:28 -0800111
Mathias Agopian7922fa22009-05-18 15:08:03 -0700112 IPCThreadState* ipc = IPCThreadState::self();
113 {
114 Parcel data, reply;
115 // no interface token on this magic transaction
116 data.writeString16(name);
117 data.writeStrongBinder(caller);
118 status_t result = ipc->transact(0 /*magic*/, 0, data, &reply, 0);
119 if (result == NO_ERROR) {
120 object = reply.readStrongBinder();
121 }
122 }
Yifan Hong1e118d22017-01-12 14:42:28 -0800123
Mathias Agopian7922fa22009-05-18 15:08:03 -0700124 ipc->flushCommands();
Yifan Hong1e118d22017-01-12 14:42:28 -0800125
Mathias Agopian7922fa22009-05-18 15:08:03 -0700126 if (object != NULL) setContextObject(object, name);
127 return object;
128}
129
Mathias Agopian7922fa22009-05-18 15:08:03 -0700130void ProcessState::startThreadPool()
131{
132 AutoMutex _l(mLock);
133 if (!mThreadPoolStarted) {
134 mThreadPoolStarted = true;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100135 if (mSpawnThreadOnStart) {
136 spawnPooledThread(true);
137 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700138 }
139}
140
141bool ProcessState::isContextManager(void) const
142{
143 return mManagesContexts;
144}
145
146bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
147{
148 if (!mManagesContexts) {
149 AutoMutex _l(mLock);
150 mBinderContextCheckFunc = checkFunc;
151 mBinderContextUserData = userData;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700152
153 int dummy = 0;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700154 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
Jeff Brownc0f143d2011-07-08 18:52:57 -0700155 if (result == 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700156 mManagesContexts = true;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700157 } else if (result == -1) {
158 mBinderContextCheckFunc = NULL;
159 mBinderContextUserData = NULL;
Steve Blockeafc6212012-01-06 19:20:56 +0000160 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700161 }
162 }
163 return mManagesContexts;
164}
165
166ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
167{
168 const size_t N=mHandleToObject.size();
169 if (N <= (size_t)handle) {
170 handle_entry e;
171 e.binder = NULL;
172 e.refs = NULL;
173 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
174 if (err < NO_ERROR) return NULL;
175 }
176 return &mHandleToObject.editItemAt(handle);
177}
178
179sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
180{
181 sp<IBinder> result;
182
183 AutoMutex _l(mLock);
184
185 handle_entry* e = lookupHandleLocked(handle);
186
187 if (e != NULL) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800188 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700189 // are unable to acquire a weak reference on this current one. See comment
190 // in getWeakProxyForHandle() for more info about this.
191 IBinder* b = e->binder;
192 if (b == NULL || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800193 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700194 e->binder = b;
195 if (b) e->refs = b->getWeakRefs();
196 result = b;
197 } else {
198 // This little bit of nastyness is to allow us to add a primary
199 // reference to the remote proxy when this team doesn't have one
200 // but another team is sending the handle to us.
201 result.force_set(b);
202 e->refs->decWeak(this);
203 }
204 }
205
206 return result;
207}
208
209wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
210{
211 wp<IBinder> result;
212
213 AutoMutex _l(mLock);
214
215 handle_entry* e = lookupHandleLocked(handle);
216
Yifan Hong1e118d22017-01-12 14:42:28 -0800217 if (e != NULL) {
218 // We need to create a new BpHwBinder if there isn't currently one, OR we
Mathias Agopian7922fa22009-05-18 15:08:03 -0700219 // are unable to acquire a weak reference on this current one. The
Yifan Hong1e118d22017-01-12 14:42:28 -0800220 // attemptIncWeak() is safe because we know the BpHwBinder destructor will always
Mathias Agopian7922fa22009-05-18 15:08:03 -0700221 // call expungeHandle(), which acquires the same lock we are holding now.
222 // We need to do this because there is a race condition between someone
Yifan Hong1e118d22017-01-12 14:42:28 -0800223 // releasing a reference on this BpHwBinder, and a new reference on its handle
Mathias Agopian7922fa22009-05-18 15:08:03 -0700224 // arriving from the driver.
225 IBinder* b = e->binder;
226 if (b == NULL || !e->refs->attemptIncWeak(this)) {
Yifan Hong1e118d22017-01-12 14:42:28 -0800227 b = new BpHwBinder(handle);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700228 result = b;
229 e->binder = b;
230 if (b) e->refs = b->getWeakRefs();
231 } else {
232 result = b;
233 e->refs->decWeak(this);
234 }
235 }
236
237 return result;
238}
239
240void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
241{
242 AutoMutex _l(mLock);
Yifan Hong1e118d22017-01-12 14:42:28 -0800243
Mathias Agopian7922fa22009-05-18 15:08:03 -0700244 handle_entry* e = lookupHandleLocked(handle);
245
Yifan Hong1e118d22017-01-12 14:42:28 -0800246 // This handle may have already been replaced with a new BpHwBinder
Mathias Agopian7922fa22009-05-18 15:08:03 -0700247 // (if someone failed the AttemptIncWeak() above); we don't want
248 // to overwrite it.
249 if (e && e->binder == binder) e->binder = NULL;
250}
251
Mathias Agopiand191ed72013-03-07 15:34:28 -0800252String8 ProcessState::makeBinderThreadName() {
253 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadra788e0052016-04-08 10:29:14 -0700254 pid_t pid = getpid();
Mathias Agopiand191ed72013-03-07 15:34:28 -0800255 String8 name;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100256 name.appendFormat("HwBinder:%d_%X", pid, s);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800257 return name;
258}
259
Mathias Agopian7922fa22009-05-18 15:08:03 -0700260void ProcessState::spawnPooledThread(bool isMain)
261{
262 if (mThreadPoolStarted) {
Mathias Agopiand191ed72013-03-07 15:34:28 -0800263 String8 name = makeBinderThreadName();
264 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700265 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800266 t->run(name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700267 }
268}
269
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100270status_t ProcessState::setThreadPoolConfiguration(size_t maxThreads, bool callerJoinsPool) {
271 LOG_ALWAYS_FATAL_IF(maxThreads < 1, "Binder threadpool must have a minimum of one thread.");
Mathias Agopian8297f502012-04-17 16:11:08 -0700272 status_t result = NO_ERROR;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100273 // the BINDER_SET_MAX_THREADS ioctl really tells the kernel how many threads
274 // it's allowed to spawn, *in addition* to any threads we may have already
275 // spawned locally. If 'callerJoinsPool' is true, it means that the caller
276 // will join the threadpool, and so the kernel needs to create one less thread.
277 // If 'callerJoinsPool' is false, we will still spawn a thread locally, and we should
278 // also tell the kernel to create one less thread than what was requested here.
279 size_t kernelMaxThreads = maxThreads - 1;
280 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &kernelMaxThreads) != -1) {
281 AutoMutex _l(mLock);
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700282 mMaxThreads = maxThreads;
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100283 mSpawnThreadOnStart = !callerJoinsPool;
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700284 } else {
Mathias Agopian8297f502012-04-17 16:11:08 -0700285 result = -errno;
286 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
287 }
288 return result;
289}
290
Mathias Agopiand191ed72013-03-07 15:34:28 -0800291void ProcessState::giveThreadPoolName() {
292 androidSetThreadName( makeBinderThreadName().string() );
293}
294
Mathias Agopian7922fa22009-05-18 15:08:03 -0700295static int open_driver()
296{
Martijn Coenena660cbc2016-05-12 11:29:23 +0200297 int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700298 if (fd >= 0) {
Jin Wei023b4682012-10-18 17:00:48 +0800299 int vers = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700300 status_t result = ioctl(fd, BINDER_VERSION, &vers);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700301 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000302 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700303 close(fd);
304 fd = -1;
305 }
306 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Steve Blockeafc6212012-01-06 19:20:56 +0000307 ALOGE("Binder driver protocol does not match user space protocol!");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700308 close(fd);
309 fd = -1;
310 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700311 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700312 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
313 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000314 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700315 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700316 } else {
Iliyan Malcheve3256a62016-09-19 16:00:32 -0700317 ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700318 }
319 return fd;
320}
321
322ProcessState::ProcessState()
323 : mDriverFD(open_driver())
324 , mVMStart(MAP_FAILED)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700325 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
326 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
327 , mExecutingThreadsCount(0)
328 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Crossb1dc6542016-04-15 14:29:55 -0700329 , mStarvationStartTimeMs(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700330 , mManagesContexts(false)
331 , mBinderContextCheckFunc(NULL)
332 , mBinderContextUserData(NULL)
333 , mThreadPoolStarted(false)
Martijn Coenenab00b6a2016-11-22 15:03:13 +0100334 , mSpawnThreadOnStart(true)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700335 , mThreadPoolSeq(1)
336{
337 if (mDriverFD >= 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700338 // mmap the binder, providing a chunk of virtual address space to receive transactions.
339 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
340 if (mVMStart == MAP_FAILED) {
341 // *sigh*
Iliyan Malchevb7e22332016-09-26 00:08:55 -0700342 ALOGE("Using /dev/hwbinder failed: unable to mmap transaction memory.\n");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700343 close(mDriverFD);
344 mDriverFD = -1;
345 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700346 }
Iliyan Malchevb7e22332016-09-26 00:08:55 -0700347 else {
348 ALOGE("Binder driver could not be opened. Terminating.");
349 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700350}
351
352ProcessState::~ProcessState()
353{
zhongjie8e8a0252016-03-09 15:05:04 +0800354 if (mDriverFD >= 0) {
355 if (mVMStart != MAP_FAILED) {
356 munmap(mVMStart, BINDER_VM_SIZE);
357 }
358 close(mDriverFD);
359 }
360 mDriverFD = -1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700361}
Yifan Hong1e118d22017-01-12 14:42:28 -0800362
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200363}; // namespace hardware
Mathias Agopian7922fa22009-05-18 15:08:03 -0700364}; // namespace android