blob: c8e788c8aa7f0ae61e25cec8ec11065815702371 [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
17#define LOG_TAG "ProcessState"
18
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>
Martijn Coenen4080edc2016-05-04 14:17:02 +020022#include <hwbinder/BpBinder.h>
23#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))
Wale Ogunwale2e604f02015-04-13 16:16:10 -070043#define DEFAULT_MAX_BINDER_THREADS 15
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 }
57
58protected:
59 virtual bool threadLoop()
60 {
61 IPCThreadState::self()->joinThreadPool(mIsMain);
62 return false;
63 }
64
65 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();
100
101 //printf("Getting context object %s for %p\n", String8(name).string(), caller.get());
102
103 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 }
111
112 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 }
123
124 ipc->flushCommands();
125
126 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;
135 spawnPooledThread(true);
136 }
137}
138
139bool ProcessState::isContextManager(void) const
140{
141 return mManagesContexts;
142}
143
144bool ProcessState::becomeContextManager(context_check_func checkFunc, void* userData)
145{
146 if (!mManagesContexts) {
147 AutoMutex _l(mLock);
148 mBinderContextCheckFunc = checkFunc;
149 mBinderContextUserData = userData;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700150
151 int dummy = 0;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700152 status_t result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &dummy);
Jeff Brownc0f143d2011-07-08 18:52:57 -0700153 if (result == 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700154 mManagesContexts = true;
Jeff Brownc0f143d2011-07-08 18:52:57 -0700155 } else if (result == -1) {
156 mBinderContextCheckFunc = NULL;
157 mBinderContextUserData = NULL;
Steve Blockeafc6212012-01-06 19:20:56 +0000158 ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700159 }
160 }
161 return mManagesContexts;
162}
163
164ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)
165{
166 const size_t N=mHandleToObject.size();
167 if (N <= (size_t)handle) {
168 handle_entry e;
169 e.binder = NULL;
170 e.refs = NULL;
171 status_t err = mHandleToObject.insertAt(e, N, handle+1-N);
172 if (err < NO_ERROR) return NULL;
173 }
174 return &mHandleToObject.editItemAt(handle);
175}
176
177sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle)
178{
179 sp<IBinder> result;
180
181 AutoMutex _l(mLock);
182
183 handle_entry* e = lookupHandleLocked(handle);
184
185 if (e != NULL) {
186 // We need to create a new BpBinder if there isn't currently one, OR we
187 // are unable to acquire a weak reference on this current one. See comment
188 // in getWeakProxyForHandle() for more info about this.
189 IBinder* b = e->binder;
190 if (b == NULL || !e->refs->attemptIncWeak(this)) {
Todd Poynore6df0c12013-06-18 17:25:37 -0700191 if (handle == 0) {
192 // Special case for context manager...
193 // The context manager is the only object for which we create
194 // a BpBinder proxy without already holding a reference.
195 // Perform a dummy transaction to ensure the context manager
196 // is registered before we create the first local reference
197 // to it (which will occur when creating the BpBinder).
198 // If a local reference is created for the BpBinder when the
199 // context manager is not present, the driver will fail to
200 // provide a reference to the context manager, but the
201 // driver API does not return status.
202 //
203 // Note that this is not race-free if the context manager
204 // dies while this code runs.
205 //
206 // TODO: add a driver API to wait for context manager, or
207 // stop special casing handle 0 for context manager and add
208 // a driver API to get a handle to the context manager with
209 // proper reference counting.
210
211 Parcel data;
212 status_t status = IPCThreadState::self()->transact(
213 0, IBinder::PING_TRANSACTION, data, NULL, 0);
214 if (status == DEAD_OBJECT)
215 return NULL;
216 }
217
Mathias Agopian7922fa22009-05-18 15:08:03 -0700218 b = new BpBinder(handle);
219 e->binder = b;
220 if (b) e->refs = b->getWeakRefs();
221 result = b;
222 } else {
223 // This little bit of nastyness is to allow us to add a primary
224 // reference to the remote proxy when this team doesn't have one
225 // but another team is sending the handle to us.
226 result.force_set(b);
227 e->refs->decWeak(this);
228 }
229 }
230
231 return result;
232}
233
234wp<IBinder> ProcessState::getWeakProxyForHandle(int32_t handle)
235{
236 wp<IBinder> result;
237
238 AutoMutex _l(mLock);
239
240 handle_entry* e = lookupHandleLocked(handle);
241
242 if (e != NULL) {
243 // We need to create a new BpBinder if there isn't currently one, OR we
244 // are unable to acquire a weak reference on this current one. The
245 // attemptIncWeak() is safe because we know the BpBinder destructor will always
246 // call expungeHandle(), which acquires the same lock we are holding now.
247 // We need to do this because there is a race condition between someone
248 // releasing a reference on this BpBinder, and a new reference on its handle
249 // arriving from the driver.
250 IBinder* b = e->binder;
251 if (b == NULL || !e->refs->attemptIncWeak(this)) {
252 b = new BpBinder(handle);
253 result = b;
254 e->binder = b;
255 if (b) e->refs = b->getWeakRefs();
256 } else {
257 result = b;
258 e->refs->decWeak(this);
259 }
260 }
261
262 return result;
263}
264
265void ProcessState::expungeHandle(int32_t handle, IBinder* binder)
266{
267 AutoMutex _l(mLock);
268
269 handle_entry* e = lookupHandleLocked(handle);
270
271 // This handle may have already been replaced with a new BpBinder
272 // (if someone failed the AttemptIncWeak() above); we don't want
273 // to overwrite it.
274 if (e && e->binder == binder) e->binder = NULL;
275}
276
Mathias Agopiand191ed72013-03-07 15:34:28 -0800277String8 ProcessState::makeBinderThreadName() {
278 int32_t s = android_atomic_add(1, &mThreadPoolSeq);
Philip Cuadra788e0052016-04-08 10:29:14 -0700279 pid_t pid = getpid();
Mathias Agopiand191ed72013-03-07 15:34:28 -0800280 String8 name;
Philip Cuadra788e0052016-04-08 10:29:14 -0700281 name.appendFormat("Binder:%d_%X", pid, s);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800282 return name;
283}
284
Mathias Agopian7922fa22009-05-18 15:08:03 -0700285void ProcessState::spawnPooledThread(bool isMain)
286{
287 if (mThreadPoolStarted) {
Mathias Agopiand191ed72013-03-07 15:34:28 -0800288 String8 name = makeBinderThreadName();
289 ALOGV("Spawning new pooled thread, name=%s\n", name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700290 sp<Thread> t = new PoolThread(isMain);
Mathias Agopiand191ed72013-03-07 15:34:28 -0800291 t->run(name.string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700292 }
293}
294
Mathias Agopian8297f502012-04-17 16:11:08 -0700295status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
296 status_t result = NO_ERROR;
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700297 if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
298 mMaxThreads = maxThreads;
299 } else {
Mathias Agopian8297f502012-04-17 16:11:08 -0700300 result = -errno;
301 ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
302 }
303 return result;
304}
305
Mathias Agopiand191ed72013-03-07 15:34:28 -0800306void ProcessState::giveThreadPoolName() {
307 androidSetThreadName( makeBinderThreadName().string() );
308}
309
Mathias Agopian7922fa22009-05-18 15:08:03 -0700310static int open_driver()
311{
Martijn Coenena660cbc2016-05-12 11:29:23 +0200312 int fd = open("/dev/hwbinder", O_RDWR | O_CLOEXEC);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700313 if (fd >= 0) {
Jin Wei023b4682012-10-18 17:00:48 +0800314 int vers = 0;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700315 status_t result = ioctl(fd, BINDER_VERSION, &vers);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700316 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000317 ALOGE("Binder ioctl to obtain version failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700318 close(fd);
319 fd = -1;
320 }
321 if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) {
Steve Blockeafc6212012-01-06 19:20:56 +0000322 ALOGE("Binder driver protocol does not match user space protocol!");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700323 close(fd);
324 fd = -1;
325 }
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700326 size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700327 result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
328 if (result == -1) {
Steve Blockeafc6212012-01-06 19:20:56 +0000329 ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700330 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700331 } else {
Iliyan Malcheve3256a62016-09-19 16:00:32 -0700332 ALOGW("Opening '/dev/hwbinder' failed: %s\n", strerror(errno));
Mathias Agopian7922fa22009-05-18 15:08:03 -0700333 }
334 return fd;
335}
336
337ProcessState::ProcessState()
338 : mDriverFD(open_driver())
339 , mVMStart(MAP_FAILED)
Wale Ogunwale2e604f02015-04-13 16:16:10 -0700340 , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
341 , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
342 , mExecutingThreadsCount(0)
343 , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
Colin Crossb1dc6542016-04-15 14:29:55 -0700344 , mStarvationStartTimeMs(0)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700345 , mManagesContexts(false)
346 , mBinderContextCheckFunc(NULL)
347 , mBinderContextUserData(NULL)
348 , mThreadPoolStarted(false)
349 , mThreadPoolSeq(1)
350{
351 if (mDriverFD >= 0) {
Mathias Agopian7922fa22009-05-18 15:08:03 -0700352 // mmap the binder, providing a chunk of virtual address space to receive transactions.
353 mVMStart = mmap(0, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);
354 if (mVMStart == MAP_FAILED) {
355 // *sigh*
Iliyan Malchevb7e22332016-09-26 00:08:55 -0700356 ALOGE("Using /dev/hwbinder failed: unable to mmap transaction memory.\n");
Mathias Agopian7922fa22009-05-18 15:08:03 -0700357 close(mDriverFD);
358 mDriverFD = -1;
359 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700360 }
Iliyan Malchevb7e22332016-09-26 00:08:55 -0700361 else {
362 ALOGE("Binder driver could not be opened. Terminating.");
363 }
Mathias Agopian7922fa22009-05-18 15:08:03 -0700364}
365
366ProcessState::~ProcessState()
367{
zhongjie8e8a0252016-03-09 15:05:04 +0800368 if (mDriverFD >= 0) {
369 if (mVMStart != MAP_FAILED) {
370 munmap(mVMStart, BINDER_VM_SIZE);
371 }
372 close(mDriverFD);
373 }
374 mDriverFD = -1;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700375}
376
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200377}; // namespace hardware
Mathias Agopian7922fa22009-05-18 15:08:03 -0700378}; // namespace android